s3:libsmb: fix the talloc parent of clistr_pull_talloc() in cli_notify_done()
[Samba/gebeck_regimport.git] / libcli / smb / smbXcli_base.c
blobeb9e7d55cfe59891b8d6112e4af738fda57a6ccd
1 /*
2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "../libcli/smb/smb_common.h"
29 #include "../libcli/smb/smb_seal.h"
30 #include "../libcli/smb/smb_signing.h"
31 #include "../libcli/smb/read_smb.h"
32 #include "smbXcli_base.h"
33 #include "librpc/ndr/libndr.h"
35 struct smbXcli_conn;
36 struct smbXcli_req;
37 struct smbXcli_session;
39 struct smbXcli_conn {
40 int read_fd;
41 int write_fd;
42 struct sockaddr_storage local_ss;
43 struct sockaddr_storage remote_ss;
44 const char *remote_name;
46 struct tevent_queue *outgoing;
47 struct tevent_req **pending;
48 struct tevent_req *read_smb_req;
50 enum protocol_types protocol;
51 bool allow_signing;
52 bool desire_signing;
53 bool mandatory_signing;
56 * The incoming dispatch function should return:
57 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
58 * - NT_STATUS_OK, if no more processing is desired, e.g.
59 * the dispatch function called
60 * tevent_req_done().
61 * - All other return values disconnect the connection.
63 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
64 TALLOC_CTX *tmp_mem,
65 uint8_t *inbuf);
67 struct {
68 struct {
69 uint32_t capabilities;
70 uint32_t max_xmit;
71 } client;
73 struct {
74 uint32_t capabilities;
75 uint32_t max_xmit;
76 uint16_t max_mux;
77 uint16_t security_mode;
78 bool readbraw;
79 bool writebraw;
80 bool lockread;
81 bool writeunlock;
82 uint32_t session_key;
83 struct GUID guid;
84 DATA_BLOB gss_blob;
85 uint8_t challenge[8];
86 const char *workgroup;
87 const char *name;
88 int time_zone;
89 NTTIME system_time;
90 } server;
92 uint32_t capabilities;
93 uint32_t max_xmit;
95 uint16_t mid;
97 struct smb_signing_state *signing;
98 struct smb_trans_enc_state *trans_enc;
100 struct tevent_req *read_braw_req;
101 } smb1;
103 struct {
104 struct {
105 uint32_t capabilities;
106 uint16_t security_mode;
107 struct GUID guid;
108 } client;
110 struct {
111 uint32_t capabilities;
112 uint16_t security_mode;
113 struct GUID guid;
114 uint32_t max_trans_size;
115 uint32_t max_read_size;
116 uint32_t max_write_size;
117 NTTIME system_time;
118 NTTIME start_time;
119 DATA_BLOB gss_blob;
120 } server;
122 uint64_t mid;
123 uint16_t cur_credits;
124 uint16_t max_credits;
125 } smb2;
127 struct smbXcli_session *sessions;
130 struct smbXcli_session {
131 struct smbXcli_session *prev, *next;
132 struct smbXcli_conn *conn;
134 struct {
135 uint64_t session_id;
136 uint16_t session_flags;
137 DATA_BLOB application_key;
138 DATA_BLOB signing_key;
139 bool should_sign;
140 DATA_BLOB channel_signing_key;
141 } smb2;
144 struct smbXcli_req_state {
145 struct tevent_context *ev;
146 struct smbXcli_conn *conn;
147 struct smbXcli_session *session; /* maybe NULL */
149 uint8_t length_hdr[4];
151 bool one_way;
153 uint8_t *inbuf;
155 struct {
156 /* Space for the header including the wct */
157 uint8_t hdr[HDR_VWV];
160 * For normal requests, smb1cli_req_send chooses a mid.
161 * SecondaryV trans requests need to use the mid of the primary
162 * request, so we need a place to store it.
163 * Assume it is set if != 0.
165 uint16_t mid;
167 uint16_t *vwv;
168 uint8_t bytecount_buf[2];
170 #define MAX_SMB_IOV 10
171 /* length_hdr, hdr, words, byte_count, buffers */
172 struct iovec iov[1 + 3 + MAX_SMB_IOV];
173 int iov_count;
175 bool one_way_seqnum;
176 uint32_t seqnum;
177 struct tevent_req **chained_requests;
179 uint8_t recv_cmd;
180 NTSTATUS recv_status;
181 /* always an array of 3 talloc elements */
182 struct iovec *recv_iov;
183 } smb1;
185 struct {
186 const uint8_t *fixed;
187 uint16_t fixed_len;
188 const uint8_t *dyn;
189 uint32_t dyn_len;
191 uint8_t hdr[64];
192 uint8_t pad[7]; /* padding space for compounding */
194 /* always an array of 3 talloc elements */
195 struct iovec *recv_iov;
197 uint16_t credit_charge;
199 bool signing_skipped;
200 bool notify_async;
201 bool got_async;
202 } smb2;
205 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
208 * NT_STATUS_OK, means we do not notify the callers
210 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
212 while (conn->sessions) {
213 conn->sessions->conn = NULL;
214 DLIST_REMOVE(conn->sessions, conn->sessions);
217 if (conn->smb1.trans_enc) {
218 TALLOC_FREE(conn->smb1.trans_enc);
221 return 0;
224 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
225 int fd,
226 const char *remote_name,
227 enum smb_signing_setting signing_state,
228 uint32_t smb1_capabilities,
229 struct GUID *client_guid,
230 uint32_t smb2_capabilities)
232 struct smbXcli_conn *conn = NULL;
233 void *ss = NULL;
234 struct sockaddr *sa = NULL;
235 socklen_t sa_length;
236 int ret;
238 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
239 if (!conn) {
240 return NULL;
243 conn->read_fd = fd;
244 conn->write_fd = dup(fd);
245 if (conn->write_fd == -1) {
246 goto error;
249 conn->remote_name = talloc_strdup(conn, remote_name);
250 if (conn->remote_name == NULL) {
251 goto error;
255 ss = (void *)&conn->local_ss;
256 sa = (struct sockaddr *)ss;
257 sa_length = sizeof(conn->local_ss);
258 ret = getsockname(fd, sa, &sa_length);
259 if (ret == -1) {
260 goto error;
262 ss = (void *)&conn->remote_ss;
263 sa = (struct sockaddr *)ss;
264 sa_length = sizeof(conn->remote_ss);
265 ret = getpeername(fd, sa, &sa_length);
266 if (ret == -1) {
267 goto error;
270 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
271 if (conn->outgoing == NULL) {
272 goto error;
274 conn->pending = NULL;
276 conn->protocol = PROTOCOL_NONE;
278 switch (signing_state) {
279 case SMB_SIGNING_OFF:
280 /* never */
281 conn->allow_signing = false;
282 conn->desire_signing = false;
283 conn->mandatory_signing = false;
284 break;
285 case SMB_SIGNING_DEFAULT:
286 case SMB_SIGNING_IF_REQUIRED:
287 /* if the server requires it */
288 conn->allow_signing = true;
289 conn->desire_signing = false;
290 conn->mandatory_signing = false;
291 break;
292 case SMB_SIGNING_REQUIRED:
293 /* always */
294 conn->allow_signing = true;
295 conn->desire_signing = true;
296 conn->mandatory_signing = true;
297 break;
300 conn->smb1.client.capabilities = smb1_capabilities;
301 conn->smb1.client.max_xmit = UINT16_MAX;
303 conn->smb1.capabilities = conn->smb1.client.capabilities;
304 conn->smb1.max_xmit = 1024;
306 conn->smb1.mid = 1;
308 /* initialise signing */
309 conn->smb1.signing = smb_signing_init(conn,
310 conn->allow_signing,
311 conn->desire_signing,
312 conn->mandatory_signing);
313 if (!conn->smb1.signing) {
314 goto error;
317 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
318 if (conn->mandatory_signing) {
319 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
321 if (client_guid) {
322 conn->smb2.client.guid = *client_guid;
324 conn->smb2.client.capabilities = smb2_capabilities;
326 conn->smb2.cur_credits = 1;
327 conn->smb2.max_credits = 0;
329 talloc_set_destructor(conn, smbXcli_conn_destructor);
330 return conn;
332 error:
333 if (conn->write_fd != -1) {
334 close(conn->write_fd);
336 TALLOC_FREE(conn);
337 return NULL;
340 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
342 if (conn == NULL) {
343 return false;
346 if (conn->read_fd == -1) {
347 return false;
350 return true;
353 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
355 return conn->protocol;
358 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
360 if (conn->protocol >= PROTOCOL_SMB2_02) {
361 return true;
364 if (conn->smb1.capabilities & CAP_UNICODE) {
365 return true;
368 return false;
371 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
373 set_socket_options(conn->read_fd, options);
376 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
378 return &conn->local_ss;
381 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
383 return &conn->remote_ss;
386 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
388 return conn->remote_name;
391 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
393 if (conn->protocol >= PROTOCOL_SMB2_02) {
395 * TODO...
397 return 1;
400 return conn->smb1.server.max_mux;
403 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
405 if (conn->protocol >= PROTOCOL_SMB2_02) {
406 return conn->smb2.server.system_time;
409 return conn->smb1.server.system_time;
412 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
414 if (conn->protocol >= PROTOCOL_SMB2_02) {
415 return &conn->smb2.server.gss_blob;
418 return &conn->smb1.server.gss_blob;
421 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
423 if (conn->protocol >= PROTOCOL_SMB2_02) {
424 return &conn->smb2.server.guid;
427 return &conn->smb1.server.guid;
430 struct smbXcli_conn_samba_suicide_state {
431 struct smbXcli_conn *conn;
432 struct iovec iov;
433 uint8_t buf[9];
436 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
438 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
439 struct tevent_context *ev,
440 struct smbXcli_conn *conn,
441 uint8_t exitcode)
443 struct tevent_req *req, *subreq;
444 struct smbXcli_conn_samba_suicide_state *state;
446 req = tevent_req_create(mem_ctx, &state,
447 struct smbXcli_conn_samba_suicide_state);
448 if (req == NULL) {
449 return NULL;
451 state->conn = conn;
452 SIVAL(state->buf, 4, 0x74697865);
453 SCVAL(state->buf, 8, exitcode);
454 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
456 state->iov.iov_base = state->buf;
457 state->iov.iov_len = sizeof(state->buf);
459 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
460 false, &state->iov, 1);
461 if (tevent_req_nomem(subreq, req)) {
462 return tevent_req_post(req, ev);
464 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
465 return req;
468 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
470 struct tevent_req *req = tevent_req_callback_data(
471 subreq, struct tevent_req);
472 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
473 req, struct smbXcli_conn_samba_suicide_state);
474 ssize_t nwritten;
475 int err;
477 nwritten = writev_recv(subreq, &err);
478 TALLOC_FREE(subreq);
479 if (nwritten == -1) {
480 NTSTATUS status = map_nt_error_from_unix_common(err);
481 smbXcli_conn_disconnect(state->conn, status);
482 return;
484 tevent_req_done(req);
487 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
489 return tevent_req_simple_recv_ntstatus(req);
492 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
493 uint8_t exitcode)
495 TALLOC_CTX *frame = talloc_stackframe();
496 struct tevent_context *ev;
497 struct tevent_req *req;
498 NTSTATUS status = NT_STATUS_NO_MEMORY;
499 bool ok;
501 if (smbXcli_conn_has_async_calls(conn)) {
503 * Can't use sync call while an async call is in flight
505 status = NT_STATUS_INVALID_PARAMETER_MIX;
506 goto fail;
508 ev = tevent_context_init(frame);
509 if (ev == NULL) {
510 goto fail;
512 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
513 if (req == NULL) {
514 goto fail;
516 ok = tevent_req_poll(req, ev);
517 if (!ok) {
518 status = map_nt_error_from_unix_common(errno);
519 goto fail;
521 status = smbXcli_conn_samba_suicide_recv(req);
522 fail:
523 TALLOC_FREE(frame);
524 return status;
527 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
529 return conn->smb1.capabilities;
532 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
534 return conn->smb1.max_xmit;
537 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
539 return conn->smb1.server.session_key;
542 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
544 return conn->smb1.server.challenge;
547 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
549 return conn->smb1.server.security_mode;
552 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
554 return conn->smb1.server.readbraw;
557 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
559 return conn->smb1.server.writebraw;
562 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
564 return conn->smb1.server.lockread;
567 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
569 return conn->smb1.server.writeunlock;
572 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
574 return conn->smb1.server.time_zone;
577 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
578 const DATA_BLOB user_session_key,
579 const DATA_BLOB response)
581 return smb_signing_activate(conn->smb1.signing,
582 user_session_key,
583 response);
586 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
587 const uint8_t *buf, uint32_t seqnum)
589 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
592 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
594 return smb_signing_is_active(conn->smb1.signing);
597 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
598 struct smb_trans_enc_state *es)
600 /* Replace the old state, if any. */
601 if (conn->smb1.trans_enc) {
602 TALLOC_FREE(conn->smb1.trans_enc);
604 conn->smb1.trans_enc = es;
607 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
609 return common_encryption_on(conn->smb1.trans_enc);
613 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
615 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
616 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
618 if (NT_STATUS_IS_OK(status)) {
619 return NT_STATUS_OK;
622 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
623 return status;
626 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
630 * Is the SMB command able to hold an AND_X successor
631 * @param[in] cmd The SMB command in question
632 * @retval Can we add a chained request after "cmd"?
634 bool smb1cli_is_andx_req(uint8_t cmd)
636 switch (cmd) {
637 case SMBtconX:
638 case SMBlockingX:
639 case SMBopenX:
640 case SMBreadX:
641 case SMBwriteX:
642 case SMBsesssetupX:
643 case SMBulogoffX:
644 case SMBntcreateX:
645 return true;
646 break;
647 default:
648 break;
651 return false;
654 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
656 size_t num_pending = talloc_array_length(conn->pending);
657 uint16_t result;
659 while (true) {
660 size_t i;
662 result = conn->smb1.mid++;
663 if ((result == 0) || (result == 0xffff)) {
664 continue;
667 for (i=0; i<num_pending; i++) {
668 if (result == smb1cli_req_mid(conn->pending[i])) {
669 break;
673 if (i == num_pending) {
674 return result;
679 void smbXcli_req_unset_pending(struct tevent_req *req)
681 struct smbXcli_req_state *state =
682 tevent_req_data(req,
683 struct smbXcli_req_state);
684 struct smbXcli_conn *conn = state->conn;
685 size_t num_pending = talloc_array_length(conn->pending);
686 size_t i;
688 if (state->smb1.mid != 0) {
690 * This is a [nt]trans[2] request which waits
691 * for more than one reply.
693 return;
696 talloc_set_destructor(req, NULL);
698 if (num_pending == 1) {
700 * The pending read_smb tevent_req is a child of
701 * conn->pending. So if nothing is pending anymore, we need to
702 * delete the socket read fde.
704 TALLOC_FREE(conn->pending);
705 conn->read_smb_req = NULL;
706 return;
709 for (i=0; i<num_pending; i++) {
710 if (req == conn->pending[i]) {
711 break;
714 if (i == num_pending) {
716 * Something's seriously broken. Just returning here is the
717 * right thing nevertheless, the point of this routine is to
718 * remove ourselves from conn->pending.
720 return;
724 * Remove ourselves from the conn->pending array
726 for (; i < (num_pending - 1); i++) {
727 conn->pending[i] = conn->pending[i+1];
731 * No NULL check here, we're shrinking by sizeof(void *), and
732 * talloc_realloc just adjusts the size for this.
734 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
735 num_pending - 1);
736 return;
739 static int smbXcli_req_destructor(struct tevent_req *req)
741 struct smbXcli_req_state *state =
742 tevent_req_data(req,
743 struct smbXcli_req_state);
746 * Make sure we really remove it from
747 * the pending array on destruction.
749 state->smb1.mid = 0;
750 smbXcli_req_unset_pending(req);
751 return 0;
754 static bool smb1cli_req_cancel(struct tevent_req *req);
755 static bool smb2cli_req_cancel(struct tevent_req *req);
757 static bool smbXcli_req_cancel(struct tevent_req *req)
759 struct smbXcli_req_state *state =
760 tevent_req_data(req,
761 struct smbXcli_req_state);
763 if (!smbXcli_conn_is_connected(state->conn)) {
764 return false;
767 if (state->conn->protocol == PROTOCOL_NONE) {
768 return false;
771 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
772 return smb2cli_req_cancel(req);
775 return smb1cli_req_cancel(req);
778 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
780 bool smbXcli_req_set_pending(struct tevent_req *req)
782 struct smbXcli_req_state *state =
783 tevent_req_data(req,
784 struct smbXcli_req_state);
785 struct smbXcli_conn *conn;
786 struct tevent_req **pending;
787 size_t num_pending;
789 conn = state->conn;
791 if (!smbXcli_conn_is_connected(conn)) {
792 return false;
795 num_pending = talloc_array_length(conn->pending);
797 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
798 num_pending+1);
799 if (pending == NULL) {
800 return false;
802 pending[num_pending] = req;
803 conn->pending = pending;
804 talloc_set_destructor(req, smbXcli_req_destructor);
805 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
807 if (!smbXcli_conn_receive_next(conn)) {
809 * the caller should notify the current request
811 * And all other pending requests get notified
812 * by smbXcli_conn_disconnect().
814 smbXcli_req_unset_pending(req);
815 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
816 return false;
819 return true;
822 static void smbXcli_conn_received(struct tevent_req *subreq);
824 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
826 size_t num_pending = talloc_array_length(conn->pending);
827 struct tevent_req *req;
828 struct smbXcli_req_state *state;
830 if (conn->read_smb_req != NULL) {
831 return true;
834 if (num_pending == 0) {
835 if (conn->smb2.mid < UINT64_MAX) {
836 /* no more pending requests, so we are done for now */
837 return true;
841 * If there are no more SMB2 requests possible,
842 * because we are out of message ids,
843 * we need to disconnect.
845 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
846 return true;
849 req = conn->pending[0];
850 state = tevent_req_data(req, struct smbXcli_req_state);
853 * We're the first ones, add the read_smb request that waits for the
854 * answer from the server
856 conn->read_smb_req = read_smb_send(conn->pending,
857 state->ev,
858 conn->read_fd);
859 if (conn->read_smb_req == NULL) {
860 return false;
862 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
863 return true;
866 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
868 tevent_queue_stop(conn->outgoing);
870 if (conn->read_fd != -1) {
871 close(conn->read_fd);
873 if (conn->write_fd != -1) {
874 close(conn->write_fd);
876 conn->read_fd = -1;
877 conn->write_fd = -1;
880 * Cancel all pending requests. We do not do a for-loop walking
881 * conn->pending because that array changes in
882 * smbXcli_req_unset_pending.
884 while (talloc_array_length(conn->pending) > 0) {
885 struct tevent_req *req;
886 struct smbXcli_req_state *state;
887 struct tevent_req **chain;
888 size_t num_chained;
889 size_t i;
891 req = conn->pending[0];
892 state = tevent_req_data(req, struct smbXcli_req_state);
894 if (state->smb1.chained_requests == NULL) {
896 * We're dead. No point waiting for trans2
897 * replies.
899 state->smb1.mid = 0;
901 smbXcli_req_unset_pending(req);
903 if (NT_STATUS_IS_OK(status)) {
904 /* do not notify the callers */
905 continue;
909 * we need to defer the callback, because we may notify
910 * more then one caller.
912 tevent_req_defer_callback(req, state->ev);
913 tevent_req_nterror(req, status);
914 continue;
917 chain = talloc_move(conn, &state->smb1.chained_requests);
918 num_chained = talloc_array_length(chain);
920 for (i=0; i<num_chained; i++) {
921 req = chain[i];
922 state = tevent_req_data(req, struct smbXcli_req_state);
925 * We're dead. No point waiting for trans2
926 * replies.
928 state->smb1.mid = 0;
930 smbXcli_req_unset_pending(req);
932 if (NT_STATUS_IS_OK(status)) {
933 /* do not notify the callers */
934 continue;
938 * we need to defer the callback, because we may notify
939 * more than one caller.
941 tevent_req_defer_callback(req, state->ev);
942 tevent_req_nterror(req, status);
944 TALLOC_FREE(chain);
949 * Fetch a smb request's mid. Only valid after the request has been sent by
950 * smb1cli_req_send().
952 uint16_t smb1cli_req_mid(struct tevent_req *req)
954 struct smbXcli_req_state *state =
955 tevent_req_data(req,
956 struct smbXcli_req_state);
958 if (state->smb1.mid != 0) {
959 return state->smb1.mid;
962 return SVAL(state->smb1.hdr, HDR_MID);
965 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
967 struct smbXcli_req_state *state =
968 tevent_req_data(req,
969 struct smbXcli_req_state);
971 state->smb1.mid = mid;
974 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
976 struct smbXcli_req_state *state =
977 tevent_req_data(req,
978 struct smbXcli_req_state);
980 return state->smb1.seqnum;
983 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
985 struct smbXcli_req_state *state =
986 tevent_req_data(req,
987 struct smbXcli_req_state);
989 state->smb1.seqnum = seqnum;
992 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
994 size_t result = 0;
995 int i;
996 for (i=0; i<count; i++) {
997 result += iov[i].iov_len;
999 return result;
1002 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1003 const struct iovec *iov,
1004 int count)
1006 size_t len = smbXcli_iov_len(iov, count);
1007 size_t copied;
1008 uint8_t *buf;
1009 int i;
1011 buf = talloc_array(mem_ctx, uint8_t, len);
1012 if (buf == NULL) {
1013 return NULL;
1015 copied = 0;
1016 for (i=0; i<count; i++) {
1017 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1018 copied += iov[i].iov_len;
1020 return buf;
1023 static void smb1cli_req_flags(enum protocol_types protocol,
1024 uint32_t smb1_capabilities,
1025 uint8_t smb_command,
1026 uint8_t additional_flags,
1027 uint8_t clear_flags,
1028 uint8_t *_flags,
1029 uint16_t additional_flags2,
1030 uint16_t clear_flags2,
1031 uint16_t *_flags2)
1033 uint8_t flags = 0;
1034 uint16_t flags2 = 0;
1036 if (protocol >= PROTOCOL_LANMAN1) {
1037 flags |= FLAG_CASELESS_PATHNAMES;
1038 flags |= FLAG_CANONICAL_PATHNAMES;
1041 if (protocol >= PROTOCOL_LANMAN2) {
1042 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1043 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1046 if (protocol >= PROTOCOL_NT1) {
1047 flags2 |= FLAGS2_IS_LONG_NAME;
1049 if (smb1_capabilities & CAP_UNICODE) {
1050 flags2 |= FLAGS2_UNICODE_STRINGS;
1052 if (smb1_capabilities & CAP_STATUS32) {
1053 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1055 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1056 flags2 |= FLAGS2_EXTENDED_SECURITY;
1060 flags |= additional_flags;
1061 flags &= ~clear_flags;
1062 flags2 |= additional_flags2;
1063 flags2 &= ~clear_flags2;
1065 *_flags = flags;
1066 *_flags2 = flags2;
1069 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1071 static bool smb1cli_req_cancel(struct tevent_req *req)
1073 struct smbXcli_req_state *state =
1074 tevent_req_data(req,
1075 struct smbXcli_req_state);
1076 uint8_t flags;
1077 uint16_t flags2;
1078 uint32_t pid;
1079 uint16_t tid;
1080 uint16_t uid;
1081 uint16_t mid;
1082 struct tevent_req *subreq;
1083 NTSTATUS status;
1085 flags = CVAL(state->smb1.hdr, HDR_FLG);
1086 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1087 pid = SVAL(state->smb1.hdr, HDR_PID);
1088 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1089 tid = SVAL(state->smb1.hdr, HDR_TID);
1090 uid = SVAL(state->smb1.hdr, HDR_UID);
1091 mid = SVAL(state->smb1.hdr, HDR_MID);
1093 subreq = smb1cli_req_create(state, state->ev,
1094 state->conn,
1095 SMBntcancel,
1096 flags, 0,
1097 flags2, 0,
1098 0, /* timeout */
1099 pid, tid, uid,
1100 0, NULL, /* vwv */
1101 0, NULL); /* bytes */
1102 if (subreq == NULL) {
1103 return false;
1105 smb1cli_req_set_mid(subreq, mid);
1107 status = smb1cli_req_chain_submit(&subreq, 1);
1108 if (!NT_STATUS_IS_OK(status)) {
1109 TALLOC_FREE(subreq);
1110 return false;
1112 smb1cli_req_set_mid(subreq, 0);
1114 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1116 return true;
1119 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1121 /* we do not care about the result */
1122 TALLOC_FREE(subreq);
1125 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1126 struct tevent_context *ev,
1127 struct smbXcli_conn *conn,
1128 uint8_t smb_command,
1129 uint8_t additional_flags,
1130 uint8_t clear_flags,
1131 uint16_t additional_flags2,
1132 uint16_t clear_flags2,
1133 uint32_t timeout_msec,
1134 uint32_t pid,
1135 uint16_t tid,
1136 uint16_t uid,
1137 uint8_t wct, uint16_t *vwv,
1138 int iov_count,
1139 struct iovec *bytes_iov)
1141 struct tevent_req *req;
1142 struct smbXcli_req_state *state;
1143 uint8_t flags = 0;
1144 uint16_t flags2 = 0;
1146 if (iov_count > MAX_SMB_IOV) {
1148 * Should not happen :-)
1150 return NULL;
1153 req = tevent_req_create(mem_ctx, &state,
1154 struct smbXcli_req_state);
1155 if (req == NULL) {
1156 return NULL;
1158 state->ev = ev;
1159 state->conn = conn;
1161 state->smb1.recv_cmd = 0xFF;
1162 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1163 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1164 if (state->smb1.recv_iov == NULL) {
1165 TALLOC_FREE(req);
1166 return NULL;
1169 smb1cli_req_flags(conn->protocol,
1170 conn->smb1.capabilities,
1171 smb_command,
1172 additional_flags,
1173 clear_flags,
1174 &flags,
1175 additional_flags2,
1176 clear_flags2,
1177 &flags2);
1179 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1180 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1181 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1182 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1183 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1184 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1185 SSVAL(state->smb1.hdr, HDR_TID, tid);
1186 SSVAL(state->smb1.hdr, HDR_PID, pid);
1187 SSVAL(state->smb1.hdr, HDR_UID, uid);
1188 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1189 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1191 state->smb1.vwv = vwv;
1193 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1195 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1196 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1197 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1198 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1199 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1200 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1201 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1202 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1204 if (iov_count != 0) {
1205 memcpy(&state->smb1.iov[4], bytes_iov,
1206 iov_count * sizeof(*bytes_iov));
1208 state->smb1.iov_count = iov_count + 4;
1210 if (timeout_msec > 0) {
1211 struct timeval endtime;
1213 endtime = timeval_current_ofs_msec(timeout_msec);
1214 if (!tevent_req_set_endtime(req, ev, endtime)) {
1215 return req;
1219 switch (smb_command) {
1220 case SMBtranss:
1221 case SMBtranss2:
1222 case SMBnttranss:
1223 state->one_way = true;
1224 break;
1225 case SMBntcancel:
1226 state->one_way = true;
1227 state->smb1.one_way_seqnum = true;
1228 break;
1229 case SMBlockingX:
1230 if ((wct == 8) &&
1231 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1232 state->one_way = true;
1234 break;
1237 return req;
1240 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1241 struct iovec *iov, int iov_count,
1242 uint32_t *seqnum,
1243 bool one_way_seqnum)
1245 TALLOC_CTX *frame = NULL;
1246 uint8_t *buf;
1249 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1250 * iovec directly. MD5Update would do that just fine.
1253 if (iov_count < 4) {
1254 return NT_STATUS_INVALID_PARAMETER_MIX;
1256 if (iov[0].iov_len != NBT_HDR_SIZE) {
1257 return NT_STATUS_INVALID_PARAMETER_MIX;
1259 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1260 return NT_STATUS_INVALID_PARAMETER_MIX;
1262 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1263 return NT_STATUS_INVALID_PARAMETER_MIX;
1265 if (iov[3].iov_len != sizeof(uint16_t)) {
1266 return NT_STATUS_INVALID_PARAMETER_MIX;
1269 frame = talloc_stackframe();
1271 buf = smbXcli_iov_concat(frame, iov, iov_count);
1272 if (buf == NULL) {
1273 return NT_STATUS_NO_MEMORY;
1276 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1277 one_way_seqnum);
1278 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1279 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1281 TALLOC_FREE(frame);
1282 return NT_STATUS_OK;
1285 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1286 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1287 TALLOC_CTX *tmp_mem,
1288 uint8_t *inbuf);
1290 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1291 struct smbXcli_req_state *state,
1292 struct iovec *iov, int iov_count)
1294 struct tevent_req *subreq;
1295 NTSTATUS status;
1296 uint8_t cmd;
1297 uint16_t mid;
1299 if (!smbXcli_conn_is_connected(state->conn)) {
1300 return NT_STATUS_CONNECTION_DISCONNECTED;
1303 if (state->conn->protocol > PROTOCOL_NT1) {
1304 return NT_STATUS_REVISION_MISMATCH;
1307 if (iov_count < 4) {
1308 return NT_STATUS_INVALID_PARAMETER_MIX;
1310 if (iov[0].iov_len != NBT_HDR_SIZE) {
1311 return NT_STATUS_INVALID_PARAMETER_MIX;
1313 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1314 return NT_STATUS_INVALID_PARAMETER_MIX;
1316 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1317 return NT_STATUS_INVALID_PARAMETER_MIX;
1319 if (iov[3].iov_len != sizeof(uint16_t)) {
1320 return NT_STATUS_INVALID_PARAMETER_MIX;
1323 cmd = CVAL(iov[1].iov_base, HDR_COM);
1324 if (cmd == SMBreadBraw) {
1325 if (smbXcli_conn_has_async_calls(state->conn)) {
1326 return NT_STATUS_INVALID_PARAMETER_MIX;
1328 state->conn->smb1.read_braw_req = req;
1331 if (state->smb1.mid != 0) {
1332 mid = state->smb1.mid;
1333 } else {
1334 mid = smb1cli_alloc_mid(state->conn);
1336 SSVAL(iov[1].iov_base, HDR_MID, mid);
1338 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1340 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1341 &state->smb1.seqnum,
1342 state->smb1.one_way_seqnum);
1344 if (!NT_STATUS_IS_OK(status)) {
1345 return status;
1349 * If we supported multiple encrytion contexts
1350 * here we'd look up based on tid.
1352 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1353 char *buf, *enc_buf;
1355 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1356 if (buf == NULL) {
1357 return NT_STATUS_NO_MEMORY;
1359 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1360 (char *)buf, &enc_buf);
1361 TALLOC_FREE(buf);
1362 if (!NT_STATUS_IS_OK(status)) {
1363 DEBUG(0, ("Error in encrypting client message: %s\n",
1364 nt_errstr(status)));
1365 return status;
1367 buf = (char *)talloc_memdup(state, enc_buf,
1368 smb_len_nbt(enc_buf)+4);
1369 SAFE_FREE(enc_buf);
1370 if (buf == NULL) {
1371 return NT_STATUS_NO_MEMORY;
1373 iov[0].iov_base = (void *)buf;
1374 iov[0].iov_len = talloc_get_size(buf);
1375 iov_count = 1;
1378 if (state->conn->dispatch_incoming == NULL) {
1379 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1382 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1384 subreq = writev_send(state, state->ev, state->conn->outgoing,
1385 state->conn->write_fd, false, iov, iov_count);
1386 if (subreq == NULL) {
1387 return NT_STATUS_NO_MEMORY;
1389 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1390 return NT_STATUS_OK;
1393 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1394 struct tevent_context *ev,
1395 struct smbXcli_conn *conn,
1396 uint8_t smb_command,
1397 uint8_t additional_flags,
1398 uint8_t clear_flags,
1399 uint16_t additional_flags2,
1400 uint16_t clear_flags2,
1401 uint32_t timeout_msec,
1402 uint32_t pid,
1403 uint16_t tid,
1404 uint16_t uid,
1405 uint8_t wct, uint16_t *vwv,
1406 uint32_t num_bytes,
1407 const uint8_t *bytes)
1409 struct tevent_req *req;
1410 struct iovec iov;
1411 NTSTATUS status;
1413 iov.iov_base = discard_const_p(void, bytes);
1414 iov.iov_len = num_bytes;
1416 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1417 additional_flags, clear_flags,
1418 additional_flags2, clear_flags2,
1419 timeout_msec,
1420 pid, tid, uid,
1421 wct, vwv, 1, &iov);
1422 if (req == NULL) {
1423 return NULL;
1425 if (!tevent_req_is_in_progress(req)) {
1426 return tevent_req_post(req, ev);
1428 status = smb1cli_req_chain_submit(&req, 1);
1429 if (tevent_req_nterror(req, status)) {
1430 return tevent_req_post(req, ev);
1432 return req;
1435 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1437 struct tevent_req *req =
1438 tevent_req_callback_data(subreq,
1439 struct tevent_req);
1440 struct smbXcli_req_state *state =
1441 tevent_req_data(req,
1442 struct smbXcli_req_state);
1443 ssize_t nwritten;
1444 int err;
1446 nwritten = writev_recv(subreq, &err);
1447 TALLOC_FREE(subreq);
1448 if (nwritten == -1) {
1449 NTSTATUS status = map_nt_error_from_unix_common(err);
1450 smbXcli_conn_disconnect(state->conn, status);
1451 return;
1454 if (state->one_way) {
1455 state->inbuf = NULL;
1456 tevent_req_done(req);
1457 return;
1460 if (!smbXcli_req_set_pending(req)) {
1461 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1462 return;
1466 static void smbXcli_conn_received(struct tevent_req *subreq)
1468 struct smbXcli_conn *conn =
1469 tevent_req_callback_data(subreq,
1470 struct smbXcli_conn);
1471 TALLOC_CTX *frame = talloc_stackframe();
1472 NTSTATUS status;
1473 uint8_t *inbuf;
1474 ssize_t received;
1475 int err;
1477 if (subreq != conn->read_smb_req) {
1478 DEBUG(1, ("Internal error: cli_smb_received called with "
1479 "unexpected subreq\n"));
1480 status = NT_STATUS_INTERNAL_ERROR;
1481 smbXcli_conn_disconnect(conn, status);
1482 TALLOC_FREE(frame);
1483 return;
1485 conn->read_smb_req = NULL;
1487 received = read_smb_recv(subreq, frame, &inbuf, &err);
1488 TALLOC_FREE(subreq);
1489 if (received == -1) {
1490 status = map_nt_error_from_unix_common(err);
1491 smbXcli_conn_disconnect(conn, status);
1492 TALLOC_FREE(frame);
1493 return;
1496 status = conn->dispatch_incoming(conn, frame, inbuf);
1497 TALLOC_FREE(frame);
1498 if (NT_STATUS_IS_OK(status)) {
1500 * We should not do any more processing
1501 * as the dispatch function called
1502 * tevent_req_done().
1504 return;
1505 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1507 * We got an error, so notify all pending requests
1509 smbXcli_conn_disconnect(conn, status);
1510 return;
1514 * We got NT_STATUS_RETRY, so we may ask for a
1515 * next incoming pdu.
1517 if (!smbXcli_conn_receive_next(conn)) {
1518 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1522 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1523 struct iovec **piov, int *pnum_iov)
1525 struct iovec *iov;
1526 int num_iov;
1527 size_t buflen;
1528 size_t taken;
1529 size_t remaining;
1530 uint8_t *hdr;
1531 uint8_t cmd;
1532 uint32_t wct_ofs;
1534 buflen = smb_len_nbt(buf);
1535 taken = 0;
1537 hdr = buf + NBT_HDR_SIZE;
1539 if (buflen < MIN_SMB_SIZE) {
1540 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1544 * This returns iovec elements in the following order:
1546 * - SMB header
1548 * - Parameter Block
1549 * - Data Block
1551 * - Parameter Block
1552 * - Data Block
1554 * - Parameter Block
1555 * - Data Block
1557 num_iov = 1;
1559 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1560 if (iov == NULL) {
1561 return NT_STATUS_NO_MEMORY;
1563 iov[0].iov_base = hdr;
1564 iov[0].iov_len = HDR_WCT;
1565 taken += HDR_WCT;
1567 cmd = CVAL(hdr, HDR_COM);
1568 wct_ofs = HDR_WCT;
1570 while (true) {
1571 size_t len = buflen - taken;
1572 struct iovec *cur;
1573 struct iovec *iov_tmp;
1574 uint8_t wct;
1575 uint32_t bcc_ofs;
1576 uint16_t bcc;
1577 size_t needed;
1580 * we need at least WCT and BCC
1582 needed = sizeof(uint8_t) + sizeof(uint16_t);
1583 if (len < needed) {
1584 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1585 __location__, (int)len, (int)needed));
1586 goto inval;
1590 * Now we check if the specified words are there
1592 wct = CVAL(hdr, wct_ofs);
1593 needed += wct * sizeof(uint16_t);
1594 if (len < needed) {
1595 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1596 __location__, (int)len, (int)needed));
1597 goto inval;
1601 * Now we check if the specified bytes are there
1603 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1604 bcc = SVAL(hdr, bcc_ofs);
1605 needed += bcc * sizeof(uint8_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 * we allocate 2 iovec structures for words and bytes
1615 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1616 num_iov + 2);
1617 if (iov_tmp == NULL) {
1618 TALLOC_FREE(iov);
1619 return NT_STATUS_NO_MEMORY;
1621 iov = iov_tmp;
1622 cur = &iov[num_iov];
1623 num_iov += 2;
1625 cur[0].iov_len = wct * sizeof(uint16_t);
1626 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1627 cur[1].iov_len = bcc * sizeof(uint8_t);
1628 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1630 taken += needed;
1632 if (!smb1cli_is_andx_req(cmd)) {
1634 * If the current command does not have AndX chanining
1635 * we are done.
1637 break;
1640 if (wct == 0 && bcc == 0) {
1642 * An empty response also ends the chain,
1643 * most likely with an error.
1645 break;
1648 if (wct < 2) {
1649 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1650 __location__, (int)wct, (int)cmd));
1651 goto inval;
1653 cmd = CVAL(cur[0].iov_base, 0);
1654 if (cmd == 0xFF) {
1656 * If it is the end of the chain we are also done.
1658 break;
1660 wct_ofs = SVAL(cur[0].iov_base, 2);
1662 if (wct_ofs < taken) {
1663 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1665 if (wct_ofs > buflen) {
1666 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1670 * we consumed everything up to the start of the next
1671 * parameter block.
1673 taken = wct_ofs;
1676 remaining = buflen - taken;
1678 if (remaining > 0 && num_iov >= 3) {
1680 * The last DATA block gets the remaining
1681 * bytes, this is needed to support
1682 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1684 iov[num_iov-1].iov_len += remaining;
1687 *piov = iov;
1688 *pnum_iov = num_iov;
1689 return NT_STATUS_OK;
1691 inval:
1692 TALLOC_FREE(iov);
1693 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1696 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1697 TALLOC_CTX *tmp_mem,
1698 uint8_t *inbuf)
1700 struct tevent_req *req;
1701 struct smbXcli_req_state *state;
1702 NTSTATUS status;
1703 size_t num_pending;
1704 size_t i;
1705 uint8_t cmd;
1706 uint16_t mid;
1707 bool oplock_break;
1708 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1709 struct iovec *iov = NULL;
1710 int num_iov = 0;
1711 struct tevent_req **chain = NULL;
1712 size_t num_chained = 0;
1713 size_t num_responses = 0;
1715 if (conn->smb1.read_braw_req != NULL) {
1716 req = conn->smb1.read_braw_req;
1717 conn->smb1.read_braw_req = NULL;
1718 state = tevent_req_data(req, struct smbXcli_req_state);
1720 smbXcli_req_unset_pending(req);
1722 if (state->smb1.recv_iov == NULL) {
1724 * For requests with more than
1725 * one response, we have to readd the
1726 * recv_iov array.
1728 state->smb1.recv_iov = talloc_zero_array(state,
1729 struct iovec,
1731 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1732 return NT_STATUS_OK;
1736 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1737 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1738 ZERO_STRUCT(state->smb1.recv_iov[1]);
1739 ZERO_STRUCT(state->smb1.recv_iov[2]);
1741 state->smb1.recv_cmd = SMBreadBraw;
1742 state->smb1.recv_status = NT_STATUS_OK;
1743 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1745 tevent_req_done(req);
1746 return NT_STATUS_OK;
1749 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1750 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1751 DEBUG(10, ("Got non-SMB PDU\n"));
1752 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1756 * If we supported multiple encrytion contexts
1757 * here we'd look up based on tid.
1759 if (common_encryption_on(conn->smb1.trans_enc)
1760 && (CVAL(inbuf, 0) == 0)) {
1761 uint16_t enc_ctx_num;
1763 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1764 if (!NT_STATUS_IS_OK(status)) {
1765 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1766 nt_errstr(status)));
1767 return status;
1770 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1771 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1772 enc_ctx_num,
1773 conn->smb1.trans_enc->enc_ctx_num));
1774 return NT_STATUS_INVALID_HANDLE;
1777 status = common_decrypt_buffer(conn->smb1.trans_enc,
1778 (char *)inbuf);
1779 if (!NT_STATUS_IS_OK(status)) {
1780 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1781 nt_errstr(status)));
1782 return status;
1786 mid = SVAL(inhdr, HDR_MID);
1787 num_pending = talloc_array_length(conn->pending);
1789 for (i=0; i<num_pending; i++) {
1790 if (mid == smb1cli_req_mid(conn->pending[i])) {
1791 break;
1794 if (i == num_pending) {
1795 /* Dump unexpected reply */
1796 return NT_STATUS_RETRY;
1799 oplock_break = false;
1801 if (mid == 0xffff) {
1803 * Paranoia checks that this is really an oplock break request.
1805 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1806 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1807 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1808 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1809 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1811 if (!oplock_break) {
1812 /* Dump unexpected reply */
1813 return NT_STATUS_RETRY;
1817 req = conn->pending[i];
1818 state = tevent_req_data(req, struct smbXcli_req_state);
1820 if (!oplock_break /* oplock breaks are not signed */
1821 && !smb_signing_check_pdu(conn->smb1.signing,
1822 inbuf, state->smb1.seqnum+1)) {
1823 DEBUG(10, ("cli_check_sign_mac failed\n"));
1824 return NT_STATUS_ACCESS_DENIED;
1827 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1828 &iov, &num_iov);
1829 if (!NT_STATUS_IS_OK(status)) {
1830 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1831 nt_errstr(status)));
1832 return status;
1835 cmd = CVAL(inhdr, HDR_COM);
1836 status = smb1cli_pull_raw_error(inhdr);
1838 if (state->smb1.chained_requests == NULL) {
1839 if (num_iov != 3) {
1840 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1843 smbXcli_req_unset_pending(req);
1845 if (state->smb1.recv_iov == NULL) {
1847 * For requests with more than
1848 * one response, we have to readd the
1849 * recv_iov array.
1851 state->smb1.recv_iov = talloc_zero_array(state,
1852 struct iovec,
1854 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1855 return NT_STATUS_OK;
1859 state->smb1.recv_cmd = cmd;
1860 state->smb1.recv_status = status;
1861 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1863 state->smb1.recv_iov[0] = iov[0];
1864 state->smb1.recv_iov[1] = iov[1];
1865 state->smb1.recv_iov[2] = iov[2];
1867 if (talloc_array_length(conn->pending) == 0) {
1868 tevent_req_done(req);
1869 return NT_STATUS_OK;
1872 tevent_req_defer_callback(req, state->ev);
1873 tevent_req_done(req);
1874 return NT_STATUS_RETRY;
1877 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1878 num_chained = talloc_array_length(chain);
1879 num_responses = (num_iov - 1)/2;
1881 if (num_responses > num_chained) {
1882 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1885 for (i=0; i<num_chained; i++) {
1886 size_t iov_idx = 1 + (i*2);
1887 struct iovec *cur = &iov[iov_idx];
1888 uint8_t *inbuf_ref;
1890 req = chain[i];
1891 state = tevent_req_data(req, struct smbXcli_req_state);
1893 smbXcli_req_unset_pending(req);
1896 * as we finish multiple requests here
1897 * we need to defer the callbacks as
1898 * they could destroy our current stack state.
1900 tevent_req_defer_callback(req, state->ev);
1902 if (i >= num_responses) {
1903 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1904 continue;
1907 if (state->smb1.recv_iov == NULL) {
1909 * For requests with more than
1910 * one response, we have to readd the
1911 * recv_iov array.
1913 state->smb1.recv_iov = talloc_zero_array(state,
1914 struct iovec,
1916 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1917 continue;
1921 state->smb1.recv_cmd = cmd;
1923 if (i == (num_responses - 1)) {
1925 * The last request in the chain gets the status
1927 state->smb1.recv_status = status;
1928 } else {
1929 cmd = CVAL(cur[0].iov_base, 0);
1930 state->smb1.recv_status = NT_STATUS_OK;
1933 state->inbuf = inbuf;
1936 * Note: here we use talloc_reference() in a way
1937 * that does not expose it to the caller.
1939 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1940 if (tevent_req_nomem(inbuf_ref, req)) {
1941 continue;
1944 /* copy the related buffers */
1945 state->smb1.recv_iov[0] = iov[0];
1946 state->smb1.recv_iov[1] = cur[0];
1947 state->smb1.recv_iov[2] = cur[1];
1949 tevent_req_done(req);
1952 return NT_STATUS_RETRY;
1955 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1956 TALLOC_CTX *mem_ctx,
1957 struct iovec **piov,
1958 uint8_t **phdr,
1959 uint8_t *pwct,
1960 uint16_t **pvwv,
1961 uint32_t *pvwv_offset,
1962 uint32_t *pnum_bytes,
1963 uint8_t **pbytes,
1964 uint32_t *pbytes_offset,
1965 uint8_t **pinbuf,
1966 const struct smb1cli_req_expected_response *expected,
1967 size_t num_expected)
1969 struct smbXcli_req_state *state =
1970 tevent_req_data(req,
1971 struct smbXcli_req_state);
1972 NTSTATUS status = NT_STATUS_OK;
1973 struct iovec *recv_iov = NULL;
1974 uint8_t *hdr = NULL;
1975 uint8_t wct = 0;
1976 uint32_t vwv_offset = 0;
1977 uint16_t *vwv = NULL;
1978 uint32_t num_bytes = 0;
1979 uint32_t bytes_offset = 0;
1980 uint8_t *bytes = NULL;
1981 size_t i;
1982 bool found_status = false;
1983 bool found_size = false;
1985 if (piov != NULL) {
1986 *piov = NULL;
1988 if (phdr != NULL) {
1989 *phdr = 0;
1991 if (pwct != NULL) {
1992 *pwct = 0;
1994 if (pvwv != NULL) {
1995 *pvwv = NULL;
1997 if (pvwv_offset != NULL) {
1998 *pvwv_offset = 0;
2000 if (pnum_bytes != NULL) {
2001 *pnum_bytes = 0;
2003 if (pbytes != NULL) {
2004 *pbytes = NULL;
2006 if (pbytes_offset != NULL) {
2007 *pbytes_offset = 0;
2009 if (pinbuf != NULL) {
2010 *pinbuf = NULL;
2013 if (state->inbuf != NULL) {
2014 recv_iov = state->smb1.recv_iov;
2015 state->smb1.recv_iov = NULL;
2016 if (state->smb1.recv_cmd != SMBreadBraw) {
2017 hdr = (uint8_t *)recv_iov[0].iov_base;
2018 wct = recv_iov[1].iov_len/2;
2019 vwv = (uint16_t *)recv_iov[1].iov_base;
2020 vwv_offset = PTR_DIFF(vwv, hdr);
2021 num_bytes = recv_iov[2].iov_len;
2022 bytes = (uint8_t *)recv_iov[2].iov_base;
2023 bytes_offset = PTR_DIFF(bytes, hdr);
2027 if (tevent_req_is_nterror(req, &status)) {
2028 for (i=0; i < num_expected; i++) {
2029 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2030 found_status = true;
2031 break;
2035 if (found_status) {
2036 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2039 return status;
2042 if (num_expected == 0) {
2043 found_status = true;
2044 found_size = true;
2047 status = state->smb1.recv_status;
2049 for (i=0; i < num_expected; i++) {
2050 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2051 continue;
2054 found_status = true;
2055 if (expected[i].wct == 0) {
2056 found_size = true;
2057 break;
2060 if (expected[i].wct == wct) {
2061 found_size = true;
2062 break;
2066 if (!found_status) {
2067 return status;
2070 if (!found_size) {
2071 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2074 if (piov != NULL) {
2075 *piov = talloc_move(mem_ctx, &recv_iov);
2078 if (phdr != NULL) {
2079 *phdr = hdr;
2081 if (pwct != NULL) {
2082 *pwct = wct;
2084 if (pvwv != NULL) {
2085 *pvwv = vwv;
2087 if (pvwv_offset != NULL) {
2088 *pvwv_offset = vwv_offset;
2090 if (pnum_bytes != NULL) {
2091 *pnum_bytes = num_bytes;
2093 if (pbytes != NULL) {
2094 *pbytes = bytes;
2096 if (pbytes_offset != NULL) {
2097 *pbytes_offset = bytes_offset;
2099 if (pinbuf != NULL) {
2100 *pinbuf = state->inbuf;
2103 return status;
2106 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2108 size_t wct_ofs;
2109 int i;
2111 wct_ofs = HDR_WCT;
2113 for (i=0; i<num_reqs; i++) {
2114 struct smbXcli_req_state *state;
2115 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2116 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2117 state->smb1.iov_count-2);
2118 wct_ofs = (wct_ofs + 3) & ~3;
2120 return wct_ofs;
2123 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2125 struct smbXcli_req_state *first_state =
2126 tevent_req_data(reqs[0],
2127 struct smbXcli_req_state);
2128 struct smbXcli_req_state *state;
2129 size_t wct_offset;
2130 size_t chain_padding = 0;
2131 int i, iovlen;
2132 struct iovec *iov = NULL;
2133 struct iovec *this_iov;
2134 NTSTATUS status;
2135 size_t nbt_len;
2137 if (num_reqs == 1) {
2138 return smb1cli_req_writev_submit(reqs[0], first_state,
2139 first_state->smb1.iov,
2140 first_state->smb1.iov_count);
2143 iovlen = 0;
2144 for (i=0; i<num_reqs; i++) {
2145 if (!tevent_req_is_in_progress(reqs[i])) {
2146 return NT_STATUS_INTERNAL_ERROR;
2149 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2151 if (state->smb1.iov_count < 4) {
2152 return NT_STATUS_INVALID_PARAMETER_MIX;
2155 if (i == 0) {
2157 * The NBT and SMB header
2159 iovlen += 2;
2160 } else {
2162 * Chain padding
2164 iovlen += 1;
2168 * words and bytes
2170 iovlen += state->smb1.iov_count - 2;
2173 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2174 if (iov == NULL) {
2175 return NT_STATUS_NO_MEMORY;
2178 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2179 first_state, reqs, sizeof(*reqs) * num_reqs);
2180 if (first_state->smb1.chained_requests == NULL) {
2181 TALLOC_FREE(iov);
2182 return NT_STATUS_NO_MEMORY;
2185 wct_offset = HDR_WCT;
2186 this_iov = iov;
2188 for (i=0; i<num_reqs; i++) {
2189 size_t next_padding = 0;
2190 uint16_t *vwv;
2192 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2194 if (i < num_reqs-1) {
2195 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2196 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2197 TALLOC_FREE(iov);
2198 TALLOC_FREE(first_state->smb1.chained_requests);
2199 return NT_STATUS_INVALID_PARAMETER_MIX;
2203 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2204 state->smb1.iov_count-2) + 1;
2205 if ((wct_offset % 4) != 0) {
2206 next_padding = 4 - (wct_offset % 4);
2208 wct_offset += next_padding;
2209 vwv = state->smb1.vwv;
2211 if (i < num_reqs-1) {
2212 struct smbXcli_req_state *next_state =
2213 tevent_req_data(reqs[i+1],
2214 struct smbXcli_req_state);
2215 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2216 SCVAL(vwv+0, 1, 0);
2217 SSVAL(vwv+1, 0, wct_offset);
2218 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2219 /* properly end the chain */
2220 SCVAL(vwv+0, 0, 0xff);
2221 SCVAL(vwv+0, 1, 0xff);
2222 SSVAL(vwv+1, 0, 0);
2225 if (i == 0) {
2227 * The NBT and SMB header
2229 this_iov[0] = state->smb1.iov[0];
2230 this_iov[1] = state->smb1.iov[1];
2231 this_iov += 2;
2232 } else {
2234 * This one is a bit subtle. We have to add
2235 * chain_padding bytes between the requests, and we
2236 * have to also include the wct field of the
2237 * subsequent requests. We use the subsequent header
2238 * for the padding, it contains the wct field in its
2239 * last byte.
2241 this_iov[0].iov_len = chain_padding+1;
2242 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2243 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2244 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2245 this_iov += 1;
2249 * copy the words and bytes
2251 memcpy(this_iov, state->smb1.iov+2,
2252 sizeof(struct iovec) * (state->smb1.iov_count-2));
2253 this_iov += state->smb1.iov_count - 2;
2254 chain_padding = next_padding;
2257 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2258 if (nbt_len > first_state->conn->smb1.max_xmit) {
2259 TALLOC_FREE(iov);
2260 TALLOC_FREE(first_state->smb1.chained_requests);
2261 return NT_STATUS_INVALID_PARAMETER_MIX;
2264 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2265 if (!NT_STATUS_IS_OK(status)) {
2266 TALLOC_FREE(iov);
2267 TALLOC_FREE(first_state->smb1.chained_requests);
2268 return status;
2271 return NT_STATUS_OK;
2274 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2276 return ((tevent_queue_length(conn->outgoing) != 0)
2277 || (talloc_array_length(conn->pending) != 0));
2280 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2282 return conn->smb2.server.capabilities;
2285 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2287 return conn->smb2.server.security_mode;
2290 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2292 return conn->smb2.server.max_trans_size;
2295 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2297 return conn->smb2.server.max_read_size;
2300 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2302 return conn->smb2.server.max_write_size;
2305 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2306 uint16_t max_credits)
2308 conn->smb2.max_credits = max_credits;
2311 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2313 static bool smb2cli_req_cancel(struct tevent_req *req)
2315 struct smbXcli_req_state *state =
2316 tevent_req_data(req,
2317 struct smbXcli_req_state);
2318 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2319 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2320 uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2321 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2322 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2323 struct smbXcli_session *session = state->session;
2324 uint8_t *fixed = state->smb2.pad;
2325 uint16_t fixed_len = 4;
2326 struct tevent_req *subreq;
2327 struct smbXcli_req_state *substate;
2328 NTSTATUS status;
2330 SSVAL(fixed, 0, 0x04);
2331 SSVAL(fixed, 2, 0);
2333 subreq = smb2cli_req_create(state, state->ev,
2334 state->conn,
2335 SMB2_OP_CANCEL,
2336 flags, 0,
2337 0, /* timeout */
2338 pid, tid, session,
2339 fixed, fixed_len,
2340 NULL, 0);
2341 if (subreq == NULL) {
2342 return false;
2344 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2346 if (flags & SMB2_HDR_FLAG_ASYNC) {
2347 mid = 0;
2350 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2351 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2352 SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2353 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2354 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2356 status = smb2cli_req_compound_submit(&subreq, 1);
2357 if (!NT_STATUS_IS_OK(status)) {
2358 TALLOC_FREE(subreq);
2359 return false;
2362 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2364 return true;
2367 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2369 /* we do not care about the result */
2370 TALLOC_FREE(subreq);
2373 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2374 struct tevent_context *ev,
2375 struct smbXcli_conn *conn,
2376 uint16_t cmd,
2377 uint32_t additional_flags,
2378 uint32_t clear_flags,
2379 uint32_t timeout_msec,
2380 uint32_t pid,
2381 uint32_t tid,
2382 struct smbXcli_session *session,
2383 const uint8_t *fixed,
2384 uint16_t fixed_len,
2385 const uint8_t *dyn,
2386 uint32_t dyn_len)
2388 struct tevent_req *req;
2389 struct smbXcli_req_state *state;
2390 uint32_t flags = 0;
2391 uint64_t uid = 0;
2393 req = tevent_req_create(mem_ctx, &state,
2394 struct smbXcli_req_state);
2395 if (req == NULL) {
2396 return NULL;
2399 state->ev = ev;
2400 state->conn = conn;
2401 state->session = session;
2403 if (session) {
2404 uid = session->smb2.session_id;
2407 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2408 if (state->smb2.recv_iov == NULL) {
2409 TALLOC_FREE(req);
2410 return NULL;
2413 flags |= additional_flags;
2414 flags &= ~clear_flags;
2416 state->smb2.fixed = fixed;
2417 state->smb2.fixed_len = fixed_len;
2418 state->smb2.dyn = dyn;
2419 state->smb2.dyn_len = dyn_len;
2421 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2422 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2423 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2424 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2425 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2426 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2427 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2429 switch (cmd) {
2430 case SMB2_OP_CANCEL:
2431 state->one_way = true;
2432 break;
2433 case SMB2_OP_BREAK:
2435 * If this is a dummy request, it will have
2436 * UINT64_MAX as message id.
2437 * If we send on break acknowledgement,
2438 * this gets overwritten later.
2440 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2441 break;
2444 if (timeout_msec > 0) {
2445 struct timeval endtime;
2447 endtime = timeval_current_ofs_msec(timeout_msec);
2448 if (!tevent_req_set_endtime(req, ev, endtime)) {
2449 return req;
2453 return req;
2456 void smb2cli_req_set_notify_async(struct tevent_req *req)
2458 struct smbXcli_req_state *state =
2459 tevent_req_data(req,
2460 struct smbXcli_req_state);
2462 state->smb2.notify_async = true;
2465 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2466 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2467 TALLOC_CTX *tmp_mem,
2468 uint8_t *inbuf);
2470 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2471 int num_reqs)
2473 struct smbXcli_req_state *state;
2474 struct tevent_req *subreq;
2475 struct iovec *iov;
2476 int i, num_iov, nbt_len;
2479 * 1 for the nbt length
2480 * per request: HDR, fixed, dyn, padding
2481 * -1 because the last one does not need padding
2484 iov = talloc_array(reqs[0], struct iovec, 1 + 4*num_reqs - 1);
2485 if (iov == NULL) {
2486 return NT_STATUS_NO_MEMORY;
2489 num_iov = 1;
2490 nbt_len = 0;
2492 for (i=0; i<num_reqs; i++) {
2493 int hdr_iov;
2494 size_t reqlen;
2495 bool ret;
2496 uint16_t opcode;
2497 uint64_t avail;
2498 uint16_t charge;
2499 uint16_t credits;
2500 uint64_t mid;
2501 const DATA_BLOB *signing_key = NULL;
2503 if (!tevent_req_is_in_progress(reqs[i])) {
2504 return NT_STATUS_INTERNAL_ERROR;
2507 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2509 if (!smbXcli_conn_is_connected(state->conn)) {
2510 return NT_STATUS_CONNECTION_DISCONNECTED;
2513 if ((state->conn->protocol != PROTOCOL_NONE) &&
2514 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2515 return NT_STATUS_REVISION_MISMATCH;
2518 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2519 if (opcode == SMB2_OP_CANCEL) {
2520 goto skip_credits;
2523 avail = UINT64_MAX - state->conn->smb2.mid;
2524 if (avail < 1) {
2525 return NT_STATUS_CONNECTION_ABORTED;
2528 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2529 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2530 } else {
2531 charge = 1;
2534 charge = MAX(state->smb2.credit_charge, charge);
2536 avail = MIN(avail, state->conn->smb2.cur_credits);
2537 if (avail < charge) {
2538 return NT_STATUS_INTERNAL_ERROR;
2541 credits = 0;
2542 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2543 credits = state->conn->smb2.max_credits -
2544 state->conn->smb2.cur_credits;
2546 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2547 credits += 1;
2550 mid = state->conn->smb2.mid;
2551 state->conn->smb2.mid += charge;
2552 state->conn->smb2.cur_credits -= charge;
2554 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2555 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2557 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2558 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2560 skip_credits:
2561 hdr_iov = num_iov;
2562 iov[num_iov].iov_base = state->smb2.hdr;
2563 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2564 num_iov += 1;
2566 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2567 iov[num_iov].iov_len = state->smb2.fixed_len;
2568 num_iov += 1;
2570 if (state->smb2.dyn != NULL) {
2571 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2572 iov[num_iov].iov_len = state->smb2.dyn_len;
2573 num_iov += 1;
2576 reqlen = sizeof(state->smb2.hdr);
2577 reqlen += state->smb2.fixed_len;
2578 reqlen += state->smb2.dyn_len;
2580 if (i < num_reqs-1) {
2581 if ((reqlen % 8) > 0) {
2582 uint8_t pad = 8 - (reqlen % 8);
2583 iov[num_iov].iov_base = state->smb2.pad;
2584 iov[num_iov].iov_len = pad;
2585 num_iov += 1;
2586 reqlen += pad;
2588 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2590 nbt_len += reqlen;
2592 if (state->session) {
2593 bool should_sign = state->session->smb2.should_sign;
2595 if (opcode == SMB2_OP_SESSSETUP &&
2596 state->session->smb2.signing_key.length != 0) {
2597 should_sign = true;
2601 * We prefer the channel signing key if it is
2602 * already there.
2604 if (should_sign) {
2605 signing_key = &state->session->smb2.channel_signing_key;
2609 * If it is a channel binding, we already have the main
2610 * signing key and try that one.
2612 if (signing_key && signing_key->length == 0) {
2613 signing_key = &state->session->smb2.signing_key;
2617 * If we do not have any session key yet, we skip the
2618 * signing of SMB2_OP_SESSSETUP requests.
2620 if (signing_key && signing_key->length == 0) {
2621 signing_key = NULL;
2625 if (signing_key) {
2626 NTSTATUS status;
2628 status = smb2_signing_sign_pdu(*signing_key,
2629 state->session->conn->protocol,
2630 &iov[hdr_iov], num_iov - hdr_iov);
2631 if (!NT_STATUS_IS_OK(status)) {
2632 return status;
2636 ret = smbXcli_req_set_pending(reqs[i]);
2637 if (!ret) {
2638 return NT_STATUS_NO_MEMORY;
2642 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2643 _smb_setlen_tcp(state->length_hdr, nbt_len);
2644 iov[0].iov_base = state->length_hdr;
2645 iov[0].iov_len = sizeof(state->length_hdr);
2647 if (state->conn->dispatch_incoming == NULL) {
2648 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2651 subreq = writev_send(state, state->ev, state->conn->outgoing,
2652 state->conn->write_fd, false, iov, num_iov);
2653 if (subreq == NULL) {
2654 return NT_STATUS_NO_MEMORY;
2656 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2657 return NT_STATUS_OK;
2660 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2662 struct smbXcli_req_state *state =
2663 tevent_req_data(req,
2664 struct smbXcli_req_state);
2666 state->smb2.credit_charge = charge;
2669 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2670 struct tevent_context *ev,
2671 struct smbXcli_conn *conn,
2672 uint16_t cmd,
2673 uint32_t additional_flags,
2674 uint32_t clear_flags,
2675 uint32_t timeout_msec,
2676 uint32_t pid,
2677 uint32_t tid,
2678 struct smbXcli_session *session,
2679 const uint8_t *fixed,
2680 uint16_t fixed_len,
2681 const uint8_t *dyn,
2682 uint32_t dyn_len)
2684 struct tevent_req *req;
2685 NTSTATUS status;
2687 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2688 additional_flags, clear_flags,
2689 timeout_msec,
2690 pid, tid, session,
2691 fixed, fixed_len, dyn, dyn_len);
2692 if (req == NULL) {
2693 return NULL;
2695 if (!tevent_req_is_in_progress(req)) {
2696 return tevent_req_post(req, ev);
2698 status = smb2cli_req_compound_submit(&req, 1);
2699 if (tevent_req_nterror(req, status)) {
2700 return tevent_req_post(req, ev);
2702 return req;
2705 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2707 struct tevent_req *req =
2708 tevent_req_callback_data(subreq,
2709 struct tevent_req);
2710 struct smbXcli_req_state *state =
2711 tevent_req_data(req,
2712 struct smbXcli_req_state);
2713 ssize_t nwritten;
2714 int err;
2716 nwritten = writev_recv(subreq, &err);
2717 TALLOC_FREE(subreq);
2718 if (nwritten == -1) {
2719 /* here, we need to notify all pending requests */
2720 NTSTATUS status = map_nt_error_from_unix_common(err);
2721 smbXcli_conn_disconnect(state->conn, status);
2722 return;
2726 static NTSTATUS smb2cli_inbuf_parse_compound(uint8_t *buf, TALLOC_CTX *mem_ctx,
2727 struct iovec **piov, int *pnum_iov)
2729 struct iovec *iov;
2730 int num_iov;
2731 size_t buflen;
2732 size_t taken;
2733 uint8_t *first_hdr;
2735 num_iov = 0;
2737 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2738 if (iov == NULL) {
2739 return NT_STATUS_NO_MEMORY;
2742 buflen = smb_len_tcp(buf);
2743 taken = 0;
2744 first_hdr = buf + NBT_HDR_SIZE;
2746 while (taken < buflen) {
2747 size_t len = buflen - taken;
2748 uint8_t *hdr = first_hdr + taken;
2749 struct iovec *cur;
2750 size_t full_size;
2751 size_t next_command_ofs;
2752 uint16_t body_size;
2753 struct iovec *iov_tmp;
2756 * We need the header plus the body length field
2759 if (len < SMB2_HDR_BODY + 2) {
2760 DEBUG(10, ("%d bytes left, expected at least %d\n",
2761 (int)len, SMB2_HDR_BODY));
2762 goto inval;
2764 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2765 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2766 IVAL(hdr, 0)));
2767 goto inval;
2769 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2770 DEBUG(10, ("Got HDR len %d, expected %d\n",
2771 SVAL(hdr, 4), SMB2_HDR_BODY));
2772 goto inval;
2775 full_size = len;
2776 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2777 body_size = SVAL(hdr, SMB2_HDR_BODY);
2779 if (next_command_ofs != 0) {
2780 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2781 goto inval;
2783 if (next_command_ofs > full_size) {
2784 goto inval;
2786 full_size = next_command_ofs;
2788 if (body_size < 2) {
2789 goto inval;
2791 body_size &= 0xfffe;
2793 if (body_size > (full_size - SMB2_HDR_BODY)) {
2794 goto inval;
2797 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2798 num_iov + 3);
2799 if (iov_tmp == NULL) {
2800 TALLOC_FREE(iov);
2801 return NT_STATUS_NO_MEMORY;
2803 iov = iov_tmp;
2804 cur = &iov[num_iov];
2805 num_iov += 3;
2807 cur[0].iov_base = hdr;
2808 cur[0].iov_len = SMB2_HDR_BODY;
2809 cur[1].iov_base = hdr + SMB2_HDR_BODY;
2810 cur[1].iov_len = body_size;
2811 cur[2].iov_base = hdr + SMB2_HDR_BODY + body_size;
2812 cur[2].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2814 taken += full_size;
2817 *piov = iov;
2818 *pnum_iov = num_iov;
2819 return NT_STATUS_OK;
2821 inval:
2822 TALLOC_FREE(iov);
2823 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2826 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2827 uint64_t mid)
2829 size_t num_pending = talloc_array_length(conn->pending);
2830 size_t i;
2832 for (i=0; i<num_pending; i++) {
2833 struct tevent_req *req = conn->pending[i];
2834 struct smbXcli_req_state *state =
2835 tevent_req_data(req,
2836 struct smbXcli_req_state);
2838 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2839 return req;
2842 return NULL;
2845 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2846 TALLOC_CTX *tmp_mem,
2847 uint8_t *inbuf)
2849 struct tevent_req *req;
2850 struct smbXcli_req_state *state = NULL;
2851 struct iovec *iov;
2852 int i, num_iov;
2853 NTSTATUS status;
2854 bool defer = true;
2855 struct smbXcli_session *last_session = NULL;
2857 status = smb2cli_inbuf_parse_compound(inbuf, tmp_mem,
2858 &iov, &num_iov);
2859 if (!NT_STATUS_IS_OK(status)) {
2860 return status;
2863 for (i=0; i<num_iov; i+=3) {
2864 uint8_t *inbuf_ref = NULL;
2865 struct iovec *cur = &iov[i];
2866 uint8_t *inhdr = (uint8_t *)cur[0].iov_base;
2867 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
2868 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
2869 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
2870 uint16_t req_opcode;
2871 uint32_t req_flags;
2872 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
2873 uint32_t new_credits;
2874 struct smbXcli_session *session = NULL;
2875 const DATA_BLOB *signing_key = NULL;
2876 bool should_sign = false;
2878 new_credits = conn->smb2.cur_credits;
2879 new_credits += credits;
2880 if (new_credits > UINT16_MAX) {
2881 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2883 conn->smb2.cur_credits += credits;
2885 req = smb2cli_conn_find_pending(conn, mid);
2886 if (req == NULL) {
2887 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2889 state = tevent_req_data(req, struct smbXcli_req_state);
2891 state->smb2.got_async = false;
2893 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2894 if (opcode != req_opcode) {
2895 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2897 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2899 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
2900 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2903 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
2904 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
2905 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
2906 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
2909 * async interim responses are not signed,
2910 * even if the SMB2_HDR_FLAG_SIGNED flag
2911 * is set.
2913 req_flags |= SMB2_HDR_FLAG_ASYNC;
2914 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
2915 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
2917 if (state->smb2.notify_async) {
2918 state->smb2.got_async = true;
2919 tevent_req_defer_callback(req, state->ev);
2920 tevent_req_notify_callback(req);
2922 continue;
2925 session = state->session;
2926 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
2927 session = last_session;
2929 last_session = session;
2931 if (session) {
2932 should_sign = session->smb2.should_sign;
2933 if (opcode == SMB2_OP_SESSSETUP &&
2934 session->smb2.signing_key.length != 0) {
2935 should_sign = true;
2939 if (should_sign) {
2940 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
2941 return NT_STATUS_ACCESS_DENIED;
2945 if (flags & SMB2_HDR_FLAG_SIGNED) {
2946 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
2948 if (session == NULL) {
2949 struct smbXcli_session *s;
2951 s = state->conn->sessions;
2952 for (; s; s = s->next) {
2953 if (s->smb2.session_id != uid) {
2954 continue;
2957 session = s;
2958 break;
2962 if (session == NULL) {
2963 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2966 last_session = session;
2967 signing_key = &session->smb2.channel_signing_key;
2970 if (opcode == SMB2_OP_SESSSETUP) {
2972 * We prefer the channel signing key, if it is
2973 * already there.
2975 * If we do not have a channel signing key yet,
2976 * we try the main signing key, if it is not
2977 * the final response.
2979 if (signing_key && signing_key->length == 0 &&
2980 !NT_STATUS_IS_OK(status)) {
2981 signing_key = &session->smb2.signing_key;
2984 if (signing_key && signing_key->length == 0) {
2986 * If we do not have a session key to
2987 * verify the signature, we defer the
2988 * signing check to the caller.
2990 * The caller gets NT_STATUS_OK, it
2991 * has to call
2992 * smb2cli_session_set_session_key()
2993 * or
2994 * smb2cli_session_set_channel_key()
2995 * which will check the signature
2996 * with the channel signing key.
2998 signing_key = NULL;
3002 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3004 * if the server returns NT_STATUS_USER_SESSION_DELETED
3005 * the response is not signed and we should
3006 * propagate the NT_STATUS_USER_SESSION_DELETED
3007 * status to the caller.
3009 signing_key = NULL;
3012 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3013 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3014 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3016 * if the server returns
3017 * NT_STATUS_NETWORK_NAME_DELETED
3018 * NT_STATUS_FILE_CLOSED
3019 * NT_STATUS_INVALID_PARAMETER
3020 * the response might not be signed
3021 * as this happens before the signing checks.
3023 * If server echos the signature (or all zeros)
3024 * we should report the status from the server
3025 * to the caller.
3027 if (signing_key) {
3028 int cmp;
3030 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3031 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3032 16);
3033 if (cmp == 0) {
3034 state->smb2.signing_skipped = true;
3035 signing_key = NULL;
3038 if (signing_key) {
3039 int cmp;
3040 static const uint8_t zeros[16];
3042 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3043 zeros,
3044 16);
3045 if (cmp == 0) {
3046 state->smb2.signing_skipped = true;
3047 signing_key = NULL;
3052 if (signing_key) {
3053 status = smb2_signing_check_pdu(*signing_key,
3054 state->conn->protocol,
3055 cur, 3);
3056 if (!NT_STATUS_IS_OK(status)) {
3058 * If the signing check fails, we disconnect
3059 * the connection.
3061 return status;
3065 smbXcli_req_unset_pending(req);
3068 * There might be more than one response
3069 * we need to defer the notifications
3071 if ((num_iov == 4) && (talloc_array_length(conn->pending) == 0)) {
3072 defer = false;
3075 if (defer) {
3076 tevent_req_defer_callback(req, state->ev);
3080 * Note: here we use talloc_reference() in a way
3081 * that does not expose it to the caller.
3083 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3084 if (tevent_req_nomem(inbuf_ref, req)) {
3085 continue;
3088 /* copy the related buffers */
3089 state->smb2.recv_iov[0] = cur[0];
3090 state->smb2.recv_iov[1] = cur[1];
3091 state->smb2.recv_iov[2] = cur[2];
3093 tevent_req_done(req);
3096 if (defer) {
3097 return NT_STATUS_RETRY;
3100 return NT_STATUS_OK;
3103 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3104 struct iovec **piov,
3105 const struct smb2cli_req_expected_response *expected,
3106 size_t num_expected)
3108 struct smbXcli_req_state *state =
3109 tevent_req_data(req,
3110 struct smbXcli_req_state);
3111 NTSTATUS status;
3112 size_t body_size;
3113 bool found_status = false;
3114 bool found_size = false;
3115 size_t i;
3117 if (piov != NULL) {
3118 *piov = NULL;
3121 if (state->smb2.got_async) {
3122 return STATUS_PENDING;
3125 if (tevent_req_is_nterror(req, &status)) {
3126 for (i=0; i < num_expected; i++) {
3127 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3128 found_status = true;
3129 break;
3133 if (found_status) {
3134 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3137 return status;
3140 if (num_expected == 0) {
3141 found_status = true;
3142 found_size = true;
3145 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3146 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3148 for (i=0; i < num_expected; i++) {
3149 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3150 continue;
3153 found_status = true;
3154 if (expected[i].body_size == 0) {
3155 found_size = true;
3156 break;
3159 if (expected[i].body_size == body_size) {
3160 found_size = true;
3161 break;
3165 if (!found_status) {
3166 return status;
3169 if (state->smb2.signing_skipped) {
3170 if (num_expected > 0) {
3171 return NT_STATUS_ACCESS_DENIED;
3173 if (!NT_STATUS_IS_ERR(status)) {
3174 return NT_STATUS_ACCESS_DENIED;
3178 if (!found_size) {
3179 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3182 if (piov != NULL) {
3183 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3186 return status;
3189 static const struct {
3190 enum protocol_types proto;
3191 const char *smb1_name;
3192 } smb1cli_prots[] = {
3193 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3194 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3195 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3196 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3197 {PROTOCOL_LANMAN2, "LM1.2X002"},
3198 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3199 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3200 {PROTOCOL_LANMAN2, "Samba"},
3201 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3202 {PROTOCOL_NT1, "NT LM 0.12"},
3203 {PROTOCOL_SMB2_02, "SMB 2.002"},
3204 {PROTOCOL_SMB2_10, "SMB 2.???"},
3207 static const struct {
3208 enum protocol_types proto;
3209 uint16_t smb2_dialect;
3210 } smb2cli_prots[] = {
3211 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3212 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3213 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3214 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3215 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3218 struct smbXcli_negprot_state {
3219 struct smbXcli_conn *conn;
3220 struct tevent_context *ev;
3221 uint32_t timeout_msec;
3222 enum protocol_types min_protocol;
3223 enum protocol_types max_protocol;
3225 struct {
3226 uint8_t fixed[36];
3227 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3228 } smb2;
3231 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3232 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3233 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3234 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3235 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3236 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3237 TALLOC_CTX *frame,
3238 uint8_t *inbuf);
3240 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3241 struct tevent_context *ev,
3242 struct smbXcli_conn *conn,
3243 uint32_t timeout_msec,
3244 enum protocol_types min_protocol,
3245 enum protocol_types max_protocol)
3247 struct tevent_req *req, *subreq;
3248 struct smbXcli_negprot_state *state;
3250 req = tevent_req_create(mem_ctx, &state,
3251 struct smbXcli_negprot_state);
3252 if (req == NULL) {
3253 return NULL;
3255 state->conn = conn;
3256 state->ev = ev;
3257 state->timeout_msec = timeout_msec;
3258 state->min_protocol = min_protocol;
3259 state->max_protocol = max_protocol;
3261 if (min_protocol == PROTOCOL_NONE) {
3262 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3263 return tevent_req_post(req, ev);
3266 if (max_protocol == PROTOCOL_NONE) {
3267 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3268 return tevent_req_post(req, ev);
3271 if (min_protocol > max_protocol) {
3272 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3273 return tevent_req_post(req, ev);
3276 if ((min_protocol < PROTOCOL_SMB2_02) &&
3277 (max_protocol < PROTOCOL_SMB2_02)) {
3279 * SMB1 only...
3281 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3283 subreq = smbXcli_negprot_smb1_subreq(state);
3284 if (tevent_req_nomem(subreq, req)) {
3285 return tevent_req_post(req, ev);
3287 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3288 return req;
3291 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3292 (max_protocol >= PROTOCOL_SMB2_02)) {
3294 * SMB2 only...
3296 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3298 subreq = smbXcli_negprot_smb2_subreq(state);
3299 if (tevent_req_nomem(subreq, req)) {
3300 return tevent_req_post(req, ev);
3302 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3303 return req;
3307 * We send an SMB1 negprot with the SMB2 dialects
3308 * and expect a SMB1 or a SMB2 response.
3310 * smbXcli_negprot_dispatch_incoming() will fix the
3311 * callback to match protocol of the response.
3313 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3315 subreq = smbXcli_negprot_smb1_subreq(state);
3316 if (tevent_req_nomem(subreq, req)) {
3317 return tevent_req_post(req, ev);
3319 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3320 return req;
3323 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3325 struct tevent_req *req =
3326 tevent_req_callback_data(subreq,
3327 struct tevent_req);
3328 NTSTATUS status;
3331 * we just want the low level error
3333 status = tevent_req_simple_recv_ntstatus(subreq);
3334 TALLOC_FREE(subreq);
3335 if (tevent_req_nterror(req, status)) {
3336 return;
3339 /* this should never happen */
3340 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3343 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3345 size_t i;
3346 DATA_BLOB bytes = data_blob_null;
3347 uint8_t flags;
3348 uint16_t flags2;
3350 /* setup the protocol strings */
3351 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3352 uint8_t c = 2;
3353 bool ok;
3355 if (smb1cli_prots[i].proto < state->min_protocol) {
3356 continue;
3359 if (smb1cli_prots[i].proto > state->max_protocol) {
3360 continue;
3363 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3364 if (!ok) {
3365 return NULL;
3369 * We now it is already ascii and
3370 * we want NULL termination.
3372 ok = data_blob_append(state, &bytes,
3373 smb1cli_prots[i].smb1_name,
3374 strlen(smb1cli_prots[i].smb1_name)+1);
3375 if (!ok) {
3376 return NULL;
3380 smb1cli_req_flags(state->max_protocol,
3381 state->conn->smb1.client.capabilities,
3382 SMBnegprot,
3383 0, 0, &flags,
3384 0, 0, &flags2);
3386 return smb1cli_req_send(state, state->ev, state->conn,
3387 SMBnegprot,
3388 flags, ~flags,
3389 flags2, ~flags2,
3390 state->timeout_msec,
3391 0xFFFE, 0, 0, /* pid, tid, uid */
3392 0, NULL, /* wct, vwv */
3393 bytes.length, bytes.data);
3396 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3398 struct tevent_req *req =
3399 tevent_req_callback_data(subreq,
3400 struct tevent_req);
3401 struct smbXcli_negprot_state *state =
3402 tevent_req_data(req,
3403 struct smbXcli_negprot_state);
3404 struct smbXcli_conn *conn = state->conn;
3405 struct iovec *recv_iov = NULL;
3406 uint8_t *inhdr;
3407 uint8_t wct;
3408 uint16_t *vwv;
3409 uint32_t num_bytes;
3410 uint8_t *bytes;
3411 NTSTATUS status;
3412 uint16_t protnum;
3413 size_t i;
3414 size_t num_prots = 0;
3415 uint8_t flags;
3416 uint32_t client_capabilities = conn->smb1.client.capabilities;
3417 uint32_t both_capabilities;
3418 uint32_t server_capabilities = 0;
3419 uint32_t capabilities;
3420 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3421 uint32_t server_max_xmit = 0;
3422 uint32_t max_xmit;
3423 uint32_t server_max_mux = 0;
3424 uint16_t server_security_mode = 0;
3425 uint32_t server_session_key = 0;
3426 bool server_readbraw = false;
3427 bool server_writebraw = false;
3428 bool server_lockread = false;
3429 bool server_writeunlock = false;
3430 struct GUID server_guid = GUID_zero();
3431 DATA_BLOB server_gss_blob = data_blob_null;
3432 uint8_t server_challenge[8];
3433 char *server_workgroup = NULL;
3434 char *server_name = NULL;
3435 int server_time_zone = 0;
3436 NTTIME server_system_time = 0;
3437 static const struct smb1cli_req_expected_response expected[] = {
3439 .status = NT_STATUS_OK,
3440 .wct = 0x11, /* NT1 */
3443 .status = NT_STATUS_OK,
3444 .wct = 0x0D, /* LM */
3447 .status = NT_STATUS_OK,
3448 .wct = 0x01, /* CORE */
3452 ZERO_STRUCT(server_challenge);
3454 status = smb1cli_req_recv(subreq, state,
3455 &recv_iov,
3456 &inhdr,
3457 &wct,
3458 &vwv,
3459 NULL, /* pvwv_offset */
3460 &num_bytes,
3461 &bytes,
3462 NULL, /* pbytes_offset */
3463 NULL, /* pinbuf */
3464 expected, ARRAY_SIZE(expected));
3465 TALLOC_FREE(subreq);
3466 if (tevent_req_nterror(req, status)) {
3467 return;
3470 flags = CVAL(inhdr, HDR_FLG);
3472 protnum = SVAL(vwv, 0);
3474 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3475 if (smb1cli_prots[i].proto < state->min_protocol) {
3476 continue;
3479 if (smb1cli_prots[i].proto > state->max_protocol) {
3480 continue;
3483 if (protnum != num_prots) {
3484 num_prots++;
3485 continue;
3488 conn->protocol = smb1cli_prots[i].proto;
3489 break;
3492 if (conn->protocol == PROTOCOL_NONE) {
3493 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3494 return;
3497 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3498 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3499 "and the selected protocol level doesn't support it.\n"));
3500 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3501 return;
3504 if (flags & FLAG_SUPPORT_LOCKREAD) {
3505 server_lockread = true;
3506 server_writeunlock = true;
3509 if (conn->protocol >= PROTOCOL_NT1) {
3510 const char *client_signing = NULL;
3511 bool server_mandatory = false;
3512 bool server_allowed = false;
3513 const char *server_signing = NULL;
3514 bool ok;
3515 uint8_t key_len;
3517 if (wct != 0x11) {
3518 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3519 return;
3522 /* NT protocol */
3523 server_security_mode = CVAL(vwv + 1, 0);
3524 server_max_mux = SVAL(vwv + 1, 1);
3525 server_max_xmit = IVAL(vwv + 3, 1);
3526 server_session_key = IVAL(vwv + 7, 1);
3527 server_time_zone = SVALS(vwv + 15, 1);
3528 server_time_zone *= 60;
3529 /* this time arrives in real GMT */
3530 server_system_time = BVAL(vwv + 11, 1);
3531 server_capabilities = IVAL(vwv + 9, 1);
3533 key_len = CVAL(vwv + 16, 1);
3535 if (server_capabilities & CAP_RAW_MODE) {
3536 server_readbraw = true;
3537 server_writebraw = true;
3539 if (server_capabilities & CAP_LOCK_AND_READ) {
3540 server_lockread = true;
3543 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3544 DATA_BLOB blob1, blob2;
3546 if (num_bytes < 16) {
3547 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3548 return;
3551 blob1 = data_blob_const(bytes, 16);
3552 status = GUID_from_data_blob(&blob1, &server_guid);
3553 if (tevent_req_nterror(req, status)) {
3554 return;
3557 blob1 = data_blob_const(bytes+16, num_bytes-16);
3558 blob2 = data_blob_dup_talloc(state, blob1);
3559 if (blob1.length > 0 &&
3560 tevent_req_nomem(blob2.data, req)) {
3561 return;
3563 server_gss_blob = blob2;
3564 } else {
3565 DATA_BLOB blob1, blob2;
3567 if (num_bytes < key_len) {
3568 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3569 return;
3572 if (key_len != 0 && key_len != 8) {
3573 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3574 return;
3577 if (key_len == 8) {
3578 memcpy(server_challenge, bytes, 8);
3581 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3582 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3583 if (blob1.length > 0) {
3584 size_t len;
3586 len = utf16_len_n(blob1.data,
3587 blob1.length);
3588 blob1.length = len;
3590 ok = convert_string_talloc(state,
3591 CH_UTF16LE,
3592 CH_UNIX,
3593 blob1.data,
3594 blob1.length,
3595 &server_workgroup,
3596 &len);
3597 if (!ok) {
3598 status = map_nt_error_from_unix_common(errno);
3599 tevent_req_nterror(req, status);
3600 return;
3604 blob2.data += blob1.length;
3605 blob2.length -= blob1.length;
3606 if (blob2.length > 0) {
3607 size_t len;
3609 len = utf16_len_n(blob1.data,
3610 blob1.length);
3611 blob1.length = len;
3613 ok = convert_string_talloc(state,
3614 CH_UTF16LE,
3615 CH_UNIX,
3616 blob2.data,
3617 blob2.length,
3618 &server_name,
3619 &len);
3620 if (!ok) {
3621 status = map_nt_error_from_unix_common(errno);
3622 tevent_req_nterror(req, status);
3623 return;
3628 client_signing = "disabled";
3629 if (conn->allow_signing) {
3630 client_signing = "allowed";
3632 if (conn->mandatory_signing) {
3633 client_signing = "required";
3636 server_signing = "not supported";
3637 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3638 server_signing = "supported";
3639 server_allowed = true;
3641 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3642 server_signing = "required";
3643 server_mandatory = true;
3646 ok = smb_signing_set_negotiated(conn->smb1.signing,
3647 server_allowed,
3648 server_mandatory);
3649 if (!ok) {
3650 DEBUG(1,("cli_negprot: SMB signing is required, "
3651 "but client[%s] and server[%s] mismatch\n",
3652 client_signing, server_signing));
3653 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3654 return;
3657 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3658 DATA_BLOB blob1;
3659 uint8_t key_len;
3660 time_t t;
3662 if (wct != 0x0D) {
3663 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3664 return;
3667 server_security_mode = SVAL(vwv + 1, 0);
3668 server_max_xmit = SVAL(vwv + 2, 0);
3669 server_max_mux = SVAL(vwv + 3, 0);
3670 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3671 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3672 server_session_key = IVAL(vwv + 6, 0);
3673 server_time_zone = SVALS(vwv + 10, 0);
3674 server_time_zone *= 60;
3675 /* this time is converted to GMT by make_unix_date */
3676 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3677 unix_to_nt_time(&server_system_time, t);
3678 key_len = SVAL(vwv + 11, 0);
3680 if (num_bytes < key_len) {
3681 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3682 return;
3685 if (key_len != 0 && key_len != 8) {
3686 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3687 return;
3690 if (key_len == 8) {
3691 memcpy(server_challenge, bytes, 8);
3694 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3695 if (blob1.length > 0) {
3696 size_t len;
3697 bool ok;
3699 len = utf16_len_n(blob1.data,
3700 blob1.length);
3701 blob1.length = len;
3703 ok = convert_string_talloc(state,
3704 CH_DOS,
3705 CH_UNIX,
3706 blob1.data,
3707 blob1.length,
3708 &server_workgroup,
3709 &len);
3710 if (!ok) {
3711 status = map_nt_error_from_unix_common(errno);
3712 tevent_req_nterror(req, status);
3713 return;
3717 } else {
3718 /* the old core protocol */
3719 server_time_zone = get_time_zone(time(NULL));
3720 server_max_xmit = 1024;
3721 server_max_mux = 1;
3724 if (server_max_xmit < 1024) {
3725 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3726 return;
3729 if (server_max_mux < 1) {
3730 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3731 return;
3735 * Now calculate the negotiated capabilities
3736 * based on the mask for:
3737 * - client only flags
3738 * - flags used in both directions
3739 * - server only flags
3741 both_capabilities = client_capabilities & server_capabilities;
3742 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3743 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3744 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3746 max_xmit = MIN(client_max_xmit, server_max_xmit);
3748 conn->smb1.server.capabilities = server_capabilities;
3749 conn->smb1.capabilities = capabilities;
3751 conn->smb1.server.max_xmit = server_max_xmit;
3752 conn->smb1.max_xmit = max_xmit;
3754 conn->smb1.server.max_mux = server_max_mux;
3756 conn->smb1.server.security_mode = server_security_mode;
3758 conn->smb1.server.readbraw = server_readbraw;
3759 conn->smb1.server.writebraw = server_writebraw;
3760 conn->smb1.server.lockread = server_lockread;
3761 conn->smb1.server.writeunlock = server_writeunlock;
3763 conn->smb1.server.session_key = server_session_key;
3765 talloc_steal(conn, server_gss_blob.data);
3766 conn->smb1.server.gss_blob = server_gss_blob;
3767 conn->smb1.server.guid = server_guid;
3768 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3769 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3770 conn->smb1.server.name = talloc_move(conn, &server_name);
3772 conn->smb1.server.time_zone = server_time_zone;
3773 conn->smb1.server.system_time = server_system_time;
3775 tevent_req_done(req);
3778 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3780 size_t i;
3781 uint8_t *buf;
3782 uint16_t dialect_count = 0;
3784 buf = state->smb2.dyn;
3785 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3786 if (smb2cli_prots[i].proto < state->min_protocol) {
3787 continue;
3790 if (smb2cli_prots[i].proto > state->max_protocol) {
3791 continue;
3794 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3795 dialect_count++;
3798 buf = state->smb2.fixed;
3799 SSVAL(buf, 0, 36);
3800 SSVAL(buf, 2, dialect_count);
3801 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3802 SSVAL(buf, 6, 0); /* Reserved */
3803 if (state->max_protocol >= PROTOCOL_SMB2_22) {
3804 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
3805 } else {
3806 SIVAL(buf, 8, 0); /* Capabilities */
3808 if (state->max_protocol >= PROTOCOL_SMB2_10) {
3809 NTSTATUS status;
3810 DATA_BLOB blob;
3812 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3813 state, &blob);
3814 if (!NT_STATUS_IS_OK(status)) {
3815 return NULL;
3817 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3818 } else {
3819 memset(buf+12, 0, 16); /* ClientGuid */
3821 SBVAL(buf, 28, 0); /* ClientStartTime */
3823 return smb2cli_req_send(state, state->ev,
3824 state->conn, SMB2_OP_NEGPROT,
3825 0, 0, /* flags */
3826 state->timeout_msec,
3827 0xFEFF, 0, NULL, /* pid, tid, session */
3828 state->smb2.fixed, sizeof(state->smb2.fixed),
3829 state->smb2.dyn, dialect_count*2);
3832 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3834 struct tevent_req *req =
3835 tevent_req_callback_data(subreq,
3836 struct tevent_req);
3837 struct smbXcli_negprot_state *state =
3838 tevent_req_data(req,
3839 struct smbXcli_negprot_state);
3840 struct smbXcli_conn *conn = state->conn;
3841 size_t security_offset, security_length;
3842 DATA_BLOB blob;
3843 NTSTATUS status;
3844 struct iovec *iov;
3845 uint8_t *body;
3846 size_t i;
3847 uint16_t dialect_revision;
3848 static const struct smb2cli_req_expected_response expected[] = {
3850 .status = NT_STATUS_OK,
3851 .body_size = 0x41
3855 status = smb2cli_req_recv(subreq, state, &iov,
3856 expected, ARRAY_SIZE(expected));
3857 TALLOC_FREE(subreq);
3858 if (tevent_req_nterror(req, status)) {
3859 return;
3862 body = (uint8_t *)iov[1].iov_base;
3864 dialect_revision = SVAL(body, 4);
3866 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3867 if (smb2cli_prots[i].proto < state->min_protocol) {
3868 continue;
3871 if (smb2cli_prots[i].proto > state->max_protocol) {
3872 continue;
3875 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
3876 continue;
3879 conn->protocol = smb2cli_prots[i].proto;
3880 break;
3883 if (conn->protocol == PROTOCOL_NONE) {
3884 if (state->min_protocol >= PROTOCOL_SMB2_02) {
3885 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3886 return;
3889 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
3890 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3891 return;
3894 /* make sure we do not loop forever */
3895 state->min_protocol = PROTOCOL_SMB2_02;
3898 * send a SMB2 negprot, in order to negotiate
3899 * the SMB2 dialect.
3901 subreq = smbXcli_negprot_smb2_subreq(state);
3902 if (tevent_req_nomem(subreq, req)) {
3903 return;
3905 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3906 return;
3909 conn->smb2.server.security_mode = SVAL(body, 2);
3911 blob = data_blob_const(body + 8, 16);
3912 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
3913 if (tevent_req_nterror(req, status)) {
3914 return;
3917 conn->smb2.server.capabilities = IVAL(body, 24);
3918 conn->smb2.server.max_trans_size= IVAL(body, 28);
3919 conn->smb2.server.max_read_size = IVAL(body, 32);
3920 conn->smb2.server.max_write_size= IVAL(body, 36);
3921 conn->smb2.server.system_time = BVAL(body, 40);
3922 conn->smb2.server.start_time = BVAL(body, 48);
3924 security_offset = SVAL(body, 56);
3925 security_length = SVAL(body, 58);
3927 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
3928 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3929 return;
3932 if (security_length > iov[2].iov_len) {
3933 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3934 return;
3937 conn->smb2.server.gss_blob = data_blob_talloc(conn,
3938 iov[2].iov_base,
3939 security_length);
3940 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
3941 return;
3944 tevent_req_done(req);
3947 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3948 TALLOC_CTX *tmp_mem,
3949 uint8_t *inbuf)
3951 size_t num_pending = talloc_array_length(conn->pending);
3952 struct tevent_req *subreq;
3953 struct smbXcli_req_state *substate;
3954 struct tevent_req *req;
3955 uint32_t protocol_magic = IVAL(inbuf, 4);
3957 if (num_pending != 1) {
3958 return NT_STATUS_INTERNAL_ERROR;
3961 subreq = conn->pending[0];
3962 substate = tevent_req_data(subreq, struct smbXcli_req_state);
3963 req = tevent_req_callback_data(subreq, struct tevent_req);
3965 switch (protocol_magic) {
3966 case SMB_MAGIC:
3967 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3968 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3969 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3971 case SMB2_MAGIC:
3972 if (substate->smb2.recv_iov == NULL) {
3974 * For the SMB1 negprot we have move it.
3976 substate->smb2.recv_iov = substate->smb1.recv_iov;
3977 substate->smb1.recv_iov = NULL;
3981 * we got an SMB2 answer, which consumed sequence number 0
3982 * so we need to use 1 as the next one
3984 conn->smb2.mid = 1;
3985 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3986 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3987 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3990 DEBUG(10, ("Got non-SMB PDU\n"));
3991 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3994 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
3996 return tevent_req_simple_recv_ntstatus(req);
3999 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4000 uint32_t timeout_msec,
4001 enum protocol_types min_protocol,
4002 enum protocol_types max_protocol)
4004 TALLOC_CTX *frame = talloc_stackframe();
4005 struct tevent_context *ev;
4006 struct tevent_req *req;
4007 NTSTATUS status = NT_STATUS_NO_MEMORY;
4008 bool ok;
4010 if (smbXcli_conn_has_async_calls(conn)) {
4012 * Can't use sync call while an async call is in flight
4014 status = NT_STATUS_INVALID_PARAMETER_MIX;
4015 goto fail;
4017 ev = tevent_context_init(frame);
4018 if (ev == NULL) {
4019 goto fail;
4021 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4022 min_protocol, max_protocol);
4023 if (req == NULL) {
4024 goto fail;
4026 ok = tevent_req_poll(req, ev);
4027 if (!ok) {
4028 status = map_nt_error_from_unix_common(errno);
4029 goto fail;
4031 status = smbXcli_negprot_recv(req);
4032 fail:
4033 TALLOC_FREE(frame);
4034 return status;
4037 static int smbXcli_session_destructor(struct smbXcli_session *session)
4039 if (session->conn == NULL) {
4040 return 0;
4043 DLIST_REMOVE(session->conn->sessions, session);
4044 return 0;
4047 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4048 struct smbXcli_conn *conn)
4050 struct smbXcli_session *session;
4052 session = talloc_zero(mem_ctx, struct smbXcli_session);
4053 if (session == NULL) {
4054 return NULL;
4056 talloc_set_destructor(session, smbXcli_session_destructor);
4058 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4059 session->conn = conn;
4061 return session;
4064 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4066 struct smbXcli_conn *conn = session->conn;
4067 uint8_t security_mode = 0;
4069 if (conn == NULL) {
4070 return security_mode;
4073 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4074 if (conn->mandatory_signing) {
4075 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4078 return security_mode;
4081 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4083 return session->smb2.session_id;
4086 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4088 return session->smb2.session_flags;
4091 NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
4092 TALLOC_CTX *mem_ctx,
4093 DATA_BLOB *key)
4095 *key = data_blob_null;
4097 if (session->smb2.application_key.length == 0) {
4098 return NT_STATUS_NO_USER_SESSION_KEY;
4101 *key = data_blob_dup_talloc(mem_ctx, session->smb2.application_key);
4102 if (key->data == NULL) {
4103 return NT_STATUS_NO_MEMORY;
4106 return NT_STATUS_OK;
4109 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4110 uint64_t session_id,
4111 uint16_t session_flags)
4113 session->smb2.session_id = session_id;
4114 session->smb2.session_flags = session_flags;
4117 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4118 const DATA_BLOB _session_key,
4119 const struct iovec *recv_iov)
4121 struct smbXcli_conn *conn = session->conn;
4122 uint16_t no_sign_flags;
4123 uint8_t session_key[16];
4124 NTSTATUS status;
4126 if (conn == NULL) {
4127 return NT_STATUS_INVALID_PARAMETER_MIX;
4130 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4132 if (session->smb2.session_flags & no_sign_flags) {
4133 session->smb2.should_sign = false;
4134 return NT_STATUS_OK;
4137 if (session->smb2.signing_key.length != 0) {
4138 return NT_STATUS_INVALID_PARAMETER_MIX;
4141 ZERO_STRUCT(session_key);
4142 memcpy(session_key, _session_key.data,
4143 MIN(_session_key.length, sizeof(session_key)));
4145 session->smb2.signing_key = data_blob_talloc(session,
4146 session_key,
4147 sizeof(session_key));
4148 if (session->smb2.signing_key.data == NULL) {
4149 ZERO_STRUCT(session_key);
4150 return NT_STATUS_NO_MEMORY;
4153 if (conn->protocol >= PROTOCOL_SMB2_24) {
4154 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4155 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4157 smb2_key_derivation(session_key, sizeof(session_key),
4158 label.data, label.length,
4159 context.data, context.length,
4160 session->smb2.signing_key.data);
4163 session->smb2.application_key = data_blob_dup_talloc(session,
4164 session->smb2.signing_key);
4165 if (session->smb2.application_key.data == NULL) {
4166 ZERO_STRUCT(session_key);
4167 return NT_STATUS_NO_MEMORY;
4170 if (conn->protocol >= PROTOCOL_SMB2_24) {
4171 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4172 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4174 smb2_key_derivation(session_key, sizeof(session_key),
4175 label.data, label.length,
4176 context.data, context.length,
4177 session->smb2.application_key.data);
4179 ZERO_STRUCT(session_key);
4181 session->smb2.channel_signing_key = data_blob_dup_talloc(session,
4182 session->smb2.signing_key);
4183 if (session->smb2.channel_signing_key.data == NULL) {
4184 return NT_STATUS_NO_MEMORY;
4187 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4188 session->conn->protocol,
4189 recv_iov, 3);
4190 if (!NT_STATUS_IS_OK(status)) {
4191 return status;
4194 session->smb2.should_sign = false;
4196 if (conn->desire_signing) {
4197 session->smb2.should_sign = true;
4200 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4201 session->smb2.should_sign = true;
4204 return NT_STATUS_OK;
4207 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4208 struct smbXcli_session *session1,
4209 struct smbXcli_conn *conn,
4210 struct smbXcli_session **_session2)
4212 struct smbXcli_session *session2;
4214 if (session1->smb2.signing_key.length == 0) {
4215 return NT_STATUS_INVALID_PARAMETER_MIX;
4218 if (conn == NULL) {
4219 return NT_STATUS_INVALID_PARAMETER_MIX;
4222 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4223 if (session2 == NULL) {
4224 return NT_STATUS_NO_MEMORY;
4226 session2->smb2.session_id = session1->smb2.session_id;
4227 session2->smb2.session_flags = session1->smb2.session_flags;
4229 session2->smb2.signing_key = data_blob_dup_talloc(session2,
4230 session1->smb2.signing_key);
4231 if (session2->smb2.signing_key.data == NULL) {
4232 return NT_STATUS_NO_MEMORY;
4235 session2->smb2.should_sign = session1->smb2.should_sign;
4237 talloc_set_destructor(session2, smbXcli_session_destructor);
4238 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4239 session2->conn = conn;
4241 *_session2 = session2;
4242 return NT_STATUS_OK;
4245 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4246 const DATA_BLOB _channel_key,
4247 const struct iovec *recv_iov)
4249 struct smbXcli_conn *conn = session->conn;
4250 uint8_t channel_key[16];
4251 NTSTATUS status;
4253 if (conn == NULL) {
4254 return NT_STATUS_INVALID_PARAMETER_MIX;
4257 if (session->smb2.channel_signing_key.length != 0) {
4258 return NT_STATUS_INVALID_PARAMETER_MIX;
4261 ZERO_STRUCT(channel_key);
4262 memcpy(channel_key, _channel_key.data,
4263 MIN(_channel_key.length, sizeof(channel_key)));
4265 session->smb2.channel_signing_key = data_blob_talloc(session,
4266 channel_key,
4267 sizeof(channel_key));
4268 if (session->smb2.channel_signing_key.data == NULL) {
4269 ZERO_STRUCT(channel_key);
4270 return NT_STATUS_NO_MEMORY;
4273 if (conn->protocol >= PROTOCOL_SMB2_24) {
4274 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4275 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4277 smb2_key_derivation(channel_key, sizeof(channel_key),
4278 label.data, label.length,
4279 context.data, context.length,
4280 session->smb2.channel_signing_key.data);
4282 ZERO_STRUCT(channel_key);
4284 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4285 session->conn->protocol,
4286 recv_iov, 3);
4287 if (!NT_STATUS_IS_OK(status)) {
4288 return status;
4291 return NT_STATUS_OK;