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/>.
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"
37 struct smbXcli_session;
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;
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
61 * - All other return values disconnect the connection.
63 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
69 uint32_t capabilities;
74 uint32_t capabilities;
77 uint16_t security_mode;
86 const char *workgroup;
92 uint32_t capabilities;
97 struct smb_signing_state *signing;
98 struct smb_trans_enc_state *trans_enc;
100 struct tevent_req *read_braw_req;
105 uint32_t capabilities;
106 uint16_t security_mode;
111 uint32_t capabilities;
112 uint16_t security_mode;
114 uint32_t max_trans_size;
115 uint32_t max_read_size;
116 uint32_t max_write_size;
123 uint16_t cur_credits;
124 uint16_t max_credits;
127 struct smbXcli_session *sessions;
130 struct smbXcli_session {
131 struct smbXcli_session *prev, *next;
132 struct smbXcli_conn *conn;
136 uint16_t session_flags;
137 DATA_BLOB application_key;
138 DATA_BLOB signing_key;
141 DATA_BLOB encryption_key;
142 DATA_BLOB decryption_key;
143 uint64_t channel_nonce;
144 uint64_t channel_next;
145 DATA_BLOB channel_signing_key;
149 struct smbXcli_req_state {
150 struct tevent_context *ev;
151 struct smbXcli_conn *conn;
152 struct smbXcli_session *session; /* maybe NULL */
154 uint8_t length_hdr[4];
161 /* Space for the header including the wct */
162 uint8_t hdr[HDR_VWV];
165 * For normal requests, smb1cli_req_send chooses a mid.
166 * SecondaryV trans requests need to use the mid of the primary
167 * request, so we need a place to store it.
168 * Assume it is set if != 0.
173 uint8_t bytecount_buf[2];
175 #define MAX_SMB_IOV 10
176 /* length_hdr, hdr, words, byte_count, buffers */
177 struct iovec iov[1 + 3 + MAX_SMB_IOV];
182 struct tevent_req **chained_requests;
185 NTSTATUS recv_status;
186 /* always an array of 3 talloc elements */
187 struct iovec *recv_iov;
191 const uint8_t *fixed;
197 uint8_t pad[7]; /* padding space for compounding */
200 * always an array of 3 talloc elements
201 * (without a SMB2_TRANSFORM header!)
205 struct iovec *recv_iov;
207 uint16_t credit_charge;
209 bool signing_skipped;
215 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
218 * NT_STATUS_OK, means we do not notify the callers
220 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
222 while (conn->sessions) {
223 conn->sessions->conn = NULL;
224 DLIST_REMOVE(conn->sessions, conn->sessions);
227 if (conn->smb1.trans_enc) {
228 TALLOC_FREE(conn->smb1.trans_enc);
234 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
236 const char *remote_name,
237 enum smb_signing_setting signing_state,
238 uint32_t smb1_capabilities,
239 struct GUID *client_guid,
240 uint32_t smb2_capabilities)
242 struct smbXcli_conn *conn = NULL;
244 struct sockaddr *sa = NULL;
248 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
254 conn->write_fd = dup(fd);
255 if (conn->write_fd == -1) {
259 conn->remote_name = talloc_strdup(conn, remote_name);
260 if (conn->remote_name == NULL) {
265 ss = (void *)&conn->local_ss;
266 sa = (struct sockaddr *)ss;
267 sa_length = sizeof(conn->local_ss);
268 ret = getsockname(fd, sa, &sa_length);
272 ss = (void *)&conn->remote_ss;
273 sa = (struct sockaddr *)ss;
274 sa_length = sizeof(conn->remote_ss);
275 ret = getpeername(fd, sa, &sa_length);
280 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
281 if (conn->outgoing == NULL) {
284 conn->pending = NULL;
286 conn->protocol = PROTOCOL_NONE;
288 switch (signing_state) {
289 case SMB_SIGNING_OFF:
291 conn->allow_signing = false;
292 conn->desire_signing = false;
293 conn->mandatory_signing = false;
295 case SMB_SIGNING_DEFAULT:
296 case SMB_SIGNING_IF_REQUIRED:
297 /* if the server requires it */
298 conn->allow_signing = true;
299 conn->desire_signing = false;
300 conn->mandatory_signing = false;
302 case SMB_SIGNING_REQUIRED:
304 conn->allow_signing = true;
305 conn->desire_signing = true;
306 conn->mandatory_signing = true;
310 conn->smb1.client.capabilities = smb1_capabilities;
311 conn->smb1.client.max_xmit = UINT16_MAX;
313 conn->smb1.capabilities = conn->smb1.client.capabilities;
314 conn->smb1.max_xmit = 1024;
318 /* initialise signing */
319 conn->smb1.signing = smb_signing_init(conn,
321 conn->desire_signing,
322 conn->mandatory_signing);
323 if (!conn->smb1.signing) {
327 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
328 if (conn->mandatory_signing) {
329 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
332 conn->smb2.client.guid = *client_guid;
334 conn->smb2.client.capabilities = smb2_capabilities;
336 conn->smb2.cur_credits = 1;
337 conn->smb2.max_credits = 0;
339 talloc_set_destructor(conn, smbXcli_conn_destructor);
343 if (conn->write_fd != -1) {
344 close(conn->write_fd);
350 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
356 if (conn->read_fd == -1) {
363 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
365 return conn->protocol;
368 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
370 if (conn->protocol >= PROTOCOL_SMB2_02) {
374 if (conn->smb1.capabilities & CAP_UNICODE) {
381 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
383 set_socket_options(conn->read_fd, options);
386 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
388 return &conn->local_ss;
391 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
393 return &conn->remote_ss;
396 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
398 return conn->remote_name;
401 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
403 if (conn->protocol >= PROTOCOL_SMB2_02) {
410 return conn->smb1.server.max_mux;
413 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
415 if (conn->protocol >= PROTOCOL_SMB2_02) {
416 return conn->smb2.server.system_time;
419 return conn->smb1.server.system_time;
422 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
424 if (conn->protocol >= PROTOCOL_SMB2_02) {
425 return &conn->smb2.server.gss_blob;
428 return &conn->smb1.server.gss_blob;
431 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
433 if (conn->protocol >= PROTOCOL_SMB2_02) {
434 return &conn->smb2.server.guid;
437 return &conn->smb1.server.guid;
440 struct smbXcli_conn_samba_suicide_state {
441 struct smbXcli_conn *conn;
446 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
448 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
449 struct tevent_context *ev,
450 struct smbXcli_conn *conn,
453 struct tevent_req *req, *subreq;
454 struct smbXcli_conn_samba_suicide_state *state;
456 req = tevent_req_create(mem_ctx, &state,
457 struct smbXcli_conn_samba_suicide_state);
462 SIVAL(state->buf, 4, 0x74697865);
463 SCVAL(state->buf, 8, exitcode);
464 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
466 state->iov.iov_base = state->buf;
467 state->iov.iov_len = sizeof(state->buf);
469 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
470 false, &state->iov, 1);
471 if (tevent_req_nomem(subreq, req)) {
472 return tevent_req_post(req, ev);
474 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
478 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
480 struct tevent_req *req = tevent_req_callback_data(
481 subreq, struct tevent_req);
482 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
483 req, struct smbXcli_conn_samba_suicide_state);
487 nwritten = writev_recv(subreq, &err);
489 if (nwritten == -1) {
490 NTSTATUS status = map_nt_error_from_unix_common(err);
491 smbXcli_conn_disconnect(state->conn, status);
494 tevent_req_done(req);
497 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
499 return tevent_req_simple_recv_ntstatus(req);
502 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
505 TALLOC_CTX *frame = talloc_stackframe();
506 struct tevent_context *ev;
507 struct tevent_req *req;
508 NTSTATUS status = NT_STATUS_NO_MEMORY;
511 if (smbXcli_conn_has_async_calls(conn)) {
513 * Can't use sync call while an async call is in flight
515 status = NT_STATUS_INVALID_PARAMETER_MIX;
518 ev = tevent_context_init(frame);
522 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
526 ok = tevent_req_poll(req, ev);
528 status = map_nt_error_from_unix_common(errno);
531 status = smbXcli_conn_samba_suicide_recv(req);
537 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
539 return conn->smb1.capabilities;
542 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
544 return conn->smb1.max_xmit;
547 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
549 return conn->smb1.server.session_key;
552 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
554 return conn->smb1.server.challenge;
557 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
559 return conn->smb1.server.security_mode;
562 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
564 return conn->smb1.server.readbraw;
567 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
569 return conn->smb1.server.writebraw;
572 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
574 return conn->smb1.server.lockread;
577 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
579 return conn->smb1.server.writeunlock;
582 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
584 return conn->smb1.server.time_zone;
587 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
588 const DATA_BLOB user_session_key,
589 const DATA_BLOB response)
591 return smb_signing_activate(conn->smb1.signing,
596 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
597 const uint8_t *buf, uint32_t seqnum)
599 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
602 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
604 return smb_signing_is_active(conn->smb1.signing);
607 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
608 struct smb_trans_enc_state *es)
610 /* Replace the old state, if any. */
611 if (conn->smb1.trans_enc) {
612 TALLOC_FREE(conn->smb1.trans_enc);
614 conn->smb1.trans_enc = es;
617 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
619 return common_encryption_on(conn->smb1.trans_enc);
623 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
625 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
626 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
628 if (NT_STATUS_IS_OK(status)) {
632 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
636 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
640 * Is the SMB command able to hold an AND_X successor
641 * @param[in] cmd The SMB command in question
642 * @retval Can we add a chained request after "cmd"?
644 bool smb1cli_is_andx_req(uint8_t cmd)
664 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
666 size_t num_pending = talloc_array_length(conn->pending);
672 result = conn->smb1.mid++;
673 if ((result == 0) || (result == 0xffff)) {
677 for (i=0; i<num_pending; i++) {
678 if (result == smb1cli_req_mid(conn->pending[i])) {
683 if (i == num_pending) {
689 void smbXcli_req_unset_pending(struct tevent_req *req)
691 struct smbXcli_req_state *state =
693 struct smbXcli_req_state);
694 struct smbXcli_conn *conn = state->conn;
695 size_t num_pending = talloc_array_length(conn->pending);
698 if (state->smb1.mid != 0) {
700 * This is a [nt]trans[2] request which waits
701 * for more than one reply.
706 talloc_set_destructor(req, NULL);
708 if (num_pending == 1) {
710 * The pending read_smb tevent_req is a child of
711 * conn->pending. So if nothing is pending anymore, we need to
712 * delete the socket read fde.
714 TALLOC_FREE(conn->pending);
715 conn->read_smb_req = NULL;
719 for (i=0; i<num_pending; i++) {
720 if (req == conn->pending[i]) {
724 if (i == num_pending) {
726 * Something's seriously broken. Just returning here is the
727 * right thing nevertheless, the point of this routine is to
728 * remove ourselves from conn->pending.
734 * Remove ourselves from the conn->pending array
736 for (; i < (num_pending - 1); i++) {
737 conn->pending[i] = conn->pending[i+1];
741 * No NULL check here, we're shrinking by sizeof(void *), and
742 * talloc_realloc just adjusts the size for this.
744 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
749 static int smbXcli_req_destructor(struct tevent_req *req)
751 struct smbXcli_req_state *state =
753 struct smbXcli_req_state);
756 * Make sure we really remove it from
757 * the pending array on destruction.
760 smbXcli_req_unset_pending(req);
764 static bool smb1cli_req_cancel(struct tevent_req *req);
765 static bool smb2cli_req_cancel(struct tevent_req *req);
767 static bool smbXcli_req_cancel(struct tevent_req *req)
769 struct smbXcli_req_state *state =
771 struct smbXcli_req_state);
773 if (!smbXcli_conn_is_connected(state->conn)) {
777 if (state->conn->protocol == PROTOCOL_NONE) {
781 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
782 return smb2cli_req_cancel(req);
785 return smb1cli_req_cancel(req);
788 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
790 bool smbXcli_req_set_pending(struct tevent_req *req)
792 struct smbXcli_req_state *state =
794 struct smbXcli_req_state);
795 struct smbXcli_conn *conn;
796 struct tevent_req **pending;
801 if (!smbXcli_conn_is_connected(conn)) {
805 num_pending = talloc_array_length(conn->pending);
807 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
809 if (pending == NULL) {
812 pending[num_pending] = req;
813 conn->pending = pending;
814 talloc_set_destructor(req, smbXcli_req_destructor);
815 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
817 if (!smbXcli_conn_receive_next(conn)) {
819 * the caller should notify the current request
821 * And all other pending requests get notified
822 * by smbXcli_conn_disconnect().
824 smbXcli_req_unset_pending(req);
825 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
832 static void smbXcli_conn_received(struct tevent_req *subreq);
834 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
836 size_t num_pending = talloc_array_length(conn->pending);
837 struct tevent_req *req;
838 struct smbXcli_req_state *state;
840 if (conn->read_smb_req != NULL) {
844 if (num_pending == 0) {
845 if (conn->smb2.mid < UINT64_MAX) {
846 /* no more pending requests, so we are done for now */
851 * If there are no more SMB2 requests possible,
852 * because we are out of message ids,
853 * we need to disconnect.
855 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
859 req = conn->pending[0];
860 state = tevent_req_data(req, struct smbXcli_req_state);
863 * We're the first ones, add the read_smb request that waits for the
864 * answer from the server
866 conn->read_smb_req = read_smb_send(conn->pending,
869 if (conn->read_smb_req == NULL) {
872 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
876 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
878 tevent_queue_stop(conn->outgoing);
880 if (conn->read_fd != -1) {
881 close(conn->read_fd);
883 if (conn->write_fd != -1) {
884 close(conn->write_fd);
890 * Cancel all pending requests. We do not do a for-loop walking
891 * conn->pending because that array changes in
892 * smbXcli_req_unset_pending.
894 while (talloc_array_length(conn->pending) > 0) {
895 struct tevent_req *req;
896 struct smbXcli_req_state *state;
897 struct tevent_req **chain;
901 req = conn->pending[0];
902 state = tevent_req_data(req, struct smbXcli_req_state);
904 if (state->smb1.chained_requests == NULL) {
906 * We're dead. No point waiting for trans2
911 smbXcli_req_unset_pending(req);
913 if (NT_STATUS_IS_OK(status)) {
914 /* do not notify the callers */
919 * we need to defer the callback, because we may notify
920 * more then one caller.
922 tevent_req_defer_callback(req, state->ev);
923 tevent_req_nterror(req, status);
927 chain = talloc_move(conn, &state->smb1.chained_requests);
928 num_chained = talloc_array_length(chain);
930 for (i=0; i<num_chained; i++) {
932 state = tevent_req_data(req, struct smbXcli_req_state);
935 * We're dead. No point waiting for trans2
940 smbXcli_req_unset_pending(req);
942 if (NT_STATUS_IS_OK(status)) {
943 /* do not notify the callers */
948 * we need to defer the callback, because we may notify
949 * more than one caller.
951 tevent_req_defer_callback(req, state->ev);
952 tevent_req_nterror(req, status);
959 * Fetch a smb request's mid. Only valid after the request has been sent by
960 * smb1cli_req_send().
962 uint16_t smb1cli_req_mid(struct tevent_req *req)
964 struct smbXcli_req_state *state =
966 struct smbXcli_req_state);
968 if (state->smb1.mid != 0) {
969 return state->smb1.mid;
972 return SVAL(state->smb1.hdr, HDR_MID);
975 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
977 struct smbXcli_req_state *state =
979 struct smbXcli_req_state);
981 state->smb1.mid = mid;
984 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
986 struct smbXcli_req_state *state =
988 struct smbXcli_req_state);
990 return state->smb1.seqnum;
993 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
995 struct smbXcli_req_state *state =
997 struct smbXcli_req_state);
999 state->smb1.seqnum = seqnum;
1002 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1006 for (i=0; i<count; i++) {
1007 result += iov[i].iov_len;
1012 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1013 const struct iovec *iov,
1016 size_t len = smbXcli_iov_len(iov, count);
1021 buf = talloc_array(mem_ctx, uint8_t, len);
1026 for (i=0; i<count; i++) {
1027 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1028 copied += iov[i].iov_len;
1033 static void smb1cli_req_flags(enum protocol_types protocol,
1034 uint32_t smb1_capabilities,
1035 uint8_t smb_command,
1036 uint8_t additional_flags,
1037 uint8_t clear_flags,
1039 uint16_t additional_flags2,
1040 uint16_t clear_flags2,
1044 uint16_t flags2 = 0;
1046 if (protocol >= PROTOCOL_LANMAN1) {
1047 flags |= FLAG_CASELESS_PATHNAMES;
1048 flags |= FLAG_CANONICAL_PATHNAMES;
1051 if (protocol >= PROTOCOL_LANMAN2) {
1052 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1053 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1056 if (protocol >= PROTOCOL_NT1) {
1057 flags2 |= FLAGS2_IS_LONG_NAME;
1059 if (smb1_capabilities & CAP_UNICODE) {
1060 flags2 |= FLAGS2_UNICODE_STRINGS;
1062 if (smb1_capabilities & CAP_STATUS32) {
1063 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1065 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1066 flags2 |= FLAGS2_EXTENDED_SECURITY;
1070 flags |= additional_flags;
1071 flags &= ~clear_flags;
1072 flags2 |= additional_flags2;
1073 flags2 &= ~clear_flags2;
1079 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1081 static bool smb1cli_req_cancel(struct tevent_req *req)
1083 struct smbXcli_req_state *state =
1084 tevent_req_data(req,
1085 struct smbXcli_req_state);
1092 struct tevent_req *subreq;
1095 flags = CVAL(state->smb1.hdr, HDR_FLG);
1096 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1097 pid = SVAL(state->smb1.hdr, HDR_PID);
1098 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1099 tid = SVAL(state->smb1.hdr, HDR_TID);
1100 uid = SVAL(state->smb1.hdr, HDR_UID);
1101 mid = SVAL(state->smb1.hdr, HDR_MID);
1103 subreq = smb1cli_req_create(state, state->ev,
1111 0, NULL); /* bytes */
1112 if (subreq == NULL) {
1115 smb1cli_req_set_mid(subreq, mid);
1117 status = smb1cli_req_chain_submit(&subreq, 1);
1118 if (!NT_STATUS_IS_OK(status)) {
1119 TALLOC_FREE(subreq);
1122 smb1cli_req_set_mid(subreq, 0);
1124 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1129 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1131 /* we do not care about the result */
1132 TALLOC_FREE(subreq);
1135 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1136 struct tevent_context *ev,
1137 struct smbXcli_conn *conn,
1138 uint8_t smb_command,
1139 uint8_t additional_flags,
1140 uint8_t clear_flags,
1141 uint16_t additional_flags2,
1142 uint16_t clear_flags2,
1143 uint32_t timeout_msec,
1147 uint8_t wct, uint16_t *vwv,
1149 struct iovec *bytes_iov)
1151 struct tevent_req *req;
1152 struct smbXcli_req_state *state;
1154 uint16_t flags2 = 0;
1156 if (iov_count > MAX_SMB_IOV) {
1158 * Should not happen :-)
1163 req = tevent_req_create(mem_ctx, &state,
1164 struct smbXcli_req_state);
1171 state->smb1.recv_cmd = 0xFF;
1172 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1173 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1174 if (state->smb1.recv_iov == NULL) {
1179 smb1cli_req_flags(conn->protocol,
1180 conn->smb1.capabilities,
1189 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1190 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1191 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1192 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1193 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1194 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1195 SSVAL(state->smb1.hdr, HDR_TID, tid);
1196 SSVAL(state->smb1.hdr, HDR_PID, pid);
1197 SSVAL(state->smb1.hdr, HDR_UID, uid);
1198 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1199 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1201 state->smb1.vwv = vwv;
1203 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1205 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1206 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1207 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1208 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1209 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1210 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1211 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1212 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1214 if (iov_count != 0) {
1215 memcpy(&state->smb1.iov[4], bytes_iov,
1216 iov_count * sizeof(*bytes_iov));
1218 state->smb1.iov_count = iov_count + 4;
1220 if (timeout_msec > 0) {
1221 struct timeval endtime;
1223 endtime = timeval_current_ofs_msec(timeout_msec);
1224 if (!tevent_req_set_endtime(req, ev, endtime)) {
1229 switch (smb_command) {
1233 state->one_way = true;
1236 state->one_way = true;
1237 state->smb1.one_way_seqnum = true;
1241 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1242 state->one_way = true;
1250 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1251 struct iovec *iov, int iov_count,
1253 bool one_way_seqnum)
1255 TALLOC_CTX *frame = NULL;
1259 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1260 * iovec directly. MD5Update would do that just fine.
1263 if (iov_count < 4) {
1264 return NT_STATUS_INVALID_PARAMETER_MIX;
1266 if (iov[0].iov_len != NBT_HDR_SIZE) {
1267 return NT_STATUS_INVALID_PARAMETER_MIX;
1269 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1270 return NT_STATUS_INVALID_PARAMETER_MIX;
1272 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1273 return NT_STATUS_INVALID_PARAMETER_MIX;
1275 if (iov[3].iov_len != sizeof(uint16_t)) {
1276 return NT_STATUS_INVALID_PARAMETER_MIX;
1279 frame = talloc_stackframe();
1281 buf = smbXcli_iov_concat(frame, iov, iov_count);
1283 return NT_STATUS_NO_MEMORY;
1286 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1288 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1289 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1292 return NT_STATUS_OK;
1295 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1296 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1297 TALLOC_CTX *tmp_mem,
1300 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1301 struct smbXcli_req_state *state,
1302 struct iovec *iov, int iov_count)
1304 struct tevent_req *subreq;
1309 if (!smbXcli_conn_is_connected(state->conn)) {
1310 return NT_STATUS_CONNECTION_DISCONNECTED;
1313 if (state->conn->protocol > PROTOCOL_NT1) {
1314 return NT_STATUS_REVISION_MISMATCH;
1317 if (iov_count < 4) {
1318 return NT_STATUS_INVALID_PARAMETER_MIX;
1320 if (iov[0].iov_len != NBT_HDR_SIZE) {
1321 return NT_STATUS_INVALID_PARAMETER_MIX;
1323 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1324 return NT_STATUS_INVALID_PARAMETER_MIX;
1326 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1327 return NT_STATUS_INVALID_PARAMETER_MIX;
1329 if (iov[3].iov_len != sizeof(uint16_t)) {
1330 return NT_STATUS_INVALID_PARAMETER_MIX;
1333 cmd = CVAL(iov[1].iov_base, HDR_COM);
1334 if (cmd == SMBreadBraw) {
1335 if (smbXcli_conn_has_async_calls(state->conn)) {
1336 return NT_STATUS_INVALID_PARAMETER_MIX;
1338 state->conn->smb1.read_braw_req = req;
1341 if (state->smb1.mid != 0) {
1342 mid = state->smb1.mid;
1344 mid = smb1cli_alloc_mid(state->conn);
1346 SSVAL(iov[1].iov_base, HDR_MID, mid);
1348 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1350 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1351 &state->smb1.seqnum,
1352 state->smb1.one_way_seqnum);
1354 if (!NT_STATUS_IS_OK(status)) {
1359 * If we supported multiple encrytion contexts
1360 * here we'd look up based on tid.
1362 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1363 char *buf, *enc_buf;
1365 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1367 return NT_STATUS_NO_MEMORY;
1369 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1370 (char *)buf, &enc_buf);
1372 if (!NT_STATUS_IS_OK(status)) {
1373 DEBUG(0, ("Error in encrypting client message: %s\n",
1374 nt_errstr(status)));
1377 buf = (char *)talloc_memdup(state, enc_buf,
1378 smb_len_nbt(enc_buf)+4);
1381 return NT_STATUS_NO_MEMORY;
1383 iov[0].iov_base = (void *)buf;
1384 iov[0].iov_len = talloc_get_size(buf);
1388 if (state->conn->dispatch_incoming == NULL) {
1389 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1392 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1394 subreq = writev_send(state, state->ev, state->conn->outgoing,
1395 state->conn->write_fd, false, iov, iov_count);
1396 if (subreq == NULL) {
1397 return NT_STATUS_NO_MEMORY;
1399 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1400 return NT_STATUS_OK;
1403 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1404 struct tevent_context *ev,
1405 struct smbXcli_conn *conn,
1406 uint8_t smb_command,
1407 uint8_t additional_flags,
1408 uint8_t clear_flags,
1409 uint16_t additional_flags2,
1410 uint16_t clear_flags2,
1411 uint32_t timeout_msec,
1415 uint8_t wct, uint16_t *vwv,
1417 const uint8_t *bytes)
1419 struct tevent_req *req;
1423 iov.iov_base = discard_const_p(void, bytes);
1424 iov.iov_len = num_bytes;
1426 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1427 additional_flags, clear_flags,
1428 additional_flags2, clear_flags2,
1435 if (!tevent_req_is_in_progress(req)) {
1436 return tevent_req_post(req, ev);
1438 status = smb1cli_req_chain_submit(&req, 1);
1439 if (tevent_req_nterror(req, status)) {
1440 return tevent_req_post(req, ev);
1445 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1447 struct tevent_req *req =
1448 tevent_req_callback_data(subreq,
1450 struct smbXcli_req_state *state =
1451 tevent_req_data(req,
1452 struct smbXcli_req_state);
1456 nwritten = writev_recv(subreq, &err);
1457 TALLOC_FREE(subreq);
1458 if (nwritten == -1) {
1459 NTSTATUS status = map_nt_error_from_unix_common(err);
1460 smbXcli_conn_disconnect(state->conn, status);
1464 if (state->one_way) {
1465 state->inbuf = NULL;
1466 tevent_req_done(req);
1470 if (!smbXcli_req_set_pending(req)) {
1471 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1476 static void smbXcli_conn_received(struct tevent_req *subreq)
1478 struct smbXcli_conn *conn =
1479 tevent_req_callback_data(subreq,
1480 struct smbXcli_conn);
1481 TALLOC_CTX *frame = talloc_stackframe();
1487 if (subreq != conn->read_smb_req) {
1488 DEBUG(1, ("Internal error: cli_smb_received called with "
1489 "unexpected subreq\n"));
1490 status = NT_STATUS_INTERNAL_ERROR;
1491 smbXcli_conn_disconnect(conn, status);
1495 conn->read_smb_req = NULL;
1497 received = read_smb_recv(subreq, frame, &inbuf, &err);
1498 TALLOC_FREE(subreq);
1499 if (received == -1) {
1500 status = map_nt_error_from_unix_common(err);
1501 smbXcli_conn_disconnect(conn, status);
1506 status = conn->dispatch_incoming(conn, frame, inbuf);
1508 if (NT_STATUS_IS_OK(status)) {
1510 * We should not do any more processing
1511 * as the dispatch function called
1512 * tevent_req_done().
1515 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1517 * We got an error, so notify all pending requests
1519 smbXcli_conn_disconnect(conn, status);
1524 * We got NT_STATUS_RETRY, so we may ask for a
1525 * next incoming pdu.
1527 if (!smbXcli_conn_receive_next(conn)) {
1528 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1532 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1533 struct iovec **piov, int *pnum_iov)
1544 buflen = smb_len_nbt(buf);
1547 hdr = buf + NBT_HDR_SIZE;
1549 if (buflen < MIN_SMB_SIZE) {
1550 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1554 * This returns iovec elements in the following order:
1569 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1571 return NT_STATUS_NO_MEMORY;
1573 iov[0].iov_base = hdr;
1574 iov[0].iov_len = HDR_WCT;
1577 cmd = CVAL(hdr, HDR_COM);
1581 size_t len = buflen - taken;
1583 struct iovec *iov_tmp;
1590 * we need at least WCT and BCC
1592 needed = sizeof(uint8_t) + sizeof(uint16_t);
1594 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1595 __location__, (int)len, (int)needed));
1600 * Now we check if the specified words are there
1602 wct = CVAL(hdr, wct_ofs);
1603 needed += wct * sizeof(uint16_t);
1605 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1606 __location__, (int)len, (int)needed));
1611 * Now we check if the specified bytes are there
1613 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1614 bcc = SVAL(hdr, bcc_ofs);
1615 needed += bcc * sizeof(uint8_t);
1617 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1618 __location__, (int)len, (int)needed));
1623 * we allocate 2 iovec structures for words and bytes
1625 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1627 if (iov_tmp == NULL) {
1629 return NT_STATUS_NO_MEMORY;
1632 cur = &iov[num_iov];
1635 cur[0].iov_len = wct * sizeof(uint16_t);
1636 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1637 cur[1].iov_len = bcc * sizeof(uint8_t);
1638 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1642 if (!smb1cli_is_andx_req(cmd)) {
1644 * If the current command does not have AndX chanining
1650 if (wct == 0 && bcc == 0) {
1652 * An empty response also ends the chain,
1653 * most likely with an error.
1659 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1660 __location__, (int)wct, (int)cmd));
1663 cmd = CVAL(cur[0].iov_base, 0);
1666 * If it is the end of the chain we are also done.
1670 wct_ofs = SVAL(cur[0].iov_base, 2);
1672 if (wct_ofs < taken) {
1673 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1675 if (wct_ofs > buflen) {
1676 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1680 * we consumed everything up to the start of the next
1686 remaining = buflen - taken;
1688 if (remaining > 0 && num_iov >= 3) {
1690 * The last DATA block gets the remaining
1691 * bytes, this is needed to support
1692 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1694 iov[num_iov-1].iov_len += remaining;
1698 *pnum_iov = num_iov;
1699 return NT_STATUS_OK;
1703 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1706 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1707 TALLOC_CTX *tmp_mem,
1710 struct tevent_req *req;
1711 struct smbXcli_req_state *state;
1718 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1719 struct iovec *iov = NULL;
1721 struct tevent_req **chain = NULL;
1722 size_t num_chained = 0;
1723 size_t num_responses = 0;
1725 if (conn->smb1.read_braw_req != NULL) {
1726 req = conn->smb1.read_braw_req;
1727 conn->smb1.read_braw_req = NULL;
1728 state = tevent_req_data(req, struct smbXcli_req_state);
1730 smbXcli_req_unset_pending(req);
1732 if (state->smb1.recv_iov == NULL) {
1734 * For requests with more than
1735 * one response, we have to readd the
1738 state->smb1.recv_iov = talloc_zero_array(state,
1741 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1742 return NT_STATUS_OK;
1746 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1747 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1748 ZERO_STRUCT(state->smb1.recv_iov[1]);
1749 ZERO_STRUCT(state->smb1.recv_iov[2]);
1751 state->smb1.recv_cmd = SMBreadBraw;
1752 state->smb1.recv_status = NT_STATUS_OK;
1753 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1755 tevent_req_done(req);
1756 return NT_STATUS_OK;
1759 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1760 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1761 DEBUG(10, ("Got non-SMB PDU\n"));
1762 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1766 * If we supported multiple encrytion contexts
1767 * here we'd look up based on tid.
1769 if (common_encryption_on(conn->smb1.trans_enc)
1770 && (CVAL(inbuf, 0) == 0)) {
1771 uint16_t enc_ctx_num;
1773 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1774 if (!NT_STATUS_IS_OK(status)) {
1775 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1776 nt_errstr(status)));
1780 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1781 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1783 conn->smb1.trans_enc->enc_ctx_num));
1784 return NT_STATUS_INVALID_HANDLE;
1787 status = common_decrypt_buffer(conn->smb1.trans_enc,
1789 if (!NT_STATUS_IS_OK(status)) {
1790 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1791 nt_errstr(status)));
1796 mid = SVAL(inhdr, HDR_MID);
1797 num_pending = talloc_array_length(conn->pending);
1799 for (i=0; i<num_pending; i++) {
1800 if (mid == smb1cli_req_mid(conn->pending[i])) {
1804 if (i == num_pending) {
1805 /* Dump unexpected reply */
1806 return NT_STATUS_RETRY;
1809 oplock_break = false;
1811 if (mid == 0xffff) {
1813 * Paranoia checks that this is really an oplock break request.
1815 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1816 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1817 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1818 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1819 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1821 if (!oplock_break) {
1822 /* Dump unexpected reply */
1823 return NT_STATUS_RETRY;
1827 req = conn->pending[i];
1828 state = tevent_req_data(req, struct smbXcli_req_state);
1830 if (!oplock_break /* oplock breaks are not signed */
1831 && !smb_signing_check_pdu(conn->smb1.signing,
1832 inbuf, state->smb1.seqnum+1)) {
1833 DEBUG(10, ("cli_check_sign_mac failed\n"));
1834 return NT_STATUS_ACCESS_DENIED;
1837 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1839 if (!NT_STATUS_IS_OK(status)) {
1840 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1841 nt_errstr(status)));
1845 cmd = CVAL(inhdr, HDR_COM);
1846 status = smb1cli_pull_raw_error(inhdr);
1848 if (state->smb1.chained_requests == NULL) {
1850 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1853 smbXcli_req_unset_pending(req);
1855 if (state->smb1.recv_iov == NULL) {
1857 * For requests with more than
1858 * one response, we have to readd the
1861 state->smb1.recv_iov = talloc_zero_array(state,
1864 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1865 return NT_STATUS_OK;
1869 state->smb1.recv_cmd = cmd;
1870 state->smb1.recv_status = status;
1871 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1873 state->smb1.recv_iov[0] = iov[0];
1874 state->smb1.recv_iov[1] = iov[1];
1875 state->smb1.recv_iov[2] = iov[2];
1877 if (talloc_array_length(conn->pending) == 0) {
1878 tevent_req_done(req);
1879 return NT_STATUS_OK;
1882 tevent_req_defer_callback(req, state->ev);
1883 tevent_req_done(req);
1884 return NT_STATUS_RETRY;
1887 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1888 num_chained = talloc_array_length(chain);
1889 num_responses = (num_iov - 1)/2;
1891 if (num_responses > num_chained) {
1892 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1895 for (i=0; i<num_chained; i++) {
1896 size_t iov_idx = 1 + (i*2);
1897 struct iovec *cur = &iov[iov_idx];
1901 state = tevent_req_data(req, struct smbXcli_req_state);
1903 smbXcli_req_unset_pending(req);
1906 * as we finish multiple requests here
1907 * we need to defer the callbacks as
1908 * they could destroy our current stack state.
1910 tevent_req_defer_callback(req, state->ev);
1912 if (i >= num_responses) {
1913 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1917 if (state->smb1.recv_iov == NULL) {
1919 * For requests with more than
1920 * one response, we have to readd the
1923 state->smb1.recv_iov = talloc_zero_array(state,
1926 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1931 state->smb1.recv_cmd = cmd;
1933 if (i == (num_responses - 1)) {
1935 * The last request in the chain gets the status
1937 state->smb1.recv_status = status;
1939 cmd = CVAL(cur[0].iov_base, 0);
1940 state->smb1.recv_status = NT_STATUS_OK;
1943 state->inbuf = inbuf;
1946 * Note: here we use talloc_reference() in a way
1947 * that does not expose it to the caller.
1949 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1950 if (tevent_req_nomem(inbuf_ref, req)) {
1954 /* copy the related buffers */
1955 state->smb1.recv_iov[0] = iov[0];
1956 state->smb1.recv_iov[1] = cur[0];
1957 state->smb1.recv_iov[2] = cur[1];
1959 tevent_req_done(req);
1962 return NT_STATUS_RETRY;
1965 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1966 TALLOC_CTX *mem_ctx,
1967 struct iovec **piov,
1971 uint32_t *pvwv_offset,
1972 uint32_t *pnum_bytes,
1974 uint32_t *pbytes_offset,
1976 const struct smb1cli_req_expected_response *expected,
1977 size_t num_expected)
1979 struct smbXcli_req_state *state =
1980 tevent_req_data(req,
1981 struct smbXcli_req_state);
1982 NTSTATUS status = NT_STATUS_OK;
1983 struct iovec *recv_iov = NULL;
1984 uint8_t *hdr = NULL;
1986 uint32_t vwv_offset = 0;
1987 uint16_t *vwv = NULL;
1988 uint32_t num_bytes = 0;
1989 uint32_t bytes_offset = 0;
1990 uint8_t *bytes = NULL;
1992 bool found_status = false;
1993 bool found_size = false;
2007 if (pvwv_offset != NULL) {
2010 if (pnum_bytes != NULL) {
2013 if (pbytes != NULL) {
2016 if (pbytes_offset != NULL) {
2019 if (pinbuf != NULL) {
2023 if (state->inbuf != NULL) {
2024 recv_iov = state->smb1.recv_iov;
2025 state->smb1.recv_iov = NULL;
2026 if (state->smb1.recv_cmd != SMBreadBraw) {
2027 hdr = (uint8_t *)recv_iov[0].iov_base;
2028 wct = recv_iov[1].iov_len/2;
2029 vwv = (uint16_t *)recv_iov[1].iov_base;
2030 vwv_offset = PTR_DIFF(vwv, hdr);
2031 num_bytes = recv_iov[2].iov_len;
2032 bytes = (uint8_t *)recv_iov[2].iov_base;
2033 bytes_offset = PTR_DIFF(bytes, hdr);
2037 if (tevent_req_is_nterror(req, &status)) {
2038 for (i=0; i < num_expected; i++) {
2039 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2040 found_status = true;
2046 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2052 if (num_expected == 0) {
2053 found_status = true;
2057 status = state->smb1.recv_status;
2059 for (i=0; i < num_expected; i++) {
2060 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2064 found_status = true;
2065 if (expected[i].wct == 0) {
2070 if (expected[i].wct == wct) {
2076 if (!found_status) {
2081 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2085 *piov = talloc_move(mem_ctx, &recv_iov);
2097 if (pvwv_offset != NULL) {
2098 *pvwv_offset = vwv_offset;
2100 if (pnum_bytes != NULL) {
2101 *pnum_bytes = num_bytes;
2103 if (pbytes != NULL) {
2106 if (pbytes_offset != NULL) {
2107 *pbytes_offset = bytes_offset;
2109 if (pinbuf != NULL) {
2110 *pinbuf = state->inbuf;
2116 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2123 for (i=0; i<num_reqs; i++) {
2124 struct smbXcli_req_state *state;
2125 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2126 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2127 state->smb1.iov_count-2);
2128 wct_ofs = (wct_ofs + 3) & ~3;
2133 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2135 struct smbXcli_req_state *first_state =
2136 tevent_req_data(reqs[0],
2137 struct smbXcli_req_state);
2138 struct smbXcli_req_state *state;
2140 size_t chain_padding = 0;
2142 struct iovec *iov = NULL;
2143 struct iovec *this_iov;
2147 if (num_reqs == 1) {
2148 return smb1cli_req_writev_submit(reqs[0], first_state,
2149 first_state->smb1.iov,
2150 first_state->smb1.iov_count);
2154 for (i=0; i<num_reqs; i++) {
2155 if (!tevent_req_is_in_progress(reqs[i])) {
2156 return NT_STATUS_INTERNAL_ERROR;
2159 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2161 if (state->smb1.iov_count < 4) {
2162 return NT_STATUS_INVALID_PARAMETER_MIX;
2167 * The NBT and SMB header
2180 iovlen += state->smb1.iov_count - 2;
2183 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2185 return NT_STATUS_NO_MEMORY;
2188 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2189 first_state, reqs, sizeof(*reqs) * num_reqs);
2190 if (first_state->smb1.chained_requests == NULL) {
2192 return NT_STATUS_NO_MEMORY;
2195 wct_offset = HDR_WCT;
2198 for (i=0; i<num_reqs; i++) {
2199 size_t next_padding = 0;
2202 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2204 if (i < num_reqs-1) {
2205 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2206 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2208 TALLOC_FREE(first_state->smb1.chained_requests);
2209 return NT_STATUS_INVALID_PARAMETER_MIX;
2213 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2214 state->smb1.iov_count-2) + 1;
2215 if ((wct_offset % 4) != 0) {
2216 next_padding = 4 - (wct_offset % 4);
2218 wct_offset += next_padding;
2219 vwv = state->smb1.vwv;
2221 if (i < num_reqs-1) {
2222 struct smbXcli_req_state *next_state =
2223 tevent_req_data(reqs[i+1],
2224 struct smbXcli_req_state);
2225 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2227 SSVAL(vwv+1, 0, wct_offset);
2228 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2229 /* properly end the chain */
2230 SCVAL(vwv+0, 0, 0xff);
2231 SCVAL(vwv+0, 1, 0xff);
2237 * The NBT and SMB header
2239 this_iov[0] = state->smb1.iov[0];
2240 this_iov[1] = state->smb1.iov[1];
2244 * This one is a bit subtle. We have to add
2245 * chain_padding bytes between the requests, and we
2246 * have to also include the wct field of the
2247 * subsequent requests. We use the subsequent header
2248 * for the padding, it contains the wct field in its
2251 this_iov[0].iov_len = chain_padding+1;
2252 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2253 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2254 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2259 * copy the words and bytes
2261 memcpy(this_iov, state->smb1.iov+2,
2262 sizeof(struct iovec) * (state->smb1.iov_count-2));
2263 this_iov += state->smb1.iov_count - 2;
2264 chain_padding = next_padding;
2267 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2268 if (nbt_len > first_state->conn->smb1.max_xmit) {
2270 TALLOC_FREE(first_state->smb1.chained_requests);
2271 return NT_STATUS_INVALID_PARAMETER_MIX;
2274 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2275 if (!NT_STATUS_IS_OK(status)) {
2277 TALLOC_FREE(first_state->smb1.chained_requests);
2281 return NT_STATUS_OK;
2284 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2286 return ((tevent_queue_length(conn->outgoing) != 0)
2287 || (talloc_array_length(conn->pending) != 0));
2290 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2292 return conn->smb2.server.capabilities;
2295 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2297 return conn->smb2.server.security_mode;
2300 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2302 return conn->smb2.server.max_trans_size;
2305 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2307 return conn->smb2.server.max_read_size;
2310 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2312 return conn->smb2.server.max_write_size;
2315 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2316 uint16_t max_credits)
2318 conn->smb2.max_credits = max_credits;
2321 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2323 static bool smb2cli_req_cancel(struct tevent_req *req)
2325 struct smbXcli_req_state *state =
2326 tevent_req_data(req,
2327 struct smbXcli_req_state);
2328 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2329 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2330 uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2331 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2332 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2333 struct smbXcli_session *session = state->session;
2334 uint8_t *fixed = state->smb2.pad;
2335 uint16_t fixed_len = 4;
2336 struct tevent_req *subreq;
2337 struct smbXcli_req_state *substate;
2340 SSVAL(fixed, 0, 0x04);
2343 subreq = smb2cli_req_create(state, state->ev,
2351 if (subreq == NULL) {
2354 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2356 if (flags & SMB2_HDR_FLAG_ASYNC) {
2360 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2361 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2362 SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2363 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2364 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2366 status = smb2cli_req_compound_submit(&subreq, 1);
2367 if (!NT_STATUS_IS_OK(status)) {
2368 TALLOC_FREE(subreq);
2372 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2377 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2379 /* we do not care about the result */
2380 TALLOC_FREE(subreq);
2383 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2384 struct tevent_context *ev,
2385 struct smbXcli_conn *conn,
2387 uint32_t additional_flags,
2388 uint32_t clear_flags,
2389 uint32_t timeout_msec,
2392 struct smbXcli_session *session,
2393 const uint8_t *fixed,
2398 struct tevent_req *req;
2399 struct smbXcli_req_state *state;
2403 req = tevent_req_create(mem_ctx, &state,
2404 struct smbXcli_req_state);
2411 state->session = session;
2414 uid = session->smb2.session_id;
2417 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2418 if (state->smb2.recv_iov == NULL) {
2423 flags |= additional_flags;
2424 flags &= ~clear_flags;
2426 state->smb2.fixed = fixed;
2427 state->smb2.fixed_len = fixed_len;
2428 state->smb2.dyn = dyn;
2429 state->smb2.dyn_len = dyn_len;
2431 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2432 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2433 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2434 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2435 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2436 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2437 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2440 case SMB2_OP_CANCEL:
2441 state->one_way = true;
2445 * If this is a dummy request, it will have
2446 * UINT64_MAX as message id.
2447 * If we send on break acknowledgement,
2448 * this gets overwritten later.
2450 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2454 if (timeout_msec > 0) {
2455 struct timeval endtime;
2457 endtime = timeval_current_ofs_msec(timeout_msec);
2458 if (!tevent_req_set_endtime(req, ev, endtime)) {
2466 void smb2cli_req_set_notify_async(struct tevent_req *req)
2468 struct smbXcli_req_state *state =
2469 tevent_req_data(req,
2470 struct smbXcli_req_state);
2472 state->smb2.notify_async = true;
2475 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2476 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2477 TALLOC_CTX *tmp_mem,
2480 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2483 struct smbXcli_req_state *state;
2484 struct tevent_req *subreq;
2486 int i, num_iov, nbt_len;
2489 * 1 for the nbt length
2490 * per request: HDR, fixed, dyn, padding
2491 * -1 because the last one does not need padding
2494 iov = talloc_array(reqs[0], struct iovec, 1 + 4*num_reqs - 1);
2496 return NT_STATUS_NO_MEMORY;
2502 for (i=0; i<num_reqs; i++) {
2511 const DATA_BLOB *signing_key = NULL;
2513 if (!tevent_req_is_in_progress(reqs[i])) {
2514 return NT_STATUS_INTERNAL_ERROR;
2517 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2519 if (!smbXcli_conn_is_connected(state->conn)) {
2520 return NT_STATUS_CONNECTION_DISCONNECTED;
2523 if ((state->conn->protocol != PROTOCOL_NONE) &&
2524 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2525 return NT_STATUS_REVISION_MISMATCH;
2528 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2529 if (opcode == SMB2_OP_CANCEL) {
2533 avail = UINT64_MAX - state->conn->smb2.mid;
2535 return NT_STATUS_CONNECTION_ABORTED;
2538 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2539 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2544 charge = MAX(state->smb2.credit_charge, charge);
2546 avail = MIN(avail, state->conn->smb2.cur_credits);
2547 if (avail < charge) {
2548 return NT_STATUS_INTERNAL_ERROR;
2552 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2553 credits = state->conn->smb2.max_credits -
2554 state->conn->smb2.cur_credits;
2556 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2560 mid = state->conn->smb2.mid;
2561 state->conn->smb2.mid += charge;
2562 state->conn->smb2.cur_credits -= charge;
2564 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2565 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2567 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2568 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2571 if (state->session) {
2572 bool should_sign = state->session->smb2.should_sign;
2574 if (opcode == SMB2_OP_SESSSETUP &&
2575 state->session->smb2.signing_key.length != 0) {
2580 * We prefer the channel signing key if it is
2584 signing_key = &state->session->smb2.channel_signing_key;
2588 * If it is a channel binding, we already have the main
2589 * signing key and try that one.
2591 if (signing_key && signing_key->length == 0) {
2592 signing_key = &state->session->smb2.signing_key;
2596 * If we do not have any session key yet, we skip the
2597 * signing of SMB2_OP_SESSSETUP requests.
2599 if (signing_key && signing_key->length == 0) {
2605 iov[num_iov].iov_base = state->smb2.hdr;
2606 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2609 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2610 iov[num_iov].iov_len = state->smb2.fixed_len;
2613 if (state->smb2.dyn != NULL) {
2614 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2615 iov[num_iov].iov_len = state->smb2.dyn_len;
2619 reqlen = sizeof(state->smb2.hdr);
2620 reqlen += state->smb2.fixed_len;
2621 reqlen += state->smb2.dyn_len;
2623 if (i < num_reqs-1) {
2624 if ((reqlen % 8) > 0) {
2625 uint8_t pad = 8 - (reqlen % 8);
2626 iov[num_iov].iov_base = state->smb2.pad;
2627 iov[num_iov].iov_len = pad;
2631 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2638 status = smb2_signing_sign_pdu(*signing_key,
2639 state->session->conn->protocol,
2640 &iov[hdr_iov], num_iov - hdr_iov);
2641 if (!NT_STATUS_IS_OK(status)) {
2646 ret = smbXcli_req_set_pending(reqs[i]);
2648 return NT_STATUS_NO_MEMORY;
2652 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2653 _smb_setlen_tcp(state->length_hdr, nbt_len);
2654 iov[0].iov_base = state->length_hdr;
2655 iov[0].iov_len = sizeof(state->length_hdr);
2657 if (state->conn->dispatch_incoming == NULL) {
2658 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2661 subreq = writev_send(state, state->ev, state->conn->outgoing,
2662 state->conn->write_fd, false, iov, num_iov);
2663 if (subreq == NULL) {
2664 return NT_STATUS_NO_MEMORY;
2666 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2667 return NT_STATUS_OK;
2670 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2672 struct smbXcli_req_state *state =
2673 tevent_req_data(req,
2674 struct smbXcli_req_state);
2676 state->smb2.credit_charge = charge;
2679 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2680 struct tevent_context *ev,
2681 struct smbXcli_conn *conn,
2683 uint32_t additional_flags,
2684 uint32_t clear_flags,
2685 uint32_t timeout_msec,
2688 struct smbXcli_session *session,
2689 const uint8_t *fixed,
2694 struct tevent_req *req;
2697 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2698 additional_flags, clear_flags,
2701 fixed, fixed_len, dyn, dyn_len);
2705 if (!tevent_req_is_in_progress(req)) {
2706 return tevent_req_post(req, ev);
2708 status = smb2cli_req_compound_submit(&req, 1);
2709 if (tevent_req_nterror(req, status)) {
2710 return tevent_req_post(req, ev);
2715 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2717 struct tevent_req *req =
2718 tevent_req_callback_data(subreq,
2720 struct smbXcli_req_state *state =
2721 tevent_req_data(req,
2722 struct smbXcli_req_state);
2726 nwritten = writev_recv(subreq, &err);
2727 TALLOC_FREE(subreq);
2728 if (nwritten == -1) {
2729 /* here, we need to notify all pending requests */
2730 NTSTATUS status = map_nt_error_from_unix_common(err);
2731 smbXcli_conn_disconnect(state->conn, status);
2736 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2739 TALLOC_CTX *mem_ctx,
2740 struct iovec **piov, int *pnum_iov)
2745 uint8_t *first_hdr = buf;
2747 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2749 return NT_STATUS_NO_MEMORY;
2752 while (taken < buflen) {
2755 size_t len = buflen - taken;
2756 uint8_t *hdr = first_hdr + taken;
2759 size_t next_command_ofs;
2761 struct iovec *iov_tmp;
2764 DEBUG(10, ("%d bytes left, expected at least %d\n",
2768 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2769 struct smbXcli_session *s;
2771 struct iovec tf_iov[2];
2774 if (len < SMB2_TF_HDR_SIZE) {
2775 DEBUG(10, ("%d bytes left, expected at least %d\n",
2776 (int)len, SMB2_TF_HDR_SIZE));
2780 tf_len = SMB2_TF_HDR_SIZE;
2783 hdr = first_hdr + taken;
2784 len = IVAL(tf, SMB2_TF_MSG_SIZE);
2785 uid = BVAL(tf, SMB2_TF_SESSION_ID);
2788 for (; s; s = s->next) {
2789 if (s->smb2.session_id != uid) {
2796 DEBUG(10, ("unknown session_id %llu\n",
2797 (unsigned long long)uid));
2801 tf_iov[0].iov_base = (void *)tf;
2802 tf_iov[0].iov_len = tf_len;
2803 tf_iov[1].iov_base = (void *)hdr;
2804 tf_iov[1].iov_len = len;
2806 status = smb2_signing_decrypt_pdu(s->smb2.decryption_key,
2809 if (!NT_STATUS_IS_OK(status)) {
2816 * We need the header plus the body length field
2819 if (len < SMB2_HDR_BODY + 2) {
2820 DEBUG(10, ("%d bytes left, expected at least %d\n",
2821 (int)len, SMB2_HDR_BODY));
2824 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2825 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2829 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2830 DEBUG(10, ("Got HDR len %d, expected %d\n",
2831 SVAL(hdr, 4), SMB2_HDR_BODY));
2836 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2837 body_size = SVAL(hdr, SMB2_HDR_BODY);
2839 if (next_command_ofs != 0) {
2840 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2843 if (next_command_ofs > full_size) {
2846 if (tf && next_command_ofs < len) {
2849 full_size = next_command_ofs;
2851 if (body_size < 2) {
2854 body_size &= 0xfffe;
2856 if (body_size > (full_size - SMB2_HDR_BODY)) {
2860 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2862 if (iov_tmp == NULL) {
2864 return NT_STATUS_NO_MEMORY;
2867 cur = &iov[num_iov];
2870 cur[0].iov_base = tf;
2871 cur[0].iov_len = tf_len;
2872 cur[1].iov_base = hdr;
2873 cur[1].iov_len = SMB2_HDR_BODY;
2874 cur[2].iov_base = hdr + SMB2_HDR_BODY;
2875 cur[2].iov_len = body_size;
2876 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
2877 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2883 *pnum_iov = num_iov;
2884 return NT_STATUS_OK;
2888 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2891 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2894 size_t num_pending = talloc_array_length(conn->pending);
2897 for (i=0; i<num_pending; i++) {
2898 struct tevent_req *req = conn->pending[i];
2899 struct smbXcli_req_state *state =
2900 tevent_req_data(req,
2901 struct smbXcli_req_state);
2903 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2910 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2911 TALLOC_CTX *tmp_mem,
2914 struct tevent_req *req;
2915 struct smbXcli_req_state *state = NULL;
2920 struct smbXcli_session *last_session = NULL;
2921 size_t inbuf_len = smb_len_tcp(inbuf);
2923 status = smb2cli_inbuf_parse_compound(conn,
2924 inbuf + NBT_HDR_SIZE,
2928 if (!NT_STATUS_IS_OK(status)) {
2932 for (i=0; i<num_iov; i+=4) {
2933 uint8_t *inbuf_ref = NULL;
2934 struct iovec *cur = &iov[i];
2935 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
2936 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
2937 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
2938 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
2939 uint16_t req_opcode;
2941 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
2942 uint32_t new_credits;
2943 struct smbXcli_session *session = NULL;
2944 const DATA_BLOB *signing_key = NULL;
2945 bool should_sign = false;
2947 new_credits = conn->smb2.cur_credits;
2948 new_credits += credits;
2949 if (new_credits > UINT16_MAX) {
2950 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2952 conn->smb2.cur_credits += credits;
2954 req = smb2cli_conn_find_pending(conn, mid);
2956 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2958 state = tevent_req_data(req, struct smbXcli_req_state);
2960 state->smb2.got_async = false;
2962 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2963 if (opcode != req_opcode) {
2964 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2966 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2968 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
2969 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2972 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
2973 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
2974 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
2975 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
2978 * async interim responses are not signed,
2979 * even if the SMB2_HDR_FLAG_SIGNED flag
2982 req_flags |= SMB2_HDR_FLAG_ASYNC;
2983 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
2984 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
2986 if (state->smb2.notify_async) {
2987 state->smb2.got_async = true;
2988 tevent_req_defer_callback(req, state->ev);
2989 tevent_req_notify_callback(req);
2994 session = state->session;
2995 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
2996 session = last_session;
2998 last_session = session;
3001 should_sign = session->smb2.should_sign;
3002 if (opcode == SMB2_OP_SESSSETUP &&
3003 session->smb2.signing_key.length != 0) {
3009 * If we have a SMB2_TRANSFORM header we already verified
3012 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3013 should_sign = false;
3017 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3018 return NT_STATUS_ACCESS_DENIED;
3022 if (flags & SMB2_HDR_FLAG_SIGNED) {
3023 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3025 if (session == NULL) {
3026 struct smbXcli_session *s;
3028 s = state->conn->sessions;
3029 for (; s; s = s->next) {
3030 if (s->smb2.session_id != uid) {
3039 if (session == NULL) {
3040 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3043 last_session = session;
3044 signing_key = &session->smb2.channel_signing_key;
3047 if (opcode == SMB2_OP_SESSSETUP) {
3049 * We prefer the channel signing key, if it is
3052 * If we do not have a channel signing key yet,
3053 * we try the main signing key, if it is not
3054 * the final response.
3056 if (signing_key && signing_key->length == 0 &&
3057 !NT_STATUS_IS_OK(status)) {
3058 signing_key = &session->smb2.signing_key;
3061 if (signing_key && signing_key->length == 0) {
3063 * If we do not have a session key to
3064 * verify the signature, we defer the
3065 * signing check to the caller.
3067 * The caller gets NT_STATUS_OK, it
3069 * smb2cli_session_set_session_key()
3071 * smb2cli_session_set_channel_key()
3072 * which will check the signature
3073 * with the channel signing key.
3079 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3081 * if the server returns NT_STATUS_USER_SESSION_DELETED
3082 * the response is not signed and we should
3083 * propagate the NT_STATUS_USER_SESSION_DELETED
3084 * status to the caller.
3089 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3090 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3091 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3093 * if the server returns
3094 * NT_STATUS_NETWORK_NAME_DELETED
3095 * NT_STATUS_FILE_CLOSED
3096 * NT_STATUS_INVALID_PARAMETER
3097 * the response might not be signed
3098 * as this happens before the signing checks.
3100 * If server echos the signature (or all zeros)
3101 * we should report the status from the server
3107 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3108 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3111 state->smb2.signing_skipped = true;
3117 static const uint8_t zeros[16];
3119 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3123 state->smb2.signing_skipped = true;
3130 status = smb2_signing_check_pdu(*signing_key,
3131 state->conn->protocol,
3133 if (!NT_STATUS_IS_OK(status)) {
3135 * If the signing check fails, we disconnect
3142 smbXcli_req_unset_pending(req);
3145 * There might be more than one response
3146 * we need to defer the notifications
3148 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3153 tevent_req_defer_callback(req, state->ev);
3157 * Note: here we use talloc_reference() in a way
3158 * that does not expose it to the caller.
3160 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3161 if (tevent_req_nomem(inbuf_ref, req)) {
3165 /* copy the related buffers */
3166 state->smb2.recv_iov[0] = cur[1];
3167 state->smb2.recv_iov[1] = cur[2];
3168 state->smb2.recv_iov[2] = cur[3];
3170 tevent_req_done(req);
3174 return NT_STATUS_RETRY;
3177 return NT_STATUS_OK;
3180 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3181 struct iovec **piov,
3182 const struct smb2cli_req_expected_response *expected,
3183 size_t num_expected)
3185 struct smbXcli_req_state *state =
3186 tevent_req_data(req,
3187 struct smbXcli_req_state);
3190 bool found_status = false;
3191 bool found_size = false;
3198 if (state->smb2.got_async) {
3199 return STATUS_PENDING;
3202 if (tevent_req_is_nterror(req, &status)) {
3203 for (i=0; i < num_expected; i++) {
3204 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3205 found_status = true;
3211 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3217 if (num_expected == 0) {
3218 found_status = true;
3222 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3223 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3225 for (i=0; i < num_expected; i++) {
3226 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3230 found_status = true;
3231 if (expected[i].body_size == 0) {
3236 if (expected[i].body_size == body_size) {
3242 if (!found_status) {
3246 if (state->smb2.signing_skipped) {
3247 if (num_expected > 0) {
3248 return NT_STATUS_ACCESS_DENIED;
3250 if (!NT_STATUS_IS_ERR(status)) {
3251 return NT_STATUS_ACCESS_DENIED;
3256 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3260 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3266 static const struct {
3267 enum protocol_types proto;
3268 const char *smb1_name;
3269 } smb1cli_prots[] = {
3270 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3271 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3272 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3273 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3274 {PROTOCOL_LANMAN2, "LM1.2X002"},
3275 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3276 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3277 {PROTOCOL_LANMAN2, "Samba"},
3278 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3279 {PROTOCOL_NT1, "NT LM 0.12"},
3280 {PROTOCOL_SMB2_02, "SMB 2.002"},
3281 {PROTOCOL_SMB2_10, "SMB 2.???"},
3284 static const struct {
3285 enum protocol_types proto;
3286 uint16_t smb2_dialect;
3287 } smb2cli_prots[] = {
3288 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3289 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3290 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3291 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3292 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3295 struct smbXcli_negprot_state {
3296 struct smbXcli_conn *conn;
3297 struct tevent_context *ev;
3298 uint32_t timeout_msec;
3299 enum protocol_types min_protocol;
3300 enum protocol_types max_protocol;
3304 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3308 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3309 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3310 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3311 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3312 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3313 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3317 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3318 struct tevent_context *ev,
3319 struct smbXcli_conn *conn,
3320 uint32_t timeout_msec,
3321 enum protocol_types min_protocol,
3322 enum protocol_types max_protocol)
3324 struct tevent_req *req, *subreq;
3325 struct smbXcli_negprot_state *state;
3327 req = tevent_req_create(mem_ctx, &state,
3328 struct smbXcli_negprot_state);
3334 state->timeout_msec = timeout_msec;
3335 state->min_protocol = min_protocol;
3336 state->max_protocol = max_protocol;
3338 if (min_protocol == PROTOCOL_NONE) {
3339 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3340 return tevent_req_post(req, ev);
3343 if (max_protocol == PROTOCOL_NONE) {
3344 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3345 return tevent_req_post(req, ev);
3348 if (min_protocol > max_protocol) {
3349 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3350 return tevent_req_post(req, ev);
3353 if ((min_protocol < PROTOCOL_SMB2_02) &&
3354 (max_protocol < PROTOCOL_SMB2_02)) {
3358 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3360 subreq = smbXcli_negprot_smb1_subreq(state);
3361 if (tevent_req_nomem(subreq, req)) {
3362 return tevent_req_post(req, ev);
3364 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3368 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3369 (max_protocol >= PROTOCOL_SMB2_02)) {
3373 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3375 subreq = smbXcli_negprot_smb2_subreq(state);
3376 if (tevent_req_nomem(subreq, req)) {
3377 return tevent_req_post(req, ev);
3379 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3384 * We send an SMB1 negprot with the SMB2 dialects
3385 * and expect a SMB1 or a SMB2 response.
3387 * smbXcli_negprot_dispatch_incoming() will fix the
3388 * callback to match protocol of the response.
3390 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3392 subreq = smbXcli_negprot_smb1_subreq(state);
3393 if (tevent_req_nomem(subreq, req)) {
3394 return tevent_req_post(req, ev);
3396 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3400 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3402 struct tevent_req *req =
3403 tevent_req_callback_data(subreq,
3408 * we just want the low level error
3410 status = tevent_req_simple_recv_ntstatus(subreq);
3411 TALLOC_FREE(subreq);
3412 if (tevent_req_nterror(req, status)) {
3416 /* this should never happen */
3417 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3420 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3423 DATA_BLOB bytes = data_blob_null;
3427 /* setup the protocol strings */
3428 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3432 if (smb1cli_prots[i].proto < state->min_protocol) {
3436 if (smb1cli_prots[i].proto > state->max_protocol) {
3440 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3446 * We now it is already ascii and
3447 * we want NULL termination.
3449 ok = data_blob_append(state, &bytes,
3450 smb1cli_prots[i].smb1_name,
3451 strlen(smb1cli_prots[i].smb1_name)+1);
3457 smb1cli_req_flags(state->max_protocol,
3458 state->conn->smb1.client.capabilities,
3463 return smb1cli_req_send(state, state->ev, state->conn,
3467 state->timeout_msec,
3468 0xFFFE, 0, 0, /* pid, tid, uid */
3469 0, NULL, /* wct, vwv */
3470 bytes.length, bytes.data);
3473 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3475 struct tevent_req *req =
3476 tevent_req_callback_data(subreq,
3478 struct smbXcli_negprot_state *state =
3479 tevent_req_data(req,
3480 struct smbXcli_negprot_state);
3481 struct smbXcli_conn *conn = state->conn;
3482 struct iovec *recv_iov = NULL;
3491 size_t num_prots = 0;
3493 uint32_t client_capabilities = conn->smb1.client.capabilities;
3494 uint32_t both_capabilities;
3495 uint32_t server_capabilities = 0;
3496 uint32_t capabilities;
3497 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3498 uint32_t server_max_xmit = 0;
3500 uint32_t server_max_mux = 0;
3501 uint16_t server_security_mode = 0;
3502 uint32_t server_session_key = 0;
3503 bool server_readbraw = false;
3504 bool server_writebraw = false;
3505 bool server_lockread = false;
3506 bool server_writeunlock = false;
3507 struct GUID server_guid = GUID_zero();
3508 DATA_BLOB server_gss_blob = data_blob_null;
3509 uint8_t server_challenge[8];
3510 char *server_workgroup = NULL;
3511 char *server_name = NULL;
3512 int server_time_zone = 0;
3513 NTTIME server_system_time = 0;
3514 static const struct smb1cli_req_expected_response expected[] = {
3516 .status = NT_STATUS_OK,
3517 .wct = 0x11, /* NT1 */
3520 .status = NT_STATUS_OK,
3521 .wct = 0x0D, /* LM */
3524 .status = NT_STATUS_OK,
3525 .wct = 0x01, /* CORE */
3529 ZERO_STRUCT(server_challenge);
3531 status = smb1cli_req_recv(subreq, state,
3536 NULL, /* pvwv_offset */
3539 NULL, /* pbytes_offset */
3541 expected, ARRAY_SIZE(expected));
3542 TALLOC_FREE(subreq);
3543 if (tevent_req_nterror(req, status)) {
3547 flags = CVAL(inhdr, HDR_FLG);
3549 protnum = SVAL(vwv, 0);
3551 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3552 if (smb1cli_prots[i].proto < state->min_protocol) {
3556 if (smb1cli_prots[i].proto > state->max_protocol) {
3560 if (protnum != num_prots) {
3565 conn->protocol = smb1cli_prots[i].proto;
3569 if (conn->protocol == PROTOCOL_NONE) {
3570 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3574 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3575 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3576 "and the selected protocol level doesn't support it.\n"));
3577 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3581 if (flags & FLAG_SUPPORT_LOCKREAD) {
3582 server_lockread = true;
3583 server_writeunlock = true;
3586 if (conn->protocol >= PROTOCOL_NT1) {
3587 const char *client_signing = NULL;
3588 bool server_mandatory = false;
3589 bool server_allowed = false;
3590 const char *server_signing = NULL;
3595 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3600 server_security_mode = CVAL(vwv + 1, 0);
3601 server_max_mux = SVAL(vwv + 1, 1);
3602 server_max_xmit = IVAL(vwv + 3, 1);
3603 server_session_key = IVAL(vwv + 7, 1);
3604 server_time_zone = SVALS(vwv + 15, 1);
3605 server_time_zone *= 60;
3606 /* this time arrives in real GMT */
3607 server_system_time = BVAL(vwv + 11, 1);
3608 server_capabilities = IVAL(vwv + 9, 1);
3610 key_len = CVAL(vwv + 16, 1);
3612 if (server_capabilities & CAP_RAW_MODE) {
3613 server_readbraw = true;
3614 server_writebraw = true;
3616 if (server_capabilities & CAP_LOCK_AND_READ) {
3617 server_lockread = true;
3620 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3621 DATA_BLOB blob1, blob2;
3623 if (num_bytes < 16) {
3624 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3628 blob1 = data_blob_const(bytes, 16);
3629 status = GUID_from_data_blob(&blob1, &server_guid);
3630 if (tevent_req_nterror(req, status)) {
3634 blob1 = data_blob_const(bytes+16, num_bytes-16);
3635 blob2 = data_blob_dup_talloc(state, blob1);
3636 if (blob1.length > 0 &&
3637 tevent_req_nomem(blob2.data, req)) {
3640 server_gss_blob = blob2;
3642 DATA_BLOB blob1, blob2;
3644 if (num_bytes < key_len) {
3645 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3649 if (key_len != 0 && key_len != 8) {
3650 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3655 memcpy(server_challenge, bytes, 8);
3658 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3659 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3660 if (blob1.length > 0) {
3663 len = utf16_len_n(blob1.data,
3667 ok = convert_string_talloc(state,
3675 status = map_nt_error_from_unix_common(errno);
3676 tevent_req_nterror(req, status);
3681 blob2.data += blob1.length;
3682 blob2.length -= blob1.length;
3683 if (blob2.length > 0) {
3686 len = utf16_len_n(blob1.data,
3690 ok = convert_string_talloc(state,
3698 status = map_nt_error_from_unix_common(errno);
3699 tevent_req_nterror(req, status);
3705 client_signing = "disabled";
3706 if (conn->allow_signing) {
3707 client_signing = "allowed";
3709 if (conn->mandatory_signing) {
3710 client_signing = "required";
3713 server_signing = "not supported";
3714 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3715 server_signing = "supported";
3716 server_allowed = true;
3718 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3719 server_signing = "required";
3720 server_mandatory = true;
3723 ok = smb_signing_set_negotiated(conn->smb1.signing,
3727 DEBUG(1,("cli_negprot: SMB signing is required, "
3728 "but client[%s] and server[%s] mismatch\n",
3729 client_signing, server_signing));
3730 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3734 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3740 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3744 server_security_mode = SVAL(vwv + 1, 0);
3745 server_max_xmit = SVAL(vwv + 2, 0);
3746 server_max_mux = SVAL(vwv + 3, 0);
3747 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3748 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3749 server_session_key = IVAL(vwv + 6, 0);
3750 server_time_zone = SVALS(vwv + 10, 0);
3751 server_time_zone *= 60;
3752 /* this time is converted to GMT by make_unix_date */
3753 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3754 unix_to_nt_time(&server_system_time, t);
3755 key_len = SVAL(vwv + 11, 0);
3757 if (num_bytes < key_len) {
3758 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3762 if (key_len != 0 && key_len != 8) {
3763 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3768 memcpy(server_challenge, bytes, 8);
3771 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3772 if (blob1.length > 0) {
3776 len = utf16_len_n(blob1.data,
3780 ok = convert_string_talloc(state,
3788 status = map_nt_error_from_unix_common(errno);
3789 tevent_req_nterror(req, status);
3795 /* the old core protocol */
3796 server_time_zone = get_time_zone(time(NULL));
3797 server_max_xmit = 1024;
3801 if (server_max_xmit < 1024) {
3802 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3806 if (server_max_mux < 1) {
3807 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3812 * Now calculate the negotiated capabilities
3813 * based on the mask for:
3814 * - client only flags
3815 * - flags used in both directions
3816 * - server only flags
3818 both_capabilities = client_capabilities & server_capabilities;
3819 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3820 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3821 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3823 max_xmit = MIN(client_max_xmit, server_max_xmit);
3825 conn->smb1.server.capabilities = server_capabilities;
3826 conn->smb1.capabilities = capabilities;
3828 conn->smb1.server.max_xmit = server_max_xmit;
3829 conn->smb1.max_xmit = max_xmit;
3831 conn->smb1.server.max_mux = server_max_mux;
3833 conn->smb1.server.security_mode = server_security_mode;
3835 conn->smb1.server.readbraw = server_readbraw;
3836 conn->smb1.server.writebraw = server_writebraw;
3837 conn->smb1.server.lockread = server_lockread;
3838 conn->smb1.server.writeunlock = server_writeunlock;
3840 conn->smb1.server.session_key = server_session_key;
3842 talloc_steal(conn, server_gss_blob.data);
3843 conn->smb1.server.gss_blob = server_gss_blob;
3844 conn->smb1.server.guid = server_guid;
3845 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3846 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3847 conn->smb1.server.name = talloc_move(conn, &server_name);
3849 conn->smb1.server.time_zone = server_time_zone;
3850 conn->smb1.server.system_time = server_system_time;
3852 tevent_req_done(req);
3855 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3859 uint16_t dialect_count = 0;
3861 buf = state->smb2.dyn;
3862 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3863 if (smb2cli_prots[i].proto < state->min_protocol) {
3867 if (smb2cli_prots[i].proto > state->max_protocol) {
3871 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3875 buf = state->smb2.fixed;
3877 SSVAL(buf, 2, dialect_count);
3878 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3879 SSVAL(buf, 6, 0); /* Reserved */
3880 if (state->max_protocol >= PROTOCOL_SMB2_22) {
3881 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
3883 SIVAL(buf, 8, 0); /* Capabilities */
3885 if (state->max_protocol >= PROTOCOL_SMB2_10) {
3889 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3891 if (!NT_STATUS_IS_OK(status)) {
3894 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3896 memset(buf+12, 0, 16); /* ClientGuid */
3898 SBVAL(buf, 28, 0); /* ClientStartTime */
3900 return smb2cli_req_send(state, state->ev,
3901 state->conn, SMB2_OP_NEGPROT,
3903 state->timeout_msec,
3904 0xFEFF, 0, NULL, /* pid, tid, session */
3905 state->smb2.fixed, sizeof(state->smb2.fixed),
3906 state->smb2.dyn, dialect_count*2);
3909 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3911 struct tevent_req *req =
3912 tevent_req_callback_data(subreq,
3914 struct smbXcli_negprot_state *state =
3915 tevent_req_data(req,
3916 struct smbXcli_negprot_state);
3917 struct smbXcli_conn *conn = state->conn;
3918 size_t security_offset, security_length;
3924 uint16_t dialect_revision;
3925 static const struct smb2cli_req_expected_response expected[] = {
3927 .status = NT_STATUS_OK,
3932 status = smb2cli_req_recv(subreq, state, &iov,
3933 expected, ARRAY_SIZE(expected));
3934 TALLOC_FREE(subreq);
3935 if (tevent_req_nterror(req, status)) {
3939 body = (uint8_t *)iov[1].iov_base;
3941 dialect_revision = SVAL(body, 4);
3943 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3944 if (smb2cli_prots[i].proto < state->min_protocol) {
3948 if (smb2cli_prots[i].proto > state->max_protocol) {
3952 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
3956 conn->protocol = smb2cli_prots[i].proto;
3960 if (conn->protocol == PROTOCOL_NONE) {
3961 if (state->min_protocol >= PROTOCOL_SMB2_02) {
3962 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3966 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
3967 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3971 /* make sure we do not loop forever */
3972 state->min_protocol = PROTOCOL_SMB2_02;
3975 * send a SMB2 negprot, in order to negotiate
3978 subreq = smbXcli_negprot_smb2_subreq(state);
3979 if (tevent_req_nomem(subreq, req)) {
3982 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3986 conn->smb2.server.security_mode = SVAL(body, 2);
3988 blob = data_blob_const(body + 8, 16);
3989 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
3990 if (tevent_req_nterror(req, status)) {
3994 conn->smb2.server.capabilities = IVAL(body, 24);
3995 conn->smb2.server.max_trans_size= IVAL(body, 28);
3996 conn->smb2.server.max_read_size = IVAL(body, 32);
3997 conn->smb2.server.max_write_size= IVAL(body, 36);
3998 conn->smb2.server.system_time = BVAL(body, 40);
3999 conn->smb2.server.start_time = BVAL(body, 48);
4001 security_offset = SVAL(body, 56);
4002 security_length = SVAL(body, 58);
4004 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4005 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4009 if (security_length > iov[2].iov_len) {
4010 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4014 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4017 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4021 tevent_req_done(req);
4024 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4025 TALLOC_CTX *tmp_mem,
4028 size_t num_pending = talloc_array_length(conn->pending);
4029 struct tevent_req *subreq;
4030 struct smbXcli_req_state *substate;
4031 struct tevent_req *req;
4032 uint32_t protocol_magic;
4033 size_t inbuf_len = smb_len_nbt(inbuf);
4035 if (num_pending != 1) {
4036 return NT_STATUS_INTERNAL_ERROR;
4039 if (inbuf_len < 4) {
4040 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4043 subreq = conn->pending[0];
4044 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4045 req = tevent_req_callback_data(subreq, struct tevent_req);
4047 protocol_magic = IVAL(inbuf, 4);
4049 switch (protocol_magic) {
4051 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4052 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4053 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4056 if (substate->smb2.recv_iov == NULL) {
4058 * For the SMB1 negprot we have move it.
4060 substate->smb2.recv_iov = substate->smb1.recv_iov;
4061 substate->smb1.recv_iov = NULL;
4065 * we got an SMB2 answer, which consumed sequence number 0
4066 * so we need to use 1 as the next one
4069 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4070 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4071 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4074 DEBUG(10, ("Got non-SMB PDU\n"));
4075 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4078 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4080 return tevent_req_simple_recv_ntstatus(req);
4083 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4084 uint32_t timeout_msec,
4085 enum protocol_types min_protocol,
4086 enum protocol_types max_protocol)
4088 TALLOC_CTX *frame = talloc_stackframe();
4089 struct tevent_context *ev;
4090 struct tevent_req *req;
4091 NTSTATUS status = NT_STATUS_NO_MEMORY;
4094 if (smbXcli_conn_has_async_calls(conn)) {
4096 * Can't use sync call while an async call is in flight
4098 status = NT_STATUS_INVALID_PARAMETER_MIX;
4101 ev = tevent_context_init(frame);
4105 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4106 min_protocol, max_protocol);
4110 ok = tevent_req_poll(req, ev);
4112 status = map_nt_error_from_unix_common(errno);
4115 status = smbXcli_negprot_recv(req);
4121 static int smbXcli_session_destructor(struct smbXcli_session *session)
4123 if (session->conn == NULL) {
4127 DLIST_REMOVE(session->conn->sessions, session);
4131 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4132 struct smbXcli_conn *conn)
4134 struct smbXcli_session *session;
4136 session = talloc_zero(mem_ctx, struct smbXcli_session);
4137 if (session == NULL) {
4140 talloc_set_destructor(session, smbXcli_session_destructor);
4142 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4143 session->conn = conn;
4148 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4150 struct smbXcli_conn *conn = session->conn;
4151 uint8_t security_mode = 0;
4154 return security_mode;
4157 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4158 if (conn->mandatory_signing) {
4159 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4162 return security_mode;
4165 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4167 return session->smb2.session_id;
4170 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4172 return session->smb2.session_flags;
4175 NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
4176 TALLOC_CTX *mem_ctx,
4179 *key = data_blob_null;
4181 if (session->smb2.application_key.length == 0) {
4182 return NT_STATUS_NO_USER_SESSION_KEY;
4185 *key = data_blob_dup_talloc(mem_ctx, session->smb2.application_key);
4186 if (key->data == NULL) {
4187 return NT_STATUS_NO_MEMORY;
4190 return NT_STATUS_OK;
4193 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4194 uint64_t session_id,
4195 uint16_t session_flags)
4197 session->smb2.session_id = session_id;
4198 session->smb2.session_flags = session_flags;
4201 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4202 const DATA_BLOB _session_key,
4203 const struct iovec *recv_iov)
4205 struct smbXcli_conn *conn = session->conn;
4206 uint16_t no_sign_flags;
4207 uint8_t session_key[16];
4211 return NT_STATUS_INVALID_PARAMETER_MIX;
4214 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4216 if (session->smb2.session_flags & no_sign_flags) {
4217 session->smb2.should_sign = false;
4218 return NT_STATUS_OK;
4221 if (session->smb2.signing_key.length != 0) {
4222 return NT_STATUS_INVALID_PARAMETER_MIX;
4225 ZERO_STRUCT(session_key);
4226 memcpy(session_key, _session_key.data,
4227 MIN(_session_key.length, sizeof(session_key)));
4229 session->smb2.signing_key = data_blob_talloc(session,
4231 sizeof(session_key));
4232 if (session->smb2.signing_key.data == NULL) {
4233 ZERO_STRUCT(session_key);
4234 return NT_STATUS_NO_MEMORY;
4237 if (conn->protocol >= PROTOCOL_SMB2_24) {
4238 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4239 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4241 smb2_key_derivation(session_key, sizeof(session_key),
4242 label.data, label.length,
4243 context.data, context.length,
4244 session->smb2.signing_key.data);
4247 session->smb2.encryption_key = data_blob_dup_talloc(session,
4248 session->smb2.signing_key);
4249 if (session->smb2.encryption_key.data == NULL) {
4250 ZERO_STRUCT(session_key);
4251 return NT_STATUS_NO_MEMORY;
4254 if (conn->protocol >= PROTOCOL_SMB2_24) {
4255 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4256 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4258 smb2_key_derivation(session_key, sizeof(session_key),
4259 label.data, label.length,
4260 context.data, context.length,
4261 session->smb2.encryption_key.data);
4264 session->smb2.decryption_key = data_blob_dup_talloc(session,
4265 session->smb2.signing_key);
4266 if (session->smb2.decryption_key.data == NULL) {
4267 ZERO_STRUCT(session_key);
4268 return NT_STATUS_NO_MEMORY;
4271 if (conn->protocol >= PROTOCOL_SMB2_24) {
4272 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4273 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4275 smb2_key_derivation(session_key, sizeof(session_key),
4276 label.data, label.length,
4277 context.data, context.length,
4278 session->smb2.decryption_key.data);
4281 session->smb2.application_key = data_blob_dup_talloc(session,
4282 session->smb2.signing_key);
4283 if (session->smb2.application_key.data == NULL) {
4284 ZERO_STRUCT(session_key);
4285 return NT_STATUS_NO_MEMORY;
4288 if (conn->protocol >= PROTOCOL_SMB2_24) {
4289 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4290 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4292 smb2_key_derivation(session_key, sizeof(session_key),
4293 label.data, label.length,
4294 context.data, context.length,
4295 session->smb2.application_key.data);
4297 ZERO_STRUCT(session_key);
4299 session->smb2.channel_signing_key = data_blob_dup_talloc(session,
4300 session->smb2.signing_key);
4301 if (session->smb2.channel_signing_key.data == NULL) {
4302 return NT_STATUS_NO_MEMORY;
4305 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4306 session->conn->protocol,
4308 if (!NT_STATUS_IS_OK(status)) {
4312 session->smb2.should_sign = false;
4313 session->smb2.should_encrypt = false;
4315 if (conn->desire_signing) {
4316 session->smb2.should_sign = true;
4319 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4320 session->smb2.should_sign = true;
4323 generate_random_buffer((uint8_t *)&session->smb2.channel_nonce,
4324 sizeof(session->smb2.channel_nonce));
4325 session->smb2.channel_next = 1;
4327 return NT_STATUS_OK;
4330 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4331 struct smbXcli_session *session1,
4332 struct smbXcli_conn *conn,
4333 struct smbXcli_session **_session2)
4335 struct smbXcli_session *session2;
4337 if (session1->smb2.signing_key.length == 0) {
4338 return NT_STATUS_INVALID_PARAMETER_MIX;
4341 if (session1->smb2.channel_next == 0) {
4342 return NT_STATUS_INVALID_PARAMETER_MIX;
4346 return NT_STATUS_INVALID_PARAMETER_MIX;
4349 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4350 if (session2 == NULL) {
4351 return NT_STATUS_NO_MEMORY;
4353 session2->smb2.session_id = session1->smb2.session_id;
4354 session2->smb2.session_flags = session1->smb2.session_flags;
4356 session2->smb2.signing_key = data_blob_dup_talloc(session2,
4357 session1->smb2.signing_key);
4358 if (session2->smb2.signing_key.data == NULL) {
4359 return NT_STATUS_NO_MEMORY;
4362 session2->smb2.application_key = data_blob_dup_talloc(session2,
4363 session1->smb2.application_key);
4364 if (session2->smb2.application_key.data == NULL) {
4365 return NT_STATUS_NO_MEMORY;
4368 session2->smb2.should_sign = session1->smb2.should_sign;
4369 session2->smb2.should_encrypt = session1->smb2.should_encrypt;
4371 session2->smb2.channel_nonce = session1->smb2.channel_nonce;
4372 session2->smb2.channel_nonce += session1->smb2.channel_next;
4373 session1->smb2.channel_next++;
4375 session2->smb2.encryption_key = data_blob_dup_talloc(session2,
4376 session1->smb2.encryption_key);
4377 if (session2->smb2.encryption_key.data == NULL) {
4378 return NT_STATUS_NO_MEMORY;
4381 session2->smb2.decryption_key = data_blob_dup_talloc(session2,
4382 session1->smb2.decryption_key);
4383 if (session2->smb2.decryption_key.data == NULL) {
4384 return NT_STATUS_NO_MEMORY;
4387 talloc_set_destructor(session2, smbXcli_session_destructor);
4388 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4389 session2->conn = conn;
4391 *_session2 = session2;
4392 return NT_STATUS_OK;
4395 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4396 const DATA_BLOB _channel_key,
4397 const struct iovec *recv_iov)
4399 struct smbXcli_conn *conn = session->conn;
4400 uint8_t channel_key[16];
4404 return NT_STATUS_INVALID_PARAMETER_MIX;
4407 if (session->smb2.channel_signing_key.length != 0) {
4408 return NT_STATUS_INVALID_PARAMETER_MIX;
4411 ZERO_STRUCT(channel_key);
4412 memcpy(channel_key, _channel_key.data,
4413 MIN(_channel_key.length, sizeof(channel_key)));
4415 session->smb2.channel_signing_key = data_blob_talloc(session,
4417 sizeof(channel_key));
4418 if (session->smb2.channel_signing_key.data == NULL) {
4419 ZERO_STRUCT(channel_key);
4420 return NT_STATUS_NO_MEMORY;
4423 if (conn->protocol >= PROTOCOL_SMB2_24) {
4424 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4425 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4427 smb2_key_derivation(channel_key, sizeof(channel_key),
4428 label.data, label.length,
4429 context.data, context.length,
4430 session->smb2.channel_signing_key.data);
4432 ZERO_STRUCT(channel_key);
4434 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4435 session->conn->protocol,
4437 if (!NT_STATUS_IS_OK(status)) {
4441 return NT_STATUS_OK;