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 * this should be a short term hack
163 * until the upper layers have implemented
166 bool disconnect_expired;
169 struct smbXcli_tcon {
171 uint32_t fs_attributes;
175 uint16_t optional_support;
176 uint32_t maximal_access;
177 uint32_t guest_maximal_access;
186 uint32_t capabilities;
187 uint32_t maximal_access;
192 struct smbXcli_req_state {
193 struct tevent_context *ev;
194 struct smbXcli_conn *conn;
195 struct smbXcli_session *session; /* maybe NULL */
196 struct smbXcli_tcon *tcon; /* maybe NULL */
198 uint8_t length_hdr[4];
205 /* Space for the header including the wct */
206 uint8_t hdr[HDR_VWV];
209 * For normal requests, smb1cli_req_send chooses a mid.
210 * SecondaryV trans requests need to use the mid of the primary
211 * request, so we need a place to store it.
212 * Assume it is set if != 0.
217 uint8_t bytecount_buf[2];
219 #define MAX_SMB_IOV 10
220 /* length_hdr, hdr, words, byte_count, buffers */
221 struct iovec iov[1 + 3 + MAX_SMB_IOV];
226 struct tevent_req **chained_requests;
229 NTSTATUS recv_status;
230 /* always an array of 3 talloc elements */
231 struct iovec *recv_iov;
235 const uint8_t *fixed;
240 uint8_t transform[SMB2_TF_HDR_SIZE];
241 uint8_t hdr[SMB2_HDR_BODY];
242 uint8_t pad[7]; /* padding space for compounding */
245 * always an array of 3 talloc elements
246 * (without a SMB2_TRANSFORM header!)
250 struct iovec *recv_iov;
253 * the expected max for the response dyn_len
255 uint32_t max_dyn_len;
257 uint16_t credit_charge;
261 uint64_t encryption_session_id;
263 bool signing_skipped;
269 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
272 * NT_STATUS_OK, means we do not notify the callers
274 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
276 while (conn->sessions) {
277 conn->sessions->conn = NULL;
278 DLIST_REMOVE(conn->sessions, conn->sessions);
281 if (conn->smb1.trans_enc) {
282 TALLOC_FREE(conn->smb1.trans_enc);
288 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
290 const char *remote_name,
291 enum smb_signing_setting signing_state,
292 uint32_t smb1_capabilities,
293 struct GUID *client_guid,
294 uint32_t smb2_capabilities)
296 struct smbXcli_conn *conn = NULL;
298 struct sockaddr *sa = NULL;
302 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
308 conn->write_fd = dup(fd);
309 if (conn->write_fd == -1) {
313 conn->remote_name = talloc_strdup(conn, remote_name);
314 if (conn->remote_name == NULL) {
319 ss = (void *)&conn->local_ss;
320 sa = (struct sockaddr *)ss;
321 sa_length = sizeof(conn->local_ss);
322 ret = getsockname(fd, sa, &sa_length);
326 ss = (void *)&conn->remote_ss;
327 sa = (struct sockaddr *)ss;
328 sa_length = sizeof(conn->remote_ss);
329 ret = getpeername(fd, sa, &sa_length);
334 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
335 if (conn->outgoing == NULL) {
338 conn->pending = NULL;
340 conn->protocol = PROTOCOL_NONE;
342 switch (signing_state) {
343 case SMB_SIGNING_OFF:
345 conn->allow_signing = false;
346 conn->desire_signing = false;
347 conn->mandatory_signing = false;
349 case SMB_SIGNING_DEFAULT:
350 case SMB_SIGNING_IF_REQUIRED:
351 /* if the server requires it */
352 conn->allow_signing = true;
353 conn->desire_signing = false;
354 conn->mandatory_signing = false;
356 case SMB_SIGNING_REQUIRED:
358 conn->allow_signing = true;
359 conn->desire_signing = true;
360 conn->mandatory_signing = true;
364 conn->smb1.client.capabilities = smb1_capabilities;
365 conn->smb1.client.max_xmit = UINT16_MAX;
367 conn->smb1.capabilities = conn->smb1.client.capabilities;
368 conn->smb1.max_xmit = 1024;
372 /* initialise signing */
373 conn->smb1.signing = smb_signing_init(conn,
375 conn->desire_signing,
376 conn->mandatory_signing);
377 if (!conn->smb1.signing) {
381 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
382 if (conn->mandatory_signing) {
383 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
386 conn->smb2.client.guid = *client_guid;
388 conn->smb2.client.capabilities = smb2_capabilities;
390 conn->smb2.cur_credits = 1;
391 conn->smb2.max_credits = 0;
393 talloc_set_destructor(conn, smbXcli_conn_destructor);
397 if (conn->write_fd != -1) {
398 close(conn->write_fd);
404 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
410 if (conn->read_fd == -1) {
417 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
419 return conn->protocol;
422 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
424 if (conn->protocol >= PROTOCOL_SMB2_02) {
428 if (conn->smb1.capabilities & CAP_UNICODE) {
435 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
437 set_socket_options(conn->read_fd, options);
440 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
442 return &conn->local_ss;
445 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
447 return &conn->remote_ss;
450 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
452 return conn->remote_name;
455 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
457 if (conn->protocol >= PROTOCOL_SMB2_02) {
464 return conn->smb1.server.max_mux;
467 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
469 if (conn->protocol >= PROTOCOL_SMB2_02) {
470 return conn->smb2.server.system_time;
473 return conn->smb1.server.system_time;
476 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
478 if (conn->protocol >= PROTOCOL_SMB2_02) {
479 return &conn->smb2.server.gss_blob;
482 return &conn->smb1.server.gss_blob;
485 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
487 if (conn->protocol >= PROTOCOL_SMB2_02) {
488 return &conn->smb2.server.guid;
491 return &conn->smb1.server.guid;
494 struct smbXcli_conn_samba_suicide_state {
495 struct smbXcli_conn *conn;
500 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
502 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
503 struct tevent_context *ev,
504 struct smbXcli_conn *conn,
507 struct tevent_req *req, *subreq;
508 struct smbXcli_conn_samba_suicide_state *state;
510 req = tevent_req_create(mem_ctx, &state,
511 struct smbXcli_conn_samba_suicide_state);
516 SIVAL(state->buf, 4, 0x74697865);
517 SCVAL(state->buf, 8, exitcode);
518 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
520 state->iov.iov_base = state->buf;
521 state->iov.iov_len = sizeof(state->buf);
523 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
524 false, &state->iov, 1);
525 if (tevent_req_nomem(subreq, req)) {
526 return tevent_req_post(req, ev);
528 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
532 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
534 struct tevent_req *req = tevent_req_callback_data(
535 subreq, struct tevent_req);
536 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
537 req, struct smbXcli_conn_samba_suicide_state);
541 nwritten = writev_recv(subreq, &err);
543 if (nwritten == -1) {
544 NTSTATUS status = map_nt_error_from_unix_common(err);
545 smbXcli_conn_disconnect(state->conn, status);
548 tevent_req_done(req);
551 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
553 return tevent_req_simple_recv_ntstatus(req);
556 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
559 TALLOC_CTX *frame = talloc_stackframe();
560 struct tevent_context *ev;
561 struct tevent_req *req;
562 NTSTATUS status = NT_STATUS_NO_MEMORY;
565 if (smbXcli_conn_has_async_calls(conn)) {
567 * Can't use sync call while an async call is in flight
569 status = NT_STATUS_INVALID_PARAMETER_MIX;
572 ev = samba_tevent_context_init(frame);
576 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
580 ok = tevent_req_poll(req, ev);
582 status = map_nt_error_from_unix_common(errno);
585 status = smbXcli_conn_samba_suicide_recv(req);
591 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
593 return conn->smb1.capabilities;
596 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
598 return conn->smb1.max_xmit;
601 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
604 uint16_t possible = conn->smb1.server.max_mux;
606 pending = tevent_queue_length(conn->outgoing);
607 if (pending >= possible) {
610 pending += talloc_array_length(conn->pending);
611 if (pending >= possible) {
618 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
620 return conn->smb1.server.session_key;
623 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
625 return conn->smb1.server.challenge;
628 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
630 return conn->smb1.server.security_mode;
633 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
635 return conn->smb1.server.readbraw;
638 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
640 return conn->smb1.server.writebraw;
643 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
645 return conn->smb1.server.lockread;
648 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
650 return conn->smb1.server.writeunlock;
653 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
655 return conn->smb1.server.time_zone;
658 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
659 const DATA_BLOB user_session_key,
660 const DATA_BLOB response)
662 return smb_signing_activate(conn->smb1.signing,
667 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
668 const uint8_t *buf, uint32_t seqnum)
670 const uint8_t *hdr = buf + NBT_HDR_SIZE;
671 size_t len = smb_len_nbt(buf);
673 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
676 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
678 return smb_signing_is_active(conn->smb1.signing);
681 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
682 struct smb_trans_enc_state *es)
684 /* Replace the old state, if any. */
685 if (conn->smb1.trans_enc) {
686 TALLOC_FREE(conn->smb1.trans_enc);
688 conn->smb1.trans_enc = es;
691 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
693 return common_encryption_on(conn->smb1.trans_enc);
697 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
699 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
700 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
702 if (NT_STATUS_IS_OK(status)) {
706 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
710 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
714 * Is the SMB command able to hold an AND_X successor
715 * @param[in] cmd The SMB command in question
716 * @retval Can we add a chained request after "cmd"?
718 bool smb1cli_is_andx_req(uint8_t cmd)
738 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
740 size_t num_pending = talloc_array_length(conn->pending);
743 if (conn->protocol == PROTOCOL_NONE) {
745 * This is what windows sends on the SMB1 Negprot request
746 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
754 result = conn->smb1.mid++;
755 if ((result == 0) || (result == 0xffff)) {
759 for (i=0; i<num_pending; i++) {
760 if (result == smb1cli_req_mid(conn->pending[i])) {
765 if (i == num_pending) {
771 void smbXcli_req_unset_pending(struct tevent_req *req)
773 struct smbXcli_req_state *state =
775 struct smbXcli_req_state);
776 struct smbXcli_conn *conn = state->conn;
777 size_t num_pending = talloc_array_length(conn->pending);
780 if (state->smb1.mid != 0) {
782 * This is a [nt]trans[2] request which waits
783 * for more than one reply.
788 talloc_set_destructor(req, NULL);
790 if (num_pending == 1) {
792 * The pending read_smb tevent_req is a child of
793 * conn->pending. So if nothing is pending anymore, we need to
794 * delete the socket read fde.
796 TALLOC_FREE(conn->pending);
797 conn->read_smb_req = NULL;
801 for (i=0; i<num_pending; i++) {
802 if (req == conn->pending[i]) {
806 if (i == num_pending) {
808 * Something's seriously broken. Just returning here is the
809 * right thing nevertheless, the point of this routine is to
810 * remove ourselves from conn->pending.
816 * Remove ourselves from the conn->pending array
818 for (; i < (num_pending - 1); i++) {
819 conn->pending[i] = conn->pending[i+1];
823 * No NULL check here, we're shrinking by sizeof(void *), and
824 * talloc_realloc just adjusts the size for this.
826 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
831 static int smbXcli_req_destructor(struct tevent_req *req)
833 struct smbXcli_req_state *state =
835 struct smbXcli_req_state);
838 * Make sure we really remove it from
839 * the pending array on destruction.
842 smbXcli_req_unset_pending(req);
846 static bool smb1cli_req_cancel(struct tevent_req *req);
847 static bool smb2cli_req_cancel(struct tevent_req *req);
849 static bool smbXcli_req_cancel(struct tevent_req *req)
851 struct smbXcli_req_state *state =
853 struct smbXcli_req_state);
855 if (!smbXcli_conn_is_connected(state->conn)) {
859 if (state->conn->protocol == PROTOCOL_NONE) {
863 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
864 return smb2cli_req_cancel(req);
867 return smb1cli_req_cancel(req);
870 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
872 bool smbXcli_req_set_pending(struct tevent_req *req)
874 struct smbXcli_req_state *state =
876 struct smbXcli_req_state);
877 struct smbXcli_conn *conn;
878 struct tevent_req **pending;
883 if (!smbXcli_conn_is_connected(conn)) {
887 num_pending = talloc_array_length(conn->pending);
889 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
891 if (pending == NULL) {
894 pending[num_pending] = req;
895 conn->pending = pending;
896 talloc_set_destructor(req, smbXcli_req_destructor);
897 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
899 if (!smbXcli_conn_receive_next(conn)) {
901 * the caller should notify the current request
903 * And all other pending requests get notified
904 * by smbXcli_conn_disconnect().
906 smbXcli_req_unset_pending(req);
907 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
914 static void smbXcli_conn_received(struct tevent_req *subreq);
916 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
918 size_t num_pending = talloc_array_length(conn->pending);
919 struct tevent_req *req;
920 struct smbXcli_req_state *state;
922 if (conn->read_smb_req != NULL) {
926 if (num_pending == 0) {
927 if (conn->smb2.mid < UINT64_MAX) {
928 /* no more pending requests, so we are done for now */
933 * If there are no more SMB2 requests possible,
934 * because we are out of message ids,
935 * we need to disconnect.
937 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
941 req = conn->pending[0];
942 state = tevent_req_data(req, struct smbXcli_req_state);
945 * We're the first ones, add the read_smb request that waits for the
946 * answer from the server
948 conn->read_smb_req = read_smb_send(conn->pending,
951 if (conn->read_smb_req == NULL) {
954 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
958 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
960 struct smbXcli_session *session;
962 tevent_queue_stop(conn->outgoing);
964 if (conn->read_fd != -1) {
965 close(conn->read_fd);
967 if (conn->write_fd != -1) {
968 close(conn->write_fd);
973 session = conn->sessions;
974 if (talloc_array_length(conn->pending) == 0) {
976 * if we do not have pending requests
977 * there is no need to update the channel_sequence
981 for (; session; session = session->next) {
982 smb2cli_session_increment_channel_sequence(session);
986 * Cancel all pending requests. We do not do a for-loop walking
987 * conn->pending because that array changes in
988 * smbXcli_req_unset_pending.
990 while (talloc_array_length(conn->pending) > 0) {
991 struct tevent_req *req;
992 struct smbXcli_req_state *state;
993 struct tevent_req **chain;
997 req = conn->pending[0];
998 state = tevent_req_data(req, struct smbXcli_req_state);
1000 if (state->smb1.chained_requests == NULL) {
1002 * We're dead. No point waiting for trans2
1005 state->smb1.mid = 0;
1007 smbXcli_req_unset_pending(req);
1009 if (NT_STATUS_IS_OK(status)) {
1010 /* do not notify the callers */
1015 * we need to defer the callback, because we may notify
1016 * more then one caller.
1018 tevent_req_defer_callback(req, state->ev);
1019 tevent_req_nterror(req, status);
1023 chain = talloc_move(conn, &state->smb1.chained_requests);
1024 num_chained = talloc_array_length(chain);
1026 for (i=0; i<num_chained; i++) {
1028 state = tevent_req_data(req, struct smbXcli_req_state);
1031 * We're dead. No point waiting for trans2
1034 state->smb1.mid = 0;
1036 smbXcli_req_unset_pending(req);
1038 if (NT_STATUS_IS_OK(status)) {
1039 /* do not notify the callers */
1044 * we need to defer the callback, because we may notify
1045 * more than one caller.
1047 tevent_req_defer_callback(req, state->ev);
1048 tevent_req_nterror(req, status);
1055 * Fetch a smb request's mid. Only valid after the request has been sent by
1056 * smb1cli_req_send().
1058 uint16_t smb1cli_req_mid(struct tevent_req *req)
1060 struct smbXcli_req_state *state =
1061 tevent_req_data(req,
1062 struct smbXcli_req_state);
1064 if (state->smb1.mid != 0) {
1065 return state->smb1.mid;
1068 return SVAL(state->smb1.hdr, HDR_MID);
1071 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1073 struct smbXcli_req_state *state =
1074 tevent_req_data(req,
1075 struct smbXcli_req_state);
1077 state->smb1.mid = mid;
1080 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1082 struct smbXcli_req_state *state =
1083 tevent_req_data(req,
1084 struct smbXcli_req_state);
1086 return state->smb1.seqnum;
1089 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1091 struct smbXcli_req_state *state =
1092 tevent_req_data(req,
1093 struct smbXcli_req_state);
1095 state->smb1.seqnum = seqnum;
1098 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1102 for (i=0; i<count; i++) {
1103 result += iov[i].iov_len;
1108 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1109 const struct iovec *iov,
1112 size_t len = smbXcli_iov_len(iov, count);
1117 buf = talloc_array(mem_ctx, uint8_t, len);
1122 for (i=0; i<count; i++) {
1123 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1124 copied += iov[i].iov_len;
1129 static void smb1cli_req_flags(enum protocol_types protocol,
1130 uint32_t smb1_capabilities,
1131 uint8_t smb_command,
1132 uint8_t additional_flags,
1133 uint8_t clear_flags,
1135 uint16_t additional_flags2,
1136 uint16_t clear_flags2,
1140 uint16_t flags2 = 0;
1142 if (protocol >= PROTOCOL_LANMAN1) {
1143 flags |= FLAG_CASELESS_PATHNAMES;
1144 flags |= FLAG_CANONICAL_PATHNAMES;
1147 if (protocol >= PROTOCOL_LANMAN2) {
1148 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1149 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1152 if (protocol >= PROTOCOL_NT1) {
1153 flags2 |= FLAGS2_IS_LONG_NAME;
1155 if (smb1_capabilities & CAP_UNICODE) {
1156 flags2 |= FLAGS2_UNICODE_STRINGS;
1158 if (smb1_capabilities & CAP_STATUS32) {
1159 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1161 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1162 flags2 |= FLAGS2_EXTENDED_SECURITY;
1166 flags |= additional_flags;
1167 flags &= ~clear_flags;
1168 flags2 |= additional_flags2;
1169 flags2 &= ~clear_flags2;
1175 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1177 static bool smb1cli_req_cancel(struct tevent_req *req)
1179 struct smbXcli_req_state *state =
1180 tevent_req_data(req,
1181 struct smbXcli_req_state);
1186 struct tevent_req *subreq;
1189 flags = CVAL(state->smb1.hdr, HDR_FLG);
1190 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1191 pid = SVAL(state->smb1.hdr, HDR_PID);
1192 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1193 mid = SVAL(state->smb1.hdr, HDR_MID);
1195 subreq = smb1cli_req_create(state, state->ev,
1205 0, NULL); /* bytes */
1206 if (subreq == NULL) {
1209 smb1cli_req_set_mid(subreq, mid);
1211 status = smb1cli_req_chain_submit(&subreq, 1);
1212 if (!NT_STATUS_IS_OK(status)) {
1213 TALLOC_FREE(subreq);
1216 smb1cli_req_set_mid(subreq, 0);
1218 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1223 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1225 /* we do not care about the result */
1226 TALLOC_FREE(subreq);
1229 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1230 struct tevent_context *ev,
1231 struct smbXcli_conn *conn,
1232 uint8_t smb_command,
1233 uint8_t additional_flags,
1234 uint8_t clear_flags,
1235 uint16_t additional_flags2,
1236 uint16_t clear_flags2,
1237 uint32_t timeout_msec,
1239 struct smbXcli_tcon *tcon,
1240 struct smbXcli_session *session,
1241 uint8_t wct, uint16_t *vwv,
1243 struct iovec *bytes_iov)
1245 struct tevent_req *req;
1246 struct smbXcli_req_state *state;
1248 uint16_t flags2 = 0;
1252 if (iov_count > MAX_SMB_IOV) {
1254 * Should not happen :-)
1259 req = tevent_req_create(mem_ctx, &state,
1260 struct smbXcli_req_state);
1266 state->session = session;
1270 uid = session->smb1.session_id;
1274 tid = tcon->smb1.tcon_id;
1277 state->smb1.recv_cmd = 0xFF;
1278 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1279 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1280 if (state->smb1.recv_iov == NULL) {
1285 smb1cli_req_flags(conn->protocol,
1286 conn->smb1.capabilities,
1295 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1296 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1297 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1298 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1299 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1300 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1301 SSVAL(state->smb1.hdr, HDR_TID, tid);
1302 SSVAL(state->smb1.hdr, HDR_PID, pid);
1303 SSVAL(state->smb1.hdr, HDR_UID, uid);
1304 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1305 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1307 state->smb1.vwv = vwv;
1309 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1311 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1312 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1313 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1314 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1315 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1316 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1317 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1318 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1320 if (iov_count != 0) {
1321 memcpy(&state->smb1.iov[4], bytes_iov,
1322 iov_count * sizeof(*bytes_iov));
1324 state->smb1.iov_count = iov_count + 4;
1326 if (timeout_msec > 0) {
1327 struct timeval endtime;
1329 endtime = timeval_current_ofs_msec(timeout_msec);
1330 if (!tevent_req_set_endtime(req, ev, endtime)) {
1335 switch (smb_command) {
1339 state->one_way = true;
1342 state->one_way = true;
1343 state->smb1.one_way_seqnum = true;
1347 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1348 state->one_way = true;
1356 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1357 struct iovec *iov, int iov_count,
1359 bool one_way_seqnum)
1361 TALLOC_CTX *frame = NULL;
1365 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1366 * iovec directly. MD5Update would do that just fine.
1369 if (iov_count < 4) {
1370 return NT_STATUS_INVALID_PARAMETER_MIX;
1372 if (iov[0].iov_len != NBT_HDR_SIZE) {
1373 return NT_STATUS_INVALID_PARAMETER_MIX;
1375 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1376 return NT_STATUS_INVALID_PARAMETER_MIX;
1378 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1379 return NT_STATUS_INVALID_PARAMETER_MIX;
1381 if (iov[3].iov_len != sizeof(uint16_t)) {
1382 return NT_STATUS_INVALID_PARAMETER_MIX;
1385 frame = talloc_stackframe();
1387 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1389 return NT_STATUS_NO_MEMORY;
1392 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1394 smb_signing_sign_pdu(conn->smb1.signing,
1395 buf, talloc_get_size(buf),
1397 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1400 return NT_STATUS_OK;
1403 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1404 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1405 TALLOC_CTX *tmp_mem,
1408 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1409 struct smbXcli_req_state *state,
1410 struct iovec *iov, int iov_count)
1412 struct tevent_req *subreq;
1417 if (!smbXcli_conn_is_connected(state->conn)) {
1418 return NT_STATUS_CONNECTION_DISCONNECTED;
1421 if (state->conn->protocol > PROTOCOL_NT1) {
1422 return NT_STATUS_REVISION_MISMATCH;
1425 if (iov_count < 4) {
1426 return NT_STATUS_INVALID_PARAMETER_MIX;
1428 if (iov[0].iov_len != NBT_HDR_SIZE) {
1429 return NT_STATUS_INVALID_PARAMETER_MIX;
1431 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1432 return NT_STATUS_INVALID_PARAMETER_MIX;
1434 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1435 return NT_STATUS_INVALID_PARAMETER_MIX;
1437 if (iov[3].iov_len != sizeof(uint16_t)) {
1438 return NT_STATUS_INVALID_PARAMETER_MIX;
1441 cmd = CVAL(iov[1].iov_base, HDR_COM);
1442 if (cmd == SMBreadBraw) {
1443 if (smbXcli_conn_has_async_calls(state->conn)) {
1444 return NT_STATUS_INVALID_PARAMETER_MIX;
1446 state->conn->smb1.read_braw_req = req;
1449 if (state->smb1.mid != 0) {
1450 mid = state->smb1.mid;
1452 mid = smb1cli_alloc_mid(state->conn);
1454 SSVAL(iov[1].iov_base, HDR_MID, mid);
1456 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1458 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1459 &state->smb1.seqnum,
1460 state->smb1.one_way_seqnum);
1462 if (!NT_STATUS_IS_OK(status)) {
1467 * If we supported multiple encrytion contexts
1468 * here we'd look up based on tid.
1470 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1471 char *buf, *enc_buf;
1473 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1475 return NT_STATUS_NO_MEMORY;
1477 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1478 (char *)buf, &enc_buf);
1480 if (!NT_STATUS_IS_OK(status)) {
1481 DEBUG(0, ("Error in encrypting client message: %s\n",
1482 nt_errstr(status)));
1485 buf = (char *)talloc_memdup(state, enc_buf,
1486 smb_len_nbt(enc_buf)+4);
1489 return NT_STATUS_NO_MEMORY;
1491 iov[0].iov_base = (void *)buf;
1492 iov[0].iov_len = talloc_get_size(buf);
1496 if (state->conn->dispatch_incoming == NULL) {
1497 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1500 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1502 subreq = writev_send(state, state->ev, state->conn->outgoing,
1503 state->conn->write_fd, false, iov, iov_count);
1504 if (subreq == NULL) {
1505 return NT_STATUS_NO_MEMORY;
1507 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1508 return NT_STATUS_OK;
1511 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1512 struct tevent_context *ev,
1513 struct smbXcli_conn *conn,
1514 uint8_t smb_command,
1515 uint8_t additional_flags,
1516 uint8_t clear_flags,
1517 uint16_t additional_flags2,
1518 uint16_t clear_flags2,
1519 uint32_t timeout_msec,
1521 struct smbXcli_tcon *tcon,
1522 struct smbXcli_session *session,
1523 uint8_t wct, uint16_t *vwv,
1525 const uint8_t *bytes)
1527 struct tevent_req *req;
1531 iov.iov_base = discard_const_p(void, bytes);
1532 iov.iov_len = num_bytes;
1534 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1535 additional_flags, clear_flags,
1536 additional_flags2, clear_flags2,
1543 if (!tevent_req_is_in_progress(req)) {
1544 return tevent_req_post(req, ev);
1546 status = smb1cli_req_chain_submit(&req, 1);
1547 if (tevent_req_nterror(req, status)) {
1548 return tevent_req_post(req, ev);
1553 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1555 struct tevent_req *req =
1556 tevent_req_callback_data(subreq,
1558 struct smbXcli_req_state *state =
1559 tevent_req_data(req,
1560 struct smbXcli_req_state);
1564 nwritten = writev_recv(subreq, &err);
1565 TALLOC_FREE(subreq);
1566 if (nwritten == -1) {
1567 NTSTATUS status = map_nt_error_from_unix_common(err);
1568 smbXcli_conn_disconnect(state->conn, status);
1572 if (state->one_way) {
1573 state->inbuf = NULL;
1574 tevent_req_done(req);
1578 if (!smbXcli_req_set_pending(req)) {
1579 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1584 static void smbXcli_conn_received(struct tevent_req *subreq)
1586 struct smbXcli_conn *conn =
1587 tevent_req_callback_data(subreq,
1588 struct smbXcli_conn);
1589 TALLOC_CTX *frame = talloc_stackframe();
1595 if (subreq != conn->read_smb_req) {
1596 DEBUG(1, ("Internal error: cli_smb_received called with "
1597 "unexpected subreq\n"));
1598 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1602 conn->read_smb_req = NULL;
1604 received = read_smb_recv(subreq, frame, &inbuf, &err);
1605 TALLOC_FREE(subreq);
1606 if (received == -1) {
1607 status = map_nt_error_from_unix_common(err);
1608 smbXcli_conn_disconnect(conn, status);
1613 status = conn->dispatch_incoming(conn, frame, inbuf);
1615 if (NT_STATUS_IS_OK(status)) {
1617 * We should not do any more processing
1618 * as the dispatch function called
1619 * tevent_req_done().
1624 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1626 * We got an error, so notify all pending requests
1628 smbXcli_conn_disconnect(conn, status);
1633 * We got NT_STATUS_RETRY, so we may ask for a
1634 * next incoming pdu.
1636 if (!smbXcli_conn_receive_next(conn)) {
1637 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1641 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1642 struct iovec **piov, int *pnum_iov)
1653 size_t min_size = MIN_SMB_SIZE;
1655 buflen = smb_len_tcp(buf);
1658 hdr = buf + NBT_HDR_SIZE;
1660 status = smb1cli_pull_raw_error(hdr);
1661 if (NT_STATUS_IS_ERR(status)) {
1663 * This is an ugly hack to support OS/2
1664 * which skips the byte_count in the DATA block
1665 * on some error responses.
1669 min_size -= sizeof(uint16_t);
1672 if (buflen < min_size) {
1673 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1677 * This returns iovec elements in the following order:
1692 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1694 return NT_STATUS_NO_MEMORY;
1696 iov[0].iov_base = hdr;
1697 iov[0].iov_len = HDR_WCT;
1700 cmd = CVAL(hdr, HDR_COM);
1704 size_t len = buflen - taken;
1706 struct iovec *iov_tmp;
1713 * we need at least WCT
1715 needed = sizeof(uint8_t);
1717 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1718 __location__, (int)len, (int)needed));
1723 * Now we check if the specified words are there
1725 wct = CVAL(hdr, wct_ofs);
1726 needed += wct * sizeof(uint16_t);
1728 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1729 __location__, (int)len, (int)needed));
1733 if ((num_iov == 1) &&
1735 NT_STATUS_IS_ERR(status))
1738 * This is an ugly hack to support OS/2
1739 * which skips the byte_count in the DATA block
1740 * on some error responses.
1744 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1746 if (iov_tmp == NULL) {
1748 return NT_STATUS_NO_MEMORY;
1751 cur = &iov[num_iov];
1755 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1757 cur[1].iov_base = cur[0].iov_base;
1764 * we need at least BCC
1766 needed += sizeof(uint16_t);
1768 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1769 __location__, (int)len, (int)needed));
1774 * Now we check if the specified bytes are there
1776 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1777 bcc = SVAL(hdr, bcc_ofs);
1778 needed += bcc * sizeof(uint8_t);
1780 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1781 __location__, (int)len, (int)needed));
1786 * we allocate 2 iovec structures for words and bytes
1788 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1790 if (iov_tmp == NULL) {
1792 return NT_STATUS_NO_MEMORY;
1795 cur = &iov[num_iov];
1798 cur[0].iov_len = wct * sizeof(uint16_t);
1799 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1800 cur[1].iov_len = bcc * sizeof(uint8_t);
1801 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1805 if (!smb1cli_is_andx_req(cmd)) {
1807 * If the current command does not have AndX chanining
1813 if (wct == 0 && bcc == 0) {
1815 * An empty response also ends the chain,
1816 * most likely with an error.
1822 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1823 __location__, (int)wct, (int)cmd));
1826 cmd = CVAL(cur[0].iov_base, 0);
1829 * If it is the end of the chain we are also done.
1833 wct_ofs = SVAL(cur[0].iov_base, 2);
1835 if (wct_ofs < taken) {
1836 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1838 if (wct_ofs > buflen) {
1839 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1843 * we consumed everything up to the start of the next
1849 remaining = buflen - taken;
1851 if (remaining > 0 && num_iov >= 3) {
1853 * The last DATA block gets the remaining
1854 * bytes, this is needed to support
1855 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1857 iov[num_iov-1].iov_len += remaining;
1861 *pnum_iov = num_iov;
1862 return NT_STATUS_OK;
1866 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1869 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1870 TALLOC_CTX *tmp_mem,
1873 struct tevent_req *req;
1874 struct smbXcli_req_state *state;
1881 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1882 size_t len = smb_len_tcp(inbuf);
1883 struct iovec *iov = NULL;
1885 struct tevent_req **chain = NULL;
1886 size_t num_chained = 0;
1887 size_t num_responses = 0;
1889 if (conn->smb1.read_braw_req != NULL) {
1890 req = conn->smb1.read_braw_req;
1891 conn->smb1.read_braw_req = NULL;
1892 state = tevent_req_data(req, struct smbXcli_req_state);
1894 smbXcli_req_unset_pending(req);
1896 if (state->smb1.recv_iov == NULL) {
1898 * For requests with more than
1899 * one response, we have to readd the
1902 state->smb1.recv_iov = talloc_zero_array(state,
1905 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1906 return NT_STATUS_OK;
1910 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1911 state->smb1.recv_iov[0].iov_len = len;
1912 ZERO_STRUCT(state->smb1.recv_iov[1]);
1913 ZERO_STRUCT(state->smb1.recv_iov[2]);
1915 state->smb1.recv_cmd = SMBreadBraw;
1916 state->smb1.recv_status = NT_STATUS_OK;
1917 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1919 tevent_req_done(req);
1920 return NT_STATUS_OK;
1923 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1924 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1925 DEBUG(10, ("Got non-SMB PDU\n"));
1926 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1930 * If we supported multiple encrytion contexts
1931 * here we'd look up based on tid.
1933 if (common_encryption_on(conn->smb1.trans_enc)
1934 && (CVAL(inbuf, 0) == 0)) {
1935 uint16_t enc_ctx_num;
1937 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1938 if (!NT_STATUS_IS_OK(status)) {
1939 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1940 nt_errstr(status)));
1944 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1945 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1947 conn->smb1.trans_enc->enc_ctx_num));
1948 return NT_STATUS_INVALID_HANDLE;
1951 status = common_decrypt_buffer(conn->smb1.trans_enc,
1953 if (!NT_STATUS_IS_OK(status)) {
1954 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1955 nt_errstr(status)));
1958 inhdr = inbuf + NBT_HDR_SIZE;
1959 len = smb_len_nbt(inbuf);
1962 mid = SVAL(inhdr, HDR_MID);
1963 num_pending = talloc_array_length(conn->pending);
1965 for (i=0; i<num_pending; i++) {
1966 if (mid == smb1cli_req_mid(conn->pending[i])) {
1970 if (i == num_pending) {
1971 /* Dump unexpected reply */
1972 return NT_STATUS_RETRY;
1975 oplock_break = false;
1977 if (mid == 0xffff) {
1979 * Paranoia checks that this is really an oplock break request.
1981 oplock_break = (len == 51); /* hdr + 8 words */
1982 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1983 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1984 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1985 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1987 if (!oplock_break) {
1988 /* Dump unexpected reply */
1989 return NT_STATUS_RETRY;
1993 req = conn->pending[i];
1994 state = tevent_req_data(req, struct smbXcli_req_state);
1996 if (!oplock_break /* oplock breaks are not signed */
1997 && !smb_signing_check_pdu(conn->smb1.signing,
1998 inhdr, len, state->smb1.seqnum+1)) {
1999 DEBUG(10, ("cli_check_sign_mac failed\n"));
2000 return NT_STATUS_ACCESS_DENIED;
2003 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2005 if (!NT_STATUS_IS_OK(status)) {
2006 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2007 nt_errstr(status)));
2011 cmd = CVAL(inhdr, HDR_COM);
2012 status = smb1cli_pull_raw_error(inhdr);
2014 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2015 (state->session != NULL) && state->session->disconnect_expired)
2018 * this should be a short term hack
2019 * until the upper layers have implemented
2020 * re-authentication.
2025 if (state->smb1.chained_requests == NULL) {
2027 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2030 smbXcli_req_unset_pending(req);
2032 if (state->smb1.recv_iov == NULL) {
2034 * For requests with more than
2035 * one response, we have to readd the
2038 state->smb1.recv_iov = talloc_zero_array(state,
2041 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2042 return NT_STATUS_OK;
2046 state->smb1.recv_cmd = cmd;
2047 state->smb1.recv_status = status;
2048 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2050 state->smb1.recv_iov[0] = iov[0];
2051 state->smb1.recv_iov[1] = iov[1];
2052 state->smb1.recv_iov[2] = iov[2];
2054 if (talloc_array_length(conn->pending) == 0) {
2055 tevent_req_done(req);
2056 return NT_STATUS_OK;
2059 tevent_req_defer_callback(req, state->ev);
2060 tevent_req_done(req);
2061 return NT_STATUS_RETRY;
2064 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2065 num_chained = talloc_array_length(chain);
2066 num_responses = (num_iov - 1)/2;
2068 if (num_responses > num_chained) {
2069 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2072 for (i=0; i<num_chained; i++) {
2073 size_t iov_idx = 1 + (i*2);
2074 struct iovec *cur = &iov[iov_idx];
2078 state = tevent_req_data(req, struct smbXcli_req_state);
2080 smbXcli_req_unset_pending(req);
2083 * as we finish multiple requests here
2084 * we need to defer the callbacks as
2085 * they could destroy our current stack state.
2087 tevent_req_defer_callback(req, state->ev);
2089 if (i >= num_responses) {
2090 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2094 if (state->smb1.recv_iov == NULL) {
2096 * For requests with more than
2097 * one response, we have to readd the
2100 state->smb1.recv_iov = talloc_zero_array(state,
2103 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2108 state->smb1.recv_cmd = cmd;
2110 if (i == (num_responses - 1)) {
2112 * The last request in the chain gets the status
2114 state->smb1.recv_status = status;
2116 cmd = CVAL(cur[0].iov_base, 0);
2117 state->smb1.recv_status = NT_STATUS_OK;
2120 state->inbuf = inbuf;
2123 * Note: here we use talloc_reference() in a way
2124 * that does not expose it to the caller.
2126 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2127 if (tevent_req_nomem(inbuf_ref, req)) {
2131 /* copy the related buffers */
2132 state->smb1.recv_iov[0] = iov[0];
2133 state->smb1.recv_iov[1] = cur[0];
2134 state->smb1.recv_iov[2] = cur[1];
2136 tevent_req_done(req);
2139 return NT_STATUS_RETRY;
2142 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2143 TALLOC_CTX *mem_ctx,
2144 struct iovec **piov,
2148 uint32_t *pvwv_offset,
2149 uint32_t *pnum_bytes,
2151 uint32_t *pbytes_offset,
2153 const struct smb1cli_req_expected_response *expected,
2154 size_t num_expected)
2156 struct smbXcli_req_state *state =
2157 tevent_req_data(req,
2158 struct smbXcli_req_state);
2159 NTSTATUS status = NT_STATUS_OK;
2160 struct iovec *recv_iov = NULL;
2161 uint8_t *hdr = NULL;
2163 uint32_t vwv_offset = 0;
2164 uint16_t *vwv = NULL;
2165 uint32_t num_bytes = 0;
2166 uint32_t bytes_offset = 0;
2167 uint8_t *bytes = NULL;
2169 bool found_status = false;
2170 bool found_size = false;
2184 if (pvwv_offset != NULL) {
2187 if (pnum_bytes != NULL) {
2190 if (pbytes != NULL) {
2193 if (pbytes_offset != NULL) {
2196 if (pinbuf != NULL) {
2200 if (state->inbuf != NULL) {
2201 recv_iov = state->smb1.recv_iov;
2202 state->smb1.recv_iov = NULL;
2203 if (state->smb1.recv_cmd != SMBreadBraw) {
2204 hdr = (uint8_t *)recv_iov[0].iov_base;
2205 wct = recv_iov[1].iov_len/2;
2206 vwv = (uint16_t *)recv_iov[1].iov_base;
2207 vwv_offset = PTR_DIFF(vwv, hdr);
2208 num_bytes = recv_iov[2].iov_len;
2209 bytes = (uint8_t *)recv_iov[2].iov_base;
2210 bytes_offset = PTR_DIFF(bytes, hdr);
2214 if (tevent_req_is_nterror(req, &status)) {
2215 for (i=0; i < num_expected; i++) {
2216 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2217 found_status = true;
2223 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2229 if (num_expected == 0) {
2230 found_status = true;
2234 status = state->smb1.recv_status;
2236 for (i=0; i < num_expected; i++) {
2237 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2241 found_status = true;
2242 if (expected[i].wct == 0) {
2247 if (expected[i].wct == wct) {
2253 if (!found_status) {
2258 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2262 *piov = talloc_move(mem_ctx, &recv_iov);
2274 if (pvwv_offset != NULL) {
2275 *pvwv_offset = vwv_offset;
2277 if (pnum_bytes != NULL) {
2278 *pnum_bytes = num_bytes;
2280 if (pbytes != NULL) {
2283 if (pbytes_offset != NULL) {
2284 *pbytes_offset = bytes_offset;
2286 if (pinbuf != NULL) {
2287 *pinbuf = state->inbuf;
2293 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2300 for (i=0; i<num_reqs; i++) {
2301 struct smbXcli_req_state *state;
2302 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2303 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2304 state->smb1.iov_count-2);
2305 wct_ofs = (wct_ofs + 3) & ~3;
2310 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2312 struct smbXcli_req_state *first_state =
2313 tevent_req_data(reqs[0],
2314 struct smbXcli_req_state);
2315 struct smbXcli_req_state *state;
2317 size_t chain_padding = 0;
2319 struct iovec *iov = NULL;
2320 struct iovec *this_iov;
2324 if (num_reqs == 1) {
2325 return smb1cli_req_writev_submit(reqs[0], first_state,
2326 first_state->smb1.iov,
2327 first_state->smb1.iov_count);
2331 for (i=0; i<num_reqs; i++) {
2332 if (!tevent_req_is_in_progress(reqs[i])) {
2333 return NT_STATUS_INTERNAL_ERROR;
2336 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2338 if (state->smb1.iov_count < 4) {
2339 return NT_STATUS_INVALID_PARAMETER_MIX;
2344 * The NBT and SMB header
2357 iovlen += state->smb1.iov_count - 2;
2360 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2362 return NT_STATUS_NO_MEMORY;
2365 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2366 first_state, reqs, sizeof(*reqs) * num_reqs);
2367 if (first_state->smb1.chained_requests == NULL) {
2369 return NT_STATUS_NO_MEMORY;
2372 wct_offset = HDR_WCT;
2375 for (i=0; i<num_reqs; i++) {
2376 size_t next_padding = 0;
2379 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2381 if (i < num_reqs-1) {
2382 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2383 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2385 TALLOC_FREE(first_state->smb1.chained_requests);
2386 return NT_STATUS_INVALID_PARAMETER_MIX;
2390 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2391 state->smb1.iov_count-2) + 1;
2392 if ((wct_offset % 4) != 0) {
2393 next_padding = 4 - (wct_offset % 4);
2395 wct_offset += next_padding;
2396 vwv = state->smb1.vwv;
2398 if (i < num_reqs-1) {
2399 struct smbXcli_req_state *next_state =
2400 tevent_req_data(reqs[i+1],
2401 struct smbXcli_req_state);
2402 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2404 SSVAL(vwv+1, 0, wct_offset);
2405 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2406 /* properly end the chain */
2407 SCVAL(vwv+0, 0, 0xff);
2408 SCVAL(vwv+0, 1, 0xff);
2414 * The NBT and SMB header
2416 this_iov[0] = state->smb1.iov[0];
2417 this_iov[1] = state->smb1.iov[1];
2421 * This one is a bit subtle. We have to add
2422 * chain_padding bytes between the requests, and we
2423 * have to also include the wct field of the
2424 * subsequent requests. We use the subsequent header
2425 * for the padding, it contains the wct field in its
2428 this_iov[0].iov_len = chain_padding+1;
2429 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2430 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2431 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2436 * copy the words and bytes
2438 memcpy(this_iov, state->smb1.iov+2,
2439 sizeof(struct iovec) * (state->smb1.iov_count-2));
2440 this_iov += state->smb1.iov_count - 2;
2441 chain_padding = next_padding;
2444 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2445 if (nbt_len > first_state->conn->smb1.max_xmit) {
2447 TALLOC_FREE(first_state->smb1.chained_requests);
2448 return NT_STATUS_INVALID_PARAMETER_MIX;
2451 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2452 if (!NT_STATUS_IS_OK(status)) {
2454 TALLOC_FREE(first_state->smb1.chained_requests);
2458 return NT_STATUS_OK;
2461 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2463 return ((tevent_queue_length(conn->outgoing) != 0)
2464 || (talloc_array_length(conn->pending) != 0));
2467 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2469 if (conn->protocol >= PROTOCOL_SMB2_02) {
2470 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2473 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2476 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2478 uint16_t credits = 1;
2480 if (conn->smb2.cur_credits == 0) {
2481 if (max_dyn_len != NULL) {
2487 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2488 credits = conn->smb2.cur_credits;
2491 if (max_dyn_len != NULL) {
2492 *max_dyn_len = credits * 65536;
2498 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2500 return conn->smb2.server.capabilities;
2503 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2505 return conn->smb2.server.security_mode;
2508 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2510 return conn->smb2.server.max_trans_size;
2513 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2515 return conn->smb2.server.max_read_size;
2518 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2520 return conn->smb2.server.max_write_size;
2523 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2524 uint16_t max_credits)
2526 conn->smb2.max_credits = max_credits;
2529 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2531 static bool smb2cli_req_cancel(struct tevent_req *req)
2533 struct smbXcli_req_state *state =
2534 tevent_req_data(req,
2535 struct smbXcli_req_state);
2536 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2537 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2538 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2539 struct smbXcli_tcon *tcon = state->tcon;
2540 struct smbXcli_session *session = state->session;
2541 uint8_t *fixed = state->smb2.pad;
2542 uint16_t fixed_len = 4;
2543 struct tevent_req *subreq;
2544 struct smbXcli_req_state *substate;
2547 SSVAL(fixed, 0, 0x04);
2550 subreq = smb2cli_req_create(state, state->ev,
2558 if (subreq == NULL) {
2561 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2564 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2565 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2567 flags &= SMB2_HDR_FLAG_ASYNC;
2569 if (flags & SMB2_HDR_FLAG_ASYNC) {
2573 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2574 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2575 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2577 status = smb2cli_req_compound_submit(&subreq, 1);
2578 if (!NT_STATUS_IS_OK(status)) {
2579 TALLOC_FREE(subreq);
2583 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2588 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2590 /* we do not care about the result */
2591 TALLOC_FREE(subreq);
2594 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2595 struct tevent_context *ev,
2596 struct smbXcli_conn *conn,
2598 uint32_t additional_flags,
2599 uint32_t clear_flags,
2600 uint32_t timeout_msec,
2601 struct smbXcli_tcon *tcon,
2602 struct smbXcli_session *session,
2603 const uint8_t *fixed,
2607 uint32_t max_dyn_len)
2609 struct tevent_req *req;
2610 struct smbXcli_req_state *state;
2614 bool use_channel_sequence = false;
2615 uint16_t channel_sequence = 0;
2617 req = tevent_req_create(mem_ctx, &state,
2618 struct smbXcli_req_state);
2625 state->session = session;
2628 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2629 use_channel_sequence = true;
2630 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2631 use_channel_sequence = true;
2635 uid = session->smb2->session_id;
2637 if (use_channel_sequence) {
2638 channel_sequence = session->smb2->channel_sequence;
2641 state->smb2.should_sign = session->smb2->should_sign;
2642 state->smb2.should_encrypt = session->smb2->should_encrypt;
2644 if (cmd == SMB2_OP_SESSSETUP &&
2645 session->smb2->signing_key.length != 0) {
2646 state->smb2.should_sign = true;
2649 if (cmd == SMB2_OP_SESSSETUP &&
2650 session->smb2_channel.signing_key.length == 0) {
2651 state->smb2.should_encrypt = false;
2656 tid = tcon->smb2.tcon_id;
2658 if (tcon->smb2.should_encrypt) {
2659 state->smb2.should_encrypt = true;
2663 if (state->smb2.should_encrypt) {
2664 state->smb2.should_sign = false;
2667 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2668 if (state->smb2.recv_iov == NULL) {
2673 flags |= additional_flags;
2674 flags &= ~clear_flags;
2676 state->smb2.fixed = fixed;
2677 state->smb2.fixed_len = fixed_len;
2678 state->smb2.dyn = dyn;
2679 state->smb2.dyn_len = dyn_len;
2680 state->smb2.max_dyn_len = max_dyn_len;
2682 if (state->smb2.should_encrypt) {
2683 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2684 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2687 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2688 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2689 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2690 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2691 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2692 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2693 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2694 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2697 case SMB2_OP_CANCEL:
2698 state->one_way = true;
2702 * If this is a dummy request, it will have
2703 * UINT64_MAX as message id.
2704 * If we send on break acknowledgement,
2705 * this gets overwritten later.
2707 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2711 if (timeout_msec > 0) {
2712 struct timeval endtime;
2714 endtime = timeval_current_ofs_msec(timeout_msec);
2715 if (!tevent_req_set_endtime(req, ev, endtime)) {
2723 void smb2cli_req_set_notify_async(struct tevent_req *req)
2725 struct smbXcli_req_state *state =
2726 tevent_req_data(req,
2727 struct smbXcli_req_state);
2729 state->smb2.notify_async = true;
2732 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2733 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2734 TALLOC_CTX *tmp_mem,
2737 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2740 struct smbXcli_req_state *state;
2741 struct tevent_req *subreq;
2743 int i, num_iov, nbt_len;
2745 const DATA_BLOB *encryption_key = NULL;
2746 uint64_t encryption_session_id = 0;
2749 * 1 for the nbt length, optional TRANSFORM
2750 * per request: HDR, fixed, dyn, padding
2751 * -1 because the last one does not need padding
2754 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2756 return NT_STATUS_NO_MEMORY;
2763 * the session of the first request that requires encryption
2764 * specifies the encryption key.
2766 for (i=0; i<num_reqs; i++) {
2767 if (!tevent_req_is_in_progress(reqs[i])) {
2768 return NT_STATUS_INTERNAL_ERROR;
2771 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2773 if (!smbXcli_conn_is_connected(state->conn)) {
2774 return NT_STATUS_CONNECTION_DISCONNECTED;
2777 if ((state->conn->protocol != PROTOCOL_NONE) &&
2778 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2779 return NT_STATUS_REVISION_MISMATCH;
2782 if (state->session == NULL) {
2786 if (!state->smb2.should_encrypt) {
2790 encryption_key = &state->session->smb2->encryption_key;
2791 if (encryption_key->length == 0) {
2792 return NT_STATUS_INVALID_PARAMETER_MIX;
2795 encryption_session_id = state->session->smb2->session_id;
2798 iov[num_iov].iov_base = state->smb2.transform;
2799 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2802 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2803 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2804 state->session->smb2->nonce_low);
2805 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2806 state->session->smb2->nonce_high);
2807 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2808 encryption_session_id);
2810 state->session->smb2->nonce_low += 1;
2811 if (state->session->smb2->nonce_low == 0) {
2812 state->session->smb2->nonce_high += 1;
2813 state->session->smb2->nonce_low += 1;
2816 nbt_len += SMB2_TF_HDR_SIZE;
2820 for (i=0; i<num_reqs; i++) {
2829 const DATA_BLOB *signing_key = NULL;
2831 if (!tevent_req_is_in_progress(reqs[i])) {
2832 return NT_STATUS_INTERNAL_ERROR;
2835 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2837 if (!smbXcli_conn_is_connected(state->conn)) {
2838 return NT_STATUS_CONNECTION_DISCONNECTED;
2841 if ((state->conn->protocol != PROTOCOL_NONE) &&
2842 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2843 return NT_STATUS_REVISION_MISMATCH;
2846 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2847 if (opcode == SMB2_OP_CANCEL) {
2851 avail = UINT64_MAX - state->conn->smb2.mid;
2853 return NT_STATUS_CONNECTION_ABORTED;
2856 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2857 uint32_t max_dyn_len = 1;
2859 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2860 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2862 charge = (max_dyn_len - 1)/ 65536 + 1;
2867 charge = MAX(state->smb2.credit_charge, charge);
2869 avail = MIN(avail, state->conn->smb2.cur_credits);
2870 if (avail < charge) {
2871 return NT_STATUS_INTERNAL_ERROR;
2875 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2876 credits = state->conn->smb2.max_credits -
2877 state->conn->smb2.cur_credits;
2879 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2883 mid = state->conn->smb2.mid;
2884 state->conn->smb2.mid += charge;
2885 state->conn->smb2.cur_credits -= charge;
2887 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2888 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2890 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2891 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2894 if (state->session && encryption_key == NULL) {
2896 * We prefer the channel signing key if it is
2899 if (state->smb2.should_sign) {
2900 signing_key = &state->session->smb2_channel.signing_key;
2904 * If it is a channel binding, we already have the main
2905 * signing key and try that one.
2907 if (signing_key && signing_key->length == 0) {
2908 signing_key = &state->session->smb2->signing_key;
2912 * If we do not have any session key yet, we skip the
2913 * signing of SMB2_OP_SESSSETUP requests.
2915 if (signing_key && signing_key->length == 0) {
2921 iov[num_iov].iov_base = state->smb2.hdr;
2922 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2925 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2926 iov[num_iov].iov_len = state->smb2.fixed_len;
2929 if (state->smb2.dyn != NULL) {
2930 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2931 iov[num_iov].iov_len = state->smb2.dyn_len;
2935 reqlen = sizeof(state->smb2.hdr);
2936 reqlen += state->smb2.fixed_len;
2937 reqlen += state->smb2.dyn_len;
2939 if (i < num_reqs-1) {
2940 if ((reqlen % 8) > 0) {
2941 uint8_t pad = 8 - (reqlen % 8);
2942 iov[num_iov].iov_base = state->smb2.pad;
2943 iov[num_iov].iov_len = pad;
2947 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2950 state->smb2.encryption_session_id = encryption_session_id;
2952 if (signing_key != NULL) {
2955 status = smb2_signing_sign_pdu(*signing_key,
2956 state->session->conn->protocol,
2957 &iov[hdr_iov], num_iov - hdr_iov);
2958 if (!NT_STATUS_IS_OK(status)) {
2965 ret = smbXcli_req_set_pending(reqs[i]);
2967 return NT_STATUS_NO_MEMORY;
2971 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2972 _smb_setlen_tcp(state->length_hdr, nbt_len);
2973 iov[0].iov_base = state->length_hdr;
2974 iov[0].iov_len = sizeof(state->length_hdr);
2976 if (encryption_key != NULL) {
2978 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2982 buf = talloc_array(iov, uint8_t, buflen);
2984 return NT_STATUS_NO_MEMORY;
2988 * We copy the buffers before encrypting them,
2989 * this is at least currently needed for the
2990 * to keep state->smb2.hdr.
2992 * Also the callers may expect there buffers
2995 for (vi = tf_iov + 1; vi < num_iov; vi++) {
2996 struct iovec *v = &iov[vi];
2997 const uint8_t *o = (const uint8_t *)v->iov_base;
2999 memcpy(buf, o, v->iov_len);
3000 v->iov_base = (void *)buf;
3004 status = smb2_signing_encrypt_pdu(*encryption_key,
3005 state->conn->protocol,
3006 &iov[tf_iov], num_iov - tf_iov);
3007 if (!NT_STATUS_IS_OK(status)) {
3012 if (state->conn->dispatch_incoming == NULL) {
3013 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3016 subreq = writev_send(state, state->ev, state->conn->outgoing,
3017 state->conn->write_fd, false, iov, num_iov);
3018 if (subreq == NULL) {
3019 return NT_STATUS_NO_MEMORY;
3021 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3022 return NT_STATUS_OK;
3025 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3027 struct smbXcli_req_state *state =
3028 tevent_req_data(req,
3029 struct smbXcli_req_state);
3031 state->smb2.credit_charge = charge;
3034 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3035 struct tevent_context *ev,
3036 struct smbXcli_conn *conn,
3038 uint32_t additional_flags,
3039 uint32_t clear_flags,
3040 uint32_t timeout_msec,
3041 struct smbXcli_tcon *tcon,
3042 struct smbXcli_session *session,
3043 const uint8_t *fixed,
3047 uint32_t max_dyn_len)
3049 struct tevent_req *req;
3052 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3053 additional_flags, clear_flags,
3062 if (!tevent_req_is_in_progress(req)) {
3063 return tevent_req_post(req, ev);
3065 status = smb2cli_req_compound_submit(&req, 1);
3066 if (tevent_req_nterror(req, status)) {
3067 return tevent_req_post(req, ev);
3072 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3074 struct tevent_req *req =
3075 tevent_req_callback_data(subreq,
3077 struct smbXcli_req_state *state =
3078 tevent_req_data(req,
3079 struct smbXcli_req_state);
3083 nwritten = writev_recv(subreq, &err);
3084 TALLOC_FREE(subreq);
3085 if (nwritten == -1) {
3086 /* here, we need to notify all pending requests */
3087 NTSTATUS status = map_nt_error_from_unix_common(err);
3088 smbXcli_conn_disconnect(state->conn, status);
3093 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3096 TALLOC_CTX *mem_ctx,
3097 struct iovec **piov, int *pnum_iov)
3102 uint8_t *first_hdr = buf;
3103 size_t verified_buflen = 0;
3107 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3109 return NT_STATUS_NO_MEMORY;
3112 while (taken < buflen) {
3113 size_t len = buflen - taken;
3114 uint8_t *hdr = first_hdr + taken;
3117 size_t next_command_ofs;
3119 struct iovec *iov_tmp;
3121 if (verified_buflen > taken) {
3122 len = verified_buflen - taken;
3129 DEBUG(10, ("%d bytes left, expected at least %d\n",
3133 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3134 struct smbXcli_session *s;
3136 struct iovec tf_iov[2];
3140 if (len < SMB2_TF_HDR_SIZE) {
3141 DEBUG(10, ("%d bytes left, expected at least %d\n",
3142 (int)len, SMB2_TF_HDR_SIZE));
3146 tf_len = SMB2_TF_HDR_SIZE;
3149 hdr = first_hdr + taken;
3150 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3151 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3153 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3154 DEBUG(10, ("%d bytes left, expected at least %d\n",
3156 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3161 for (; s; s = s->next) {
3162 if (s->smb2->session_id != uid) {
3169 DEBUG(10, ("unknown session_id %llu\n",
3170 (unsigned long long)uid));
3174 tf_iov[0].iov_base = (void *)tf;
3175 tf_iov[0].iov_len = tf_len;
3176 tf_iov[1].iov_base = (void *)hdr;
3177 tf_iov[1].iov_len = enc_len;
3179 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3182 if (!NT_STATUS_IS_OK(status)) {
3187 verified_buflen = taken + enc_len;
3192 * We need the header plus the body length field
3195 if (len < SMB2_HDR_BODY + 2) {
3196 DEBUG(10, ("%d bytes left, expected at least %d\n",
3197 (int)len, SMB2_HDR_BODY));
3200 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3201 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3205 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3206 DEBUG(10, ("Got HDR len %d, expected %d\n",
3207 SVAL(hdr, 4), SMB2_HDR_BODY));
3212 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3213 body_size = SVAL(hdr, SMB2_HDR_BODY);
3215 if (next_command_ofs != 0) {
3216 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3219 if (next_command_ofs > full_size) {
3222 full_size = next_command_ofs;
3224 if (body_size < 2) {
3227 body_size &= 0xfffe;
3229 if (body_size > (full_size - SMB2_HDR_BODY)) {
3233 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3235 if (iov_tmp == NULL) {
3237 return NT_STATUS_NO_MEMORY;
3240 cur = &iov[num_iov];
3243 cur[0].iov_base = tf;
3244 cur[0].iov_len = tf_len;
3245 cur[1].iov_base = hdr;
3246 cur[1].iov_len = SMB2_HDR_BODY;
3247 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3248 cur[2].iov_len = body_size;
3249 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3250 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3256 *pnum_iov = num_iov;
3257 return NT_STATUS_OK;
3261 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3264 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3267 size_t num_pending = talloc_array_length(conn->pending);
3270 for (i=0; i<num_pending; i++) {
3271 struct tevent_req *req = conn->pending[i];
3272 struct smbXcli_req_state *state =
3273 tevent_req_data(req,
3274 struct smbXcli_req_state);
3276 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3283 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3284 TALLOC_CTX *tmp_mem,
3287 struct tevent_req *req;
3288 struct smbXcli_req_state *state = NULL;
3293 struct smbXcli_session *last_session = NULL;
3294 size_t inbuf_len = smb_len_tcp(inbuf);
3296 status = smb2cli_inbuf_parse_compound(conn,
3297 inbuf + NBT_HDR_SIZE,
3301 if (!NT_STATUS_IS_OK(status)) {
3305 for (i=0; i<num_iov; i+=4) {
3306 uint8_t *inbuf_ref = NULL;
3307 struct iovec *cur = &iov[i];
3308 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3309 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3310 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3311 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3312 uint16_t req_opcode;
3314 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3315 uint32_t new_credits;
3316 struct smbXcli_session *session = NULL;
3317 const DATA_BLOB *signing_key = NULL;
3318 bool was_encrypted = false;
3320 new_credits = conn->smb2.cur_credits;
3321 new_credits += credits;
3322 if (new_credits > UINT16_MAX) {
3323 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3325 conn->smb2.cur_credits += credits;
3327 req = smb2cli_conn_find_pending(conn, mid);
3329 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3331 state = tevent_req_data(req, struct smbXcli_req_state);
3333 state->smb2.got_async = false;
3335 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3336 if (opcode != req_opcode) {
3337 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3339 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3341 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3342 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3345 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3346 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3347 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3348 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3351 * async interim responses are not signed,
3352 * even if the SMB2_HDR_FLAG_SIGNED flag
3355 req_flags |= SMB2_HDR_FLAG_ASYNC;
3356 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3357 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3359 if (state->smb2.notify_async) {
3360 state->smb2.got_async = true;
3361 tevent_req_defer_callback(req, state->ev);
3362 tevent_req_notify_callback(req);
3367 session = state->session;
3368 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3369 session = last_session;
3371 last_session = session;
3373 if (state->smb2.should_sign) {
3374 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3375 return NT_STATUS_ACCESS_DENIED;
3379 if (flags & SMB2_HDR_FLAG_SIGNED) {
3380 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3382 if (session == NULL) {
3383 struct smbXcli_session *s;
3385 s = state->conn->sessions;
3386 for (; s; s = s->next) {
3387 if (s->smb2->session_id != uid) {
3396 if (session == NULL) {
3397 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3400 last_session = session;
3401 signing_key = &session->smb2_channel.signing_key;
3404 if (opcode == SMB2_OP_SESSSETUP) {
3406 * We prefer the channel signing key, if it is
3409 * If we do not have a channel signing key yet,
3410 * we try the main signing key, if it is not
3411 * the final response.
3413 if (signing_key && signing_key->length == 0 &&
3414 !NT_STATUS_IS_OK(status)) {
3415 signing_key = &session->smb2->signing_key;
3418 if (signing_key && signing_key->length == 0) {
3420 * If we do not have a session key to
3421 * verify the signature, we defer the
3422 * signing check to the caller.
3424 * The caller gets NT_STATUS_OK, it
3426 * smb2cli_session_set_session_key()
3428 * smb2cli_session_set_channel_key()
3429 * which will check the signature
3430 * with the channel signing key.
3436 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3437 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3438 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3441 * If the response was encrypted in a SMB2_TRANSFORM
3442 * pdu, which belongs to the correct session,
3443 * we do not need to do signing checks
3445 * It could be the session the response belongs to
3446 * or the session that was used to encrypt the
3447 * SMB2_TRANSFORM request.
3449 if ((session && session->smb2->session_id == uid) ||
3450 (state->smb2.encryption_session_id == uid)) {
3452 was_encrypted = true;
3456 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3458 * if the server returns NT_STATUS_USER_SESSION_DELETED
3459 * the response is not signed and we should
3460 * propagate the NT_STATUS_USER_SESSION_DELETED
3461 * status to the caller.
3463 state->smb2.signing_skipped = true;
3467 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3469 * if the server returns
3470 * NT_STATUS_INVALID_PARAMETER
3471 * the response might not be encrypted.
3473 if (state->smb2.should_encrypt && !was_encrypted) {
3474 state->smb2.signing_skipped = true;
3479 if (state->smb2.should_encrypt && !was_encrypted) {
3480 if (!state->smb2.signing_skipped) {
3481 return NT_STATUS_ACCESS_DENIED;
3485 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3486 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3487 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3489 * if the server returns
3490 * NT_STATUS_NETWORK_NAME_DELETED
3491 * NT_STATUS_FILE_CLOSED
3492 * NT_STATUS_INVALID_PARAMETER
3493 * the response might not be signed
3494 * as this happens before the signing checks.
3496 * If server echos the signature (or all zeros)
3497 * we should report the status from the server
3503 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3504 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3507 state->smb2.signing_skipped = true;
3513 static const uint8_t zeros[16];
3515 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3519 state->smb2.signing_skipped = true;
3526 status = smb2_signing_check_pdu(*signing_key,
3527 state->conn->protocol,
3529 if (!NT_STATUS_IS_OK(status)) {
3531 * If the signing check fails, we disconnect
3538 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3539 (session != NULL) && session->disconnect_expired)
3542 * this should be a short term hack
3543 * until the upper layers have implemented
3544 * re-authentication.
3549 smbXcli_req_unset_pending(req);
3552 * There might be more than one response
3553 * we need to defer the notifications
3555 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3560 tevent_req_defer_callback(req, state->ev);
3564 * Note: here we use talloc_reference() in a way
3565 * that does not expose it to the caller.
3567 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3568 if (tevent_req_nomem(inbuf_ref, req)) {
3572 /* copy the related buffers */
3573 state->smb2.recv_iov[0] = cur[1];
3574 state->smb2.recv_iov[1] = cur[2];
3575 state->smb2.recv_iov[2] = cur[3];
3577 tevent_req_done(req);
3581 return NT_STATUS_RETRY;
3584 return NT_STATUS_OK;
3587 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3588 struct iovec **piov,
3589 const struct smb2cli_req_expected_response *expected,
3590 size_t num_expected)
3592 struct smbXcli_req_state *state =
3593 tevent_req_data(req,
3594 struct smbXcli_req_state);
3597 bool found_status = false;
3598 bool found_size = false;
3605 if (state->smb2.got_async) {
3606 return STATUS_PENDING;
3609 if (tevent_req_is_nterror(req, &status)) {
3610 for (i=0; i < num_expected; i++) {
3611 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3612 found_status = true;
3618 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3624 if (num_expected == 0) {
3625 found_status = true;
3629 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3630 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3632 for (i=0; i < num_expected; i++) {
3633 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3637 found_status = true;
3638 if (expected[i].body_size == 0) {
3643 if (expected[i].body_size == body_size) {
3649 if (!found_status) {
3653 if (state->smb2.signing_skipped) {
3654 if (num_expected > 0) {
3655 return NT_STATUS_ACCESS_DENIED;
3657 if (!NT_STATUS_IS_ERR(status)) {
3658 return NT_STATUS_ACCESS_DENIED;
3663 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3667 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3673 static const struct {
3674 enum protocol_types proto;
3675 const char *smb1_name;
3676 } smb1cli_prots[] = {
3677 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3678 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3679 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3680 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3681 {PROTOCOL_LANMAN2, "LM1.2X002"},
3682 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3683 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3684 {PROTOCOL_LANMAN2, "Samba"},
3685 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3686 {PROTOCOL_NT1, "NT LM 0.12"},
3687 {PROTOCOL_SMB2_02, "SMB 2.002"},
3688 {PROTOCOL_SMB2_10, "SMB 2.???"},
3691 static const struct {
3692 enum protocol_types proto;
3693 uint16_t smb2_dialect;
3694 } smb2cli_prots[] = {
3695 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3696 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3697 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3698 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3699 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3700 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
3703 struct smbXcli_negprot_state {
3704 struct smbXcli_conn *conn;
3705 struct tevent_context *ev;
3706 uint32_t timeout_msec;
3707 enum protocol_types min_protocol;
3708 enum protocol_types max_protocol;
3712 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3716 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3717 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3718 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3719 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3720 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3721 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3725 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3726 struct tevent_context *ev,
3727 struct smbXcli_conn *conn,
3728 uint32_t timeout_msec,
3729 enum protocol_types min_protocol,
3730 enum protocol_types max_protocol)
3732 struct tevent_req *req, *subreq;
3733 struct smbXcli_negprot_state *state;
3735 req = tevent_req_create(mem_ctx, &state,
3736 struct smbXcli_negprot_state);
3742 state->timeout_msec = timeout_msec;
3743 state->min_protocol = min_protocol;
3744 state->max_protocol = max_protocol;
3746 if (min_protocol == PROTOCOL_NONE) {
3747 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3748 return tevent_req_post(req, ev);
3751 if (max_protocol == PROTOCOL_NONE) {
3752 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3753 return tevent_req_post(req, ev);
3756 if (min_protocol > max_protocol) {
3757 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3758 return tevent_req_post(req, ev);
3761 if ((min_protocol < PROTOCOL_SMB2_02) &&
3762 (max_protocol < PROTOCOL_SMB2_02)) {
3766 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3768 subreq = smbXcli_negprot_smb1_subreq(state);
3769 if (tevent_req_nomem(subreq, req)) {
3770 return tevent_req_post(req, ev);
3772 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3776 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3777 (max_protocol >= PROTOCOL_SMB2_02)) {
3781 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3783 subreq = smbXcli_negprot_smb2_subreq(state);
3784 if (tevent_req_nomem(subreq, req)) {
3785 return tevent_req_post(req, ev);
3787 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3792 * We send an SMB1 negprot with the SMB2 dialects
3793 * and expect a SMB1 or a SMB2 response.
3795 * smbXcli_negprot_dispatch_incoming() will fix the
3796 * callback to match protocol of the response.
3798 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3800 subreq = smbXcli_negprot_smb1_subreq(state);
3801 if (tevent_req_nomem(subreq, req)) {
3802 return tevent_req_post(req, ev);
3804 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3808 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3810 struct tevent_req *req =
3811 tevent_req_callback_data(subreq,
3816 * we just want the low level error
3818 status = tevent_req_simple_recv_ntstatus(subreq);
3819 TALLOC_FREE(subreq);
3820 if (tevent_req_nterror(req, status)) {
3824 /* this should never happen */
3825 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3828 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3831 DATA_BLOB bytes = data_blob_null;
3835 /* setup the protocol strings */
3836 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3840 if (smb1cli_prots[i].proto < state->min_protocol) {
3844 if (smb1cli_prots[i].proto > state->max_protocol) {
3848 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3854 * We now it is already ascii and
3855 * we want NULL termination.
3857 ok = data_blob_append(state, &bytes,
3858 smb1cli_prots[i].smb1_name,
3859 strlen(smb1cli_prots[i].smb1_name)+1);
3865 smb1cli_req_flags(state->max_protocol,
3866 state->conn->smb1.client.capabilities,
3871 return smb1cli_req_send(state, state->ev, state->conn,
3875 state->timeout_msec,
3876 0xFFFE, 0, NULL, /* pid, tid, session */
3877 0, NULL, /* wct, vwv */
3878 bytes.length, bytes.data);
3881 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3883 struct tevent_req *req =
3884 tevent_req_callback_data(subreq,
3886 struct smbXcli_negprot_state *state =
3887 tevent_req_data(req,
3888 struct smbXcli_negprot_state);
3889 struct smbXcli_conn *conn = state->conn;
3890 struct iovec *recv_iov = NULL;
3899 size_t num_prots = 0;
3901 uint32_t client_capabilities = conn->smb1.client.capabilities;
3902 uint32_t both_capabilities;
3903 uint32_t server_capabilities = 0;
3904 uint32_t capabilities;
3905 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3906 uint32_t server_max_xmit = 0;
3908 uint32_t server_max_mux = 0;
3909 uint16_t server_security_mode = 0;
3910 uint32_t server_session_key = 0;
3911 bool server_readbraw = false;
3912 bool server_writebraw = false;
3913 bool server_lockread = false;
3914 bool server_writeunlock = false;
3915 struct GUID server_guid = GUID_zero();
3916 DATA_BLOB server_gss_blob = data_blob_null;
3917 uint8_t server_challenge[8];
3918 char *server_workgroup = NULL;
3919 char *server_name = NULL;
3920 int server_time_zone = 0;
3921 NTTIME server_system_time = 0;
3922 static const struct smb1cli_req_expected_response expected[] = {
3924 .status = NT_STATUS_OK,
3925 .wct = 0x11, /* NT1 */
3928 .status = NT_STATUS_OK,
3929 .wct = 0x0D, /* LM */
3932 .status = NT_STATUS_OK,
3933 .wct = 0x01, /* CORE */
3937 ZERO_STRUCT(server_challenge);
3939 status = smb1cli_req_recv(subreq, state,
3944 NULL, /* pvwv_offset */
3947 NULL, /* pbytes_offset */
3949 expected, ARRAY_SIZE(expected));
3950 TALLOC_FREE(subreq);
3951 if (tevent_req_nterror(req, status)) {
3955 flags = CVAL(inhdr, HDR_FLG);
3957 protnum = SVAL(vwv, 0);
3959 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3960 if (smb1cli_prots[i].proto < state->min_protocol) {
3964 if (smb1cli_prots[i].proto > state->max_protocol) {
3968 if (protnum != num_prots) {
3973 conn->protocol = smb1cli_prots[i].proto;
3977 if (conn->protocol == PROTOCOL_NONE) {
3978 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3982 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3983 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3984 "and the selected protocol level doesn't support it.\n"));
3985 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3989 if (flags & FLAG_SUPPORT_LOCKREAD) {
3990 server_lockread = true;
3991 server_writeunlock = true;
3994 if (conn->protocol >= PROTOCOL_NT1) {
3995 const char *client_signing = NULL;
3996 bool server_mandatory = false;
3997 bool server_allowed = false;
3998 const char *server_signing = NULL;
4003 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4008 server_security_mode = CVAL(vwv + 1, 0);
4009 server_max_mux = SVAL(vwv + 1, 1);
4010 server_max_xmit = IVAL(vwv + 3, 1);
4011 server_session_key = IVAL(vwv + 7, 1);
4012 server_time_zone = SVALS(vwv + 15, 1);
4013 server_time_zone *= 60;
4014 /* this time arrives in real GMT */
4015 server_system_time = BVAL(vwv + 11, 1);
4016 server_capabilities = IVAL(vwv + 9, 1);
4018 key_len = CVAL(vwv + 16, 1);
4020 if (server_capabilities & CAP_RAW_MODE) {
4021 server_readbraw = true;
4022 server_writebraw = true;
4024 if (server_capabilities & CAP_LOCK_AND_READ) {
4025 server_lockread = true;
4028 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4029 DATA_BLOB blob1, blob2;
4031 if (num_bytes < 16) {
4032 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4036 blob1 = data_blob_const(bytes, 16);
4037 status = GUID_from_data_blob(&blob1, &server_guid);
4038 if (tevent_req_nterror(req, status)) {
4042 blob1 = data_blob_const(bytes+16, num_bytes-16);
4043 blob2 = data_blob_dup_talloc(state, blob1);
4044 if (blob1.length > 0 &&
4045 tevent_req_nomem(blob2.data, req)) {
4048 server_gss_blob = blob2;
4050 DATA_BLOB blob1, blob2;
4052 if (num_bytes < key_len) {
4053 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4057 if (key_len != 0 && key_len != 8) {
4058 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4063 memcpy(server_challenge, bytes, 8);
4066 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4067 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4068 if (blob1.length > 0) {
4071 len = utf16_len_n(blob1.data,
4075 ok = convert_string_talloc(state,
4083 status = map_nt_error_from_unix_common(errno);
4084 tevent_req_nterror(req, status);
4089 blob2.data += blob1.length;
4090 blob2.length -= blob1.length;
4091 if (blob2.length > 0) {
4094 len = utf16_len_n(blob1.data,
4098 ok = convert_string_talloc(state,
4106 status = map_nt_error_from_unix_common(errno);
4107 tevent_req_nterror(req, status);
4113 client_signing = "disabled";
4114 if (conn->allow_signing) {
4115 client_signing = "allowed";
4117 if (conn->mandatory_signing) {
4118 client_signing = "required";
4121 server_signing = "not supported";
4122 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4123 server_signing = "supported";
4124 server_allowed = true;
4125 } else if (conn->mandatory_signing) {
4127 * We have mandatory signing as client
4128 * lets assume the server will look at our
4129 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4130 * flag in the session setup
4132 server_signing = "not announced";
4133 server_allowed = true;
4135 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4136 server_signing = "required";
4137 server_mandatory = true;
4140 ok = smb_signing_set_negotiated(conn->smb1.signing,
4144 DEBUG(1,("cli_negprot: SMB signing is required, "
4145 "but client[%s] and server[%s] mismatch\n",
4146 client_signing, server_signing));
4147 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4151 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4157 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4161 server_security_mode = SVAL(vwv + 1, 0);
4162 server_max_xmit = SVAL(vwv + 2, 0);
4163 server_max_mux = SVAL(vwv + 3, 0);
4164 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4165 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4166 server_session_key = IVAL(vwv + 6, 0);
4167 server_time_zone = SVALS(vwv + 10, 0);
4168 server_time_zone *= 60;
4169 /* this time is converted to GMT by make_unix_date */
4170 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4171 unix_to_nt_time(&server_system_time, t);
4172 key_len = SVAL(vwv + 11, 0);
4174 if (num_bytes < key_len) {
4175 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4179 if (key_len != 0 && key_len != 8) {
4180 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4185 memcpy(server_challenge, bytes, 8);
4188 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4189 if (blob1.length > 0) {
4193 len = utf16_len_n(blob1.data,
4197 ok = convert_string_talloc(state,
4205 status = map_nt_error_from_unix_common(errno);
4206 tevent_req_nterror(req, status);
4212 /* the old core protocol */
4213 server_time_zone = get_time_zone(time(NULL));
4214 server_max_xmit = 1024;
4218 if (server_max_xmit < 1024) {
4219 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4223 if (server_max_mux < 1) {
4224 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4229 * Now calculate the negotiated capabilities
4230 * based on the mask for:
4231 * - client only flags
4232 * - flags used in both directions
4233 * - server only flags
4235 both_capabilities = client_capabilities & server_capabilities;
4236 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4237 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4238 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4240 max_xmit = MIN(client_max_xmit, server_max_xmit);
4242 conn->smb1.server.capabilities = server_capabilities;
4243 conn->smb1.capabilities = capabilities;
4245 conn->smb1.server.max_xmit = server_max_xmit;
4246 conn->smb1.max_xmit = max_xmit;
4248 conn->smb1.server.max_mux = server_max_mux;
4250 conn->smb1.server.security_mode = server_security_mode;
4252 conn->smb1.server.readbraw = server_readbraw;
4253 conn->smb1.server.writebraw = server_writebraw;
4254 conn->smb1.server.lockread = server_lockread;
4255 conn->smb1.server.writeunlock = server_writeunlock;
4257 conn->smb1.server.session_key = server_session_key;
4259 talloc_steal(conn, server_gss_blob.data);
4260 conn->smb1.server.gss_blob = server_gss_blob;
4261 conn->smb1.server.guid = server_guid;
4262 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4263 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4264 conn->smb1.server.name = talloc_move(conn, &server_name);
4266 conn->smb1.server.time_zone = server_time_zone;
4267 conn->smb1.server.system_time = server_system_time;
4269 tevent_req_done(req);
4272 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4276 uint16_t dialect_count = 0;
4278 buf = state->smb2.dyn;
4279 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4280 if (smb2cli_prots[i].proto < state->min_protocol) {
4284 if (smb2cli_prots[i].proto > state->max_protocol) {
4288 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4292 buf = state->smb2.fixed;
4294 SSVAL(buf, 2, dialect_count);
4295 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4296 SSVAL(buf, 6, 0); /* Reserved */
4297 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4298 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4300 SIVAL(buf, 8, 0); /* Capabilities */
4302 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4306 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4308 if (!NT_STATUS_IS_OK(status)) {
4311 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4313 memset(buf+12, 0, 16); /* ClientGuid */
4315 SBVAL(buf, 28, 0); /* ClientStartTime */
4317 return smb2cli_req_send(state, state->ev,
4318 state->conn, SMB2_OP_NEGPROT,
4320 state->timeout_msec,
4321 NULL, NULL, /* tcon, session */
4322 state->smb2.fixed, sizeof(state->smb2.fixed),
4323 state->smb2.dyn, dialect_count*2,
4324 UINT16_MAX); /* max_dyn_len */
4327 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4329 struct tevent_req *req =
4330 tevent_req_callback_data(subreq,
4332 struct smbXcli_negprot_state *state =
4333 tevent_req_data(req,
4334 struct smbXcli_negprot_state);
4335 struct smbXcli_conn *conn = state->conn;
4336 size_t security_offset, security_length;
4342 uint16_t dialect_revision;
4343 static const struct smb2cli_req_expected_response expected[] = {
4345 .status = NT_STATUS_OK,
4350 status = smb2cli_req_recv(subreq, state, &iov,
4351 expected, ARRAY_SIZE(expected));
4352 TALLOC_FREE(subreq);
4353 if (tevent_req_nterror(req, status)) {
4357 body = (uint8_t *)iov[1].iov_base;
4359 dialect_revision = SVAL(body, 4);
4361 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4362 if (smb2cli_prots[i].proto < state->min_protocol) {
4366 if (smb2cli_prots[i].proto > state->max_protocol) {
4370 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4374 conn->protocol = smb2cli_prots[i].proto;
4378 if (conn->protocol == PROTOCOL_NONE) {
4379 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4380 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4384 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4385 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4389 /* make sure we do not loop forever */
4390 state->min_protocol = PROTOCOL_SMB2_02;
4393 * send a SMB2 negprot, in order to negotiate
4396 subreq = smbXcli_negprot_smb2_subreq(state);
4397 if (tevent_req_nomem(subreq, req)) {
4400 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4404 conn->smb2.server.security_mode = SVAL(body, 2);
4406 blob = data_blob_const(body + 8, 16);
4407 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4408 if (tevent_req_nterror(req, status)) {
4412 conn->smb2.server.capabilities = IVAL(body, 24);
4413 conn->smb2.server.max_trans_size= IVAL(body, 28);
4414 conn->smb2.server.max_read_size = IVAL(body, 32);
4415 conn->smb2.server.max_write_size= IVAL(body, 36);
4416 conn->smb2.server.system_time = BVAL(body, 40);
4417 conn->smb2.server.start_time = BVAL(body, 48);
4419 security_offset = SVAL(body, 56);
4420 security_length = SVAL(body, 58);
4422 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4423 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4427 if (security_length > iov[2].iov_len) {
4428 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4432 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4435 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4439 tevent_req_done(req);
4442 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4443 TALLOC_CTX *tmp_mem,
4446 size_t num_pending = talloc_array_length(conn->pending);
4447 struct tevent_req *subreq;
4448 struct smbXcli_req_state *substate;
4449 struct tevent_req *req;
4450 uint32_t protocol_magic;
4451 size_t inbuf_len = smb_len_nbt(inbuf);
4453 if (num_pending != 1) {
4454 return NT_STATUS_INTERNAL_ERROR;
4457 if (inbuf_len < 4) {
4458 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4461 subreq = conn->pending[0];
4462 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4463 req = tevent_req_callback_data(subreq, struct tevent_req);
4465 protocol_magic = IVAL(inbuf, 4);
4467 switch (protocol_magic) {
4469 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4470 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4471 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4474 if (substate->smb2.recv_iov == NULL) {
4476 * For the SMB1 negprot we have move it.
4478 substate->smb2.recv_iov = substate->smb1.recv_iov;
4479 substate->smb1.recv_iov = NULL;
4483 * we got an SMB2 answer, which consumed sequence number 0
4484 * so we need to use 1 as the next one.
4486 * we also need to set the current credits to 0
4487 * as we consumed the initial one. The SMB2 answer
4488 * hopefully grant us a new credit.
4491 conn->smb2.cur_credits = 0;
4492 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4493 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4494 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4497 DEBUG(10, ("Got non-SMB PDU\n"));
4498 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4501 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4503 return tevent_req_simple_recv_ntstatus(req);
4506 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4507 uint32_t timeout_msec,
4508 enum protocol_types min_protocol,
4509 enum protocol_types max_protocol)
4511 TALLOC_CTX *frame = talloc_stackframe();
4512 struct tevent_context *ev;
4513 struct tevent_req *req;
4514 NTSTATUS status = NT_STATUS_NO_MEMORY;
4517 if (smbXcli_conn_has_async_calls(conn)) {
4519 * Can't use sync call while an async call is in flight
4521 status = NT_STATUS_INVALID_PARAMETER_MIX;
4524 ev = samba_tevent_context_init(frame);
4528 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4529 min_protocol, max_protocol);
4533 ok = tevent_req_poll(req, ev);
4535 status = map_nt_error_from_unix_common(errno);
4538 status = smbXcli_negprot_recv(req);
4544 static int smbXcli_session_destructor(struct smbXcli_session *session)
4546 if (session->conn == NULL) {
4550 DLIST_REMOVE(session->conn->sessions, session);
4554 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4555 struct smbXcli_conn *conn)
4557 struct smbXcli_session *session;
4559 session = talloc_zero(mem_ctx, struct smbXcli_session);
4560 if (session == NULL) {
4563 session->smb2 = talloc_zero(session, struct smb2cli_session);
4564 if (session->smb2 == NULL) {
4565 talloc_free(session);
4568 talloc_set_destructor(session, smbXcli_session_destructor);
4570 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4571 session->conn = conn;
4576 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4577 struct smbXcli_session *src)
4579 struct smbXcli_session *session;
4581 session = talloc_zero(mem_ctx, struct smbXcli_session);
4582 if (session == NULL) {
4585 session->smb2 = talloc_zero(session, struct smb2cli_session);
4586 if (session->smb2 == NULL) {
4587 talloc_free(session);
4591 session->conn = src->conn;
4592 *session->smb2 = *src->smb2;
4593 session->smb2_channel = src->smb2_channel;
4594 session->disconnect_expired = src->disconnect_expired;
4596 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4597 talloc_set_destructor(session, smbXcli_session_destructor);
4603 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4604 TALLOC_CTX *mem_ctx,
4607 const DATA_BLOB *application_key;
4609 *key = data_blob_null;
4611 if (session->conn == NULL) {
4612 return NT_STATUS_NO_USER_SESSION_KEY;
4615 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4616 application_key = &session->smb2->application_key;
4618 application_key = &session->smb1.application_key;
4621 if (application_key->length == 0) {
4622 return NT_STATUS_NO_USER_SESSION_KEY;
4625 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4626 if (key->data == NULL) {
4627 return NT_STATUS_NO_MEMORY;
4630 return NT_STATUS_OK;
4633 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4635 session->disconnect_expired = true;
4638 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4640 return session->smb1.session_id;
4643 void smb1cli_session_set_id(struct smbXcli_session *session,
4644 uint16_t session_id)
4646 session->smb1.session_id = session_id;
4649 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4650 const DATA_BLOB _session_key)
4652 struct smbXcli_conn *conn = session->conn;
4653 uint8_t session_key[16];
4656 return NT_STATUS_INVALID_PARAMETER_MIX;
4659 if (session->smb1.application_key.length != 0) {
4661 * TODO: do not allow this...
4663 * return NT_STATUS_INVALID_PARAMETER_MIX;
4665 data_blob_clear_free(&session->smb1.application_key);
4666 session->smb1.protected_key = false;
4669 if (_session_key.length == 0) {
4670 return NT_STATUS_OK;
4673 ZERO_STRUCT(session_key);
4674 memcpy(session_key, _session_key.data,
4675 MIN(_session_key.length, sizeof(session_key)));
4677 session->smb1.application_key = data_blob_talloc(session,
4679 sizeof(session_key));
4680 ZERO_STRUCT(session_key);
4681 if (session->smb1.application_key.data == NULL) {
4682 return NT_STATUS_NO_MEMORY;
4685 session->smb1.protected_key = false;
4687 return NT_STATUS_OK;
4690 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4692 if (session->smb1.protected_key) {
4693 /* already protected */
4694 return NT_STATUS_OK;
4697 if (session->smb1.application_key.length != 16) {
4698 return NT_STATUS_INVALID_PARAMETER_MIX;
4701 smb_key_derivation(session->smb1.application_key.data,
4702 session->smb1.application_key.length,
4703 session->smb1.application_key.data);
4705 session->smb1.protected_key = true;
4707 return NT_STATUS_OK;
4710 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4712 struct smbXcli_conn *conn = session->conn;
4713 uint8_t security_mode = 0;
4716 return security_mode;
4719 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4720 if (conn->mandatory_signing) {
4721 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4724 return security_mode;
4727 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4729 return session->smb2->session_id;
4732 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4734 return session->smb2->session_flags;
4737 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4738 uint64_t session_id,
4739 uint16_t session_flags)
4741 session->smb2->session_id = session_id;
4742 session->smb2->session_flags = session_flags;
4745 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4747 session->smb2->channel_sequence += 1;
4750 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4751 const DATA_BLOB _session_key,
4752 const struct iovec *recv_iov)
4754 struct smbXcli_conn *conn = session->conn;
4755 uint16_t no_sign_flags;
4756 uint8_t session_key[16];
4757 bool check_signature = true;
4762 return NT_STATUS_INVALID_PARAMETER_MIX;
4765 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
4766 return NT_STATUS_INVALID_PARAMETER_MIX;
4769 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4771 if (session->smb2->session_flags & no_sign_flags) {
4772 session->smb2->should_sign = false;
4773 return NT_STATUS_OK;
4776 if (session->smb2->signing_key.length != 0) {
4777 return NT_STATUS_INVALID_PARAMETER_MIX;
4780 ZERO_STRUCT(session_key);
4781 memcpy(session_key, _session_key.data,
4782 MIN(_session_key.length, sizeof(session_key)));
4784 session->smb2->signing_key = data_blob_talloc(session,
4786 sizeof(session_key));
4787 if (session->smb2->signing_key.data == NULL) {
4788 ZERO_STRUCT(session_key);
4789 return NT_STATUS_NO_MEMORY;
4792 if (conn->protocol >= PROTOCOL_SMB2_24) {
4793 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4794 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4796 smb2_key_derivation(session_key, sizeof(session_key),
4797 label.data, label.length,
4798 context.data, context.length,
4799 session->smb2->signing_key.data);
4802 session->smb2->encryption_key = data_blob_dup_talloc(session,
4803 session->smb2->signing_key);
4804 if (session->smb2->encryption_key.data == NULL) {
4805 ZERO_STRUCT(session_key);
4806 return NT_STATUS_NO_MEMORY;
4809 if (conn->protocol >= PROTOCOL_SMB2_24) {
4810 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4811 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4813 smb2_key_derivation(session_key, sizeof(session_key),
4814 label.data, label.length,
4815 context.data, context.length,
4816 session->smb2->encryption_key.data);
4819 session->smb2->decryption_key = data_blob_dup_talloc(session,
4820 session->smb2->signing_key);
4821 if (session->smb2->decryption_key.data == NULL) {
4822 ZERO_STRUCT(session_key);
4823 return NT_STATUS_NO_MEMORY;
4826 if (conn->protocol >= PROTOCOL_SMB2_24) {
4827 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4828 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4830 smb2_key_derivation(session_key, sizeof(session_key),
4831 label.data, label.length,
4832 context.data, context.length,
4833 session->smb2->decryption_key.data);
4836 session->smb2->application_key = data_blob_dup_talloc(session,
4837 session->smb2->signing_key);
4838 if (session->smb2->application_key.data == NULL) {
4839 ZERO_STRUCT(session_key);
4840 return NT_STATUS_NO_MEMORY;
4843 if (conn->protocol >= PROTOCOL_SMB2_24) {
4844 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4845 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4847 smb2_key_derivation(session_key, sizeof(session_key),
4848 label.data, label.length,
4849 context.data, context.length,
4850 session->smb2->application_key.data);
4852 ZERO_STRUCT(session_key);
4854 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4855 session->smb2->signing_key);
4856 if (session->smb2_channel.signing_key.data == NULL) {
4857 return NT_STATUS_NO_MEMORY;
4860 check_signature = conn->mandatory_signing;
4862 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
4863 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
4865 * Sadly some vendors don't sign the
4866 * final SMB2 session setup response
4868 * At least Windows and Samba are always doing this
4869 * if there's a session key available.
4871 * We only check the signature if it's mandatory
4872 * or SMB2_HDR_FLAG_SIGNED is provided.
4874 check_signature = true;
4877 if (check_signature) {
4878 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4879 session->conn->protocol,
4881 if (!NT_STATUS_IS_OK(status)) {
4886 session->smb2->should_sign = false;
4887 session->smb2->should_encrypt = false;
4889 if (conn->desire_signing) {
4890 session->smb2->should_sign = true;
4893 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4894 session->smb2->should_sign = true;
4897 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4898 session->smb2->should_encrypt = true;
4901 if (conn->protocol < PROTOCOL_SMB2_24) {
4902 session->smb2->should_encrypt = false;
4905 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4906 session->smb2->should_encrypt = false;
4909 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4910 sizeof(session->smb2->nonce_high));
4911 session->smb2->nonce_low = 1;
4913 return NT_STATUS_OK;
4916 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4917 struct smbXcli_session *session1,
4918 struct smbXcli_conn *conn,
4919 struct smbXcli_session **_session2)
4921 struct smbXcli_session *session2;
4923 if (session1->smb2->signing_key.length == 0) {
4924 return NT_STATUS_INVALID_PARAMETER_MIX;
4928 return NT_STATUS_INVALID_PARAMETER_MIX;
4931 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4932 if (session2 == NULL) {
4933 return NT_STATUS_NO_MEMORY;
4935 session2->smb2 = talloc_reference(session2, session1->smb2);
4936 if (session2->smb2 == NULL) {
4937 talloc_free(session2);
4938 return NT_STATUS_NO_MEMORY;
4941 talloc_set_destructor(session2, smbXcli_session_destructor);
4942 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4943 session2->conn = conn;
4945 *_session2 = session2;
4946 return NT_STATUS_OK;
4949 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4950 const DATA_BLOB _channel_key,
4951 const struct iovec *recv_iov)
4953 struct smbXcli_conn *conn = session->conn;
4954 uint8_t channel_key[16];
4958 return NT_STATUS_INVALID_PARAMETER_MIX;
4961 if (session->smb2_channel.signing_key.length != 0) {
4962 return NT_STATUS_INVALID_PARAMETER_MIX;
4965 ZERO_STRUCT(channel_key);
4966 memcpy(channel_key, _channel_key.data,
4967 MIN(_channel_key.length, sizeof(channel_key)));
4969 session->smb2_channel.signing_key = data_blob_talloc(session,
4971 sizeof(channel_key));
4972 if (session->smb2_channel.signing_key.data == NULL) {
4973 ZERO_STRUCT(channel_key);
4974 return NT_STATUS_NO_MEMORY;
4977 if (conn->protocol >= PROTOCOL_SMB2_24) {
4978 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4979 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4981 smb2_key_derivation(channel_key, sizeof(channel_key),
4982 label.data, label.length,
4983 context.data, context.length,
4984 session->smb2_channel.signing_key.data);
4986 ZERO_STRUCT(channel_key);
4988 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4989 session->conn->protocol,
4991 if (!NT_STATUS_IS_OK(status)) {
4995 return NT_STATUS_OK;
4998 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
5000 if (session->smb2->should_encrypt) {
5001 return NT_STATUS_OK;
5004 if (session->conn->protocol < PROTOCOL_SMB2_24) {
5005 return NT_STATUS_NOT_SUPPORTED;
5008 if (!(session->conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
5009 return NT_STATUS_NOT_SUPPORTED;
5012 if (session->smb2->signing_key.data == NULL) {
5013 return NT_STATUS_NOT_SUPPORTED;
5015 session->smb2->should_encrypt = true;
5016 return NT_STATUS_OK;
5019 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
5021 struct smbXcli_tcon *tcon;
5023 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
5031 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
5032 uint32_t fs_attributes)
5034 tcon->fs_attributes = fs_attributes;
5037 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
5039 return tcon->fs_attributes;
5042 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
5048 if (tcon->is_smb1) {
5049 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
5056 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
5063 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
5065 return tcon->smb1.tcon_id;
5068 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
5070 tcon->is_smb1 = true;
5071 tcon->smb1.tcon_id = tcon_id;
5074 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5076 uint16_t optional_support,
5077 uint32_t maximal_access,
5078 uint32_t guest_maximal_access,
5079 const char *service,
5080 const char *fs_type)
5082 tcon->is_smb1 = true;
5083 tcon->fs_attributes = 0;
5084 tcon->smb1.tcon_id = tcon_id;
5085 tcon->smb1.optional_support = optional_support;
5086 tcon->smb1.maximal_access = maximal_access;
5087 tcon->smb1.guest_maximal_access = guest_maximal_access;
5089 TALLOC_FREE(tcon->smb1.service);
5090 tcon->smb1.service = talloc_strdup(tcon, service);
5091 if (service != NULL && tcon->smb1.service == NULL) {
5095 TALLOC_FREE(tcon->smb1.fs_type);
5096 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5097 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5104 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5106 return tcon->smb2.tcon_id;
5109 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5111 return tcon->smb2.capabilities;
5114 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5115 struct smbXcli_session *session,
5119 uint32_t capabilities,
5120 uint32_t maximal_access)
5122 tcon->is_smb1 = false;
5123 tcon->fs_attributes = 0;
5124 tcon->smb2.tcon_id = tcon_id;
5125 tcon->smb2.type = type;
5126 tcon->smb2.flags = flags;
5127 tcon->smb2.capabilities = capabilities;
5128 tcon->smb2.maximal_access = maximal_access;
5130 tcon->smb2.should_encrypt = false;
5132 if (session == NULL) {
5136 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5138 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5139 tcon->smb2.should_encrypt = true;
5143 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
5145 return tcon->smb2.should_encrypt;