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;
43 struct sockaddr_storage local_ss;
44 struct sockaddr_storage remote_ss;
45 const char *remote_name;
47 struct tevent_queue *outgoing;
48 struct tevent_req **pending;
49 struct tevent_req *read_smb_req;
51 enum protocol_types protocol;
54 bool mandatory_signing;
57 * The incoming dispatch function should return:
58 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
59 * - NT_STATUS_OK, if no more processing is desired, e.g.
60 * the dispatch function called
62 * - All other return values disconnect the connection.
64 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
70 uint32_t capabilities;
75 uint32_t capabilities;
78 uint16_t security_mode;
87 const char *workgroup;
93 uint32_t capabilities;
98 struct smb_signing_state *signing;
99 struct smb_trans_enc_state *trans_enc;
101 struct tevent_req *read_braw_req;
106 uint32_t capabilities;
107 uint16_t security_mode;
112 uint32_t capabilities;
113 uint16_t security_mode;
115 uint32_t max_trans_size;
116 uint32_t max_read_size;
117 uint32_t max_write_size;
124 uint16_t cur_credits;
125 uint16_t max_credits;
128 struct smbXcli_session *sessions;
131 struct smb2cli_session {
133 uint16_t session_flags;
134 DATA_BLOB application_key;
135 DATA_BLOB signing_key;
138 DATA_BLOB encryption_key;
139 DATA_BLOB decryption_key;
142 uint16_t channel_sequence;
145 struct smbXcli_session {
146 struct smbXcli_session *prev, *next;
147 struct smbXcli_conn *conn;
151 DATA_BLOB application_key;
155 struct smb2cli_session *smb2;
158 DATA_BLOB signing_key;
162 struct smbXcli_tcon {
165 uint16_t optional_support;
166 uint32_t maximal_access;
167 uint32_t guest_maximal_access;
176 uint32_t capabilities;
177 uint32_t maximal_access;
182 struct smbXcli_req_state {
183 struct tevent_context *ev;
184 struct smbXcli_conn *conn;
185 struct smbXcli_session *session; /* maybe NULL */
186 struct smbXcli_tcon *tcon; /* maybe NULL */
188 uint8_t length_hdr[4];
195 /* Space for the header including the wct */
196 uint8_t hdr[HDR_VWV];
199 * For normal requests, smb1cli_req_send chooses a mid.
200 * SecondaryV trans requests need to use the mid of the primary
201 * request, so we need a place to store it.
202 * Assume it is set if != 0.
207 uint8_t bytecount_buf[2];
209 #define MAX_SMB_IOV 10
210 /* length_hdr, hdr, words, byte_count, buffers */
211 struct iovec iov[1 + 3 + MAX_SMB_IOV];
216 struct tevent_req **chained_requests;
219 NTSTATUS recv_status;
220 /* always an array of 3 talloc elements */
221 struct iovec *recv_iov;
225 const uint8_t *fixed;
230 uint8_t transform[SMB2_TF_HDR_SIZE];
231 uint8_t hdr[SMB2_HDR_BODY];
232 uint8_t pad[7]; /* padding space for compounding */
235 * always an array of 3 talloc elements
236 * (without a SMB2_TRANSFORM header!)
240 struct iovec *recv_iov;
242 uint16_t credit_charge;
247 bool signing_skipped;
253 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
256 * NT_STATUS_OK, means we do not notify the callers
258 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
260 while (conn->sessions) {
261 conn->sessions->conn = NULL;
262 DLIST_REMOVE(conn->sessions, conn->sessions);
265 if (conn->smb1.trans_enc) {
266 TALLOC_FREE(conn->smb1.trans_enc);
272 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
274 const char *remote_name,
275 enum smb_signing_setting signing_state,
276 uint32_t smb1_capabilities,
277 struct GUID *client_guid,
278 uint32_t smb2_capabilities)
280 struct smbXcli_conn *conn = NULL;
282 struct sockaddr *sa = NULL;
286 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
292 conn->write_fd = dup(fd);
293 if (conn->write_fd == -1) {
297 conn->remote_name = talloc_strdup(conn, remote_name);
298 if (conn->remote_name == NULL) {
303 ss = (void *)&conn->local_ss;
304 sa = (struct sockaddr *)ss;
305 sa_length = sizeof(conn->local_ss);
306 ret = getsockname(fd, sa, &sa_length);
310 ss = (void *)&conn->remote_ss;
311 sa = (struct sockaddr *)ss;
312 sa_length = sizeof(conn->remote_ss);
313 ret = getpeername(fd, sa, &sa_length);
318 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
319 if (conn->outgoing == NULL) {
322 conn->pending = NULL;
324 conn->protocol = PROTOCOL_NONE;
326 switch (signing_state) {
327 case SMB_SIGNING_OFF:
329 conn->allow_signing = false;
330 conn->desire_signing = false;
331 conn->mandatory_signing = false;
333 case SMB_SIGNING_DEFAULT:
334 case SMB_SIGNING_IF_REQUIRED:
335 /* if the server requires it */
336 conn->allow_signing = true;
337 conn->desire_signing = false;
338 conn->mandatory_signing = false;
340 case SMB_SIGNING_REQUIRED:
342 conn->allow_signing = true;
343 conn->desire_signing = true;
344 conn->mandatory_signing = true;
348 conn->smb1.client.capabilities = smb1_capabilities;
349 conn->smb1.client.max_xmit = UINT16_MAX;
351 conn->smb1.capabilities = conn->smb1.client.capabilities;
352 conn->smb1.max_xmit = 1024;
356 /* initialise signing */
357 conn->smb1.signing = smb_signing_init(conn,
359 conn->desire_signing,
360 conn->mandatory_signing);
361 if (!conn->smb1.signing) {
365 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
366 if (conn->mandatory_signing) {
367 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
370 conn->smb2.client.guid = *client_guid;
372 conn->smb2.client.capabilities = smb2_capabilities;
374 conn->smb2.cur_credits = 1;
375 conn->smb2.max_credits = 0;
377 talloc_set_destructor(conn, smbXcli_conn_destructor);
381 if (conn->write_fd != -1) {
382 close(conn->write_fd);
388 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
394 if (conn->read_fd == -1) {
401 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
403 return conn->protocol;
406 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
408 if (conn->protocol >= PROTOCOL_SMB2_02) {
412 if (conn->smb1.capabilities & CAP_UNICODE) {
419 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
421 set_socket_options(conn->read_fd, options);
424 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
426 return &conn->local_ss;
429 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
431 return &conn->remote_ss;
434 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
436 return conn->remote_name;
439 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
441 if (conn->protocol >= PROTOCOL_SMB2_02) {
448 return conn->smb1.server.max_mux;
451 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
453 if (conn->protocol >= PROTOCOL_SMB2_02) {
454 return conn->smb2.server.system_time;
457 return conn->smb1.server.system_time;
460 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
462 if (conn->protocol >= PROTOCOL_SMB2_02) {
463 return &conn->smb2.server.gss_blob;
466 return &conn->smb1.server.gss_blob;
469 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
471 if (conn->protocol >= PROTOCOL_SMB2_02) {
472 return &conn->smb2.server.guid;
475 return &conn->smb1.server.guid;
478 struct smbXcli_conn_samba_suicide_state {
479 struct smbXcli_conn *conn;
484 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
486 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
487 struct tevent_context *ev,
488 struct smbXcli_conn *conn,
491 struct tevent_req *req, *subreq;
492 struct smbXcli_conn_samba_suicide_state *state;
494 req = tevent_req_create(mem_ctx, &state,
495 struct smbXcli_conn_samba_suicide_state);
500 SIVAL(state->buf, 4, 0x74697865);
501 SCVAL(state->buf, 8, exitcode);
502 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
504 state->iov.iov_base = state->buf;
505 state->iov.iov_len = sizeof(state->buf);
507 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
508 false, &state->iov, 1);
509 if (tevent_req_nomem(subreq, req)) {
510 return tevent_req_post(req, ev);
512 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
516 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
518 struct tevent_req *req = tevent_req_callback_data(
519 subreq, struct tevent_req);
520 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
521 req, struct smbXcli_conn_samba_suicide_state);
525 nwritten = writev_recv(subreq, &err);
527 if (nwritten == -1) {
528 NTSTATUS status = map_nt_error_from_unix_common(err);
529 smbXcli_conn_disconnect(state->conn, status);
532 tevent_req_done(req);
535 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
537 return tevent_req_simple_recv_ntstatus(req);
540 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
543 TALLOC_CTX *frame = talloc_stackframe();
544 struct tevent_context *ev;
545 struct tevent_req *req;
546 NTSTATUS status = NT_STATUS_NO_MEMORY;
549 if (smbXcli_conn_has_async_calls(conn)) {
551 * Can't use sync call while an async call is in flight
553 status = NT_STATUS_INVALID_PARAMETER_MIX;
556 ev = tevent_context_init(frame);
560 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
564 ok = tevent_req_poll(req, ev);
566 status = map_nt_error_from_unix_common(errno);
569 status = smbXcli_conn_samba_suicide_recv(req);
575 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
577 return conn->smb1.capabilities;
580 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
582 return conn->smb1.max_xmit;
585 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
587 return conn->smb1.server.session_key;
590 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
592 return conn->smb1.server.challenge;
595 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
597 return conn->smb1.server.security_mode;
600 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
602 return conn->smb1.server.readbraw;
605 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
607 return conn->smb1.server.writebraw;
610 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
612 return conn->smb1.server.lockread;
615 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
617 return conn->smb1.server.writeunlock;
620 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
622 return conn->smb1.server.time_zone;
625 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
626 const DATA_BLOB user_session_key,
627 const DATA_BLOB response)
629 return smb_signing_activate(conn->smb1.signing,
634 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
635 const uint8_t *buf, uint32_t seqnum)
637 const uint8_t *hdr = buf + NBT_HDR_SIZE;
638 size_t len = smb_len_nbt(buf);
640 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
643 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
645 return smb_signing_is_active(conn->smb1.signing);
648 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
649 struct smb_trans_enc_state *es)
651 /* Replace the old state, if any. */
652 if (conn->smb1.trans_enc) {
653 TALLOC_FREE(conn->smb1.trans_enc);
655 conn->smb1.trans_enc = es;
658 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
660 return common_encryption_on(conn->smb1.trans_enc);
664 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
666 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
667 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
669 if (NT_STATUS_IS_OK(status)) {
673 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
677 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
681 * Is the SMB command able to hold an AND_X successor
682 * @param[in] cmd The SMB command in question
683 * @retval Can we add a chained request after "cmd"?
685 bool smb1cli_is_andx_req(uint8_t cmd)
705 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
707 size_t num_pending = talloc_array_length(conn->pending);
713 result = conn->smb1.mid++;
714 if ((result == 0) || (result == 0xffff)) {
718 for (i=0; i<num_pending; i++) {
719 if (result == smb1cli_req_mid(conn->pending[i])) {
724 if (i == num_pending) {
730 void smbXcli_req_unset_pending(struct tevent_req *req)
732 struct smbXcli_req_state *state =
734 struct smbXcli_req_state);
735 struct smbXcli_conn *conn = state->conn;
736 size_t num_pending = talloc_array_length(conn->pending);
739 if (state->smb1.mid != 0) {
741 * This is a [nt]trans[2] request which waits
742 * for more than one reply.
747 talloc_set_destructor(req, NULL);
749 if (num_pending == 1) {
751 * The pending read_smb tevent_req is a child of
752 * conn->pending. So if nothing is pending anymore, we need to
753 * delete the socket read fde.
755 TALLOC_FREE(conn->pending);
756 conn->read_smb_req = NULL;
760 for (i=0; i<num_pending; i++) {
761 if (req == conn->pending[i]) {
765 if (i == num_pending) {
767 * Something's seriously broken. Just returning here is the
768 * right thing nevertheless, the point of this routine is to
769 * remove ourselves from conn->pending.
775 * Remove ourselves from the conn->pending array
777 for (; i < (num_pending - 1); i++) {
778 conn->pending[i] = conn->pending[i+1];
782 * No NULL check here, we're shrinking by sizeof(void *), and
783 * talloc_realloc just adjusts the size for this.
785 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
790 static int smbXcli_req_destructor(struct tevent_req *req)
792 struct smbXcli_req_state *state =
794 struct smbXcli_req_state);
797 * Make sure we really remove it from
798 * the pending array on destruction.
801 smbXcli_req_unset_pending(req);
805 static bool smb1cli_req_cancel(struct tevent_req *req);
806 static bool smb2cli_req_cancel(struct tevent_req *req);
808 static bool smbXcli_req_cancel(struct tevent_req *req)
810 struct smbXcli_req_state *state =
812 struct smbXcli_req_state);
814 if (!smbXcli_conn_is_connected(state->conn)) {
818 if (state->conn->protocol == PROTOCOL_NONE) {
822 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
823 return smb2cli_req_cancel(req);
826 return smb1cli_req_cancel(req);
829 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
831 bool smbXcli_req_set_pending(struct tevent_req *req)
833 struct smbXcli_req_state *state =
835 struct smbXcli_req_state);
836 struct smbXcli_conn *conn;
837 struct tevent_req **pending;
842 if (!smbXcli_conn_is_connected(conn)) {
846 num_pending = talloc_array_length(conn->pending);
848 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
850 if (pending == NULL) {
853 pending[num_pending] = req;
854 conn->pending = pending;
855 talloc_set_destructor(req, smbXcli_req_destructor);
856 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
858 if (!smbXcli_conn_receive_next(conn)) {
860 * the caller should notify the current request
862 * And all other pending requests get notified
863 * by smbXcli_conn_disconnect().
865 smbXcli_req_unset_pending(req);
866 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
873 static void smbXcli_conn_received(struct tevent_req *subreq);
875 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
877 size_t num_pending = talloc_array_length(conn->pending);
878 struct tevent_req *req;
879 struct smbXcli_req_state *state;
881 if (conn->read_smb_req != NULL) {
885 if (num_pending == 0) {
886 if (conn->smb2.mid < UINT64_MAX) {
887 /* no more pending requests, so we are done for now */
892 * If there are no more SMB2 requests possible,
893 * because we are out of message ids,
894 * we need to disconnect.
896 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
900 req = conn->pending[0];
901 state = tevent_req_data(req, struct smbXcli_req_state);
904 * We're the first ones, add the read_smb request that waits for the
905 * answer from the server
907 conn->read_smb_req = read_smb_send(conn->pending,
910 if (conn->read_smb_req == NULL) {
913 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
917 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
919 struct smbXcli_session *session;
921 tevent_queue_stop(conn->outgoing);
923 if (conn->read_fd != -1) {
924 close(conn->read_fd);
926 if (conn->write_fd != -1) {
927 close(conn->write_fd);
932 session = conn->sessions;
933 if (talloc_array_length(conn->pending) == 0) {
935 * if we do not have pending requests
936 * there is no need to update the channel_sequence
940 for (; session; session = session->next) {
941 smb2cli_session_increment_channel_sequence(session);
945 * Cancel all pending requests. We do not do a for-loop walking
946 * conn->pending because that array changes in
947 * smbXcli_req_unset_pending.
949 while (talloc_array_length(conn->pending) > 0) {
950 struct tevent_req *req;
951 struct smbXcli_req_state *state;
952 struct tevent_req **chain;
956 req = conn->pending[0];
957 state = tevent_req_data(req, struct smbXcli_req_state);
959 if (state->smb1.chained_requests == NULL) {
961 * We're dead. No point waiting for trans2
966 smbXcli_req_unset_pending(req);
968 if (NT_STATUS_IS_OK(status)) {
969 /* do not notify the callers */
974 * we need to defer the callback, because we may notify
975 * more then one caller.
977 tevent_req_defer_callback(req, state->ev);
978 tevent_req_nterror(req, status);
982 chain = talloc_move(conn, &state->smb1.chained_requests);
983 num_chained = talloc_array_length(chain);
985 for (i=0; i<num_chained; i++) {
987 state = tevent_req_data(req, struct smbXcli_req_state);
990 * We're dead. No point waiting for trans2
995 smbXcli_req_unset_pending(req);
997 if (NT_STATUS_IS_OK(status)) {
998 /* do not notify the callers */
1003 * we need to defer the callback, because we may notify
1004 * more than one caller.
1006 tevent_req_defer_callback(req, state->ev);
1007 tevent_req_nterror(req, status);
1014 * Fetch a smb request's mid. Only valid after the request has been sent by
1015 * smb1cli_req_send().
1017 uint16_t smb1cli_req_mid(struct tevent_req *req)
1019 struct smbXcli_req_state *state =
1020 tevent_req_data(req,
1021 struct smbXcli_req_state);
1023 if (state->smb1.mid != 0) {
1024 return state->smb1.mid;
1027 return SVAL(state->smb1.hdr, HDR_MID);
1030 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1032 struct smbXcli_req_state *state =
1033 tevent_req_data(req,
1034 struct smbXcli_req_state);
1036 state->smb1.mid = mid;
1039 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1041 struct smbXcli_req_state *state =
1042 tevent_req_data(req,
1043 struct smbXcli_req_state);
1045 return state->smb1.seqnum;
1048 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1050 struct smbXcli_req_state *state =
1051 tevent_req_data(req,
1052 struct smbXcli_req_state);
1054 state->smb1.seqnum = seqnum;
1057 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1061 for (i=0; i<count; i++) {
1062 result += iov[i].iov_len;
1067 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1068 const struct iovec *iov,
1071 size_t len = smbXcli_iov_len(iov, count);
1076 buf = talloc_array(mem_ctx, uint8_t, len);
1081 for (i=0; i<count; i++) {
1082 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1083 copied += iov[i].iov_len;
1088 static void smb1cli_req_flags(enum protocol_types protocol,
1089 uint32_t smb1_capabilities,
1090 uint8_t smb_command,
1091 uint8_t additional_flags,
1092 uint8_t clear_flags,
1094 uint16_t additional_flags2,
1095 uint16_t clear_flags2,
1099 uint16_t flags2 = 0;
1101 if (protocol >= PROTOCOL_LANMAN1) {
1102 flags |= FLAG_CASELESS_PATHNAMES;
1103 flags |= FLAG_CANONICAL_PATHNAMES;
1106 if (protocol >= PROTOCOL_LANMAN2) {
1107 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1108 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1111 if (protocol >= PROTOCOL_NT1) {
1112 flags2 |= FLAGS2_IS_LONG_NAME;
1114 if (smb1_capabilities & CAP_UNICODE) {
1115 flags2 |= FLAGS2_UNICODE_STRINGS;
1117 if (smb1_capabilities & CAP_STATUS32) {
1118 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1120 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1121 flags2 |= FLAGS2_EXTENDED_SECURITY;
1125 flags |= additional_flags;
1126 flags &= ~clear_flags;
1127 flags2 |= additional_flags2;
1128 flags2 &= ~clear_flags2;
1134 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1136 static bool smb1cli_req_cancel(struct tevent_req *req)
1138 struct smbXcli_req_state *state =
1139 tevent_req_data(req,
1140 struct smbXcli_req_state);
1145 struct tevent_req *subreq;
1148 flags = CVAL(state->smb1.hdr, HDR_FLG);
1149 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1150 pid = SVAL(state->smb1.hdr, HDR_PID);
1151 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1152 mid = SVAL(state->smb1.hdr, HDR_MID);
1154 subreq = smb1cli_req_create(state, state->ev,
1164 0, NULL); /* bytes */
1165 if (subreq == NULL) {
1168 smb1cli_req_set_mid(subreq, mid);
1170 status = smb1cli_req_chain_submit(&subreq, 1);
1171 if (!NT_STATUS_IS_OK(status)) {
1172 TALLOC_FREE(subreq);
1175 smb1cli_req_set_mid(subreq, 0);
1177 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1182 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1184 /* we do not care about the result */
1185 TALLOC_FREE(subreq);
1188 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1189 struct tevent_context *ev,
1190 struct smbXcli_conn *conn,
1191 uint8_t smb_command,
1192 uint8_t additional_flags,
1193 uint8_t clear_flags,
1194 uint16_t additional_flags2,
1195 uint16_t clear_flags2,
1196 uint32_t timeout_msec,
1198 struct smbXcli_tcon *tcon,
1199 struct smbXcli_session *session,
1200 uint8_t wct, uint16_t *vwv,
1202 struct iovec *bytes_iov)
1204 struct tevent_req *req;
1205 struct smbXcli_req_state *state;
1207 uint16_t flags2 = 0;
1211 if (iov_count > MAX_SMB_IOV) {
1213 * Should not happen :-)
1218 req = tevent_req_create(mem_ctx, &state,
1219 struct smbXcli_req_state);
1225 state->session = session;
1229 uid = session->smb1.session_id;
1233 tid = tcon->smb1.tcon_id;
1236 state->smb1.recv_cmd = 0xFF;
1237 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1238 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1239 if (state->smb1.recv_iov == NULL) {
1244 smb1cli_req_flags(conn->protocol,
1245 conn->smb1.capabilities,
1254 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1255 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1256 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1257 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1258 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1259 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1260 SSVAL(state->smb1.hdr, HDR_TID, tid);
1261 SSVAL(state->smb1.hdr, HDR_PID, pid);
1262 SSVAL(state->smb1.hdr, HDR_UID, uid);
1263 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1264 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1266 state->smb1.vwv = vwv;
1268 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1270 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1271 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1272 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1273 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1274 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1275 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1276 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1277 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1279 if (iov_count != 0) {
1280 memcpy(&state->smb1.iov[4], bytes_iov,
1281 iov_count * sizeof(*bytes_iov));
1283 state->smb1.iov_count = iov_count + 4;
1285 if (timeout_msec > 0) {
1286 struct timeval endtime;
1288 endtime = timeval_current_ofs_msec(timeout_msec);
1289 if (!tevent_req_set_endtime(req, ev, endtime)) {
1294 switch (smb_command) {
1298 state->one_way = true;
1301 state->one_way = true;
1302 state->smb1.one_way_seqnum = true;
1306 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1307 state->one_way = true;
1315 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1316 struct iovec *iov, int iov_count,
1318 bool one_way_seqnum)
1320 TALLOC_CTX *frame = NULL;
1324 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1325 * iovec directly. MD5Update would do that just fine.
1328 if (iov_count < 4) {
1329 return NT_STATUS_INVALID_PARAMETER_MIX;
1331 if (iov[0].iov_len != NBT_HDR_SIZE) {
1332 return NT_STATUS_INVALID_PARAMETER_MIX;
1334 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1335 return NT_STATUS_INVALID_PARAMETER_MIX;
1337 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1338 return NT_STATUS_INVALID_PARAMETER_MIX;
1340 if (iov[3].iov_len != sizeof(uint16_t)) {
1341 return NT_STATUS_INVALID_PARAMETER_MIX;
1344 frame = talloc_stackframe();
1346 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1348 return NT_STATUS_NO_MEMORY;
1351 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1353 smb_signing_sign_pdu(conn->smb1.signing,
1354 buf, talloc_get_size(buf),
1356 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1359 return NT_STATUS_OK;
1362 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1363 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1364 TALLOC_CTX *tmp_mem,
1367 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1368 struct smbXcli_req_state *state,
1369 struct iovec *iov, int iov_count)
1371 struct tevent_req *subreq;
1376 if (!smbXcli_conn_is_connected(state->conn)) {
1377 return NT_STATUS_CONNECTION_DISCONNECTED;
1380 if (state->conn->protocol > PROTOCOL_NT1) {
1381 return NT_STATUS_REVISION_MISMATCH;
1384 if (iov_count < 4) {
1385 return NT_STATUS_INVALID_PARAMETER_MIX;
1387 if (iov[0].iov_len != NBT_HDR_SIZE) {
1388 return NT_STATUS_INVALID_PARAMETER_MIX;
1390 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1391 return NT_STATUS_INVALID_PARAMETER_MIX;
1393 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1394 return NT_STATUS_INVALID_PARAMETER_MIX;
1396 if (iov[3].iov_len != sizeof(uint16_t)) {
1397 return NT_STATUS_INVALID_PARAMETER_MIX;
1400 cmd = CVAL(iov[1].iov_base, HDR_COM);
1401 if (cmd == SMBreadBraw) {
1402 if (smbXcli_conn_has_async_calls(state->conn)) {
1403 return NT_STATUS_INVALID_PARAMETER_MIX;
1405 state->conn->smb1.read_braw_req = req;
1408 if (state->smb1.mid != 0) {
1409 mid = state->smb1.mid;
1411 mid = smb1cli_alloc_mid(state->conn);
1413 SSVAL(iov[1].iov_base, HDR_MID, mid);
1415 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1417 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1418 &state->smb1.seqnum,
1419 state->smb1.one_way_seqnum);
1421 if (!NT_STATUS_IS_OK(status)) {
1426 * If we supported multiple encrytion contexts
1427 * here we'd look up based on tid.
1429 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1430 char *buf, *enc_buf;
1432 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1434 return NT_STATUS_NO_MEMORY;
1436 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1437 (char *)buf, &enc_buf);
1439 if (!NT_STATUS_IS_OK(status)) {
1440 DEBUG(0, ("Error in encrypting client message: %s\n",
1441 nt_errstr(status)));
1444 buf = (char *)talloc_memdup(state, enc_buf,
1445 smb_len_nbt(enc_buf)+4);
1448 return NT_STATUS_NO_MEMORY;
1450 iov[0].iov_base = (void *)buf;
1451 iov[0].iov_len = talloc_get_size(buf);
1455 if (state->conn->dispatch_incoming == NULL) {
1456 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1459 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1461 subreq = writev_send(state, state->ev, state->conn->outgoing,
1462 state->conn->write_fd, false, iov, iov_count);
1463 if (subreq == NULL) {
1464 return NT_STATUS_NO_MEMORY;
1466 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1467 return NT_STATUS_OK;
1470 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1471 struct tevent_context *ev,
1472 struct smbXcli_conn *conn,
1473 uint8_t smb_command,
1474 uint8_t additional_flags,
1475 uint8_t clear_flags,
1476 uint16_t additional_flags2,
1477 uint16_t clear_flags2,
1478 uint32_t timeout_msec,
1480 struct smbXcli_tcon *tcon,
1481 struct smbXcli_session *session,
1482 uint8_t wct, uint16_t *vwv,
1484 const uint8_t *bytes)
1486 struct tevent_req *req;
1490 iov.iov_base = discard_const_p(void, bytes);
1491 iov.iov_len = num_bytes;
1493 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1494 additional_flags, clear_flags,
1495 additional_flags2, clear_flags2,
1502 if (!tevent_req_is_in_progress(req)) {
1503 return tevent_req_post(req, ev);
1505 status = smb1cli_req_chain_submit(&req, 1);
1506 if (tevent_req_nterror(req, status)) {
1507 return tevent_req_post(req, ev);
1512 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1514 struct tevent_req *req =
1515 tevent_req_callback_data(subreq,
1517 struct smbXcli_req_state *state =
1518 tevent_req_data(req,
1519 struct smbXcli_req_state);
1523 nwritten = writev_recv(subreq, &err);
1524 TALLOC_FREE(subreq);
1525 if (nwritten == -1) {
1526 NTSTATUS status = map_nt_error_from_unix_common(err);
1527 smbXcli_conn_disconnect(state->conn, status);
1531 if (state->one_way) {
1532 state->inbuf = NULL;
1533 tevent_req_done(req);
1537 if (!smbXcli_req_set_pending(req)) {
1538 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1543 static void smbXcli_conn_received(struct tevent_req *subreq)
1545 struct smbXcli_conn *conn =
1546 tevent_req_callback_data(subreq,
1547 struct smbXcli_conn);
1548 TALLOC_CTX *frame = talloc_stackframe();
1554 if (subreq != conn->read_smb_req) {
1555 DEBUG(1, ("Internal error: cli_smb_received called with "
1556 "unexpected subreq\n"));
1557 status = NT_STATUS_INTERNAL_ERROR;
1558 smbXcli_conn_disconnect(conn, status);
1562 conn->read_smb_req = NULL;
1564 received = read_smb_recv(subreq, frame, &inbuf, &err);
1565 TALLOC_FREE(subreq);
1566 if (received == -1) {
1567 status = map_nt_error_from_unix_common(err);
1568 smbXcli_conn_disconnect(conn, status);
1573 status = conn->dispatch_incoming(conn, frame, inbuf);
1575 if (NT_STATUS_IS_OK(status)) {
1577 * We should not do any more processing
1578 * as the dispatch function called
1579 * tevent_req_done().
1582 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1584 * We got an error, so notify all pending requests
1586 smbXcli_conn_disconnect(conn, status);
1591 * We got NT_STATUS_RETRY, so we may ask for a
1592 * next incoming pdu.
1594 if (!smbXcli_conn_receive_next(conn)) {
1595 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1599 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1600 struct iovec **piov, int *pnum_iov)
1611 buflen = smb_len_nbt(buf);
1614 hdr = buf + NBT_HDR_SIZE;
1616 if (buflen < MIN_SMB_SIZE) {
1617 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1621 * This returns iovec elements in the following order:
1636 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1638 return NT_STATUS_NO_MEMORY;
1640 iov[0].iov_base = hdr;
1641 iov[0].iov_len = HDR_WCT;
1644 cmd = CVAL(hdr, HDR_COM);
1648 size_t len = buflen - taken;
1650 struct iovec *iov_tmp;
1657 * we need at least WCT and BCC
1659 needed = sizeof(uint8_t) + sizeof(uint16_t);
1661 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1662 __location__, (int)len, (int)needed));
1667 * Now we check if the specified words are there
1669 wct = CVAL(hdr, wct_ofs);
1670 needed += wct * sizeof(uint16_t);
1672 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1673 __location__, (int)len, (int)needed));
1678 * Now we check if the specified bytes are there
1680 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1681 bcc = SVAL(hdr, bcc_ofs);
1682 needed += bcc * sizeof(uint8_t);
1684 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1685 __location__, (int)len, (int)needed));
1690 * we allocate 2 iovec structures for words and bytes
1692 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1694 if (iov_tmp == NULL) {
1696 return NT_STATUS_NO_MEMORY;
1699 cur = &iov[num_iov];
1702 cur[0].iov_len = wct * sizeof(uint16_t);
1703 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1704 cur[1].iov_len = bcc * sizeof(uint8_t);
1705 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1709 if (!smb1cli_is_andx_req(cmd)) {
1711 * If the current command does not have AndX chanining
1717 if (wct == 0 && bcc == 0) {
1719 * An empty response also ends the chain,
1720 * most likely with an error.
1726 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1727 __location__, (int)wct, (int)cmd));
1730 cmd = CVAL(cur[0].iov_base, 0);
1733 * If it is the end of the chain we are also done.
1737 wct_ofs = SVAL(cur[0].iov_base, 2);
1739 if (wct_ofs < taken) {
1740 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1742 if (wct_ofs > buflen) {
1743 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1747 * we consumed everything up to the start of the next
1753 remaining = buflen - taken;
1755 if (remaining > 0 && num_iov >= 3) {
1757 * The last DATA block gets the remaining
1758 * bytes, this is needed to support
1759 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1761 iov[num_iov-1].iov_len += remaining;
1765 *pnum_iov = num_iov;
1766 return NT_STATUS_OK;
1770 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1773 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1774 TALLOC_CTX *tmp_mem,
1777 struct tevent_req *req;
1778 struct smbXcli_req_state *state;
1785 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1786 size_t len = smb_len_nbt(inbuf);
1787 struct iovec *iov = NULL;
1789 struct tevent_req **chain = NULL;
1790 size_t num_chained = 0;
1791 size_t num_responses = 0;
1793 if (conn->smb1.read_braw_req != NULL) {
1794 req = conn->smb1.read_braw_req;
1795 conn->smb1.read_braw_req = NULL;
1796 state = tevent_req_data(req, struct smbXcli_req_state);
1798 smbXcli_req_unset_pending(req);
1800 if (state->smb1.recv_iov == NULL) {
1802 * For requests with more than
1803 * one response, we have to readd the
1806 state->smb1.recv_iov = talloc_zero_array(state,
1809 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1810 return NT_STATUS_OK;
1814 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1815 state->smb1.recv_iov[0].iov_len = len;
1816 ZERO_STRUCT(state->smb1.recv_iov[1]);
1817 ZERO_STRUCT(state->smb1.recv_iov[2]);
1819 state->smb1.recv_cmd = SMBreadBraw;
1820 state->smb1.recv_status = NT_STATUS_OK;
1821 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1823 tevent_req_done(req);
1824 return NT_STATUS_OK;
1827 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1828 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1829 DEBUG(10, ("Got non-SMB PDU\n"));
1830 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1834 * If we supported multiple encrytion contexts
1835 * here we'd look up based on tid.
1837 if (common_encryption_on(conn->smb1.trans_enc)
1838 && (CVAL(inbuf, 0) == 0)) {
1839 uint16_t enc_ctx_num;
1841 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1842 if (!NT_STATUS_IS_OK(status)) {
1843 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1844 nt_errstr(status)));
1848 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1849 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1851 conn->smb1.trans_enc->enc_ctx_num));
1852 return NT_STATUS_INVALID_HANDLE;
1855 status = common_decrypt_buffer(conn->smb1.trans_enc,
1857 if (!NT_STATUS_IS_OK(status)) {
1858 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1859 nt_errstr(status)));
1862 inhdr = inbuf + NBT_HDR_SIZE;
1863 len = smb_len_nbt(inbuf);
1866 mid = SVAL(inhdr, HDR_MID);
1867 num_pending = talloc_array_length(conn->pending);
1869 for (i=0; i<num_pending; i++) {
1870 if (mid == smb1cli_req_mid(conn->pending[i])) {
1874 if (i == num_pending) {
1875 /* Dump unexpected reply */
1876 return NT_STATUS_RETRY;
1879 oplock_break = false;
1881 if (mid == 0xffff) {
1883 * Paranoia checks that this is really an oplock break request.
1885 oplock_break = (len == 51); /* hdr + 8 words */
1886 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1887 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1888 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1889 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1891 if (!oplock_break) {
1892 /* Dump unexpected reply */
1893 return NT_STATUS_RETRY;
1897 req = conn->pending[i];
1898 state = tevent_req_data(req, struct smbXcli_req_state);
1900 if (!oplock_break /* oplock breaks are not signed */
1901 && !smb_signing_check_pdu(conn->smb1.signing,
1902 inhdr, len, state->smb1.seqnum+1)) {
1903 DEBUG(10, ("cli_check_sign_mac failed\n"));
1904 return NT_STATUS_ACCESS_DENIED;
1907 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1909 if (!NT_STATUS_IS_OK(status)) {
1910 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1911 nt_errstr(status)));
1915 cmd = CVAL(inhdr, HDR_COM);
1916 status = smb1cli_pull_raw_error(inhdr);
1918 if (state->smb1.chained_requests == NULL) {
1920 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1923 smbXcli_req_unset_pending(req);
1925 if (state->smb1.recv_iov == NULL) {
1927 * For requests with more than
1928 * one response, we have to readd the
1931 state->smb1.recv_iov = talloc_zero_array(state,
1934 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1935 return NT_STATUS_OK;
1939 state->smb1.recv_cmd = cmd;
1940 state->smb1.recv_status = status;
1941 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1943 state->smb1.recv_iov[0] = iov[0];
1944 state->smb1.recv_iov[1] = iov[1];
1945 state->smb1.recv_iov[2] = iov[2];
1947 if (talloc_array_length(conn->pending) == 0) {
1948 tevent_req_done(req);
1949 return NT_STATUS_OK;
1952 tevent_req_defer_callback(req, state->ev);
1953 tevent_req_done(req);
1954 return NT_STATUS_RETRY;
1957 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1958 num_chained = talloc_array_length(chain);
1959 num_responses = (num_iov - 1)/2;
1961 if (num_responses > num_chained) {
1962 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1965 for (i=0; i<num_chained; i++) {
1966 size_t iov_idx = 1 + (i*2);
1967 struct iovec *cur = &iov[iov_idx];
1971 state = tevent_req_data(req, struct smbXcli_req_state);
1973 smbXcli_req_unset_pending(req);
1976 * as we finish multiple requests here
1977 * we need to defer the callbacks as
1978 * they could destroy our current stack state.
1980 tevent_req_defer_callback(req, state->ev);
1982 if (i >= num_responses) {
1983 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1987 if (state->smb1.recv_iov == NULL) {
1989 * For requests with more than
1990 * one response, we have to readd the
1993 state->smb1.recv_iov = talloc_zero_array(state,
1996 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2001 state->smb1.recv_cmd = cmd;
2003 if (i == (num_responses - 1)) {
2005 * The last request in the chain gets the status
2007 state->smb1.recv_status = status;
2009 cmd = CVAL(cur[0].iov_base, 0);
2010 state->smb1.recv_status = NT_STATUS_OK;
2013 state->inbuf = inbuf;
2016 * Note: here we use talloc_reference() in a way
2017 * that does not expose it to the caller.
2019 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2020 if (tevent_req_nomem(inbuf_ref, req)) {
2024 /* copy the related buffers */
2025 state->smb1.recv_iov[0] = iov[0];
2026 state->smb1.recv_iov[1] = cur[0];
2027 state->smb1.recv_iov[2] = cur[1];
2029 tevent_req_done(req);
2032 return NT_STATUS_RETRY;
2035 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2036 TALLOC_CTX *mem_ctx,
2037 struct iovec **piov,
2041 uint32_t *pvwv_offset,
2042 uint32_t *pnum_bytes,
2044 uint32_t *pbytes_offset,
2046 const struct smb1cli_req_expected_response *expected,
2047 size_t num_expected)
2049 struct smbXcli_req_state *state =
2050 tevent_req_data(req,
2051 struct smbXcli_req_state);
2052 NTSTATUS status = NT_STATUS_OK;
2053 struct iovec *recv_iov = NULL;
2054 uint8_t *hdr = NULL;
2056 uint32_t vwv_offset = 0;
2057 uint16_t *vwv = NULL;
2058 uint32_t num_bytes = 0;
2059 uint32_t bytes_offset = 0;
2060 uint8_t *bytes = NULL;
2062 bool found_status = false;
2063 bool found_size = false;
2077 if (pvwv_offset != NULL) {
2080 if (pnum_bytes != NULL) {
2083 if (pbytes != NULL) {
2086 if (pbytes_offset != NULL) {
2089 if (pinbuf != NULL) {
2093 if (state->inbuf != NULL) {
2094 recv_iov = state->smb1.recv_iov;
2095 state->smb1.recv_iov = NULL;
2096 if (state->smb1.recv_cmd != SMBreadBraw) {
2097 hdr = (uint8_t *)recv_iov[0].iov_base;
2098 wct = recv_iov[1].iov_len/2;
2099 vwv = (uint16_t *)recv_iov[1].iov_base;
2100 vwv_offset = PTR_DIFF(vwv, hdr);
2101 num_bytes = recv_iov[2].iov_len;
2102 bytes = (uint8_t *)recv_iov[2].iov_base;
2103 bytes_offset = PTR_DIFF(bytes, hdr);
2107 if (tevent_req_is_nterror(req, &status)) {
2108 for (i=0; i < num_expected; i++) {
2109 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2110 found_status = true;
2116 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2122 if (num_expected == 0) {
2123 found_status = true;
2127 status = state->smb1.recv_status;
2129 for (i=0; i < num_expected; i++) {
2130 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2134 found_status = true;
2135 if (expected[i].wct == 0) {
2140 if (expected[i].wct == wct) {
2146 if (!found_status) {
2151 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2155 *piov = talloc_move(mem_ctx, &recv_iov);
2167 if (pvwv_offset != NULL) {
2168 *pvwv_offset = vwv_offset;
2170 if (pnum_bytes != NULL) {
2171 *pnum_bytes = num_bytes;
2173 if (pbytes != NULL) {
2176 if (pbytes_offset != NULL) {
2177 *pbytes_offset = bytes_offset;
2179 if (pinbuf != NULL) {
2180 *pinbuf = state->inbuf;
2186 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2193 for (i=0; i<num_reqs; i++) {
2194 struct smbXcli_req_state *state;
2195 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2196 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2197 state->smb1.iov_count-2);
2198 wct_ofs = (wct_ofs + 3) & ~3;
2203 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2205 struct smbXcli_req_state *first_state =
2206 tevent_req_data(reqs[0],
2207 struct smbXcli_req_state);
2208 struct smbXcli_req_state *state;
2210 size_t chain_padding = 0;
2212 struct iovec *iov = NULL;
2213 struct iovec *this_iov;
2217 if (num_reqs == 1) {
2218 return smb1cli_req_writev_submit(reqs[0], first_state,
2219 first_state->smb1.iov,
2220 first_state->smb1.iov_count);
2224 for (i=0; i<num_reqs; i++) {
2225 if (!tevent_req_is_in_progress(reqs[i])) {
2226 return NT_STATUS_INTERNAL_ERROR;
2229 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2231 if (state->smb1.iov_count < 4) {
2232 return NT_STATUS_INVALID_PARAMETER_MIX;
2237 * The NBT and SMB header
2250 iovlen += state->smb1.iov_count - 2;
2253 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2255 return NT_STATUS_NO_MEMORY;
2258 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2259 first_state, reqs, sizeof(*reqs) * num_reqs);
2260 if (first_state->smb1.chained_requests == NULL) {
2262 return NT_STATUS_NO_MEMORY;
2265 wct_offset = HDR_WCT;
2268 for (i=0; i<num_reqs; i++) {
2269 size_t next_padding = 0;
2272 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2274 if (i < num_reqs-1) {
2275 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2276 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2278 TALLOC_FREE(first_state->smb1.chained_requests);
2279 return NT_STATUS_INVALID_PARAMETER_MIX;
2283 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2284 state->smb1.iov_count-2) + 1;
2285 if ((wct_offset % 4) != 0) {
2286 next_padding = 4 - (wct_offset % 4);
2288 wct_offset += next_padding;
2289 vwv = state->smb1.vwv;
2291 if (i < num_reqs-1) {
2292 struct smbXcli_req_state *next_state =
2293 tevent_req_data(reqs[i+1],
2294 struct smbXcli_req_state);
2295 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2297 SSVAL(vwv+1, 0, wct_offset);
2298 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2299 /* properly end the chain */
2300 SCVAL(vwv+0, 0, 0xff);
2301 SCVAL(vwv+0, 1, 0xff);
2307 * The NBT and SMB header
2309 this_iov[0] = state->smb1.iov[0];
2310 this_iov[1] = state->smb1.iov[1];
2314 * This one is a bit subtle. We have to add
2315 * chain_padding bytes between the requests, and we
2316 * have to also include the wct field of the
2317 * subsequent requests. We use the subsequent header
2318 * for the padding, it contains the wct field in its
2321 this_iov[0].iov_len = chain_padding+1;
2322 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2323 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2324 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2329 * copy the words and bytes
2331 memcpy(this_iov, state->smb1.iov+2,
2332 sizeof(struct iovec) * (state->smb1.iov_count-2));
2333 this_iov += state->smb1.iov_count - 2;
2334 chain_padding = next_padding;
2337 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2338 if (nbt_len > first_state->conn->smb1.max_xmit) {
2340 TALLOC_FREE(first_state->smb1.chained_requests);
2341 return NT_STATUS_INVALID_PARAMETER_MIX;
2344 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2345 if (!NT_STATUS_IS_OK(status)) {
2347 TALLOC_FREE(first_state->smb1.chained_requests);
2351 return NT_STATUS_OK;
2354 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2356 return ((tevent_queue_length(conn->outgoing) != 0)
2357 || (talloc_array_length(conn->pending) != 0));
2360 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2362 return conn->smb2.server.capabilities;
2365 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2367 return conn->smb2.server.security_mode;
2370 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2372 return conn->smb2.server.max_trans_size;
2375 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2377 return conn->smb2.server.max_read_size;
2380 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2382 return conn->smb2.server.max_write_size;
2385 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2386 uint16_t max_credits)
2388 conn->smb2.max_credits = max_credits;
2391 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2393 static bool smb2cli_req_cancel(struct tevent_req *req)
2395 struct smbXcli_req_state *state =
2396 tevent_req_data(req,
2397 struct smbXcli_req_state);
2398 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2399 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2400 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2401 struct smbXcli_tcon *tcon = state->tcon;
2402 struct smbXcli_session *session = state->session;
2403 uint8_t *fixed = state->smb2.pad;
2404 uint16_t fixed_len = 4;
2405 struct tevent_req *subreq;
2406 struct smbXcli_req_state *substate;
2409 SSVAL(fixed, 0, 0x04);
2412 subreq = smb2cli_req_create(state, state->ev,
2420 if (subreq == NULL) {
2423 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2425 if (flags & SMB2_HDR_FLAG_ASYNC) {
2429 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2430 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2431 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2433 status = smb2cli_req_compound_submit(&subreq, 1);
2434 if (!NT_STATUS_IS_OK(status)) {
2435 TALLOC_FREE(subreq);
2439 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2444 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2446 /* we do not care about the result */
2447 TALLOC_FREE(subreq);
2450 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2451 struct tevent_context *ev,
2452 struct smbXcli_conn *conn,
2454 uint32_t additional_flags,
2455 uint32_t clear_flags,
2456 uint32_t timeout_msec,
2457 struct smbXcli_tcon *tcon,
2458 struct smbXcli_session *session,
2459 const uint8_t *fixed,
2464 struct tevent_req *req;
2465 struct smbXcli_req_state *state;
2469 bool use_channel_sequence = false;
2470 uint16_t channel_sequence = 0;
2472 req = tevent_req_create(mem_ctx, &state,
2473 struct smbXcli_req_state);
2480 state->session = session;
2483 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2484 use_channel_sequence = true;
2485 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2486 use_channel_sequence = true;
2490 uid = session->smb2->session_id;
2492 if (use_channel_sequence) {
2493 channel_sequence = session->smb2->channel_sequence;
2496 state->smb2.should_sign = session->smb2->should_sign;
2497 state->smb2.should_encrypt = session->smb2->should_encrypt;
2499 if (cmd == SMB2_OP_SESSSETUP &&
2500 session->smb2->signing_key.length != 0) {
2501 state->smb2.should_sign = true;
2504 if (cmd == SMB2_OP_SESSSETUP &&
2505 session->smb2_channel.signing_key.length == 0) {
2506 state->smb2.should_encrypt = false;
2511 tid = tcon->smb2.tcon_id;
2513 if (tcon->smb2.should_encrypt) {
2514 state->smb2.should_encrypt = true;
2518 if (state->smb2.should_encrypt) {
2519 state->smb2.should_sign = false;
2522 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2523 if (state->smb2.recv_iov == NULL) {
2528 flags |= additional_flags;
2529 flags &= ~clear_flags;
2531 state->smb2.fixed = fixed;
2532 state->smb2.fixed_len = fixed_len;
2533 state->smb2.dyn = dyn;
2534 state->smb2.dyn_len = dyn_len;
2536 if (state->smb2.should_encrypt) {
2537 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2538 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2541 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2542 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2543 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2544 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2545 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2546 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2547 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2548 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2551 case SMB2_OP_CANCEL:
2552 state->one_way = true;
2556 * If this is a dummy request, it will have
2557 * UINT64_MAX as message id.
2558 * If we send on break acknowledgement,
2559 * this gets overwritten later.
2561 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2565 if (timeout_msec > 0) {
2566 struct timeval endtime;
2568 endtime = timeval_current_ofs_msec(timeout_msec);
2569 if (!tevent_req_set_endtime(req, ev, endtime)) {
2577 void smb2cli_req_set_notify_async(struct tevent_req *req)
2579 struct smbXcli_req_state *state =
2580 tevent_req_data(req,
2581 struct smbXcli_req_state);
2583 state->smb2.notify_async = true;
2586 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2587 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2588 TALLOC_CTX *tmp_mem,
2591 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2594 struct smbXcli_req_state *state;
2595 struct tevent_req *subreq;
2597 int i, num_iov, nbt_len;
2600 * 1 for the nbt length
2601 * per request: TRANSFORM, HDR, fixed, dyn, padding
2602 * -1 because the last one does not need padding
2605 iov = talloc_array(reqs[0], struct iovec, 1 + 5*num_reqs - 1);
2607 return NT_STATUS_NO_MEMORY;
2613 for (i=0; i<num_reqs; i++) {
2623 const DATA_BLOB *signing_key = NULL;
2624 const DATA_BLOB *encryption_key = NULL;
2626 if (!tevent_req_is_in_progress(reqs[i])) {
2627 return NT_STATUS_INTERNAL_ERROR;
2630 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2632 if (!smbXcli_conn_is_connected(state->conn)) {
2633 return NT_STATUS_CONNECTION_DISCONNECTED;
2636 if ((state->conn->protocol != PROTOCOL_NONE) &&
2637 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2638 return NT_STATUS_REVISION_MISMATCH;
2641 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2642 if (opcode == SMB2_OP_CANCEL) {
2646 avail = UINT64_MAX - state->conn->smb2.mid;
2648 return NT_STATUS_CONNECTION_ABORTED;
2651 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2652 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2657 charge = MAX(state->smb2.credit_charge, charge);
2659 avail = MIN(avail, state->conn->smb2.cur_credits);
2660 if (avail < charge) {
2661 return NT_STATUS_INTERNAL_ERROR;
2665 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2666 credits = state->conn->smb2.max_credits -
2667 state->conn->smb2.cur_credits;
2669 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2673 mid = state->conn->smb2.mid;
2674 state->conn->smb2.mid += charge;
2675 state->conn->smb2.cur_credits -= charge;
2677 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2678 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2680 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2681 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2684 if (state->session) {
2686 * We prefer the channel signing key if it is
2689 if (state->smb2.should_sign) {
2690 signing_key = &state->session->smb2_channel.signing_key;
2694 * If it is a channel binding, we already have the main
2695 * signing key and try that one.
2697 if (signing_key && signing_key->length == 0) {
2698 signing_key = &state->session->smb2->signing_key;
2702 * If we do not have any session key yet, we skip the
2703 * signing of SMB2_OP_SESSSETUP requests.
2705 if (signing_key && signing_key->length == 0) {
2709 if (state->smb2.should_encrypt) {
2710 encryption_key = &state->session->smb2->encryption_key;
2714 if (encryption_key) {
2716 iov[num_iov].iov_base = state->smb2.transform;
2717 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2722 iov[num_iov].iov_base = state->smb2.hdr;
2723 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2726 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2727 iov[num_iov].iov_len = state->smb2.fixed_len;
2730 if (state->smb2.dyn != NULL) {
2731 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2732 iov[num_iov].iov_len = state->smb2.dyn_len;
2736 reqlen = sizeof(state->smb2.hdr);
2737 reqlen += state->smb2.fixed_len;
2738 reqlen += state->smb2.dyn_len;
2740 if (i < num_reqs-1) {
2741 if ((reqlen % 8) > 0) {
2742 uint8_t pad = 8 - (reqlen % 8);
2743 iov[num_iov].iov_base = state->smb2.pad;
2744 iov[num_iov].iov_len = pad;
2748 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2751 if (encryption_key) {
2756 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2757 state->session->smb2->nonce_low);
2758 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2759 state->session->smb2->nonce_high);
2761 state->session->smb2->nonce_low += 1;
2762 if (state->session->smb2->nonce_low == 0) {
2763 state->session->smb2->nonce_high += 1;
2764 state->session->smb2->nonce_low += 1;
2767 buf = talloc_array(iov, uint8_t, reqlen);
2769 return NT_STATUS_NO_MEMORY;
2772 reqlen += SMB2_TF_HDR_SIZE;
2775 * We copy the buffers before encrypting them,
2776 * this is at least currently needed for the
2777 * to keep state->smb2.hdr.
2779 * Also the callers may expect there buffers
2782 for (vi = hdr_iov; vi < num_iov; vi++) {
2783 struct iovec *v = &iov[vi];
2784 const uint8_t *o = (const uint8_t *)v->iov_base;
2786 memcpy(buf, o, v->iov_len);
2787 v->iov_base = (void *)buf;
2791 status = smb2_signing_encrypt_pdu(*encryption_key,
2792 state->session->conn->protocol,
2793 &iov[tf_iov], num_iov - tf_iov);
2794 if (!NT_STATUS_IS_OK(status)) {
2797 } else if (signing_key) {
2800 status = smb2_signing_sign_pdu(*signing_key,
2801 state->session->conn->protocol,
2802 &iov[hdr_iov], num_iov - hdr_iov);
2803 if (!NT_STATUS_IS_OK(status)) {
2810 ret = smbXcli_req_set_pending(reqs[i]);
2812 return NT_STATUS_NO_MEMORY;
2816 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2817 _smb_setlen_tcp(state->length_hdr, nbt_len);
2818 iov[0].iov_base = state->length_hdr;
2819 iov[0].iov_len = sizeof(state->length_hdr);
2821 if (state->conn->dispatch_incoming == NULL) {
2822 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2825 subreq = writev_send(state, state->ev, state->conn->outgoing,
2826 state->conn->write_fd, false, iov, num_iov);
2827 if (subreq == NULL) {
2828 return NT_STATUS_NO_MEMORY;
2830 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2831 return NT_STATUS_OK;
2834 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2836 struct smbXcli_req_state *state =
2837 tevent_req_data(req,
2838 struct smbXcli_req_state);
2840 state->smb2.credit_charge = charge;
2843 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2844 struct tevent_context *ev,
2845 struct smbXcli_conn *conn,
2847 uint32_t additional_flags,
2848 uint32_t clear_flags,
2849 uint32_t timeout_msec,
2850 struct smbXcli_tcon *tcon,
2851 struct smbXcli_session *session,
2852 const uint8_t *fixed,
2857 struct tevent_req *req;
2860 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2861 additional_flags, clear_flags,
2864 fixed, fixed_len, dyn, dyn_len);
2868 if (!tevent_req_is_in_progress(req)) {
2869 return tevent_req_post(req, ev);
2871 status = smb2cli_req_compound_submit(&req, 1);
2872 if (tevent_req_nterror(req, status)) {
2873 return tevent_req_post(req, ev);
2878 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2880 struct tevent_req *req =
2881 tevent_req_callback_data(subreq,
2883 struct smbXcli_req_state *state =
2884 tevent_req_data(req,
2885 struct smbXcli_req_state);
2889 nwritten = writev_recv(subreq, &err);
2890 TALLOC_FREE(subreq);
2891 if (nwritten == -1) {
2892 /* here, we need to notify all pending requests */
2893 NTSTATUS status = map_nt_error_from_unix_common(err);
2894 smbXcli_conn_disconnect(state->conn, status);
2899 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2902 TALLOC_CTX *mem_ctx,
2903 struct iovec **piov, int *pnum_iov)
2908 uint8_t *first_hdr = buf;
2910 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2912 return NT_STATUS_NO_MEMORY;
2915 while (taken < buflen) {
2918 size_t len = buflen - taken;
2919 uint8_t *hdr = first_hdr + taken;
2922 size_t next_command_ofs;
2924 struct iovec *iov_tmp;
2927 DEBUG(10, ("%d bytes left, expected at least %d\n",
2931 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2932 struct smbXcli_session *s;
2934 struct iovec tf_iov[2];
2937 if (len < SMB2_TF_HDR_SIZE) {
2938 DEBUG(10, ("%d bytes left, expected at least %d\n",
2939 (int)len, SMB2_TF_HDR_SIZE));
2943 tf_len = SMB2_TF_HDR_SIZE;
2946 hdr = first_hdr + taken;
2947 len = IVAL(tf, SMB2_TF_MSG_SIZE);
2948 uid = BVAL(tf, SMB2_TF_SESSION_ID);
2951 for (; s; s = s->next) {
2952 if (s->smb2->session_id != uid) {
2959 DEBUG(10, ("unknown session_id %llu\n",
2960 (unsigned long long)uid));
2964 tf_iov[0].iov_base = (void *)tf;
2965 tf_iov[0].iov_len = tf_len;
2966 tf_iov[1].iov_base = (void *)hdr;
2967 tf_iov[1].iov_len = len;
2969 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
2972 if (!NT_STATUS_IS_OK(status)) {
2979 * We need the header plus the body length field
2982 if (len < SMB2_HDR_BODY + 2) {
2983 DEBUG(10, ("%d bytes left, expected at least %d\n",
2984 (int)len, SMB2_HDR_BODY));
2987 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2988 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2992 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2993 DEBUG(10, ("Got HDR len %d, expected %d\n",
2994 SVAL(hdr, 4), SMB2_HDR_BODY));
2999 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3000 body_size = SVAL(hdr, SMB2_HDR_BODY);
3002 if (next_command_ofs != 0) {
3003 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3006 if (next_command_ofs > full_size) {
3009 if (tf && next_command_ofs < len) {
3012 full_size = next_command_ofs;
3014 if (body_size < 2) {
3017 body_size &= 0xfffe;
3019 if (body_size > (full_size - SMB2_HDR_BODY)) {
3023 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3025 if (iov_tmp == NULL) {
3027 return NT_STATUS_NO_MEMORY;
3030 cur = &iov[num_iov];
3033 cur[0].iov_base = tf;
3034 cur[0].iov_len = tf_len;
3035 cur[1].iov_base = hdr;
3036 cur[1].iov_len = SMB2_HDR_BODY;
3037 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3038 cur[2].iov_len = body_size;
3039 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3040 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3046 *pnum_iov = num_iov;
3047 return NT_STATUS_OK;
3051 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3054 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3057 size_t num_pending = talloc_array_length(conn->pending);
3060 for (i=0; i<num_pending; i++) {
3061 struct tevent_req *req = conn->pending[i];
3062 struct smbXcli_req_state *state =
3063 tevent_req_data(req,
3064 struct smbXcli_req_state);
3066 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3073 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3074 TALLOC_CTX *tmp_mem,
3077 struct tevent_req *req;
3078 struct smbXcli_req_state *state = NULL;
3083 struct smbXcli_session *last_session = NULL;
3084 size_t inbuf_len = smb_len_tcp(inbuf);
3086 status = smb2cli_inbuf_parse_compound(conn,
3087 inbuf + NBT_HDR_SIZE,
3091 if (!NT_STATUS_IS_OK(status)) {
3095 for (i=0; i<num_iov; i+=4) {
3096 uint8_t *inbuf_ref = NULL;
3097 struct iovec *cur = &iov[i];
3098 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3099 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3100 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3101 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3102 uint16_t req_opcode;
3104 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3105 uint32_t new_credits;
3106 struct smbXcli_session *session = NULL;
3107 const DATA_BLOB *signing_key = NULL;
3109 new_credits = conn->smb2.cur_credits;
3110 new_credits += credits;
3111 if (new_credits > UINT16_MAX) {
3112 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3114 conn->smb2.cur_credits += credits;
3116 req = smb2cli_conn_find_pending(conn, mid);
3118 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3120 state = tevent_req_data(req, struct smbXcli_req_state);
3122 state->smb2.got_async = false;
3124 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3125 if (opcode != req_opcode) {
3126 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3128 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3130 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3131 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3134 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3135 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3136 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3137 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3140 * async interim responses are not signed,
3141 * even if the SMB2_HDR_FLAG_SIGNED flag
3144 req_flags |= SMB2_HDR_FLAG_ASYNC;
3145 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3146 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3148 if (state->smb2.notify_async) {
3149 state->smb2.got_async = true;
3150 tevent_req_defer_callback(req, state->ev);
3151 tevent_req_notify_callback(req);
3156 session = state->session;
3157 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3158 session = last_session;
3160 last_session = session;
3162 if (state->smb2.should_sign) {
3163 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3164 return NT_STATUS_ACCESS_DENIED;
3168 if (flags & SMB2_HDR_FLAG_SIGNED) {
3169 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3171 if (session == NULL) {
3172 struct smbXcli_session *s;
3174 s = state->conn->sessions;
3175 for (; s; s = s->next) {
3176 if (s->smb2->session_id != uid) {
3185 if (session == NULL) {
3186 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3189 last_session = session;
3190 signing_key = &session->smb2_channel.signing_key;
3193 if (opcode == SMB2_OP_SESSSETUP) {
3195 * We prefer the channel signing key, if it is
3198 * If we do not have a channel signing key yet,
3199 * we try the main signing key, if it is not
3200 * the final response.
3202 if (signing_key && signing_key->length == 0 &&
3203 !NT_STATUS_IS_OK(status)) {
3204 signing_key = &session->smb2->signing_key;
3207 if (signing_key && signing_key->length == 0) {
3209 * If we do not have a session key to
3210 * verify the signature, we defer the
3211 * signing check to the caller.
3213 * The caller gets NT_STATUS_OK, it
3215 * smb2cli_session_set_session_key()
3217 * smb2cli_session_set_channel_key()
3218 * which will check the signature
3219 * with the channel signing key.
3225 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3227 * if the server returns NT_STATUS_USER_SESSION_DELETED
3228 * the response is not signed and we should
3229 * propagate the NT_STATUS_USER_SESSION_DELETED
3230 * status to the caller.
3233 } else if (state->smb2.should_encrypt) {
3234 if (cur[0].iov_len != SMB2_TF_HDR_SIZE) {
3235 return NT_STATUS_ACCESS_DENIED;
3239 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3240 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3241 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3243 * if the server returns
3244 * NT_STATUS_NETWORK_NAME_DELETED
3245 * NT_STATUS_FILE_CLOSED
3246 * NT_STATUS_INVALID_PARAMETER
3247 * the response might not be signed
3248 * as this happens before the signing checks.
3250 * If server echos the signature (or all zeros)
3251 * we should report the status from the server
3257 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3258 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3261 state->smb2.signing_skipped = true;
3267 static const uint8_t zeros[16];
3269 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3273 state->smb2.signing_skipped = true;
3280 status = smb2_signing_check_pdu(*signing_key,
3281 state->conn->protocol,
3283 if (!NT_STATUS_IS_OK(status)) {
3285 * If the signing check fails, we disconnect
3292 smbXcli_req_unset_pending(req);
3295 * There might be more than one response
3296 * we need to defer the notifications
3298 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3303 tevent_req_defer_callback(req, state->ev);
3307 * Note: here we use talloc_reference() in a way
3308 * that does not expose it to the caller.
3310 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3311 if (tevent_req_nomem(inbuf_ref, req)) {
3315 /* copy the related buffers */
3316 state->smb2.recv_iov[0] = cur[1];
3317 state->smb2.recv_iov[1] = cur[2];
3318 state->smb2.recv_iov[2] = cur[3];
3320 tevent_req_done(req);
3324 return NT_STATUS_RETRY;
3327 return NT_STATUS_OK;
3330 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3331 struct iovec **piov,
3332 const struct smb2cli_req_expected_response *expected,
3333 size_t num_expected)
3335 struct smbXcli_req_state *state =
3336 tevent_req_data(req,
3337 struct smbXcli_req_state);
3340 bool found_status = false;
3341 bool found_size = false;
3348 if (state->smb2.got_async) {
3349 return STATUS_PENDING;
3352 if (tevent_req_is_nterror(req, &status)) {
3353 for (i=0; i < num_expected; i++) {
3354 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3355 found_status = true;
3361 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3367 if (num_expected == 0) {
3368 found_status = true;
3372 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3373 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3375 for (i=0; i < num_expected; i++) {
3376 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3380 found_status = true;
3381 if (expected[i].body_size == 0) {
3386 if (expected[i].body_size == body_size) {
3392 if (!found_status) {
3396 if (state->smb2.signing_skipped) {
3397 if (num_expected > 0) {
3398 return NT_STATUS_ACCESS_DENIED;
3400 if (!NT_STATUS_IS_ERR(status)) {
3401 return NT_STATUS_ACCESS_DENIED;
3406 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3410 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3416 static const struct {
3417 enum protocol_types proto;
3418 const char *smb1_name;
3419 } smb1cli_prots[] = {
3420 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3421 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3422 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3423 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3424 {PROTOCOL_LANMAN2, "LM1.2X002"},
3425 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3426 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3427 {PROTOCOL_LANMAN2, "Samba"},
3428 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3429 {PROTOCOL_NT1, "NT LM 0.12"},
3430 {PROTOCOL_SMB2_02, "SMB 2.002"},
3431 {PROTOCOL_SMB2_10, "SMB 2.???"},
3434 static const struct {
3435 enum protocol_types proto;
3436 uint16_t smb2_dialect;
3437 } smb2cli_prots[] = {
3438 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3439 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3440 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3441 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3442 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3445 struct smbXcli_negprot_state {
3446 struct smbXcli_conn *conn;
3447 struct tevent_context *ev;
3448 uint32_t timeout_msec;
3449 enum protocol_types min_protocol;
3450 enum protocol_types max_protocol;
3454 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3458 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3459 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3460 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3461 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3462 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3463 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3467 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3468 struct tevent_context *ev,
3469 struct smbXcli_conn *conn,
3470 uint32_t timeout_msec,
3471 enum protocol_types min_protocol,
3472 enum protocol_types max_protocol)
3474 struct tevent_req *req, *subreq;
3475 struct smbXcli_negprot_state *state;
3477 req = tevent_req_create(mem_ctx, &state,
3478 struct smbXcli_negprot_state);
3484 state->timeout_msec = timeout_msec;
3485 state->min_protocol = min_protocol;
3486 state->max_protocol = max_protocol;
3488 if (min_protocol == PROTOCOL_NONE) {
3489 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3490 return tevent_req_post(req, ev);
3493 if (max_protocol == PROTOCOL_NONE) {
3494 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3495 return tevent_req_post(req, ev);
3498 if (min_protocol > max_protocol) {
3499 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3500 return tevent_req_post(req, ev);
3503 if ((min_protocol < PROTOCOL_SMB2_02) &&
3504 (max_protocol < PROTOCOL_SMB2_02)) {
3508 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3510 subreq = smbXcli_negprot_smb1_subreq(state);
3511 if (tevent_req_nomem(subreq, req)) {
3512 return tevent_req_post(req, ev);
3514 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3518 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3519 (max_protocol >= PROTOCOL_SMB2_02)) {
3523 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3525 subreq = smbXcli_negprot_smb2_subreq(state);
3526 if (tevent_req_nomem(subreq, req)) {
3527 return tevent_req_post(req, ev);
3529 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3534 * We send an SMB1 negprot with the SMB2 dialects
3535 * and expect a SMB1 or a SMB2 response.
3537 * smbXcli_negprot_dispatch_incoming() will fix the
3538 * callback to match protocol of the response.
3540 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3542 subreq = smbXcli_negprot_smb1_subreq(state);
3543 if (tevent_req_nomem(subreq, req)) {
3544 return tevent_req_post(req, ev);
3546 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3550 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3552 struct tevent_req *req =
3553 tevent_req_callback_data(subreq,
3558 * we just want the low level error
3560 status = tevent_req_simple_recv_ntstatus(subreq);
3561 TALLOC_FREE(subreq);
3562 if (tevent_req_nterror(req, status)) {
3566 /* this should never happen */
3567 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3570 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3573 DATA_BLOB bytes = data_blob_null;
3577 /* setup the protocol strings */
3578 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3582 if (smb1cli_prots[i].proto < state->min_protocol) {
3586 if (smb1cli_prots[i].proto > state->max_protocol) {
3590 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3596 * We now it is already ascii and
3597 * we want NULL termination.
3599 ok = data_blob_append(state, &bytes,
3600 smb1cli_prots[i].smb1_name,
3601 strlen(smb1cli_prots[i].smb1_name)+1);
3607 smb1cli_req_flags(state->max_protocol,
3608 state->conn->smb1.client.capabilities,
3613 return smb1cli_req_send(state, state->ev, state->conn,
3617 state->timeout_msec,
3618 0xFFFE, 0, NULL, /* pid, tid, session */
3619 0, NULL, /* wct, vwv */
3620 bytes.length, bytes.data);
3623 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3625 struct tevent_req *req =
3626 tevent_req_callback_data(subreq,
3628 struct smbXcli_negprot_state *state =
3629 tevent_req_data(req,
3630 struct smbXcli_negprot_state);
3631 struct smbXcli_conn *conn = state->conn;
3632 struct iovec *recv_iov = NULL;
3641 size_t num_prots = 0;
3643 uint32_t client_capabilities = conn->smb1.client.capabilities;
3644 uint32_t both_capabilities;
3645 uint32_t server_capabilities = 0;
3646 uint32_t capabilities;
3647 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3648 uint32_t server_max_xmit = 0;
3650 uint32_t server_max_mux = 0;
3651 uint16_t server_security_mode = 0;
3652 uint32_t server_session_key = 0;
3653 bool server_readbraw = false;
3654 bool server_writebraw = false;
3655 bool server_lockread = false;
3656 bool server_writeunlock = false;
3657 struct GUID server_guid = GUID_zero();
3658 DATA_BLOB server_gss_blob = data_blob_null;
3659 uint8_t server_challenge[8];
3660 char *server_workgroup = NULL;
3661 char *server_name = NULL;
3662 int server_time_zone = 0;
3663 NTTIME server_system_time = 0;
3664 static const struct smb1cli_req_expected_response expected[] = {
3666 .status = NT_STATUS_OK,
3667 .wct = 0x11, /* NT1 */
3670 .status = NT_STATUS_OK,
3671 .wct = 0x0D, /* LM */
3674 .status = NT_STATUS_OK,
3675 .wct = 0x01, /* CORE */
3679 ZERO_STRUCT(server_challenge);
3681 status = smb1cli_req_recv(subreq, state,
3686 NULL, /* pvwv_offset */
3689 NULL, /* pbytes_offset */
3691 expected, ARRAY_SIZE(expected));
3692 TALLOC_FREE(subreq);
3693 if (tevent_req_nterror(req, status)) {
3697 flags = CVAL(inhdr, HDR_FLG);
3699 protnum = SVAL(vwv, 0);
3701 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3702 if (smb1cli_prots[i].proto < state->min_protocol) {
3706 if (smb1cli_prots[i].proto > state->max_protocol) {
3710 if (protnum != num_prots) {
3715 conn->protocol = smb1cli_prots[i].proto;
3719 if (conn->protocol == PROTOCOL_NONE) {
3720 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3724 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3725 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3726 "and the selected protocol level doesn't support it.\n"));
3727 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3731 if (flags & FLAG_SUPPORT_LOCKREAD) {
3732 server_lockread = true;
3733 server_writeunlock = true;
3736 if (conn->protocol >= PROTOCOL_NT1) {
3737 const char *client_signing = NULL;
3738 bool server_mandatory = false;
3739 bool server_allowed = false;
3740 const char *server_signing = NULL;
3745 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3750 server_security_mode = CVAL(vwv + 1, 0);
3751 server_max_mux = SVAL(vwv + 1, 1);
3752 server_max_xmit = IVAL(vwv + 3, 1);
3753 server_session_key = IVAL(vwv + 7, 1);
3754 server_time_zone = SVALS(vwv + 15, 1);
3755 server_time_zone *= 60;
3756 /* this time arrives in real GMT */
3757 server_system_time = BVAL(vwv + 11, 1);
3758 server_capabilities = IVAL(vwv + 9, 1);
3760 key_len = CVAL(vwv + 16, 1);
3762 if (server_capabilities & CAP_RAW_MODE) {
3763 server_readbraw = true;
3764 server_writebraw = true;
3766 if (server_capabilities & CAP_LOCK_AND_READ) {
3767 server_lockread = true;
3770 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3771 DATA_BLOB blob1, blob2;
3773 if (num_bytes < 16) {
3774 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3778 blob1 = data_blob_const(bytes, 16);
3779 status = GUID_from_data_blob(&blob1, &server_guid);
3780 if (tevent_req_nterror(req, status)) {
3784 blob1 = data_blob_const(bytes+16, num_bytes-16);
3785 blob2 = data_blob_dup_talloc(state, blob1);
3786 if (blob1.length > 0 &&
3787 tevent_req_nomem(blob2.data, req)) {
3790 server_gss_blob = blob2;
3792 DATA_BLOB blob1, blob2;
3794 if (num_bytes < key_len) {
3795 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3799 if (key_len != 0 && key_len != 8) {
3800 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3805 memcpy(server_challenge, bytes, 8);
3808 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3809 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3810 if (blob1.length > 0) {
3813 len = utf16_len_n(blob1.data,
3817 ok = convert_string_talloc(state,
3825 status = map_nt_error_from_unix_common(errno);
3826 tevent_req_nterror(req, status);
3831 blob2.data += blob1.length;
3832 blob2.length -= blob1.length;
3833 if (blob2.length > 0) {
3836 len = utf16_len_n(blob1.data,
3840 ok = convert_string_talloc(state,
3848 status = map_nt_error_from_unix_common(errno);
3849 tevent_req_nterror(req, status);
3855 client_signing = "disabled";
3856 if (conn->allow_signing) {
3857 client_signing = "allowed";
3859 if (conn->mandatory_signing) {
3860 client_signing = "required";
3863 server_signing = "not supported";
3864 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3865 server_signing = "supported";
3866 server_allowed = true;
3868 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3869 server_signing = "required";
3870 server_mandatory = true;
3873 ok = smb_signing_set_negotiated(conn->smb1.signing,
3877 DEBUG(1,("cli_negprot: SMB signing is required, "
3878 "but client[%s] and server[%s] mismatch\n",
3879 client_signing, server_signing));
3880 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3884 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3890 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3894 server_security_mode = SVAL(vwv + 1, 0);
3895 server_max_xmit = SVAL(vwv + 2, 0);
3896 server_max_mux = SVAL(vwv + 3, 0);
3897 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3898 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3899 server_session_key = IVAL(vwv + 6, 0);
3900 server_time_zone = SVALS(vwv + 10, 0);
3901 server_time_zone *= 60;
3902 /* this time is converted to GMT by make_unix_date */
3903 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3904 unix_to_nt_time(&server_system_time, t);
3905 key_len = SVAL(vwv + 11, 0);
3907 if (num_bytes < key_len) {
3908 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3912 if (key_len != 0 && key_len != 8) {
3913 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3918 memcpy(server_challenge, bytes, 8);
3921 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3922 if (blob1.length > 0) {
3926 len = utf16_len_n(blob1.data,
3930 ok = convert_string_talloc(state,
3938 status = map_nt_error_from_unix_common(errno);
3939 tevent_req_nterror(req, status);
3945 /* the old core protocol */
3946 server_time_zone = get_time_zone(time(NULL));
3947 server_max_xmit = 1024;
3951 if (server_max_xmit < 1024) {
3952 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3956 if (server_max_mux < 1) {
3957 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3962 * Now calculate the negotiated capabilities
3963 * based on the mask for:
3964 * - client only flags
3965 * - flags used in both directions
3966 * - server only flags
3968 both_capabilities = client_capabilities & server_capabilities;
3969 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3970 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3971 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3973 max_xmit = MIN(client_max_xmit, server_max_xmit);
3975 conn->smb1.server.capabilities = server_capabilities;
3976 conn->smb1.capabilities = capabilities;
3978 conn->smb1.server.max_xmit = server_max_xmit;
3979 conn->smb1.max_xmit = max_xmit;
3981 conn->smb1.server.max_mux = server_max_mux;
3983 conn->smb1.server.security_mode = server_security_mode;
3985 conn->smb1.server.readbraw = server_readbraw;
3986 conn->smb1.server.writebraw = server_writebraw;
3987 conn->smb1.server.lockread = server_lockread;
3988 conn->smb1.server.writeunlock = server_writeunlock;
3990 conn->smb1.server.session_key = server_session_key;
3992 talloc_steal(conn, server_gss_blob.data);
3993 conn->smb1.server.gss_blob = server_gss_blob;
3994 conn->smb1.server.guid = server_guid;
3995 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3996 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3997 conn->smb1.server.name = talloc_move(conn, &server_name);
3999 conn->smb1.server.time_zone = server_time_zone;
4000 conn->smb1.server.system_time = server_system_time;
4002 tevent_req_done(req);
4005 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4009 uint16_t dialect_count = 0;
4011 buf = state->smb2.dyn;
4012 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4013 if (smb2cli_prots[i].proto < state->min_protocol) {
4017 if (smb2cli_prots[i].proto > state->max_protocol) {
4021 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4025 buf = state->smb2.fixed;
4027 SSVAL(buf, 2, dialect_count);
4028 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4029 SSVAL(buf, 6, 0); /* Reserved */
4030 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4031 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4033 SIVAL(buf, 8, 0); /* Capabilities */
4035 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4039 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4041 if (!NT_STATUS_IS_OK(status)) {
4044 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4046 memset(buf+12, 0, 16); /* ClientGuid */
4048 SBVAL(buf, 28, 0); /* ClientStartTime */
4050 return smb2cli_req_send(state, state->ev,
4051 state->conn, SMB2_OP_NEGPROT,
4053 state->timeout_msec,
4054 NULL, NULL, /* tcon, session */
4055 state->smb2.fixed, sizeof(state->smb2.fixed),
4056 state->smb2.dyn, dialect_count*2);
4059 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4061 struct tevent_req *req =
4062 tevent_req_callback_data(subreq,
4064 struct smbXcli_negprot_state *state =
4065 tevent_req_data(req,
4066 struct smbXcli_negprot_state);
4067 struct smbXcli_conn *conn = state->conn;
4068 size_t security_offset, security_length;
4074 uint16_t dialect_revision;
4075 static const struct smb2cli_req_expected_response expected[] = {
4077 .status = NT_STATUS_OK,
4082 status = smb2cli_req_recv(subreq, state, &iov,
4083 expected, ARRAY_SIZE(expected));
4084 TALLOC_FREE(subreq);
4085 if (tevent_req_nterror(req, status)) {
4089 body = (uint8_t *)iov[1].iov_base;
4091 dialect_revision = SVAL(body, 4);
4093 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4094 if (smb2cli_prots[i].proto < state->min_protocol) {
4098 if (smb2cli_prots[i].proto > state->max_protocol) {
4102 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4106 conn->protocol = smb2cli_prots[i].proto;
4110 if (conn->protocol == PROTOCOL_NONE) {
4111 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4112 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4116 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4117 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4121 /* make sure we do not loop forever */
4122 state->min_protocol = PROTOCOL_SMB2_02;
4125 * send a SMB2 negprot, in order to negotiate
4128 subreq = smbXcli_negprot_smb2_subreq(state);
4129 if (tevent_req_nomem(subreq, req)) {
4132 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4136 conn->smb2.server.security_mode = SVAL(body, 2);
4138 blob = data_blob_const(body + 8, 16);
4139 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4140 if (tevent_req_nterror(req, status)) {
4144 conn->smb2.server.capabilities = IVAL(body, 24);
4145 conn->smb2.server.max_trans_size= IVAL(body, 28);
4146 conn->smb2.server.max_read_size = IVAL(body, 32);
4147 conn->smb2.server.max_write_size= IVAL(body, 36);
4148 conn->smb2.server.system_time = BVAL(body, 40);
4149 conn->smb2.server.start_time = BVAL(body, 48);
4151 security_offset = SVAL(body, 56);
4152 security_length = SVAL(body, 58);
4154 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4155 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4159 if (security_length > iov[2].iov_len) {
4160 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4164 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4167 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4171 tevent_req_done(req);
4174 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4175 TALLOC_CTX *tmp_mem,
4178 size_t num_pending = talloc_array_length(conn->pending);
4179 struct tevent_req *subreq;
4180 struct smbXcli_req_state *substate;
4181 struct tevent_req *req;
4182 uint32_t protocol_magic;
4183 size_t inbuf_len = smb_len_nbt(inbuf);
4185 if (num_pending != 1) {
4186 return NT_STATUS_INTERNAL_ERROR;
4189 if (inbuf_len < 4) {
4190 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4193 subreq = conn->pending[0];
4194 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4195 req = tevent_req_callback_data(subreq, struct tevent_req);
4197 protocol_magic = IVAL(inbuf, 4);
4199 switch (protocol_magic) {
4201 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4202 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4203 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4206 if (substate->smb2.recv_iov == NULL) {
4208 * For the SMB1 negprot we have move it.
4210 substate->smb2.recv_iov = substate->smb1.recv_iov;
4211 substate->smb1.recv_iov = NULL;
4215 * we got an SMB2 answer, which consumed sequence number 0
4216 * so we need to use 1 as the next one
4219 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4220 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4221 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4224 DEBUG(10, ("Got non-SMB PDU\n"));
4225 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4228 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4230 return tevent_req_simple_recv_ntstatus(req);
4233 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4234 uint32_t timeout_msec,
4235 enum protocol_types min_protocol,
4236 enum protocol_types max_protocol)
4238 TALLOC_CTX *frame = talloc_stackframe();
4239 struct tevent_context *ev;
4240 struct tevent_req *req;
4241 NTSTATUS status = NT_STATUS_NO_MEMORY;
4244 if (smbXcli_conn_has_async_calls(conn)) {
4246 * Can't use sync call while an async call is in flight
4248 status = NT_STATUS_INVALID_PARAMETER_MIX;
4251 ev = tevent_context_init(frame);
4255 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4256 min_protocol, max_protocol);
4260 ok = tevent_req_poll(req, ev);
4262 status = map_nt_error_from_unix_common(errno);
4265 status = smbXcli_negprot_recv(req);
4271 static int smbXcli_session_destructor(struct smbXcli_session *session)
4273 if (session->conn == NULL) {
4277 DLIST_REMOVE(session->conn->sessions, session);
4281 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4282 struct smbXcli_conn *conn)
4284 struct smbXcli_session *session;
4286 session = talloc_zero(mem_ctx, struct smbXcli_session);
4287 if (session == NULL) {
4290 session->smb2 = talloc_zero(session, struct smb2cli_session);
4291 if (session->smb2 == NULL) {
4292 talloc_free(session);
4295 talloc_set_destructor(session, smbXcli_session_destructor);
4297 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4298 session->conn = conn;
4303 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4304 TALLOC_CTX *mem_ctx,
4307 const DATA_BLOB *application_key;
4309 *key = data_blob_null;
4311 if (session->conn == NULL) {
4312 return NT_STATUS_NO_USER_SESSION_KEY;
4315 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4316 application_key = &session->smb2->application_key;
4318 application_key = &session->smb1.application_key;
4321 if (application_key->length == 0) {
4322 return NT_STATUS_NO_USER_SESSION_KEY;
4325 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4326 if (key->data == NULL) {
4327 return NT_STATUS_NO_MEMORY;
4330 return NT_STATUS_OK;
4333 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4335 return session->smb1.session_id;
4338 void smb1cli_session_set_id(struct smbXcli_session *session,
4339 uint16_t session_id)
4341 session->smb1.session_id = session_id;
4344 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4345 const DATA_BLOB _session_key)
4347 struct smbXcli_conn *conn = session->conn;
4348 uint8_t session_key[16];
4351 return NT_STATUS_INVALID_PARAMETER_MIX;
4354 if (session->smb1.application_key.length != 0) {
4356 * TODO: do not allow this...
4358 * return NT_STATUS_INVALID_PARAMETER_MIX;
4360 data_blob_clear_free(&session->smb1.application_key);
4361 session->smb1.protected_key = false;
4364 if (_session_key.length == 0) {
4365 return NT_STATUS_OK;
4368 ZERO_STRUCT(session_key);
4369 memcpy(session_key, _session_key.data,
4370 MIN(_session_key.length, sizeof(session_key)));
4372 session->smb1.application_key = data_blob_talloc(session,
4374 sizeof(session_key));
4375 ZERO_STRUCT(session_key);
4376 if (session->smb1.application_key.data == NULL) {
4377 return NT_STATUS_NO_MEMORY;
4380 session->smb1.protected_key = false;
4382 return NT_STATUS_OK;
4385 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4387 if (session->smb1.protected_key) {
4388 /* already protected */
4389 return NT_STATUS_OK;
4392 if (session->smb1.application_key.length != 16) {
4393 return NT_STATUS_INVALID_PARAMETER_MIX;
4396 smb_key_derivation(session->smb1.application_key.data,
4397 session->smb1.application_key.length,
4398 session->smb1.application_key.data);
4400 session->smb1.protected_key = true;
4402 return NT_STATUS_OK;
4405 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4407 struct smbXcli_conn *conn = session->conn;
4408 uint8_t security_mode = 0;
4411 return security_mode;
4414 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4415 if (conn->mandatory_signing) {
4416 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4419 return security_mode;
4422 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4424 return session->smb2->session_id;
4427 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4429 return session->smb2->session_flags;
4432 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4433 uint64_t session_id,
4434 uint16_t session_flags)
4436 session->smb2->session_id = session_id;
4437 session->smb2->session_flags = session_flags;
4440 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4442 session->smb2->channel_sequence += 1;
4445 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4446 const DATA_BLOB _session_key,
4447 const struct iovec *recv_iov)
4449 struct smbXcli_conn *conn = session->conn;
4450 uint16_t no_sign_flags;
4451 uint8_t session_key[16];
4455 return NT_STATUS_INVALID_PARAMETER_MIX;
4458 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4460 if (session->smb2->session_flags & no_sign_flags) {
4461 session->smb2->should_sign = false;
4462 return NT_STATUS_OK;
4465 if (session->smb2->signing_key.length != 0) {
4466 return NT_STATUS_INVALID_PARAMETER_MIX;
4469 ZERO_STRUCT(session_key);
4470 memcpy(session_key, _session_key.data,
4471 MIN(_session_key.length, sizeof(session_key)));
4473 session->smb2->signing_key = data_blob_talloc(session,
4475 sizeof(session_key));
4476 if (session->smb2->signing_key.data == NULL) {
4477 ZERO_STRUCT(session_key);
4478 return NT_STATUS_NO_MEMORY;
4481 if (conn->protocol >= PROTOCOL_SMB2_24) {
4482 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4483 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4485 smb2_key_derivation(session_key, sizeof(session_key),
4486 label.data, label.length,
4487 context.data, context.length,
4488 session->smb2->signing_key.data);
4491 session->smb2->encryption_key = data_blob_dup_talloc(session,
4492 session->smb2->signing_key);
4493 if (session->smb2->encryption_key.data == NULL) {
4494 ZERO_STRUCT(session_key);
4495 return NT_STATUS_NO_MEMORY;
4498 if (conn->protocol >= PROTOCOL_SMB2_24) {
4499 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4500 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4502 smb2_key_derivation(session_key, sizeof(session_key),
4503 label.data, label.length,
4504 context.data, context.length,
4505 session->smb2->encryption_key.data);
4508 session->smb2->decryption_key = data_blob_dup_talloc(session,
4509 session->smb2->signing_key);
4510 if (session->smb2->decryption_key.data == NULL) {
4511 ZERO_STRUCT(session_key);
4512 return NT_STATUS_NO_MEMORY;
4515 if (conn->protocol >= PROTOCOL_SMB2_24) {
4516 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4517 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4519 smb2_key_derivation(session_key, sizeof(session_key),
4520 label.data, label.length,
4521 context.data, context.length,
4522 session->smb2->decryption_key.data);
4525 session->smb2->application_key = data_blob_dup_talloc(session,
4526 session->smb2->signing_key);
4527 if (session->smb2->application_key.data == NULL) {
4528 ZERO_STRUCT(session_key);
4529 return NT_STATUS_NO_MEMORY;
4532 if (conn->protocol >= PROTOCOL_SMB2_24) {
4533 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4534 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4536 smb2_key_derivation(session_key, sizeof(session_key),
4537 label.data, label.length,
4538 context.data, context.length,
4539 session->smb2->application_key.data);
4541 ZERO_STRUCT(session_key);
4543 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4544 session->smb2->signing_key);
4545 if (session->smb2_channel.signing_key.data == NULL) {
4546 return NT_STATUS_NO_MEMORY;
4549 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4550 session->conn->protocol,
4552 if (!NT_STATUS_IS_OK(status)) {
4556 session->smb2->should_sign = false;
4557 session->smb2->should_encrypt = false;
4559 if (conn->desire_signing) {
4560 session->smb2->should_sign = true;
4563 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4564 session->smb2->should_sign = true;
4567 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4568 session->smb2->should_encrypt = true;
4571 if (conn->protocol < PROTOCOL_SMB2_24) {
4572 session->smb2->should_encrypt = false;
4575 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4576 session->smb2->should_encrypt = false;
4579 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4580 sizeof(session->smb2->nonce_high));
4581 session->smb2->nonce_low = 1;
4583 return NT_STATUS_OK;
4586 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4587 struct smbXcli_session *session1,
4588 struct smbXcli_conn *conn,
4589 struct smbXcli_session **_session2)
4591 struct smbXcli_session *session2;
4593 if (session1->smb2->signing_key.length == 0) {
4594 return NT_STATUS_INVALID_PARAMETER_MIX;
4598 return NT_STATUS_INVALID_PARAMETER_MIX;
4601 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4602 if (session2 == NULL) {
4603 return NT_STATUS_NO_MEMORY;
4605 session2->smb2 = talloc_reference(session2, session1->smb2);
4606 if (session2->smb2 == NULL) {
4607 talloc_free(session2);
4608 return NT_STATUS_NO_MEMORY;
4611 talloc_set_destructor(session2, smbXcli_session_destructor);
4612 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4613 session2->conn = conn;
4615 *_session2 = session2;
4616 return NT_STATUS_OK;
4619 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4620 const DATA_BLOB _channel_key,
4621 const struct iovec *recv_iov)
4623 struct smbXcli_conn *conn = session->conn;
4624 uint8_t channel_key[16];
4628 return NT_STATUS_INVALID_PARAMETER_MIX;
4631 if (session->smb2_channel.signing_key.length != 0) {
4632 return NT_STATUS_INVALID_PARAMETER_MIX;
4635 ZERO_STRUCT(channel_key);
4636 memcpy(channel_key, _channel_key.data,
4637 MIN(_channel_key.length, sizeof(channel_key)));
4639 session->smb2_channel.signing_key = data_blob_talloc(session,
4641 sizeof(channel_key));
4642 if (session->smb2_channel.signing_key.data == NULL) {
4643 ZERO_STRUCT(channel_key);
4644 return NT_STATUS_NO_MEMORY;
4647 if (conn->protocol >= PROTOCOL_SMB2_24) {
4648 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4649 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4651 smb2_key_derivation(channel_key, sizeof(channel_key),
4652 label.data, label.length,
4653 context.data, context.length,
4654 session->smb2_channel.signing_key.data);
4656 ZERO_STRUCT(channel_key);
4658 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4659 session->conn->protocol,
4661 if (!NT_STATUS_IS_OK(status)) {
4665 return NT_STATUS_OK;
4668 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4670 struct smbXcli_tcon *tcon;
4672 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4680 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4682 return tcon->smb1.tcon_id;
4685 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4687 tcon->smb1.tcon_id = tcon_id;
4690 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4692 uint16_t optional_support,
4693 uint32_t maximal_access,
4694 uint32_t guest_maximal_access,
4695 const char *service,
4696 const char *fs_type)
4698 tcon->smb1.tcon_id = tcon_id;
4699 tcon->smb1.optional_support = optional_support;
4700 tcon->smb1.maximal_access = maximal_access;
4701 tcon->smb1.guest_maximal_access = guest_maximal_access;
4703 TALLOC_FREE(tcon->smb1.service);
4704 tcon->smb1.service = talloc_strdup(tcon, service);
4705 if (service != NULL && tcon->smb1.service == NULL) {
4709 TALLOC_FREE(tcon->smb1.fs_type);
4710 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
4711 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
4718 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
4720 return tcon->smb2.tcon_id;
4723 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
4725 return tcon->smb2.capabilities;
4728 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
4729 struct smbXcli_session *session,
4733 uint32_t capabilities,
4734 uint32_t maximal_access)
4736 tcon->smb2.tcon_id = tcon_id;
4737 tcon->smb2.type = type;
4738 tcon->smb2.flags = flags;
4739 tcon->smb2.capabilities = capabilities;
4740 tcon->smb2.maximal_access = maximal_access;
4742 tcon->smb2.should_encrypt = false;
4744 if (session == NULL) {
4748 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
4750 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
4751 tcon->smb2.should_encrypt = true;