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 uint16_t security_mode;
110 uint32_t capabilities;
111 uint16_t security_mode;
113 uint32_t max_trans_size;
114 uint32_t max_read_size;
115 uint32_t max_write_size;
122 uint16_t cur_credits;
123 uint16_t max_credits;
126 struct smbXcli_session *sessions;
129 struct smbXcli_session {
130 struct smbXcli_session *prev, *next;
131 struct smbXcli_conn *conn;
135 uint16_t session_flags;
136 DATA_BLOB signing_key;
137 DATA_BLOB session_key;
143 struct smbXcli_req_state {
144 struct tevent_context *ev;
145 struct smbXcli_conn *conn;
146 struct smbXcli_session *session; /* maybe NULL */
148 uint8_t length_hdr[4];
155 /* Space for the header including the wct */
156 uint8_t hdr[HDR_VWV];
159 * For normal requests, smb1cli_req_send chooses a mid.
160 * SecondaryV trans requests need to use the mid of the primary
161 * request, so we need a place to store it.
162 * Assume it is set if != 0.
167 uint8_t bytecount_buf[2];
169 #define MAX_SMB_IOV 10
170 /* length_hdr, hdr, words, byte_count, buffers */
171 struct iovec iov[1 + 3 + MAX_SMB_IOV];
176 struct tevent_req **chained_requests;
179 NTSTATUS recv_status;
180 /* always an array of 3 talloc elements */
181 struct iovec *recv_iov;
185 const uint8_t *fixed;
191 uint8_t pad[7]; /* padding space for compounding */
193 /* always an array of 3 talloc elements */
194 struct iovec *recv_iov;
196 uint16_t credit_charge;
198 bool signing_skipped;
204 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
207 * NT_STATUS_OK, means we do not notify the callers
209 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
211 while (conn->sessions) {
212 conn->sessions->conn = NULL;
213 DLIST_REMOVE(conn->sessions, conn->sessions);
216 if (conn->smb1.trans_enc) {
217 TALLOC_FREE(conn->smb1.trans_enc);
223 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
225 const char *remote_name,
226 enum smb_signing_setting signing_state,
227 uint32_t smb1_capabilities,
228 struct GUID *client_guid)
230 struct smbXcli_conn *conn = NULL;
232 struct sockaddr *sa = NULL;
236 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
242 conn->write_fd = dup(fd);
243 if (conn->write_fd == -1) {
247 conn->remote_name = talloc_strdup(conn, remote_name);
248 if (conn->remote_name == NULL) {
253 ss = (void *)&conn->local_ss;
254 sa = (struct sockaddr *)ss;
255 sa_length = sizeof(conn->local_ss);
256 ret = getsockname(fd, sa, &sa_length);
260 ss = (void *)&conn->remote_ss;
261 sa = (struct sockaddr *)ss;
262 sa_length = sizeof(conn->remote_ss);
263 ret = getpeername(fd, sa, &sa_length);
268 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
269 if (conn->outgoing == NULL) {
272 conn->pending = NULL;
274 conn->protocol = PROTOCOL_NONE;
276 switch (signing_state) {
277 case SMB_SIGNING_OFF:
279 conn->allow_signing = false;
280 conn->desire_signing = false;
281 conn->mandatory_signing = false;
283 case SMB_SIGNING_DEFAULT:
284 case SMB_SIGNING_IF_REQUIRED:
285 /* if the server requires it */
286 conn->allow_signing = true;
287 conn->desire_signing = false;
288 conn->mandatory_signing = false;
290 case SMB_SIGNING_REQUIRED:
292 conn->allow_signing = true;
293 conn->desire_signing = true;
294 conn->mandatory_signing = true;
298 conn->smb1.client.capabilities = smb1_capabilities;
299 conn->smb1.client.max_xmit = UINT16_MAX;
301 conn->smb1.capabilities = conn->smb1.client.capabilities;
302 conn->smb1.max_xmit = 1024;
306 /* initialise signing */
307 conn->smb1.signing = smb_signing_init(conn,
309 conn->desire_signing,
310 conn->mandatory_signing);
311 if (!conn->smb1.signing) {
315 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
316 if (conn->mandatory_signing) {
317 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
320 conn->smb2.client.guid = *client_guid;
323 conn->smb2.cur_credits = 1;
324 conn->smb2.max_credits = 0;
326 talloc_set_destructor(conn, smbXcli_conn_destructor);
330 if (conn->write_fd != -1) {
331 close(conn->write_fd);
337 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
343 if (conn->read_fd == -1) {
350 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
352 return conn->protocol;
355 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
357 if (conn->protocol >= PROTOCOL_SMB2_02) {
361 if (conn->smb1.capabilities & CAP_UNICODE) {
368 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
370 set_socket_options(conn->read_fd, options);
373 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
375 return &conn->local_ss;
378 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
380 return &conn->remote_ss;
383 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
385 return conn->remote_name;
388 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
390 if (conn->protocol >= PROTOCOL_SMB2_02) {
397 return conn->smb1.server.max_mux;
400 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
402 if (conn->protocol >= PROTOCOL_SMB2_02) {
403 return conn->smb2.server.system_time;
406 return conn->smb1.server.system_time;
409 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
411 if (conn->protocol >= PROTOCOL_SMB2_02) {
412 return &conn->smb2.server.gss_blob;
415 return &conn->smb1.server.gss_blob;
418 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
420 if (conn->protocol >= PROTOCOL_SMB2_02) {
421 return &conn->smb2.server.guid;
424 return &conn->smb1.server.guid;
427 struct smbXcli_conn_samba_suicide_state {
428 struct smbXcli_conn *conn;
433 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
435 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
436 struct tevent_context *ev,
437 struct smbXcli_conn *conn,
440 struct tevent_req *req, *subreq;
441 struct smbXcli_conn_samba_suicide_state *state;
443 req = tevent_req_create(mem_ctx, &state,
444 struct smbXcli_conn_samba_suicide_state);
449 SIVAL(state->buf, 4, 0x74697865);
450 SCVAL(state->buf, 8, exitcode);
451 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
453 state->iov.iov_base = state->buf;
454 state->iov.iov_len = sizeof(state->buf);
456 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
457 false, &state->iov, 1);
458 if (tevent_req_nomem(subreq, req)) {
459 return tevent_req_post(req, ev);
461 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
465 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
467 struct tevent_req *req = tevent_req_callback_data(
468 subreq, struct tevent_req);
469 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
470 req, struct smbXcli_conn_samba_suicide_state);
474 nwritten = writev_recv(subreq, &err);
476 if (nwritten == -1) {
477 NTSTATUS status = map_nt_error_from_unix_common(err);
478 smbXcli_conn_disconnect(state->conn, status);
481 tevent_req_done(req);
484 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
486 return tevent_req_simple_recv_ntstatus(req);
489 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
492 TALLOC_CTX *frame = talloc_stackframe();
493 struct tevent_context *ev;
494 struct tevent_req *req;
495 NTSTATUS status = NT_STATUS_NO_MEMORY;
498 if (smbXcli_conn_has_async_calls(conn)) {
500 * Can't use sync call while an async call is in flight
502 status = NT_STATUS_INVALID_PARAMETER_MIX;
505 ev = tevent_context_init(frame);
509 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
513 ok = tevent_req_poll(req, ev);
515 status = map_nt_error_from_unix_common(errno);
518 status = smbXcli_conn_samba_suicide_recv(req);
524 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
526 return conn->smb1.capabilities;
529 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
531 return conn->smb1.max_xmit;
534 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
536 return conn->smb1.server.session_key;
539 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
541 return conn->smb1.server.challenge;
544 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
546 return conn->smb1.server.security_mode;
549 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
551 return conn->smb1.server.readbraw;
554 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
556 return conn->smb1.server.writebraw;
559 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
561 return conn->smb1.server.lockread;
564 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
566 return conn->smb1.server.writeunlock;
569 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
571 return conn->smb1.server.time_zone;
574 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
575 const DATA_BLOB user_session_key,
576 const DATA_BLOB response)
578 return smb_signing_activate(conn->smb1.signing,
583 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
584 const uint8_t *buf, uint32_t seqnum)
586 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
589 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
591 return smb_signing_is_active(conn->smb1.signing);
594 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
595 struct smb_trans_enc_state *es)
597 /* Replace the old state, if any. */
598 if (conn->smb1.trans_enc) {
599 TALLOC_FREE(conn->smb1.trans_enc);
601 conn->smb1.trans_enc = es;
604 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
606 return common_encryption_on(conn->smb1.trans_enc);
610 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
612 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
613 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
615 if (NT_STATUS_IS_OK(status)) {
619 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
623 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
627 * Is the SMB command able to hold an AND_X successor
628 * @param[in] cmd The SMB command in question
629 * @retval Can we add a chained request after "cmd"?
631 bool smb1cli_is_andx_req(uint8_t cmd)
651 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
653 size_t num_pending = talloc_array_length(conn->pending);
659 result = conn->smb1.mid++;
660 if ((result == 0) || (result == 0xffff)) {
664 for (i=0; i<num_pending; i++) {
665 if (result == smb1cli_req_mid(conn->pending[i])) {
670 if (i == num_pending) {
676 void smbXcli_req_unset_pending(struct tevent_req *req)
678 struct smbXcli_req_state *state =
680 struct smbXcli_req_state);
681 struct smbXcli_conn *conn = state->conn;
682 size_t num_pending = talloc_array_length(conn->pending);
685 if (state->smb1.mid != 0) {
687 * This is a [nt]trans[2] request which waits
688 * for more than one reply.
693 talloc_set_destructor(req, NULL);
695 if (num_pending == 1) {
697 * The pending read_smb tevent_req is a child of
698 * conn->pending. So if nothing is pending anymore, we need to
699 * delete the socket read fde.
701 TALLOC_FREE(conn->pending);
702 conn->read_smb_req = NULL;
706 for (i=0; i<num_pending; i++) {
707 if (req == conn->pending[i]) {
711 if (i == num_pending) {
713 * Something's seriously broken. Just returning here is the
714 * right thing nevertheless, the point of this routine is to
715 * remove ourselves from conn->pending.
721 * Remove ourselves from the conn->pending array
723 for (; i < (num_pending - 1); i++) {
724 conn->pending[i] = conn->pending[i+1];
728 * No NULL check here, we're shrinking by sizeof(void *), and
729 * talloc_realloc just adjusts the size for this.
731 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
736 static int smbXcli_req_destructor(struct tevent_req *req)
738 struct smbXcli_req_state *state =
740 struct smbXcli_req_state);
743 * Make sure we really remove it from
744 * the pending array on destruction.
747 smbXcli_req_unset_pending(req);
751 static bool smb1cli_req_cancel(struct tevent_req *req);
752 static bool smb2cli_req_cancel(struct tevent_req *req);
754 static bool smbXcli_req_cancel(struct tevent_req *req)
756 struct smbXcli_req_state *state =
758 struct smbXcli_req_state);
760 if (!smbXcli_conn_is_connected(state->conn)) {
764 if (state->conn->protocol == PROTOCOL_NONE) {
768 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
769 return smb2cli_req_cancel(req);
772 return smb1cli_req_cancel(req);
775 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
777 bool smbXcli_req_set_pending(struct tevent_req *req)
779 struct smbXcli_req_state *state =
781 struct smbXcli_req_state);
782 struct smbXcli_conn *conn;
783 struct tevent_req **pending;
788 if (!smbXcli_conn_is_connected(conn)) {
792 num_pending = talloc_array_length(conn->pending);
794 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
796 if (pending == NULL) {
799 pending[num_pending] = req;
800 conn->pending = pending;
801 talloc_set_destructor(req, smbXcli_req_destructor);
802 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
804 if (!smbXcli_conn_receive_next(conn)) {
806 * the caller should notify the current request
808 * And all other pending requests get notified
809 * by smbXcli_conn_disconnect().
811 smbXcli_req_unset_pending(req);
812 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
819 static void smbXcli_conn_received(struct tevent_req *subreq);
821 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
823 size_t num_pending = talloc_array_length(conn->pending);
824 struct tevent_req *req;
825 struct smbXcli_req_state *state;
827 if (conn->read_smb_req != NULL) {
831 if (num_pending == 0) {
832 if (conn->smb2.mid < UINT64_MAX) {
833 /* no more pending requests, so we are done for now */
838 * If there are no more SMB2 requests possible,
839 * because we are out of message ids,
840 * we need to disconnect.
842 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
846 req = conn->pending[0];
847 state = tevent_req_data(req, struct smbXcli_req_state);
850 * We're the first ones, add the read_smb request that waits for the
851 * answer from the server
853 conn->read_smb_req = read_smb_send(conn->pending,
856 if (conn->read_smb_req == NULL) {
859 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
863 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
865 tevent_queue_stop(conn->outgoing);
867 if (conn->read_fd != -1) {
868 close(conn->read_fd);
870 if (conn->write_fd != -1) {
871 close(conn->write_fd);
877 * Cancel all pending requests. We do not do a for-loop walking
878 * conn->pending because that array changes in
879 * smbXcli_req_unset_pending.
881 while (talloc_array_length(conn->pending) > 0) {
882 struct tevent_req *req;
883 struct smbXcli_req_state *state;
884 struct tevent_req **chain;
888 req = conn->pending[0];
889 state = tevent_req_data(req, struct smbXcli_req_state);
891 if (state->smb1.chained_requests == NULL) {
893 * We're dead. No point waiting for trans2
898 smbXcli_req_unset_pending(req);
900 if (NT_STATUS_IS_OK(status)) {
901 /* do not notify the callers */
906 * we need to defer the callback, because we may notify
907 * more then one caller.
909 tevent_req_defer_callback(req, state->ev);
910 tevent_req_nterror(req, status);
914 chain = talloc_move(conn, &state->smb1.chained_requests);
915 num_chained = talloc_array_length(chain);
917 for (i=0; i<num_chained; i++) {
919 state = tevent_req_data(req, struct smbXcli_req_state);
922 * We're dead. No point waiting for trans2
927 smbXcli_req_unset_pending(req);
929 if (NT_STATUS_IS_OK(status)) {
930 /* do not notify the callers */
935 * we need to defer the callback, because we may notify
936 * more than one caller.
938 tevent_req_defer_callback(req, state->ev);
939 tevent_req_nterror(req, status);
946 * Fetch a smb request's mid. Only valid after the request has been sent by
947 * smb1cli_req_send().
949 uint16_t smb1cli_req_mid(struct tevent_req *req)
951 struct smbXcli_req_state *state =
953 struct smbXcli_req_state);
955 if (state->smb1.mid != 0) {
956 return state->smb1.mid;
959 return SVAL(state->smb1.hdr, HDR_MID);
962 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
964 struct smbXcli_req_state *state =
966 struct smbXcli_req_state);
968 state->smb1.mid = mid;
971 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
973 struct smbXcli_req_state *state =
975 struct smbXcli_req_state);
977 return state->smb1.seqnum;
980 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
982 struct smbXcli_req_state *state =
984 struct smbXcli_req_state);
986 state->smb1.seqnum = seqnum;
989 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
993 for (i=0; i<count; i++) {
994 result += iov[i].iov_len;
999 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1000 const struct iovec *iov,
1003 size_t len = smbXcli_iov_len(iov, count);
1008 buf = talloc_array(mem_ctx, uint8_t, len);
1013 for (i=0; i<count; i++) {
1014 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1015 copied += iov[i].iov_len;
1020 static void smb1cli_req_flags(enum protocol_types protocol,
1021 uint32_t smb1_capabilities,
1022 uint8_t smb_command,
1023 uint8_t additional_flags,
1024 uint8_t clear_flags,
1026 uint16_t additional_flags2,
1027 uint16_t clear_flags2,
1031 uint16_t flags2 = 0;
1033 if (protocol >= PROTOCOL_LANMAN1) {
1034 flags |= FLAG_CASELESS_PATHNAMES;
1035 flags |= FLAG_CANONICAL_PATHNAMES;
1038 if (protocol >= PROTOCOL_LANMAN2) {
1039 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1040 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1043 if (protocol >= PROTOCOL_NT1) {
1044 flags2 |= FLAGS2_IS_LONG_NAME;
1046 if (smb1_capabilities & CAP_UNICODE) {
1047 flags2 |= FLAGS2_UNICODE_STRINGS;
1049 if (smb1_capabilities & CAP_STATUS32) {
1050 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1052 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1053 flags2 |= FLAGS2_EXTENDED_SECURITY;
1057 flags |= additional_flags;
1058 flags &= ~clear_flags;
1059 flags2 |= additional_flags2;
1060 flags2 &= ~clear_flags2;
1066 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1068 static bool smb1cli_req_cancel(struct tevent_req *req)
1070 struct smbXcli_req_state *state =
1071 tevent_req_data(req,
1072 struct smbXcli_req_state);
1079 struct tevent_req *subreq;
1082 flags = CVAL(state->smb1.hdr, HDR_FLG);
1083 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1084 pid = SVAL(state->smb1.hdr, HDR_PID);
1085 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1086 tid = SVAL(state->smb1.hdr, HDR_TID);
1087 uid = SVAL(state->smb1.hdr, HDR_UID);
1088 mid = SVAL(state->smb1.hdr, HDR_MID);
1090 subreq = smb1cli_req_create(state, state->ev,
1098 0, NULL); /* bytes */
1099 if (subreq == NULL) {
1102 smb1cli_req_set_mid(subreq, mid);
1104 status = smb1cli_req_chain_submit(&subreq, 1);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 TALLOC_FREE(subreq);
1109 smb1cli_req_set_mid(subreq, 0);
1111 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1116 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1118 /* we do not care about the result */
1119 TALLOC_FREE(subreq);
1122 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1123 struct tevent_context *ev,
1124 struct smbXcli_conn *conn,
1125 uint8_t smb_command,
1126 uint8_t additional_flags,
1127 uint8_t clear_flags,
1128 uint16_t additional_flags2,
1129 uint16_t clear_flags2,
1130 uint32_t timeout_msec,
1134 uint8_t wct, uint16_t *vwv,
1136 struct iovec *bytes_iov)
1138 struct tevent_req *req;
1139 struct smbXcli_req_state *state;
1141 uint16_t flags2 = 0;
1143 if (iov_count > MAX_SMB_IOV) {
1145 * Should not happen :-)
1150 req = tevent_req_create(mem_ctx, &state,
1151 struct smbXcli_req_state);
1158 state->smb1.recv_cmd = 0xFF;
1159 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1160 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1161 if (state->smb1.recv_iov == NULL) {
1166 smb1cli_req_flags(conn->protocol,
1167 conn->smb1.capabilities,
1176 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1177 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1178 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1179 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1180 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1181 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1182 SSVAL(state->smb1.hdr, HDR_TID, tid);
1183 SSVAL(state->smb1.hdr, HDR_PID, pid);
1184 SSVAL(state->smb1.hdr, HDR_UID, uid);
1185 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1186 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1188 state->smb1.vwv = vwv;
1190 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1192 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1193 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1194 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1195 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1196 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1197 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1198 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1199 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1201 if (iov_count != 0) {
1202 memcpy(&state->smb1.iov[4], bytes_iov,
1203 iov_count * sizeof(*bytes_iov));
1205 state->smb1.iov_count = iov_count + 4;
1207 if (timeout_msec > 0) {
1208 struct timeval endtime;
1210 endtime = timeval_current_ofs_msec(timeout_msec);
1211 if (!tevent_req_set_endtime(req, ev, endtime)) {
1216 switch (smb_command) {
1220 state->one_way = true;
1223 state->one_way = true;
1224 state->smb1.one_way_seqnum = true;
1228 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1229 state->one_way = true;
1237 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1238 struct iovec *iov, int iov_count,
1240 bool one_way_seqnum)
1242 TALLOC_CTX *frame = NULL;
1246 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1247 * iovec directly. MD5Update would do that just fine.
1250 if (iov_count < 4) {
1251 return NT_STATUS_INVALID_PARAMETER_MIX;
1253 if (iov[0].iov_len != NBT_HDR_SIZE) {
1254 return NT_STATUS_INVALID_PARAMETER_MIX;
1256 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1257 return NT_STATUS_INVALID_PARAMETER_MIX;
1259 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1260 return NT_STATUS_INVALID_PARAMETER_MIX;
1262 if (iov[3].iov_len != sizeof(uint16_t)) {
1263 return NT_STATUS_INVALID_PARAMETER_MIX;
1266 frame = talloc_stackframe();
1268 buf = smbXcli_iov_concat(frame, iov, iov_count);
1270 return NT_STATUS_NO_MEMORY;
1273 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1275 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1276 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1279 return NT_STATUS_OK;
1282 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1283 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1284 TALLOC_CTX *tmp_mem,
1287 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1288 struct smbXcli_req_state *state,
1289 struct iovec *iov, int iov_count)
1291 struct tevent_req *subreq;
1296 if (!smbXcli_conn_is_connected(state->conn)) {
1297 return NT_STATUS_CONNECTION_DISCONNECTED;
1300 if (state->conn->protocol > PROTOCOL_NT1) {
1301 return NT_STATUS_REVISION_MISMATCH;
1304 if (iov_count < 4) {
1305 return NT_STATUS_INVALID_PARAMETER_MIX;
1307 if (iov[0].iov_len != NBT_HDR_SIZE) {
1308 return NT_STATUS_INVALID_PARAMETER_MIX;
1310 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1311 return NT_STATUS_INVALID_PARAMETER_MIX;
1313 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1314 return NT_STATUS_INVALID_PARAMETER_MIX;
1316 if (iov[3].iov_len != sizeof(uint16_t)) {
1317 return NT_STATUS_INVALID_PARAMETER_MIX;
1320 cmd = CVAL(iov[1].iov_base, HDR_COM);
1321 if (cmd == SMBreadBraw) {
1322 if (smbXcli_conn_has_async_calls(state->conn)) {
1323 return NT_STATUS_INVALID_PARAMETER_MIX;
1325 state->conn->smb1.read_braw_req = req;
1328 if (state->smb1.mid != 0) {
1329 mid = state->smb1.mid;
1331 mid = smb1cli_alloc_mid(state->conn);
1333 SSVAL(iov[1].iov_base, HDR_MID, mid);
1335 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1337 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1338 &state->smb1.seqnum,
1339 state->smb1.one_way_seqnum);
1341 if (!NT_STATUS_IS_OK(status)) {
1346 * If we supported multiple encrytion contexts
1347 * here we'd look up based on tid.
1349 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1350 char *buf, *enc_buf;
1352 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1354 return NT_STATUS_NO_MEMORY;
1356 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1357 (char *)buf, &enc_buf);
1359 if (!NT_STATUS_IS_OK(status)) {
1360 DEBUG(0, ("Error in encrypting client message: %s\n",
1361 nt_errstr(status)));
1364 buf = (char *)talloc_memdup(state, enc_buf,
1365 smb_len_nbt(enc_buf)+4);
1368 return NT_STATUS_NO_MEMORY;
1370 iov[0].iov_base = (void *)buf;
1371 iov[0].iov_len = talloc_get_size(buf);
1375 if (state->conn->dispatch_incoming == NULL) {
1376 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1379 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1381 subreq = writev_send(state, state->ev, state->conn->outgoing,
1382 state->conn->write_fd, false, iov, iov_count);
1383 if (subreq == NULL) {
1384 return NT_STATUS_NO_MEMORY;
1386 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1387 return NT_STATUS_OK;
1390 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1391 struct tevent_context *ev,
1392 struct smbXcli_conn *conn,
1393 uint8_t smb_command,
1394 uint8_t additional_flags,
1395 uint8_t clear_flags,
1396 uint16_t additional_flags2,
1397 uint16_t clear_flags2,
1398 uint32_t timeout_msec,
1402 uint8_t wct, uint16_t *vwv,
1404 const uint8_t *bytes)
1406 struct tevent_req *req;
1410 iov.iov_base = discard_const_p(void, bytes);
1411 iov.iov_len = num_bytes;
1413 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1414 additional_flags, clear_flags,
1415 additional_flags2, clear_flags2,
1422 if (!tevent_req_is_in_progress(req)) {
1423 return tevent_req_post(req, ev);
1425 status = smb1cli_req_chain_submit(&req, 1);
1426 if (tevent_req_nterror(req, status)) {
1427 return tevent_req_post(req, ev);
1432 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1434 struct tevent_req *req =
1435 tevent_req_callback_data(subreq,
1437 struct smbXcli_req_state *state =
1438 tevent_req_data(req,
1439 struct smbXcli_req_state);
1443 nwritten = writev_recv(subreq, &err);
1444 TALLOC_FREE(subreq);
1445 if (nwritten == -1) {
1446 NTSTATUS status = map_nt_error_from_unix_common(err);
1447 smbXcli_conn_disconnect(state->conn, status);
1451 if (state->one_way) {
1452 state->inbuf = NULL;
1453 tevent_req_done(req);
1457 if (!smbXcli_req_set_pending(req)) {
1458 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1463 static void smbXcli_conn_received(struct tevent_req *subreq)
1465 struct smbXcli_conn *conn =
1466 tevent_req_callback_data(subreq,
1467 struct smbXcli_conn);
1468 TALLOC_CTX *frame = talloc_stackframe();
1474 if (subreq != conn->read_smb_req) {
1475 DEBUG(1, ("Internal error: cli_smb_received called with "
1476 "unexpected subreq\n"));
1477 status = NT_STATUS_INTERNAL_ERROR;
1478 smbXcli_conn_disconnect(conn, status);
1482 conn->read_smb_req = NULL;
1484 received = read_smb_recv(subreq, frame, &inbuf, &err);
1485 TALLOC_FREE(subreq);
1486 if (received == -1) {
1487 status = map_nt_error_from_unix_common(err);
1488 smbXcli_conn_disconnect(conn, status);
1493 status = conn->dispatch_incoming(conn, frame, inbuf);
1495 if (NT_STATUS_IS_OK(status)) {
1497 * We should not do any more processing
1498 * as the dispatch function called
1499 * tevent_req_done().
1502 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1504 * We got an error, so notify all pending requests
1506 smbXcli_conn_disconnect(conn, status);
1511 * We got NT_STATUS_RETRY, so we may ask for a
1512 * next incoming pdu.
1514 if (!smbXcli_conn_receive_next(conn)) {
1515 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1519 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1520 struct iovec **piov, int *pnum_iov)
1531 buflen = smb_len_nbt(buf);
1534 hdr = buf + NBT_HDR_SIZE;
1536 if (buflen < MIN_SMB_SIZE) {
1537 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1541 * This returns iovec elements in the following order:
1556 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1558 return NT_STATUS_NO_MEMORY;
1560 iov[0].iov_base = hdr;
1561 iov[0].iov_len = HDR_WCT;
1564 cmd = CVAL(hdr, HDR_COM);
1568 size_t len = buflen - taken;
1570 struct iovec *iov_tmp;
1577 * we need at least WCT and BCC
1579 needed = sizeof(uint8_t) + sizeof(uint16_t);
1581 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1582 __location__, (int)len, (int)needed));
1587 * Now we check if the specified words are there
1589 wct = CVAL(hdr, wct_ofs);
1590 needed += wct * sizeof(uint16_t);
1592 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1593 __location__, (int)len, (int)needed));
1598 * Now we check if the specified bytes are there
1600 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1601 bcc = SVAL(hdr, bcc_ofs);
1602 needed += bcc * sizeof(uint8_t);
1604 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1605 __location__, (int)len, (int)needed));
1610 * we allocate 2 iovec structures for words and bytes
1612 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1614 if (iov_tmp == NULL) {
1616 return NT_STATUS_NO_MEMORY;
1619 cur = &iov[num_iov];
1622 cur[0].iov_len = wct * sizeof(uint16_t);
1623 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1624 cur[1].iov_len = bcc * sizeof(uint8_t);
1625 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1629 if (!smb1cli_is_andx_req(cmd)) {
1631 * If the current command does not have AndX chanining
1637 if (wct == 0 && bcc == 0) {
1639 * An empty response also ends the chain,
1640 * most likely with an error.
1646 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1647 __location__, (int)wct, (int)cmd));
1650 cmd = CVAL(cur[0].iov_base, 0);
1653 * If it is the end of the chain we are also done.
1657 wct_ofs = SVAL(cur[0].iov_base, 2);
1659 if (wct_ofs < taken) {
1660 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1662 if (wct_ofs > buflen) {
1663 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1667 * we consumed everything up to the start of the next
1673 remaining = buflen - taken;
1675 if (remaining > 0 && num_iov >= 3) {
1677 * The last DATA block gets the remaining
1678 * bytes, this is needed to support
1679 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1681 iov[num_iov-1].iov_len += remaining;
1685 *pnum_iov = num_iov;
1686 return NT_STATUS_OK;
1690 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1693 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1694 TALLOC_CTX *tmp_mem,
1697 struct tevent_req *req;
1698 struct smbXcli_req_state *state;
1705 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1706 struct iovec *iov = NULL;
1708 struct tevent_req **chain = NULL;
1709 size_t num_chained = 0;
1710 size_t num_responses = 0;
1712 if (conn->smb1.read_braw_req != NULL) {
1713 req = conn->smb1.read_braw_req;
1714 conn->smb1.read_braw_req = NULL;
1715 state = tevent_req_data(req, struct smbXcli_req_state);
1717 smbXcli_req_unset_pending(req);
1719 if (state->smb1.recv_iov == NULL) {
1721 * For requests with more than
1722 * one response, we have to readd the
1725 state->smb1.recv_iov = talloc_zero_array(state,
1728 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1729 return NT_STATUS_OK;
1733 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1734 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1735 ZERO_STRUCT(state->smb1.recv_iov[1]);
1736 ZERO_STRUCT(state->smb1.recv_iov[2]);
1738 state->smb1.recv_cmd = SMBreadBraw;
1739 state->smb1.recv_status = NT_STATUS_OK;
1740 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1742 tevent_req_done(req);
1743 return NT_STATUS_OK;
1746 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1747 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1748 DEBUG(10, ("Got non-SMB PDU\n"));
1749 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1753 * If we supported multiple encrytion contexts
1754 * here we'd look up based on tid.
1756 if (common_encryption_on(conn->smb1.trans_enc)
1757 && (CVAL(inbuf, 0) == 0)) {
1758 uint16_t enc_ctx_num;
1760 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1761 if (!NT_STATUS_IS_OK(status)) {
1762 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1763 nt_errstr(status)));
1767 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1768 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1770 conn->smb1.trans_enc->enc_ctx_num));
1771 return NT_STATUS_INVALID_HANDLE;
1774 status = common_decrypt_buffer(conn->smb1.trans_enc,
1776 if (!NT_STATUS_IS_OK(status)) {
1777 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1778 nt_errstr(status)));
1783 mid = SVAL(inhdr, HDR_MID);
1784 num_pending = talloc_array_length(conn->pending);
1786 for (i=0; i<num_pending; i++) {
1787 if (mid == smb1cli_req_mid(conn->pending[i])) {
1791 if (i == num_pending) {
1792 /* Dump unexpected reply */
1793 return NT_STATUS_RETRY;
1796 oplock_break = false;
1798 if (mid == 0xffff) {
1800 * Paranoia checks that this is really an oplock break request.
1802 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1803 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1804 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1805 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1806 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1808 if (!oplock_break) {
1809 /* Dump unexpected reply */
1810 return NT_STATUS_RETRY;
1814 req = conn->pending[i];
1815 state = tevent_req_data(req, struct smbXcli_req_state);
1817 if (!oplock_break /* oplock breaks are not signed */
1818 && !smb_signing_check_pdu(conn->smb1.signing,
1819 inbuf, state->smb1.seqnum+1)) {
1820 DEBUG(10, ("cli_check_sign_mac failed\n"));
1821 return NT_STATUS_ACCESS_DENIED;
1824 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1826 if (!NT_STATUS_IS_OK(status)) {
1827 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1828 nt_errstr(status)));
1832 cmd = CVAL(inhdr, HDR_COM);
1833 status = smb1cli_pull_raw_error(inhdr);
1835 if (state->smb1.chained_requests == NULL) {
1837 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1840 smbXcli_req_unset_pending(req);
1842 if (state->smb1.recv_iov == NULL) {
1844 * For requests with more than
1845 * one response, we have to readd the
1848 state->smb1.recv_iov = talloc_zero_array(state,
1851 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1852 return NT_STATUS_OK;
1856 state->smb1.recv_cmd = cmd;
1857 state->smb1.recv_status = status;
1858 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1860 state->smb1.recv_iov[0] = iov[0];
1861 state->smb1.recv_iov[1] = iov[1];
1862 state->smb1.recv_iov[2] = iov[2];
1864 if (talloc_array_length(conn->pending) == 0) {
1865 tevent_req_done(req);
1866 return NT_STATUS_OK;
1869 tevent_req_defer_callback(req, state->ev);
1870 tevent_req_done(req);
1871 return NT_STATUS_RETRY;
1874 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1875 num_chained = talloc_array_length(chain);
1876 num_responses = (num_iov - 1)/2;
1878 if (num_responses > num_chained) {
1879 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1882 for (i=0; i<num_chained; i++) {
1883 size_t iov_idx = 1 + (i*2);
1884 struct iovec *cur = &iov[iov_idx];
1888 state = tevent_req_data(req, struct smbXcli_req_state);
1890 smbXcli_req_unset_pending(req);
1893 * as we finish multiple requests here
1894 * we need to defer the callbacks as
1895 * they could destroy our current stack state.
1897 tevent_req_defer_callback(req, state->ev);
1899 if (i >= num_responses) {
1900 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1904 if (state->smb1.recv_iov == NULL) {
1906 * For requests with more than
1907 * one response, we have to readd the
1910 state->smb1.recv_iov = talloc_zero_array(state,
1913 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1918 state->smb1.recv_cmd = cmd;
1920 if (i == (num_responses - 1)) {
1922 * The last request in the chain gets the status
1924 state->smb1.recv_status = status;
1926 cmd = CVAL(cur[0].iov_base, 0);
1927 state->smb1.recv_status = NT_STATUS_OK;
1930 state->inbuf = inbuf;
1933 * Note: here we use talloc_reference() in a way
1934 * that does not expose it to the caller.
1936 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1937 if (tevent_req_nomem(inbuf_ref, req)) {
1941 /* copy the related buffers */
1942 state->smb1.recv_iov[0] = iov[0];
1943 state->smb1.recv_iov[1] = cur[0];
1944 state->smb1.recv_iov[2] = cur[1];
1946 tevent_req_done(req);
1949 return NT_STATUS_RETRY;
1952 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1953 TALLOC_CTX *mem_ctx,
1954 struct iovec **piov,
1958 uint32_t *pvwv_offset,
1959 uint32_t *pnum_bytes,
1961 uint32_t *pbytes_offset,
1963 const struct smb1cli_req_expected_response *expected,
1964 size_t num_expected)
1966 struct smbXcli_req_state *state =
1967 tevent_req_data(req,
1968 struct smbXcli_req_state);
1969 NTSTATUS status = NT_STATUS_OK;
1970 struct iovec *recv_iov = NULL;
1971 uint8_t *hdr = NULL;
1973 uint32_t vwv_offset = 0;
1974 uint16_t *vwv = NULL;
1975 uint32_t num_bytes = 0;
1976 uint32_t bytes_offset = 0;
1977 uint8_t *bytes = NULL;
1979 bool found_status = false;
1980 bool found_size = false;
1994 if (pvwv_offset != NULL) {
1997 if (pnum_bytes != NULL) {
2000 if (pbytes != NULL) {
2003 if (pbytes_offset != NULL) {
2006 if (pinbuf != NULL) {
2010 if (state->inbuf != NULL) {
2011 recv_iov = state->smb1.recv_iov;
2012 state->smb1.recv_iov = NULL;
2013 if (state->smb1.recv_cmd != SMBreadBraw) {
2014 hdr = (uint8_t *)recv_iov[0].iov_base;
2015 wct = recv_iov[1].iov_len/2;
2016 vwv = (uint16_t *)recv_iov[1].iov_base;
2017 vwv_offset = PTR_DIFF(vwv, hdr);
2018 num_bytes = recv_iov[2].iov_len;
2019 bytes = (uint8_t *)recv_iov[2].iov_base;
2020 bytes_offset = PTR_DIFF(bytes, hdr);
2024 if (tevent_req_is_nterror(req, &status)) {
2025 for (i=0; i < num_expected; i++) {
2026 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2027 found_status = true;
2033 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2039 if (num_expected == 0) {
2040 found_status = true;
2044 status = state->smb1.recv_status;
2046 for (i=0; i < num_expected; i++) {
2047 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2051 found_status = true;
2052 if (expected[i].wct == 0) {
2057 if (expected[i].wct == wct) {
2063 if (!found_status) {
2068 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2072 *piov = talloc_move(mem_ctx, &recv_iov);
2084 if (pvwv_offset != NULL) {
2085 *pvwv_offset = vwv_offset;
2087 if (pnum_bytes != NULL) {
2088 *pnum_bytes = num_bytes;
2090 if (pbytes != NULL) {
2093 if (pbytes_offset != NULL) {
2094 *pbytes_offset = bytes_offset;
2096 if (pinbuf != NULL) {
2097 *pinbuf = state->inbuf;
2103 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2110 for (i=0; i<num_reqs; i++) {
2111 struct smbXcli_req_state *state;
2112 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2113 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2114 state->smb1.iov_count-2);
2115 wct_ofs = (wct_ofs + 3) & ~3;
2120 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2122 struct smbXcli_req_state *first_state =
2123 tevent_req_data(reqs[0],
2124 struct smbXcli_req_state);
2125 struct smbXcli_req_state *state;
2127 size_t chain_padding = 0;
2129 struct iovec *iov = NULL;
2130 struct iovec *this_iov;
2134 if (num_reqs == 1) {
2135 return smb1cli_req_writev_submit(reqs[0], first_state,
2136 first_state->smb1.iov,
2137 first_state->smb1.iov_count);
2141 for (i=0; i<num_reqs; i++) {
2142 if (!tevent_req_is_in_progress(reqs[i])) {
2143 return NT_STATUS_INTERNAL_ERROR;
2146 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2148 if (state->smb1.iov_count < 4) {
2149 return NT_STATUS_INVALID_PARAMETER_MIX;
2154 * The NBT and SMB header
2167 iovlen += state->smb1.iov_count - 2;
2170 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2172 return NT_STATUS_NO_MEMORY;
2175 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2176 first_state, reqs, sizeof(*reqs) * num_reqs);
2177 if (first_state->smb1.chained_requests == NULL) {
2179 return NT_STATUS_NO_MEMORY;
2182 wct_offset = HDR_WCT;
2185 for (i=0; i<num_reqs; i++) {
2186 size_t next_padding = 0;
2189 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2191 if (i < num_reqs-1) {
2192 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2193 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2195 TALLOC_FREE(first_state->smb1.chained_requests);
2196 return NT_STATUS_INVALID_PARAMETER_MIX;
2200 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2201 state->smb1.iov_count-2) + 1;
2202 if ((wct_offset % 4) != 0) {
2203 next_padding = 4 - (wct_offset % 4);
2205 wct_offset += next_padding;
2206 vwv = state->smb1.vwv;
2208 if (i < num_reqs-1) {
2209 struct smbXcli_req_state *next_state =
2210 tevent_req_data(reqs[i+1],
2211 struct smbXcli_req_state);
2212 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2214 SSVAL(vwv+1, 0, wct_offset);
2215 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2216 /* properly end the chain */
2217 SCVAL(vwv+0, 0, 0xff);
2218 SCVAL(vwv+0, 1, 0xff);
2224 * The NBT and SMB header
2226 this_iov[0] = state->smb1.iov[0];
2227 this_iov[1] = state->smb1.iov[1];
2231 * This one is a bit subtle. We have to add
2232 * chain_padding bytes between the requests, and we
2233 * have to also include the wct field of the
2234 * subsequent requests. We use the subsequent header
2235 * for the padding, it contains the wct field in its
2238 this_iov[0].iov_len = chain_padding+1;
2239 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2240 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2241 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2246 * copy the words and bytes
2248 memcpy(this_iov, state->smb1.iov+2,
2249 sizeof(struct iovec) * (state->smb1.iov_count-2));
2250 this_iov += state->smb1.iov_count - 2;
2251 chain_padding = next_padding;
2254 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2255 if (nbt_len > first_state->conn->smb1.max_xmit) {
2257 TALLOC_FREE(first_state->smb1.chained_requests);
2258 return NT_STATUS_INVALID_PARAMETER_MIX;
2261 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2262 if (!NT_STATUS_IS_OK(status)) {
2264 TALLOC_FREE(first_state->smb1.chained_requests);
2268 return NT_STATUS_OK;
2271 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2273 return ((tevent_queue_length(conn->outgoing) != 0)
2274 || (talloc_array_length(conn->pending) != 0));
2277 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2279 return conn->smb2.server.capabilities;
2282 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2284 return conn->smb2.server.security_mode;
2287 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2289 return conn->smb2.server.max_trans_size;
2292 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2294 return conn->smb2.server.max_read_size;
2297 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2299 return conn->smb2.server.max_write_size;
2302 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2303 uint16_t max_credits)
2305 conn->smb2.max_credits = max_credits;
2308 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2310 static bool smb2cli_req_cancel(struct tevent_req *req)
2312 struct smbXcli_req_state *state =
2313 tevent_req_data(req,
2314 struct smbXcli_req_state);
2315 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2316 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2317 uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2318 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2319 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2320 struct smbXcli_session *session = state->session;
2321 uint8_t *fixed = state->smb2.pad;
2322 uint16_t fixed_len = 4;
2323 struct tevent_req *subreq;
2324 struct smbXcli_req_state *substate;
2327 SSVAL(fixed, 0, 0x04);
2330 subreq = smb2cli_req_create(state, state->ev,
2338 if (subreq == NULL) {
2341 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2343 if (flags & SMB2_HDR_FLAG_ASYNC) {
2347 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2348 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2349 SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2350 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2351 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2353 status = smb2cli_req_compound_submit(&subreq, 1);
2354 if (!NT_STATUS_IS_OK(status)) {
2355 TALLOC_FREE(subreq);
2359 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2364 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2366 /* we do not care about the result */
2367 TALLOC_FREE(subreq);
2370 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2371 struct tevent_context *ev,
2372 struct smbXcli_conn *conn,
2374 uint32_t additional_flags,
2375 uint32_t clear_flags,
2376 uint32_t timeout_msec,
2379 struct smbXcli_session *session,
2380 const uint8_t *fixed,
2385 struct tevent_req *req;
2386 struct smbXcli_req_state *state;
2390 req = tevent_req_create(mem_ctx, &state,
2391 struct smbXcli_req_state);
2398 state->session = session;
2401 uid = session->smb2.session_id;
2404 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2405 if (state->smb2.recv_iov == NULL) {
2410 flags |= additional_flags;
2411 flags &= ~clear_flags;
2413 state->smb2.fixed = fixed;
2414 state->smb2.fixed_len = fixed_len;
2415 state->smb2.dyn = dyn;
2416 state->smb2.dyn_len = dyn_len;
2418 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2419 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2420 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2421 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2422 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2423 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2424 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2427 case SMB2_OP_CANCEL:
2428 state->one_way = true;
2432 * If this is a dummy request, it will have
2433 * UINT64_MAX as message id.
2434 * If we send on break acknowledgement,
2435 * this gets overwritten later.
2437 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2441 if (timeout_msec > 0) {
2442 struct timeval endtime;
2444 endtime = timeval_current_ofs_msec(timeout_msec);
2445 if (!tevent_req_set_endtime(req, ev, endtime)) {
2453 void smb2cli_req_set_notify_async(struct tevent_req *req)
2455 struct smbXcli_req_state *state =
2456 tevent_req_data(req,
2457 struct smbXcli_req_state);
2459 state->smb2.notify_async = true;
2462 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2463 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2464 TALLOC_CTX *tmp_mem,
2467 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2470 struct smbXcli_req_state *state;
2471 struct tevent_req *subreq;
2473 int i, num_iov, nbt_len;
2476 * 1 for the nbt length
2477 * per request: HDR, fixed, dyn, padding
2478 * -1 because the last one does not need padding
2481 iov = talloc_array(reqs[0], struct iovec, 1 + 4*num_reqs - 1);
2483 return NT_STATUS_NO_MEMORY;
2489 for (i=0; i<num_reqs; i++) {
2498 bool should_sign = false;
2500 if (!tevent_req_is_in_progress(reqs[i])) {
2501 return NT_STATUS_INTERNAL_ERROR;
2504 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2506 if (!smbXcli_conn_is_connected(state->conn)) {
2507 return NT_STATUS_CONNECTION_DISCONNECTED;
2510 if ((state->conn->protocol != PROTOCOL_NONE) &&
2511 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2512 return NT_STATUS_REVISION_MISMATCH;
2515 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2516 if (opcode == SMB2_OP_CANCEL) {
2520 avail = UINT64_MAX - state->conn->smb2.mid;
2522 return NT_STATUS_CONNECTION_ABORTED;
2525 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2526 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2531 charge = MAX(state->smb2.credit_charge, charge);
2533 avail = MIN(avail, state->conn->smb2.cur_credits);
2534 if (avail < charge) {
2535 return NT_STATUS_INTERNAL_ERROR;
2539 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2540 credits = state->conn->smb2.max_credits -
2541 state->conn->smb2.cur_credits;
2543 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2547 mid = state->conn->smb2.mid;
2548 state->conn->smb2.mid += charge;
2549 state->conn->smb2.cur_credits -= charge;
2551 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2552 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2554 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2555 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2559 iov[num_iov].iov_base = state->smb2.hdr;
2560 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2563 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2564 iov[num_iov].iov_len = state->smb2.fixed_len;
2567 if (state->smb2.dyn != NULL) {
2568 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2569 iov[num_iov].iov_len = state->smb2.dyn_len;
2573 reqlen = sizeof(state->smb2.hdr);
2574 reqlen += state->smb2.fixed_len;
2575 reqlen += state->smb2.dyn_len;
2577 if (i < num_reqs-1) {
2578 if ((reqlen % 8) > 0) {
2579 uint8_t pad = 8 - (reqlen % 8);
2580 iov[num_iov].iov_base = state->smb2.pad;
2581 iov[num_iov].iov_len = pad;
2585 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2589 if (state->session) {
2590 should_sign = state->session->smb2.should_sign;
2591 if (state->session->smb2.channel_setup) {
2599 status = smb2_signing_sign_pdu(state->session->smb2.signing_key,
2600 &iov[hdr_iov], num_iov - hdr_iov);
2601 if (!NT_STATUS_IS_OK(status)) {
2606 ret = smbXcli_req_set_pending(reqs[i]);
2608 return NT_STATUS_NO_MEMORY;
2612 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2613 _smb_setlen_tcp(state->length_hdr, nbt_len);
2614 iov[0].iov_base = state->length_hdr;
2615 iov[0].iov_len = sizeof(state->length_hdr);
2617 if (state->conn->dispatch_incoming == NULL) {
2618 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2621 subreq = writev_send(state, state->ev, state->conn->outgoing,
2622 state->conn->write_fd, false, iov, num_iov);
2623 if (subreq == NULL) {
2624 return NT_STATUS_NO_MEMORY;
2626 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2627 return NT_STATUS_OK;
2630 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2632 struct smbXcli_req_state *state =
2633 tevent_req_data(req,
2634 struct smbXcli_req_state);
2636 state->smb2.credit_charge = charge;
2639 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2640 struct tevent_context *ev,
2641 struct smbXcli_conn *conn,
2643 uint32_t additional_flags,
2644 uint32_t clear_flags,
2645 uint32_t timeout_msec,
2648 struct smbXcli_session *session,
2649 const uint8_t *fixed,
2654 struct tevent_req *req;
2657 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2658 additional_flags, clear_flags,
2661 fixed, fixed_len, dyn, dyn_len);
2665 if (!tevent_req_is_in_progress(req)) {
2666 return tevent_req_post(req, ev);
2668 status = smb2cli_req_compound_submit(&req, 1);
2669 if (tevent_req_nterror(req, status)) {
2670 return tevent_req_post(req, ev);
2675 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2677 struct tevent_req *req =
2678 tevent_req_callback_data(subreq,
2680 struct smbXcli_req_state *state =
2681 tevent_req_data(req,
2682 struct smbXcli_req_state);
2686 nwritten = writev_recv(subreq, &err);
2687 TALLOC_FREE(subreq);
2688 if (nwritten == -1) {
2689 /* here, we need to notify all pending requests */
2690 NTSTATUS status = map_nt_error_from_unix_common(err);
2691 smbXcli_conn_disconnect(state->conn, status);
2696 static NTSTATUS smb2cli_inbuf_parse_compound(uint8_t *buf, TALLOC_CTX *mem_ctx,
2697 struct iovec **piov, int *pnum_iov)
2707 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2709 return NT_STATUS_NO_MEMORY;
2712 buflen = smb_len_tcp(buf);
2714 first_hdr = buf + NBT_HDR_SIZE;
2716 while (taken < buflen) {
2717 size_t len = buflen - taken;
2718 uint8_t *hdr = first_hdr + taken;
2721 size_t next_command_ofs;
2723 struct iovec *iov_tmp;
2726 * We need the header plus the body length field
2729 if (len < SMB2_HDR_BODY + 2) {
2730 DEBUG(10, ("%d bytes left, expected at least %d\n",
2731 (int)len, SMB2_HDR_BODY));
2734 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2735 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2739 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2740 DEBUG(10, ("Got HDR len %d, expected %d\n",
2741 SVAL(hdr, 4), SMB2_HDR_BODY));
2746 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2747 body_size = SVAL(hdr, SMB2_HDR_BODY);
2749 if (next_command_ofs != 0) {
2750 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2753 if (next_command_ofs > full_size) {
2756 full_size = next_command_ofs;
2758 if (body_size < 2) {
2761 body_size &= 0xfffe;
2763 if (body_size > (full_size - SMB2_HDR_BODY)) {
2767 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2769 if (iov_tmp == NULL) {
2771 return NT_STATUS_NO_MEMORY;
2774 cur = &iov[num_iov];
2777 cur[0].iov_base = hdr;
2778 cur[0].iov_len = SMB2_HDR_BODY;
2779 cur[1].iov_base = hdr + SMB2_HDR_BODY;
2780 cur[1].iov_len = body_size;
2781 cur[2].iov_base = hdr + SMB2_HDR_BODY + body_size;
2782 cur[2].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2788 *pnum_iov = num_iov;
2789 return NT_STATUS_OK;
2793 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2796 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2799 size_t num_pending = talloc_array_length(conn->pending);
2802 for (i=0; i<num_pending; i++) {
2803 struct tevent_req *req = conn->pending[i];
2804 struct smbXcli_req_state *state =
2805 tevent_req_data(req,
2806 struct smbXcli_req_state);
2808 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2815 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2816 TALLOC_CTX *tmp_mem,
2819 struct tevent_req *req;
2820 struct smbXcli_req_state *state = NULL;
2825 struct smbXcli_session *last_session = NULL;
2827 status = smb2cli_inbuf_parse_compound(inbuf, tmp_mem,
2829 if (!NT_STATUS_IS_OK(status)) {
2833 for (i=0; i<num_iov; i+=3) {
2834 uint8_t *inbuf_ref = NULL;
2835 struct iovec *cur = &iov[i];
2836 uint8_t *inhdr = (uint8_t *)cur[0].iov_base;
2837 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
2838 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
2839 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
2840 uint16_t req_opcode;
2842 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
2843 uint32_t new_credits;
2844 struct smbXcli_session *session = NULL;
2845 const DATA_BLOB *signing_key = NULL;
2846 bool should_sign = false;
2848 new_credits = conn->smb2.cur_credits;
2849 new_credits += credits;
2850 if (new_credits > UINT16_MAX) {
2851 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2853 conn->smb2.cur_credits += credits;
2855 req = smb2cli_conn_find_pending(conn, mid);
2857 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2859 state = tevent_req_data(req, struct smbXcli_req_state);
2861 state->smb2.got_async = false;
2863 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2864 if (opcode != req_opcode) {
2865 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2867 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2869 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
2870 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2873 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
2874 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
2875 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
2876 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
2879 * async interim responses are not signed,
2880 * even if the SMB2_HDR_FLAG_SIGNED flag
2883 req_flags |= SMB2_HDR_FLAG_ASYNC;
2884 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
2885 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
2887 if (state->smb2.notify_async) {
2888 state->smb2.got_async = true;
2889 tevent_req_defer_callback(req, state->ev);
2890 tevent_req_notify_callback(req);
2895 session = state->session;
2896 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
2897 session = last_session;
2899 last_session = session;
2902 should_sign = session->smb2.should_sign;
2903 if (session->smb2.channel_setup) {
2909 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
2910 return NT_STATUS_ACCESS_DENIED;
2914 if (flags & SMB2_HDR_FLAG_SIGNED) {
2915 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
2917 if (session == NULL) {
2918 struct smbXcli_session *s;
2920 s = state->conn->sessions;
2921 for (; s; s = s->next) {
2922 if (s->smb2.session_id != uid) {
2931 if (session == NULL) {
2932 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2935 last_session = session;
2936 signing_key = &session->smb2.signing_key;
2939 if ((opcode == SMB2_OP_SESSSETUP) &&
2940 NT_STATUS_IS_OK(status)) {
2942 * the caller has to check the signing
2943 * as only the caller knows the correct
2949 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
2951 * if the server returns NT_STATUS_USER_SESSION_DELETED
2952 * the response is not signed and we should
2953 * propagate the NT_STATUS_USER_SESSION_DELETED
2954 * status to the caller.
2961 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
2962 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
2963 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2965 * if the server returns
2966 * NT_STATUS_NETWORK_NAME_DELETED
2967 * NT_STATUS_FILE_CLOSED
2968 * NT_STATUS_INVALID_PARAMETER
2969 * the response might not be signed
2970 * as this happens before the signing checks.
2972 * If server echos the signature (or all zeros)
2973 * we should report the status from the server
2979 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
2980 state->smb2.hdr+SMB2_HDR_SIGNATURE,
2983 state->smb2.signing_skipped = true;
2989 static const uint8_t zeros[16];
2991 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
2995 state->smb2.signing_skipped = true;
3002 status = smb2_signing_check_pdu(*signing_key, cur, 3);
3003 if (!NT_STATUS_IS_OK(status)) {
3005 * If the signing check fails, we disconnect
3012 smbXcli_req_unset_pending(req);
3015 * There might be more than one response
3016 * we need to defer the notifications
3018 if ((num_iov == 4) && (talloc_array_length(conn->pending) == 0)) {
3023 tevent_req_defer_callback(req, state->ev);
3027 * Note: here we use talloc_reference() in a way
3028 * that does not expose it to the caller.
3030 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3031 if (tevent_req_nomem(inbuf_ref, req)) {
3035 /* copy the related buffers */
3036 state->smb2.recv_iov[0] = cur[0];
3037 state->smb2.recv_iov[1] = cur[1];
3038 state->smb2.recv_iov[2] = cur[2];
3040 tevent_req_done(req);
3044 return NT_STATUS_RETRY;
3047 return NT_STATUS_OK;
3050 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3051 struct iovec **piov,
3052 const struct smb2cli_req_expected_response *expected,
3053 size_t num_expected)
3055 struct smbXcli_req_state *state =
3056 tevent_req_data(req,
3057 struct smbXcli_req_state);
3060 bool found_status = false;
3061 bool found_size = false;
3068 if (state->smb2.got_async) {
3069 return STATUS_PENDING;
3072 if (tevent_req_is_nterror(req, &status)) {
3073 for (i=0; i < num_expected; i++) {
3074 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3075 found_status = true;
3081 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3087 if (num_expected == 0) {
3088 found_status = true;
3092 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3093 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3095 for (i=0; i < num_expected; i++) {
3096 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3100 found_status = true;
3101 if (expected[i].body_size == 0) {
3106 if (expected[i].body_size == body_size) {
3112 if (!found_status) {
3116 if (state->smb2.signing_skipped) {
3117 if (num_expected > 0) {
3118 return NT_STATUS_ACCESS_DENIED;
3120 if (!NT_STATUS_IS_ERR(status)) {
3121 return NT_STATUS_ACCESS_DENIED;
3126 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3130 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3136 static const struct {
3137 enum protocol_types proto;
3138 const char *smb1_name;
3139 } smb1cli_prots[] = {
3140 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3141 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3142 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3143 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3144 {PROTOCOL_LANMAN2, "LM1.2X002"},
3145 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3146 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3147 {PROTOCOL_LANMAN2, "Samba"},
3148 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3149 {PROTOCOL_NT1, "NT LM 0.12"},
3150 {PROTOCOL_SMB2_02, "SMB 2.002"},
3151 {PROTOCOL_SMB2_10, "SMB 2.???"},
3154 static const struct {
3155 enum protocol_types proto;
3156 uint16_t smb2_dialect;
3157 } smb2cli_prots[] = {
3158 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3159 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3160 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3161 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3164 struct smbXcli_negprot_state {
3165 struct smbXcli_conn *conn;
3166 struct tevent_context *ev;
3167 uint32_t timeout_msec;
3168 enum protocol_types min_protocol;
3169 enum protocol_types max_protocol;
3173 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3177 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3178 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3179 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3180 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3181 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3182 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3186 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3187 struct tevent_context *ev,
3188 struct smbXcli_conn *conn,
3189 uint32_t timeout_msec,
3190 enum protocol_types min_protocol,
3191 enum protocol_types max_protocol)
3193 struct tevent_req *req, *subreq;
3194 struct smbXcli_negprot_state *state;
3196 req = tevent_req_create(mem_ctx, &state,
3197 struct smbXcli_negprot_state);
3203 state->timeout_msec = timeout_msec;
3204 state->min_protocol = min_protocol;
3205 state->max_protocol = max_protocol;
3207 if (min_protocol == PROTOCOL_NONE) {
3208 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3209 return tevent_req_post(req, ev);
3212 if (max_protocol == PROTOCOL_NONE) {
3213 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3214 return tevent_req_post(req, ev);
3217 if (min_protocol > max_protocol) {
3218 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3219 return tevent_req_post(req, ev);
3222 if ((min_protocol < PROTOCOL_SMB2_02) &&
3223 (max_protocol < PROTOCOL_SMB2_02)) {
3227 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3229 subreq = smbXcli_negprot_smb1_subreq(state);
3230 if (tevent_req_nomem(subreq, req)) {
3231 return tevent_req_post(req, ev);
3233 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3237 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3238 (max_protocol >= PROTOCOL_SMB2_02)) {
3242 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3244 subreq = smbXcli_negprot_smb2_subreq(state);
3245 if (tevent_req_nomem(subreq, req)) {
3246 return tevent_req_post(req, ev);
3248 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3253 * We send an SMB1 negprot with the SMB2 dialects
3254 * and expect a SMB1 or a SMB2 response.
3256 * smbXcli_negprot_dispatch_incoming() will fix the
3257 * callback to match protocol of the response.
3259 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3261 subreq = smbXcli_negprot_smb1_subreq(state);
3262 if (tevent_req_nomem(subreq, req)) {
3263 return tevent_req_post(req, ev);
3265 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3269 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3271 struct tevent_req *req =
3272 tevent_req_callback_data(subreq,
3277 * we just want the low level error
3279 status = tevent_req_simple_recv_ntstatus(subreq);
3280 TALLOC_FREE(subreq);
3281 if (tevent_req_nterror(req, status)) {
3285 /* this should never happen */
3286 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3289 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3292 DATA_BLOB bytes = data_blob_null;
3296 /* setup the protocol strings */
3297 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3301 if (smb1cli_prots[i].proto < state->min_protocol) {
3305 if (smb1cli_prots[i].proto > state->max_protocol) {
3309 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3315 * We now it is already ascii and
3316 * we want NULL termination.
3318 ok = data_blob_append(state, &bytes,
3319 smb1cli_prots[i].smb1_name,
3320 strlen(smb1cli_prots[i].smb1_name)+1);
3326 smb1cli_req_flags(state->max_protocol,
3327 state->conn->smb1.client.capabilities,
3332 return smb1cli_req_send(state, state->ev, state->conn,
3336 state->timeout_msec,
3337 0xFFFE, 0, 0, /* pid, tid, uid */
3338 0, NULL, /* wct, vwv */
3339 bytes.length, bytes.data);
3342 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3344 struct tevent_req *req =
3345 tevent_req_callback_data(subreq,
3347 struct smbXcli_negprot_state *state =
3348 tevent_req_data(req,
3349 struct smbXcli_negprot_state);
3350 struct smbXcli_conn *conn = state->conn;
3351 struct iovec *recv_iov = NULL;
3360 size_t num_prots = 0;
3362 uint32_t client_capabilities = conn->smb1.client.capabilities;
3363 uint32_t both_capabilities;
3364 uint32_t server_capabilities = 0;
3365 uint32_t capabilities;
3366 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3367 uint32_t server_max_xmit = 0;
3369 uint32_t server_max_mux = 0;
3370 uint16_t server_security_mode = 0;
3371 uint32_t server_session_key = 0;
3372 bool server_readbraw = false;
3373 bool server_writebraw = false;
3374 bool server_lockread = false;
3375 bool server_writeunlock = false;
3376 struct GUID server_guid = GUID_zero();
3377 DATA_BLOB server_gss_blob = data_blob_null;
3378 uint8_t server_challenge[8];
3379 char *server_workgroup = NULL;
3380 char *server_name = NULL;
3381 int server_time_zone = 0;
3382 NTTIME server_system_time = 0;
3383 static const struct smb1cli_req_expected_response expected[] = {
3385 .status = NT_STATUS_OK,
3386 .wct = 0x11, /* NT1 */
3389 .status = NT_STATUS_OK,
3390 .wct = 0x0D, /* LM */
3393 .status = NT_STATUS_OK,
3394 .wct = 0x01, /* CORE */
3398 ZERO_STRUCT(server_challenge);
3400 status = smb1cli_req_recv(subreq, state,
3405 NULL, /* pvwv_offset */
3408 NULL, /* pbytes_offset */
3410 expected, ARRAY_SIZE(expected));
3411 TALLOC_FREE(subreq);
3412 if (tevent_req_nterror(req, status)) {
3416 flags = CVAL(inhdr, HDR_FLG);
3418 protnum = SVAL(vwv, 0);
3420 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3421 if (smb1cli_prots[i].proto < state->min_protocol) {
3425 if (smb1cli_prots[i].proto > state->max_protocol) {
3429 if (protnum != num_prots) {
3434 conn->protocol = smb1cli_prots[i].proto;
3438 if (conn->protocol == PROTOCOL_NONE) {
3439 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3443 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3444 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3445 "and the selected protocol level doesn't support it.\n"));
3446 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3450 if (flags & FLAG_SUPPORT_LOCKREAD) {
3451 server_lockread = true;
3452 server_writeunlock = true;
3455 if (conn->protocol >= PROTOCOL_NT1) {
3456 const char *client_signing = NULL;
3457 bool server_mandatory = false;
3458 bool server_allowed = false;
3459 const char *server_signing = NULL;
3464 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3469 server_security_mode = CVAL(vwv + 1, 0);
3470 server_max_mux = SVAL(vwv + 1, 1);
3471 server_max_xmit = IVAL(vwv + 3, 1);
3472 server_session_key = IVAL(vwv + 7, 1);
3473 server_time_zone = SVALS(vwv + 15, 1);
3474 server_time_zone *= 60;
3475 /* this time arrives in real GMT */
3476 server_system_time = BVAL(vwv + 11, 1);
3477 server_capabilities = IVAL(vwv + 9, 1);
3479 key_len = CVAL(vwv + 16, 1);
3481 if (server_capabilities & CAP_RAW_MODE) {
3482 server_readbraw = true;
3483 server_writebraw = true;
3485 if (server_capabilities & CAP_LOCK_AND_READ) {
3486 server_lockread = true;
3489 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3490 DATA_BLOB blob1, blob2;
3492 if (num_bytes < 16) {
3493 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3497 blob1 = data_blob_const(bytes, 16);
3498 status = GUID_from_data_blob(&blob1, &server_guid);
3499 if (tevent_req_nterror(req, status)) {
3503 blob1 = data_blob_const(bytes+16, num_bytes-16);
3504 blob2 = data_blob_dup_talloc(state, blob1);
3505 if (blob1.length > 0 &&
3506 tevent_req_nomem(blob2.data, req)) {
3509 server_gss_blob = blob2;
3511 DATA_BLOB blob1, blob2;
3513 if (num_bytes < key_len) {
3514 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3518 if (key_len != 0 && key_len != 8) {
3519 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3524 memcpy(server_challenge, bytes, 8);
3527 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3528 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3529 if (blob1.length > 0) {
3532 len = utf16_len_n(blob1.data,
3536 ok = convert_string_talloc(state,
3544 status = map_nt_error_from_unix_common(errno);
3545 tevent_req_nterror(req, status);
3550 blob2.data += blob1.length;
3551 blob2.length -= blob1.length;
3552 if (blob2.length > 0) {
3555 len = utf16_len_n(blob1.data,
3559 ok = convert_string_talloc(state,
3567 status = map_nt_error_from_unix_common(errno);
3568 tevent_req_nterror(req, status);
3574 client_signing = "disabled";
3575 if (conn->allow_signing) {
3576 client_signing = "allowed";
3578 if (conn->mandatory_signing) {
3579 client_signing = "required";
3582 server_signing = "not supported";
3583 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3584 server_signing = "supported";
3585 server_allowed = true;
3587 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3588 server_signing = "required";
3589 server_mandatory = true;
3592 ok = smb_signing_set_negotiated(conn->smb1.signing,
3596 DEBUG(1,("cli_negprot: SMB signing is required, "
3597 "but client[%s] and server[%s] mismatch\n",
3598 client_signing, server_signing));
3599 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3603 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3609 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3613 server_security_mode = SVAL(vwv + 1, 0);
3614 server_max_xmit = SVAL(vwv + 2, 0);
3615 server_max_mux = SVAL(vwv + 3, 0);
3616 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3617 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3618 server_session_key = IVAL(vwv + 6, 0);
3619 server_time_zone = SVALS(vwv + 10, 0);
3620 server_time_zone *= 60;
3621 /* this time is converted to GMT by make_unix_date */
3622 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3623 unix_to_nt_time(&server_system_time, t);
3624 key_len = SVAL(vwv + 11, 0);
3626 if (num_bytes < key_len) {
3627 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3631 if (key_len != 0 && key_len != 8) {
3632 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3637 memcpy(server_challenge, bytes, 8);
3640 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3641 if (blob1.length > 0) {
3645 len = utf16_len_n(blob1.data,
3649 ok = convert_string_talloc(state,
3657 status = map_nt_error_from_unix_common(errno);
3658 tevent_req_nterror(req, status);
3664 /* the old core protocol */
3665 server_time_zone = get_time_zone(time(NULL));
3666 server_max_xmit = 1024;
3670 if (server_max_xmit < 1024) {
3671 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3675 if (server_max_mux < 1) {
3676 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3681 * Now calculate the negotiated capabilities
3682 * based on the mask for:
3683 * - client only flags
3684 * - flags used in both directions
3685 * - server only flags
3687 both_capabilities = client_capabilities & server_capabilities;
3688 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3689 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3690 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3692 max_xmit = MIN(client_max_xmit, server_max_xmit);
3694 conn->smb1.server.capabilities = server_capabilities;
3695 conn->smb1.capabilities = capabilities;
3697 conn->smb1.server.max_xmit = server_max_xmit;
3698 conn->smb1.max_xmit = max_xmit;
3700 conn->smb1.server.max_mux = server_max_mux;
3702 conn->smb1.server.security_mode = server_security_mode;
3704 conn->smb1.server.readbraw = server_readbraw;
3705 conn->smb1.server.writebraw = server_writebraw;
3706 conn->smb1.server.lockread = server_lockread;
3707 conn->smb1.server.writeunlock = server_writeunlock;
3709 conn->smb1.server.session_key = server_session_key;
3711 talloc_steal(conn, server_gss_blob.data);
3712 conn->smb1.server.gss_blob = server_gss_blob;
3713 conn->smb1.server.guid = server_guid;
3714 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3715 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3716 conn->smb1.server.name = talloc_move(conn, &server_name);
3718 conn->smb1.server.time_zone = server_time_zone;
3719 conn->smb1.server.system_time = server_system_time;
3721 tevent_req_done(req);
3724 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3728 uint16_t dialect_count = 0;
3730 buf = state->smb2.dyn;
3731 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3732 if (smb2cli_prots[i].proto < state->min_protocol) {
3736 if (smb2cli_prots[i].proto > state->max_protocol) {
3740 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3744 buf = state->smb2.fixed;
3746 SSVAL(buf, 2, dialect_count);
3747 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3748 SSVAL(buf, 6, 0); /* Reserved */
3749 SSVAL(buf, 8, 0); /* Capabilities */
3750 if (state->max_protocol >= PROTOCOL_SMB2_10) {
3754 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3756 if (!NT_STATUS_IS_OK(status)) {
3759 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3761 memset(buf+12, 0, 16); /* ClientGuid */
3763 SBVAL(buf, 28, 0); /* ClientStartTime */
3765 return smb2cli_req_send(state, state->ev,
3766 state->conn, SMB2_OP_NEGPROT,
3768 state->timeout_msec,
3769 0xFEFF, 0, NULL, /* pid, tid, session */
3770 state->smb2.fixed, sizeof(state->smb2.fixed),
3771 state->smb2.dyn, dialect_count*2);
3774 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3776 struct tevent_req *req =
3777 tevent_req_callback_data(subreq,
3779 struct smbXcli_negprot_state *state =
3780 tevent_req_data(req,
3781 struct smbXcli_negprot_state);
3782 struct smbXcli_conn *conn = state->conn;
3783 size_t security_offset, security_length;
3789 uint16_t dialect_revision;
3790 static const struct smb2cli_req_expected_response expected[] = {
3792 .status = NT_STATUS_OK,
3797 status = smb2cli_req_recv(subreq, state, &iov,
3798 expected, ARRAY_SIZE(expected));
3799 TALLOC_FREE(subreq);
3800 if (tevent_req_nterror(req, status)) {
3804 body = (uint8_t *)iov[1].iov_base;
3806 dialect_revision = SVAL(body, 4);
3808 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3809 if (smb2cli_prots[i].proto < state->min_protocol) {
3813 if (smb2cli_prots[i].proto > state->max_protocol) {
3817 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
3821 conn->protocol = smb2cli_prots[i].proto;
3825 if (conn->protocol == PROTOCOL_NONE) {
3826 if (state->min_protocol >= PROTOCOL_SMB2_02) {
3827 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3831 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
3832 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3836 /* make sure we do not loop forever */
3837 state->min_protocol = PROTOCOL_SMB2_02;
3840 * send a SMB2 negprot, in order to negotiate
3843 subreq = smbXcli_negprot_smb2_subreq(state);
3844 if (tevent_req_nomem(subreq, req)) {
3847 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3851 conn->smb2.server.security_mode = SVAL(body, 2);
3853 blob = data_blob_const(body + 8, 16);
3854 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
3855 if (tevent_req_nterror(req, status)) {
3859 conn->smb2.server.capabilities = IVAL(body, 24);
3860 conn->smb2.server.max_trans_size= IVAL(body, 28);
3861 conn->smb2.server.max_read_size = IVAL(body, 32);
3862 conn->smb2.server.max_write_size= IVAL(body, 36);
3863 conn->smb2.server.system_time = BVAL(body, 40);
3864 conn->smb2.server.start_time = BVAL(body, 48);
3866 security_offset = SVAL(body, 56);
3867 security_length = SVAL(body, 58);
3869 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
3870 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3874 if (security_length > iov[2].iov_len) {
3875 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3879 conn->smb2.server.gss_blob = data_blob_talloc(conn,
3882 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
3886 tevent_req_done(req);
3889 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3890 TALLOC_CTX *tmp_mem,
3893 size_t num_pending = talloc_array_length(conn->pending);
3894 struct tevent_req *subreq;
3895 struct smbXcli_req_state *substate;
3896 struct tevent_req *req;
3897 uint32_t protocol_magic = IVAL(inbuf, 4);
3899 if (num_pending != 1) {
3900 return NT_STATUS_INTERNAL_ERROR;
3903 subreq = conn->pending[0];
3904 substate = tevent_req_data(subreq, struct smbXcli_req_state);
3905 req = tevent_req_callback_data(subreq, struct tevent_req);
3907 switch (protocol_magic) {
3909 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3910 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3911 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3914 if (substate->smb2.recv_iov == NULL) {
3916 * For the SMB1 negprot we have move it.
3918 substate->smb2.recv_iov = substate->smb1.recv_iov;
3919 substate->smb1.recv_iov = NULL;
3923 * we got an SMB2 answer, which consumed sequence number 0
3924 * so we need to use 1 as the next one
3927 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3928 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3929 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3932 DEBUG(10, ("Got non-SMB PDU\n"));
3933 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3936 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
3938 return tevent_req_simple_recv_ntstatus(req);
3941 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
3942 uint32_t timeout_msec,
3943 enum protocol_types min_protocol,
3944 enum protocol_types max_protocol)
3946 TALLOC_CTX *frame = talloc_stackframe();
3947 struct tevent_context *ev;
3948 struct tevent_req *req;
3949 NTSTATUS status = NT_STATUS_NO_MEMORY;
3952 if (smbXcli_conn_has_async_calls(conn)) {
3954 * Can't use sync call while an async call is in flight
3956 status = NT_STATUS_INVALID_PARAMETER_MIX;
3959 ev = tevent_context_init(frame);
3963 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
3964 min_protocol, max_protocol);
3968 ok = tevent_req_poll(req, ev);
3970 status = map_nt_error_from_unix_common(errno);
3973 status = smbXcli_negprot_recv(req);
3979 static int smbXcli_session_destructor(struct smbXcli_session *session)
3981 if (session->conn == NULL) {
3985 DLIST_REMOVE(session->conn->sessions, session);
3989 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
3990 struct smbXcli_conn *conn)
3992 struct smbXcli_session *session;
3994 session = talloc_zero(mem_ctx, struct smbXcli_session);
3995 if (session == NULL) {
3998 talloc_set_destructor(session, smbXcli_session_destructor);
4000 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4001 session->conn = conn;
4006 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4008 struct smbXcli_conn *conn = session->conn;
4009 uint8_t security_mode = 0;
4012 return security_mode;
4015 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4016 if (conn->mandatory_signing) {
4017 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4020 return security_mode;
4023 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4025 return session->smb2.session_id;
4028 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4029 uint64_t session_id,
4030 uint16_t session_flags)
4032 session->smb2.session_id = session_id;
4033 session->smb2.session_flags = session_flags;
4036 NTSTATUS smb2cli_session_update_session_key(struct smbXcli_session *session,
4037 const DATA_BLOB session_key,
4038 const struct iovec *recv_iov)
4040 struct smbXcli_conn *conn = session->conn;
4041 uint16_t no_sign_flags;
4042 DATA_BLOB signing_key;
4046 return NT_STATUS_INVALID_PARAMETER_MIX;
4049 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4051 if (session->smb2.session_flags & no_sign_flags) {
4052 session->smb2.should_sign = false;
4053 return NT_STATUS_OK;
4056 if (session->smb2.signing_key.length > 0) {
4057 signing_key = session->smb2.signing_key;
4059 signing_key = session_key;
4061 if (session->smb2.channel_setup) {
4062 signing_key = session_key;
4065 status = smb2_signing_check_pdu(signing_key, recv_iov, 3);
4066 if (!NT_STATUS_IS_OK(status)) {
4070 if (!session->smb2.channel_setup) {
4071 session->smb2.session_key = data_blob_dup_talloc(session,
4073 if (session->smb2.session_key.data == NULL) {
4074 return NT_STATUS_NO_MEMORY;
4078 if (session->smb2.channel_setup) {
4079 data_blob_free(&session->smb2.signing_key);
4080 session->smb2.channel_setup = false;
4083 if (session->smb2.signing_key.length > 0) {
4084 return NT_STATUS_OK;
4087 session->smb2.signing_key = data_blob_dup_talloc(session, signing_key);
4088 if (session->smb2.signing_key.data == NULL) {
4089 return NT_STATUS_NO_MEMORY;
4092 session->smb2.should_sign = false;
4094 if (conn->desire_signing) {
4095 session->smb2.should_sign = true;
4098 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4099 session->smb2.should_sign = true;
4102 return NT_STATUS_OK;
4105 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4106 struct smbXcli_session *session1,
4107 struct smbXcli_conn *conn,
4108 struct smbXcli_session **_session2)
4110 struct smbXcli_session *session2;
4112 if (session1->smb2.signing_key.length == 0) {
4113 return NT_STATUS_INVALID_PARAMETER_MIX;
4117 return NT_STATUS_INVALID_PARAMETER_MIX;
4120 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4121 if (session2 == NULL) {
4122 return NT_STATUS_NO_MEMORY;
4124 session2->smb2.session_id = session1->smb2.session_id;
4125 session2->smb2.session_flags = session1->smb2.session_flags;
4127 session2->smb2.session_key = data_blob_dup_talloc(session2,
4128 session1->smb2.session_key);
4129 if (session2->smb2.session_key.data == NULL) {
4130 return NT_STATUS_NO_MEMORY;
4133 session2->smb2.signing_key = data_blob_dup_talloc(session2,
4134 session1->smb2.signing_key);
4135 if (session2->smb2.signing_key.data == NULL) {
4136 return NT_STATUS_NO_MEMORY;
4139 session2->smb2.should_sign = session1->smb2.should_sign;
4140 session2->smb2.channel_setup = true;
4142 talloc_set_destructor(session2, smbXcli_session_destructor);
4143 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4144 session2->conn = conn;
4146 *_session2 = session2;
4147 return NT_STATUS_OK;