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;
140 DATA_BLOB channel_signing_key;
144 struct smbXcli_req_state {
145 struct tevent_context *ev;
146 struct smbXcli_conn *conn;
147 struct smbXcli_session *session; /* maybe NULL */
149 uint8_t length_hdr[4];
156 /* Space for the header including the wct */
157 uint8_t hdr[HDR_VWV];
160 * For normal requests, smb1cli_req_send chooses a mid.
161 * SecondaryV trans requests need to use the mid of the primary
162 * request, so we need a place to store it.
163 * Assume it is set if != 0.
168 uint8_t bytecount_buf[2];
170 #define MAX_SMB_IOV 10
171 /* length_hdr, hdr, words, byte_count, buffers */
172 struct iovec iov[1 + 3 + MAX_SMB_IOV];
177 struct tevent_req **chained_requests;
180 NTSTATUS recv_status;
181 /* always an array of 3 talloc elements */
182 struct iovec *recv_iov;
186 const uint8_t *fixed;
192 uint8_t pad[7]; /* padding space for compounding */
194 /* always an array of 3 talloc elements */
195 struct iovec *recv_iov;
197 uint16_t credit_charge;
199 bool signing_skipped;
205 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
208 * NT_STATUS_OK, means we do not notify the callers
210 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
212 while (conn->sessions) {
213 conn->sessions->conn = NULL;
214 DLIST_REMOVE(conn->sessions, conn->sessions);
217 if (conn->smb1.trans_enc) {
218 TALLOC_FREE(conn->smb1.trans_enc);
224 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
226 const char *remote_name,
227 enum smb_signing_setting signing_state,
228 uint32_t smb1_capabilities,
229 struct GUID *client_guid,
230 uint32_t smb2_capabilities)
232 struct smbXcli_conn *conn = NULL;
234 struct sockaddr *sa = NULL;
238 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
244 conn->write_fd = dup(fd);
245 if (conn->write_fd == -1) {
249 conn->remote_name = talloc_strdup(conn, remote_name);
250 if (conn->remote_name == NULL) {
255 ss = (void *)&conn->local_ss;
256 sa = (struct sockaddr *)ss;
257 sa_length = sizeof(conn->local_ss);
258 ret = getsockname(fd, sa, &sa_length);
262 ss = (void *)&conn->remote_ss;
263 sa = (struct sockaddr *)ss;
264 sa_length = sizeof(conn->remote_ss);
265 ret = getpeername(fd, sa, &sa_length);
270 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
271 if (conn->outgoing == NULL) {
274 conn->pending = NULL;
276 conn->protocol = PROTOCOL_NONE;
278 switch (signing_state) {
279 case SMB_SIGNING_OFF:
281 conn->allow_signing = false;
282 conn->desire_signing = false;
283 conn->mandatory_signing = false;
285 case SMB_SIGNING_DEFAULT:
286 case SMB_SIGNING_IF_REQUIRED:
287 /* if the server requires it */
288 conn->allow_signing = true;
289 conn->desire_signing = false;
290 conn->mandatory_signing = false;
292 case SMB_SIGNING_REQUIRED:
294 conn->allow_signing = true;
295 conn->desire_signing = true;
296 conn->mandatory_signing = true;
300 conn->smb1.client.capabilities = smb1_capabilities;
301 conn->smb1.client.max_xmit = UINT16_MAX;
303 conn->smb1.capabilities = conn->smb1.client.capabilities;
304 conn->smb1.max_xmit = 1024;
308 /* initialise signing */
309 conn->smb1.signing = smb_signing_init(conn,
311 conn->desire_signing,
312 conn->mandatory_signing);
313 if (!conn->smb1.signing) {
317 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
318 if (conn->mandatory_signing) {
319 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
322 conn->smb2.client.guid = *client_guid;
324 conn->smb2.client.capabilities = smb2_capabilities;
326 conn->smb2.cur_credits = 1;
327 conn->smb2.max_credits = 0;
329 talloc_set_destructor(conn, smbXcli_conn_destructor);
333 if (conn->write_fd != -1) {
334 close(conn->write_fd);
340 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
346 if (conn->read_fd == -1) {
353 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
355 return conn->protocol;
358 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
360 if (conn->protocol >= PROTOCOL_SMB2_02) {
364 if (conn->smb1.capabilities & CAP_UNICODE) {
371 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
373 set_socket_options(conn->read_fd, options);
376 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
378 return &conn->local_ss;
381 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
383 return &conn->remote_ss;
386 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
388 return conn->remote_name;
391 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
393 if (conn->protocol >= PROTOCOL_SMB2_02) {
400 return conn->smb1.server.max_mux;
403 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
405 if (conn->protocol >= PROTOCOL_SMB2_02) {
406 return conn->smb2.server.system_time;
409 return conn->smb1.server.system_time;
412 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
414 if (conn->protocol >= PROTOCOL_SMB2_02) {
415 return &conn->smb2.server.gss_blob;
418 return &conn->smb1.server.gss_blob;
421 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
423 if (conn->protocol >= PROTOCOL_SMB2_02) {
424 return &conn->smb2.server.guid;
427 return &conn->smb1.server.guid;
430 struct smbXcli_conn_samba_suicide_state {
431 struct smbXcli_conn *conn;
436 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
438 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
439 struct tevent_context *ev,
440 struct smbXcli_conn *conn,
443 struct tevent_req *req, *subreq;
444 struct smbXcli_conn_samba_suicide_state *state;
446 req = tevent_req_create(mem_ctx, &state,
447 struct smbXcli_conn_samba_suicide_state);
452 SIVAL(state->buf, 4, 0x74697865);
453 SCVAL(state->buf, 8, exitcode);
454 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
456 state->iov.iov_base = state->buf;
457 state->iov.iov_len = sizeof(state->buf);
459 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
460 false, &state->iov, 1);
461 if (tevent_req_nomem(subreq, req)) {
462 return tevent_req_post(req, ev);
464 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
468 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
470 struct tevent_req *req = tevent_req_callback_data(
471 subreq, struct tevent_req);
472 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
473 req, struct smbXcli_conn_samba_suicide_state);
477 nwritten = writev_recv(subreq, &err);
479 if (nwritten == -1) {
480 NTSTATUS status = map_nt_error_from_unix_common(err);
481 smbXcli_conn_disconnect(state->conn, status);
484 tevent_req_done(req);
487 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
489 return tevent_req_simple_recv_ntstatus(req);
492 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
495 TALLOC_CTX *frame = talloc_stackframe();
496 struct tevent_context *ev;
497 struct tevent_req *req;
498 NTSTATUS status = NT_STATUS_NO_MEMORY;
501 if (smbXcli_conn_has_async_calls(conn)) {
503 * Can't use sync call while an async call is in flight
505 status = NT_STATUS_INVALID_PARAMETER_MIX;
508 ev = tevent_context_init(frame);
512 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
516 ok = tevent_req_poll(req, ev);
518 status = map_nt_error_from_unix_common(errno);
521 status = smbXcli_conn_samba_suicide_recv(req);
527 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
529 return conn->smb1.capabilities;
532 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
534 return conn->smb1.max_xmit;
537 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
539 return conn->smb1.server.session_key;
542 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
544 return conn->smb1.server.challenge;
547 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
549 return conn->smb1.server.security_mode;
552 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
554 return conn->smb1.server.readbraw;
557 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
559 return conn->smb1.server.writebraw;
562 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
564 return conn->smb1.server.lockread;
567 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
569 return conn->smb1.server.writeunlock;
572 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
574 return conn->smb1.server.time_zone;
577 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
578 const DATA_BLOB user_session_key,
579 const DATA_BLOB response)
581 return smb_signing_activate(conn->smb1.signing,
586 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
587 const uint8_t *buf, uint32_t seqnum)
589 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
592 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
594 return smb_signing_is_active(conn->smb1.signing);
597 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
598 struct smb_trans_enc_state *es)
600 /* Replace the old state, if any. */
601 if (conn->smb1.trans_enc) {
602 TALLOC_FREE(conn->smb1.trans_enc);
604 conn->smb1.trans_enc = es;
607 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
609 return common_encryption_on(conn->smb1.trans_enc);
613 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
615 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
616 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
618 if (NT_STATUS_IS_OK(status)) {
622 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
626 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
630 * Is the SMB command able to hold an AND_X successor
631 * @param[in] cmd The SMB command in question
632 * @retval Can we add a chained request after "cmd"?
634 bool smb1cli_is_andx_req(uint8_t cmd)
654 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
656 size_t num_pending = talloc_array_length(conn->pending);
662 result = conn->smb1.mid++;
663 if ((result == 0) || (result == 0xffff)) {
667 for (i=0; i<num_pending; i++) {
668 if (result == smb1cli_req_mid(conn->pending[i])) {
673 if (i == num_pending) {
679 void smbXcli_req_unset_pending(struct tevent_req *req)
681 struct smbXcli_req_state *state =
683 struct smbXcli_req_state);
684 struct smbXcli_conn *conn = state->conn;
685 size_t num_pending = talloc_array_length(conn->pending);
688 if (state->smb1.mid != 0) {
690 * This is a [nt]trans[2] request which waits
691 * for more than one reply.
696 talloc_set_destructor(req, NULL);
698 if (num_pending == 1) {
700 * The pending read_smb tevent_req is a child of
701 * conn->pending. So if nothing is pending anymore, we need to
702 * delete the socket read fde.
704 TALLOC_FREE(conn->pending);
705 conn->read_smb_req = NULL;
709 for (i=0; i<num_pending; i++) {
710 if (req == conn->pending[i]) {
714 if (i == num_pending) {
716 * Something's seriously broken. Just returning here is the
717 * right thing nevertheless, the point of this routine is to
718 * remove ourselves from conn->pending.
724 * Remove ourselves from the conn->pending array
726 for (; i < (num_pending - 1); i++) {
727 conn->pending[i] = conn->pending[i+1];
731 * No NULL check here, we're shrinking by sizeof(void *), and
732 * talloc_realloc just adjusts the size for this.
734 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
739 static int smbXcli_req_destructor(struct tevent_req *req)
741 struct smbXcli_req_state *state =
743 struct smbXcli_req_state);
746 * Make sure we really remove it from
747 * the pending array on destruction.
750 smbXcli_req_unset_pending(req);
754 static bool smb1cli_req_cancel(struct tevent_req *req);
755 static bool smb2cli_req_cancel(struct tevent_req *req);
757 static bool smbXcli_req_cancel(struct tevent_req *req)
759 struct smbXcli_req_state *state =
761 struct smbXcli_req_state);
763 if (!smbXcli_conn_is_connected(state->conn)) {
767 if (state->conn->protocol == PROTOCOL_NONE) {
771 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
772 return smb2cli_req_cancel(req);
775 return smb1cli_req_cancel(req);
778 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
780 bool smbXcli_req_set_pending(struct tevent_req *req)
782 struct smbXcli_req_state *state =
784 struct smbXcli_req_state);
785 struct smbXcli_conn *conn;
786 struct tevent_req **pending;
791 if (!smbXcli_conn_is_connected(conn)) {
795 num_pending = talloc_array_length(conn->pending);
797 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
799 if (pending == NULL) {
802 pending[num_pending] = req;
803 conn->pending = pending;
804 talloc_set_destructor(req, smbXcli_req_destructor);
805 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
807 if (!smbXcli_conn_receive_next(conn)) {
809 * the caller should notify the current request
811 * And all other pending requests get notified
812 * by smbXcli_conn_disconnect().
814 smbXcli_req_unset_pending(req);
815 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
822 static void smbXcli_conn_received(struct tevent_req *subreq);
824 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
826 size_t num_pending = talloc_array_length(conn->pending);
827 struct tevent_req *req;
828 struct smbXcli_req_state *state;
830 if (conn->read_smb_req != NULL) {
834 if (num_pending == 0) {
835 if (conn->smb2.mid < UINT64_MAX) {
836 /* no more pending requests, so we are done for now */
841 * If there are no more SMB2 requests possible,
842 * because we are out of message ids,
843 * we need to disconnect.
845 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
849 req = conn->pending[0];
850 state = tevent_req_data(req, struct smbXcli_req_state);
853 * We're the first ones, add the read_smb request that waits for the
854 * answer from the server
856 conn->read_smb_req = read_smb_send(conn->pending,
859 if (conn->read_smb_req == NULL) {
862 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
866 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
868 tevent_queue_stop(conn->outgoing);
870 if (conn->read_fd != -1) {
871 close(conn->read_fd);
873 if (conn->write_fd != -1) {
874 close(conn->write_fd);
880 * Cancel all pending requests. We do not do a for-loop walking
881 * conn->pending because that array changes in
882 * smbXcli_req_unset_pending.
884 while (talloc_array_length(conn->pending) > 0) {
885 struct tevent_req *req;
886 struct smbXcli_req_state *state;
887 struct tevent_req **chain;
891 req = conn->pending[0];
892 state = tevent_req_data(req, struct smbXcli_req_state);
894 if (state->smb1.chained_requests == NULL) {
896 * We're dead. No point waiting for trans2
901 smbXcli_req_unset_pending(req);
903 if (NT_STATUS_IS_OK(status)) {
904 /* do not notify the callers */
909 * we need to defer the callback, because we may notify
910 * more then one caller.
912 tevent_req_defer_callback(req, state->ev);
913 tevent_req_nterror(req, status);
917 chain = talloc_move(conn, &state->smb1.chained_requests);
918 num_chained = talloc_array_length(chain);
920 for (i=0; i<num_chained; i++) {
922 state = tevent_req_data(req, struct smbXcli_req_state);
925 * We're dead. No point waiting for trans2
930 smbXcli_req_unset_pending(req);
932 if (NT_STATUS_IS_OK(status)) {
933 /* do not notify the callers */
938 * we need to defer the callback, because we may notify
939 * more than one caller.
941 tevent_req_defer_callback(req, state->ev);
942 tevent_req_nterror(req, status);
949 * Fetch a smb request's mid. Only valid after the request has been sent by
950 * smb1cli_req_send().
952 uint16_t smb1cli_req_mid(struct tevent_req *req)
954 struct smbXcli_req_state *state =
956 struct smbXcli_req_state);
958 if (state->smb1.mid != 0) {
959 return state->smb1.mid;
962 return SVAL(state->smb1.hdr, HDR_MID);
965 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
967 struct smbXcli_req_state *state =
969 struct smbXcli_req_state);
971 state->smb1.mid = mid;
974 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
976 struct smbXcli_req_state *state =
978 struct smbXcli_req_state);
980 return state->smb1.seqnum;
983 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
985 struct smbXcli_req_state *state =
987 struct smbXcli_req_state);
989 state->smb1.seqnum = seqnum;
992 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
996 for (i=0; i<count; i++) {
997 result += iov[i].iov_len;
1002 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1003 const struct iovec *iov,
1006 size_t len = smbXcli_iov_len(iov, count);
1011 buf = talloc_array(mem_ctx, uint8_t, len);
1016 for (i=0; i<count; i++) {
1017 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1018 copied += iov[i].iov_len;
1023 static void smb1cli_req_flags(enum protocol_types protocol,
1024 uint32_t smb1_capabilities,
1025 uint8_t smb_command,
1026 uint8_t additional_flags,
1027 uint8_t clear_flags,
1029 uint16_t additional_flags2,
1030 uint16_t clear_flags2,
1034 uint16_t flags2 = 0;
1036 if (protocol >= PROTOCOL_LANMAN1) {
1037 flags |= FLAG_CASELESS_PATHNAMES;
1038 flags |= FLAG_CANONICAL_PATHNAMES;
1041 if (protocol >= PROTOCOL_LANMAN2) {
1042 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1043 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1046 if (protocol >= PROTOCOL_NT1) {
1047 flags2 |= FLAGS2_IS_LONG_NAME;
1049 if (smb1_capabilities & CAP_UNICODE) {
1050 flags2 |= FLAGS2_UNICODE_STRINGS;
1052 if (smb1_capabilities & CAP_STATUS32) {
1053 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1055 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1056 flags2 |= FLAGS2_EXTENDED_SECURITY;
1060 flags |= additional_flags;
1061 flags &= ~clear_flags;
1062 flags2 |= additional_flags2;
1063 flags2 &= ~clear_flags2;
1069 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1071 static bool smb1cli_req_cancel(struct tevent_req *req)
1073 struct smbXcli_req_state *state =
1074 tevent_req_data(req,
1075 struct smbXcli_req_state);
1082 struct tevent_req *subreq;
1085 flags = CVAL(state->smb1.hdr, HDR_FLG);
1086 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1087 pid = SVAL(state->smb1.hdr, HDR_PID);
1088 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1089 tid = SVAL(state->smb1.hdr, HDR_TID);
1090 uid = SVAL(state->smb1.hdr, HDR_UID);
1091 mid = SVAL(state->smb1.hdr, HDR_MID);
1093 subreq = smb1cli_req_create(state, state->ev,
1101 0, NULL); /* bytes */
1102 if (subreq == NULL) {
1105 smb1cli_req_set_mid(subreq, mid);
1107 status = smb1cli_req_chain_submit(&subreq, 1);
1108 if (!NT_STATUS_IS_OK(status)) {
1109 TALLOC_FREE(subreq);
1112 smb1cli_req_set_mid(subreq, 0);
1114 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1119 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1121 /* we do not care about the result */
1122 TALLOC_FREE(subreq);
1125 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1126 struct tevent_context *ev,
1127 struct smbXcli_conn *conn,
1128 uint8_t smb_command,
1129 uint8_t additional_flags,
1130 uint8_t clear_flags,
1131 uint16_t additional_flags2,
1132 uint16_t clear_flags2,
1133 uint32_t timeout_msec,
1137 uint8_t wct, uint16_t *vwv,
1139 struct iovec *bytes_iov)
1141 struct tevent_req *req;
1142 struct smbXcli_req_state *state;
1144 uint16_t flags2 = 0;
1146 if (iov_count > MAX_SMB_IOV) {
1148 * Should not happen :-)
1153 req = tevent_req_create(mem_ctx, &state,
1154 struct smbXcli_req_state);
1161 state->smb1.recv_cmd = 0xFF;
1162 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1163 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1164 if (state->smb1.recv_iov == NULL) {
1169 smb1cli_req_flags(conn->protocol,
1170 conn->smb1.capabilities,
1179 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1180 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1181 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1182 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1183 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1184 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1185 SSVAL(state->smb1.hdr, HDR_TID, tid);
1186 SSVAL(state->smb1.hdr, HDR_PID, pid);
1187 SSVAL(state->smb1.hdr, HDR_UID, uid);
1188 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1189 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1191 state->smb1.vwv = vwv;
1193 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1195 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1196 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1197 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1198 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1199 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1200 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1201 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1202 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1204 if (iov_count != 0) {
1205 memcpy(&state->smb1.iov[4], bytes_iov,
1206 iov_count * sizeof(*bytes_iov));
1208 state->smb1.iov_count = iov_count + 4;
1210 if (timeout_msec > 0) {
1211 struct timeval endtime;
1213 endtime = timeval_current_ofs_msec(timeout_msec);
1214 if (!tevent_req_set_endtime(req, ev, endtime)) {
1219 switch (smb_command) {
1223 state->one_way = true;
1226 state->one_way = true;
1227 state->smb1.one_way_seqnum = true;
1231 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1232 state->one_way = true;
1240 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1241 struct iovec *iov, int iov_count,
1243 bool one_way_seqnum)
1245 TALLOC_CTX *frame = NULL;
1249 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1250 * iovec directly. MD5Update would do that just fine.
1253 if (iov_count < 4) {
1254 return NT_STATUS_INVALID_PARAMETER_MIX;
1256 if (iov[0].iov_len != NBT_HDR_SIZE) {
1257 return NT_STATUS_INVALID_PARAMETER_MIX;
1259 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1260 return NT_STATUS_INVALID_PARAMETER_MIX;
1262 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1263 return NT_STATUS_INVALID_PARAMETER_MIX;
1265 if (iov[3].iov_len != sizeof(uint16_t)) {
1266 return NT_STATUS_INVALID_PARAMETER_MIX;
1269 frame = talloc_stackframe();
1271 buf = smbXcli_iov_concat(frame, iov, iov_count);
1273 return NT_STATUS_NO_MEMORY;
1276 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1278 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1279 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1282 return NT_STATUS_OK;
1285 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1286 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1287 TALLOC_CTX *tmp_mem,
1290 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1291 struct smbXcli_req_state *state,
1292 struct iovec *iov, int iov_count)
1294 struct tevent_req *subreq;
1299 if (!smbXcli_conn_is_connected(state->conn)) {
1300 return NT_STATUS_CONNECTION_DISCONNECTED;
1303 if (state->conn->protocol > PROTOCOL_NT1) {
1304 return NT_STATUS_REVISION_MISMATCH;
1307 if (iov_count < 4) {
1308 return NT_STATUS_INVALID_PARAMETER_MIX;
1310 if (iov[0].iov_len != NBT_HDR_SIZE) {
1311 return NT_STATUS_INVALID_PARAMETER_MIX;
1313 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1314 return NT_STATUS_INVALID_PARAMETER_MIX;
1316 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1317 return NT_STATUS_INVALID_PARAMETER_MIX;
1319 if (iov[3].iov_len != sizeof(uint16_t)) {
1320 return NT_STATUS_INVALID_PARAMETER_MIX;
1323 cmd = CVAL(iov[1].iov_base, HDR_COM);
1324 if (cmd == SMBreadBraw) {
1325 if (smbXcli_conn_has_async_calls(state->conn)) {
1326 return NT_STATUS_INVALID_PARAMETER_MIX;
1328 state->conn->smb1.read_braw_req = req;
1331 if (state->smb1.mid != 0) {
1332 mid = state->smb1.mid;
1334 mid = smb1cli_alloc_mid(state->conn);
1336 SSVAL(iov[1].iov_base, HDR_MID, mid);
1338 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1340 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1341 &state->smb1.seqnum,
1342 state->smb1.one_way_seqnum);
1344 if (!NT_STATUS_IS_OK(status)) {
1349 * If we supported multiple encrytion contexts
1350 * here we'd look up based on tid.
1352 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1353 char *buf, *enc_buf;
1355 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1357 return NT_STATUS_NO_MEMORY;
1359 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1360 (char *)buf, &enc_buf);
1362 if (!NT_STATUS_IS_OK(status)) {
1363 DEBUG(0, ("Error in encrypting client message: %s\n",
1364 nt_errstr(status)));
1367 buf = (char *)talloc_memdup(state, enc_buf,
1368 smb_len_nbt(enc_buf)+4);
1371 return NT_STATUS_NO_MEMORY;
1373 iov[0].iov_base = (void *)buf;
1374 iov[0].iov_len = talloc_get_size(buf);
1378 if (state->conn->dispatch_incoming == NULL) {
1379 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1382 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1384 subreq = writev_send(state, state->ev, state->conn->outgoing,
1385 state->conn->write_fd, false, iov, iov_count);
1386 if (subreq == NULL) {
1387 return NT_STATUS_NO_MEMORY;
1389 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1390 return NT_STATUS_OK;
1393 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1394 struct tevent_context *ev,
1395 struct smbXcli_conn *conn,
1396 uint8_t smb_command,
1397 uint8_t additional_flags,
1398 uint8_t clear_flags,
1399 uint16_t additional_flags2,
1400 uint16_t clear_flags2,
1401 uint32_t timeout_msec,
1405 uint8_t wct, uint16_t *vwv,
1407 const uint8_t *bytes)
1409 struct tevent_req *req;
1413 iov.iov_base = discard_const_p(void, bytes);
1414 iov.iov_len = num_bytes;
1416 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1417 additional_flags, clear_flags,
1418 additional_flags2, clear_flags2,
1425 if (!tevent_req_is_in_progress(req)) {
1426 return tevent_req_post(req, ev);
1428 status = smb1cli_req_chain_submit(&req, 1);
1429 if (tevent_req_nterror(req, status)) {
1430 return tevent_req_post(req, ev);
1435 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1437 struct tevent_req *req =
1438 tevent_req_callback_data(subreq,
1440 struct smbXcli_req_state *state =
1441 tevent_req_data(req,
1442 struct smbXcli_req_state);
1446 nwritten = writev_recv(subreq, &err);
1447 TALLOC_FREE(subreq);
1448 if (nwritten == -1) {
1449 NTSTATUS status = map_nt_error_from_unix_common(err);
1450 smbXcli_conn_disconnect(state->conn, status);
1454 if (state->one_way) {
1455 state->inbuf = NULL;
1456 tevent_req_done(req);
1460 if (!smbXcli_req_set_pending(req)) {
1461 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1466 static void smbXcli_conn_received(struct tevent_req *subreq)
1468 struct smbXcli_conn *conn =
1469 tevent_req_callback_data(subreq,
1470 struct smbXcli_conn);
1471 TALLOC_CTX *frame = talloc_stackframe();
1477 if (subreq != conn->read_smb_req) {
1478 DEBUG(1, ("Internal error: cli_smb_received called with "
1479 "unexpected subreq\n"));
1480 status = NT_STATUS_INTERNAL_ERROR;
1481 smbXcli_conn_disconnect(conn, status);
1485 conn->read_smb_req = NULL;
1487 received = read_smb_recv(subreq, frame, &inbuf, &err);
1488 TALLOC_FREE(subreq);
1489 if (received == -1) {
1490 status = map_nt_error_from_unix_common(err);
1491 smbXcli_conn_disconnect(conn, status);
1496 status = conn->dispatch_incoming(conn, frame, inbuf);
1498 if (NT_STATUS_IS_OK(status)) {
1500 * We should not do any more processing
1501 * as the dispatch function called
1502 * tevent_req_done().
1505 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1507 * We got an error, so notify all pending requests
1509 smbXcli_conn_disconnect(conn, status);
1514 * We got NT_STATUS_RETRY, so we may ask for a
1515 * next incoming pdu.
1517 if (!smbXcli_conn_receive_next(conn)) {
1518 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1522 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1523 struct iovec **piov, int *pnum_iov)
1534 buflen = smb_len_nbt(buf);
1537 hdr = buf + NBT_HDR_SIZE;
1539 if (buflen < MIN_SMB_SIZE) {
1540 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1544 * This returns iovec elements in the following order:
1559 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1561 return NT_STATUS_NO_MEMORY;
1563 iov[0].iov_base = hdr;
1564 iov[0].iov_len = HDR_WCT;
1567 cmd = CVAL(hdr, HDR_COM);
1571 size_t len = buflen - taken;
1573 struct iovec *iov_tmp;
1580 * we need at least WCT and BCC
1582 needed = sizeof(uint8_t) + sizeof(uint16_t);
1584 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1585 __location__, (int)len, (int)needed));
1590 * Now we check if the specified words are there
1592 wct = CVAL(hdr, wct_ofs);
1593 needed += wct * sizeof(uint16_t);
1595 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1596 __location__, (int)len, (int)needed));
1601 * Now we check if the specified bytes are there
1603 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1604 bcc = SVAL(hdr, bcc_ofs);
1605 needed += bcc * sizeof(uint8_t);
1607 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1608 __location__, (int)len, (int)needed));
1613 * we allocate 2 iovec structures for words and bytes
1615 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1617 if (iov_tmp == NULL) {
1619 return NT_STATUS_NO_MEMORY;
1622 cur = &iov[num_iov];
1625 cur[0].iov_len = wct * sizeof(uint16_t);
1626 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1627 cur[1].iov_len = bcc * sizeof(uint8_t);
1628 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1632 if (!smb1cli_is_andx_req(cmd)) {
1634 * If the current command does not have AndX chanining
1640 if (wct == 0 && bcc == 0) {
1642 * An empty response also ends the chain,
1643 * most likely with an error.
1649 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1650 __location__, (int)wct, (int)cmd));
1653 cmd = CVAL(cur[0].iov_base, 0);
1656 * If it is the end of the chain we are also done.
1660 wct_ofs = SVAL(cur[0].iov_base, 2);
1662 if (wct_ofs < taken) {
1663 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1665 if (wct_ofs > buflen) {
1666 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1670 * we consumed everything up to the start of the next
1676 remaining = buflen - taken;
1678 if (remaining > 0 && num_iov >= 3) {
1680 * The last DATA block gets the remaining
1681 * bytes, this is needed to support
1682 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1684 iov[num_iov-1].iov_len += remaining;
1688 *pnum_iov = num_iov;
1689 return NT_STATUS_OK;
1693 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1696 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1697 TALLOC_CTX *tmp_mem,
1700 struct tevent_req *req;
1701 struct smbXcli_req_state *state;
1708 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1709 struct iovec *iov = NULL;
1711 struct tevent_req **chain = NULL;
1712 size_t num_chained = 0;
1713 size_t num_responses = 0;
1715 if (conn->smb1.read_braw_req != NULL) {
1716 req = conn->smb1.read_braw_req;
1717 conn->smb1.read_braw_req = NULL;
1718 state = tevent_req_data(req, struct smbXcli_req_state);
1720 smbXcli_req_unset_pending(req);
1722 if (state->smb1.recv_iov == NULL) {
1724 * For requests with more than
1725 * one response, we have to readd the
1728 state->smb1.recv_iov = talloc_zero_array(state,
1731 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1732 return NT_STATUS_OK;
1736 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1737 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1738 ZERO_STRUCT(state->smb1.recv_iov[1]);
1739 ZERO_STRUCT(state->smb1.recv_iov[2]);
1741 state->smb1.recv_cmd = SMBreadBraw;
1742 state->smb1.recv_status = NT_STATUS_OK;
1743 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1745 tevent_req_done(req);
1746 return NT_STATUS_OK;
1749 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1750 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1751 DEBUG(10, ("Got non-SMB PDU\n"));
1752 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1756 * If we supported multiple encrytion contexts
1757 * here we'd look up based on tid.
1759 if (common_encryption_on(conn->smb1.trans_enc)
1760 && (CVAL(inbuf, 0) == 0)) {
1761 uint16_t enc_ctx_num;
1763 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1764 if (!NT_STATUS_IS_OK(status)) {
1765 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1766 nt_errstr(status)));
1770 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1771 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1773 conn->smb1.trans_enc->enc_ctx_num));
1774 return NT_STATUS_INVALID_HANDLE;
1777 status = common_decrypt_buffer(conn->smb1.trans_enc,
1779 if (!NT_STATUS_IS_OK(status)) {
1780 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1781 nt_errstr(status)));
1786 mid = SVAL(inhdr, HDR_MID);
1787 num_pending = talloc_array_length(conn->pending);
1789 for (i=0; i<num_pending; i++) {
1790 if (mid == smb1cli_req_mid(conn->pending[i])) {
1794 if (i == num_pending) {
1795 /* Dump unexpected reply */
1796 return NT_STATUS_RETRY;
1799 oplock_break = false;
1801 if (mid == 0xffff) {
1803 * Paranoia checks that this is really an oplock break request.
1805 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1806 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1807 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1808 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1809 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1811 if (!oplock_break) {
1812 /* Dump unexpected reply */
1813 return NT_STATUS_RETRY;
1817 req = conn->pending[i];
1818 state = tevent_req_data(req, struct smbXcli_req_state);
1820 if (!oplock_break /* oplock breaks are not signed */
1821 && !smb_signing_check_pdu(conn->smb1.signing,
1822 inbuf, state->smb1.seqnum+1)) {
1823 DEBUG(10, ("cli_check_sign_mac failed\n"));
1824 return NT_STATUS_ACCESS_DENIED;
1827 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1829 if (!NT_STATUS_IS_OK(status)) {
1830 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1831 nt_errstr(status)));
1835 cmd = CVAL(inhdr, HDR_COM);
1836 status = smb1cli_pull_raw_error(inhdr);
1838 if (state->smb1.chained_requests == NULL) {
1840 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1843 smbXcli_req_unset_pending(req);
1845 if (state->smb1.recv_iov == NULL) {
1847 * For requests with more than
1848 * one response, we have to readd the
1851 state->smb1.recv_iov = talloc_zero_array(state,
1854 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1855 return NT_STATUS_OK;
1859 state->smb1.recv_cmd = cmd;
1860 state->smb1.recv_status = status;
1861 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1863 state->smb1.recv_iov[0] = iov[0];
1864 state->smb1.recv_iov[1] = iov[1];
1865 state->smb1.recv_iov[2] = iov[2];
1867 if (talloc_array_length(conn->pending) == 0) {
1868 tevent_req_done(req);
1869 return NT_STATUS_OK;
1872 tevent_req_defer_callback(req, state->ev);
1873 tevent_req_done(req);
1874 return NT_STATUS_RETRY;
1877 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1878 num_chained = talloc_array_length(chain);
1879 num_responses = (num_iov - 1)/2;
1881 if (num_responses > num_chained) {
1882 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1885 for (i=0; i<num_chained; i++) {
1886 size_t iov_idx = 1 + (i*2);
1887 struct iovec *cur = &iov[iov_idx];
1891 state = tevent_req_data(req, struct smbXcli_req_state);
1893 smbXcli_req_unset_pending(req);
1896 * as we finish multiple requests here
1897 * we need to defer the callbacks as
1898 * they could destroy our current stack state.
1900 tevent_req_defer_callback(req, state->ev);
1902 if (i >= num_responses) {
1903 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1907 if (state->smb1.recv_iov == NULL) {
1909 * For requests with more than
1910 * one response, we have to readd the
1913 state->smb1.recv_iov = talloc_zero_array(state,
1916 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1921 state->smb1.recv_cmd = cmd;
1923 if (i == (num_responses - 1)) {
1925 * The last request in the chain gets the status
1927 state->smb1.recv_status = status;
1929 cmd = CVAL(cur[0].iov_base, 0);
1930 state->smb1.recv_status = NT_STATUS_OK;
1933 state->inbuf = inbuf;
1936 * Note: here we use talloc_reference() in a way
1937 * that does not expose it to the caller.
1939 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1940 if (tevent_req_nomem(inbuf_ref, req)) {
1944 /* copy the related buffers */
1945 state->smb1.recv_iov[0] = iov[0];
1946 state->smb1.recv_iov[1] = cur[0];
1947 state->smb1.recv_iov[2] = cur[1];
1949 tevent_req_done(req);
1952 return NT_STATUS_RETRY;
1955 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1956 TALLOC_CTX *mem_ctx,
1957 struct iovec **piov,
1961 uint32_t *pvwv_offset,
1962 uint32_t *pnum_bytes,
1964 uint32_t *pbytes_offset,
1966 const struct smb1cli_req_expected_response *expected,
1967 size_t num_expected)
1969 struct smbXcli_req_state *state =
1970 tevent_req_data(req,
1971 struct smbXcli_req_state);
1972 NTSTATUS status = NT_STATUS_OK;
1973 struct iovec *recv_iov = NULL;
1974 uint8_t *hdr = NULL;
1976 uint32_t vwv_offset = 0;
1977 uint16_t *vwv = NULL;
1978 uint32_t num_bytes = 0;
1979 uint32_t bytes_offset = 0;
1980 uint8_t *bytes = NULL;
1982 bool found_status = false;
1983 bool found_size = false;
1997 if (pvwv_offset != NULL) {
2000 if (pnum_bytes != NULL) {
2003 if (pbytes != NULL) {
2006 if (pbytes_offset != NULL) {
2009 if (pinbuf != NULL) {
2013 if (state->inbuf != NULL) {
2014 recv_iov = state->smb1.recv_iov;
2015 state->smb1.recv_iov = NULL;
2016 if (state->smb1.recv_cmd != SMBreadBraw) {
2017 hdr = (uint8_t *)recv_iov[0].iov_base;
2018 wct = recv_iov[1].iov_len/2;
2019 vwv = (uint16_t *)recv_iov[1].iov_base;
2020 vwv_offset = PTR_DIFF(vwv, hdr);
2021 num_bytes = recv_iov[2].iov_len;
2022 bytes = (uint8_t *)recv_iov[2].iov_base;
2023 bytes_offset = PTR_DIFF(bytes, hdr);
2027 if (tevent_req_is_nterror(req, &status)) {
2028 for (i=0; i < num_expected; i++) {
2029 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2030 found_status = true;
2036 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2042 if (num_expected == 0) {
2043 found_status = true;
2047 status = state->smb1.recv_status;
2049 for (i=0; i < num_expected; i++) {
2050 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2054 found_status = true;
2055 if (expected[i].wct == 0) {
2060 if (expected[i].wct == wct) {
2066 if (!found_status) {
2071 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2075 *piov = talloc_move(mem_ctx, &recv_iov);
2087 if (pvwv_offset != NULL) {
2088 *pvwv_offset = vwv_offset;
2090 if (pnum_bytes != NULL) {
2091 *pnum_bytes = num_bytes;
2093 if (pbytes != NULL) {
2096 if (pbytes_offset != NULL) {
2097 *pbytes_offset = bytes_offset;
2099 if (pinbuf != NULL) {
2100 *pinbuf = state->inbuf;
2106 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2113 for (i=0; i<num_reqs; i++) {
2114 struct smbXcli_req_state *state;
2115 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2116 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2117 state->smb1.iov_count-2);
2118 wct_ofs = (wct_ofs + 3) & ~3;
2123 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2125 struct smbXcli_req_state *first_state =
2126 tevent_req_data(reqs[0],
2127 struct smbXcli_req_state);
2128 struct smbXcli_req_state *state;
2130 size_t chain_padding = 0;
2132 struct iovec *iov = NULL;
2133 struct iovec *this_iov;
2137 if (num_reqs == 1) {
2138 return smb1cli_req_writev_submit(reqs[0], first_state,
2139 first_state->smb1.iov,
2140 first_state->smb1.iov_count);
2144 for (i=0; i<num_reqs; i++) {
2145 if (!tevent_req_is_in_progress(reqs[i])) {
2146 return NT_STATUS_INTERNAL_ERROR;
2149 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2151 if (state->smb1.iov_count < 4) {
2152 return NT_STATUS_INVALID_PARAMETER_MIX;
2157 * The NBT and SMB header
2170 iovlen += state->smb1.iov_count - 2;
2173 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2175 return NT_STATUS_NO_MEMORY;
2178 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2179 first_state, reqs, sizeof(*reqs) * num_reqs);
2180 if (first_state->smb1.chained_requests == NULL) {
2182 return NT_STATUS_NO_MEMORY;
2185 wct_offset = HDR_WCT;
2188 for (i=0; i<num_reqs; i++) {
2189 size_t next_padding = 0;
2192 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2194 if (i < num_reqs-1) {
2195 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2196 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2198 TALLOC_FREE(first_state->smb1.chained_requests);
2199 return NT_STATUS_INVALID_PARAMETER_MIX;
2203 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2204 state->smb1.iov_count-2) + 1;
2205 if ((wct_offset % 4) != 0) {
2206 next_padding = 4 - (wct_offset % 4);
2208 wct_offset += next_padding;
2209 vwv = state->smb1.vwv;
2211 if (i < num_reqs-1) {
2212 struct smbXcli_req_state *next_state =
2213 tevent_req_data(reqs[i+1],
2214 struct smbXcli_req_state);
2215 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2217 SSVAL(vwv+1, 0, wct_offset);
2218 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2219 /* properly end the chain */
2220 SCVAL(vwv+0, 0, 0xff);
2221 SCVAL(vwv+0, 1, 0xff);
2227 * The NBT and SMB header
2229 this_iov[0] = state->smb1.iov[0];
2230 this_iov[1] = state->smb1.iov[1];
2234 * This one is a bit subtle. We have to add
2235 * chain_padding bytes between the requests, and we
2236 * have to also include the wct field of the
2237 * subsequent requests. We use the subsequent header
2238 * for the padding, it contains the wct field in its
2241 this_iov[0].iov_len = chain_padding+1;
2242 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2243 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2244 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2249 * copy the words and bytes
2251 memcpy(this_iov, state->smb1.iov+2,
2252 sizeof(struct iovec) * (state->smb1.iov_count-2));
2253 this_iov += state->smb1.iov_count - 2;
2254 chain_padding = next_padding;
2257 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2258 if (nbt_len > first_state->conn->smb1.max_xmit) {
2260 TALLOC_FREE(first_state->smb1.chained_requests);
2261 return NT_STATUS_INVALID_PARAMETER_MIX;
2264 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2265 if (!NT_STATUS_IS_OK(status)) {
2267 TALLOC_FREE(first_state->smb1.chained_requests);
2271 return NT_STATUS_OK;
2274 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2276 return ((tevent_queue_length(conn->outgoing) != 0)
2277 || (talloc_array_length(conn->pending) != 0));
2280 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2282 return conn->smb2.server.capabilities;
2285 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2287 return conn->smb2.server.security_mode;
2290 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2292 return conn->smb2.server.max_trans_size;
2295 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2297 return conn->smb2.server.max_read_size;
2300 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2302 return conn->smb2.server.max_write_size;
2305 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2306 uint16_t max_credits)
2308 conn->smb2.max_credits = max_credits;
2311 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2313 static bool smb2cli_req_cancel(struct tevent_req *req)
2315 struct smbXcli_req_state *state =
2316 tevent_req_data(req,
2317 struct smbXcli_req_state);
2318 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2319 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2320 uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2321 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2322 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2323 struct smbXcli_session *session = state->session;
2324 uint8_t *fixed = state->smb2.pad;
2325 uint16_t fixed_len = 4;
2326 struct tevent_req *subreq;
2327 struct smbXcli_req_state *substate;
2330 SSVAL(fixed, 0, 0x04);
2333 subreq = smb2cli_req_create(state, state->ev,
2341 if (subreq == NULL) {
2344 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2346 if (flags & SMB2_HDR_FLAG_ASYNC) {
2350 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2351 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2352 SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2353 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2354 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2356 status = smb2cli_req_compound_submit(&subreq, 1);
2357 if (!NT_STATUS_IS_OK(status)) {
2358 TALLOC_FREE(subreq);
2362 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2367 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2369 /* we do not care about the result */
2370 TALLOC_FREE(subreq);
2373 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2374 struct tevent_context *ev,
2375 struct smbXcli_conn *conn,
2377 uint32_t additional_flags,
2378 uint32_t clear_flags,
2379 uint32_t timeout_msec,
2382 struct smbXcli_session *session,
2383 const uint8_t *fixed,
2388 struct tevent_req *req;
2389 struct smbXcli_req_state *state;
2393 req = tevent_req_create(mem_ctx, &state,
2394 struct smbXcli_req_state);
2401 state->session = session;
2404 uid = session->smb2.session_id;
2407 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2408 if (state->smb2.recv_iov == NULL) {
2413 flags |= additional_flags;
2414 flags &= ~clear_flags;
2416 state->smb2.fixed = fixed;
2417 state->smb2.fixed_len = fixed_len;
2418 state->smb2.dyn = dyn;
2419 state->smb2.dyn_len = dyn_len;
2421 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2422 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2423 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2424 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2425 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2426 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2427 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2430 case SMB2_OP_CANCEL:
2431 state->one_way = true;
2435 * If this is a dummy request, it will have
2436 * UINT64_MAX as message id.
2437 * If we send on break acknowledgement,
2438 * this gets overwritten later.
2440 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2444 if (timeout_msec > 0) {
2445 struct timeval endtime;
2447 endtime = timeval_current_ofs_msec(timeout_msec);
2448 if (!tevent_req_set_endtime(req, ev, endtime)) {
2456 void smb2cli_req_set_notify_async(struct tevent_req *req)
2458 struct smbXcli_req_state *state =
2459 tevent_req_data(req,
2460 struct smbXcli_req_state);
2462 state->smb2.notify_async = true;
2465 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2466 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2467 TALLOC_CTX *tmp_mem,
2470 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2473 struct smbXcli_req_state *state;
2474 struct tevent_req *subreq;
2476 int i, num_iov, nbt_len;
2479 * 1 for the nbt length
2480 * per request: HDR, fixed, dyn, padding
2481 * -1 because the last one does not need padding
2484 iov = talloc_array(reqs[0], struct iovec, 1 + 4*num_reqs - 1);
2486 return NT_STATUS_NO_MEMORY;
2492 for (i=0; i<num_reqs; i++) {
2501 const DATA_BLOB *signing_key = NULL;
2503 if (!tevent_req_is_in_progress(reqs[i])) {
2504 return NT_STATUS_INTERNAL_ERROR;
2507 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2509 if (!smbXcli_conn_is_connected(state->conn)) {
2510 return NT_STATUS_CONNECTION_DISCONNECTED;
2513 if ((state->conn->protocol != PROTOCOL_NONE) &&
2514 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2515 return NT_STATUS_REVISION_MISMATCH;
2518 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2519 if (opcode == SMB2_OP_CANCEL) {
2523 avail = UINT64_MAX - state->conn->smb2.mid;
2525 return NT_STATUS_CONNECTION_ABORTED;
2528 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2529 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2534 charge = MAX(state->smb2.credit_charge, charge);
2536 avail = MIN(avail, state->conn->smb2.cur_credits);
2537 if (avail < charge) {
2538 return NT_STATUS_INTERNAL_ERROR;
2542 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2543 credits = state->conn->smb2.max_credits -
2544 state->conn->smb2.cur_credits;
2546 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2550 mid = state->conn->smb2.mid;
2551 state->conn->smb2.mid += charge;
2552 state->conn->smb2.cur_credits -= charge;
2554 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2555 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2557 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2558 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2562 iov[num_iov].iov_base = state->smb2.hdr;
2563 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2566 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2567 iov[num_iov].iov_len = state->smb2.fixed_len;
2570 if (state->smb2.dyn != NULL) {
2571 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2572 iov[num_iov].iov_len = state->smb2.dyn_len;
2576 reqlen = sizeof(state->smb2.hdr);
2577 reqlen += state->smb2.fixed_len;
2578 reqlen += state->smb2.dyn_len;
2580 if (i < num_reqs-1) {
2581 if ((reqlen % 8) > 0) {
2582 uint8_t pad = 8 - (reqlen % 8);
2583 iov[num_iov].iov_base = state->smb2.pad;
2584 iov[num_iov].iov_len = pad;
2588 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2592 if (state->session) {
2593 bool should_sign = state->session->smb2.should_sign;
2595 if (opcode == SMB2_OP_SESSSETUP &&
2596 state->session->smb2.signing_key.length != 0) {
2601 * We prefer the channel signing key if it is
2605 signing_key = &state->session->smb2.channel_signing_key;
2609 * If it is a channel binding, we already have the main
2610 * signing key and try that one.
2612 if (signing_key && signing_key->length == 0) {
2613 signing_key = &state->session->smb2.signing_key;
2617 * If we do not have any session key yet, we skip the
2618 * signing of SMB2_OP_SESSSETUP requests.
2620 if (signing_key && signing_key->length == 0) {
2628 status = smb2_signing_sign_pdu(*signing_key,
2629 state->session->conn->protocol,
2630 &iov[hdr_iov], num_iov - hdr_iov);
2631 if (!NT_STATUS_IS_OK(status)) {
2636 ret = smbXcli_req_set_pending(reqs[i]);
2638 return NT_STATUS_NO_MEMORY;
2642 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2643 _smb_setlen_tcp(state->length_hdr, nbt_len);
2644 iov[0].iov_base = state->length_hdr;
2645 iov[0].iov_len = sizeof(state->length_hdr);
2647 if (state->conn->dispatch_incoming == NULL) {
2648 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2651 subreq = writev_send(state, state->ev, state->conn->outgoing,
2652 state->conn->write_fd, false, iov, num_iov);
2653 if (subreq == NULL) {
2654 return NT_STATUS_NO_MEMORY;
2656 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2657 return NT_STATUS_OK;
2660 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2662 struct smbXcli_req_state *state =
2663 tevent_req_data(req,
2664 struct smbXcli_req_state);
2666 state->smb2.credit_charge = charge;
2669 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2670 struct tevent_context *ev,
2671 struct smbXcli_conn *conn,
2673 uint32_t additional_flags,
2674 uint32_t clear_flags,
2675 uint32_t timeout_msec,
2678 struct smbXcli_session *session,
2679 const uint8_t *fixed,
2684 struct tevent_req *req;
2687 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2688 additional_flags, clear_flags,
2691 fixed, fixed_len, dyn, dyn_len);
2695 if (!tevent_req_is_in_progress(req)) {
2696 return tevent_req_post(req, ev);
2698 status = smb2cli_req_compound_submit(&req, 1);
2699 if (tevent_req_nterror(req, status)) {
2700 return tevent_req_post(req, ev);
2705 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2707 struct tevent_req *req =
2708 tevent_req_callback_data(subreq,
2710 struct smbXcli_req_state *state =
2711 tevent_req_data(req,
2712 struct smbXcli_req_state);
2716 nwritten = writev_recv(subreq, &err);
2717 TALLOC_FREE(subreq);
2718 if (nwritten == -1) {
2719 /* here, we need to notify all pending requests */
2720 NTSTATUS status = map_nt_error_from_unix_common(err);
2721 smbXcli_conn_disconnect(state->conn, status);
2726 static NTSTATUS smb2cli_inbuf_parse_compound(uint8_t *buf, TALLOC_CTX *mem_ctx,
2727 struct iovec **piov, int *pnum_iov)
2737 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2739 return NT_STATUS_NO_MEMORY;
2742 buflen = smb_len_tcp(buf);
2744 first_hdr = buf + NBT_HDR_SIZE;
2746 while (taken < buflen) {
2747 size_t len = buflen - taken;
2748 uint8_t *hdr = first_hdr + taken;
2751 size_t next_command_ofs;
2753 struct iovec *iov_tmp;
2756 * We need the header plus the body length field
2759 if (len < SMB2_HDR_BODY + 2) {
2760 DEBUG(10, ("%d bytes left, expected at least %d\n",
2761 (int)len, SMB2_HDR_BODY));
2764 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2765 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2769 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2770 DEBUG(10, ("Got HDR len %d, expected %d\n",
2771 SVAL(hdr, 4), SMB2_HDR_BODY));
2776 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2777 body_size = SVAL(hdr, SMB2_HDR_BODY);
2779 if (next_command_ofs != 0) {
2780 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2783 if (next_command_ofs > full_size) {
2786 full_size = next_command_ofs;
2788 if (body_size < 2) {
2791 body_size &= 0xfffe;
2793 if (body_size > (full_size - SMB2_HDR_BODY)) {
2797 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2799 if (iov_tmp == NULL) {
2801 return NT_STATUS_NO_MEMORY;
2804 cur = &iov[num_iov];
2807 cur[0].iov_base = hdr;
2808 cur[0].iov_len = SMB2_HDR_BODY;
2809 cur[1].iov_base = hdr + SMB2_HDR_BODY;
2810 cur[1].iov_len = body_size;
2811 cur[2].iov_base = hdr + SMB2_HDR_BODY + body_size;
2812 cur[2].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2818 *pnum_iov = num_iov;
2819 return NT_STATUS_OK;
2823 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2826 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2829 size_t num_pending = talloc_array_length(conn->pending);
2832 for (i=0; i<num_pending; i++) {
2833 struct tevent_req *req = conn->pending[i];
2834 struct smbXcli_req_state *state =
2835 tevent_req_data(req,
2836 struct smbXcli_req_state);
2838 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2845 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2846 TALLOC_CTX *tmp_mem,
2849 struct tevent_req *req;
2850 struct smbXcli_req_state *state = NULL;
2855 struct smbXcli_session *last_session = NULL;
2857 status = smb2cli_inbuf_parse_compound(inbuf, tmp_mem,
2859 if (!NT_STATUS_IS_OK(status)) {
2863 for (i=0; i<num_iov; i+=3) {
2864 uint8_t *inbuf_ref = NULL;
2865 struct iovec *cur = &iov[i];
2866 uint8_t *inhdr = (uint8_t *)cur[0].iov_base;
2867 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
2868 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
2869 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
2870 uint16_t req_opcode;
2872 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
2873 uint32_t new_credits;
2874 struct smbXcli_session *session = NULL;
2875 const DATA_BLOB *signing_key = NULL;
2876 bool should_sign = false;
2878 new_credits = conn->smb2.cur_credits;
2879 new_credits += credits;
2880 if (new_credits > UINT16_MAX) {
2881 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2883 conn->smb2.cur_credits += credits;
2885 req = smb2cli_conn_find_pending(conn, mid);
2887 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2889 state = tevent_req_data(req, struct smbXcli_req_state);
2891 state->smb2.got_async = false;
2893 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2894 if (opcode != req_opcode) {
2895 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2897 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2899 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
2900 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2903 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
2904 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
2905 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
2906 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
2909 * async interim responses are not signed,
2910 * even if the SMB2_HDR_FLAG_SIGNED flag
2913 req_flags |= SMB2_HDR_FLAG_ASYNC;
2914 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
2915 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
2917 if (state->smb2.notify_async) {
2918 state->smb2.got_async = true;
2919 tevent_req_defer_callback(req, state->ev);
2920 tevent_req_notify_callback(req);
2925 session = state->session;
2926 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
2927 session = last_session;
2929 last_session = session;
2932 should_sign = session->smb2.should_sign;
2933 if (opcode == SMB2_OP_SESSSETUP &&
2934 session->smb2.signing_key.length != 0) {
2940 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
2941 return NT_STATUS_ACCESS_DENIED;
2945 if (flags & SMB2_HDR_FLAG_SIGNED) {
2946 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
2948 if (session == NULL) {
2949 struct smbXcli_session *s;
2951 s = state->conn->sessions;
2952 for (; s; s = s->next) {
2953 if (s->smb2.session_id != uid) {
2962 if (session == NULL) {
2963 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2966 last_session = session;
2967 signing_key = &session->smb2.channel_signing_key;
2970 if (opcode == SMB2_OP_SESSSETUP) {
2972 * We prefer the channel signing key, if it is
2975 * If we do not have a channel signing key yet,
2976 * we try the main signing key, if it is not
2977 * the final response.
2979 if (signing_key && signing_key->length == 0 &&
2980 !NT_STATUS_IS_OK(status)) {
2981 signing_key = &session->smb2.signing_key;
2984 if (signing_key && signing_key->length == 0) {
2986 * If we do not have a session key to
2987 * verify the signature, we defer the
2988 * signing check to the caller.
2990 * The caller gets NT_STATUS_OK, it
2992 * smb2cli_session_set_session_key()
2994 * smb2cli_session_set_channel_key()
2995 * which will check the signature
2996 * with the channel signing key.
3002 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3004 * if the server returns NT_STATUS_USER_SESSION_DELETED
3005 * the response is not signed and we should
3006 * propagate the NT_STATUS_USER_SESSION_DELETED
3007 * status to the caller.
3012 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3013 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3014 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3016 * if the server returns
3017 * NT_STATUS_NETWORK_NAME_DELETED
3018 * NT_STATUS_FILE_CLOSED
3019 * NT_STATUS_INVALID_PARAMETER
3020 * the response might not be signed
3021 * as this happens before the signing checks.
3023 * If server echos the signature (or all zeros)
3024 * we should report the status from the server
3030 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3031 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3034 state->smb2.signing_skipped = true;
3040 static const uint8_t zeros[16];
3042 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3046 state->smb2.signing_skipped = true;
3053 status = smb2_signing_check_pdu(*signing_key,
3054 state->conn->protocol,
3056 if (!NT_STATUS_IS_OK(status)) {
3058 * If the signing check fails, we disconnect
3065 smbXcli_req_unset_pending(req);
3068 * There might be more than one response
3069 * we need to defer the notifications
3071 if ((num_iov == 4) && (talloc_array_length(conn->pending) == 0)) {
3076 tevent_req_defer_callback(req, state->ev);
3080 * Note: here we use talloc_reference() in a way
3081 * that does not expose it to the caller.
3083 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3084 if (tevent_req_nomem(inbuf_ref, req)) {
3088 /* copy the related buffers */
3089 state->smb2.recv_iov[0] = cur[0];
3090 state->smb2.recv_iov[1] = cur[1];
3091 state->smb2.recv_iov[2] = cur[2];
3093 tevent_req_done(req);
3097 return NT_STATUS_RETRY;
3100 return NT_STATUS_OK;
3103 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3104 struct iovec **piov,
3105 const struct smb2cli_req_expected_response *expected,
3106 size_t num_expected)
3108 struct smbXcli_req_state *state =
3109 tevent_req_data(req,
3110 struct smbXcli_req_state);
3113 bool found_status = false;
3114 bool found_size = false;
3121 if (state->smb2.got_async) {
3122 return STATUS_PENDING;
3125 if (tevent_req_is_nterror(req, &status)) {
3126 for (i=0; i < num_expected; i++) {
3127 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3128 found_status = true;
3134 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3140 if (num_expected == 0) {
3141 found_status = true;
3145 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3146 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3148 for (i=0; i < num_expected; i++) {
3149 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3153 found_status = true;
3154 if (expected[i].body_size == 0) {
3159 if (expected[i].body_size == body_size) {
3165 if (!found_status) {
3169 if (state->smb2.signing_skipped) {
3170 if (num_expected > 0) {
3171 return NT_STATUS_ACCESS_DENIED;
3173 if (!NT_STATUS_IS_ERR(status)) {
3174 return NT_STATUS_ACCESS_DENIED;
3179 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3183 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3189 static const struct {
3190 enum protocol_types proto;
3191 const char *smb1_name;
3192 } smb1cli_prots[] = {
3193 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3194 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3195 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3196 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3197 {PROTOCOL_LANMAN2, "LM1.2X002"},
3198 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3199 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3200 {PROTOCOL_LANMAN2, "Samba"},
3201 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3202 {PROTOCOL_NT1, "NT LM 0.12"},
3203 {PROTOCOL_SMB2_02, "SMB 2.002"},
3204 {PROTOCOL_SMB2_10, "SMB 2.???"},
3207 static const struct {
3208 enum protocol_types proto;
3209 uint16_t smb2_dialect;
3210 } smb2cli_prots[] = {
3211 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3212 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3213 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3214 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3217 struct smbXcli_negprot_state {
3218 struct smbXcli_conn *conn;
3219 struct tevent_context *ev;
3220 uint32_t timeout_msec;
3221 enum protocol_types min_protocol;
3222 enum protocol_types max_protocol;
3226 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3230 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3231 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3232 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3233 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3234 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3235 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3239 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3240 struct tevent_context *ev,
3241 struct smbXcli_conn *conn,
3242 uint32_t timeout_msec,
3243 enum protocol_types min_protocol,
3244 enum protocol_types max_protocol)
3246 struct tevent_req *req, *subreq;
3247 struct smbXcli_negprot_state *state;
3249 req = tevent_req_create(mem_ctx, &state,
3250 struct smbXcli_negprot_state);
3256 state->timeout_msec = timeout_msec;
3257 state->min_protocol = min_protocol;
3258 state->max_protocol = max_protocol;
3260 if (min_protocol == PROTOCOL_NONE) {
3261 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3262 return tevent_req_post(req, ev);
3265 if (max_protocol == PROTOCOL_NONE) {
3266 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3267 return tevent_req_post(req, ev);
3270 if (min_protocol > max_protocol) {
3271 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3272 return tevent_req_post(req, ev);
3275 if ((min_protocol < PROTOCOL_SMB2_02) &&
3276 (max_protocol < PROTOCOL_SMB2_02)) {
3280 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3282 subreq = smbXcli_negprot_smb1_subreq(state);
3283 if (tevent_req_nomem(subreq, req)) {
3284 return tevent_req_post(req, ev);
3286 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3290 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3291 (max_protocol >= PROTOCOL_SMB2_02)) {
3295 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3297 subreq = smbXcli_negprot_smb2_subreq(state);
3298 if (tevent_req_nomem(subreq, req)) {
3299 return tevent_req_post(req, ev);
3301 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3306 * We send an SMB1 negprot with the SMB2 dialects
3307 * and expect a SMB1 or a SMB2 response.
3309 * smbXcli_negprot_dispatch_incoming() will fix the
3310 * callback to match protocol of the response.
3312 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3314 subreq = smbXcli_negprot_smb1_subreq(state);
3315 if (tevent_req_nomem(subreq, req)) {
3316 return tevent_req_post(req, ev);
3318 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3322 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3324 struct tevent_req *req =
3325 tevent_req_callback_data(subreq,
3330 * we just want the low level error
3332 status = tevent_req_simple_recv_ntstatus(subreq);
3333 TALLOC_FREE(subreq);
3334 if (tevent_req_nterror(req, status)) {
3338 /* this should never happen */
3339 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3342 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3345 DATA_BLOB bytes = data_blob_null;
3349 /* setup the protocol strings */
3350 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3354 if (smb1cli_prots[i].proto < state->min_protocol) {
3358 if (smb1cli_prots[i].proto > state->max_protocol) {
3362 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3368 * We now it is already ascii and
3369 * we want NULL termination.
3371 ok = data_blob_append(state, &bytes,
3372 smb1cli_prots[i].smb1_name,
3373 strlen(smb1cli_prots[i].smb1_name)+1);
3379 smb1cli_req_flags(state->max_protocol,
3380 state->conn->smb1.client.capabilities,
3385 return smb1cli_req_send(state, state->ev, state->conn,
3389 state->timeout_msec,
3390 0xFFFE, 0, 0, /* pid, tid, uid */
3391 0, NULL, /* wct, vwv */
3392 bytes.length, bytes.data);
3395 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3397 struct tevent_req *req =
3398 tevent_req_callback_data(subreq,
3400 struct smbXcli_negprot_state *state =
3401 tevent_req_data(req,
3402 struct smbXcli_negprot_state);
3403 struct smbXcli_conn *conn = state->conn;
3404 struct iovec *recv_iov = NULL;
3413 size_t num_prots = 0;
3415 uint32_t client_capabilities = conn->smb1.client.capabilities;
3416 uint32_t both_capabilities;
3417 uint32_t server_capabilities = 0;
3418 uint32_t capabilities;
3419 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3420 uint32_t server_max_xmit = 0;
3422 uint32_t server_max_mux = 0;
3423 uint16_t server_security_mode = 0;
3424 uint32_t server_session_key = 0;
3425 bool server_readbraw = false;
3426 bool server_writebraw = false;
3427 bool server_lockread = false;
3428 bool server_writeunlock = false;
3429 struct GUID server_guid = GUID_zero();
3430 DATA_BLOB server_gss_blob = data_blob_null;
3431 uint8_t server_challenge[8];
3432 char *server_workgroup = NULL;
3433 char *server_name = NULL;
3434 int server_time_zone = 0;
3435 NTTIME server_system_time = 0;
3436 static const struct smb1cli_req_expected_response expected[] = {
3438 .status = NT_STATUS_OK,
3439 .wct = 0x11, /* NT1 */
3442 .status = NT_STATUS_OK,
3443 .wct = 0x0D, /* LM */
3446 .status = NT_STATUS_OK,
3447 .wct = 0x01, /* CORE */
3451 ZERO_STRUCT(server_challenge);
3453 status = smb1cli_req_recv(subreq, state,
3458 NULL, /* pvwv_offset */
3461 NULL, /* pbytes_offset */
3463 expected, ARRAY_SIZE(expected));
3464 TALLOC_FREE(subreq);
3465 if (tevent_req_nterror(req, status)) {
3469 flags = CVAL(inhdr, HDR_FLG);
3471 protnum = SVAL(vwv, 0);
3473 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3474 if (smb1cli_prots[i].proto < state->min_protocol) {
3478 if (smb1cli_prots[i].proto > state->max_protocol) {
3482 if (protnum != num_prots) {
3487 conn->protocol = smb1cli_prots[i].proto;
3491 if (conn->protocol == PROTOCOL_NONE) {
3492 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3496 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3497 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3498 "and the selected protocol level doesn't support it.\n"));
3499 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3503 if (flags & FLAG_SUPPORT_LOCKREAD) {
3504 server_lockread = true;
3505 server_writeunlock = true;
3508 if (conn->protocol >= PROTOCOL_NT1) {
3509 const char *client_signing = NULL;
3510 bool server_mandatory = false;
3511 bool server_allowed = false;
3512 const char *server_signing = NULL;
3517 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3522 server_security_mode = CVAL(vwv + 1, 0);
3523 server_max_mux = SVAL(vwv + 1, 1);
3524 server_max_xmit = IVAL(vwv + 3, 1);
3525 server_session_key = IVAL(vwv + 7, 1);
3526 server_time_zone = SVALS(vwv + 15, 1);
3527 server_time_zone *= 60;
3528 /* this time arrives in real GMT */
3529 server_system_time = BVAL(vwv + 11, 1);
3530 server_capabilities = IVAL(vwv + 9, 1);
3532 key_len = CVAL(vwv + 16, 1);
3534 if (server_capabilities & CAP_RAW_MODE) {
3535 server_readbraw = true;
3536 server_writebraw = true;
3538 if (server_capabilities & CAP_LOCK_AND_READ) {
3539 server_lockread = true;
3542 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3543 DATA_BLOB blob1, blob2;
3545 if (num_bytes < 16) {
3546 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3550 blob1 = data_blob_const(bytes, 16);
3551 status = GUID_from_data_blob(&blob1, &server_guid);
3552 if (tevent_req_nterror(req, status)) {
3556 blob1 = data_blob_const(bytes+16, num_bytes-16);
3557 blob2 = data_blob_dup_talloc(state, blob1);
3558 if (blob1.length > 0 &&
3559 tevent_req_nomem(blob2.data, req)) {
3562 server_gss_blob = blob2;
3564 DATA_BLOB blob1, blob2;
3566 if (num_bytes < key_len) {
3567 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3571 if (key_len != 0 && key_len != 8) {
3572 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3577 memcpy(server_challenge, bytes, 8);
3580 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3581 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3582 if (blob1.length > 0) {
3585 len = utf16_len_n(blob1.data,
3589 ok = convert_string_talloc(state,
3597 status = map_nt_error_from_unix_common(errno);
3598 tevent_req_nterror(req, status);
3603 blob2.data += blob1.length;
3604 blob2.length -= blob1.length;
3605 if (blob2.length > 0) {
3608 len = utf16_len_n(blob1.data,
3612 ok = convert_string_talloc(state,
3620 status = map_nt_error_from_unix_common(errno);
3621 tevent_req_nterror(req, status);
3627 client_signing = "disabled";
3628 if (conn->allow_signing) {
3629 client_signing = "allowed";
3631 if (conn->mandatory_signing) {
3632 client_signing = "required";
3635 server_signing = "not supported";
3636 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3637 server_signing = "supported";
3638 server_allowed = true;
3640 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3641 server_signing = "required";
3642 server_mandatory = true;
3645 ok = smb_signing_set_negotiated(conn->smb1.signing,
3649 DEBUG(1,("cli_negprot: SMB signing is required, "
3650 "but client[%s] and server[%s] mismatch\n",
3651 client_signing, server_signing));
3652 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3656 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3662 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3666 server_security_mode = SVAL(vwv + 1, 0);
3667 server_max_xmit = SVAL(vwv + 2, 0);
3668 server_max_mux = SVAL(vwv + 3, 0);
3669 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3670 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3671 server_session_key = IVAL(vwv + 6, 0);
3672 server_time_zone = SVALS(vwv + 10, 0);
3673 server_time_zone *= 60;
3674 /* this time is converted to GMT by make_unix_date */
3675 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3676 unix_to_nt_time(&server_system_time, t);
3677 key_len = SVAL(vwv + 11, 0);
3679 if (num_bytes < key_len) {
3680 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3684 if (key_len != 0 && key_len != 8) {
3685 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3690 memcpy(server_challenge, bytes, 8);
3693 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3694 if (blob1.length > 0) {
3698 len = utf16_len_n(blob1.data,
3702 ok = convert_string_talloc(state,
3710 status = map_nt_error_from_unix_common(errno);
3711 tevent_req_nterror(req, status);
3717 /* the old core protocol */
3718 server_time_zone = get_time_zone(time(NULL));
3719 server_max_xmit = 1024;
3723 if (server_max_xmit < 1024) {
3724 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3728 if (server_max_mux < 1) {
3729 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3734 * Now calculate the negotiated capabilities
3735 * based on the mask for:
3736 * - client only flags
3737 * - flags used in both directions
3738 * - server only flags
3740 both_capabilities = client_capabilities & server_capabilities;
3741 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3742 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3743 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3745 max_xmit = MIN(client_max_xmit, server_max_xmit);
3747 conn->smb1.server.capabilities = server_capabilities;
3748 conn->smb1.capabilities = capabilities;
3750 conn->smb1.server.max_xmit = server_max_xmit;
3751 conn->smb1.max_xmit = max_xmit;
3753 conn->smb1.server.max_mux = server_max_mux;
3755 conn->smb1.server.security_mode = server_security_mode;
3757 conn->smb1.server.readbraw = server_readbraw;
3758 conn->smb1.server.writebraw = server_writebraw;
3759 conn->smb1.server.lockread = server_lockread;
3760 conn->smb1.server.writeunlock = server_writeunlock;
3762 conn->smb1.server.session_key = server_session_key;
3764 talloc_steal(conn, server_gss_blob.data);
3765 conn->smb1.server.gss_blob = server_gss_blob;
3766 conn->smb1.server.guid = server_guid;
3767 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3768 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3769 conn->smb1.server.name = talloc_move(conn, &server_name);
3771 conn->smb1.server.time_zone = server_time_zone;
3772 conn->smb1.server.system_time = server_system_time;
3774 tevent_req_done(req);
3777 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3781 uint16_t dialect_count = 0;
3783 buf = state->smb2.dyn;
3784 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3785 if (smb2cli_prots[i].proto < state->min_protocol) {
3789 if (smb2cli_prots[i].proto > state->max_protocol) {
3793 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3797 buf = state->smb2.fixed;
3799 SSVAL(buf, 2, dialect_count);
3800 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3801 SSVAL(buf, 6, 0); /* Reserved */
3802 if (state->max_protocol >= PROTOCOL_SMB2_22) {
3803 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
3805 SIVAL(buf, 8, 0); /* Capabilities */
3807 if (state->max_protocol >= PROTOCOL_SMB2_10) {
3811 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3813 if (!NT_STATUS_IS_OK(status)) {
3816 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3818 memset(buf+12, 0, 16); /* ClientGuid */
3820 SBVAL(buf, 28, 0); /* ClientStartTime */
3822 return smb2cli_req_send(state, state->ev,
3823 state->conn, SMB2_OP_NEGPROT,
3825 state->timeout_msec,
3826 0xFEFF, 0, NULL, /* pid, tid, session */
3827 state->smb2.fixed, sizeof(state->smb2.fixed),
3828 state->smb2.dyn, dialect_count*2);
3831 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3833 struct tevent_req *req =
3834 tevent_req_callback_data(subreq,
3836 struct smbXcli_negprot_state *state =
3837 tevent_req_data(req,
3838 struct smbXcli_negprot_state);
3839 struct smbXcli_conn *conn = state->conn;
3840 size_t security_offset, security_length;
3846 uint16_t dialect_revision;
3847 static const struct smb2cli_req_expected_response expected[] = {
3849 .status = NT_STATUS_OK,
3854 status = smb2cli_req_recv(subreq, state, &iov,
3855 expected, ARRAY_SIZE(expected));
3856 TALLOC_FREE(subreq);
3857 if (tevent_req_nterror(req, status)) {
3861 body = (uint8_t *)iov[1].iov_base;
3863 dialect_revision = SVAL(body, 4);
3865 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3866 if (smb2cli_prots[i].proto < state->min_protocol) {
3870 if (smb2cli_prots[i].proto > state->max_protocol) {
3874 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
3878 conn->protocol = smb2cli_prots[i].proto;
3882 if (conn->protocol == PROTOCOL_NONE) {
3883 if (state->min_protocol >= PROTOCOL_SMB2_02) {
3884 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3888 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
3889 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3893 /* make sure we do not loop forever */
3894 state->min_protocol = PROTOCOL_SMB2_02;
3897 * send a SMB2 negprot, in order to negotiate
3900 subreq = smbXcli_negprot_smb2_subreq(state);
3901 if (tevent_req_nomem(subreq, req)) {
3904 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3908 conn->smb2.server.security_mode = SVAL(body, 2);
3910 blob = data_blob_const(body + 8, 16);
3911 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
3912 if (tevent_req_nterror(req, status)) {
3916 conn->smb2.server.capabilities = IVAL(body, 24);
3917 conn->smb2.server.max_trans_size= IVAL(body, 28);
3918 conn->smb2.server.max_read_size = IVAL(body, 32);
3919 conn->smb2.server.max_write_size= IVAL(body, 36);
3920 conn->smb2.server.system_time = BVAL(body, 40);
3921 conn->smb2.server.start_time = BVAL(body, 48);
3923 security_offset = SVAL(body, 56);
3924 security_length = SVAL(body, 58);
3926 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
3927 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3931 if (security_length > iov[2].iov_len) {
3932 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3936 conn->smb2.server.gss_blob = data_blob_talloc(conn,
3939 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
3943 tevent_req_done(req);
3946 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3947 TALLOC_CTX *tmp_mem,
3950 size_t num_pending = talloc_array_length(conn->pending);
3951 struct tevent_req *subreq;
3952 struct smbXcli_req_state *substate;
3953 struct tevent_req *req;
3954 uint32_t protocol_magic = IVAL(inbuf, 4);
3956 if (num_pending != 1) {
3957 return NT_STATUS_INTERNAL_ERROR;
3960 subreq = conn->pending[0];
3961 substate = tevent_req_data(subreq, struct smbXcli_req_state);
3962 req = tevent_req_callback_data(subreq, struct tevent_req);
3964 switch (protocol_magic) {
3966 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3967 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3968 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3971 if (substate->smb2.recv_iov == NULL) {
3973 * For the SMB1 negprot we have move it.
3975 substate->smb2.recv_iov = substate->smb1.recv_iov;
3976 substate->smb1.recv_iov = NULL;
3980 * we got an SMB2 answer, which consumed sequence number 0
3981 * so we need to use 1 as the next one
3984 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3985 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3986 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3989 DEBUG(10, ("Got non-SMB PDU\n"));
3990 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3993 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
3995 return tevent_req_simple_recv_ntstatus(req);
3998 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
3999 uint32_t timeout_msec,
4000 enum protocol_types min_protocol,
4001 enum protocol_types max_protocol)
4003 TALLOC_CTX *frame = talloc_stackframe();
4004 struct tevent_context *ev;
4005 struct tevent_req *req;
4006 NTSTATUS status = NT_STATUS_NO_MEMORY;
4009 if (smbXcli_conn_has_async_calls(conn)) {
4011 * Can't use sync call while an async call is in flight
4013 status = NT_STATUS_INVALID_PARAMETER_MIX;
4016 ev = tevent_context_init(frame);
4020 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4021 min_protocol, max_protocol);
4025 ok = tevent_req_poll(req, ev);
4027 status = map_nt_error_from_unix_common(errno);
4030 status = smbXcli_negprot_recv(req);
4036 static int smbXcli_session_destructor(struct smbXcli_session *session)
4038 if (session->conn == NULL) {
4042 DLIST_REMOVE(session->conn->sessions, session);
4046 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4047 struct smbXcli_conn *conn)
4049 struct smbXcli_session *session;
4051 session = talloc_zero(mem_ctx, struct smbXcli_session);
4052 if (session == NULL) {
4055 talloc_set_destructor(session, smbXcli_session_destructor);
4057 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4058 session->conn = conn;
4063 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4065 struct smbXcli_conn *conn = session->conn;
4066 uint8_t security_mode = 0;
4069 return security_mode;
4072 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4073 if (conn->mandatory_signing) {
4074 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4077 return security_mode;
4080 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4082 return session->smb2.session_id;
4085 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4087 return session->smb2.session_flags;
4090 NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
4091 TALLOC_CTX *mem_ctx,
4094 *key = data_blob_null;
4096 if (session->smb2.application_key.length == 0) {
4097 return NT_STATUS_NO_USER_SESSION_KEY;
4100 *key = data_blob_dup_talloc(mem_ctx, session->smb2.application_key);
4101 if (key->data == NULL) {
4102 return NT_STATUS_NO_MEMORY;
4105 return NT_STATUS_OK;
4108 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4109 uint64_t session_id,
4110 uint16_t session_flags)
4112 session->smb2.session_id = session_id;
4113 session->smb2.session_flags = session_flags;
4116 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4117 const DATA_BLOB _session_key,
4118 const struct iovec *recv_iov)
4120 struct smbXcli_conn *conn = session->conn;
4121 uint16_t no_sign_flags;
4122 uint8_t session_key[16];
4126 return NT_STATUS_INVALID_PARAMETER_MIX;
4129 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4131 if (session->smb2.session_flags & no_sign_flags) {
4132 session->smb2.should_sign = false;
4133 return NT_STATUS_OK;
4136 if (session->smb2.signing_key.length != 0) {
4137 return NT_STATUS_INVALID_PARAMETER_MIX;
4140 ZERO_STRUCT(session_key);
4141 memcpy(session_key, _session_key.data,
4142 MIN(_session_key.length, sizeof(session_key)));
4144 session->smb2.signing_key = data_blob_talloc(session,
4146 sizeof(session_key));
4147 if (session->smb2.signing_key.data == NULL) {
4148 ZERO_STRUCT(session_key);
4149 return NT_STATUS_NO_MEMORY;
4152 if (conn->protocol >= PROTOCOL_SMB2_24) {
4153 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4154 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4156 smb2_key_derivation(session_key, sizeof(session_key),
4157 label.data, label.length,
4158 context.data, context.length,
4159 session->smb2.signing_key.data);
4162 session->smb2.application_key = data_blob_dup_talloc(session,
4163 session->smb2.signing_key);
4164 if (session->smb2.application_key.data == NULL) {
4165 ZERO_STRUCT(session_key);
4166 return NT_STATUS_NO_MEMORY;
4169 if (conn->protocol >= PROTOCOL_SMB2_24) {
4170 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4171 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4173 smb2_key_derivation(session_key, sizeof(session_key),
4174 label.data, label.length,
4175 context.data, context.length,
4176 session->smb2.application_key.data);
4178 ZERO_STRUCT(session_key);
4180 session->smb2.channel_signing_key = data_blob_dup_talloc(session,
4181 session->smb2.signing_key);
4182 if (session->smb2.channel_signing_key.data == NULL) {
4183 return NT_STATUS_NO_MEMORY;
4186 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4187 session->conn->protocol,
4189 if (!NT_STATUS_IS_OK(status)) {
4193 session->smb2.should_sign = false;
4195 if (conn->desire_signing) {
4196 session->smb2.should_sign = true;
4199 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4200 session->smb2.should_sign = true;
4203 return NT_STATUS_OK;
4206 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4207 struct smbXcli_session *session1,
4208 struct smbXcli_conn *conn,
4209 struct smbXcli_session **_session2)
4211 struct smbXcli_session *session2;
4213 if (session1->smb2.signing_key.length == 0) {
4214 return NT_STATUS_INVALID_PARAMETER_MIX;
4218 return NT_STATUS_INVALID_PARAMETER_MIX;
4221 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4222 if (session2 == NULL) {
4223 return NT_STATUS_NO_MEMORY;
4225 session2->smb2.session_id = session1->smb2.session_id;
4226 session2->smb2.session_flags = session1->smb2.session_flags;
4228 session2->smb2.signing_key = data_blob_dup_talloc(session2,
4229 session1->smb2.signing_key);
4230 if (session2->smb2.signing_key.data == NULL) {
4231 return NT_STATUS_NO_MEMORY;
4234 session2->smb2.should_sign = session1->smb2.should_sign;
4236 talloc_set_destructor(session2, smbXcli_session_destructor);
4237 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4238 session2->conn = conn;
4240 *_session2 = session2;
4241 return NT_STATUS_OK;
4244 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4245 const DATA_BLOB _channel_key,
4246 const struct iovec *recv_iov)
4248 struct smbXcli_conn *conn = session->conn;
4249 uint8_t channel_key[16];
4253 return NT_STATUS_INVALID_PARAMETER_MIX;
4256 if (session->smb2.channel_signing_key.length != 0) {
4257 return NT_STATUS_INVALID_PARAMETER_MIX;
4260 ZERO_STRUCT(channel_key);
4261 memcpy(channel_key, _channel_key.data,
4262 MIN(_channel_key.length, sizeof(channel_key)));
4264 session->smb2.channel_signing_key = data_blob_talloc(session,
4266 sizeof(channel_key));
4267 if (session->smb2.channel_signing_key.data == NULL) {
4268 ZERO_STRUCT(channel_key);
4269 return NT_STATUS_NO_MEMORY;
4272 if (conn->protocol >= PROTOCOL_SMB2_24) {
4273 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4274 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4276 smb2_key_derivation(channel_key, sizeof(channel_key),
4277 label.data, label.length,
4278 context.data, context.length,
4279 session->smb2.channel_signing_key.data);
4281 ZERO_STRUCT(channel_key);
4283 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4284 session->conn->protocol,
4286 if (!NT_STATUS_IS_OK(status)) {
4290 return NT_STATUS_OK;