libcli/smb: pass down smbXcli_session to smb1cli_req_create/send() and smb1cli_trans*
[Samba/gebeck_regimport.git] / libcli / smb / smbXcli_base.c
blobc41890d6df88d4f2c4b16534d1b35bc35d318909
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 uint16_t session_id;
136 } smb1;
138 struct {
139 uint64_t session_id;
140 uint16_t session_flags;
141 DATA_BLOB application_key;
142 DATA_BLOB signing_key;
143 bool should_sign;
144 bool should_encrypt;
145 DATA_BLOB encryption_key;
146 DATA_BLOB decryption_key;
147 uint64_t channel_nonce;
148 uint64_t channel_next;
149 DATA_BLOB channel_signing_key;
150 } smb2;
153 struct smbXcli_req_state {
154 struct tevent_context *ev;
155 struct smbXcli_conn *conn;
156 struct smbXcli_session *session; /* maybe NULL */
158 uint8_t length_hdr[4];
160 bool one_way;
162 uint8_t *inbuf;
164 struct {
165 /* Space for the header including the wct */
166 uint8_t hdr[HDR_VWV];
169 * For normal requests, smb1cli_req_send chooses a mid.
170 * SecondaryV trans requests need to use the mid of the primary
171 * request, so we need a place to store it.
172 * Assume it is set if != 0.
174 uint16_t mid;
176 uint16_t *vwv;
177 uint8_t bytecount_buf[2];
179 #define MAX_SMB_IOV 10
180 /* length_hdr, hdr, words, byte_count, buffers */
181 struct iovec iov[1 + 3 + MAX_SMB_IOV];
182 int iov_count;
184 bool one_way_seqnum;
185 uint32_t seqnum;
186 struct tevent_req **chained_requests;
188 uint8_t recv_cmd;
189 NTSTATUS recv_status;
190 /* always an array of 3 talloc elements */
191 struct iovec *recv_iov;
192 } smb1;
194 struct {
195 const uint8_t *fixed;
196 uint16_t fixed_len;
197 const uint8_t *dyn;
198 uint32_t dyn_len;
200 uint8_t transform[SMB2_TF_HDR_SIZE];
201 uint8_t hdr[SMB2_HDR_BODY];
202 uint8_t pad[7]; /* padding space for compounding */
205 * always an array of 3 talloc elements
206 * (without a SMB2_TRANSFORM header!)
208 * HDR, BODY, DYN
210 struct iovec *recv_iov;
212 uint16_t credit_charge;
214 bool should_sign;
215 bool should_encrypt;
217 bool signing_skipped;
218 bool notify_async;
219 bool got_async;
220 } smb2;
223 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
226 * NT_STATUS_OK, means we do not notify the callers
228 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
230 while (conn->sessions) {
231 conn->sessions->conn = NULL;
232 DLIST_REMOVE(conn->sessions, conn->sessions);
235 if (conn->smb1.trans_enc) {
236 TALLOC_FREE(conn->smb1.trans_enc);
239 return 0;
242 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
243 int fd,
244 const char *remote_name,
245 enum smb_signing_setting signing_state,
246 uint32_t smb1_capabilities,
247 struct GUID *client_guid,
248 uint32_t smb2_capabilities)
250 struct smbXcli_conn *conn = NULL;
251 void *ss = NULL;
252 struct sockaddr *sa = NULL;
253 socklen_t sa_length;
254 int ret;
256 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
257 if (!conn) {
258 return NULL;
261 conn->read_fd = fd;
262 conn->write_fd = dup(fd);
263 if (conn->write_fd == -1) {
264 goto error;
267 conn->remote_name = talloc_strdup(conn, remote_name);
268 if (conn->remote_name == NULL) {
269 goto error;
273 ss = (void *)&conn->local_ss;
274 sa = (struct sockaddr *)ss;
275 sa_length = sizeof(conn->local_ss);
276 ret = getsockname(fd, sa, &sa_length);
277 if (ret == -1) {
278 goto error;
280 ss = (void *)&conn->remote_ss;
281 sa = (struct sockaddr *)ss;
282 sa_length = sizeof(conn->remote_ss);
283 ret = getpeername(fd, sa, &sa_length);
284 if (ret == -1) {
285 goto error;
288 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
289 if (conn->outgoing == NULL) {
290 goto error;
292 conn->pending = NULL;
294 conn->protocol = PROTOCOL_NONE;
296 switch (signing_state) {
297 case SMB_SIGNING_OFF:
298 /* never */
299 conn->allow_signing = false;
300 conn->desire_signing = false;
301 conn->mandatory_signing = false;
302 break;
303 case SMB_SIGNING_DEFAULT:
304 case SMB_SIGNING_IF_REQUIRED:
305 /* if the server requires it */
306 conn->allow_signing = true;
307 conn->desire_signing = false;
308 conn->mandatory_signing = false;
309 break;
310 case SMB_SIGNING_REQUIRED:
311 /* always */
312 conn->allow_signing = true;
313 conn->desire_signing = true;
314 conn->mandatory_signing = true;
315 break;
318 conn->smb1.client.capabilities = smb1_capabilities;
319 conn->smb1.client.max_xmit = UINT16_MAX;
321 conn->smb1.capabilities = conn->smb1.client.capabilities;
322 conn->smb1.max_xmit = 1024;
324 conn->smb1.mid = 1;
326 /* initialise signing */
327 conn->smb1.signing = smb_signing_init(conn,
328 conn->allow_signing,
329 conn->desire_signing,
330 conn->mandatory_signing);
331 if (!conn->smb1.signing) {
332 goto error;
335 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
336 if (conn->mandatory_signing) {
337 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
339 if (client_guid) {
340 conn->smb2.client.guid = *client_guid;
342 conn->smb2.client.capabilities = smb2_capabilities;
344 conn->smb2.cur_credits = 1;
345 conn->smb2.max_credits = 0;
347 talloc_set_destructor(conn, smbXcli_conn_destructor);
348 return conn;
350 error:
351 if (conn->write_fd != -1) {
352 close(conn->write_fd);
354 TALLOC_FREE(conn);
355 return NULL;
358 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
360 if (conn == NULL) {
361 return false;
364 if (conn->read_fd == -1) {
365 return false;
368 return true;
371 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
373 return conn->protocol;
376 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
378 if (conn->protocol >= PROTOCOL_SMB2_02) {
379 return true;
382 if (conn->smb1.capabilities & CAP_UNICODE) {
383 return true;
386 return false;
389 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
391 set_socket_options(conn->read_fd, options);
394 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
396 return &conn->local_ss;
399 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
401 return &conn->remote_ss;
404 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
406 return conn->remote_name;
409 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
411 if (conn->protocol >= PROTOCOL_SMB2_02) {
413 * TODO...
415 return 1;
418 return conn->smb1.server.max_mux;
421 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
423 if (conn->protocol >= PROTOCOL_SMB2_02) {
424 return conn->smb2.server.system_time;
427 return conn->smb1.server.system_time;
430 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
432 if (conn->protocol >= PROTOCOL_SMB2_02) {
433 return &conn->smb2.server.gss_blob;
436 return &conn->smb1.server.gss_blob;
439 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
441 if (conn->protocol >= PROTOCOL_SMB2_02) {
442 return &conn->smb2.server.guid;
445 return &conn->smb1.server.guid;
448 struct smbXcli_conn_samba_suicide_state {
449 struct smbXcli_conn *conn;
450 struct iovec iov;
451 uint8_t buf[9];
454 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
456 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
457 struct tevent_context *ev,
458 struct smbXcli_conn *conn,
459 uint8_t exitcode)
461 struct tevent_req *req, *subreq;
462 struct smbXcli_conn_samba_suicide_state *state;
464 req = tevent_req_create(mem_ctx, &state,
465 struct smbXcli_conn_samba_suicide_state);
466 if (req == NULL) {
467 return NULL;
469 state->conn = conn;
470 SIVAL(state->buf, 4, 0x74697865);
471 SCVAL(state->buf, 8, exitcode);
472 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
474 state->iov.iov_base = state->buf;
475 state->iov.iov_len = sizeof(state->buf);
477 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
478 false, &state->iov, 1);
479 if (tevent_req_nomem(subreq, req)) {
480 return tevent_req_post(req, ev);
482 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
483 return req;
486 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
488 struct tevent_req *req = tevent_req_callback_data(
489 subreq, struct tevent_req);
490 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
491 req, struct smbXcli_conn_samba_suicide_state);
492 ssize_t nwritten;
493 int err;
495 nwritten = writev_recv(subreq, &err);
496 TALLOC_FREE(subreq);
497 if (nwritten == -1) {
498 NTSTATUS status = map_nt_error_from_unix_common(err);
499 smbXcli_conn_disconnect(state->conn, status);
500 return;
502 tevent_req_done(req);
505 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
507 return tevent_req_simple_recv_ntstatus(req);
510 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
511 uint8_t exitcode)
513 TALLOC_CTX *frame = talloc_stackframe();
514 struct tevent_context *ev;
515 struct tevent_req *req;
516 NTSTATUS status = NT_STATUS_NO_MEMORY;
517 bool ok;
519 if (smbXcli_conn_has_async_calls(conn)) {
521 * Can't use sync call while an async call is in flight
523 status = NT_STATUS_INVALID_PARAMETER_MIX;
524 goto fail;
526 ev = tevent_context_init(frame);
527 if (ev == NULL) {
528 goto fail;
530 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
531 if (req == NULL) {
532 goto fail;
534 ok = tevent_req_poll(req, ev);
535 if (!ok) {
536 status = map_nt_error_from_unix_common(errno);
537 goto fail;
539 status = smbXcli_conn_samba_suicide_recv(req);
540 fail:
541 TALLOC_FREE(frame);
542 return status;
545 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
547 return conn->smb1.capabilities;
550 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
552 return conn->smb1.max_xmit;
555 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
557 return conn->smb1.server.session_key;
560 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
562 return conn->smb1.server.challenge;
565 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
567 return conn->smb1.server.security_mode;
570 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
572 return conn->smb1.server.readbraw;
575 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
577 return conn->smb1.server.writebraw;
580 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
582 return conn->smb1.server.lockread;
585 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
587 return conn->smb1.server.writeunlock;
590 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
592 return conn->smb1.server.time_zone;
595 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
596 const DATA_BLOB user_session_key,
597 const DATA_BLOB response)
599 return smb_signing_activate(conn->smb1.signing,
600 user_session_key,
601 response);
604 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
605 const uint8_t *buf, uint32_t seqnum)
607 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
610 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
612 return smb_signing_is_active(conn->smb1.signing);
615 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
616 struct smb_trans_enc_state *es)
618 /* Replace the old state, if any. */
619 if (conn->smb1.trans_enc) {
620 TALLOC_FREE(conn->smb1.trans_enc);
622 conn->smb1.trans_enc = es;
625 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
627 return common_encryption_on(conn->smb1.trans_enc);
631 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
633 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
634 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
636 if (NT_STATUS_IS_OK(status)) {
637 return NT_STATUS_OK;
640 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
641 return status;
644 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
648 * Is the SMB command able to hold an AND_X successor
649 * @param[in] cmd The SMB command in question
650 * @retval Can we add a chained request after "cmd"?
652 bool smb1cli_is_andx_req(uint8_t cmd)
654 switch (cmd) {
655 case SMBtconX:
656 case SMBlockingX:
657 case SMBopenX:
658 case SMBreadX:
659 case SMBwriteX:
660 case SMBsesssetupX:
661 case SMBulogoffX:
662 case SMBntcreateX:
663 return true;
664 break;
665 default:
666 break;
669 return false;
672 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
674 size_t num_pending = talloc_array_length(conn->pending);
675 uint16_t result;
677 while (true) {
678 size_t i;
680 result = conn->smb1.mid++;
681 if ((result == 0) || (result == 0xffff)) {
682 continue;
685 for (i=0; i<num_pending; i++) {
686 if (result == smb1cli_req_mid(conn->pending[i])) {
687 break;
691 if (i == num_pending) {
692 return result;
697 void smbXcli_req_unset_pending(struct tevent_req *req)
699 struct smbXcli_req_state *state =
700 tevent_req_data(req,
701 struct smbXcli_req_state);
702 struct smbXcli_conn *conn = state->conn;
703 size_t num_pending = talloc_array_length(conn->pending);
704 size_t i;
706 if (state->smb1.mid != 0) {
708 * This is a [nt]trans[2] request which waits
709 * for more than one reply.
711 return;
714 talloc_set_destructor(req, NULL);
716 if (num_pending == 1) {
718 * The pending read_smb tevent_req is a child of
719 * conn->pending. So if nothing is pending anymore, we need to
720 * delete the socket read fde.
722 TALLOC_FREE(conn->pending);
723 conn->read_smb_req = NULL;
724 return;
727 for (i=0; i<num_pending; i++) {
728 if (req == conn->pending[i]) {
729 break;
732 if (i == num_pending) {
734 * Something's seriously broken. Just returning here is the
735 * right thing nevertheless, the point of this routine is to
736 * remove ourselves from conn->pending.
738 return;
742 * Remove ourselves from the conn->pending array
744 for (; i < (num_pending - 1); i++) {
745 conn->pending[i] = conn->pending[i+1];
749 * No NULL check here, we're shrinking by sizeof(void *), and
750 * talloc_realloc just adjusts the size for this.
752 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
753 num_pending - 1);
754 return;
757 static int smbXcli_req_destructor(struct tevent_req *req)
759 struct smbXcli_req_state *state =
760 tevent_req_data(req,
761 struct smbXcli_req_state);
764 * Make sure we really remove it from
765 * the pending array on destruction.
767 state->smb1.mid = 0;
768 smbXcli_req_unset_pending(req);
769 return 0;
772 static bool smb1cli_req_cancel(struct tevent_req *req);
773 static bool smb2cli_req_cancel(struct tevent_req *req);
775 static bool smbXcli_req_cancel(struct tevent_req *req)
777 struct smbXcli_req_state *state =
778 tevent_req_data(req,
779 struct smbXcli_req_state);
781 if (!smbXcli_conn_is_connected(state->conn)) {
782 return false;
785 if (state->conn->protocol == PROTOCOL_NONE) {
786 return false;
789 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
790 return smb2cli_req_cancel(req);
793 return smb1cli_req_cancel(req);
796 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
798 bool smbXcli_req_set_pending(struct tevent_req *req)
800 struct smbXcli_req_state *state =
801 tevent_req_data(req,
802 struct smbXcli_req_state);
803 struct smbXcli_conn *conn;
804 struct tevent_req **pending;
805 size_t num_pending;
807 conn = state->conn;
809 if (!smbXcli_conn_is_connected(conn)) {
810 return false;
813 num_pending = talloc_array_length(conn->pending);
815 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
816 num_pending+1);
817 if (pending == NULL) {
818 return false;
820 pending[num_pending] = req;
821 conn->pending = pending;
822 talloc_set_destructor(req, smbXcli_req_destructor);
823 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
825 if (!smbXcli_conn_receive_next(conn)) {
827 * the caller should notify the current request
829 * And all other pending requests get notified
830 * by smbXcli_conn_disconnect().
832 smbXcli_req_unset_pending(req);
833 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
834 return false;
837 return true;
840 static void smbXcli_conn_received(struct tevent_req *subreq);
842 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
844 size_t num_pending = talloc_array_length(conn->pending);
845 struct tevent_req *req;
846 struct smbXcli_req_state *state;
848 if (conn->read_smb_req != NULL) {
849 return true;
852 if (num_pending == 0) {
853 if (conn->smb2.mid < UINT64_MAX) {
854 /* no more pending requests, so we are done for now */
855 return true;
859 * If there are no more SMB2 requests possible,
860 * because we are out of message ids,
861 * we need to disconnect.
863 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
864 return true;
867 req = conn->pending[0];
868 state = tevent_req_data(req, struct smbXcli_req_state);
871 * We're the first ones, add the read_smb request that waits for the
872 * answer from the server
874 conn->read_smb_req = read_smb_send(conn->pending,
875 state->ev,
876 conn->read_fd);
877 if (conn->read_smb_req == NULL) {
878 return false;
880 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
881 return true;
884 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
886 tevent_queue_stop(conn->outgoing);
888 if (conn->read_fd != -1) {
889 close(conn->read_fd);
891 if (conn->write_fd != -1) {
892 close(conn->write_fd);
894 conn->read_fd = -1;
895 conn->write_fd = -1;
898 * Cancel all pending requests. We do not do a for-loop walking
899 * conn->pending because that array changes in
900 * smbXcli_req_unset_pending.
902 while (talloc_array_length(conn->pending) > 0) {
903 struct tevent_req *req;
904 struct smbXcli_req_state *state;
905 struct tevent_req **chain;
906 size_t num_chained;
907 size_t i;
909 req = conn->pending[0];
910 state = tevent_req_data(req, struct smbXcli_req_state);
912 if (state->smb1.chained_requests == NULL) {
914 * We're dead. No point waiting for trans2
915 * replies.
917 state->smb1.mid = 0;
919 smbXcli_req_unset_pending(req);
921 if (NT_STATUS_IS_OK(status)) {
922 /* do not notify the callers */
923 continue;
927 * we need to defer the callback, because we may notify
928 * more then one caller.
930 tevent_req_defer_callback(req, state->ev);
931 tevent_req_nterror(req, status);
932 continue;
935 chain = talloc_move(conn, &state->smb1.chained_requests);
936 num_chained = talloc_array_length(chain);
938 for (i=0; i<num_chained; i++) {
939 req = chain[i];
940 state = tevent_req_data(req, struct smbXcli_req_state);
943 * We're dead. No point waiting for trans2
944 * replies.
946 state->smb1.mid = 0;
948 smbXcli_req_unset_pending(req);
950 if (NT_STATUS_IS_OK(status)) {
951 /* do not notify the callers */
952 continue;
956 * we need to defer the callback, because we may notify
957 * more than one caller.
959 tevent_req_defer_callback(req, state->ev);
960 tevent_req_nterror(req, status);
962 TALLOC_FREE(chain);
967 * Fetch a smb request's mid. Only valid after the request has been sent by
968 * smb1cli_req_send().
970 uint16_t smb1cli_req_mid(struct tevent_req *req)
972 struct smbXcli_req_state *state =
973 tevent_req_data(req,
974 struct smbXcli_req_state);
976 if (state->smb1.mid != 0) {
977 return state->smb1.mid;
980 return SVAL(state->smb1.hdr, HDR_MID);
983 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
985 struct smbXcli_req_state *state =
986 tevent_req_data(req,
987 struct smbXcli_req_state);
989 state->smb1.mid = mid;
992 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
994 struct smbXcli_req_state *state =
995 tevent_req_data(req,
996 struct smbXcli_req_state);
998 return state->smb1.seqnum;
1001 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1003 struct smbXcli_req_state *state =
1004 tevent_req_data(req,
1005 struct smbXcli_req_state);
1007 state->smb1.seqnum = seqnum;
1010 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1012 size_t result = 0;
1013 int i;
1014 for (i=0; i<count; i++) {
1015 result += iov[i].iov_len;
1017 return result;
1020 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1021 const struct iovec *iov,
1022 int count)
1024 size_t len = smbXcli_iov_len(iov, count);
1025 size_t copied;
1026 uint8_t *buf;
1027 int i;
1029 buf = talloc_array(mem_ctx, uint8_t, len);
1030 if (buf == NULL) {
1031 return NULL;
1033 copied = 0;
1034 for (i=0; i<count; i++) {
1035 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1036 copied += iov[i].iov_len;
1038 return buf;
1041 static void smb1cli_req_flags(enum protocol_types protocol,
1042 uint32_t smb1_capabilities,
1043 uint8_t smb_command,
1044 uint8_t additional_flags,
1045 uint8_t clear_flags,
1046 uint8_t *_flags,
1047 uint16_t additional_flags2,
1048 uint16_t clear_flags2,
1049 uint16_t *_flags2)
1051 uint8_t flags = 0;
1052 uint16_t flags2 = 0;
1054 if (protocol >= PROTOCOL_LANMAN1) {
1055 flags |= FLAG_CASELESS_PATHNAMES;
1056 flags |= FLAG_CANONICAL_PATHNAMES;
1059 if (protocol >= PROTOCOL_LANMAN2) {
1060 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1061 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1064 if (protocol >= PROTOCOL_NT1) {
1065 flags2 |= FLAGS2_IS_LONG_NAME;
1067 if (smb1_capabilities & CAP_UNICODE) {
1068 flags2 |= FLAGS2_UNICODE_STRINGS;
1070 if (smb1_capabilities & CAP_STATUS32) {
1071 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1073 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1074 flags2 |= FLAGS2_EXTENDED_SECURITY;
1078 flags |= additional_flags;
1079 flags &= ~clear_flags;
1080 flags2 |= additional_flags2;
1081 flags2 &= ~clear_flags2;
1083 *_flags = flags;
1084 *_flags2 = flags2;
1087 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1089 static bool smb1cli_req_cancel(struct tevent_req *req)
1091 struct smbXcli_req_state *state =
1092 tevent_req_data(req,
1093 struct smbXcli_req_state);
1094 uint8_t flags;
1095 uint16_t flags2;
1096 uint32_t pid;
1097 uint16_t tid;
1098 uint16_t mid;
1099 struct tevent_req *subreq;
1100 NTSTATUS status;
1102 flags = CVAL(state->smb1.hdr, HDR_FLG);
1103 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1104 pid = SVAL(state->smb1.hdr, HDR_PID);
1105 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1106 tid = SVAL(state->smb1.hdr, HDR_TID);
1107 mid = SVAL(state->smb1.hdr, HDR_MID);
1109 subreq = smb1cli_req_create(state, state->ev,
1110 state->conn,
1111 SMBntcancel,
1112 flags, 0,
1113 flags2, 0,
1114 0, /* timeout */
1115 pid, tid,
1116 state->session,
1117 0, NULL, /* vwv */
1118 0, NULL); /* bytes */
1119 if (subreq == NULL) {
1120 return false;
1122 smb1cli_req_set_mid(subreq, mid);
1124 status = smb1cli_req_chain_submit(&subreq, 1);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 TALLOC_FREE(subreq);
1127 return false;
1129 smb1cli_req_set_mid(subreq, 0);
1131 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1133 return true;
1136 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1138 /* we do not care about the result */
1139 TALLOC_FREE(subreq);
1142 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1143 struct tevent_context *ev,
1144 struct smbXcli_conn *conn,
1145 uint8_t smb_command,
1146 uint8_t additional_flags,
1147 uint8_t clear_flags,
1148 uint16_t additional_flags2,
1149 uint16_t clear_flags2,
1150 uint32_t timeout_msec,
1151 uint32_t pid,
1152 uint16_t tid,
1153 struct smbXcli_session *session,
1154 uint8_t wct, uint16_t *vwv,
1155 int iov_count,
1156 struct iovec *bytes_iov)
1158 struct tevent_req *req;
1159 struct smbXcli_req_state *state;
1160 uint8_t flags = 0;
1161 uint16_t flags2 = 0;
1162 uint16_t uid = 0;
1164 if (iov_count > MAX_SMB_IOV) {
1166 * Should not happen :-)
1168 return NULL;
1171 req = tevent_req_create(mem_ctx, &state,
1172 struct smbXcli_req_state);
1173 if (req == NULL) {
1174 return NULL;
1176 state->ev = ev;
1177 state->conn = conn;
1178 state->session = session;
1180 if (session) {
1181 uid = session->smb1.session_id;
1184 state->smb1.recv_cmd = 0xFF;
1185 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1186 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1187 if (state->smb1.recv_iov == NULL) {
1188 TALLOC_FREE(req);
1189 return NULL;
1192 smb1cli_req_flags(conn->protocol,
1193 conn->smb1.capabilities,
1194 smb_command,
1195 additional_flags,
1196 clear_flags,
1197 &flags,
1198 additional_flags2,
1199 clear_flags2,
1200 &flags2);
1202 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1203 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1204 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1205 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1206 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1207 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1208 SSVAL(state->smb1.hdr, HDR_TID, tid);
1209 SSVAL(state->smb1.hdr, HDR_PID, pid);
1210 SSVAL(state->smb1.hdr, HDR_UID, uid);
1211 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1212 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1214 state->smb1.vwv = vwv;
1216 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1218 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1219 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1220 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1221 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1222 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1223 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1224 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1225 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1227 if (iov_count != 0) {
1228 memcpy(&state->smb1.iov[4], bytes_iov,
1229 iov_count * sizeof(*bytes_iov));
1231 state->smb1.iov_count = iov_count + 4;
1233 if (timeout_msec > 0) {
1234 struct timeval endtime;
1236 endtime = timeval_current_ofs_msec(timeout_msec);
1237 if (!tevent_req_set_endtime(req, ev, endtime)) {
1238 return req;
1242 switch (smb_command) {
1243 case SMBtranss:
1244 case SMBtranss2:
1245 case SMBnttranss:
1246 state->one_way = true;
1247 break;
1248 case SMBntcancel:
1249 state->one_way = true;
1250 state->smb1.one_way_seqnum = true;
1251 break;
1252 case SMBlockingX:
1253 if ((wct == 8) &&
1254 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1255 state->one_way = true;
1257 break;
1260 return req;
1263 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1264 struct iovec *iov, int iov_count,
1265 uint32_t *seqnum,
1266 bool one_way_seqnum)
1268 TALLOC_CTX *frame = NULL;
1269 uint8_t *buf;
1272 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1273 * iovec directly. MD5Update would do that just fine.
1276 if (iov_count < 4) {
1277 return NT_STATUS_INVALID_PARAMETER_MIX;
1279 if (iov[0].iov_len != NBT_HDR_SIZE) {
1280 return NT_STATUS_INVALID_PARAMETER_MIX;
1282 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1283 return NT_STATUS_INVALID_PARAMETER_MIX;
1285 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1286 return NT_STATUS_INVALID_PARAMETER_MIX;
1288 if (iov[3].iov_len != sizeof(uint16_t)) {
1289 return NT_STATUS_INVALID_PARAMETER_MIX;
1292 frame = talloc_stackframe();
1294 buf = smbXcli_iov_concat(frame, iov, iov_count);
1295 if (buf == NULL) {
1296 return NT_STATUS_NO_MEMORY;
1299 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1300 one_way_seqnum);
1301 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1302 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1304 TALLOC_FREE(frame);
1305 return NT_STATUS_OK;
1308 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1309 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1310 TALLOC_CTX *tmp_mem,
1311 uint8_t *inbuf);
1313 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1314 struct smbXcli_req_state *state,
1315 struct iovec *iov, int iov_count)
1317 struct tevent_req *subreq;
1318 NTSTATUS status;
1319 uint8_t cmd;
1320 uint16_t mid;
1322 if (!smbXcli_conn_is_connected(state->conn)) {
1323 return NT_STATUS_CONNECTION_DISCONNECTED;
1326 if (state->conn->protocol > PROTOCOL_NT1) {
1327 return NT_STATUS_REVISION_MISMATCH;
1330 if (iov_count < 4) {
1331 return NT_STATUS_INVALID_PARAMETER_MIX;
1333 if (iov[0].iov_len != NBT_HDR_SIZE) {
1334 return NT_STATUS_INVALID_PARAMETER_MIX;
1336 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1337 return NT_STATUS_INVALID_PARAMETER_MIX;
1339 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1340 return NT_STATUS_INVALID_PARAMETER_MIX;
1342 if (iov[3].iov_len != sizeof(uint16_t)) {
1343 return NT_STATUS_INVALID_PARAMETER_MIX;
1346 cmd = CVAL(iov[1].iov_base, HDR_COM);
1347 if (cmd == SMBreadBraw) {
1348 if (smbXcli_conn_has_async_calls(state->conn)) {
1349 return NT_STATUS_INVALID_PARAMETER_MIX;
1351 state->conn->smb1.read_braw_req = req;
1354 if (state->smb1.mid != 0) {
1355 mid = state->smb1.mid;
1356 } else {
1357 mid = smb1cli_alloc_mid(state->conn);
1359 SSVAL(iov[1].iov_base, HDR_MID, mid);
1361 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1363 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1364 &state->smb1.seqnum,
1365 state->smb1.one_way_seqnum);
1367 if (!NT_STATUS_IS_OK(status)) {
1368 return status;
1372 * If we supported multiple encrytion contexts
1373 * here we'd look up based on tid.
1375 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1376 char *buf, *enc_buf;
1378 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1379 if (buf == NULL) {
1380 return NT_STATUS_NO_MEMORY;
1382 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1383 (char *)buf, &enc_buf);
1384 TALLOC_FREE(buf);
1385 if (!NT_STATUS_IS_OK(status)) {
1386 DEBUG(0, ("Error in encrypting client message: %s\n",
1387 nt_errstr(status)));
1388 return status;
1390 buf = (char *)talloc_memdup(state, enc_buf,
1391 smb_len_nbt(enc_buf)+4);
1392 SAFE_FREE(enc_buf);
1393 if (buf == NULL) {
1394 return NT_STATUS_NO_MEMORY;
1396 iov[0].iov_base = (void *)buf;
1397 iov[0].iov_len = talloc_get_size(buf);
1398 iov_count = 1;
1401 if (state->conn->dispatch_incoming == NULL) {
1402 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1405 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1407 subreq = writev_send(state, state->ev, state->conn->outgoing,
1408 state->conn->write_fd, false, iov, iov_count);
1409 if (subreq == NULL) {
1410 return NT_STATUS_NO_MEMORY;
1412 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1413 return NT_STATUS_OK;
1416 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1417 struct tevent_context *ev,
1418 struct smbXcli_conn *conn,
1419 uint8_t smb_command,
1420 uint8_t additional_flags,
1421 uint8_t clear_flags,
1422 uint16_t additional_flags2,
1423 uint16_t clear_flags2,
1424 uint32_t timeout_msec,
1425 uint32_t pid,
1426 uint16_t tid,
1427 struct smbXcli_session *session,
1428 uint8_t wct, uint16_t *vwv,
1429 uint32_t num_bytes,
1430 const uint8_t *bytes)
1432 struct tevent_req *req;
1433 struct iovec iov;
1434 NTSTATUS status;
1436 iov.iov_base = discard_const_p(void, bytes);
1437 iov.iov_len = num_bytes;
1439 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1440 additional_flags, clear_flags,
1441 additional_flags2, clear_flags2,
1442 timeout_msec,
1443 pid, tid, session,
1444 wct, vwv, 1, &iov);
1445 if (req == NULL) {
1446 return NULL;
1448 if (!tevent_req_is_in_progress(req)) {
1449 return tevent_req_post(req, ev);
1451 status = smb1cli_req_chain_submit(&req, 1);
1452 if (tevent_req_nterror(req, status)) {
1453 return tevent_req_post(req, ev);
1455 return req;
1458 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1460 struct tevent_req *req =
1461 tevent_req_callback_data(subreq,
1462 struct tevent_req);
1463 struct smbXcli_req_state *state =
1464 tevent_req_data(req,
1465 struct smbXcli_req_state);
1466 ssize_t nwritten;
1467 int err;
1469 nwritten = writev_recv(subreq, &err);
1470 TALLOC_FREE(subreq);
1471 if (nwritten == -1) {
1472 NTSTATUS status = map_nt_error_from_unix_common(err);
1473 smbXcli_conn_disconnect(state->conn, status);
1474 return;
1477 if (state->one_way) {
1478 state->inbuf = NULL;
1479 tevent_req_done(req);
1480 return;
1483 if (!smbXcli_req_set_pending(req)) {
1484 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1485 return;
1489 static void smbXcli_conn_received(struct tevent_req *subreq)
1491 struct smbXcli_conn *conn =
1492 tevent_req_callback_data(subreq,
1493 struct smbXcli_conn);
1494 TALLOC_CTX *frame = talloc_stackframe();
1495 NTSTATUS status;
1496 uint8_t *inbuf;
1497 ssize_t received;
1498 int err;
1500 if (subreq != conn->read_smb_req) {
1501 DEBUG(1, ("Internal error: cli_smb_received called with "
1502 "unexpected subreq\n"));
1503 status = NT_STATUS_INTERNAL_ERROR;
1504 smbXcli_conn_disconnect(conn, status);
1505 TALLOC_FREE(frame);
1506 return;
1508 conn->read_smb_req = NULL;
1510 received = read_smb_recv(subreq, frame, &inbuf, &err);
1511 TALLOC_FREE(subreq);
1512 if (received == -1) {
1513 status = map_nt_error_from_unix_common(err);
1514 smbXcli_conn_disconnect(conn, status);
1515 TALLOC_FREE(frame);
1516 return;
1519 status = conn->dispatch_incoming(conn, frame, inbuf);
1520 TALLOC_FREE(frame);
1521 if (NT_STATUS_IS_OK(status)) {
1523 * We should not do any more processing
1524 * as the dispatch function called
1525 * tevent_req_done().
1527 return;
1528 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1530 * We got an error, so notify all pending requests
1532 smbXcli_conn_disconnect(conn, status);
1533 return;
1537 * We got NT_STATUS_RETRY, so we may ask for a
1538 * next incoming pdu.
1540 if (!smbXcli_conn_receive_next(conn)) {
1541 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1545 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1546 struct iovec **piov, int *pnum_iov)
1548 struct iovec *iov;
1549 int num_iov;
1550 size_t buflen;
1551 size_t taken;
1552 size_t remaining;
1553 uint8_t *hdr;
1554 uint8_t cmd;
1555 uint32_t wct_ofs;
1557 buflen = smb_len_nbt(buf);
1558 taken = 0;
1560 hdr = buf + NBT_HDR_SIZE;
1562 if (buflen < MIN_SMB_SIZE) {
1563 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1567 * This returns iovec elements in the following order:
1569 * - SMB header
1571 * - Parameter Block
1572 * - Data Block
1574 * - Parameter Block
1575 * - Data Block
1577 * - Parameter Block
1578 * - Data Block
1580 num_iov = 1;
1582 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1583 if (iov == NULL) {
1584 return NT_STATUS_NO_MEMORY;
1586 iov[0].iov_base = hdr;
1587 iov[0].iov_len = HDR_WCT;
1588 taken += HDR_WCT;
1590 cmd = CVAL(hdr, HDR_COM);
1591 wct_ofs = HDR_WCT;
1593 while (true) {
1594 size_t len = buflen - taken;
1595 struct iovec *cur;
1596 struct iovec *iov_tmp;
1597 uint8_t wct;
1598 uint32_t bcc_ofs;
1599 uint16_t bcc;
1600 size_t needed;
1603 * we need at least WCT and BCC
1605 needed = sizeof(uint8_t) + sizeof(uint16_t);
1606 if (len < needed) {
1607 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1608 __location__, (int)len, (int)needed));
1609 goto inval;
1613 * Now we check if the specified words are there
1615 wct = CVAL(hdr, wct_ofs);
1616 needed += wct * sizeof(uint16_t);
1617 if (len < needed) {
1618 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1619 __location__, (int)len, (int)needed));
1620 goto inval;
1624 * Now we check if the specified bytes are there
1626 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1627 bcc = SVAL(hdr, bcc_ofs);
1628 needed += bcc * sizeof(uint8_t);
1629 if (len < needed) {
1630 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1631 __location__, (int)len, (int)needed));
1632 goto inval;
1636 * we allocate 2 iovec structures for words and bytes
1638 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1639 num_iov + 2);
1640 if (iov_tmp == NULL) {
1641 TALLOC_FREE(iov);
1642 return NT_STATUS_NO_MEMORY;
1644 iov = iov_tmp;
1645 cur = &iov[num_iov];
1646 num_iov += 2;
1648 cur[0].iov_len = wct * sizeof(uint16_t);
1649 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1650 cur[1].iov_len = bcc * sizeof(uint8_t);
1651 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1653 taken += needed;
1655 if (!smb1cli_is_andx_req(cmd)) {
1657 * If the current command does not have AndX chanining
1658 * we are done.
1660 break;
1663 if (wct == 0 && bcc == 0) {
1665 * An empty response also ends the chain,
1666 * most likely with an error.
1668 break;
1671 if (wct < 2) {
1672 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1673 __location__, (int)wct, (int)cmd));
1674 goto inval;
1676 cmd = CVAL(cur[0].iov_base, 0);
1677 if (cmd == 0xFF) {
1679 * If it is the end of the chain we are also done.
1681 break;
1683 wct_ofs = SVAL(cur[0].iov_base, 2);
1685 if (wct_ofs < taken) {
1686 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1688 if (wct_ofs > buflen) {
1689 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1693 * we consumed everything up to the start of the next
1694 * parameter block.
1696 taken = wct_ofs;
1699 remaining = buflen - taken;
1701 if (remaining > 0 && num_iov >= 3) {
1703 * The last DATA block gets the remaining
1704 * bytes, this is needed to support
1705 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1707 iov[num_iov-1].iov_len += remaining;
1710 *piov = iov;
1711 *pnum_iov = num_iov;
1712 return NT_STATUS_OK;
1714 inval:
1715 TALLOC_FREE(iov);
1716 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1719 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1720 TALLOC_CTX *tmp_mem,
1721 uint8_t *inbuf)
1723 struct tevent_req *req;
1724 struct smbXcli_req_state *state;
1725 NTSTATUS status;
1726 size_t num_pending;
1727 size_t i;
1728 uint8_t cmd;
1729 uint16_t mid;
1730 bool oplock_break;
1731 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1732 struct iovec *iov = NULL;
1733 int num_iov = 0;
1734 struct tevent_req **chain = NULL;
1735 size_t num_chained = 0;
1736 size_t num_responses = 0;
1738 if (conn->smb1.read_braw_req != NULL) {
1739 req = conn->smb1.read_braw_req;
1740 conn->smb1.read_braw_req = NULL;
1741 state = tevent_req_data(req, struct smbXcli_req_state);
1743 smbXcli_req_unset_pending(req);
1745 if (state->smb1.recv_iov == NULL) {
1747 * For requests with more than
1748 * one response, we have to readd the
1749 * recv_iov array.
1751 state->smb1.recv_iov = talloc_zero_array(state,
1752 struct iovec,
1754 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1755 return NT_STATUS_OK;
1759 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1760 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1761 ZERO_STRUCT(state->smb1.recv_iov[1]);
1762 ZERO_STRUCT(state->smb1.recv_iov[2]);
1764 state->smb1.recv_cmd = SMBreadBraw;
1765 state->smb1.recv_status = NT_STATUS_OK;
1766 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1768 tevent_req_done(req);
1769 return NT_STATUS_OK;
1772 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1773 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1774 DEBUG(10, ("Got non-SMB PDU\n"));
1775 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1779 * If we supported multiple encrytion contexts
1780 * here we'd look up based on tid.
1782 if (common_encryption_on(conn->smb1.trans_enc)
1783 && (CVAL(inbuf, 0) == 0)) {
1784 uint16_t enc_ctx_num;
1786 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1787 if (!NT_STATUS_IS_OK(status)) {
1788 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1789 nt_errstr(status)));
1790 return status;
1793 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1794 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1795 enc_ctx_num,
1796 conn->smb1.trans_enc->enc_ctx_num));
1797 return NT_STATUS_INVALID_HANDLE;
1800 status = common_decrypt_buffer(conn->smb1.trans_enc,
1801 (char *)inbuf);
1802 if (!NT_STATUS_IS_OK(status)) {
1803 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1804 nt_errstr(status)));
1805 return status;
1809 mid = SVAL(inhdr, HDR_MID);
1810 num_pending = talloc_array_length(conn->pending);
1812 for (i=0; i<num_pending; i++) {
1813 if (mid == smb1cli_req_mid(conn->pending[i])) {
1814 break;
1817 if (i == num_pending) {
1818 /* Dump unexpected reply */
1819 return NT_STATUS_RETRY;
1822 oplock_break = false;
1824 if (mid == 0xffff) {
1826 * Paranoia checks that this is really an oplock break request.
1828 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1829 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1830 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1831 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1832 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1834 if (!oplock_break) {
1835 /* Dump unexpected reply */
1836 return NT_STATUS_RETRY;
1840 req = conn->pending[i];
1841 state = tevent_req_data(req, struct smbXcli_req_state);
1843 if (!oplock_break /* oplock breaks are not signed */
1844 && !smb_signing_check_pdu(conn->smb1.signing,
1845 inbuf, state->smb1.seqnum+1)) {
1846 DEBUG(10, ("cli_check_sign_mac failed\n"));
1847 return NT_STATUS_ACCESS_DENIED;
1850 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1851 &iov, &num_iov);
1852 if (!NT_STATUS_IS_OK(status)) {
1853 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1854 nt_errstr(status)));
1855 return status;
1858 cmd = CVAL(inhdr, HDR_COM);
1859 status = smb1cli_pull_raw_error(inhdr);
1861 if (state->smb1.chained_requests == NULL) {
1862 if (num_iov != 3) {
1863 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1866 smbXcli_req_unset_pending(req);
1868 if (state->smb1.recv_iov == NULL) {
1870 * For requests with more than
1871 * one response, we have to readd the
1872 * recv_iov array.
1874 state->smb1.recv_iov = talloc_zero_array(state,
1875 struct iovec,
1877 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1878 return NT_STATUS_OK;
1882 state->smb1.recv_cmd = cmd;
1883 state->smb1.recv_status = status;
1884 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1886 state->smb1.recv_iov[0] = iov[0];
1887 state->smb1.recv_iov[1] = iov[1];
1888 state->smb1.recv_iov[2] = iov[2];
1890 if (talloc_array_length(conn->pending) == 0) {
1891 tevent_req_done(req);
1892 return NT_STATUS_OK;
1895 tevent_req_defer_callback(req, state->ev);
1896 tevent_req_done(req);
1897 return NT_STATUS_RETRY;
1900 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1901 num_chained = talloc_array_length(chain);
1902 num_responses = (num_iov - 1)/2;
1904 if (num_responses > num_chained) {
1905 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1908 for (i=0; i<num_chained; i++) {
1909 size_t iov_idx = 1 + (i*2);
1910 struct iovec *cur = &iov[iov_idx];
1911 uint8_t *inbuf_ref;
1913 req = chain[i];
1914 state = tevent_req_data(req, struct smbXcli_req_state);
1916 smbXcli_req_unset_pending(req);
1919 * as we finish multiple requests here
1920 * we need to defer the callbacks as
1921 * they could destroy our current stack state.
1923 tevent_req_defer_callback(req, state->ev);
1925 if (i >= num_responses) {
1926 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1927 continue;
1930 if (state->smb1.recv_iov == NULL) {
1932 * For requests with more than
1933 * one response, we have to readd the
1934 * recv_iov array.
1936 state->smb1.recv_iov = talloc_zero_array(state,
1937 struct iovec,
1939 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1940 continue;
1944 state->smb1.recv_cmd = cmd;
1946 if (i == (num_responses - 1)) {
1948 * The last request in the chain gets the status
1950 state->smb1.recv_status = status;
1951 } else {
1952 cmd = CVAL(cur[0].iov_base, 0);
1953 state->smb1.recv_status = NT_STATUS_OK;
1956 state->inbuf = inbuf;
1959 * Note: here we use talloc_reference() in a way
1960 * that does not expose it to the caller.
1962 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1963 if (tevent_req_nomem(inbuf_ref, req)) {
1964 continue;
1967 /* copy the related buffers */
1968 state->smb1.recv_iov[0] = iov[0];
1969 state->smb1.recv_iov[1] = cur[0];
1970 state->smb1.recv_iov[2] = cur[1];
1972 tevent_req_done(req);
1975 return NT_STATUS_RETRY;
1978 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1979 TALLOC_CTX *mem_ctx,
1980 struct iovec **piov,
1981 uint8_t **phdr,
1982 uint8_t *pwct,
1983 uint16_t **pvwv,
1984 uint32_t *pvwv_offset,
1985 uint32_t *pnum_bytes,
1986 uint8_t **pbytes,
1987 uint32_t *pbytes_offset,
1988 uint8_t **pinbuf,
1989 const struct smb1cli_req_expected_response *expected,
1990 size_t num_expected)
1992 struct smbXcli_req_state *state =
1993 tevent_req_data(req,
1994 struct smbXcli_req_state);
1995 NTSTATUS status = NT_STATUS_OK;
1996 struct iovec *recv_iov = NULL;
1997 uint8_t *hdr = NULL;
1998 uint8_t wct = 0;
1999 uint32_t vwv_offset = 0;
2000 uint16_t *vwv = NULL;
2001 uint32_t num_bytes = 0;
2002 uint32_t bytes_offset = 0;
2003 uint8_t *bytes = NULL;
2004 size_t i;
2005 bool found_status = false;
2006 bool found_size = false;
2008 if (piov != NULL) {
2009 *piov = NULL;
2011 if (phdr != NULL) {
2012 *phdr = 0;
2014 if (pwct != NULL) {
2015 *pwct = 0;
2017 if (pvwv != NULL) {
2018 *pvwv = NULL;
2020 if (pvwv_offset != NULL) {
2021 *pvwv_offset = 0;
2023 if (pnum_bytes != NULL) {
2024 *pnum_bytes = 0;
2026 if (pbytes != NULL) {
2027 *pbytes = NULL;
2029 if (pbytes_offset != NULL) {
2030 *pbytes_offset = 0;
2032 if (pinbuf != NULL) {
2033 *pinbuf = NULL;
2036 if (state->inbuf != NULL) {
2037 recv_iov = state->smb1.recv_iov;
2038 state->smb1.recv_iov = NULL;
2039 if (state->smb1.recv_cmd != SMBreadBraw) {
2040 hdr = (uint8_t *)recv_iov[0].iov_base;
2041 wct = recv_iov[1].iov_len/2;
2042 vwv = (uint16_t *)recv_iov[1].iov_base;
2043 vwv_offset = PTR_DIFF(vwv, hdr);
2044 num_bytes = recv_iov[2].iov_len;
2045 bytes = (uint8_t *)recv_iov[2].iov_base;
2046 bytes_offset = PTR_DIFF(bytes, hdr);
2050 if (tevent_req_is_nterror(req, &status)) {
2051 for (i=0; i < num_expected; i++) {
2052 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2053 found_status = true;
2054 break;
2058 if (found_status) {
2059 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2062 return status;
2065 if (num_expected == 0) {
2066 found_status = true;
2067 found_size = true;
2070 status = state->smb1.recv_status;
2072 for (i=0; i < num_expected; i++) {
2073 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2074 continue;
2077 found_status = true;
2078 if (expected[i].wct == 0) {
2079 found_size = true;
2080 break;
2083 if (expected[i].wct == wct) {
2084 found_size = true;
2085 break;
2089 if (!found_status) {
2090 return status;
2093 if (!found_size) {
2094 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2097 if (piov != NULL) {
2098 *piov = talloc_move(mem_ctx, &recv_iov);
2101 if (phdr != NULL) {
2102 *phdr = hdr;
2104 if (pwct != NULL) {
2105 *pwct = wct;
2107 if (pvwv != NULL) {
2108 *pvwv = vwv;
2110 if (pvwv_offset != NULL) {
2111 *pvwv_offset = vwv_offset;
2113 if (pnum_bytes != NULL) {
2114 *pnum_bytes = num_bytes;
2116 if (pbytes != NULL) {
2117 *pbytes = bytes;
2119 if (pbytes_offset != NULL) {
2120 *pbytes_offset = bytes_offset;
2122 if (pinbuf != NULL) {
2123 *pinbuf = state->inbuf;
2126 return status;
2129 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2131 size_t wct_ofs;
2132 int i;
2134 wct_ofs = HDR_WCT;
2136 for (i=0; i<num_reqs; i++) {
2137 struct smbXcli_req_state *state;
2138 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2139 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2140 state->smb1.iov_count-2);
2141 wct_ofs = (wct_ofs + 3) & ~3;
2143 return wct_ofs;
2146 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2148 struct smbXcli_req_state *first_state =
2149 tevent_req_data(reqs[0],
2150 struct smbXcli_req_state);
2151 struct smbXcli_req_state *state;
2152 size_t wct_offset;
2153 size_t chain_padding = 0;
2154 int i, iovlen;
2155 struct iovec *iov = NULL;
2156 struct iovec *this_iov;
2157 NTSTATUS status;
2158 size_t nbt_len;
2160 if (num_reqs == 1) {
2161 return smb1cli_req_writev_submit(reqs[0], first_state,
2162 first_state->smb1.iov,
2163 first_state->smb1.iov_count);
2166 iovlen = 0;
2167 for (i=0; i<num_reqs; i++) {
2168 if (!tevent_req_is_in_progress(reqs[i])) {
2169 return NT_STATUS_INTERNAL_ERROR;
2172 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2174 if (state->smb1.iov_count < 4) {
2175 return NT_STATUS_INVALID_PARAMETER_MIX;
2178 if (i == 0) {
2180 * The NBT and SMB header
2182 iovlen += 2;
2183 } else {
2185 * Chain padding
2187 iovlen += 1;
2191 * words and bytes
2193 iovlen += state->smb1.iov_count - 2;
2196 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2197 if (iov == NULL) {
2198 return NT_STATUS_NO_MEMORY;
2201 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2202 first_state, reqs, sizeof(*reqs) * num_reqs);
2203 if (first_state->smb1.chained_requests == NULL) {
2204 TALLOC_FREE(iov);
2205 return NT_STATUS_NO_MEMORY;
2208 wct_offset = HDR_WCT;
2209 this_iov = iov;
2211 for (i=0; i<num_reqs; i++) {
2212 size_t next_padding = 0;
2213 uint16_t *vwv;
2215 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2217 if (i < num_reqs-1) {
2218 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2219 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2220 TALLOC_FREE(iov);
2221 TALLOC_FREE(first_state->smb1.chained_requests);
2222 return NT_STATUS_INVALID_PARAMETER_MIX;
2226 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2227 state->smb1.iov_count-2) + 1;
2228 if ((wct_offset % 4) != 0) {
2229 next_padding = 4 - (wct_offset % 4);
2231 wct_offset += next_padding;
2232 vwv = state->smb1.vwv;
2234 if (i < num_reqs-1) {
2235 struct smbXcli_req_state *next_state =
2236 tevent_req_data(reqs[i+1],
2237 struct smbXcli_req_state);
2238 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2239 SCVAL(vwv+0, 1, 0);
2240 SSVAL(vwv+1, 0, wct_offset);
2241 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2242 /* properly end the chain */
2243 SCVAL(vwv+0, 0, 0xff);
2244 SCVAL(vwv+0, 1, 0xff);
2245 SSVAL(vwv+1, 0, 0);
2248 if (i == 0) {
2250 * The NBT and SMB header
2252 this_iov[0] = state->smb1.iov[0];
2253 this_iov[1] = state->smb1.iov[1];
2254 this_iov += 2;
2255 } else {
2257 * This one is a bit subtle. We have to add
2258 * chain_padding bytes between the requests, and we
2259 * have to also include the wct field of the
2260 * subsequent requests. We use the subsequent header
2261 * for the padding, it contains the wct field in its
2262 * last byte.
2264 this_iov[0].iov_len = chain_padding+1;
2265 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2266 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2267 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2268 this_iov += 1;
2272 * copy the words and bytes
2274 memcpy(this_iov, state->smb1.iov+2,
2275 sizeof(struct iovec) * (state->smb1.iov_count-2));
2276 this_iov += state->smb1.iov_count - 2;
2277 chain_padding = next_padding;
2280 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2281 if (nbt_len > first_state->conn->smb1.max_xmit) {
2282 TALLOC_FREE(iov);
2283 TALLOC_FREE(first_state->smb1.chained_requests);
2284 return NT_STATUS_INVALID_PARAMETER_MIX;
2287 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2288 if (!NT_STATUS_IS_OK(status)) {
2289 TALLOC_FREE(iov);
2290 TALLOC_FREE(first_state->smb1.chained_requests);
2291 return status;
2294 return NT_STATUS_OK;
2297 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2299 return ((tevent_queue_length(conn->outgoing) != 0)
2300 || (talloc_array_length(conn->pending) != 0));
2303 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2305 return conn->smb2.server.capabilities;
2308 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2310 return conn->smb2.server.security_mode;
2313 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2315 return conn->smb2.server.max_trans_size;
2318 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2320 return conn->smb2.server.max_read_size;
2323 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2325 return conn->smb2.server.max_write_size;
2328 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2329 uint16_t max_credits)
2331 conn->smb2.max_credits = max_credits;
2334 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2336 static bool smb2cli_req_cancel(struct tevent_req *req)
2338 struct smbXcli_req_state *state =
2339 tevent_req_data(req,
2340 struct smbXcli_req_state);
2341 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2342 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2343 uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2344 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2345 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2346 struct smbXcli_session *session = state->session;
2347 uint8_t *fixed = state->smb2.pad;
2348 uint16_t fixed_len = 4;
2349 struct tevent_req *subreq;
2350 struct smbXcli_req_state *substate;
2351 NTSTATUS status;
2353 SSVAL(fixed, 0, 0x04);
2354 SSVAL(fixed, 2, 0);
2356 subreq = smb2cli_req_create(state, state->ev,
2357 state->conn,
2358 SMB2_OP_CANCEL,
2359 flags, 0,
2360 0, /* timeout */
2361 pid, tid, session,
2362 fixed, fixed_len,
2363 NULL, 0);
2364 if (subreq == NULL) {
2365 return false;
2367 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2369 if (flags & SMB2_HDR_FLAG_ASYNC) {
2370 mid = 0;
2373 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2374 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2375 SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2376 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2377 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2379 status = smb2cli_req_compound_submit(&subreq, 1);
2380 if (!NT_STATUS_IS_OK(status)) {
2381 TALLOC_FREE(subreq);
2382 return false;
2385 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2387 return true;
2390 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2392 /* we do not care about the result */
2393 TALLOC_FREE(subreq);
2396 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2397 struct tevent_context *ev,
2398 struct smbXcli_conn *conn,
2399 uint16_t cmd,
2400 uint32_t additional_flags,
2401 uint32_t clear_flags,
2402 uint32_t timeout_msec,
2403 uint32_t pid,
2404 uint32_t tid,
2405 struct smbXcli_session *session,
2406 const uint8_t *fixed,
2407 uint16_t fixed_len,
2408 const uint8_t *dyn,
2409 uint32_t dyn_len)
2411 struct tevent_req *req;
2412 struct smbXcli_req_state *state;
2413 uint32_t flags = 0;
2414 uint64_t uid = 0;
2416 req = tevent_req_create(mem_ctx, &state,
2417 struct smbXcli_req_state);
2418 if (req == NULL) {
2419 return NULL;
2422 state->ev = ev;
2423 state->conn = conn;
2424 state->session = session;
2426 if (session) {
2427 uid = session->smb2.session_id;
2429 state->smb2.should_sign = session->smb2.should_sign;
2430 state->smb2.should_encrypt = session->smb2.should_encrypt;
2432 /* TODO: turn on encryption based on the tree connect. */
2434 if (cmd == SMB2_OP_SESSSETUP &&
2435 session->smb2.signing_key.length != 0) {
2436 state->smb2.should_sign = true;
2439 if (cmd == SMB2_OP_SESSSETUP &&
2440 session->smb2.channel_signing_key.length == 0) {
2441 state->smb2.should_encrypt = false;
2444 if (state->smb2.should_encrypt) {
2445 state->smb2.should_sign = false;
2449 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2450 if (state->smb2.recv_iov == NULL) {
2451 TALLOC_FREE(req);
2452 return NULL;
2455 flags |= additional_flags;
2456 flags &= ~clear_flags;
2458 state->smb2.fixed = fixed;
2459 state->smb2.fixed_len = fixed_len;
2460 state->smb2.dyn = dyn;
2461 state->smb2.dyn_len = dyn_len;
2463 if (state->smb2.should_encrypt) {
2464 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2465 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2468 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2469 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2470 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2471 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2472 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2473 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2474 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2476 switch (cmd) {
2477 case SMB2_OP_CANCEL:
2478 state->one_way = true;
2479 break;
2480 case SMB2_OP_BREAK:
2482 * If this is a dummy request, it will have
2483 * UINT64_MAX as message id.
2484 * If we send on break acknowledgement,
2485 * this gets overwritten later.
2487 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2488 break;
2491 if (timeout_msec > 0) {
2492 struct timeval endtime;
2494 endtime = timeval_current_ofs_msec(timeout_msec);
2495 if (!tevent_req_set_endtime(req, ev, endtime)) {
2496 return req;
2500 return req;
2503 void smb2cli_req_set_notify_async(struct tevent_req *req)
2505 struct smbXcli_req_state *state =
2506 tevent_req_data(req,
2507 struct smbXcli_req_state);
2509 state->smb2.notify_async = true;
2512 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2513 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2514 TALLOC_CTX *tmp_mem,
2515 uint8_t *inbuf);
2517 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2518 int num_reqs)
2520 struct smbXcli_req_state *state;
2521 struct tevent_req *subreq;
2522 struct iovec *iov;
2523 int i, num_iov, nbt_len;
2526 * 1 for the nbt length
2527 * per request: TRANSFORM, HDR, fixed, dyn, padding
2528 * -1 because the last one does not need padding
2531 iov = talloc_array(reqs[0], struct iovec, 1 + 5*num_reqs - 1);
2532 if (iov == NULL) {
2533 return NT_STATUS_NO_MEMORY;
2536 num_iov = 1;
2537 nbt_len = 0;
2539 for (i=0; i<num_reqs; i++) {
2540 int tf_iov;
2541 int hdr_iov;
2542 size_t reqlen;
2543 bool ret;
2544 uint16_t opcode;
2545 uint64_t avail;
2546 uint16_t charge;
2547 uint16_t credits;
2548 uint64_t mid;
2549 const DATA_BLOB *signing_key = NULL;
2550 const DATA_BLOB *encryption_key = NULL;
2552 if (!tevent_req_is_in_progress(reqs[i])) {
2553 return NT_STATUS_INTERNAL_ERROR;
2556 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2558 if (!smbXcli_conn_is_connected(state->conn)) {
2559 return NT_STATUS_CONNECTION_DISCONNECTED;
2562 if ((state->conn->protocol != PROTOCOL_NONE) &&
2563 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2564 return NT_STATUS_REVISION_MISMATCH;
2567 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2568 if (opcode == SMB2_OP_CANCEL) {
2569 goto skip_credits;
2572 avail = UINT64_MAX - state->conn->smb2.mid;
2573 if (avail < 1) {
2574 return NT_STATUS_CONNECTION_ABORTED;
2577 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2578 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2579 } else {
2580 charge = 1;
2583 charge = MAX(state->smb2.credit_charge, charge);
2585 avail = MIN(avail, state->conn->smb2.cur_credits);
2586 if (avail < charge) {
2587 return NT_STATUS_INTERNAL_ERROR;
2590 credits = 0;
2591 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2592 credits = state->conn->smb2.max_credits -
2593 state->conn->smb2.cur_credits;
2595 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2596 credits += 1;
2599 mid = state->conn->smb2.mid;
2600 state->conn->smb2.mid += charge;
2601 state->conn->smb2.cur_credits -= charge;
2603 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2604 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2606 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2607 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2609 skip_credits:
2610 if (state->session) {
2612 * We prefer the channel signing key if it is
2613 * already there.
2615 if (state->smb2.should_sign) {
2616 signing_key = &state->session->smb2.channel_signing_key;
2620 * If it is a channel binding, we already have the main
2621 * signing key and try that one.
2623 if (signing_key && signing_key->length == 0) {
2624 signing_key = &state->session->smb2.signing_key;
2628 * If we do not have any session key yet, we skip the
2629 * signing of SMB2_OP_SESSSETUP requests.
2631 if (signing_key && signing_key->length == 0) {
2632 signing_key = NULL;
2635 if (state->smb2.should_encrypt) {
2636 encryption_key = &state->session->smb2.encryption_key;
2640 if (encryption_key) {
2641 tf_iov = num_iov;
2642 iov[num_iov].iov_base = state->smb2.transform;
2643 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2644 num_iov += 1;
2647 hdr_iov = num_iov;
2648 iov[num_iov].iov_base = state->smb2.hdr;
2649 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2650 num_iov += 1;
2652 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2653 iov[num_iov].iov_len = state->smb2.fixed_len;
2654 num_iov += 1;
2656 if (state->smb2.dyn != NULL) {
2657 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2658 iov[num_iov].iov_len = state->smb2.dyn_len;
2659 num_iov += 1;
2662 reqlen = sizeof(state->smb2.hdr);
2663 reqlen += state->smb2.fixed_len;
2664 reqlen += state->smb2.dyn_len;
2666 if (i < num_reqs-1) {
2667 if ((reqlen % 8) > 0) {
2668 uint8_t pad = 8 - (reqlen % 8);
2669 iov[num_iov].iov_base = state->smb2.pad;
2670 iov[num_iov].iov_len = pad;
2671 num_iov += 1;
2672 reqlen += pad;
2674 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2677 if (encryption_key) {
2678 NTSTATUS status;
2679 uint8_t *buf;
2680 int vi;
2682 SBVAL(state->smb2.transform, SMB2_TF_NONCE, mid);
2683 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2684 state->session->smb2.channel_nonce);
2685 SBVAL(state->smb2.transform, SMB2_TF_MSG_SIZE,
2686 reqlen);
2688 buf = talloc_array(iov, uint8_t, reqlen);
2689 if (buf == NULL) {
2690 return NT_STATUS_NO_MEMORY;
2693 reqlen += SMB2_TF_HDR_SIZE;
2696 * We copy the buffers before encrypting them,
2697 * this is at least currently needed for the
2698 * to keep state->smb2.hdr.
2700 * Also the callers may expect there buffers
2701 * to be const.
2703 for (vi = hdr_iov; vi < num_iov; vi++) {
2704 struct iovec *v = &iov[vi];
2705 const uint8_t *o = (const uint8_t *)v->iov_base;
2707 memcpy(buf, o, v->iov_len);
2708 v->iov_base = (void *)buf;
2709 buf += v->iov_len;
2712 status = smb2_signing_encrypt_pdu(*encryption_key,
2713 state->session->conn->protocol,
2714 &iov[tf_iov], num_iov - tf_iov);
2715 if (!NT_STATUS_IS_OK(status)) {
2716 return status;
2718 } else if (signing_key) {
2719 NTSTATUS status;
2721 status = smb2_signing_sign_pdu(*signing_key,
2722 state->session->conn->protocol,
2723 &iov[hdr_iov], num_iov - hdr_iov);
2724 if (!NT_STATUS_IS_OK(status)) {
2725 return status;
2729 nbt_len += reqlen;
2731 ret = smbXcli_req_set_pending(reqs[i]);
2732 if (!ret) {
2733 return NT_STATUS_NO_MEMORY;
2737 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2738 _smb_setlen_tcp(state->length_hdr, nbt_len);
2739 iov[0].iov_base = state->length_hdr;
2740 iov[0].iov_len = sizeof(state->length_hdr);
2742 if (state->conn->dispatch_incoming == NULL) {
2743 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2746 subreq = writev_send(state, state->ev, state->conn->outgoing,
2747 state->conn->write_fd, false, iov, num_iov);
2748 if (subreq == NULL) {
2749 return NT_STATUS_NO_MEMORY;
2751 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2752 return NT_STATUS_OK;
2755 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2757 struct smbXcli_req_state *state =
2758 tevent_req_data(req,
2759 struct smbXcli_req_state);
2761 state->smb2.credit_charge = charge;
2764 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2765 struct tevent_context *ev,
2766 struct smbXcli_conn *conn,
2767 uint16_t cmd,
2768 uint32_t additional_flags,
2769 uint32_t clear_flags,
2770 uint32_t timeout_msec,
2771 uint32_t pid,
2772 uint32_t tid,
2773 struct smbXcli_session *session,
2774 const uint8_t *fixed,
2775 uint16_t fixed_len,
2776 const uint8_t *dyn,
2777 uint32_t dyn_len)
2779 struct tevent_req *req;
2780 NTSTATUS status;
2782 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2783 additional_flags, clear_flags,
2784 timeout_msec,
2785 pid, tid, session,
2786 fixed, fixed_len, dyn, dyn_len);
2787 if (req == NULL) {
2788 return NULL;
2790 if (!tevent_req_is_in_progress(req)) {
2791 return tevent_req_post(req, ev);
2793 status = smb2cli_req_compound_submit(&req, 1);
2794 if (tevent_req_nterror(req, status)) {
2795 return tevent_req_post(req, ev);
2797 return req;
2800 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2802 struct tevent_req *req =
2803 tevent_req_callback_data(subreq,
2804 struct tevent_req);
2805 struct smbXcli_req_state *state =
2806 tevent_req_data(req,
2807 struct smbXcli_req_state);
2808 ssize_t nwritten;
2809 int err;
2811 nwritten = writev_recv(subreq, &err);
2812 TALLOC_FREE(subreq);
2813 if (nwritten == -1) {
2814 /* here, we need to notify all pending requests */
2815 NTSTATUS status = map_nt_error_from_unix_common(err);
2816 smbXcli_conn_disconnect(state->conn, status);
2817 return;
2821 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2822 uint8_t *buf,
2823 size_t buflen,
2824 TALLOC_CTX *mem_ctx,
2825 struct iovec **piov, int *pnum_iov)
2827 struct iovec *iov;
2828 int num_iov = 0;
2829 size_t taken = 0;
2830 uint8_t *first_hdr = buf;
2832 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2833 if (iov == NULL) {
2834 return NT_STATUS_NO_MEMORY;
2837 while (taken < buflen) {
2838 uint8_t *tf = NULL;
2839 size_t tf_len = 0;
2840 size_t len = buflen - taken;
2841 uint8_t *hdr = first_hdr + taken;
2842 struct iovec *cur;
2843 size_t full_size;
2844 size_t next_command_ofs;
2845 uint16_t body_size;
2846 struct iovec *iov_tmp;
2848 if (len < 4) {
2849 DEBUG(10, ("%d bytes left, expected at least %d\n",
2850 (int)len, 4));
2851 goto inval;
2853 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2854 struct smbXcli_session *s;
2855 uint64_t uid;
2856 struct iovec tf_iov[2];
2857 NTSTATUS status;
2859 if (len < SMB2_TF_HDR_SIZE) {
2860 DEBUG(10, ("%d bytes left, expected at least %d\n",
2861 (int)len, SMB2_TF_HDR_SIZE));
2862 goto inval;
2864 tf = hdr;
2865 tf_len = SMB2_TF_HDR_SIZE;
2866 taken += tf_len;
2868 hdr = first_hdr + taken;
2869 len = IVAL(tf, SMB2_TF_MSG_SIZE);
2870 uid = BVAL(tf, SMB2_TF_SESSION_ID);
2872 s = conn->sessions;
2873 for (; s; s = s->next) {
2874 if (s->smb2.session_id != uid) {
2875 continue;
2877 break;
2880 if (s == NULL) {
2881 DEBUG(10, ("unknown session_id %llu\n",
2882 (unsigned long long)uid));
2883 goto inval;
2886 tf_iov[0].iov_base = (void *)tf;
2887 tf_iov[0].iov_len = tf_len;
2888 tf_iov[1].iov_base = (void *)hdr;
2889 tf_iov[1].iov_len = len;
2891 status = smb2_signing_decrypt_pdu(s->smb2.decryption_key,
2892 conn->protocol,
2893 tf_iov, 2);
2894 if (!NT_STATUS_IS_OK(status)) {
2895 TALLOC_FREE(iov);
2896 return status;
2901 * We need the header plus the body length field
2904 if (len < SMB2_HDR_BODY + 2) {
2905 DEBUG(10, ("%d bytes left, expected at least %d\n",
2906 (int)len, SMB2_HDR_BODY));
2907 goto inval;
2909 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2910 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2911 IVAL(hdr, 0)));
2912 goto inval;
2914 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2915 DEBUG(10, ("Got HDR len %d, expected %d\n",
2916 SVAL(hdr, 4), SMB2_HDR_BODY));
2917 goto inval;
2920 full_size = len;
2921 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2922 body_size = SVAL(hdr, SMB2_HDR_BODY);
2924 if (next_command_ofs != 0) {
2925 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2926 goto inval;
2928 if (next_command_ofs > full_size) {
2929 goto inval;
2931 if (tf && next_command_ofs < len) {
2932 goto inval;
2934 full_size = next_command_ofs;
2936 if (body_size < 2) {
2937 goto inval;
2939 body_size &= 0xfffe;
2941 if (body_size > (full_size - SMB2_HDR_BODY)) {
2942 goto inval;
2945 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2946 num_iov + 4);
2947 if (iov_tmp == NULL) {
2948 TALLOC_FREE(iov);
2949 return NT_STATUS_NO_MEMORY;
2951 iov = iov_tmp;
2952 cur = &iov[num_iov];
2953 num_iov += 4;
2955 cur[0].iov_base = tf;
2956 cur[0].iov_len = tf_len;
2957 cur[1].iov_base = hdr;
2958 cur[1].iov_len = SMB2_HDR_BODY;
2959 cur[2].iov_base = hdr + SMB2_HDR_BODY;
2960 cur[2].iov_len = body_size;
2961 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
2962 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2964 taken += full_size;
2967 *piov = iov;
2968 *pnum_iov = num_iov;
2969 return NT_STATUS_OK;
2971 inval:
2972 TALLOC_FREE(iov);
2973 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2976 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2977 uint64_t mid)
2979 size_t num_pending = talloc_array_length(conn->pending);
2980 size_t i;
2982 for (i=0; i<num_pending; i++) {
2983 struct tevent_req *req = conn->pending[i];
2984 struct smbXcli_req_state *state =
2985 tevent_req_data(req,
2986 struct smbXcli_req_state);
2988 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2989 return req;
2992 return NULL;
2995 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2996 TALLOC_CTX *tmp_mem,
2997 uint8_t *inbuf)
2999 struct tevent_req *req;
3000 struct smbXcli_req_state *state = NULL;
3001 struct iovec *iov;
3002 int i, num_iov;
3003 NTSTATUS status;
3004 bool defer = true;
3005 struct smbXcli_session *last_session = NULL;
3006 size_t inbuf_len = smb_len_tcp(inbuf);
3008 status = smb2cli_inbuf_parse_compound(conn,
3009 inbuf + NBT_HDR_SIZE,
3010 inbuf_len,
3011 tmp_mem,
3012 &iov, &num_iov);
3013 if (!NT_STATUS_IS_OK(status)) {
3014 return status;
3017 for (i=0; i<num_iov; i+=4) {
3018 uint8_t *inbuf_ref = NULL;
3019 struct iovec *cur = &iov[i];
3020 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3021 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3022 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3023 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3024 uint16_t req_opcode;
3025 uint32_t req_flags;
3026 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3027 uint32_t new_credits;
3028 struct smbXcli_session *session = NULL;
3029 const DATA_BLOB *signing_key = NULL;
3031 new_credits = conn->smb2.cur_credits;
3032 new_credits += credits;
3033 if (new_credits > UINT16_MAX) {
3034 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3036 conn->smb2.cur_credits += credits;
3038 req = smb2cli_conn_find_pending(conn, mid);
3039 if (req == NULL) {
3040 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3042 state = tevent_req_data(req, struct smbXcli_req_state);
3044 state->smb2.got_async = false;
3046 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3047 if (opcode != req_opcode) {
3048 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3050 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3052 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3053 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3056 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3057 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3058 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3059 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3062 * async interim responses are not signed,
3063 * even if the SMB2_HDR_FLAG_SIGNED flag
3064 * is set.
3066 req_flags |= SMB2_HDR_FLAG_ASYNC;
3067 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3068 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3070 if (state->smb2.notify_async) {
3071 state->smb2.got_async = true;
3072 tevent_req_defer_callback(req, state->ev);
3073 tevent_req_notify_callback(req);
3075 continue;
3078 session = state->session;
3079 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3080 session = last_session;
3082 last_session = session;
3084 if (state->smb2.should_sign) {
3085 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3086 return NT_STATUS_ACCESS_DENIED;
3090 if (flags & SMB2_HDR_FLAG_SIGNED) {
3091 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3093 if (session == NULL) {
3094 struct smbXcli_session *s;
3096 s = state->conn->sessions;
3097 for (; s; s = s->next) {
3098 if (s->smb2.session_id != uid) {
3099 continue;
3102 session = s;
3103 break;
3107 if (session == NULL) {
3108 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3111 last_session = session;
3112 signing_key = &session->smb2.channel_signing_key;
3115 if (opcode == SMB2_OP_SESSSETUP) {
3117 * We prefer the channel signing key, if it is
3118 * already there.
3120 * If we do not have a channel signing key yet,
3121 * we try the main signing key, if it is not
3122 * the final response.
3124 if (signing_key && signing_key->length == 0 &&
3125 !NT_STATUS_IS_OK(status)) {
3126 signing_key = &session->smb2.signing_key;
3129 if (signing_key && signing_key->length == 0) {
3131 * If we do not have a session key to
3132 * verify the signature, we defer the
3133 * signing check to the caller.
3135 * The caller gets NT_STATUS_OK, it
3136 * has to call
3137 * smb2cli_session_set_session_key()
3138 * or
3139 * smb2cli_session_set_channel_key()
3140 * which will check the signature
3141 * with the channel signing key.
3143 signing_key = NULL;
3147 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3149 * if the server returns NT_STATUS_USER_SESSION_DELETED
3150 * the response is not signed and we should
3151 * propagate the NT_STATUS_USER_SESSION_DELETED
3152 * status to the caller.
3154 signing_key = NULL;
3155 } else if (state->smb2.should_encrypt) {
3156 if (cur[0].iov_len != SMB2_TF_HDR_SIZE) {
3157 return NT_STATUS_ACCESS_DENIED;
3161 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3162 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3163 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3165 * if the server returns
3166 * NT_STATUS_NETWORK_NAME_DELETED
3167 * NT_STATUS_FILE_CLOSED
3168 * NT_STATUS_INVALID_PARAMETER
3169 * the response might not be signed
3170 * as this happens before the signing checks.
3172 * If server echos the signature (or all zeros)
3173 * we should report the status from the server
3174 * to the caller.
3176 if (signing_key) {
3177 int cmp;
3179 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3180 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3181 16);
3182 if (cmp == 0) {
3183 state->smb2.signing_skipped = true;
3184 signing_key = NULL;
3187 if (signing_key) {
3188 int cmp;
3189 static const uint8_t zeros[16];
3191 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3192 zeros,
3193 16);
3194 if (cmp == 0) {
3195 state->smb2.signing_skipped = true;
3196 signing_key = NULL;
3201 if (signing_key) {
3202 status = smb2_signing_check_pdu(*signing_key,
3203 state->conn->protocol,
3204 &cur[1], 3);
3205 if (!NT_STATUS_IS_OK(status)) {
3207 * If the signing check fails, we disconnect
3208 * the connection.
3210 return status;
3214 smbXcli_req_unset_pending(req);
3217 * There might be more than one response
3218 * we need to defer the notifications
3220 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3221 defer = false;
3224 if (defer) {
3225 tevent_req_defer_callback(req, state->ev);
3229 * Note: here we use talloc_reference() in a way
3230 * that does not expose it to the caller.
3232 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3233 if (tevent_req_nomem(inbuf_ref, req)) {
3234 continue;
3237 /* copy the related buffers */
3238 state->smb2.recv_iov[0] = cur[1];
3239 state->smb2.recv_iov[1] = cur[2];
3240 state->smb2.recv_iov[2] = cur[3];
3242 tevent_req_done(req);
3245 if (defer) {
3246 return NT_STATUS_RETRY;
3249 return NT_STATUS_OK;
3252 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3253 struct iovec **piov,
3254 const struct smb2cli_req_expected_response *expected,
3255 size_t num_expected)
3257 struct smbXcli_req_state *state =
3258 tevent_req_data(req,
3259 struct smbXcli_req_state);
3260 NTSTATUS status;
3261 size_t body_size;
3262 bool found_status = false;
3263 bool found_size = false;
3264 size_t i;
3266 if (piov != NULL) {
3267 *piov = NULL;
3270 if (state->smb2.got_async) {
3271 return STATUS_PENDING;
3274 if (tevent_req_is_nterror(req, &status)) {
3275 for (i=0; i < num_expected; i++) {
3276 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3277 found_status = true;
3278 break;
3282 if (found_status) {
3283 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3286 return status;
3289 if (num_expected == 0) {
3290 found_status = true;
3291 found_size = true;
3294 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3295 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3297 for (i=0; i < num_expected; i++) {
3298 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3299 continue;
3302 found_status = true;
3303 if (expected[i].body_size == 0) {
3304 found_size = true;
3305 break;
3308 if (expected[i].body_size == body_size) {
3309 found_size = true;
3310 break;
3314 if (!found_status) {
3315 return status;
3318 if (state->smb2.signing_skipped) {
3319 if (num_expected > 0) {
3320 return NT_STATUS_ACCESS_DENIED;
3322 if (!NT_STATUS_IS_ERR(status)) {
3323 return NT_STATUS_ACCESS_DENIED;
3327 if (!found_size) {
3328 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3331 if (piov != NULL) {
3332 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3335 return status;
3338 static const struct {
3339 enum protocol_types proto;
3340 const char *smb1_name;
3341 } smb1cli_prots[] = {
3342 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3343 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3344 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3345 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3346 {PROTOCOL_LANMAN2, "LM1.2X002"},
3347 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3348 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3349 {PROTOCOL_LANMAN2, "Samba"},
3350 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3351 {PROTOCOL_NT1, "NT LM 0.12"},
3352 {PROTOCOL_SMB2_02, "SMB 2.002"},
3353 {PROTOCOL_SMB2_10, "SMB 2.???"},
3356 static const struct {
3357 enum protocol_types proto;
3358 uint16_t smb2_dialect;
3359 } smb2cli_prots[] = {
3360 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3361 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3362 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3363 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3364 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3367 struct smbXcli_negprot_state {
3368 struct smbXcli_conn *conn;
3369 struct tevent_context *ev;
3370 uint32_t timeout_msec;
3371 enum protocol_types min_protocol;
3372 enum protocol_types max_protocol;
3374 struct {
3375 uint8_t fixed[36];
3376 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3377 } smb2;
3380 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3381 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3382 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3383 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3384 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3385 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3386 TALLOC_CTX *frame,
3387 uint8_t *inbuf);
3389 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3390 struct tevent_context *ev,
3391 struct smbXcli_conn *conn,
3392 uint32_t timeout_msec,
3393 enum protocol_types min_protocol,
3394 enum protocol_types max_protocol)
3396 struct tevent_req *req, *subreq;
3397 struct smbXcli_negprot_state *state;
3399 req = tevent_req_create(mem_ctx, &state,
3400 struct smbXcli_negprot_state);
3401 if (req == NULL) {
3402 return NULL;
3404 state->conn = conn;
3405 state->ev = ev;
3406 state->timeout_msec = timeout_msec;
3407 state->min_protocol = min_protocol;
3408 state->max_protocol = max_protocol;
3410 if (min_protocol == PROTOCOL_NONE) {
3411 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3412 return tevent_req_post(req, ev);
3415 if (max_protocol == PROTOCOL_NONE) {
3416 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3417 return tevent_req_post(req, ev);
3420 if (min_protocol > max_protocol) {
3421 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3422 return tevent_req_post(req, ev);
3425 if ((min_protocol < PROTOCOL_SMB2_02) &&
3426 (max_protocol < PROTOCOL_SMB2_02)) {
3428 * SMB1 only...
3430 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3432 subreq = smbXcli_negprot_smb1_subreq(state);
3433 if (tevent_req_nomem(subreq, req)) {
3434 return tevent_req_post(req, ev);
3436 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3437 return req;
3440 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3441 (max_protocol >= PROTOCOL_SMB2_02)) {
3443 * SMB2 only...
3445 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3447 subreq = smbXcli_negprot_smb2_subreq(state);
3448 if (tevent_req_nomem(subreq, req)) {
3449 return tevent_req_post(req, ev);
3451 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3452 return req;
3456 * We send an SMB1 negprot with the SMB2 dialects
3457 * and expect a SMB1 or a SMB2 response.
3459 * smbXcli_negprot_dispatch_incoming() will fix the
3460 * callback to match protocol of the response.
3462 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3464 subreq = smbXcli_negprot_smb1_subreq(state);
3465 if (tevent_req_nomem(subreq, req)) {
3466 return tevent_req_post(req, ev);
3468 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3469 return req;
3472 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3474 struct tevent_req *req =
3475 tevent_req_callback_data(subreq,
3476 struct tevent_req);
3477 NTSTATUS status;
3480 * we just want the low level error
3482 status = tevent_req_simple_recv_ntstatus(subreq);
3483 TALLOC_FREE(subreq);
3484 if (tevent_req_nterror(req, status)) {
3485 return;
3488 /* this should never happen */
3489 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3492 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3494 size_t i;
3495 DATA_BLOB bytes = data_blob_null;
3496 uint8_t flags;
3497 uint16_t flags2;
3499 /* setup the protocol strings */
3500 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3501 uint8_t c = 2;
3502 bool ok;
3504 if (smb1cli_prots[i].proto < state->min_protocol) {
3505 continue;
3508 if (smb1cli_prots[i].proto > state->max_protocol) {
3509 continue;
3512 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3513 if (!ok) {
3514 return NULL;
3518 * We now it is already ascii and
3519 * we want NULL termination.
3521 ok = data_blob_append(state, &bytes,
3522 smb1cli_prots[i].smb1_name,
3523 strlen(smb1cli_prots[i].smb1_name)+1);
3524 if (!ok) {
3525 return NULL;
3529 smb1cli_req_flags(state->max_protocol,
3530 state->conn->smb1.client.capabilities,
3531 SMBnegprot,
3532 0, 0, &flags,
3533 0, 0, &flags2);
3535 return smb1cli_req_send(state, state->ev, state->conn,
3536 SMBnegprot,
3537 flags, ~flags,
3538 flags2, ~flags2,
3539 state->timeout_msec,
3540 0xFFFE, 0, NULL, /* pid, tid, session */
3541 0, NULL, /* wct, vwv */
3542 bytes.length, bytes.data);
3545 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3547 struct tevent_req *req =
3548 tevent_req_callback_data(subreq,
3549 struct tevent_req);
3550 struct smbXcli_negprot_state *state =
3551 tevent_req_data(req,
3552 struct smbXcli_negprot_state);
3553 struct smbXcli_conn *conn = state->conn;
3554 struct iovec *recv_iov = NULL;
3555 uint8_t *inhdr;
3556 uint8_t wct;
3557 uint16_t *vwv;
3558 uint32_t num_bytes;
3559 uint8_t *bytes;
3560 NTSTATUS status;
3561 uint16_t protnum;
3562 size_t i;
3563 size_t num_prots = 0;
3564 uint8_t flags;
3565 uint32_t client_capabilities = conn->smb1.client.capabilities;
3566 uint32_t both_capabilities;
3567 uint32_t server_capabilities = 0;
3568 uint32_t capabilities;
3569 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3570 uint32_t server_max_xmit = 0;
3571 uint32_t max_xmit;
3572 uint32_t server_max_mux = 0;
3573 uint16_t server_security_mode = 0;
3574 uint32_t server_session_key = 0;
3575 bool server_readbraw = false;
3576 bool server_writebraw = false;
3577 bool server_lockread = false;
3578 bool server_writeunlock = false;
3579 struct GUID server_guid = GUID_zero();
3580 DATA_BLOB server_gss_blob = data_blob_null;
3581 uint8_t server_challenge[8];
3582 char *server_workgroup = NULL;
3583 char *server_name = NULL;
3584 int server_time_zone = 0;
3585 NTTIME server_system_time = 0;
3586 static const struct smb1cli_req_expected_response expected[] = {
3588 .status = NT_STATUS_OK,
3589 .wct = 0x11, /* NT1 */
3592 .status = NT_STATUS_OK,
3593 .wct = 0x0D, /* LM */
3596 .status = NT_STATUS_OK,
3597 .wct = 0x01, /* CORE */
3601 ZERO_STRUCT(server_challenge);
3603 status = smb1cli_req_recv(subreq, state,
3604 &recv_iov,
3605 &inhdr,
3606 &wct,
3607 &vwv,
3608 NULL, /* pvwv_offset */
3609 &num_bytes,
3610 &bytes,
3611 NULL, /* pbytes_offset */
3612 NULL, /* pinbuf */
3613 expected, ARRAY_SIZE(expected));
3614 TALLOC_FREE(subreq);
3615 if (tevent_req_nterror(req, status)) {
3616 return;
3619 flags = CVAL(inhdr, HDR_FLG);
3621 protnum = SVAL(vwv, 0);
3623 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3624 if (smb1cli_prots[i].proto < state->min_protocol) {
3625 continue;
3628 if (smb1cli_prots[i].proto > state->max_protocol) {
3629 continue;
3632 if (protnum != num_prots) {
3633 num_prots++;
3634 continue;
3637 conn->protocol = smb1cli_prots[i].proto;
3638 break;
3641 if (conn->protocol == PROTOCOL_NONE) {
3642 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3643 return;
3646 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3647 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3648 "and the selected protocol level doesn't support it.\n"));
3649 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3650 return;
3653 if (flags & FLAG_SUPPORT_LOCKREAD) {
3654 server_lockread = true;
3655 server_writeunlock = true;
3658 if (conn->protocol >= PROTOCOL_NT1) {
3659 const char *client_signing = NULL;
3660 bool server_mandatory = false;
3661 bool server_allowed = false;
3662 const char *server_signing = NULL;
3663 bool ok;
3664 uint8_t key_len;
3666 if (wct != 0x11) {
3667 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3668 return;
3671 /* NT protocol */
3672 server_security_mode = CVAL(vwv + 1, 0);
3673 server_max_mux = SVAL(vwv + 1, 1);
3674 server_max_xmit = IVAL(vwv + 3, 1);
3675 server_session_key = IVAL(vwv + 7, 1);
3676 server_time_zone = SVALS(vwv + 15, 1);
3677 server_time_zone *= 60;
3678 /* this time arrives in real GMT */
3679 server_system_time = BVAL(vwv + 11, 1);
3680 server_capabilities = IVAL(vwv + 9, 1);
3682 key_len = CVAL(vwv + 16, 1);
3684 if (server_capabilities & CAP_RAW_MODE) {
3685 server_readbraw = true;
3686 server_writebraw = true;
3688 if (server_capabilities & CAP_LOCK_AND_READ) {
3689 server_lockread = true;
3692 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3693 DATA_BLOB blob1, blob2;
3695 if (num_bytes < 16) {
3696 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3697 return;
3700 blob1 = data_blob_const(bytes, 16);
3701 status = GUID_from_data_blob(&blob1, &server_guid);
3702 if (tevent_req_nterror(req, status)) {
3703 return;
3706 blob1 = data_blob_const(bytes+16, num_bytes-16);
3707 blob2 = data_blob_dup_talloc(state, blob1);
3708 if (blob1.length > 0 &&
3709 tevent_req_nomem(blob2.data, req)) {
3710 return;
3712 server_gss_blob = blob2;
3713 } else {
3714 DATA_BLOB blob1, blob2;
3716 if (num_bytes < key_len) {
3717 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3718 return;
3721 if (key_len != 0 && key_len != 8) {
3722 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3723 return;
3726 if (key_len == 8) {
3727 memcpy(server_challenge, bytes, 8);
3730 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3731 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3732 if (blob1.length > 0) {
3733 size_t len;
3735 len = utf16_len_n(blob1.data,
3736 blob1.length);
3737 blob1.length = len;
3739 ok = convert_string_talloc(state,
3740 CH_UTF16LE,
3741 CH_UNIX,
3742 blob1.data,
3743 blob1.length,
3744 &server_workgroup,
3745 &len);
3746 if (!ok) {
3747 status = map_nt_error_from_unix_common(errno);
3748 tevent_req_nterror(req, status);
3749 return;
3753 blob2.data += blob1.length;
3754 blob2.length -= blob1.length;
3755 if (blob2.length > 0) {
3756 size_t len;
3758 len = utf16_len_n(blob1.data,
3759 blob1.length);
3760 blob1.length = len;
3762 ok = convert_string_talloc(state,
3763 CH_UTF16LE,
3764 CH_UNIX,
3765 blob2.data,
3766 blob2.length,
3767 &server_name,
3768 &len);
3769 if (!ok) {
3770 status = map_nt_error_from_unix_common(errno);
3771 tevent_req_nterror(req, status);
3772 return;
3777 client_signing = "disabled";
3778 if (conn->allow_signing) {
3779 client_signing = "allowed";
3781 if (conn->mandatory_signing) {
3782 client_signing = "required";
3785 server_signing = "not supported";
3786 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3787 server_signing = "supported";
3788 server_allowed = true;
3790 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3791 server_signing = "required";
3792 server_mandatory = true;
3795 ok = smb_signing_set_negotiated(conn->smb1.signing,
3796 server_allowed,
3797 server_mandatory);
3798 if (!ok) {
3799 DEBUG(1,("cli_negprot: SMB signing is required, "
3800 "but client[%s] and server[%s] mismatch\n",
3801 client_signing, server_signing));
3802 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3803 return;
3806 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3807 DATA_BLOB blob1;
3808 uint8_t key_len;
3809 time_t t;
3811 if (wct != 0x0D) {
3812 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3813 return;
3816 server_security_mode = SVAL(vwv + 1, 0);
3817 server_max_xmit = SVAL(vwv + 2, 0);
3818 server_max_mux = SVAL(vwv + 3, 0);
3819 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3820 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3821 server_session_key = IVAL(vwv + 6, 0);
3822 server_time_zone = SVALS(vwv + 10, 0);
3823 server_time_zone *= 60;
3824 /* this time is converted to GMT by make_unix_date */
3825 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3826 unix_to_nt_time(&server_system_time, t);
3827 key_len = SVAL(vwv + 11, 0);
3829 if (num_bytes < key_len) {
3830 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3831 return;
3834 if (key_len != 0 && key_len != 8) {
3835 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3836 return;
3839 if (key_len == 8) {
3840 memcpy(server_challenge, bytes, 8);
3843 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3844 if (blob1.length > 0) {
3845 size_t len;
3846 bool ok;
3848 len = utf16_len_n(blob1.data,
3849 blob1.length);
3850 blob1.length = len;
3852 ok = convert_string_talloc(state,
3853 CH_DOS,
3854 CH_UNIX,
3855 blob1.data,
3856 blob1.length,
3857 &server_workgroup,
3858 &len);
3859 if (!ok) {
3860 status = map_nt_error_from_unix_common(errno);
3861 tevent_req_nterror(req, status);
3862 return;
3866 } else {
3867 /* the old core protocol */
3868 server_time_zone = get_time_zone(time(NULL));
3869 server_max_xmit = 1024;
3870 server_max_mux = 1;
3873 if (server_max_xmit < 1024) {
3874 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3875 return;
3878 if (server_max_mux < 1) {
3879 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3880 return;
3884 * Now calculate the negotiated capabilities
3885 * based on the mask for:
3886 * - client only flags
3887 * - flags used in both directions
3888 * - server only flags
3890 both_capabilities = client_capabilities & server_capabilities;
3891 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3892 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3893 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3895 max_xmit = MIN(client_max_xmit, server_max_xmit);
3897 conn->smb1.server.capabilities = server_capabilities;
3898 conn->smb1.capabilities = capabilities;
3900 conn->smb1.server.max_xmit = server_max_xmit;
3901 conn->smb1.max_xmit = max_xmit;
3903 conn->smb1.server.max_mux = server_max_mux;
3905 conn->smb1.server.security_mode = server_security_mode;
3907 conn->smb1.server.readbraw = server_readbraw;
3908 conn->smb1.server.writebraw = server_writebraw;
3909 conn->smb1.server.lockread = server_lockread;
3910 conn->smb1.server.writeunlock = server_writeunlock;
3912 conn->smb1.server.session_key = server_session_key;
3914 talloc_steal(conn, server_gss_blob.data);
3915 conn->smb1.server.gss_blob = server_gss_blob;
3916 conn->smb1.server.guid = server_guid;
3917 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3918 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3919 conn->smb1.server.name = talloc_move(conn, &server_name);
3921 conn->smb1.server.time_zone = server_time_zone;
3922 conn->smb1.server.system_time = server_system_time;
3924 tevent_req_done(req);
3927 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3929 size_t i;
3930 uint8_t *buf;
3931 uint16_t dialect_count = 0;
3933 buf = state->smb2.dyn;
3934 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3935 if (smb2cli_prots[i].proto < state->min_protocol) {
3936 continue;
3939 if (smb2cli_prots[i].proto > state->max_protocol) {
3940 continue;
3943 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3944 dialect_count++;
3947 buf = state->smb2.fixed;
3948 SSVAL(buf, 0, 36);
3949 SSVAL(buf, 2, dialect_count);
3950 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3951 SSVAL(buf, 6, 0); /* Reserved */
3952 if (state->max_protocol >= PROTOCOL_SMB2_22) {
3953 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
3954 } else {
3955 SIVAL(buf, 8, 0); /* Capabilities */
3957 if (state->max_protocol >= PROTOCOL_SMB2_10) {
3958 NTSTATUS status;
3959 DATA_BLOB blob;
3961 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3962 state, &blob);
3963 if (!NT_STATUS_IS_OK(status)) {
3964 return NULL;
3966 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3967 } else {
3968 memset(buf+12, 0, 16); /* ClientGuid */
3970 SBVAL(buf, 28, 0); /* ClientStartTime */
3972 return smb2cli_req_send(state, state->ev,
3973 state->conn, SMB2_OP_NEGPROT,
3974 0, 0, /* flags */
3975 state->timeout_msec,
3976 0xFEFF, 0, NULL, /* pid, tid, session */
3977 state->smb2.fixed, sizeof(state->smb2.fixed),
3978 state->smb2.dyn, dialect_count*2);
3981 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3983 struct tevent_req *req =
3984 tevent_req_callback_data(subreq,
3985 struct tevent_req);
3986 struct smbXcli_negprot_state *state =
3987 tevent_req_data(req,
3988 struct smbXcli_negprot_state);
3989 struct smbXcli_conn *conn = state->conn;
3990 size_t security_offset, security_length;
3991 DATA_BLOB blob;
3992 NTSTATUS status;
3993 struct iovec *iov;
3994 uint8_t *body;
3995 size_t i;
3996 uint16_t dialect_revision;
3997 static const struct smb2cli_req_expected_response expected[] = {
3999 .status = NT_STATUS_OK,
4000 .body_size = 0x41
4004 status = smb2cli_req_recv(subreq, state, &iov,
4005 expected, ARRAY_SIZE(expected));
4006 TALLOC_FREE(subreq);
4007 if (tevent_req_nterror(req, status)) {
4008 return;
4011 body = (uint8_t *)iov[1].iov_base;
4013 dialect_revision = SVAL(body, 4);
4015 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4016 if (smb2cli_prots[i].proto < state->min_protocol) {
4017 continue;
4020 if (smb2cli_prots[i].proto > state->max_protocol) {
4021 continue;
4024 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4025 continue;
4028 conn->protocol = smb2cli_prots[i].proto;
4029 break;
4032 if (conn->protocol == PROTOCOL_NONE) {
4033 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4034 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4035 return;
4038 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4039 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4040 return;
4043 /* make sure we do not loop forever */
4044 state->min_protocol = PROTOCOL_SMB2_02;
4047 * send a SMB2 negprot, in order to negotiate
4048 * the SMB2 dialect.
4050 subreq = smbXcli_negprot_smb2_subreq(state);
4051 if (tevent_req_nomem(subreq, req)) {
4052 return;
4054 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4055 return;
4058 conn->smb2.server.security_mode = SVAL(body, 2);
4060 blob = data_blob_const(body + 8, 16);
4061 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4062 if (tevent_req_nterror(req, status)) {
4063 return;
4066 conn->smb2.server.capabilities = IVAL(body, 24);
4067 conn->smb2.server.max_trans_size= IVAL(body, 28);
4068 conn->smb2.server.max_read_size = IVAL(body, 32);
4069 conn->smb2.server.max_write_size= IVAL(body, 36);
4070 conn->smb2.server.system_time = BVAL(body, 40);
4071 conn->smb2.server.start_time = BVAL(body, 48);
4073 security_offset = SVAL(body, 56);
4074 security_length = SVAL(body, 58);
4076 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4077 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4078 return;
4081 if (security_length > iov[2].iov_len) {
4082 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4083 return;
4086 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4087 iov[2].iov_base,
4088 security_length);
4089 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4090 return;
4093 tevent_req_done(req);
4096 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4097 TALLOC_CTX *tmp_mem,
4098 uint8_t *inbuf)
4100 size_t num_pending = talloc_array_length(conn->pending);
4101 struct tevent_req *subreq;
4102 struct smbXcli_req_state *substate;
4103 struct tevent_req *req;
4104 uint32_t protocol_magic;
4105 size_t inbuf_len = smb_len_nbt(inbuf);
4107 if (num_pending != 1) {
4108 return NT_STATUS_INTERNAL_ERROR;
4111 if (inbuf_len < 4) {
4112 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4115 subreq = conn->pending[0];
4116 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4117 req = tevent_req_callback_data(subreq, struct tevent_req);
4119 protocol_magic = IVAL(inbuf, 4);
4121 switch (protocol_magic) {
4122 case SMB_MAGIC:
4123 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4124 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4125 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4127 case SMB2_MAGIC:
4128 if (substate->smb2.recv_iov == NULL) {
4130 * For the SMB1 negprot we have move it.
4132 substate->smb2.recv_iov = substate->smb1.recv_iov;
4133 substate->smb1.recv_iov = NULL;
4137 * we got an SMB2 answer, which consumed sequence number 0
4138 * so we need to use 1 as the next one
4140 conn->smb2.mid = 1;
4141 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4142 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4143 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4146 DEBUG(10, ("Got non-SMB PDU\n"));
4147 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4150 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4152 return tevent_req_simple_recv_ntstatus(req);
4155 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4156 uint32_t timeout_msec,
4157 enum protocol_types min_protocol,
4158 enum protocol_types max_protocol)
4160 TALLOC_CTX *frame = talloc_stackframe();
4161 struct tevent_context *ev;
4162 struct tevent_req *req;
4163 NTSTATUS status = NT_STATUS_NO_MEMORY;
4164 bool ok;
4166 if (smbXcli_conn_has_async_calls(conn)) {
4168 * Can't use sync call while an async call is in flight
4170 status = NT_STATUS_INVALID_PARAMETER_MIX;
4171 goto fail;
4173 ev = tevent_context_init(frame);
4174 if (ev == NULL) {
4175 goto fail;
4177 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4178 min_protocol, max_protocol);
4179 if (req == NULL) {
4180 goto fail;
4182 ok = tevent_req_poll(req, ev);
4183 if (!ok) {
4184 status = map_nt_error_from_unix_common(errno);
4185 goto fail;
4187 status = smbXcli_negprot_recv(req);
4188 fail:
4189 TALLOC_FREE(frame);
4190 return status;
4193 static int smbXcli_session_destructor(struct smbXcli_session *session)
4195 if (session->conn == NULL) {
4196 return 0;
4199 DLIST_REMOVE(session->conn->sessions, session);
4200 return 0;
4203 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4204 struct smbXcli_conn *conn)
4206 struct smbXcli_session *session;
4208 session = talloc_zero(mem_ctx, struct smbXcli_session);
4209 if (session == NULL) {
4210 return NULL;
4212 talloc_set_destructor(session, smbXcli_session_destructor);
4214 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4215 session->conn = conn;
4217 return session;
4220 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4222 return session->smb1.session_id;
4225 void smb1cli_session_set_id(struct smbXcli_session *session,
4226 uint16_t session_id)
4228 session->smb1.session_id = session_id;
4231 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4233 struct smbXcli_conn *conn = session->conn;
4234 uint8_t security_mode = 0;
4236 if (conn == NULL) {
4237 return security_mode;
4240 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4241 if (conn->mandatory_signing) {
4242 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4245 return security_mode;
4248 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4250 return session->smb2.session_id;
4253 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4255 return session->smb2.session_flags;
4258 NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
4259 TALLOC_CTX *mem_ctx,
4260 DATA_BLOB *key)
4262 *key = data_blob_null;
4264 if (session->smb2.application_key.length == 0) {
4265 return NT_STATUS_NO_USER_SESSION_KEY;
4268 *key = data_blob_dup_talloc(mem_ctx, session->smb2.application_key);
4269 if (key->data == NULL) {
4270 return NT_STATUS_NO_MEMORY;
4273 return NT_STATUS_OK;
4276 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4277 uint64_t session_id,
4278 uint16_t session_flags)
4280 session->smb2.session_id = session_id;
4281 session->smb2.session_flags = session_flags;
4284 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4285 const DATA_BLOB _session_key,
4286 const struct iovec *recv_iov)
4288 struct smbXcli_conn *conn = session->conn;
4289 uint16_t no_sign_flags;
4290 uint8_t session_key[16];
4291 NTSTATUS status;
4293 if (conn == NULL) {
4294 return NT_STATUS_INVALID_PARAMETER_MIX;
4297 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4299 if (session->smb2.session_flags & no_sign_flags) {
4300 session->smb2.should_sign = false;
4301 return NT_STATUS_OK;
4304 if (session->smb2.signing_key.length != 0) {
4305 return NT_STATUS_INVALID_PARAMETER_MIX;
4308 ZERO_STRUCT(session_key);
4309 memcpy(session_key, _session_key.data,
4310 MIN(_session_key.length, sizeof(session_key)));
4312 session->smb2.signing_key = data_blob_talloc(session,
4313 session_key,
4314 sizeof(session_key));
4315 if (session->smb2.signing_key.data == NULL) {
4316 ZERO_STRUCT(session_key);
4317 return NT_STATUS_NO_MEMORY;
4320 if (conn->protocol >= PROTOCOL_SMB2_24) {
4321 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4322 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4324 smb2_key_derivation(session_key, sizeof(session_key),
4325 label.data, label.length,
4326 context.data, context.length,
4327 session->smb2.signing_key.data);
4330 session->smb2.encryption_key = data_blob_dup_talloc(session,
4331 session->smb2.signing_key);
4332 if (session->smb2.encryption_key.data == NULL) {
4333 ZERO_STRUCT(session_key);
4334 return NT_STATUS_NO_MEMORY;
4337 if (conn->protocol >= PROTOCOL_SMB2_24) {
4338 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4339 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4341 smb2_key_derivation(session_key, sizeof(session_key),
4342 label.data, label.length,
4343 context.data, context.length,
4344 session->smb2.encryption_key.data);
4347 session->smb2.decryption_key = data_blob_dup_talloc(session,
4348 session->smb2.signing_key);
4349 if (session->smb2.decryption_key.data == NULL) {
4350 ZERO_STRUCT(session_key);
4351 return NT_STATUS_NO_MEMORY;
4354 if (conn->protocol >= PROTOCOL_SMB2_24) {
4355 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4356 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4358 smb2_key_derivation(session_key, sizeof(session_key),
4359 label.data, label.length,
4360 context.data, context.length,
4361 session->smb2.decryption_key.data);
4364 session->smb2.application_key = data_blob_dup_talloc(session,
4365 session->smb2.signing_key);
4366 if (session->smb2.application_key.data == NULL) {
4367 ZERO_STRUCT(session_key);
4368 return NT_STATUS_NO_MEMORY;
4371 if (conn->protocol >= PROTOCOL_SMB2_24) {
4372 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4373 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4375 smb2_key_derivation(session_key, sizeof(session_key),
4376 label.data, label.length,
4377 context.data, context.length,
4378 session->smb2.application_key.data);
4380 ZERO_STRUCT(session_key);
4382 session->smb2.channel_signing_key = data_blob_dup_talloc(session,
4383 session->smb2.signing_key);
4384 if (session->smb2.channel_signing_key.data == NULL) {
4385 return NT_STATUS_NO_MEMORY;
4388 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4389 session->conn->protocol,
4390 recv_iov, 3);
4391 if (!NT_STATUS_IS_OK(status)) {
4392 return status;
4395 session->smb2.should_sign = false;
4396 session->smb2.should_encrypt = false;
4398 if (conn->desire_signing) {
4399 session->smb2.should_sign = true;
4402 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4403 session->smb2.should_sign = true;
4406 if (session->smb2.session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4407 session->smb2.should_encrypt = true;
4410 if (conn->protocol < PROTOCOL_SMB2_24) {
4411 session->smb2.should_encrypt = false;
4414 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4415 session->smb2.should_encrypt = false;
4418 generate_random_buffer((uint8_t *)&session->smb2.channel_nonce,
4419 sizeof(session->smb2.channel_nonce));
4420 session->smb2.channel_next = 1;
4422 return NT_STATUS_OK;
4425 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4426 struct smbXcli_session *session1,
4427 struct smbXcli_conn *conn,
4428 struct smbXcli_session **_session2)
4430 struct smbXcli_session *session2;
4432 if (session1->smb2.signing_key.length == 0) {
4433 return NT_STATUS_INVALID_PARAMETER_MIX;
4436 if (session1->smb2.channel_next == 0) {
4437 return NT_STATUS_INVALID_PARAMETER_MIX;
4440 if (conn == NULL) {
4441 return NT_STATUS_INVALID_PARAMETER_MIX;
4444 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4445 if (session2 == NULL) {
4446 return NT_STATUS_NO_MEMORY;
4448 session2->smb2.session_id = session1->smb2.session_id;
4449 session2->smb2.session_flags = session1->smb2.session_flags;
4451 session2->smb2.signing_key = data_blob_dup_talloc(session2,
4452 session1->smb2.signing_key);
4453 if (session2->smb2.signing_key.data == NULL) {
4454 return NT_STATUS_NO_MEMORY;
4457 session2->smb2.application_key = data_blob_dup_talloc(session2,
4458 session1->smb2.application_key);
4459 if (session2->smb2.application_key.data == NULL) {
4460 return NT_STATUS_NO_MEMORY;
4463 session2->smb2.should_sign = session1->smb2.should_sign;
4464 session2->smb2.should_encrypt = session1->smb2.should_encrypt;
4466 session2->smb2.channel_nonce = session1->smb2.channel_nonce;
4467 session2->smb2.channel_nonce += session1->smb2.channel_next;
4468 session1->smb2.channel_next++;
4470 session2->smb2.encryption_key = data_blob_dup_talloc(session2,
4471 session1->smb2.encryption_key);
4472 if (session2->smb2.encryption_key.data == NULL) {
4473 return NT_STATUS_NO_MEMORY;
4476 session2->smb2.decryption_key = data_blob_dup_talloc(session2,
4477 session1->smb2.decryption_key);
4478 if (session2->smb2.decryption_key.data == NULL) {
4479 return NT_STATUS_NO_MEMORY;
4482 talloc_set_destructor(session2, smbXcli_session_destructor);
4483 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4484 session2->conn = conn;
4486 *_session2 = session2;
4487 return NT_STATUS_OK;
4490 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4491 const DATA_BLOB _channel_key,
4492 const struct iovec *recv_iov)
4494 struct smbXcli_conn *conn = session->conn;
4495 uint8_t channel_key[16];
4496 NTSTATUS status;
4498 if (conn == NULL) {
4499 return NT_STATUS_INVALID_PARAMETER_MIX;
4502 if (session->smb2.channel_signing_key.length != 0) {
4503 return NT_STATUS_INVALID_PARAMETER_MIX;
4506 ZERO_STRUCT(channel_key);
4507 memcpy(channel_key, _channel_key.data,
4508 MIN(_channel_key.length, sizeof(channel_key)));
4510 session->smb2.channel_signing_key = data_blob_talloc(session,
4511 channel_key,
4512 sizeof(channel_key));
4513 if (session->smb2.channel_signing_key.data == NULL) {
4514 ZERO_STRUCT(channel_key);
4515 return NT_STATUS_NO_MEMORY;
4518 if (conn->protocol >= PROTOCOL_SMB2_24) {
4519 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4520 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4522 smb2_key_derivation(channel_key, sizeof(channel_key),
4523 label.data, label.length,
4524 context.data, context.length,
4525 session->smb2.channel_signing_key.data);
4527 ZERO_STRUCT(channel_key);
4529 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4530 session->conn->protocol,
4531 recv_iov, 3);
4532 if (!NT_STATUS_IS_OK(status)) {
4533 return status;
4536 return NT_STATUS_OK;