libcli/smb: pass smbXcli_tcon to smb2cli_req_create/send()
[Samba/gebeck_regimport.git] / libcli / smb / smbXcli_base.c
blob1b1b92aa0383290e78bc020895bdd121a67299e0
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;
38 struct smbXcli_tcon;
40 struct smbXcli_conn {
41 int read_fd;
42 int write_fd;
43 struct sockaddr_storage local_ss;
44 struct sockaddr_storage remote_ss;
45 const char *remote_name;
47 struct tevent_queue *outgoing;
48 struct tevent_req **pending;
49 struct tevent_req *read_smb_req;
51 enum protocol_types protocol;
52 bool allow_signing;
53 bool desire_signing;
54 bool mandatory_signing;
57 * The incoming dispatch function should return:
58 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
59 * - NT_STATUS_OK, if no more processing is desired, e.g.
60 * the dispatch function called
61 * tevent_req_done().
62 * - All other return values disconnect the connection.
64 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
65 TALLOC_CTX *tmp_mem,
66 uint8_t *inbuf);
68 struct {
69 struct {
70 uint32_t capabilities;
71 uint32_t max_xmit;
72 } client;
74 struct {
75 uint32_t capabilities;
76 uint32_t max_xmit;
77 uint16_t max_mux;
78 uint16_t security_mode;
79 bool readbraw;
80 bool writebraw;
81 bool lockread;
82 bool writeunlock;
83 uint32_t session_key;
84 struct GUID guid;
85 DATA_BLOB gss_blob;
86 uint8_t challenge[8];
87 const char *workgroup;
88 const char *name;
89 int time_zone;
90 NTTIME system_time;
91 } server;
93 uint32_t capabilities;
94 uint32_t max_xmit;
96 uint16_t mid;
98 struct smb_signing_state *signing;
99 struct smb_trans_enc_state *trans_enc;
101 struct tevent_req *read_braw_req;
102 } smb1;
104 struct {
105 struct {
106 uint32_t capabilities;
107 uint16_t security_mode;
108 struct GUID guid;
109 } client;
111 struct {
112 uint32_t capabilities;
113 uint16_t security_mode;
114 struct GUID guid;
115 uint32_t max_trans_size;
116 uint32_t max_read_size;
117 uint32_t max_write_size;
118 NTTIME system_time;
119 NTTIME start_time;
120 DATA_BLOB gss_blob;
121 } server;
123 uint64_t mid;
124 uint16_t cur_credits;
125 uint16_t max_credits;
126 } smb2;
128 struct smbXcli_session *sessions;
131 struct smbXcli_session {
132 struct smbXcli_session *prev, *next;
133 struct smbXcli_conn *conn;
135 struct {
136 uint16_t session_id;
137 } smb1;
139 struct {
140 uint64_t session_id;
141 uint16_t session_flags;
142 DATA_BLOB application_key;
143 DATA_BLOB signing_key;
144 bool should_sign;
145 bool should_encrypt;
146 DATA_BLOB encryption_key;
147 DATA_BLOB decryption_key;
148 uint64_t channel_nonce;
149 uint64_t channel_next;
150 DATA_BLOB channel_signing_key;
151 } smb2;
154 struct smbXcli_tcon {
155 struct {
156 uint16_t tcon_id;
157 uint16_t optional_support;
158 uint32_t maximal_access;
159 uint32_t guest_maximal_access;
160 char *service;
161 char *fs_type;
162 } smb1;
164 struct {
165 uint32_t tcon_id;
166 uint8_t type;
167 uint32_t flags;
168 uint32_t capabilities;
169 uint32_t maximal_access;
170 } smb2;
173 struct smbXcli_req_state {
174 struct tevent_context *ev;
175 struct smbXcli_conn *conn;
176 struct smbXcli_session *session; /* maybe NULL */
177 struct smbXcli_tcon *tcon; /* maybe NULL */
179 uint8_t length_hdr[4];
181 bool one_way;
183 uint8_t *inbuf;
185 struct {
186 /* Space for the header including the wct */
187 uint8_t hdr[HDR_VWV];
190 * For normal requests, smb1cli_req_send chooses a mid.
191 * SecondaryV trans requests need to use the mid of the primary
192 * request, so we need a place to store it.
193 * Assume it is set if != 0.
195 uint16_t mid;
197 uint16_t *vwv;
198 uint8_t bytecount_buf[2];
200 #define MAX_SMB_IOV 10
201 /* length_hdr, hdr, words, byte_count, buffers */
202 struct iovec iov[1 + 3 + MAX_SMB_IOV];
203 int iov_count;
205 bool one_way_seqnum;
206 uint32_t seqnum;
207 struct tevent_req **chained_requests;
209 uint8_t recv_cmd;
210 NTSTATUS recv_status;
211 /* always an array of 3 talloc elements */
212 struct iovec *recv_iov;
213 } smb1;
215 struct {
216 const uint8_t *fixed;
217 uint16_t fixed_len;
218 const uint8_t *dyn;
219 uint32_t dyn_len;
221 uint8_t transform[SMB2_TF_HDR_SIZE];
222 uint8_t hdr[SMB2_HDR_BODY];
223 uint8_t pad[7]; /* padding space for compounding */
226 * always an array of 3 talloc elements
227 * (without a SMB2_TRANSFORM header!)
229 * HDR, BODY, DYN
231 struct iovec *recv_iov;
233 uint16_t credit_charge;
235 bool should_sign;
236 bool should_encrypt;
238 bool signing_skipped;
239 bool notify_async;
240 bool got_async;
241 } smb2;
244 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
247 * NT_STATUS_OK, means we do not notify the callers
249 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
251 while (conn->sessions) {
252 conn->sessions->conn = NULL;
253 DLIST_REMOVE(conn->sessions, conn->sessions);
256 if (conn->smb1.trans_enc) {
257 TALLOC_FREE(conn->smb1.trans_enc);
260 return 0;
263 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
264 int fd,
265 const char *remote_name,
266 enum smb_signing_setting signing_state,
267 uint32_t smb1_capabilities,
268 struct GUID *client_guid,
269 uint32_t smb2_capabilities)
271 struct smbXcli_conn *conn = NULL;
272 void *ss = NULL;
273 struct sockaddr *sa = NULL;
274 socklen_t sa_length;
275 int ret;
277 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
278 if (!conn) {
279 return NULL;
282 conn->read_fd = fd;
283 conn->write_fd = dup(fd);
284 if (conn->write_fd == -1) {
285 goto error;
288 conn->remote_name = talloc_strdup(conn, remote_name);
289 if (conn->remote_name == NULL) {
290 goto error;
294 ss = (void *)&conn->local_ss;
295 sa = (struct sockaddr *)ss;
296 sa_length = sizeof(conn->local_ss);
297 ret = getsockname(fd, sa, &sa_length);
298 if (ret == -1) {
299 goto error;
301 ss = (void *)&conn->remote_ss;
302 sa = (struct sockaddr *)ss;
303 sa_length = sizeof(conn->remote_ss);
304 ret = getpeername(fd, sa, &sa_length);
305 if (ret == -1) {
306 goto error;
309 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
310 if (conn->outgoing == NULL) {
311 goto error;
313 conn->pending = NULL;
315 conn->protocol = PROTOCOL_NONE;
317 switch (signing_state) {
318 case SMB_SIGNING_OFF:
319 /* never */
320 conn->allow_signing = false;
321 conn->desire_signing = false;
322 conn->mandatory_signing = false;
323 break;
324 case SMB_SIGNING_DEFAULT:
325 case SMB_SIGNING_IF_REQUIRED:
326 /* if the server requires it */
327 conn->allow_signing = true;
328 conn->desire_signing = false;
329 conn->mandatory_signing = false;
330 break;
331 case SMB_SIGNING_REQUIRED:
332 /* always */
333 conn->allow_signing = true;
334 conn->desire_signing = true;
335 conn->mandatory_signing = true;
336 break;
339 conn->smb1.client.capabilities = smb1_capabilities;
340 conn->smb1.client.max_xmit = UINT16_MAX;
342 conn->smb1.capabilities = conn->smb1.client.capabilities;
343 conn->smb1.max_xmit = 1024;
345 conn->smb1.mid = 1;
347 /* initialise signing */
348 conn->smb1.signing = smb_signing_init(conn,
349 conn->allow_signing,
350 conn->desire_signing,
351 conn->mandatory_signing);
352 if (!conn->smb1.signing) {
353 goto error;
356 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
357 if (conn->mandatory_signing) {
358 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
360 if (client_guid) {
361 conn->smb2.client.guid = *client_guid;
363 conn->smb2.client.capabilities = smb2_capabilities;
365 conn->smb2.cur_credits = 1;
366 conn->smb2.max_credits = 0;
368 talloc_set_destructor(conn, smbXcli_conn_destructor);
369 return conn;
371 error:
372 if (conn->write_fd != -1) {
373 close(conn->write_fd);
375 TALLOC_FREE(conn);
376 return NULL;
379 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
381 if (conn == NULL) {
382 return false;
385 if (conn->read_fd == -1) {
386 return false;
389 return true;
392 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
394 return conn->protocol;
397 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
399 if (conn->protocol >= PROTOCOL_SMB2_02) {
400 return true;
403 if (conn->smb1.capabilities & CAP_UNICODE) {
404 return true;
407 return false;
410 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
412 set_socket_options(conn->read_fd, options);
415 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
417 return &conn->local_ss;
420 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
422 return &conn->remote_ss;
425 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
427 return conn->remote_name;
430 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
432 if (conn->protocol >= PROTOCOL_SMB2_02) {
434 * TODO...
436 return 1;
439 return conn->smb1.server.max_mux;
442 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
444 if (conn->protocol >= PROTOCOL_SMB2_02) {
445 return conn->smb2.server.system_time;
448 return conn->smb1.server.system_time;
451 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
453 if (conn->protocol >= PROTOCOL_SMB2_02) {
454 return &conn->smb2.server.gss_blob;
457 return &conn->smb1.server.gss_blob;
460 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
462 if (conn->protocol >= PROTOCOL_SMB2_02) {
463 return &conn->smb2.server.guid;
466 return &conn->smb1.server.guid;
469 struct smbXcli_conn_samba_suicide_state {
470 struct smbXcli_conn *conn;
471 struct iovec iov;
472 uint8_t buf[9];
475 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
477 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
478 struct tevent_context *ev,
479 struct smbXcli_conn *conn,
480 uint8_t exitcode)
482 struct tevent_req *req, *subreq;
483 struct smbXcli_conn_samba_suicide_state *state;
485 req = tevent_req_create(mem_ctx, &state,
486 struct smbXcli_conn_samba_suicide_state);
487 if (req == NULL) {
488 return NULL;
490 state->conn = conn;
491 SIVAL(state->buf, 4, 0x74697865);
492 SCVAL(state->buf, 8, exitcode);
493 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
495 state->iov.iov_base = state->buf;
496 state->iov.iov_len = sizeof(state->buf);
498 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
499 false, &state->iov, 1);
500 if (tevent_req_nomem(subreq, req)) {
501 return tevent_req_post(req, ev);
503 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
504 return req;
507 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
509 struct tevent_req *req = tevent_req_callback_data(
510 subreq, struct tevent_req);
511 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
512 req, struct smbXcli_conn_samba_suicide_state);
513 ssize_t nwritten;
514 int err;
516 nwritten = writev_recv(subreq, &err);
517 TALLOC_FREE(subreq);
518 if (nwritten == -1) {
519 NTSTATUS status = map_nt_error_from_unix_common(err);
520 smbXcli_conn_disconnect(state->conn, status);
521 return;
523 tevent_req_done(req);
526 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
528 return tevent_req_simple_recv_ntstatus(req);
531 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
532 uint8_t exitcode)
534 TALLOC_CTX *frame = talloc_stackframe();
535 struct tevent_context *ev;
536 struct tevent_req *req;
537 NTSTATUS status = NT_STATUS_NO_MEMORY;
538 bool ok;
540 if (smbXcli_conn_has_async_calls(conn)) {
542 * Can't use sync call while an async call is in flight
544 status = NT_STATUS_INVALID_PARAMETER_MIX;
545 goto fail;
547 ev = tevent_context_init(frame);
548 if (ev == NULL) {
549 goto fail;
551 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
552 if (req == NULL) {
553 goto fail;
555 ok = tevent_req_poll(req, ev);
556 if (!ok) {
557 status = map_nt_error_from_unix_common(errno);
558 goto fail;
560 status = smbXcli_conn_samba_suicide_recv(req);
561 fail:
562 TALLOC_FREE(frame);
563 return status;
566 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
568 return conn->smb1.capabilities;
571 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
573 return conn->smb1.max_xmit;
576 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
578 return conn->smb1.server.session_key;
581 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
583 return conn->smb1.server.challenge;
586 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
588 return conn->smb1.server.security_mode;
591 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
593 return conn->smb1.server.readbraw;
596 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
598 return conn->smb1.server.writebraw;
601 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
603 return conn->smb1.server.lockread;
606 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
608 return conn->smb1.server.writeunlock;
611 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
613 return conn->smb1.server.time_zone;
616 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
617 const DATA_BLOB user_session_key,
618 const DATA_BLOB response)
620 return smb_signing_activate(conn->smb1.signing,
621 user_session_key,
622 response);
625 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
626 const uint8_t *buf, uint32_t seqnum)
628 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
631 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
633 return smb_signing_is_active(conn->smb1.signing);
636 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
637 struct smb_trans_enc_state *es)
639 /* Replace the old state, if any. */
640 if (conn->smb1.trans_enc) {
641 TALLOC_FREE(conn->smb1.trans_enc);
643 conn->smb1.trans_enc = es;
646 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
648 return common_encryption_on(conn->smb1.trans_enc);
652 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
654 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
655 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
657 if (NT_STATUS_IS_OK(status)) {
658 return NT_STATUS_OK;
661 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
662 return status;
665 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
669 * Is the SMB command able to hold an AND_X successor
670 * @param[in] cmd The SMB command in question
671 * @retval Can we add a chained request after "cmd"?
673 bool smb1cli_is_andx_req(uint8_t cmd)
675 switch (cmd) {
676 case SMBtconX:
677 case SMBlockingX:
678 case SMBopenX:
679 case SMBreadX:
680 case SMBwriteX:
681 case SMBsesssetupX:
682 case SMBulogoffX:
683 case SMBntcreateX:
684 return true;
685 break;
686 default:
687 break;
690 return false;
693 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
695 size_t num_pending = talloc_array_length(conn->pending);
696 uint16_t result;
698 while (true) {
699 size_t i;
701 result = conn->smb1.mid++;
702 if ((result == 0) || (result == 0xffff)) {
703 continue;
706 for (i=0; i<num_pending; i++) {
707 if (result == smb1cli_req_mid(conn->pending[i])) {
708 break;
712 if (i == num_pending) {
713 return result;
718 void smbXcli_req_unset_pending(struct tevent_req *req)
720 struct smbXcli_req_state *state =
721 tevent_req_data(req,
722 struct smbXcli_req_state);
723 struct smbXcli_conn *conn = state->conn;
724 size_t num_pending = talloc_array_length(conn->pending);
725 size_t i;
727 if (state->smb1.mid != 0) {
729 * This is a [nt]trans[2] request which waits
730 * for more than one reply.
732 return;
735 talloc_set_destructor(req, NULL);
737 if (num_pending == 1) {
739 * The pending read_smb tevent_req is a child of
740 * conn->pending. So if nothing is pending anymore, we need to
741 * delete the socket read fde.
743 TALLOC_FREE(conn->pending);
744 conn->read_smb_req = NULL;
745 return;
748 for (i=0; i<num_pending; i++) {
749 if (req == conn->pending[i]) {
750 break;
753 if (i == num_pending) {
755 * Something's seriously broken. Just returning here is the
756 * right thing nevertheless, the point of this routine is to
757 * remove ourselves from conn->pending.
759 return;
763 * Remove ourselves from the conn->pending array
765 for (; i < (num_pending - 1); i++) {
766 conn->pending[i] = conn->pending[i+1];
770 * No NULL check here, we're shrinking by sizeof(void *), and
771 * talloc_realloc just adjusts the size for this.
773 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
774 num_pending - 1);
775 return;
778 static int smbXcli_req_destructor(struct tevent_req *req)
780 struct smbXcli_req_state *state =
781 tevent_req_data(req,
782 struct smbXcli_req_state);
785 * Make sure we really remove it from
786 * the pending array on destruction.
788 state->smb1.mid = 0;
789 smbXcli_req_unset_pending(req);
790 return 0;
793 static bool smb1cli_req_cancel(struct tevent_req *req);
794 static bool smb2cli_req_cancel(struct tevent_req *req);
796 static bool smbXcli_req_cancel(struct tevent_req *req)
798 struct smbXcli_req_state *state =
799 tevent_req_data(req,
800 struct smbXcli_req_state);
802 if (!smbXcli_conn_is_connected(state->conn)) {
803 return false;
806 if (state->conn->protocol == PROTOCOL_NONE) {
807 return false;
810 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
811 return smb2cli_req_cancel(req);
814 return smb1cli_req_cancel(req);
817 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
819 bool smbXcli_req_set_pending(struct tevent_req *req)
821 struct smbXcli_req_state *state =
822 tevent_req_data(req,
823 struct smbXcli_req_state);
824 struct smbXcli_conn *conn;
825 struct tevent_req **pending;
826 size_t num_pending;
828 conn = state->conn;
830 if (!smbXcli_conn_is_connected(conn)) {
831 return false;
834 num_pending = talloc_array_length(conn->pending);
836 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
837 num_pending+1);
838 if (pending == NULL) {
839 return false;
841 pending[num_pending] = req;
842 conn->pending = pending;
843 talloc_set_destructor(req, smbXcli_req_destructor);
844 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
846 if (!smbXcli_conn_receive_next(conn)) {
848 * the caller should notify the current request
850 * And all other pending requests get notified
851 * by smbXcli_conn_disconnect().
853 smbXcli_req_unset_pending(req);
854 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
855 return false;
858 return true;
861 static void smbXcli_conn_received(struct tevent_req *subreq);
863 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
865 size_t num_pending = talloc_array_length(conn->pending);
866 struct tevent_req *req;
867 struct smbXcli_req_state *state;
869 if (conn->read_smb_req != NULL) {
870 return true;
873 if (num_pending == 0) {
874 if (conn->smb2.mid < UINT64_MAX) {
875 /* no more pending requests, so we are done for now */
876 return true;
880 * If there are no more SMB2 requests possible,
881 * because we are out of message ids,
882 * we need to disconnect.
884 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
885 return true;
888 req = conn->pending[0];
889 state = tevent_req_data(req, struct smbXcli_req_state);
892 * We're the first ones, add the read_smb request that waits for the
893 * answer from the server
895 conn->read_smb_req = read_smb_send(conn->pending,
896 state->ev,
897 conn->read_fd);
898 if (conn->read_smb_req == NULL) {
899 return false;
901 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
902 return true;
905 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
907 tevent_queue_stop(conn->outgoing);
909 if (conn->read_fd != -1) {
910 close(conn->read_fd);
912 if (conn->write_fd != -1) {
913 close(conn->write_fd);
915 conn->read_fd = -1;
916 conn->write_fd = -1;
919 * Cancel all pending requests. We do not do a for-loop walking
920 * conn->pending because that array changes in
921 * smbXcli_req_unset_pending.
923 while (talloc_array_length(conn->pending) > 0) {
924 struct tevent_req *req;
925 struct smbXcli_req_state *state;
926 struct tevent_req **chain;
927 size_t num_chained;
928 size_t i;
930 req = conn->pending[0];
931 state = tevent_req_data(req, struct smbXcli_req_state);
933 if (state->smb1.chained_requests == NULL) {
935 * We're dead. No point waiting for trans2
936 * replies.
938 state->smb1.mid = 0;
940 smbXcli_req_unset_pending(req);
942 if (NT_STATUS_IS_OK(status)) {
943 /* do not notify the callers */
944 continue;
948 * we need to defer the callback, because we may notify
949 * more then one caller.
951 tevent_req_defer_callback(req, state->ev);
952 tevent_req_nterror(req, status);
953 continue;
956 chain = talloc_move(conn, &state->smb1.chained_requests);
957 num_chained = talloc_array_length(chain);
959 for (i=0; i<num_chained; i++) {
960 req = chain[i];
961 state = tevent_req_data(req, struct smbXcli_req_state);
964 * We're dead. No point waiting for trans2
965 * replies.
967 state->smb1.mid = 0;
969 smbXcli_req_unset_pending(req);
971 if (NT_STATUS_IS_OK(status)) {
972 /* do not notify the callers */
973 continue;
977 * we need to defer the callback, because we may notify
978 * more than one caller.
980 tevent_req_defer_callback(req, state->ev);
981 tevent_req_nterror(req, status);
983 TALLOC_FREE(chain);
988 * Fetch a smb request's mid. Only valid after the request has been sent by
989 * smb1cli_req_send().
991 uint16_t smb1cli_req_mid(struct tevent_req *req)
993 struct smbXcli_req_state *state =
994 tevent_req_data(req,
995 struct smbXcli_req_state);
997 if (state->smb1.mid != 0) {
998 return state->smb1.mid;
1001 return SVAL(state->smb1.hdr, HDR_MID);
1004 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1006 struct smbXcli_req_state *state =
1007 tevent_req_data(req,
1008 struct smbXcli_req_state);
1010 state->smb1.mid = mid;
1013 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1015 struct smbXcli_req_state *state =
1016 tevent_req_data(req,
1017 struct smbXcli_req_state);
1019 return state->smb1.seqnum;
1022 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1024 struct smbXcli_req_state *state =
1025 tevent_req_data(req,
1026 struct smbXcli_req_state);
1028 state->smb1.seqnum = seqnum;
1031 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1033 size_t result = 0;
1034 int i;
1035 for (i=0; i<count; i++) {
1036 result += iov[i].iov_len;
1038 return result;
1041 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1042 const struct iovec *iov,
1043 int count)
1045 size_t len = smbXcli_iov_len(iov, count);
1046 size_t copied;
1047 uint8_t *buf;
1048 int i;
1050 buf = talloc_array(mem_ctx, uint8_t, len);
1051 if (buf == NULL) {
1052 return NULL;
1054 copied = 0;
1055 for (i=0; i<count; i++) {
1056 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1057 copied += iov[i].iov_len;
1059 return buf;
1062 static void smb1cli_req_flags(enum protocol_types protocol,
1063 uint32_t smb1_capabilities,
1064 uint8_t smb_command,
1065 uint8_t additional_flags,
1066 uint8_t clear_flags,
1067 uint8_t *_flags,
1068 uint16_t additional_flags2,
1069 uint16_t clear_flags2,
1070 uint16_t *_flags2)
1072 uint8_t flags = 0;
1073 uint16_t flags2 = 0;
1075 if (protocol >= PROTOCOL_LANMAN1) {
1076 flags |= FLAG_CASELESS_PATHNAMES;
1077 flags |= FLAG_CANONICAL_PATHNAMES;
1080 if (protocol >= PROTOCOL_LANMAN2) {
1081 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1082 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1085 if (protocol >= PROTOCOL_NT1) {
1086 flags2 |= FLAGS2_IS_LONG_NAME;
1088 if (smb1_capabilities & CAP_UNICODE) {
1089 flags2 |= FLAGS2_UNICODE_STRINGS;
1091 if (smb1_capabilities & CAP_STATUS32) {
1092 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1094 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1095 flags2 |= FLAGS2_EXTENDED_SECURITY;
1099 flags |= additional_flags;
1100 flags &= ~clear_flags;
1101 flags2 |= additional_flags2;
1102 flags2 &= ~clear_flags2;
1104 *_flags = flags;
1105 *_flags2 = flags2;
1108 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1110 static bool smb1cli_req_cancel(struct tevent_req *req)
1112 struct smbXcli_req_state *state =
1113 tevent_req_data(req,
1114 struct smbXcli_req_state);
1115 uint8_t flags;
1116 uint16_t flags2;
1117 uint32_t pid;
1118 uint16_t mid;
1119 struct tevent_req *subreq;
1120 NTSTATUS status;
1122 flags = CVAL(state->smb1.hdr, HDR_FLG);
1123 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1124 pid = SVAL(state->smb1.hdr, HDR_PID);
1125 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1126 mid = SVAL(state->smb1.hdr, HDR_MID);
1128 subreq = smb1cli_req_create(state, state->ev,
1129 state->conn,
1130 SMBntcancel,
1131 flags, 0,
1132 flags2, 0,
1133 0, /* timeout */
1134 pid,
1135 state->tcon,
1136 state->session,
1137 0, NULL, /* vwv */
1138 0, NULL); /* bytes */
1139 if (subreq == NULL) {
1140 return false;
1142 smb1cli_req_set_mid(subreq, mid);
1144 status = smb1cli_req_chain_submit(&subreq, 1);
1145 if (!NT_STATUS_IS_OK(status)) {
1146 TALLOC_FREE(subreq);
1147 return false;
1149 smb1cli_req_set_mid(subreq, 0);
1151 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1153 return true;
1156 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1158 /* we do not care about the result */
1159 TALLOC_FREE(subreq);
1162 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1163 struct tevent_context *ev,
1164 struct smbXcli_conn *conn,
1165 uint8_t smb_command,
1166 uint8_t additional_flags,
1167 uint8_t clear_flags,
1168 uint16_t additional_flags2,
1169 uint16_t clear_flags2,
1170 uint32_t timeout_msec,
1171 uint32_t pid,
1172 struct smbXcli_tcon *tcon,
1173 struct smbXcli_session *session,
1174 uint8_t wct, uint16_t *vwv,
1175 int iov_count,
1176 struct iovec *bytes_iov)
1178 struct tevent_req *req;
1179 struct smbXcli_req_state *state;
1180 uint8_t flags = 0;
1181 uint16_t flags2 = 0;
1182 uint16_t uid = 0;
1183 uint16_t tid = 0;
1185 if (iov_count > MAX_SMB_IOV) {
1187 * Should not happen :-)
1189 return NULL;
1192 req = tevent_req_create(mem_ctx, &state,
1193 struct smbXcli_req_state);
1194 if (req == NULL) {
1195 return NULL;
1197 state->ev = ev;
1198 state->conn = conn;
1199 state->session = session;
1200 state->tcon = tcon;
1202 if (session) {
1203 uid = session->smb1.session_id;
1206 if (tcon) {
1207 tid = tcon->smb1.tcon_id;
1210 state->smb1.recv_cmd = 0xFF;
1211 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1212 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1213 if (state->smb1.recv_iov == NULL) {
1214 TALLOC_FREE(req);
1215 return NULL;
1218 smb1cli_req_flags(conn->protocol,
1219 conn->smb1.capabilities,
1220 smb_command,
1221 additional_flags,
1222 clear_flags,
1223 &flags,
1224 additional_flags2,
1225 clear_flags2,
1226 &flags2);
1228 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1229 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1230 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1231 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1232 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1233 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1234 SSVAL(state->smb1.hdr, HDR_TID, tid);
1235 SSVAL(state->smb1.hdr, HDR_PID, pid);
1236 SSVAL(state->smb1.hdr, HDR_UID, uid);
1237 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1238 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1240 state->smb1.vwv = vwv;
1242 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1244 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1245 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1246 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1247 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1248 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1249 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1250 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1251 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1253 if (iov_count != 0) {
1254 memcpy(&state->smb1.iov[4], bytes_iov,
1255 iov_count * sizeof(*bytes_iov));
1257 state->smb1.iov_count = iov_count + 4;
1259 if (timeout_msec > 0) {
1260 struct timeval endtime;
1262 endtime = timeval_current_ofs_msec(timeout_msec);
1263 if (!tevent_req_set_endtime(req, ev, endtime)) {
1264 return req;
1268 switch (smb_command) {
1269 case SMBtranss:
1270 case SMBtranss2:
1271 case SMBnttranss:
1272 state->one_way = true;
1273 break;
1274 case SMBntcancel:
1275 state->one_way = true;
1276 state->smb1.one_way_seqnum = true;
1277 break;
1278 case SMBlockingX:
1279 if ((wct == 8) &&
1280 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1281 state->one_way = true;
1283 break;
1286 return req;
1289 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1290 struct iovec *iov, int iov_count,
1291 uint32_t *seqnum,
1292 bool one_way_seqnum)
1294 TALLOC_CTX *frame = NULL;
1295 uint8_t *buf;
1298 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1299 * iovec directly. MD5Update would do that just fine.
1302 if (iov_count < 4) {
1303 return NT_STATUS_INVALID_PARAMETER_MIX;
1305 if (iov[0].iov_len != NBT_HDR_SIZE) {
1306 return NT_STATUS_INVALID_PARAMETER_MIX;
1308 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1309 return NT_STATUS_INVALID_PARAMETER_MIX;
1311 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1312 return NT_STATUS_INVALID_PARAMETER_MIX;
1314 if (iov[3].iov_len != sizeof(uint16_t)) {
1315 return NT_STATUS_INVALID_PARAMETER_MIX;
1318 frame = talloc_stackframe();
1320 buf = smbXcli_iov_concat(frame, iov, iov_count);
1321 if (buf == NULL) {
1322 return NT_STATUS_NO_MEMORY;
1325 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1326 one_way_seqnum);
1327 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1328 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1330 TALLOC_FREE(frame);
1331 return NT_STATUS_OK;
1334 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1335 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1336 TALLOC_CTX *tmp_mem,
1337 uint8_t *inbuf);
1339 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1340 struct smbXcli_req_state *state,
1341 struct iovec *iov, int iov_count)
1343 struct tevent_req *subreq;
1344 NTSTATUS status;
1345 uint8_t cmd;
1346 uint16_t mid;
1348 if (!smbXcli_conn_is_connected(state->conn)) {
1349 return NT_STATUS_CONNECTION_DISCONNECTED;
1352 if (state->conn->protocol > PROTOCOL_NT1) {
1353 return NT_STATUS_REVISION_MISMATCH;
1356 if (iov_count < 4) {
1357 return NT_STATUS_INVALID_PARAMETER_MIX;
1359 if (iov[0].iov_len != NBT_HDR_SIZE) {
1360 return NT_STATUS_INVALID_PARAMETER_MIX;
1362 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1363 return NT_STATUS_INVALID_PARAMETER_MIX;
1365 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1366 return NT_STATUS_INVALID_PARAMETER_MIX;
1368 if (iov[3].iov_len != sizeof(uint16_t)) {
1369 return NT_STATUS_INVALID_PARAMETER_MIX;
1372 cmd = CVAL(iov[1].iov_base, HDR_COM);
1373 if (cmd == SMBreadBraw) {
1374 if (smbXcli_conn_has_async_calls(state->conn)) {
1375 return NT_STATUS_INVALID_PARAMETER_MIX;
1377 state->conn->smb1.read_braw_req = req;
1380 if (state->smb1.mid != 0) {
1381 mid = state->smb1.mid;
1382 } else {
1383 mid = smb1cli_alloc_mid(state->conn);
1385 SSVAL(iov[1].iov_base, HDR_MID, mid);
1387 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1389 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1390 &state->smb1.seqnum,
1391 state->smb1.one_way_seqnum);
1393 if (!NT_STATUS_IS_OK(status)) {
1394 return status;
1398 * If we supported multiple encrytion contexts
1399 * here we'd look up based on tid.
1401 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1402 char *buf, *enc_buf;
1404 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1405 if (buf == NULL) {
1406 return NT_STATUS_NO_MEMORY;
1408 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1409 (char *)buf, &enc_buf);
1410 TALLOC_FREE(buf);
1411 if (!NT_STATUS_IS_OK(status)) {
1412 DEBUG(0, ("Error in encrypting client message: %s\n",
1413 nt_errstr(status)));
1414 return status;
1416 buf = (char *)talloc_memdup(state, enc_buf,
1417 smb_len_nbt(enc_buf)+4);
1418 SAFE_FREE(enc_buf);
1419 if (buf == NULL) {
1420 return NT_STATUS_NO_MEMORY;
1422 iov[0].iov_base = (void *)buf;
1423 iov[0].iov_len = talloc_get_size(buf);
1424 iov_count = 1;
1427 if (state->conn->dispatch_incoming == NULL) {
1428 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1431 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1433 subreq = writev_send(state, state->ev, state->conn->outgoing,
1434 state->conn->write_fd, false, iov, iov_count);
1435 if (subreq == NULL) {
1436 return NT_STATUS_NO_MEMORY;
1438 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1439 return NT_STATUS_OK;
1442 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1443 struct tevent_context *ev,
1444 struct smbXcli_conn *conn,
1445 uint8_t smb_command,
1446 uint8_t additional_flags,
1447 uint8_t clear_flags,
1448 uint16_t additional_flags2,
1449 uint16_t clear_flags2,
1450 uint32_t timeout_msec,
1451 uint32_t pid,
1452 struct smbXcli_tcon *tcon,
1453 struct smbXcli_session *session,
1454 uint8_t wct, uint16_t *vwv,
1455 uint32_t num_bytes,
1456 const uint8_t *bytes)
1458 struct tevent_req *req;
1459 struct iovec iov;
1460 NTSTATUS status;
1462 iov.iov_base = discard_const_p(void, bytes);
1463 iov.iov_len = num_bytes;
1465 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1466 additional_flags, clear_flags,
1467 additional_flags2, clear_flags2,
1468 timeout_msec,
1469 pid, tcon, session,
1470 wct, vwv, 1, &iov);
1471 if (req == NULL) {
1472 return NULL;
1474 if (!tevent_req_is_in_progress(req)) {
1475 return tevent_req_post(req, ev);
1477 status = smb1cli_req_chain_submit(&req, 1);
1478 if (tevent_req_nterror(req, status)) {
1479 return tevent_req_post(req, ev);
1481 return req;
1484 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1486 struct tevent_req *req =
1487 tevent_req_callback_data(subreq,
1488 struct tevent_req);
1489 struct smbXcli_req_state *state =
1490 tevent_req_data(req,
1491 struct smbXcli_req_state);
1492 ssize_t nwritten;
1493 int err;
1495 nwritten = writev_recv(subreq, &err);
1496 TALLOC_FREE(subreq);
1497 if (nwritten == -1) {
1498 NTSTATUS status = map_nt_error_from_unix_common(err);
1499 smbXcli_conn_disconnect(state->conn, status);
1500 return;
1503 if (state->one_way) {
1504 state->inbuf = NULL;
1505 tevent_req_done(req);
1506 return;
1509 if (!smbXcli_req_set_pending(req)) {
1510 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1511 return;
1515 static void smbXcli_conn_received(struct tevent_req *subreq)
1517 struct smbXcli_conn *conn =
1518 tevent_req_callback_data(subreq,
1519 struct smbXcli_conn);
1520 TALLOC_CTX *frame = talloc_stackframe();
1521 NTSTATUS status;
1522 uint8_t *inbuf;
1523 ssize_t received;
1524 int err;
1526 if (subreq != conn->read_smb_req) {
1527 DEBUG(1, ("Internal error: cli_smb_received called with "
1528 "unexpected subreq\n"));
1529 status = NT_STATUS_INTERNAL_ERROR;
1530 smbXcli_conn_disconnect(conn, status);
1531 TALLOC_FREE(frame);
1532 return;
1534 conn->read_smb_req = NULL;
1536 received = read_smb_recv(subreq, frame, &inbuf, &err);
1537 TALLOC_FREE(subreq);
1538 if (received == -1) {
1539 status = map_nt_error_from_unix_common(err);
1540 smbXcli_conn_disconnect(conn, status);
1541 TALLOC_FREE(frame);
1542 return;
1545 status = conn->dispatch_incoming(conn, frame, inbuf);
1546 TALLOC_FREE(frame);
1547 if (NT_STATUS_IS_OK(status)) {
1549 * We should not do any more processing
1550 * as the dispatch function called
1551 * tevent_req_done().
1553 return;
1554 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1556 * We got an error, so notify all pending requests
1558 smbXcli_conn_disconnect(conn, status);
1559 return;
1563 * We got NT_STATUS_RETRY, so we may ask for a
1564 * next incoming pdu.
1566 if (!smbXcli_conn_receive_next(conn)) {
1567 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1571 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1572 struct iovec **piov, int *pnum_iov)
1574 struct iovec *iov;
1575 int num_iov;
1576 size_t buflen;
1577 size_t taken;
1578 size_t remaining;
1579 uint8_t *hdr;
1580 uint8_t cmd;
1581 uint32_t wct_ofs;
1583 buflen = smb_len_nbt(buf);
1584 taken = 0;
1586 hdr = buf + NBT_HDR_SIZE;
1588 if (buflen < MIN_SMB_SIZE) {
1589 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1593 * This returns iovec elements in the following order:
1595 * - SMB header
1597 * - Parameter Block
1598 * - Data Block
1600 * - Parameter Block
1601 * - Data Block
1603 * - Parameter Block
1604 * - Data Block
1606 num_iov = 1;
1608 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1609 if (iov == NULL) {
1610 return NT_STATUS_NO_MEMORY;
1612 iov[0].iov_base = hdr;
1613 iov[0].iov_len = HDR_WCT;
1614 taken += HDR_WCT;
1616 cmd = CVAL(hdr, HDR_COM);
1617 wct_ofs = HDR_WCT;
1619 while (true) {
1620 size_t len = buflen - taken;
1621 struct iovec *cur;
1622 struct iovec *iov_tmp;
1623 uint8_t wct;
1624 uint32_t bcc_ofs;
1625 uint16_t bcc;
1626 size_t needed;
1629 * we need at least WCT and BCC
1631 needed = sizeof(uint8_t) + sizeof(uint16_t);
1632 if (len < needed) {
1633 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1634 __location__, (int)len, (int)needed));
1635 goto inval;
1639 * Now we check if the specified words are there
1641 wct = CVAL(hdr, wct_ofs);
1642 needed += wct * sizeof(uint16_t);
1643 if (len < needed) {
1644 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1645 __location__, (int)len, (int)needed));
1646 goto inval;
1650 * Now we check if the specified bytes are there
1652 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1653 bcc = SVAL(hdr, bcc_ofs);
1654 needed += bcc * sizeof(uint8_t);
1655 if (len < needed) {
1656 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1657 __location__, (int)len, (int)needed));
1658 goto inval;
1662 * we allocate 2 iovec structures for words and bytes
1664 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1665 num_iov + 2);
1666 if (iov_tmp == NULL) {
1667 TALLOC_FREE(iov);
1668 return NT_STATUS_NO_MEMORY;
1670 iov = iov_tmp;
1671 cur = &iov[num_iov];
1672 num_iov += 2;
1674 cur[0].iov_len = wct * sizeof(uint16_t);
1675 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1676 cur[1].iov_len = bcc * sizeof(uint8_t);
1677 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1679 taken += needed;
1681 if (!smb1cli_is_andx_req(cmd)) {
1683 * If the current command does not have AndX chanining
1684 * we are done.
1686 break;
1689 if (wct == 0 && bcc == 0) {
1691 * An empty response also ends the chain,
1692 * most likely with an error.
1694 break;
1697 if (wct < 2) {
1698 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1699 __location__, (int)wct, (int)cmd));
1700 goto inval;
1702 cmd = CVAL(cur[0].iov_base, 0);
1703 if (cmd == 0xFF) {
1705 * If it is the end of the chain we are also done.
1707 break;
1709 wct_ofs = SVAL(cur[0].iov_base, 2);
1711 if (wct_ofs < taken) {
1712 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1714 if (wct_ofs > buflen) {
1715 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1719 * we consumed everything up to the start of the next
1720 * parameter block.
1722 taken = wct_ofs;
1725 remaining = buflen - taken;
1727 if (remaining > 0 && num_iov >= 3) {
1729 * The last DATA block gets the remaining
1730 * bytes, this is needed to support
1731 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1733 iov[num_iov-1].iov_len += remaining;
1736 *piov = iov;
1737 *pnum_iov = num_iov;
1738 return NT_STATUS_OK;
1740 inval:
1741 TALLOC_FREE(iov);
1742 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1745 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1746 TALLOC_CTX *tmp_mem,
1747 uint8_t *inbuf)
1749 struct tevent_req *req;
1750 struct smbXcli_req_state *state;
1751 NTSTATUS status;
1752 size_t num_pending;
1753 size_t i;
1754 uint8_t cmd;
1755 uint16_t mid;
1756 bool oplock_break;
1757 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1758 struct iovec *iov = NULL;
1759 int num_iov = 0;
1760 struct tevent_req **chain = NULL;
1761 size_t num_chained = 0;
1762 size_t num_responses = 0;
1764 if (conn->smb1.read_braw_req != NULL) {
1765 req = conn->smb1.read_braw_req;
1766 conn->smb1.read_braw_req = NULL;
1767 state = tevent_req_data(req, struct smbXcli_req_state);
1769 smbXcli_req_unset_pending(req);
1771 if (state->smb1.recv_iov == NULL) {
1773 * For requests with more than
1774 * one response, we have to readd the
1775 * recv_iov array.
1777 state->smb1.recv_iov = talloc_zero_array(state,
1778 struct iovec,
1780 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1781 return NT_STATUS_OK;
1785 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1786 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1787 ZERO_STRUCT(state->smb1.recv_iov[1]);
1788 ZERO_STRUCT(state->smb1.recv_iov[2]);
1790 state->smb1.recv_cmd = SMBreadBraw;
1791 state->smb1.recv_status = NT_STATUS_OK;
1792 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1794 tevent_req_done(req);
1795 return NT_STATUS_OK;
1798 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1799 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1800 DEBUG(10, ("Got non-SMB PDU\n"));
1801 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1805 * If we supported multiple encrytion contexts
1806 * here we'd look up based on tid.
1808 if (common_encryption_on(conn->smb1.trans_enc)
1809 && (CVAL(inbuf, 0) == 0)) {
1810 uint16_t enc_ctx_num;
1812 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1813 if (!NT_STATUS_IS_OK(status)) {
1814 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1815 nt_errstr(status)));
1816 return status;
1819 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1820 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1821 enc_ctx_num,
1822 conn->smb1.trans_enc->enc_ctx_num));
1823 return NT_STATUS_INVALID_HANDLE;
1826 status = common_decrypt_buffer(conn->smb1.trans_enc,
1827 (char *)inbuf);
1828 if (!NT_STATUS_IS_OK(status)) {
1829 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1830 nt_errstr(status)));
1831 return status;
1835 mid = SVAL(inhdr, HDR_MID);
1836 num_pending = talloc_array_length(conn->pending);
1838 for (i=0; i<num_pending; i++) {
1839 if (mid == smb1cli_req_mid(conn->pending[i])) {
1840 break;
1843 if (i == num_pending) {
1844 /* Dump unexpected reply */
1845 return NT_STATUS_RETRY;
1848 oplock_break = false;
1850 if (mid == 0xffff) {
1852 * Paranoia checks that this is really an oplock break request.
1854 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1855 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1856 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1857 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1858 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1860 if (!oplock_break) {
1861 /* Dump unexpected reply */
1862 return NT_STATUS_RETRY;
1866 req = conn->pending[i];
1867 state = tevent_req_data(req, struct smbXcli_req_state);
1869 if (!oplock_break /* oplock breaks are not signed */
1870 && !smb_signing_check_pdu(conn->smb1.signing,
1871 inbuf, state->smb1.seqnum+1)) {
1872 DEBUG(10, ("cli_check_sign_mac failed\n"));
1873 return NT_STATUS_ACCESS_DENIED;
1876 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1877 &iov, &num_iov);
1878 if (!NT_STATUS_IS_OK(status)) {
1879 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1880 nt_errstr(status)));
1881 return status;
1884 cmd = CVAL(inhdr, HDR_COM);
1885 status = smb1cli_pull_raw_error(inhdr);
1887 if (state->smb1.chained_requests == NULL) {
1888 if (num_iov != 3) {
1889 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1892 smbXcli_req_unset_pending(req);
1894 if (state->smb1.recv_iov == NULL) {
1896 * For requests with more than
1897 * one response, we have to readd the
1898 * recv_iov array.
1900 state->smb1.recv_iov = talloc_zero_array(state,
1901 struct iovec,
1903 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1904 return NT_STATUS_OK;
1908 state->smb1.recv_cmd = cmd;
1909 state->smb1.recv_status = status;
1910 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1912 state->smb1.recv_iov[0] = iov[0];
1913 state->smb1.recv_iov[1] = iov[1];
1914 state->smb1.recv_iov[2] = iov[2];
1916 if (talloc_array_length(conn->pending) == 0) {
1917 tevent_req_done(req);
1918 return NT_STATUS_OK;
1921 tevent_req_defer_callback(req, state->ev);
1922 tevent_req_done(req);
1923 return NT_STATUS_RETRY;
1926 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1927 num_chained = talloc_array_length(chain);
1928 num_responses = (num_iov - 1)/2;
1930 if (num_responses > num_chained) {
1931 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1934 for (i=0; i<num_chained; i++) {
1935 size_t iov_idx = 1 + (i*2);
1936 struct iovec *cur = &iov[iov_idx];
1937 uint8_t *inbuf_ref;
1939 req = chain[i];
1940 state = tevent_req_data(req, struct smbXcli_req_state);
1942 smbXcli_req_unset_pending(req);
1945 * as we finish multiple requests here
1946 * we need to defer the callbacks as
1947 * they could destroy our current stack state.
1949 tevent_req_defer_callback(req, state->ev);
1951 if (i >= num_responses) {
1952 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1953 continue;
1956 if (state->smb1.recv_iov == NULL) {
1958 * For requests with more than
1959 * one response, we have to readd the
1960 * recv_iov array.
1962 state->smb1.recv_iov = talloc_zero_array(state,
1963 struct iovec,
1965 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1966 continue;
1970 state->smb1.recv_cmd = cmd;
1972 if (i == (num_responses - 1)) {
1974 * The last request in the chain gets the status
1976 state->smb1.recv_status = status;
1977 } else {
1978 cmd = CVAL(cur[0].iov_base, 0);
1979 state->smb1.recv_status = NT_STATUS_OK;
1982 state->inbuf = inbuf;
1985 * Note: here we use talloc_reference() in a way
1986 * that does not expose it to the caller.
1988 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1989 if (tevent_req_nomem(inbuf_ref, req)) {
1990 continue;
1993 /* copy the related buffers */
1994 state->smb1.recv_iov[0] = iov[0];
1995 state->smb1.recv_iov[1] = cur[0];
1996 state->smb1.recv_iov[2] = cur[1];
1998 tevent_req_done(req);
2001 return NT_STATUS_RETRY;
2004 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2005 TALLOC_CTX *mem_ctx,
2006 struct iovec **piov,
2007 uint8_t **phdr,
2008 uint8_t *pwct,
2009 uint16_t **pvwv,
2010 uint32_t *pvwv_offset,
2011 uint32_t *pnum_bytes,
2012 uint8_t **pbytes,
2013 uint32_t *pbytes_offset,
2014 uint8_t **pinbuf,
2015 const struct smb1cli_req_expected_response *expected,
2016 size_t num_expected)
2018 struct smbXcli_req_state *state =
2019 tevent_req_data(req,
2020 struct smbXcli_req_state);
2021 NTSTATUS status = NT_STATUS_OK;
2022 struct iovec *recv_iov = NULL;
2023 uint8_t *hdr = NULL;
2024 uint8_t wct = 0;
2025 uint32_t vwv_offset = 0;
2026 uint16_t *vwv = NULL;
2027 uint32_t num_bytes = 0;
2028 uint32_t bytes_offset = 0;
2029 uint8_t *bytes = NULL;
2030 size_t i;
2031 bool found_status = false;
2032 bool found_size = false;
2034 if (piov != NULL) {
2035 *piov = NULL;
2037 if (phdr != NULL) {
2038 *phdr = 0;
2040 if (pwct != NULL) {
2041 *pwct = 0;
2043 if (pvwv != NULL) {
2044 *pvwv = NULL;
2046 if (pvwv_offset != NULL) {
2047 *pvwv_offset = 0;
2049 if (pnum_bytes != NULL) {
2050 *pnum_bytes = 0;
2052 if (pbytes != NULL) {
2053 *pbytes = NULL;
2055 if (pbytes_offset != NULL) {
2056 *pbytes_offset = 0;
2058 if (pinbuf != NULL) {
2059 *pinbuf = NULL;
2062 if (state->inbuf != NULL) {
2063 recv_iov = state->smb1.recv_iov;
2064 state->smb1.recv_iov = NULL;
2065 if (state->smb1.recv_cmd != SMBreadBraw) {
2066 hdr = (uint8_t *)recv_iov[0].iov_base;
2067 wct = recv_iov[1].iov_len/2;
2068 vwv = (uint16_t *)recv_iov[1].iov_base;
2069 vwv_offset = PTR_DIFF(vwv, hdr);
2070 num_bytes = recv_iov[2].iov_len;
2071 bytes = (uint8_t *)recv_iov[2].iov_base;
2072 bytes_offset = PTR_DIFF(bytes, hdr);
2076 if (tevent_req_is_nterror(req, &status)) {
2077 for (i=0; i < num_expected; i++) {
2078 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2079 found_status = true;
2080 break;
2084 if (found_status) {
2085 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2088 return status;
2091 if (num_expected == 0) {
2092 found_status = true;
2093 found_size = true;
2096 status = state->smb1.recv_status;
2098 for (i=0; i < num_expected; i++) {
2099 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2100 continue;
2103 found_status = true;
2104 if (expected[i].wct == 0) {
2105 found_size = true;
2106 break;
2109 if (expected[i].wct == wct) {
2110 found_size = true;
2111 break;
2115 if (!found_status) {
2116 return status;
2119 if (!found_size) {
2120 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2123 if (piov != NULL) {
2124 *piov = talloc_move(mem_ctx, &recv_iov);
2127 if (phdr != NULL) {
2128 *phdr = hdr;
2130 if (pwct != NULL) {
2131 *pwct = wct;
2133 if (pvwv != NULL) {
2134 *pvwv = vwv;
2136 if (pvwv_offset != NULL) {
2137 *pvwv_offset = vwv_offset;
2139 if (pnum_bytes != NULL) {
2140 *pnum_bytes = num_bytes;
2142 if (pbytes != NULL) {
2143 *pbytes = bytes;
2145 if (pbytes_offset != NULL) {
2146 *pbytes_offset = bytes_offset;
2148 if (pinbuf != NULL) {
2149 *pinbuf = state->inbuf;
2152 return status;
2155 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2157 size_t wct_ofs;
2158 int i;
2160 wct_ofs = HDR_WCT;
2162 for (i=0; i<num_reqs; i++) {
2163 struct smbXcli_req_state *state;
2164 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2165 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2166 state->smb1.iov_count-2);
2167 wct_ofs = (wct_ofs + 3) & ~3;
2169 return wct_ofs;
2172 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2174 struct smbXcli_req_state *first_state =
2175 tevent_req_data(reqs[0],
2176 struct smbXcli_req_state);
2177 struct smbXcli_req_state *state;
2178 size_t wct_offset;
2179 size_t chain_padding = 0;
2180 int i, iovlen;
2181 struct iovec *iov = NULL;
2182 struct iovec *this_iov;
2183 NTSTATUS status;
2184 size_t nbt_len;
2186 if (num_reqs == 1) {
2187 return smb1cli_req_writev_submit(reqs[0], first_state,
2188 first_state->smb1.iov,
2189 first_state->smb1.iov_count);
2192 iovlen = 0;
2193 for (i=0; i<num_reqs; i++) {
2194 if (!tevent_req_is_in_progress(reqs[i])) {
2195 return NT_STATUS_INTERNAL_ERROR;
2198 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2200 if (state->smb1.iov_count < 4) {
2201 return NT_STATUS_INVALID_PARAMETER_MIX;
2204 if (i == 0) {
2206 * The NBT and SMB header
2208 iovlen += 2;
2209 } else {
2211 * Chain padding
2213 iovlen += 1;
2217 * words and bytes
2219 iovlen += state->smb1.iov_count - 2;
2222 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2223 if (iov == NULL) {
2224 return NT_STATUS_NO_MEMORY;
2227 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2228 first_state, reqs, sizeof(*reqs) * num_reqs);
2229 if (first_state->smb1.chained_requests == NULL) {
2230 TALLOC_FREE(iov);
2231 return NT_STATUS_NO_MEMORY;
2234 wct_offset = HDR_WCT;
2235 this_iov = iov;
2237 for (i=0; i<num_reqs; i++) {
2238 size_t next_padding = 0;
2239 uint16_t *vwv;
2241 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2243 if (i < num_reqs-1) {
2244 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2245 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2246 TALLOC_FREE(iov);
2247 TALLOC_FREE(first_state->smb1.chained_requests);
2248 return NT_STATUS_INVALID_PARAMETER_MIX;
2252 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2253 state->smb1.iov_count-2) + 1;
2254 if ((wct_offset % 4) != 0) {
2255 next_padding = 4 - (wct_offset % 4);
2257 wct_offset += next_padding;
2258 vwv = state->smb1.vwv;
2260 if (i < num_reqs-1) {
2261 struct smbXcli_req_state *next_state =
2262 tevent_req_data(reqs[i+1],
2263 struct smbXcli_req_state);
2264 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2265 SCVAL(vwv+0, 1, 0);
2266 SSVAL(vwv+1, 0, wct_offset);
2267 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2268 /* properly end the chain */
2269 SCVAL(vwv+0, 0, 0xff);
2270 SCVAL(vwv+0, 1, 0xff);
2271 SSVAL(vwv+1, 0, 0);
2274 if (i == 0) {
2276 * The NBT and SMB header
2278 this_iov[0] = state->smb1.iov[0];
2279 this_iov[1] = state->smb1.iov[1];
2280 this_iov += 2;
2281 } else {
2283 * This one is a bit subtle. We have to add
2284 * chain_padding bytes between the requests, and we
2285 * have to also include the wct field of the
2286 * subsequent requests. We use the subsequent header
2287 * for the padding, it contains the wct field in its
2288 * last byte.
2290 this_iov[0].iov_len = chain_padding+1;
2291 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2292 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2293 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2294 this_iov += 1;
2298 * copy the words and bytes
2300 memcpy(this_iov, state->smb1.iov+2,
2301 sizeof(struct iovec) * (state->smb1.iov_count-2));
2302 this_iov += state->smb1.iov_count - 2;
2303 chain_padding = next_padding;
2306 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2307 if (nbt_len > first_state->conn->smb1.max_xmit) {
2308 TALLOC_FREE(iov);
2309 TALLOC_FREE(first_state->smb1.chained_requests);
2310 return NT_STATUS_INVALID_PARAMETER_MIX;
2313 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2314 if (!NT_STATUS_IS_OK(status)) {
2315 TALLOC_FREE(iov);
2316 TALLOC_FREE(first_state->smb1.chained_requests);
2317 return status;
2320 return NT_STATUS_OK;
2323 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2325 return ((tevent_queue_length(conn->outgoing) != 0)
2326 || (talloc_array_length(conn->pending) != 0));
2329 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2331 return conn->smb2.server.capabilities;
2334 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2336 return conn->smb2.server.security_mode;
2339 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2341 return conn->smb2.server.max_trans_size;
2344 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2346 return conn->smb2.server.max_read_size;
2349 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2351 return conn->smb2.server.max_write_size;
2354 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2355 uint16_t max_credits)
2357 conn->smb2.max_credits = max_credits;
2360 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2362 static bool smb2cli_req_cancel(struct tevent_req *req)
2364 struct smbXcli_req_state *state =
2365 tevent_req_data(req,
2366 struct smbXcli_req_state);
2367 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2368 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2369 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2370 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2371 struct smbXcli_tcon *tcon = state->tcon;
2372 struct smbXcli_session *session = state->session;
2373 uint8_t *fixed = state->smb2.pad;
2374 uint16_t fixed_len = 4;
2375 struct tevent_req *subreq;
2376 struct smbXcli_req_state *substate;
2377 NTSTATUS status;
2379 SSVAL(fixed, 0, 0x04);
2380 SSVAL(fixed, 2, 0);
2382 subreq = smb2cli_req_create(state, state->ev,
2383 state->conn,
2384 SMB2_OP_CANCEL,
2385 flags, 0,
2386 0, /* timeout */
2387 pid, tcon, session,
2388 fixed, fixed_len,
2389 NULL, 0);
2390 if (subreq == NULL) {
2391 return false;
2393 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2395 if (flags & SMB2_HDR_FLAG_ASYNC) {
2396 mid = 0;
2399 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2400 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2401 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2402 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2404 status = smb2cli_req_compound_submit(&subreq, 1);
2405 if (!NT_STATUS_IS_OK(status)) {
2406 TALLOC_FREE(subreq);
2407 return false;
2410 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2412 return true;
2415 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2417 /* we do not care about the result */
2418 TALLOC_FREE(subreq);
2421 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2422 struct tevent_context *ev,
2423 struct smbXcli_conn *conn,
2424 uint16_t cmd,
2425 uint32_t additional_flags,
2426 uint32_t clear_flags,
2427 uint32_t timeout_msec,
2428 uint32_t pid,
2429 struct smbXcli_tcon *tcon,
2430 struct smbXcli_session *session,
2431 const uint8_t *fixed,
2432 uint16_t fixed_len,
2433 const uint8_t *dyn,
2434 uint32_t dyn_len)
2436 struct tevent_req *req;
2437 struct smbXcli_req_state *state;
2438 uint32_t flags = 0;
2439 uint32_t tid = 0;
2440 uint64_t uid = 0;
2442 req = tevent_req_create(mem_ctx, &state,
2443 struct smbXcli_req_state);
2444 if (req == NULL) {
2445 return NULL;
2448 state->ev = ev;
2449 state->conn = conn;
2450 state->session = session;
2451 state->tcon = tcon;
2453 if (session) {
2454 uid = session->smb2.session_id;
2456 state->smb2.should_sign = session->smb2.should_sign;
2457 state->smb2.should_encrypt = session->smb2.should_encrypt;
2459 if (cmd == SMB2_OP_SESSSETUP &&
2460 session->smb2.signing_key.length != 0) {
2461 state->smb2.should_sign = true;
2464 if (cmd == SMB2_OP_SESSSETUP &&
2465 session->smb2.channel_signing_key.length == 0) {
2466 state->smb2.should_encrypt = false;
2470 if (tcon) {
2471 tid = tcon->smb2.tcon_id;
2473 /* TODO: turn on encryption based on the tree connect. */
2476 if (state->smb2.should_encrypt) {
2477 state->smb2.should_sign = false;
2480 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2481 if (state->smb2.recv_iov == NULL) {
2482 TALLOC_FREE(req);
2483 return NULL;
2486 flags |= additional_flags;
2487 flags &= ~clear_flags;
2489 state->smb2.fixed = fixed;
2490 state->smb2.fixed_len = fixed_len;
2491 state->smb2.dyn = dyn;
2492 state->smb2.dyn_len = dyn_len;
2494 if (state->smb2.should_encrypt) {
2495 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2496 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2499 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2500 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2501 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2502 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2503 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2504 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2505 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2507 switch (cmd) {
2508 case SMB2_OP_CANCEL:
2509 state->one_way = true;
2510 break;
2511 case SMB2_OP_BREAK:
2513 * If this is a dummy request, it will have
2514 * UINT64_MAX as message id.
2515 * If we send on break acknowledgement,
2516 * this gets overwritten later.
2518 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2519 break;
2522 if (timeout_msec > 0) {
2523 struct timeval endtime;
2525 endtime = timeval_current_ofs_msec(timeout_msec);
2526 if (!tevent_req_set_endtime(req, ev, endtime)) {
2527 return req;
2531 return req;
2534 void smb2cli_req_set_notify_async(struct tevent_req *req)
2536 struct smbXcli_req_state *state =
2537 tevent_req_data(req,
2538 struct smbXcli_req_state);
2540 state->smb2.notify_async = true;
2543 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2544 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2545 TALLOC_CTX *tmp_mem,
2546 uint8_t *inbuf);
2548 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2549 int num_reqs)
2551 struct smbXcli_req_state *state;
2552 struct tevent_req *subreq;
2553 struct iovec *iov;
2554 int i, num_iov, nbt_len;
2557 * 1 for the nbt length
2558 * per request: TRANSFORM, HDR, fixed, dyn, padding
2559 * -1 because the last one does not need padding
2562 iov = talloc_array(reqs[0], struct iovec, 1 + 5*num_reqs - 1);
2563 if (iov == NULL) {
2564 return NT_STATUS_NO_MEMORY;
2567 num_iov = 1;
2568 nbt_len = 0;
2570 for (i=0; i<num_reqs; i++) {
2571 int tf_iov;
2572 int hdr_iov;
2573 size_t reqlen;
2574 bool ret;
2575 uint16_t opcode;
2576 uint64_t avail;
2577 uint16_t charge;
2578 uint16_t credits;
2579 uint64_t mid;
2580 const DATA_BLOB *signing_key = NULL;
2581 const DATA_BLOB *encryption_key = NULL;
2583 if (!tevent_req_is_in_progress(reqs[i])) {
2584 return NT_STATUS_INTERNAL_ERROR;
2587 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2589 if (!smbXcli_conn_is_connected(state->conn)) {
2590 return NT_STATUS_CONNECTION_DISCONNECTED;
2593 if ((state->conn->protocol != PROTOCOL_NONE) &&
2594 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2595 return NT_STATUS_REVISION_MISMATCH;
2598 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2599 if (opcode == SMB2_OP_CANCEL) {
2600 goto skip_credits;
2603 avail = UINT64_MAX - state->conn->smb2.mid;
2604 if (avail < 1) {
2605 return NT_STATUS_CONNECTION_ABORTED;
2608 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2609 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2610 } else {
2611 charge = 1;
2614 charge = MAX(state->smb2.credit_charge, charge);
2616 avail = MIN(avail, state->conn->smb2.cur_credits);
2617 if (avail < charge) {
2618 return NT_STATUS_INTERNAL_ERROR;
2621 credits = 0;
2622 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2623 credits = state->conn->smb2.max_credits -
2624 state->conn->smb2.cur_credits;
2626 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2627 credits += 1;
2630 mid = state->conn->smb2.mid;
2631 state->conn->smb2.mid += charge;
2632 state->conn->smb2.cur_credits -= charge;
2634 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2635 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2637 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2638 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2640 skip_credits:
2641 if (state->session) {
2643 * We prefer the channel signing key if it is
2644 * already there.
2646 if (state->smb2.should_sign) {
2647 signing_key = &state->session->smb2.channel_signing_key;
2651 * If it is a channel binding, we already have the main
2652 * signing key and try that one.
2654 if (signing_key && signing_key->length == 0) {
2655 signing_key = &state->session->smb2.signing_key;
2659 * If we do not have any session key yet, we skip the
2660 * signing of SMB2_OP_SESSSETUP requests.
2662 if (signing_key && signing_key->length == 0) {
2663 signing_key = NULL;
2666 if (state->smb2.should_encrypt) {
2667 encryption_key = &state->session->smb2.encryption_key;
2671 if (encryption_key) {
2672 tf_iov = num_iov;
2673 iov[num_iov].iov_base = state->smb2.transform;
2674 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2675 num_iov += 1;
2678 hdr_iov = num_iov;
2679 iov[num_iov].iov_base = state->smb2.hdr;
2680 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2681 num_iov += 1;
2683 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2684 iov[num_iov].iov_len = state->smb2.fixed_len;
2685 num_iov += 1;
2687 if (state->smb2.dyn != NULL) {
2688 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2689 iov[num_iov].iov_len = state->smb2.dyn_len;
2690 num_iov += 1;
2693 reqlen = sizeof(state->smb2.hdr);
2694 reqlen += state->smb2.fixed_len;
2695 reqlen += state->smb2.dyn_len;
2697 if (i < num_reqs-1) {
2698 if ((reqlen % 8) > 0) {
2699 uint8_t pad = 8 - (reqlen % 8);
2700 iov[num_iov].iov_base = state->smb2.pad;
2701 iov[num_iov].iov_len = pad;
2702 num_iov += 1;
2703 reqlen += pad;
2705 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2708 if (encryption_key) {
2709 NTSTATUS status;
2710 uint8_t *buf;
2711 int vi;
2713 SBVAL(state->smb2.transform, SMB2_TF_NONCE, mid);
2714 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2715 state->session->smb2.channel_nonce);
2716 SBVAL(state->smb2.transform, SMB2_TF_MSG_SIZE,
2717 reqlen);
2719 buf = talloc_array(iov, uint8_t, reqlen);
2720 if (buf == NULL) {
2721 return NT_STATUS_NO_MEMORY;
2724 reqlen += SMB2_TF_HDR_SIZE;
2727 * We copy the buffers before encrypting them,
2728 * this is at least currently needed for the
2729 * to keep state->smb2.hdr.
2731 * Also the callers may expect there buffers
2732 * to be const.
2734 for (vi = hdr_iov; vi < num_iov; vi++) {
2735 struct iovec *v = &iov[vi];
2736 const uint8_t *o = (const uint8_t *)v->iov_base;
2738 memcpy(buf, o, v->iov_len);
2739 v->iov_base = (void *)buf;
2740 buf += v->iov_len;
2743 status = smb2_signing_encrypt_pdu(*encryption_key,
2744 state->session->conn->protocol,
2745 &iov[tf_iov], num_iov - tf_iov);
2746 if (!NT_STATUS_IS_OK(status)) {
2747 return status;
2749 } else if (signing_key) {
2750 NTSTATUS status;
2752 status = smb2_signing_sign_pdu(*signing_key,
2753 state->session->conn->protocol,
2754 &iov[hdr_iov], num_iov - hdr_iov);
2755 if (!NT_STATUS_IS_OK(status)) {
2756 return status;
2760 nbt_len += reqlen;
2762 ret = smbXcli_req_set_pending(reqs[i]);
2763 if (!ret) {
2764 return NT_STATUS_NO_MEMORY;
2768 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2769 _smb_setlen_tcp(state->length_hdr, nbt_len);
2770 iov[0].iov_base = state->length_hdr;
2771 iov[0].iov_len = sizeof(state->length_hdr);
2773 if (state->conn->dispatch_incoming == NULL) {
2774 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2777 subreq = writev_send(state, state->ev, state->conn->outgoing,
2778 state->conn->write_fd, false, iov, num_iov);
2779 if (subreq == NULL) {
2780 return NT_STATUS_NO_MEMORY;
2782 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2783 return NT_STATUS_OK;
2786 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2788 struct smbXcli_req_state *state =
2789 tevent_req_data(req,
2790 struct smbXcli_req_state);
2792 state->smb2.credit_charge = charge;
2795 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2796 struct tevent_context *ev,
2797 struct smbXcli_conn *conn,
2798 uint16_t cmd,
2799 uint32_t additional_flags,
2800 uint32_t clear_flags,
2801 uint32_t timeout_msec,
2802 uint32_t pid,
2803 struct smbXcli_tcon *tcon,
2804 struct smbXcli_session *session,
2805 const uint8_t *fixed,
2806 uint16_t fixed_len,
2807 const uint8_t *dyn,
2808 uint32_t dyn_len)
2810 struct tevent_req *req;
2811 NTSTATUS status;
2813 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2814 additional_flags, clear_flags,
2815 timeout_msec,
2816 pid, tcon, session,
2817 fixed, fixed_len, dyn, dyn_len);
2818 if (req == NULL) {
2819 return NULL;
2821 if (!tevent_req_is_in_progress(req)) {
2822 return tevent_req_post(req, ev);
2824 status = smb2cli_req_compound_submit(&req, 1);
2825 if (tevent_req_nterror(req, status)) {
2826 return tevent_req_post(req, ev);
2828 return req;
2831 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2833 struct tevent_req *req =
2834 tevent_req_callback_data(subreq,
2835 struct tevent_req);
2836 struct smbXcli_req_state *state =
2837 tevent_req_data(req,
2838 struct smbXcli_req_state);
2839 ssize_t nwritten;
2840 int err;
2842 nwritten = writev_recv(subreq, &err);
2843 TALLOC_FREE(subreq);
2844 if (nwritten == -1) {
2845 /* here, we need to notify all pending requests */
2846 NTSTATUS status = map_nt_error_from_unix_common(err);
2847 smbXcli_conn_disconnect(state->conn, status);
2848 return;
2852 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2853 uint8_t *buf,
2854 size_t buflen,
2855 TALLOC_CTX *mem_ctx,
2856 struct iovec **piov, int *pnum_iov)
2858 struct iovec *iov;
2859 int num_iov = 0;
2860 size_t taken = 0;
2861 uint8_t *first_hdr = buf;
2863 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2864 if (iov == NULL) {
2865 return NT_STATUS_NO_MEMORY;
2868 while (taken < buflen) {
2869 uint8_t *tf = NULL;
2870 size_t tf_len = 0;
2871 size_t len = buflen - taken;
2872 uint8_t *hdr = first_hdr + taken;
2873 struct iovec *cur;
2874 size_t full_size;
2875 size_t next_command_ofs;
2876 uint16_t body_size;
2877 struct iovec *iov_tmp;
2879 if (len < 4) {
2880 DEBUG(10, ("%d bytes left, expected at least %d\n",
2881 (int)len, 4));
2882 goto inval;
2884 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2885 struct smbXcli_session *s;
2886 uint64_t uid;
2887 struct iovec tf_iov[2];
2888 NTSTATUS status;
2890 if (len < SMB2_TF_HDR_SIZE) {
2891 DEBUG(10, ("%d bytes left, expected at least %d\n",
2892 (int)len, SMB2_TF_HDR_SIZE));
2893 goto inval;
2895 tf = hdr;
2896 tf_len = SMB2_TF_HDR_SIZE;
2897 taken += tf_len;
2899 hdr = first_hdr + taken;
2900 len = IVAL(tf, SMB2_TF_MSG_SIZE);
2901 uid = BVAL(tf, SMB2_TF_SESSION_ID);
2903 s = conn->sessions;
2904 for (; s; s = s->next) {
2905 if (s->smb2.session_id != uid) {
2906 continue;
2908 break;
2911 if (s == NULL) {
2912 DEBUG(10, ("unknown session_id %llu\n",
2913 (unsigned long long)uid));
2914 goto inval;
2917 tf_iov[0].iov_base = (void *)tf;
2918 tf_iov[0].iov_len = tf_len;
2919 tf_iov[1].iov_base = (void *)hdr;
2920 tf_iov[1].iov_len = len;
2922 status = smb2_signing_decrypt_pdu(s->smb2.decryption_key,
2923 conn->protocol,
2924 tf_iov, 2);
2925 if (!NT_STATUS_IS_OK(status)) {
2926 TALLOC_FREE(iov);
2927 return status;
2932 * We need the header plus the body length field
2935 if (len < SMB2_HDR_BODY + 2) {
2936 DEBUG(10, ("%d bytes left, expected at least %d\n",
2937 (int)len, SMB2_HDR_BODY));
2938 goto inval;
2940 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2941 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2942 IVAL(hdr, 0)));
2943 goto inval;
2945 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2946 DEBUG(10, ("Got HDR len %d, expected %d\n",
2947 SVAL(hdr, 4), SMB2_HDR_BODY));
2948 goto inval;
2951 full_size = len;
2952 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2953 body_size = SVAL(hdr, SMB2_HDR_BODY);
2955 if (next_command_ofs != 0) {
2956 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2957 goto inval;
2959 if (next_command_ofs > full_size) {
2960 goto inval;
2962 if (tf && next_command_ofs < len) {
2963 goto inval;
2965 full_size = next_command_ofs;
2967 if (body_size < 2) {
2968 goto inval;
2970 body_size &= 0xfffe;
2972 if (body_size > (full_size - SMB2_HDR_BODY)) {
2973 goto inval;
2976 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2977 num_iov + 4);
2978 if (iov_tmp == NULL) {
2979 TALLOC_FREE(iov);
2980 return NT_STATUS_NO_MEMORY;
2982 iov = iov_tmp;
2983 cur = &iov[num_iov];
2984 num_iov += 4;
2986 cur[0].iov_base = tf;
2987 cur[0].iov_len = tf_len;
2988 cur[1].iov_base = hdr;
2989 cur[1].iov_len = SMB2_HDR_BODY;
2990 cur[2].iov_base = hdr + SMB2_HDR_BODY;
2991 cur[2].iov_len = body_size;
2992 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
2993 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2995 taken += full_size;
2998 *piov = iov;
2999 *pnum_iov = num_iov;
3000 return NT_STATUS_OK;
3002 inval:
3003 TALLOC_FREE(iov);
3004 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3007 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3008 uint64_t mid)
3010 size_t num_pending = talloc_array_length(conn->pending);
3011 size_t i;
3013 for (i=0; i<num_pending; i++) {
3014 struct tevent_req *req = conn->pending[i];
3015 struct smbXcli_req_state *state =
3016 tevent_req_data(req,
3017 struct smbXcli_req_state);
3019 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3020 return req;
3023 return NULL;
3026 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3027 TALLOC_CTX *tmp_mem,
3028 uint8_t *inbuf)
3030 struct tevent_req *req;
3031 struct smbXcli_req_state *state = NULL;
3032 struct iovec *iov;
3033 int i, num_iov;
3034 NTSTATUS status;
3035 bool defer = true;
3036 struct smbXcli_session *last_session = NULL;
3037 size_t inbuf_len = smb_len_tcp(inbuf);
3039 status = smb2cli_inbuf_parse_compound(conn,
3040 inbuf + NBT_HDR_SIZE,
3041 inbuf_len,
3042 tmp_mem,
3043 &iov, &num_iov);
3044 if (!NT_STATUS_IS_OK(status)) {
3045 return status;
3048 for (i=0; i<num_iov; i+=4) {
3049 uint8_t *inbuf_ref = NULL;
3050 struct iovec *cur = &iov[i];
3051 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3052 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3053 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3054 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3055 uint16_t req_opcode;
3056 uint32_t req_flags;
3057 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3058 uint32_t new_credits;
3059 struct smbXcli_session *session = NULL;
3060 const DATA_BLOB *signing_key = NULL;
3062 new_credits = conn->smb2.cur_credits;
3063 new_credits += credits;
3064 if (new_credits > UINT16_MAX) {
3065 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3067 conn->smb2.cur_credits += credits;
3069 req = smb2cli_conn_find_pending(conn, mid);
3070 if (req == NULL) {
3071 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3073 state = tevent_req_data(req, struct smbXcli_req_state);
3075 state->smb2.got_async = false;
3077 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3078 if (opcode != req_opcode) {
3079 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3081 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3083 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3084 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3087 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3088 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3089 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3090 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3093 * async interim responses are not signed,
3094 * even if the SMB2_HDR_FLAG_SIGNED flag
3095 * is set.
3097 req_flags |= SMB2_HDR_FLAG_ASYNC;
3098 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3099 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3101 if (state->smb2.notify_async) {
3102 state->smb2.got_async = true;
3103 tevent_req_defer_callback(req, state->ev);
3104 tevent_req_notify_callback(req);
3106 continue;
3109 session = state->session;
3110 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3111 session = last_session;
3113 last_session = session;
3115 if (state->smb2.should_sign) {
3116 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3117 return NT_STATUS_ACCESS_DENIED;
3121 if (flags & SMB2_HDR_FLAG_SIGNED) {
3122 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3124 if (session == NULL) {
3125 struct smbXcli_session *s;
3127 s = state->conn->sessions;
3128 for (; s; s = s->next) {
3129 if (s->smb2.session_id != uid) {
3130 continue;
3133 session = s;
3134 break;
3138 if (session == NULL) {
3139 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3142 last_session = session;
3143 signing_key = &session->smb2.channel_signing_key;
3146 if (opcode == SMB2_OP_SESSSETUP) {
3148 * We prefer the channel signing key, if it is
3149 * already there.
3151 * If we do not have a channel signing key yet,
3152 * we try the main signing key, if it is not
3153 * the final response.
3155 if (signing_key && signing_key->length == 0 &&
3156 !NT_STATUS_IS_OK(status)) {
3157 signing_key = &session->smb2.signing_key;
3160 if (signing_key && signing_key->length == 0) {
3162 * If we do not have a session key to
3163 * verify the signature, we defer the
3164 * signing check to the caller.
3166 * The caller gets NT_STATUS_OK, it
3167 * has to call
3168 * smb2cli_session_set_session_key()
3169 * or
3170 * smb2cli_session_set_channel_key()
3171 * which will check the signature
3172 * with the channel signing key.
3174 signing_key = NULL;
3178 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3180 * if the server returns NT_STATUS_USER_SESSION_DELETED
3181 * the response is not signed and we should
3182 * propagate the NT_STATUS_USER_SESSION_DELETED
3183 * status to the caller.
3185 signing_key = NULL;
3186 } else if (state->smb2.should_encrypt) {
3187 if (cur[0].iov_len != SMB2_TF_HDR_SIZE) {
3188 return NT_STATUS_ACCESS_DENIED;
3192 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3193 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3194 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3196 * if the server returns
3197 * NT_STATUS_NETWORK_NAME_DELETED
3198 * NT_STATUS_FILE_CLOSED
3199 * NT_STATUS_INVALID_PARAMETER
3200 * the response might not be signed
3201 * as this happens before the signing checks.
3203 * If server echos the signature (or all zeros)
3204 * we should report the status from the server
3205 * to the caller.
3207 if (signing_key) {
3208 int cmp;
3210 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3211 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3212 16);
3213 if (cmp == 0) {
3214 state->smb2.signing_skipped = true;
3215 signing_key = NULL;
3218 if (signing_key) {
3219 int cmp;
3220 static const uint8_t zeros[16];
3222 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3223 zeros,
3224 16);
3225 if (cmp == 0) {
3226 state->smb2.signing_skipped = true;
3227 signing_key = NULL;
3232 if (signing_key) {
3233 status = smb2_signing_check_pdu(*signing_key,
3234 state->conn->protocol,
3235 &cur[1], 3);
3236 if (!NT_STATUS_IS_OK(status)) {
3238 * If the signing check fails, we disconnect
3239 * the connection.
3241 return status;
3245 smbXcli_req_unset_pending(req);
3248 * There might be more than one response
3249 * we need to defer the notifications
3251 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3252 defer = false;
3255 if (defer) {
3256 tevent_req_defer_callback(req, state->ev);
3260 * Note: here we use talloc_reference() in a way
3261 * that does not expose it to the caller.
3263 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3264 if (tevent_req_nomem(inbuf_ref, req)) {
3265 continue;
3268 /* copy the related buffers */
3269 state->smb2.recv_iov[0] = cur[1];
3270 state->smb2.recv_iov[1] = cur[2];
3271 state->smb2.recv_iov[2] = cur[3];
3273 tevent_req_done(req);
3276 if (defer) {
3277 return NT_STATUS_RETRY;
3280 return NT_STATUS_OK;
3283 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3284 struct iovec **piov,
3285 const struct smb2cli_req_expected_response *expected,
3286 size_t num_expected)
3288 struct smbXcli_req_state *state =
3289 tevent_req_data(req,
3290 struct smbXcli_req_state);
3291 NTSTATUS status;
3292 size_t body_size;
3293 bool found_status = false;
3294 bool found_size = false;
3295 size_t i;
3297 if (piov != NULL) {
3298 *piov = NULL;
3301 if (state->smb2.got_async) {
3302 return STATUS_PENDING;
3305 if (tevent_req_is_nterror(req, &status)) {
3306 for (i=0; i < num_expected; i++) {
3307 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3308 found_status = true;
3309 break;
3313 if (found_status) {
3314 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3317 return status;
3320 if (num_expected == 0) {
3321 found_status = true;
3322 found_size = true;
3325 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3326 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3328 for (i=0; i < num_expected; i++) {
3329 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3330 continue;
3333 found_status = true;
3334 if (expected[i].body_size == 0) {
3335 found_size = true;
3336 break;
3339 if (expected[i].body_size == body_size) {
3340 found_size = true;
3341 break;
3345 if (!found_status) {
3346 return status;
3349 if (state->smb2.signing_skipped) {
3350 if (num_expected > 0) {
3351 return NT_STATUS_ACCESS_DENIED;
3353 if (!NT_STATUS_IS_ERR(status)) {
3354 return NT_STATUS_ACCESS_DENIED;
3358 if (!found_size) {
3359 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3362 if (piov != NULL) {
3363 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3366 return status;
3369 static const struct {
3370 enum protocol_types proto;
3371 const char *smb1_name;
3372 } smb1cli_prots[] = {
3373 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3374 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3375 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3376 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3377 {PROTOCOL_LANMAN2, "LM1.2X002"},
3378 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3379 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3380 {PROTOCOL_LANMAN2, "Samba"},
3381 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3382 {PROTOCOL_NT1, "NT LM 0.12"},
3383 {PROTOCOL_SMB2_02, "SMB 2.002"},
3384 {PROTOCOL_SMB2_10, "SMB 2.???"},
3387 static const struct {
3388 enum protocol_types proto;
3389 uint16_t smb2_dialect;
3390 } smb2cli_prots[] = {
3391 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3392 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3393 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3394 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3395 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3398 struct smbXcli_negprot_state {
3399 struct smbXcli_conn *conn;
3400 struct tevent_context *ev;
3401 uint32_t timeout_msec;
3402 enum protocol_types min_protocol;
3403 enum protocol_types max_protocol;
3405 struct {
3406 uint8_t fixed[36];
3407 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3408 } smb2;
3411 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3412 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3413 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3414 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3415 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3416 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3417 TALLOC_CTX *frame,
3418 uint8_t *inbuf);
3420 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3421 struct tevent_context *ev,
3422 struct smbXcli_conn *conn,
3423 uint32_t timeout_msec,
3424 enum protocol_types min_protocol,
3425 enum protocol_types max_protocol)
3427 struct tevent_req *req, *subreq;
3428 struct smbXcli_negprot_state *state;
3430 req = tevent_req_create(mem_ctx, &state,
3431 struct smbXcli_negprot_state);
3432 if (req == NULL) {
3433 return NULL;
3435 state->conn = conn;
3436 state->ev = ev;
3437 state->timeout_msec = timeout_msec;
3438 state->min_protocol = min_protocol;
3439 state->max_protocol = max_protocol;
3441 if (min_protocol == PROTOCOL_NONE) {
3442 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3443 return tevent_req_post(req, ev);
3446 if (max_protocol == PROTOCOL_NONE) {
3447 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3448 return tevent_req_post(req, ev);
3451 if (min_protocol > max_protocol) {
3452 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3453 return tevent_req_post(req, ev);
3456 if ((min_protocol < PROTOCOL_SMB2_02) &&
3457 (max_protocol < PROTOCOL_SMB2_02)) {
3459 * SMB1 only...
3461 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3463 subreq = smbXcli_negprot_smb1_subreq(state);
3464 if (tevent_req_nomem(subreq, req)) {
3465 return tevent_req_post(req, ev);
3467 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3468 return req;
3471 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3472 (max_protocol >= PROTOCOL_SMB2_02)) {
3474 * SMB2 only...
3476 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3478 subreq = smbXcli_negprot_smb2_subreq(state);
3479 if (tevent_req_nomem(subreq, req)) {
3480 return tevent_req_post(req, ev);
3482 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3483 return req;
3487 * We send an SMB1 negprot with the SMB2 dialects
3488 * and expect a SMB1 or a SMB2 response.
3490 * smbXcli_negprot_dispatch_incoming() will fix the
3491 * callback to match protocol of the response.
3493 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3495 subreq = smbXcli_negprot_smb1_subreq(state);
3496 if (tevent_req_nomem(subreq, req)) {
3497 return tevent_req_post(req, ev);
3499 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3500 return req;
3503 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3505 struct tevent_req *req =
3506 tevent_req_callback_data(subreq,
3507 struct tevent_req);
3508 NTSTATUS status;
3511 * we just want the low level error
3513 status = tevent_req_simple_recv_ntstatus(subreq);
3514 TALLOC_FREE(subreq);
3515 if (tevent_req_nterror(req, status)) {
3516 return;
3519 /* this should never happen */
3520 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3523 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3525 size_t i;
3526 DATA_BLOB bytes = data_blob_null;
3527 uint8_t flags;
3528 uint16_t flags2;
3530 /* setup the protocol strings */
3531 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3532 uint8_t c = 2;
3533 bool ok;
3535 if (smb1cli_prots[i].proto < state->min_protocol) {
3536 continue;
3539 if (smb1cli_prots[i].proto > state->max_protocol) {
3540 continue;
3543 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3544 if (!ok) {
3545 return NULL;
3549 * We now it is already ascii and
3550 * we want NULL termination.
3552 ok = data_blob_append(state, &bytes,
3553 smb1cli_prots[i].smb1_name,
3554 strlen(smb1cli_prots[i].smb1_name)+1);
3555 if (!ok) {
3556 return NULL;
3560 smb1cli_req_flags(state->max_protocol,
3561 state->conn->smb1.client.capabilities,
3562 SMBnegprot,
3563 0, 0, &flags,
3564 0, 0, &flags2);
3566 return smb1cli_req_send(state, state->ev, state->conn,
3567 SMBnegprot,
3568 flags, ~flags,
3569 flags2, ~flags2,
3570 state->timeout_msec,
3571 0xFFFE, 0, NULL, /* pid, tid, session */
3572 0, NULL, /* wct, vwv */
3573 bytes.length, bytes.data);
3576 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3578 struct tevent_req *req =
3579 tevent_req_callback_data(subreq,
3580 struct tevent_req);
3581 struct smbXcli_negprot_state *state =
3582 tevent_req_data(req,
3583 struct smbXcli_negprot_state);
3584 struct smbXcli_conn *conn = state->conn;
3585 struct iovec *recv_iov = NULL;
3586 uint8_t *inhdr;
3587 uint8_t wct;
3588 uint16_t *vwv;
3589 uint32_t num_bytes;
3590 uint8_t *bytes;
3591 NTSTATUS status;
3592 uint16_t protnum;
3593 size_t i;
3594 size_t num_prots = 0;
3595 uint8_t flags;
3596 uint32_t client_capabilities = conn->smb1.client.capabilities;
3597 uint32_t both_capabilities;
3598 uint32_t server_capabilities = 0;
3599 uint32_t capabilities;
3600 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3601 uint32_t server_max_xmit = 0;
3602 uint32_t max_xmit;
3603 uint32_t server_max_mux = 0;
3604 uint16_t server_security_mode = 0;
3605 uint32_t server_session_key = 0;
3606 bool server_readbraw = false;
3607 bool server_writebraw = false;
3608 bool server_lockread = false;
3609 bool server_writeunlock = false;
3610 struct GUID server_guid = GUID_zero();
3611 DATA_BLOB server_gss_blob = data_blob_null;
3612 uint8_t server_challenge[8];
3613 char *server_workgroup = NULL;
3614 char *server_name = NULL;
3615 int server_time_zone = 0;
3616 NTTIME server_system_time = 0;
3617 static const struct smb1cli_req_expected_response expected[] = {
3619 .status = NT_STATUS_OK,
3620 .wct = 0x11, /* NT1 */
3623 .status = NT_STATUS_OK,
3624 .wct = 0x0D, /* LM */
3627 .status = NT_STATUS_OK,
3628 .wct = 0x01, /* CORE */
3632 ZERO_STRUCT(server_challenge);
3634 status = smb1cli_req_recv(subreq, state,
3635 &recv_iov,
3636 &inhdr,
3637 &wct,
3638 &vwv,
3639 NULL, /* pvwv_offset */
3640 &num_bytes,
3641 &bytes,
3642 NULL, /* pbytes_offset */
3643 NULL, /* pinbuf */
3644 expected, ARRAY_SIZE(expected));
3645 TALLOC_FREE(subreq);