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 tevent_req_set_cleanup_fn(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 void smbXcli_req_cleanup(struct tevent_req *req,
832 enum tevent_req_state req_state)
834 struct smbXcli_req_state *state =
836 struct smbXcli_req_state);
839 case TEVENT_REQ_RECEIVED:
841 * Make sure we really remove it from
842 * the pending array on destruction.
845 smbXcli_req_unset_pending(req);
852 static bool smb1cli_req_cancel(struct tevent_req *req);
853 static bool smb2cli_req_cancel(struct tevent_req *req);
855 static bool smbXcli_req_cancel(struct tevent_req *req)
857 struct smbXcli_req_state *state =
859 struct smbXcli_req_state);
861 if (!smbXcli_conn_is_connected(state->conn)) {
865 if (state->conn->protocol == PROTOCOL_NONE) {
869 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
870 return smb2cli_req_cancel(req);
873 return smb1cli_req_cancel(req);
876 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
878 bool smbXcli_req_set_pending(struct tevent_req *req)
880 struct smbXcli_req_state *state =
882 struct smbXcli_req_state);
883 struct smbXcli_conn *conn;
884 struct tevent_req **pending;
889 if (!smbXcli_conn_is_connected(conn)) {
893 num_pending = talloc_array_length(conn->pending);
895 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
897 if (pending == NULL) {
900 pending[num_pending] = req;
901 conn->pending = pending;
902 tevent_req_set_cleanup_fn(req, smbXcli_req_cleanup);
903 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
905 if (!smbXcli_conn_receive_next(conn)) {
907 * the caller should notify the current request
909 * And all other pending requests get notified
910 * by smbXcli_conn_disconnect().
912 smbXcli_req_unset_pending(req);
913 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
920 static void smbXcli_conn_received(struct tevent_req *subreq);
922 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
924 size_t num_pending = talloc_array_length(conn->pending);
925 struct tevent_req *req;
926 struct smbXcli_req_state *state;
928 if (conn->read_smb_req != NULL) {
932 if (num_pending == 0) {
933 if (conn->smb2.mid < UINT64_MAX) {
934 /* no more pending requests, so we are done for now */
939 * If there are no more SMB2 requests possible,
940 * because we are out of message ids,
941 * we need to disconnect.
943 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
947 req = conn->pending[0];
948 state = tevent_req_data(req, struct smbXcli_req_state);
951 * We're the first ones, add the read_smb request that waits for the
952 * answer from the server
954 conn->read_smb_req = read_smb_send(conn->pending,
957 if (conn->read_smb_req == NULL) {
960 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
964 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
966 struct smbXcli_session *session;
968 tevent_queue_stop(conn->outgoing);
970 if (conn->read_fd != -1) {
971 close(conn->read_fd);
973 if (conn->write_fd != -1) {
974 close(conn->write_fd);
979 session = conn->sessions;
980 if (talloc_array_length(conn->pending) == 0) {
982 * if we do not have pending requests
983 * there is no need to update the channel_sequence
987 for (; session; session = session->next) {
988 smb2cli_session_increment_channel_sequence(session);
992 * Cancel all pending requests. We do not do a for-loop walking
993 * conn->pending because that array changes in
994 * smbXcli_req_unset_pending.
996 while (talloc_array_length(conn->pending) > 0) {
997 struct tevent_req *req;
998 struct smbXcli_req_state *state;
999 struct tevent_req **chain;
1003 req = conn->pending[0];
1004 state = tevent_req_data(req, struct smbXcli_req_state);
1006 if (state->smb1.chained_requests == NULL) {
1008 * We're dead. No point waiting for trans2
1011 state->smb1.mid = 0;
1013 smbXcli_req_unset_pending(req);
1015 if (NT_STATUS_IS_OK(status)) {
1016 /* do not notify the callers */
1021 * we need to defer the callback, because we may notify
1022 * more then one caller.
1024 tevent_req_defer_callback(req, state->ev);
1025 tevent_req_nterror(req, status);
1029 chain = talloc_move(conn, &state->smb1.chained_requests);
1030 num_chained = talloc_array_length(chain);
1032 for (i=0; i<num_chained; i++) {
1034 state = tevent_req_data(req, struct smbXcli_req_state);
1037 * We're dead. No point waiting for trans2
1040 state->smb1.mid = 0;
1042 smbXcli_req_unset_pending(req);
1044 if (NT_STATUS_IS_OK(status)) {
1045 /* do not notify the callers */
1050 * we need to defer the callback, because we may notify
1051 * more than one caller.
1053 tevent_req_defer_callback(req, state->ev);
1054 tevent_req_nterror(req, status);
1061 * Fetch a smb request's mid. Only valid after the request has been sent by
1062 * smb1cli_req_send().
1064 uint16_t smb1cli_req_mid(struct tevent_req *req)
1066 struct smbXcli_req_state *state =
1067 tevent_req_data(req,
1068 struct smbXcli_req_state);
1070 if (state->smb1.mid != 0) {
1071 return state->smb1.mid;
1074 return SVAL(state->smb1.hdr, HDR_MID);
1077 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1079 struct smbXcli_req_state *state =
1080 tevent_req_data(req,
1081 struct smbXcli_req_state);
1083 state->smb1.mid = mid;
1086 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1088 struct smbXcli_req_state *state =
1089 tevent_req_data(req,
1090 struct smbXcli_req_state);
1092 return state->smb1.seqnum;
1095 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1097 struct smbXcli_req_state *state =
1098 tevent_req_data(req,
1099 struct smbXcli_req_state);
1101 state->smb1.seqnum = seqnum;
1104 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1108 for (i=0; i<count; i++) {
1109 result += iov[i].iov_len;
1114 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1115 const struct iovec *iov,
1118 size_t len = smbXcli_iov_len(iov, count);
1123 buf = talloc_array(mem_ctx, uint8_t, len);
1128 for (i=0; i<count; i++) {
1129 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1130 copied += iov[i].iov_len;
1135 static void smb1cli_req_flags(enum protocol_types protocol,
1136 uint32_t smb1_capabilities,
1137 uint8_t smb_command,
1138 uint8_t additional_flags,
1139 uint8_t clear_flags,
1141 uint16_t additional_flags2,
1142 uint16_t clear_flags2,
1146 uint16_t flags2 = 0;
1148 if (protocol >= PROTOCOL_LANMAN1) {
1149 flags |= FLAG_CASELESS_PATHNAMES;
1150 flags |= FLAG_CANONICAL_PATHNAMES;
1153 if (protocol >= PROTOCOL_LANMAN2) {
1154 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1155 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1158 if (protocol >= PROTOCOL_NT1) {
1159 flags2 |= FLAGS2_IS_LONG_NAME;
1161 if (smb1_capabilities & CAP_UNICODE) {
1162 flags2 |= FLAGS2_UNICODE_STRINGS;
1164 if (smb1_capabilities & CAP_STATUS32) {
1165 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1167 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1168 flags2 |= FLAGS2_EXTENDED_SECURITY;
1172 flags |= additional_flags;
1173 flags &= ~clear_flags;
1174 flags2 |= additional_flags2;
1175 flags2 &= ~clear_flags2;
1181 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1183 static bool smb1cli_req_cancel(struct tevent_req *req)
1185 struct smbXcli_req_state *state =
1186 tevent_req_data(req,
1187 struct smbXcli_req_state);
1192 struct tevent_req *subreq;
1195 flags = CVAL(state->smb1.hdr, HDR_FLG);
1196 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1197 pid = SVAL(state->smb1.hdr, HDR_PID);
1198 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1199 mid = SVAL(state->smb1.hdr, HDR_MID);
1201 subreq = smb1cli_req_create(state, state->ev,
1211 0, NULL); /* bytes */
1212 if (subreq == NULL) {
1215 smb1cli_req_set_mid(subreq, mid);
1217 status = smb1cli_req_chain_submit(&subreq, 1);
1218 if (!NT_STATUS_IS_OK(status)) {
1219 TALLOC_FREE(subreq);
1222 smb1cli_req_set_mid(subreq, 0);
1224 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1229 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1231 /* we do not care about the result */
1232 TALLOC_FREE(subreq);
1235 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1236 struct tevent_context *ev,
1237 struct smbXcli_conn *conn,
1238 uint8_t smb_command,
1239 uint8_t additional_flags,
1240 uint8_t clear_flags,
1241 uint16_t additional_flags2,
1242 uint16_t clear_flags2,
1243 uint32_t timeout_msec,
1245 struct smbXcli_tcon *tcon,
1246 struct smbXcli_session *session,
1247 uint8_t wct, uint16_t *vwv,
1249 struct iovec *bytes_iov)
1251 struct tevent_req *req;
1252 struct smbXcli_req_state *state;
1254 uint16_t flags2 = 0;
1258 if (iov_count > MAX_SMB_IOV) {
1260 * Should not happen :-)
1265 req = tevent_req_create(mem_ctx, &state,
1266 struct smbXcli_req_state);
1272 state->session = session;
1276 uid = session->smb1.session_id;
1280 tid = tcon->smb1.tcon_id;
1282 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1283 clear_flags |= FLAG_CASELESS_PATHNAMES;
1285 /* Default setting, case insensitive. */
1286 additional_flags |= FLAG_CASELESS_PATHNAMES;
1289 if (smbXcli_conn_dfs_supported(conn) &&
1290 smbXcli_tcon_is_dfs_share(tcon))
1292 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1296 state->smb1.recv_cmd = 0xFF;
1297 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1298 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1299 if (state->smb1.recv_iov == NULL) {
1304 smb1cli_req_flags(conn->protocol,
1305 conn->smb1.capabilities,
1314 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1315 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1316 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1317 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1318 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1319 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1320 SSVAL(state->smb1.hdr, HDR_TID, tid);
1321 SSVAL(state->smb1.hdr, HDR_PID, pid);
1322 SSVAL(state->smb1.hdr, HDR_UID, uid);
1323 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1324 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1326 state->smb1.vwv = vwv;
1328 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1330 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1331 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1332 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1333 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1334 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1335 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1336 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1337 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1339 if (iov_count != 0) {
1340 memcpy(&state->smb1.iov[4], bytes_iov,
1341 iov_count * sizeof(*bytes_iov));
1343 state->smb1.iov_count = iov_count + 4;
1345 if (timeout_msec > 0) {
1346 struct timeval endtime;
1348 endtime = timeval_current_ofs_msec(timeout_msec);
1349 if (!tevent_req_set_endtime(req, ev, endtime)) {
1354 switch (smb_command) {
1358 state->one_way = true;
1361 state->one_way = true;
1362 state->smb1.one_way_seqnum = true;
1366 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1367 state->one_way = true;
1375 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1376 struct iovec *iov, int iov_count,
1378 bool one_way_seqnum)
1380 TALLOC_CTX *frame = NULL;
1384 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1385 * iovec directly. MD5Update would do that just fine.
1388 if (iov_count < 4) {
1389 return NT_STATUS_INVALID_PARAMETER_MIX;
1391 if (iov[0].iov_len != NBT_HDR_SIZE) {
1392 return NT_STATUS_INVALID_PARAMETER_MIX;
1394 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1395 return NT_STATUS_INVALID_PARAMETER_MIX;
1397 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1398 return NT_STATUS_INVALID_PARAMETER_MIX;
1400 if (iov[3].iov_len != sizeof(uint16_t)) {
1401 return NT_STATUS_INVALID_PARAMETER_MIX;
1404 frame = talloc_stackframe();
1406 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1408 return NT_STATUS_NO_MEMORY;
1411 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1413 smb_signing_sign_pdu(conn->smb1.signing,
1414 buf, talloc_get_size(buf),
1416 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1419 return NT_STATUS_OK;
1422 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1423 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1424 TALLOC_CTX *tmp_mem,
1427 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1428 struct smbXcli_req_state *state,
1429 struct iovec *iov, int iov_count)
1431 struct tevent_req *subreq;
1436 if (!smbXcli_conn_is_connected(state->conn)) {
1437 return NT_STATUS_CONNECTION_DISCONNECTED;
1440 if (state->conn->protocol > PROTOCOL_NT1) {
1441 return NT_STATUS_REVISION_MISMATCH;
1444 if (iov_count < 4) {
1445 return NT_STATUS_INVALID_PARAMETER_MIX;
1447 if (iov[0].iov_len != NBT_HDR_SIZE) {
1448 return NT_STATUS_INVALID_PARAMETER_MIX;
1450 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1451 return NT_STATUS_INVALID_PARAMETER_MIX;
1453 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1454 return NT_STATUS_INVALID_PARAMETER_MIX;
1456 if (iov[3].iov_len != sizeof(uint16_t)) {
1457 return NT_STATUS_INVALID_PARAMETER_MIX;
1460 cmd = CVAL(iov[1].iov_base, HDR_COM);
1461 if (cmd == SMBreadBraw) {
1462 if (smbXcli_conn_has_async_calls(state->conn)) {
1463 return NT_STATUS_INVALID_PARAMETER_MIX;
1465 state->conn->smb1.read_braw_req = req;
1468 if (state->smb1.mid != 0) {
1469 mid = state->smb1.mid;
1471 mid = smb1cli_alloc_mid(state->conn);
1473 SSVAL(iov[1].iov_base, HDR_MID, mid);
1475 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1477 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1478 &state->smb1.seqnum,
1479 state->smb1.one_way_seqnum);
1481 if (!NT_STATUS_IS_OK(status)) {
1486 * If we supported multiple encrytion contexts
1487 * here we'd look up based on tid.
1489 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1490 char *buf, *enc_buf;
1492 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1494 return NT_STATUS_NO_MEMORY;
1496 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1497 (char *)buf, &enc_buf);
1499 if (!NT_STATUS_IS_OK(status)) {
1500 DEBUG(0, ("Error in encrypting client message: %s\n",
1501 nt_errstr(status)));
1504 buf = (char *)talloc_memdup(state, enc_buf,
1505 smb_len_nbt(enc_buf)+4);
1508 return NT_STATUS_NO_MEMORY;
1510 iov[0].iov_base = (void *)buf;
1511 iov[0].iov_len = talloc_get_size(buf);
1515 if (state->conn->dispatch_incoming == NULL) {
1516 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1519 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1521 subreq = writev_send(state, state->ev, state->conn->outgoing,
1522 state->conn->write_fd, false, iov, iov_count);
1523 if (subreq == NULL) {
1524 return NT_STATUS_NO_MEMORY;
1526 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1527 return NT_STATUS_OK;
1530 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1531 struct tevent_context *ev,
1532 struct smbXcli_conn *conn,
1533 uint8_t smb_command,
1534 uint8_t additional_flags,
1535 uint8_t clear_flags,
1536 uint16_t additional_flags2,
1537 uint16_t clear_flags2,
1538 uint32_t timeout_msec,
1540 struct smbXcli_tcon *tcon,
1541 struct smbXcli_session *session,
1542 uint8_t wct, uint16_t *vwv,
1544 const uint8_t *bytes)
1546 struct tevent_req *req;
1550 iov.iov_base = discard_const_p(void, bytes);
1551 iov.iov_len = num_bytes;
1553 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1554 additional_flags, clear_flags,
1555 additional_flags2, clear_flags2,
1562 if (!tevent_req_is_in_progress(req)) {
1563 return tevent_req_post(req, ev);
1565 status = smb1cli_req_chain_submit(&req, 1);
1566 if (tevent_req_nterror(req, status)) {
1567 return tevent_req_post(req, ev);
1572 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1574 struct tevent_req *req =
1575 tevent_req_callback_data(subreq,
1577 struct smbXcli_req_state *state =
1578 tevent_req_data(req,
1579 struct smbXcli_req_state);
1583 nwritten = writev_recv(subreq, &err);
1584 TALLOC_FREE(subreq);
1585 if (nwritten == -1) {
1586 NTSTATUS status = map_nt_error_from_unix_common(err);
1587 smbXcli_conn_disconnect(state->conn, status);
1591 if (state->one_way) {
1592 state->inbuf = NULL;
1593 tevent_req_done(req);
1597 if (!smbXcli_req_set_pending(req)) {
1598 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1603 static void smbXcli_conn_received(struct tevent_req *subreq)
1605 struct smbXcli_conn *conn =
1606 tevent_req_callback_data(subreq,
1607 struct smbXcli_conn);
1608 TALLOC_CTX *frame = talloc_stackframe();
1614 if (subreq != conn->read_smb_req) {
1615 DEBUG(1, ("Internal error: cli_smb_received called with "
1616 "unexpected subreq\n"));
1617 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1621 conn->read_smb_req = NULL;
1623 received = read_smb_recv(subreq, frame, &inbuf, &err);
1624 TALLOC_FREE(subreq);
1625 if (received == -1) {
1626 status = map_nt_error_from_unix_common(err);
1627 smbXcli_conn_disconnect(conn, status);
1632 status = conn->dispatch_incoming(conn, frame, inbuf);
1634 if (NT_STATUS_IS_OK(status)) {
1636 * We should not do any more processing
1637 * as the dispatch function called
1638 * tevent_req_done().
1643 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1645 * We got an error, so notify all pending requests
1647 smbXcli_conn_disconnect(conn, status);
1652 * We got NT_STATUS_RETRY, so we may ask for a
1653 * next incoming pdu.
1655 if (!smbXcli_conn_receive_next(conn)) {
1656 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1660 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1661 struct iovec **piov, int *pnum_iov)
1672 size_t min_size = MIN_SMB_SIZE;
1674 buflen = smb_len_tcp(buf);
1677 hdr = buf + NBT_HDR_SIZE;
1679 status = smb1cli_pull_raw_error(hdr);
1680 if (NT_STATUS_IS_ERR(status)) {
1682 * This is an ugly hack to support OS/2
1683 * which skips the byte_count in the DATA block
1684 * on some error responses.
1688 min_size -= sizeof(uint16_t);
1691 if (buflen < min_size) {
1692 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1696 * This returns iovec elements in the following order:
1711 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1713 return NT_STATUS_NO_MEMORY;
1715 iov[0].iov_base = hdr;
1716 iov[0].iov_len = HDR_WCT;
1719 cmd = CVAL(hdr, HDR_COM);
1723 size_t len = buflen - taken;
1725 struct iovec *iov_tmp;
1732 * we need at least WCT
1734 needed = sizeof(uint8_t);
1736 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1737 __location__, (int)len, (int)needed));
1742 * Now we check if the specified words are there
1744 wct = CVAL(hdr, wct_ofs);
1745 needed += wct * sizeof(uint16_t);
1747 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1748 __location__, (int)len, (int)needed));
1752 if ((num_iov == 1) &&
1754 NT_STATUS_IS_ERR(status))
1757 * This is an ugly hack to support OS/2
1758 * which skips the byte_count in the DATA block
1759 * on some error responses.
1763 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1765 if (iov_tmp == NULL) {
1767 return NT_STATUS_NO_MEMORY;
1770 cur = &iov[num_iov];
1774 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1776 cur[1].iov_base = cur[0].iov_base;
1783 * we need at least BCC
1785 needed += sizeof(uint16_t);
1787 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1788 __location__, (int)len, (int)needed));
1793 * Now we check if the specified bytes are there
1795 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1796 bcc = SVAL(hdr, bcc_ofs);
1797 needed += bcc * sizeof(uint8_t);
1799 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1800 __location__, (int)len, (int)needed));
1805 * we allocate 2 iovec structures for words and bytes
1807 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1809 if (iov_tmp == NULL) {
1811 return NT_STATUS_NO_MEMORY;
1814 cur = &iov[num_iov];
1817 cur[0].iov_len = wct * sizeof(uint16_t);
1818 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1819 cur[1].iov_len = bcc * sizeof(uint8_t);
1820 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1824 if (!smb1cli_is_andx_req(cmd)) {
1826 * If the current command does not have AndX chanining
1832 if (wct == 0 && bcc == 0) {
1834 * An empty response also ends the chain,
1835 * most likely with an error.
1841 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1842 __location__, (int)wct, (int)cmd));
1845 cmd = CVAL(cur[0].iov_base, 0);
1848 * If it is the end of the chain we are also done.
1852 wct_ofs = SVAL(cur[0].iov_base, 2);
1854 if (wct_ofs < taken) {
1855 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1857 if (wct_ofs > buflen) {
1858 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1862 * we consumed everything up to the start of the next
1868 remaining = buflen - taken;
1870 if (remaining > 0 && num_iov >= 3) {
1872 * The last DATA block gets the remaining
1873 * bytes, this is needed to support
1874 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1876 iov[num_iov-1].iov_len += remaining;
1880 *pnum_iov = num_iov;
1881 return NT_STATUS_OK;
1885 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1888 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1889 TALLOC_CTX *tmp_mem,
1892 struct tevent_req *req;
1893 struct smbXcli_req_state *state;
1900 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1901 size_t len = smb_len_tcp(inbuf);
1902 struct iovec *iov = NULL;
1904 struct tevent_req **chain = NULL;
1905 size_t num_chained = 0;
1906 size_t num_responses = 0;
1908 if (conn->smb1.read_braw_req != NULL) {
1909 req = conn->smb1.read_braw_req;
1910 conn->smb1.read_braw_req = NULL;
1911 state = tevent_req_data(req, struct smbXcli_req_state);
1913 smbXcli_req_unset_pending(req);
1915 if (state->smb1.recv_iov == NULL) {
1917 * For requests with more than
1918 * one response, we have to readd the
1921 state->smb1.recv_iov = talloc_zero_array(state,
1924 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1925 return NT_STATUS_OK;
1929 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1930 state->smb1.recv_iov[0].iov_len = len;
1931 ZERO_STRUCT(state->smb1.recv_iov[1]);
1932 ZERO_STRUCT(state->smb1.recv_iov[2]);
1934 state->smb1.recv_cmd = SMBreadBraw;
1935 state->smb1.recv_status = NT_STATUS_OK;
1936 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1938 tevent_req_done(req);
1939 return NT_STATUS_OK;
1942 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1943 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1944 DEBUG(10, ("Got non-SMB PDU\n"));
1945 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1949 * If we supported multiple encrytion contexts
1950 * here we'd look up based on tid.
1952 if (common_encryption_on(conn->smb1.trans_enc)
1953 && (CVAL(inbuf, 0) == 0)) {
1954 uint16_t enc_ctx_num;
1956 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1957 if (!NT_STATUS_IS_OK(status)) {
1958 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1959 nt_errstr(status)));
1963 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1964 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1966 conn->smb1.trans_enc->enc_ctx_num));
1967 return NT_STATUS_INVALID_HANDLE;
1970 status = common_decrypt_buffer(conn->smb1.trans_enc,
1972 if (!NT_STATUS_IS_OK(status)) {
1973 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1974 nt_errstr(status)));
1977 inhdr = inbuf + NBT_HDR_SIZE;
1978 len = smb_len_nbt(inbuf);
1981 mid = SVAL(inhdr, HDR_MID);
1982 num_pending = talloc_array_length(conn->pending);
1984 for (i=0; i<num_pending; i++) {
1985 if (mid == smb1cli_req_mid(conn->pending[i])) {
1989 if (i == num_pending) {
1990 /* Dump unexpected reply */
1991 return NT_STATUS_RETRY;
1994 oplock_break = false;
1996 if (mid == 0xffff) {
1998 * Paranoia checks that this is really an oplock break request.
2000 oplock_break = (len == 51); /* hdr + 8 words */
2001 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2002 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2003 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2004 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2006 if (!oplock_break) {
2007 /* Dump unexpected reply */
2008 return NT_STATUS_RETRY;
2012 req = conn->pending[i];
2013 state = tevent_req_data(req, struct smbXcli_req_state);
2015 if (!oplock_break /* oplock breaks are not signed */
2016 && !smb_signing_check_pdu(conn->smb1.signing,
2017 inhdr, len, state->smb1.seqnum+1)) {
2018 DEBUG(10, ("cli_check_sign_mac failed\n"));
2019 return NT_STATUS_ACCESS_DENIED;
2022 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2024 if (!NT_STATUS_IS_OK(status)) {
2025 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2026 nt_errstr(status)));
2030 cmd = CVAL(inhdr, HDR_COM);
2031 status = smb1cli_pull_raw_error(inhdr);
2033 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2034 (state->session != NULL) && state->session->disconnect_expired)
2037 * this should be a short term hack
2038 * until the upper layers have implemented
2039 * re-authentication.
2044 if (state->smb1.chained_requests == NULL) {
2046 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2049 smbXcli_req_unset_pending(req);
2051 if (state->smb1.recv_iov == NULL) {
2053 * For requests with more than
2054 * one response, we have to readd the
2057 state->smb1.recv_iov = talloc_zero_array(state,
2060 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2061 return NT_STATUS_OK;
2065 state->smb1.recv_cmd = cmd;
2066 state->smb1.recv_status = status;
2067 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2069 state->smb1.recv_iov[0] = iov[0];
2070 state->smb1.recv_iov[1] = iov[1];
2071 state->smb1.recv_iov[2] = iov[2];
2073 if (talloc_array_length(conn->pending) == 0) {
2074 tevent_req_done(req);
2075 return NT_STATUS_OK;
2078 tevent_req_defer_callback(req, state->ev);
2079 tevent_req_done(req);
2080 return NT_STATUS_RETRY;
2083 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2084 num_chained = talloc_array_length(chain);
2085 num_responses = (num_iov - 1)/2;
2087 if (num_responses > num_chained) {
2088 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2091 for (i=0; i<num_chained; i++) {
2092 size_t iov_idx = 1 + (i*2);
2093 struct iovec *cur = &iov[iov_idx];
2097 state = tevent_req_data(req, struct smbXcli_req_state);
2099 smbXcli_req_unset_pending(req);
2102 * as we finish multiple requests here
2103 * we need to defer the callbacks as
2104 * they could destroy our current stack state.
2106 tevent_req_defer_callback(req, state->ev);
2108 if (i >= num_responses) {
2109 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2113 if (state->smb1.recv_iov == NULL) {
2115 * For requests with more than
2116 * one response, we have to readd the
2119 state->smb1.recv_iov = talloc_zero_array(state,
2122 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2127 state->smb1.recv_cmd = cmd;
2129 if (i == (num_responses - 1)) {
2131 * The last request in the chain gets the status
2133 state->smb1.recv_status = status;
2135 cmd = CVAL(cur[0].iov_base, 0);
2136 state->smb1.recv_status = NT_STATUS_OK;
2139 state->inbuf = inbuf;
2142 * Note: here we use talloc_reference() in a way
2143 * that does not expose it to the caller.
2145 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2146 if (tevent_req_nomem(inbuf_ref, req)) {
2150 /* copy the related buffers */
2151 state->smb1.recv_iov[0] = iov[0];
2152 state->smb1.recv_iov[1] = cur[0];
2153 state->smb1.recv_iov[2] = cur[1];
2155 tevent_req_done(req);
2158 return NT_STATUS_RETRY;
2161 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2162 TALLOC_CTX *mem_ctx,
2163 struct iovec **piov,
2167 uint32_t *pvwv_offset,
2168 uint32_t *pnum_bytes,
2170 uint32_t *pbytes_offset,
2172 const struct smb1cli_req_expected_response *expected,
2173 size_t num_expected)
2175 struct smbXcli_req_state *state =
2176 tevent_req_data(req,
2177 struct smbXcli_req_state);
2178 NTSTATUS status = NT_STATUS_OK;
2179 struct iovec *recv_iov = NULL;
2180 uint8_t *hdr = NULL;
2182 uint32_t vwv_offset = 0;
2183 uint16_t *vwv = NULL;
2184 uint32_t num_bytes = 0;
2185 uint32_t bytes_offset = 0;
2186 uint8_t *bytes = NULL;
2188 bool found_status = false;
2189 bool found_size = false;
2203 if (pvwv_offset != NULL) {
2206 if (pnum_bytes != NULL) {
2209 if (pbytes != NULL) {
2212 if (pbytes_offset != NULL) {
2215 if (pinbuf != NULL) {
2219 if (state->inbuf != NULL) {
2220 recv_iov = state->smb1.recv_iov;
2221 state->smb1.recv_iov = NULL;
2222 if (state->smb1.recv_cmd != SMBreadBraw) {
2223 hdr = (uint8_t *)recv_iov[0].iov_base;
2224 wct = recv_iov[1].iov_len/2;
2225 vwv = (uint16_t *)recv_iov[1].iov_base;
2226 vwv_offset = PTR_DIFF(vwv, hdr);
2227 num_bytes = recv_iov[2].iov_len;
2228 bytes = (uint8_t *)recv_iov[2].iov_base;
2229 bytes_offset = PTR_DIFF(bytes, hdr);
2233 if (tevent_req_is_nterror(req, &status)) {
2234 for (i=0; i < num_expected; i++) {
2235 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2236 found_status = true;
2242 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2248 if (num_expected == 0) {
2249 found_status = true;
2253 status = state->smb1.recv_status;
2255 for (i=0; i < num_expected; i++) {
2256 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2260 found_status = true;
2261 if (expected[i].wct == 0) {
2266 if (expected[i].wct == wct) {
2272 if (!found_status) {
2277 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2281 *piov = talloc_move(mem_ctx, &recv_iov);
2293 if (pvwv_offset != NULL) {
2294 *pvwv_offset = vwv_offset;
2296 if (pnum_bytes != NULL) {
2297 *pnum_bytes = num_bytes;
2299 if (pbytes != NULL) {
2302 if (pbytes_offset != NULL) {
2303 *pbytes_offset = bytes_offset;
2305 if (pinbuf != NULL) {
2306 *pinbuf = state->inbuf;
2312 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2319 for (i=0; i<num_reqs; i++) {
2320 struct smbXcli_req_state *state;
2321 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2322 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2323 state->smb1.iov_count-2);
2324 wct_ofs = (wct_ofs + 3) & ~3;
2329 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2331 struct smbXcli_req_state *first_state =
2332 tevent_req_data(reqs[0],
2333 struct smbXcli_req_state);
2334 struct smbXcli_req_state *state;
2336 size_t chain_padding = 0;
2338 struct iovec *iov = NULL;
2339 struct iovec *this_iov;
2343 if (num_reqs == 1) {
2344 return smb1cli_req_writev_submit(reqs[0], first_state,
2345 first_state->smb1.iov,
2346 first_state->smb1.iov_count);
2350 for (i=0; i<num_reqs; i++) {
2351 if (!tevent_req_is_in_progress(reqs[i])) {
2352 return NT_STATUS_INTERNAL_ERROR;
2355 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2357 if (state->smb1.iov_count < 4) {
2358 return NT_STATUS_INVALID_PARAMETER_MIX;
2363 * The NBT and SMB header
2376 iovlen += state->smb1.iov_count - 2;
2379 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2381 return NT_STATUS_NO_MEMORY;
2384 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2385 first_state, reqs, sizeof(*reqs) * num_reqs);
2386 if (first_state->smb1.chained_requests == NULL) {
2388 return NT_STATUS_NO_MEMORY;
2391 wct_offset = HDR_WCT;
2394 for (i=0; i<num_reqs; i++) {
2395 size_t next_padding = 0;
2398 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2400 if (i < num_reqs-1) {
2401 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2402 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2404 TALLOC_FREE(first_state->smb1.chained_requests);
2405 return NT_STATUS_INVALID_PARAMETER_MIX;
2409 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2410 state->smb1.iov_count-2) + 1;
2411 if ((wct_offset % 4) != 0) {
2412 next_padding = 4 - (wct_offset % 4);
2414 wct_offset += next_padding;
2415 vwv = state->smb1.vwv;
2417 if (i < num_reqs-1) {
2418 struct smbXcli_req_state *next_state =
2419 tevent_req_data(reqs[i+1],
2420 struct smbXcli_req_state);
2421 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2423 SSVAL(vwv+1, 0, wct_offset);
2424 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2425 /* properly end the chain */
2426 SCVAL(vwv+0, 0, 0xff);
2427 SCVAL(vwv+0, 1, 0xff);
2433 * The NBT and SMB header
2435 this_iov[0] = state->smb1.iov[0];
2436 this_iov[1] = state->smb1.iov[1];
2440 * This one is a bit subtle. We have to add
2441 * chain_padding bytes between the requests, and we
2442 * have to also include the wct field of the
2443 * subsequent requests. We use the subsequent header
2444 * for the padding, it contains the wct field in its
2447 this_iov[0].iov_len = chain_padding+1;
2448 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2449 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2450 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2455 * copy the words and bytes
2457 memcpy(this_iov, state->smb1.iov+2,
2458 sizeof(struct iovec) * (state->smb1.iov_count-2));
2459 this_iov += state->smb1.iov_count - 2;
2460 chain_padding = next_padding;
2463 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2464 if (nbt_len > first_state->conn->smb1.max_xmit) {
2466 TALLOC_FREE(first_state->smb1.chained_requests);
2467 return NT_STATUS_INVALID_PARAMETER_MIX;
2470 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2471 if (!NT_STATUS_IS_OK(status)) {
2473 TALLOC_FREE(first_state->smb1.chained_requests);
2477 return NT_STATUS_OK;
2480 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2482 return ((tevent_queue_length(conn->outgoing) != 0)
2483 || (talloc_array_length(conn->pending) != 0));
2486 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2488 if (conn->protocol >= PROTOCOL_SMB2_02) {
2489 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2492 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2495 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2497 uint16_t credits = 1;
2499 if (conn->smb2.cur_credits == 0) {
2500 if (max_dyn_len != NULL) {
2506 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2507 credits = conn->smb2.cur_credits;
2510 if (max_dyn_len != NULL) {
2511 *max_dyn_len = credits * 65536;
2517 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2519 return conn->smb2.server.capabilities;
2522 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2524 return conn->smb2.server.security_mode;
2527 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2529 return conn->smb2.server.max_trans_size;
2532 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2534 return conn->smb2.server.max_read_size;
2537 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2539 return conn->smb2.server.max_write_size;
2542 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2543 uint16_t max_credits)
2545 conn->smb2.max_credits = max_credits;
2548 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2550 static bool smb2cli_req_cancel(struct tevent_req *req)
2552 struct smbXcli_req_state *state =
2553 tevent_req_data(req,
2554 struct smbXcli_req_state);
2555 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2556 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2557 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2558 struct smbXcli_tcon *tcon = state->tcon;
2559 struct smbXcli_session *session = state->session;
2560 uint8_t *fixed = state->smb2.pad;
2561 uint16_t fixed_len = 4;
2562 struct tevent_req *subreq;
2563 struct smbXcli_req_state *substate;
2566 SSVAL(fixed, 0, 0x04);
2569 subreq = smb2cli_req_create(state, state->ev,
2577 if (subreq == NULL) {
2580 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2583 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2584 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2586 flags &= SMB2_HDR_FLAG_ASYNC;
2588 if (flags & SMB2_HDR_FLAG_ASYNC) {
2592 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2593 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2594 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2596 status = smb2cli_req_compound_submit(&subreq, 1);
2597 if (!NT_STATUS_IS_OK(status)) {
2598 TALLOC_FREE(subreq);
2602 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2607 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2609 /* we do not care about the result */
2610 TALLOC_FREE(subreq);
2613 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2614 struct tevent_context *ev,
2615 struct smbXcli_conn *conn,
2617 uint32_t additional_flags,
2618 uint32_t clear_flags,
2619 uint32_t timeout_msec,
2620 struct smbXcli_tcon *tcon,
2621 struct smbXcli_session *session,
2622 const uint8_t *fixed,
2626 uint32_t max_dyn_len)
2628 struct tevent_req *req;
2629 struct smbXcli_req_state *state;
2633 bool use_channel_sequence = false;
2634 uint16_t channel_sequence = 0;
2636 req = tevent_req_create(mem_ctx, &state,
2637 struct smbXcli_req_state);
2644 state->session = session;
2647 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2648 use_channel_sequence = true;
2649 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2650 use_channel_sequence = true;
2654 uid = session->smb2->session_id;
2656 if (use_channel_sequence) {
2657 channel_sequence = session->smb2->channel_sequence;
2660 state->smb2.should_sign = session->smb2->should_sign;
2661 state->smb2.should_encrypt = session->smb2->should_encrypt;
2663 if (cmd == SMB2_OP_SESSSETUP &&
2664 session->smb2->signing_key.length != 0) {
2665 state->smb2.should_sign = true;
2668 if (cmd == SMB2_OP_SESSSETUP &&
2669 session->smb2_channel.signing_key.length == 0) {
2670 state->smb2.should_encrypt = false;
2675 tid = tcon->smb2.tcon_id;
2677 if (tcon->smb2.should_encrypt) {
2678 state->smb2.should_encrypt = true;
2682 if (state->smb2.should_encrypt) {
2683 state->smb2.should_sign = false;
2686 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2687 if (state->smb2.recv_iov == NULL) {
2692 flags |= additional_flags;
2693 flags &= ~clear_flags;
2695 state->smb2.fixed = fixed;
2696 state->smb2.fixed_len = fixed_len;
2697 state->smb2.dyn = dyn;
2698 state->smb2.dyn_len = dyn_len;
2699 state->smb2.max_dyn_len = max_dyn_len;
2701 if (state->smb2.should_encrypt) {
2702 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2703 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2706 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2707 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2708 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2709 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2710 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2711 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2712 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2713 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2716 case SMB2_OP_CANCEL:
2717 state->one_way = true;
2721 * If this is a dummy request, it will have
2722 * UINT64_MAX as message id.
2723 * If we send on break acknowledgement,
2724 * this gets overwritten later.
2726 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2730 if (timeout_msec > 0) {
2731 struct timeval endtime;
2733 endtime = timeval_current_ofs_msec(timeout_msec);
2734 if (!tevent_req_set_endtime(req, ev, endtime)) {
2742 void smb2cli_req_set_notify_async(struct tevent_req *req)
2744 struct smbXcli_req_state *state =
2745 tevent_req_data(req,
2746 struct smbXcli_req_state);
2748 state->smb2.notify_async = true;
2751 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2752 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2753 TALLOC_CTX *tmp_mem,
2756 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2759 struct smbXcli_req_state *state;
2760 struct tevent_req *subreq;
2762 int i, num_iov, nbt_len;
2764 const DATA_BLOB *encryption_key = NULL;
2765 uint64_t encryption_session_id = 0;
2768 * 1 for the nbt length, optional TRANSFORM
2769 * per request: HDR, fixed, dyn, padding
2770 * -1 because the last one does not need padding
2773 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2775 return NT_STATUS_NO_MEMORY;
2782 * the session of the first request that requires encryption
2783 * specifies the encryption key.
2785 for (i=0; i<num_reqs; i++) {
2786 if (!tevent_req_is_in_progress(reqs[i])) {
2787 return NT_STATUS_INTERNAL_ERROR;
2790 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2792 if (!smbXcli_conn_is_connected(state->conn)) {
2793 return NT_STATUS_CONNECTION_DISCONNECTED;
2796 if ((state->conn->protocol != PROTOCOL_NONE) &&
2797 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2798 return NT_STATUS_REVISION_MISMATCH;
2801 if (state->session == NULL) {
2805 if (!state->smb2.should_encrypt) {
2809 encryption_key = &state->session->smb2->encryption_key;
2810 if (encryption_key->length == 0) {
2811 return NT_STATUS_INVALID_PARAMETER_MIX;
2814 encryption_session_id = state->session->smb2->session_id;
2817 iov[num_iov].iov_base = state->smb2.transform;
2818 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2821 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2822 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2823 state->session->smb2->nonce_low);
2824 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2825 state->session->smb2->nonce_high);
2826 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2827 encryption_session_id);
2829 state->session->smb2->nonce_low += 1;
2830 if (state->session->smb2->nonce_low == 0) {
2831 state->session->smb2->nonce_high += 1;
2832 state->session->smb2->nonce_low += 1;
2835 nbt_len += SMB2_TF_HDR_SIZE;
2839 for (i=0; i<num_reqs; i++) {
2848 const DATA_BLOB *signing_key = NULL;
2850 if (!tevent_req_is_in_progress(reqs[i])) {
2851 return NT_STATUS_INTERNAL_ERROR;
2854 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2856 if (!smbXcli_conn_is_connected(state->conn)) {
2857 return NT_STATUS_CONNECTION_DISCONNECTED;
2860 if ((state->conn->protocol != PROTOCOL_NONE) &&
2861 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2862 return NT_STATUS_REVISION_MISMATCH;
2865 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2866 if (opcode == SMB2_OP_CANCEL) {
2870 avail = UINT64_MAX - state->conn->smb2.mid;
2872 return NT_STATUS_CONNECTION_ABORTED;
2875 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2876 uint32_t max_dyn_len = 1;
2878 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2879 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2881 charge = (max_dyn_len - 1)/ 65536 + 1;
2886 charge = MAX(state->smb2.credit_charge, charge);
2888 avail = MIN(avail, state->conn->smb2.cur_credits);
2889 if (avail < charge) {
2890 return NT_STATUS_INTERNAL_ERROR;
2894 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2895 credits = state->conn->smb2.max_credits -
2896 state->conn->smb2.cur_credits;
2898 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2902 mid = state->conn->smb2.mid;
2903 state->conn->smb2.mid += charge;
2904 state->conn->smb2.cur_credits -= charge;
2906 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2907 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2909 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2910 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2913 if (state->session && encryption_key == NULL) {
2915 * We prefer the channel signing key if it is
2918 if (state->smb2.should_sign) {
2919 signing_key = &state->session->smb2_channel.signing_key;
2923 * If it is a channel binding, we already have the main
2924 * signing key and try that one.
2926 if (signing_key && signing_key->length == 0) {
2927 signing_key = &state->session->smb2->signing_key;
2931 * If we do not have any session key yet, we skip the
2932 * signing of SMB2_OP_SESSSETUP requests.
2934 if (signing_key && signing_key->length == 0) {
2940 iov[num_iov].iov_base = state->smb2.hdr;
2941 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2944 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2945 iov[num_iov].iov_len = state->smb2.fixed_len;
2948 if (state->smb2.dyn != NULL) {
2949 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2950 iov[num_iov].iov_len = state->smb2.dyn_len;
2954 reqlen = sizeof(state->smb2.hdr);
2955 reqlen += state->smb2.fixed_len;
2956 reqlen += state->smb2.dyn_len;
2958 if (i < num_reqs-1) {
2959 if ((reqlen % 8) > 0) {
2960 uint8_t pad = 8 - (reqlen % 8);
2961 iov[num_iov].iov_base = state->smb2.pad;
2962 iov[num_iov].iov_len = pad;
2966 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2969 state->smb2.encryption_session_id = encryption_session_id;
2971 if (signing_key != NULL) {
2974 status = smb2_signing_sign_pdu(*signing_key,
2975 state->session->conn->protocol,
2976 &iov[hdr_iov], num_iov - hdr_iov);
2977 if (!NT_STATUS_IS_OK(status)) {
2984 ret = smbXcli_req_set_pending(reqs[i]);
2986 return NT_STATUS_NO_MEMORY;
2990 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2991 _smb_setlen_tcp(state->length_hdr, nbt_len);
2992 iov[0].iov_base = state->length_hdr;
2993 iov[0].iov_len = sizeof(state->length_hdr);
2995 if (encryption_key != NULL) {
2997 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3001 buf = talloc_array(iov, uint8_t, buflen);
3003 return NT_STATUS_NO_MEMORY;
3007 * We copy the buffers before encrypting them,
3008 * this is at least currently needed for the
3009 * to keep state->smb2.hdr.
3011 * Also the callers may expect there buffers
3014 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3015 struct iovec *v = &iov[vi];
3016 const uint8_t *o = (const uint8_t *)v->iov_base;
3018 memcpy(buf, o, v->iov_len);
3019 v->iov_base = (void *)buf;
3023 status = smb2_signing_encrypt_pdu(*encryption_key,
3024 state->conn->protocol,
3025 &iov[tf_iov], num_iov - tf_iov);
3026 if (!NT_STATUS_IS_OK(status)) {
3031 if (state->conn->dispatch_incoming == NULL) {
3032 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3035 subreq = writev_send(state, state->ev, state->conn->outgoing,
3036 state->conn->write_fd, false, iov, num_iov);
3037 if (subreq == NULL) {
3038 return NT_STATUS_NO_MEMORY;
3040 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3041 return NT_STATUS_OK;
3044 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3046 struct smbXcli_req_state *state =
3047 tevent_req_data(req,
3048 struct smbXcli_req_state);
3050 state->smb2.credit_charge = charge;
3053 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3054 struct tevent_context *ev,
3055 struct smbXcli_conn *conn,
3057 uint32_t additional_flags,
3058 uint32_t clear_flags,
3059 uint32_t timeout_msec,
3060 struct smbXcli_tcon *tcon,
3061 struct smbXcli_session *session,
3062 const uint8_t *fixed,
3066 uint32_t max_dyn_len)
3068 struct tevent_req *req;
3071 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3072 additional_flags, clear_flags,
3081 if (!tevent_req_is_in_progress(req)) {
3082 return tevent_req_post(req, ev);
3084 status = smb2cli_req_compound_submit(&req, 1);
3085 if (tevent_req_nterror(req, status)) {
3086 return tevent_req_post(req, ev);
3091 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3093 struct tevent_req *req =
3094 tevent_req_callback_data(subreq,
3096 struct smbXcli_req_state *state =
3097 tevent_req_data(req,
3098 struct smbXcli_req_state);
3102 nwritten = writev_recv(subreq, &err);
3103 TALLOC_FREE(subreq);
3104 if (nwritten == -1) {
3105 /* here, we need to notify all pending requests */
3106 NTSTATUS status = map_nt_error_from_unix_common(err);
3107 smbXcli_conn_disconnect(state->conn, status);
3112 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3115 TALLOC_CTX *mem_ctx,
3116 struct iovec **piov, int *pnum_iov)
3121 uint8_t *first_hdr = buf;
3122 size_t verified_buflen = 0;
3126 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3128 return NT_STATUS_NO_MEMORY;
3131 while (taken < buflen) {
3132 size_t len = buflen - taken;
3133 uint8_t *hdr = first_hdr + taken;
3136 size_t next_command_ofs;
3138 struct iovec *iov_tmp;
3140 if (verified_buflen > taken) {
3141 len = verified_buflen - taken;
3148 DEBUG(10, ("%d bytes left, expected at least %d\n",
3152 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3153 struct smbXcli_session *s;
3155 struct iovec tf_iov[2];
3159 if (len < SMB2_TF_HDR_SIZE) {
3160 DEBUG(10, ("%d bytes left, expected at least %d\n",
3161 (int)len, SMB2_TF_HDR_SIZE));
3165 tf_len = SMB2_TF_HDR_SIZE;
3168 hdr = first_hdr + taken;
3169 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3170 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3172 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3173 DEBUG(10, ("%d bytes left, expected at least %d\n",
3175 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3180 for (; s; s = s->next) {
3181 if (s->smb2->session_id != uid) {
3188 DEBUG(10, ("unknown session_id %llu\n",
3189 (unsigned long long)uid));
3193 tf_iov[0].iov_base = (void *)tf;
3194 tf_iov[0].iov_len = tf_len;
3195 tf_iov[1].iov_base = (void *)hdr;
3196 tf_iov[1].iov_len = enc_len;
3198 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3201 if (!NT_STATUS_IS_OK(status)) {
3206 verified_buflen = taken + enc_len;
3211 * We need the header plus the body length field
3214 if (len < SMB2_HDR_BODY + 2) {
3215 DEBUG(10, ("%d bytes left, expected at least %d\n",
3216 (int)len, SMB2_HDR_BODY));
3219 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3220 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3224 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3225 DEBUG(10, ("Got HDR len %d, expected %d\n",
3226 SVAL(hdr, 4), SMB2_HDR_BODY));
3231 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3232 body_size = SVAL(hdr, SMB2_HDR_BODY);
3234 if (next_command_ofs != 0) {
3235 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3238 if (next_command_ofs > full_size) {
3241 full_size = next_command_ofs;
3243 if (body_size < 2) {
3246 body_size &= 0xfffe;
3248 if (body_size > (full_size - SMB2_HDR_BODY)) {
3252 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3254 if (iov_tmp == NULL) {
3256 return NT_STATUS_NO_MEMORY;
3259 cur = &iov[num_iov];
3262 cur[0].iov_base = tf;
3263 cur[0].iov_len = tf_len;
3264 cur[1].iov_base = hdr;
3265 cur[1].iov_len = SMB2_HDR_BODY;
3266 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3267 cur[2].iov_len = body_size;
3268 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3269 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3275 *pnum_iov = num_iov;
3276 return NT_STATUS_OK;
3280 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3283 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3286 size_t num_pending = talloc_array_length(conn->pending);
3289 for (i=0; i<num_pending; i++) {
3290 struct tevent_req *req = conn->pending[i];
3291 struct smbXcli_req_state *state =
3292 tevent_req_data(req,
3293 struct smbXcli_req_state);
3295 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3302 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3303 TALLOC_CTX *tmp_mem,
3306 struct tevent_req *req;
3307 struct smbXcli_req_state *state = NULL;
3312 struct smbXcli_session *last_session = NULL;
3313 size_t inbuf_len = smb_len_tcp(inbuf);
3315 status = smb2cli_inbuf_parse_compound(conn,
3316 inbuf + NBT_HDR_SIZE,
3320 if (!NT_STATUS_IS_OK(status)) {
3324 for (i=0; i<num_iov; i+=4) {
3325 uint8_t *inbuf_ref = NULL;
3326 struct iovec *cur = &iov[i];
3327 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3328 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3329 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3330 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3331 uint16_t req_opcode;
3333 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3334 uint32_t new_credits;
3335 struct smbXcli_session *session = NULL;
3336 const DATA_BLOB *signing_key = NULL;
3337 bool was_encrypted = false;
3339 new_credits = conn->smb2.cur_credits;
3340 new_credits += credits;
3341 if (new_credits > UINT16_MAX) {
3342 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3344 conn->smb2.cur_credits += credits;
3346 req = smb2cli_conn_find_pending(conn, mid);
3348 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3350 state = tevent_req_data(req, struct smbXcli_req_state);
3352 state->smb2.got_async = false;
3354 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3355 if (opcode != req_opcode) {
3356 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3358 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3360 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3361 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3364 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3365 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3366 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3367 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3370 * async interim responses are not signed,
3371 * even if the SMB2_HDR_FLAG_SIGNED flag
3374 req_flags |= SMB2_HDR_FLAG_ASYNC;
3375 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3376 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3378 if (state->smb2.notify_async) {
3379 state->smb2.got_async = true;
3380 tevent_req_defer_callback(req, state->ev);
3381 tevent_req_notify_callback(req);
3386 session = state->session;
3387 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3388 session = last_session;
3390 last_session = session;
3392 if (state->smb2.should_sign) {
3393 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3394 return NT_STATUS_ACCESS_DENIED;
3398 if (flags & SMB2_HDR_FLAG_SIGNED) {
3399 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3401 if (session == NULL) {
3402 struct smbXcli_session *s;
3404 s = state->conn->sessions;
3405 for (; s; s = s->next) {
3406 if (s->smb2->session_id != uid) {
3415 if (session == NULL) {
3416 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3419 last_session = session;
3420 signing_key = &session->smb2_channel.signing_key;
3423 if (opcode == SMB2_OP_SESSSETUP) {
3425 * We prefer the channel signing key, if it is
3428 * If we do not have a channel signing key yet,
3429 * we try the main signing key, if it is not
3430 * the final response.
3432 if (signing_key && signing_key->length == 0 &&
3433 !NT_STATUS_IS_OK(status)) {
3434 signing_key = &session->smb2->signing_key;
3437 if (signing_key && signing_key->length == 0) {
3439 * If we do not have a session key to
3440 * verify the signature, we defer the
3441 * signing check to the caller.
3443 * The caller gets NT_STATUS_OK, it
3445 * smb2cli_session_set_session_key()
3447 * smb2cli_session_set_channel_key()
3448 * which will check the signature
3449 * with the channel signing key.
3455 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3456 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3457 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3460 * If the response was encrypted in a SMB2_TRANSFORM
3461 * pdu, which belongs to the correct session,
3462 * we do not need to do signing checks
3464 * It could be the session the response belongs to
3465 * or the session that was used to encrypt the
3466 * SMB2_TRANSFORM request.
3468 if ((session && session->smb2->session_id == uid) ||
3469 (state->smb2.encryption_session_id == uid)) {
3471 was_encrypted = true;
3475 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3477 * if the server returns NT_STATUS_USER_SESSION_DELETED
3478 * the response is not signed and we should
3479 * propagate the NT_STATUS_USER_SESSION_DELETED
3480 * status to the caller.
3482 state->smb2.signing_skipped = true;
3486 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3488 * if the server returns
3489 * NT_STATUS_INVALID_PARAMETER
3490 * the response might not be encrypted.
3492 if (state->smb2.should_encrypt && !was_encrypted) {
3493 state->smb2.signing_skipped = true;
3498 if (state->smb2.should_encrypt && !was_encrypted) {
3499 if (!state->smb2.signing_skipped) {
3500 return NT_STATUS_ACCESS_DENIED;
3504 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3505 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3506 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3508 * if the server returns
3509 * NT_STATUS_NETWORK_NAME_DELETED
3510 * NT_STATUS_FILE_CLOSED
3511 * NT_STATUS_INVALID_PARAMETER
3512 * the response might not be signed
3513 * as this happens before the signing checks.
3515 * If server echos the signature (or all zeros)
3516 * we should report the status from the server
3522 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3523 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3526 state->smb2.signing_skipped = true;
3532 static const uint8_t zeros[16];
3534 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3538 state->smb2.signing_skipped = true;
3545 status = smb2_signing_check_pdu(*signing_key,
3546 state->conn->protocol,
3548 if (!NT_STATUS_IS_OK(status)) {
3550 * If the signing check fails, we disconnect
3557 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3558 (session != NULL) && session->disconnect_expired)
3561 * this should be a short term hack
3562 * until the upper layers have implemented
3563 * re-authentication.
3568 smbXcli_req_unset_pending(req);
3571 * There might be more than one response
3572 * we need to defer the notifications
3574 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3579 tevent_req_defer_callback(req, state->ev);
3583 * Note: here we use talloc_reference() in a way
3584 * that does not expose it to the caller.
3586 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3587 if (tevent_req_nomem(inbuf_ref, req)) {
3591 /* copy the related buffers */
3592 state->smb2.recv_iov[0] = cur[1];
3593 state->smb2.recv_iov[1] = cur[2];
3594 state->smb2.recv_iov[2] = cur[3];
3596 tevent_req_done(req);
3600 return NT_STATUS_RETRY;
3603 return NT_STATUS_OK;
3606 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3607 struct iovec **piov,
3608 const struct smb2cli_req_expected_response *expected,
3609 size_t num_expected)
3611 struct smbXcli_req_state *state =
3612 tevent_req_data(req,
3613 struct smbXcli_req_state);
3616 bool found_status = false;
3617 bool found_size = false;
3624 if (state->smb2.got_async) {
3625 return STATUS_PENDING;
3628 if (tevent_req_is_nterror(req, &status)) {
3629 for (i=0; i < num_expected; i++) {
3630 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3631 found_status = true;
3637 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3643 if (num_expected == 0) {
3644 found_status = true;
3648 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3649 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3651 for (i=0; i < num_expected; i++) {
3652 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3656 found_status = true;
3657 if (expected[i].body_size == 0) {
3662 if (expected[i].body_size == body_size) {
3668 if (!found_status) {
3672 if (state->smb2.signing_skipped) {
3673 if (num_expected > 0) {
3674 return NT_STATUS_ACCESS_DENIED;
3676 if (!NT_STATUS_IS_ERR(status)) {
3677 return NT_STATUS_ACCESS_DENIED;
3682 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3686 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3692 static const struct {
3693 enum protocol_types proto;
3694 const char *smb1_name;
3695 } smb1cli_prots[] = {
3696 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3697 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3698 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3699 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3700 {PROTOCOL_LANMAN2, "LM1.2X002"},
3701 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3702 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3703 {PROTOCOL_LANMAN2, "Samba"},
3704 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3705 {PROTOCOL_NT1, "NT LM 0.12"},
3706 {PROTOCOL_SMB2_02, "SMB 2.002"},
3707 {PROTOCOL_SMB2_10, "SMB 2.???"},
3710 static const struct {
3711 enum protocol_types proto;
3712 uint16_t smb2_dialect;
3713 } smb2cli_prots[] = {
3714 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3715 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3716 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3717 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3718 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3719 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
3722 struct smbXcli_negprot_state {
3723 struct smbXcli_conn *conn;
3724 struct tevent_context *ev;
3725 uint32_t timeout_msec;
3726 enum protocol_types min_protocol;
3727 enum protocol_types max_protocol;
3731 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3735 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3736 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3737 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3738 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3739 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3740 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3744 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3745 struct tevent_context *ev,
3746 struct smbXcli_conn *conn,
3747 uint32_t timeout_msec,
3748 enum protocol_types min_protocol,
3749 enum protocol_types max_protocol)
3751 struct tevent_req *req, *subreq;
3752 struct smbXcli_negprot_state *state;
3754 req = tevent_req_create(mem_ctx, &state,
3755 struct smbXcli_negprot_state);
3761 state->timeout_msec = timeout_msec;
3762 state->min_protocol = min_protocol;
3763 state->max_protocol = max_protocol;
3765 if (min_protocol == PROTOCOL_NONE) {
3766 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3767 return tevent_req_post(req, ev);
3770 if (max_protocol == PROTOCOL_NONE) {
3771 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3772 return tevent_req_post(req, ev);
3775 if (min_protocol > max_protocol) {
3776 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3777 return tevent_req_post(req, ev);
3780 if ((min_protocol < PROTOCOL_SMB2_02) &&
3781 (max_protocol < PROTOCOL_SMB2_02)) {
3785 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3787 subreq = smbXcli_negprot_smb1_subreq(state);
3788 if (tevent_req_nomem(subreq, req)) {
3789 return tevent_req_post(req, ev);
3791 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3795 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3796 (max_protocol >= PROTOCOL_SMB2_02)) {
3800 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3802 subreq = smbXcli_negprot_smb2_subreq(state);
3803 if (tevent_req_nomem(subreq, req)) {
3804 return tevent_req_post(req, ev);
3806 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3811 * We send an SMB1 negprot with the SMB2 dialects
3812 * and expect a SMB1 or a SMB2 response.
3814 * smbXcli_negprot_dispatch_incoming() will fix the
3815 * callback to match protocol of the response.
3817 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3819 subreq = smbXcli_negprot_smb1_subreq(state);
3820 if (tevent_req_nomem(subreq, req)) {
3821 return tevent_req_post(req, ev);
3823 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3827 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3829 struct tevent_req *req =
3830 tevent_req_callback_data(subreq,
3835 * we just want the low level error
3837 status = tevent_req_simple_recv_ntstatus(subreq);
3838 TALLOC_FREE(subreq);
3839 if (tevent_req_nterror(req, status)) {
3843 /* this should never happen */
3844 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3847 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3850 DATA_BLOB bytes = data_blob_null;
3854 /* setup the protocol strings */
3855 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3859 if (smb1cli_prots[i].proto < state->min_protocol) {
3863 if (smb1cli_prots[i].proto > state->max_protocol) {
3867 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3873 * We now it is already ascii and
3874 * we want NULL termination.
3876 ok = data_blob_append(state, &bytes,
3877 smb1cli_prots[i].smb1_name,
3878 strlen(smb1cli_prots[i].smb1_name)+1);
3884 smb1cli_req_flags(state->max_protocol,
3885 state->conn->smb1.client.capabilities,
3890 return smb1cli_req_send(state, state->ev, state->conn,
3894 state->timeout_msec,
3895 0xFFFE, 0, NULL, /* pid, tid, session */
3896 0, NULL, /* wct, vwv */
3897 bytes.length, bytes.data);
3900 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3902 struct tevent_req *req =
3903 tevent_req_callback_data(subreq,
3905 struct smbXcli_negprot_state *state =
3906 tevent_req_data(req,
3907 struct smbXcli_negprot_state);
3908 struct smbXcli_conn *conn = state->conn;
3909 struct iovec *recv_iov = NULL;
3918 size_t num_prots = 0;
3920 uint32_t client_capabilities = conn->smb1.client.capabilities;
3921 uint32_t both_capabilities;
3922 uint32_t server_capabilities = 0;
3923 uint32_t capabilities;
3924 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3925 uint32_t server_max_xmit = 0;
3927 uint32_t server_max_mux = 0;
3928 uint16_t server_security_mode = 0;
3929 uint32_t server_session_key = 0;
3930 bool server_readbraw = false;
3931 bool server_writebraw = false;
3932 bool server_lockread = false;
3933 bool server_writeunlock = false;
3934 struct GUID server_guid = GUID_zero();
3935 DATA_BLOB server_gss_blob = data_blob_null;
3936 uint8_t server_challenge[8];
3937 char *server_workgroup = NULL;
3938 char *server_name = NULL;
3939 int server_time_zone = 0;
3940 NTTIME server_system_time = 0;
3941 static const struct smb1cli_req_expected_response expected[] = {
3943 .status = NT_STATUS_OK,
3944 .wct = 0x11, /* NT1 */
3947 .status = NT_STATUS_OK,
3948 .wct = 0x0D, /* LM */
3951 .status = NT_STATUS_OK,
3952 .wct = 0x01, /* CORE */
3956 ZERO_STRUCT(server_challenge);
3958 status = smb1cli_req_recv(subreq, state,
3963 NULL, /* pvwv_offset */
3966 NULL, /* pbytes_offset */
3968 expected, ARRAY_SIZE(expected));
3969 TALLOC_FREE(subreq);
3970 if (tevent_req_nterror(req, status)) {
3974 flags = CVAL(inhdr, HDR_FLG);
3976 protnum = SVAL(vwv, 0);
3978 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3979 if (smb1cli_prots[i].proto < state->min_protocol) {
3983 if (smb1cli_prots[i].proto > state->max_protocol) {
3987 if (protnum != num_prots) {
3992 conn->protocol = smb1cli_prots[i].proto;
3996 if (conn->protocol == PROTOCOL_NONE) {
3997 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4001 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4002 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4003 "and the selected protocol level doesn't support it.\n"));
4004 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4008 if (flags & FLAG_SUPPORT_LOCKREAD) {
4009 server_lockread = true;
4010 server_writeunlock = true;
4013 if (conn->protocol >= PROTOCOL_NT1) {
4014 const char *client_signing = NULL;
4015 bool server_mandatory = false;
4016 bool server_allowed = false;
4017 const char *server_signing = NULL;
4022 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4027 server_security_mode = CVAL(vwv + 1, 0);
4028 server_max_mux = SVAL(vwv + 1, 1);
4029 server_max_xmit = IVAL(vwv + 3, 1);
4030 server_session_key = IVAL(vwv + 7, 1);
4031 server_time_zone = SVALS(vwv + 15, 1);
4032 server_time_zone *= 60;
4033 /* this time arrives in real GMT */
4034 server_system_time = BVAL(vwv + 11, 1);
4035 server_capabilities = IVAL(vwv + 9, 1);
4037 key_len = CVAL(vwv + 16, 1);
4039 if (server_capabilities & CAP_RAW_MODE) {
4040 server_readbraw = true;
4041 server_writebraw = true;
4043 if (server_capabilities & CAP_LOCK_AND_READ) {
4044 server_lockread = true;
4047 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4048 DATA_BLOB blob1, blob2;
4050 if (num_bytes < 16) {
4051 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4055 blob1 = data_blob_const(bytes, 16);
4056 status = GUID_from_data_blob(&blob1, &server_guid);
4057 if (tevent_req_nterror(req, status)) {
4061 blob1 = data_blob_const(bytes+16, num_bytes-16);
4062 blob2 = data_blob_dup_talloc(state, blob1);
4063 if (blob1.length > 0 &&
4064 tevent_req_nomem(blob2.data, req)) {
4067 server_gss_blob = blob2;
4069 DATA_BLOB blob1, blob2;
4071 if (num_bytes < key_len) {
4072 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4076 if (key_len != 0 && key_len != 8) {
4077 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4082 memcpy(server_challenge, bytes, 8);
4085 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4086 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4087 if (blob1.length > 0) {
4090 len = utf16_len_n(blob1.data,
4094 ok = convert_string_talloc(state,
4102 status = map_nt_error_from_unix_common(errno);
4103 tevent_req_nterror(req, status);
4108 blob2.data += blob1.length;
4109 blob2.length -= blob1.length;
4110 if (blob2.length > 0) {
4113 len = utf16_len_n(blob1.data,
4117 ok = convert_string_talloc(state,
4125 status = map_nt_error_from_unix_common(errno);
4126 tevent_req_nterror(req, status);
4132 client_signing = "disabled";
4133 if (conn->allow_signing) {
4134 client_signing = "allowed";
4136 if (conn->mandatory_signing) {
4137 client_signing = "required";
4140 server_signing = "not supported";
4141 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4142 server_signing = "supported";
4143 server_allowed = true;
4144 } else if (conn->mandatory_signing) {
4146 * We have mandatory signing as client
4147 * lets assume the server will look at our
4148 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4149 * flag in the session setup
4151 server_signing = "not announced";
4152 server_allowed = true;
4154 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4155 server_signing = "required";
4156 server_mandatory = true;
4159 ok = smb_signing_set_negotiated(conn->smb1.signing,
4163 DEBUG(1,("cli_negprot: SMB signing is required, "
4164 "but client[%s] and server[%s] mismatch\n",
4165 client_signing, server_signing));
4166 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4170 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4176 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4180 server_security_mode = SVAL(vwv + 1, 0);
4181 server_max_xmit = SVAL(vwv + 2, 0);
4182 server_max_mux = SVAL(vwv + 3, 0);
4183 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4184 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4185 server_session_key = IVAL(vwv + 6, 0);
4186 server_time_zone = SVALS(vwv + 10, 0);
4187 server_time_zone *= 60;
4188 /* this time is converted to GMT by make_unix_date */
4189 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4190 unix_to_nt_time(&server_system_time, t);
4191 key_len = SVAL(vwv + 11, 0);
4193 if (num_bytes < key_len) {
4194 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4198 if (key_len != 0 && key_len != 8) {
4199 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4204 memcpy(server_challenge, bytes, 8);
4207 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4208 if (blob1.length > 0) {
4212 len = utf16_len_n(blob1.data,
4216 ok = convert_string_talloc(state,
4224 status = map_nt_error_from_unix_common(errno);
4225 tevent_req_nterror(req, status);
4231 /* the old core protocol */
4232 server_time_zone = get_time_zone(time(NULL));
4233 server_max_xmit = 1024;
4237 if (server_max_xmit < 1024) {
4238 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4242 if (server_max_mux < 1) {
4243 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4248 * Now calculate the negotiated capabilities
4249 * based on the mask for:
4250 * - client only flags
4251 * - flags used in both directions
4252 * - server only flags
4254 both_capabilities = client_capabilities & server_capabilities;
4255 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4256 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4257 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4259 max_xmit = MIN(client_max_xmit, server_max_xmit);
4261 conn->smb1.server.capabilities = server_capabilities;
4262 conn->smb1.capabilities = capabilities;
4264 conn->smb1.server.max_xmit = server_max_xmit;
4265 conn->smb1.max_xmit = max_xmit;
4267 conn->smb1.server.max_mux = server_max_mux;
4269 conn->smb1.server.security_mode = server_security_mode;
4271 conn->smb1.server.readbraw = server_readbraw;
4272 conn->smb1.server.writebraw = server_writebraw;
4273 conn->smb1.server.lockread = server_lockread;
4274 conn->smb1.server.writeunlock = server_writeunlock;
4276 conn->smb1.server.session_key = server_session_key;
4278 talloc_steal(conn, server_gss_blob.data);
4279 conn->smb1.server.gss_blob = server_gss_blob;
4280 conn->smb1.server.guid = server_guid;
4281 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4282 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4283 conn->smb1.server.name = talloc_move(conn, &server_name);
4285 conn->smb1.server.time_zone = server_time_zone;
4286 conn->smb1.server.system_time = server_system_time;
4288 tevent_req_done(req);
4291 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4295 uint16_t dialect_count = 0;
4297 buf = state->smb2.dyn;
4298 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4299 if (smb2cli_prots[i].proto < state->min_protocol) {
4303 if (smb2cli_prots[i].proto > state->max_protocol) {
4307 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4311 buf = state->smb2.fixed;
4313 SSVAL(buf, 2, dialect_count);
4314 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4315 SSVAL(buf, 6, 0); /* Reserved */
4316 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4317 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4319 SIVAL(buf, 8, 0); /* Capabilities */
4321 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4325 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4327 if (!NT_STATUS_IS_OK(status)) {
4330 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4332 memset(buf+12, 0, 16); /* ClientGuid */
4334 SBVAL(buf, 28, 0); /* ClientStartTime */
4336 return smb2cli_req_send(state, state->ev,
4337 state->conn, SMB2_OP_NEGPROT,
4339 state->timeout_msec,
4340 NULL, NULL, /* tcon, session */
4341 state->smb2.fixed, sizeof(state->smb2.fixed),
4342 state->smb2.dyn, dialect_count*2,
4343 UINT16_MAX); /* max_dyn_len */
4346 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4348 struct tevent_req *req =
4349 tevent_req_callback_data(subreq,
4351 struct smbXcli_negprot_state *state =
4352 tevent_req_data(req,
4353 struct smbXcli_negprot_state);
4354 struct smbXcli_conn *conn = state->conn;
4355 size_t security_offset, security_length;
4361 uint16_t dialect_revision;
4362 static const struct smb2cli_req_expected_response expected[] = {
4364 .status = NT_STATUS_OK,
4369 status = smb2cli_req_recv(subreq, state, &iov,
4370 expected, ARRAY_SIZE(expected));
4371 TALLOC_FREE(subreq);
4372 if (tevent_req_nterror(req, status)) {
4376 body = (uint8_t *)iov[1].iov_base;
4378 dialect_revision = SVAL(body, 4);
4380 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4381 if (smb2cli_prots[i].proto < state->min_protocol) {
4385 if (smb2cli_prots[i].proto > state->max_protocol) {
4389 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4393 conn->protocol = smb2cli_prots[i].proto;
4397 if (conn->protocol == PROTOCOL_NONE) {
4398 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4399 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4403 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4404 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4408 /* make sure we do not loop forever */
4409 state->min_protocol = PROTOCOL_SMB2_02;
4412 * send a SMB2 negprot, in order to negotiate
4415 subreq = smbXcli_negprot_smb2_subreq(state);
4416 if (tevent_req_nomem(subreq, req)) {
4419 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4423 conn->smb2.server.security_mode = SVAL(body, 2);
4425 blob = data_blob_const(body + 8, 16);
4426 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4427 if (tevent_req_nterror(req, status)) {
4431 conn->smb2.server.capabilities = IVAL(body, 24);
4432 conn->smb2.server.max_trans_size= IVAL(body, 28);
4433 conn->smb2.server.max_read_size = IVAL(body, 32);
4434 conn->smb2.server.max_write_size= IVAL(body, 36);
4435 conn->smb2.server.system_time = BVAL(body, 40);
4436 conn->smb2.server.start_time = BVAL(body, 48);
4438 security_offset = SVAL(body, 56);
4439 security_length = SVAL(body, 58);
4441 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4442 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4446 if (security_length > iov[2].iov_len) {
4447 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4451 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4454 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4458 tevent_req_done(req);
4461 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4462 TALLOC_CTX *tmp_mem,
4465 size_t num_pending = talloc_array_length(conn->pending);
4466 struct tevent_req *subreq;
4467 struct smbXcli_req_state *substate;
4468 struct tevent_req *req;
4469 uint32_t protocol_magic;
4470 size_t inbuf_len = smb_len_nbt(inbuf);
4472 if (num_pending != 1) {
4473 return NT_STATUS_INTERNAL_ERROR;
4476 if (inbuf_len < 4) {
4477 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4480 subreq = conn->pending[0];
4481 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4482 req = tevent_req_callback_data(subreq, struct tevent_req);
4484 protocol_magic = IVAL(inbuf, 4);
4486 switch (protocol_magic) {
4488 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4489 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4490 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4493 if (substate->smb2.recv_iov == NULL) {
4495 * For the SMB1 negprot we have move it.
4497 substate->smb2.recv_iov = substate->smb1.recv_iov;
4498 substate->smb1.recv_iov = NULL;
4502 * we got an SMB2 answer, which consumed sequence number 0
4503 * so we need to use 1 as the next one.
4505 * we also need to set the current credits to 0
4506 * as we consumed the initial one. The SMB2 answer
4507 * hopefully grant us a new credit.
4510 conn->smb2.cur_credits = 0;
4511 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4512 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4513 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4516 DEBUG(10, ("Got non-SMB PDU\n"));
4517 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4520 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4522 return tevent_req_simple_recv_ntstatus(req);
4525 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4526 uint32_t timeout_msec,
4527 enum protocol_types min_protocol,
4528 enum protocol_types max_protocol)
4530 TALLOC_CTX *frame = talloc_stackframe();
4531 struct tevent_context *ev;
4532 struct tevent_req *req;
4533 NTSTATUS status = NT_STATUS_NO_MEMORY;
4536 if (smbXcli_conn_has_async_calls(conn)) {
4538 * Can't use sync call while an async call is in flight
4540 status = NT_STATUS_INVALID_PARAMETER_MIX;
4543 ev = samba_tevent_context_init(frame);
4547 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4548 min_protocol, max_protocol);
4552 ok = tevent_req_poll(req, ev);
4554 status = map_nt_error_from_unix_common(errno);
4557 status = smbXcli_negprot_recv(req);
4563 static int smbXcli_session_destructor(struct smbXcli_session *session)
4565 if (session->conn == NULL) {
4569 DLIST_REMOVE(session->conn->sessions, session);
4573 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4574 struct smbXcli_conn *conn)
4576 struct smbXcli_session *session;
4578 session = talloc_zero(mem_ctx, struct smbXcli_session);
4579 if (session == NULL) {
4582 session->smb2 = talloc_zero(session, struct smb2cli_session);
4583 if (session->smb2 == NULL) {
4584 talloc_free(session);
4587 talloc_set_destructor(session, smbXcli_session_destructor);
4589 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4590 session->conn = conn;
4595 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4596 struct smbXcli_session *src)
4598 struct smbXcli_session *session;
4600 session = talloc_zero(mem_ctx, struct smbXcli_session);
4601 if (session == NULL) {
4604 session->smb2 = talloc_zero(session, struct smb2cli_session);
4605 if (session->smb2 == NULL) {
4606 talloc_free(session);
4610 session->conn = src->conn;
4611 *session->smb2 = *src->smb2;
4612 session->smb2_channel = src->smb2_channel;
4613 session->disconnect_expired = src->disconnect_expired;
4615 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4616 talloc_set_destructor(session, smbXcli_session_destructor);
4622 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4623 TALLOC_CTX *mem_ctx,
4626 const DATA_BLOB *application_key;
4628 *key = data_blob_null;
4630 if (session->conn == NULL) {
4631 return NT_STATUS_NO_USER_SESSION_KEY;
4634 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4635 application_key = &session->smb2->application_key;
4637 application_key = &session->smb1.application_key;
4640 if (application_key->length == 0) {
4641 return NT_STATUS_NO_USER_SESSION_KEY;
4644 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4645 if (key->data == NULL) {
4646 return NT_STATUS_NO_MEMORY;
4649 return NT_STATUS_OK;
4652 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4654 session->disconnect_expired = true;
4657 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4659 return session->smb1.session_id;
4662 void smb1cli_session_set_id(struct smbXcli_session *session,
4663 uint16_t session_id)
4665 session->smb1.session_id = session_id;
4668 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4669 const DATA_BLOB _session_key)
4671 struct smbXcli_conn *conn = session->conn;
4672 uint8_t session_key[16];
4675 return NT_STATUS_INVALID_PARAMETER_MIX;
4678 if (session->smb1.application_key.length != 0) {
4680 * TODO: do not allow this...
4682 * return NT_STATUS_INVALID_PARAMETER_MIX;
4684 data_blob_clear_free(&session->smb1.application_key);
4685 session->smb1.protected_key = false;
4688 if (_session_key.length == 0) {
4689 return NT_STATUS_OK;
4692 ZERO_STRUCT(session_key);
4693 memcpy(session_key, _session_key.data,
4694 MIN(_session_key.length, sizeof(session_key)));
4696 session->smb1.application_key = data_blob_talloc(session,
4698 sizeof(session_key));
4699 ZERO_STRUCT(session_key);
4700 if (session->smb1.application_key.data == NULL) {
4701 return NT_STATUS_NO_MEMORY;
4704 session->smb1.protected_key = false;
4706 return NT_STATUS_OK;
4709 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4711 if (session->smb1.protected_key) {
4712 /* already protected */
4713 return NT_STATUS_OK;
4716 if (session->smb1.application_key.length != 16) {
4717 return NT_STATUS_INVALID_PARAMETER_MIX;
4720 smb_key_derivation(session->smb1.application_key.data,
4721 session->smb1.application_key.length,
4722 session->smb1.application_key.data);
4724 session->smb1.protected_key = true;
4726 return NT_STATUS_OK;
4729 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4731 struct smbXcli_conn *conn = session->conn;
4732 uint8_t security_mode = 0;
4735 return security_mode;
4738 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4739 if (conn->mandatory_signing) {
4740 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4743 return security_mode;
4746 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4748 return session->smb2->session_id;
4751 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4753 return session->smb2->session_flags;
4756 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4757 uint64_t session_id,
4758 uint16_t session_flags)
4760 session->smb2->session_id = session_id;
4761 session->smb2->session_flags = session_flags;
4764 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4766 session->smb2->channel_sequence += 1;
4769 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4770 const DATA_BLOB _session_key,
4771 const struct iovec *recv_iov)
4773 struct smbXcli_conn *conn = session->conn;
4774 uint16_t no_sign_flags;
4775 uint8_t session_key[16];
4776 bool check_signature = true;
4781 return NT_STATUS_INVALID_PARAMETER_MIX;
4784 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
4785 return NT_STATUS_INVALID_PARAMETER_MIX;
4788 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4790 if (session->smb2->session_flags & no_sign_flags) {
4791 session->smb2->should_sign = false;
4792 return NT_STATUS_OK;
4795 if (session->smb2->signing_key.length != 0) {
4796 return NT_STATUS_INVALID_PARAMETER_MIX;
4799 ZERO_STRUCT(session_key);
4800 memcpy(session_key, _session_key.data,
4801 MIN(_session_key.length, sizeof(session_key)));
4803 session->smb2->signing_key = data_blob_talloc(session,
4805 sizeof(session_key));
4806 if (session->smb2->signing_key.data == NULL) {
4807 ZERO_STRUCT(session_key);
4808 return NT_STATUS_NO_MEMORY;
4811 if (conn->protocol >= PROTOCOL_SMB2_24) {
4812 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4813 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4815 smb2_key_derivation(session_key, sizeof(session_key),
4816 label.data, label.length,
4817 context.data, context.length,
4818 session->smb2->signing_key.data);
4821 session->smb2->encryption_key = data_blob_dup_talloc(session,
4822 session->smb2->signing_key);
4823 if (session->smb2->encryption_key.data == NULL) {
4824 ZERO_STRUCT(session_key);
4825 return NT_STATUS_NO_MEMORY;
4828 if (conn->protocol >= PROTOCOL_SMB2_24) {
4829 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4830 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4832 smb2_key_derivation(session_key, sizeof(session_key),
4833 label.data, label.length,
4834 context.data, context.length,
4835 session->smb2->encryption_key.data);
4838 session->smb2->decryption_key = data_blob_dup_talloc(session,
4839 session->smb2->signing_key);
4840 if (session->smb2->decryption_key.data == NULL) {
4841 ZERO_STRUCT(session_key);
4842 return NT_STATUS_NO_MEMORY;
4845 if (conn->protocol >= PROTOCOL_SMB2_24) {
4846 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4847 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4849 smb2_key_derivation(session_key, sizeof(session_key),
4850 label.data, label.length,
4851 context.data, context.length,
4852 session->smb2->decryption_key.data);
4855 session->smb2->application_key = data_blob_dup_talloc(session,
4856 session->smb2->signing_key);
4857 if (session->smb2->application_key.data == NULL) {
4858 ZERO_STRUCT(session_key);
4859 return NT_STATUS_NO_MEMORY;
4862 if (conn->protocol >= PROTOCOL_SMB2_24) {
4863 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4864 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4866 smb2_key_derivation(session_key, sizeof(session_key),
4867 label.data, label.length,
4868 context.data, context.length,
4869 session->smb2->application_key.data);
4871 ZERO_STRUCT(session_key);
4873 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4874 session->smb2->signing_key);
4875 if (session->smb2_channel.signing_key.data == NULL) {
4876 return NT_STATUS_NO_MEMORY;
4879 check_signature = conn->mandatory_signing;
4881 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
4882 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
4884 * Sadly some vendors don't sign the
4885 * final SMB2 session setup response
4887 * At least Windows and Samba are always doing this
4888 * if there's a session key available.
4890 * We only check the signature if it's mandatory
4891 * or SMB2_HDR_FLAG_SIGNED is provided.
4893 check_signature = true;
4896 if (check_signature) {
4897 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4898 session->conn->protocol,
4900 if (!NT_STATUS_IS_OK(status)) {
4905 session->smb2->should_sign = false;
4906 session->smb2->should_encrypt = false;
4908 if (conn->desire_signing) {
4909 session->smb2->should_sign = true;
4912 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4913 session->smb2->should_sign = true;
4916 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4917 session->smb2->should_encrypt = true;
4920 if (conn->protocol < PROTOCOL_SMB2_24) {
4921 session->smb2->should_encrypt = false;
4924 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4925 session->smb2->should_encrypt = false;
4928 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4929 sizeof(session->smb2->nonce_high));
4930 session->smb2->nonce_low = 1;
4932 return NT_STATUS_OK;
4935 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4936 struct smbXcli_session *session1,
4937 struct smbXcli_conn *conn,
4938 struct smbXcli_session **_session2)
4940 struct smbXcli_session *session2;
4942 if (session1->smb2->signing_key.length == 0) {
4943 return NT_STATUS_INVALID_PARAMETER_MIX;
4947 return NT_STATUS_INVALID_PARAMETER_MIX;
4950 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4951 if (session2 == NULL) {
4952 return NT_STATUS_NO_MEMORY;
4954 session2->smb2 = talloc_reference(session2, session1->smb2);
4955 if (session2->smb2 == NULL) {
4956 talloc_free(session2);
4957 return NT_STATUS_NO_MEMORY;
4960 talloc_set_destructor(session2, smbXcli_session_destructor);
4961 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4962 session2->conn = conn;
4964 *_session2 = session2;
4965 return NT_STATUS_OK;
4968 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4969 const DATA_BLOB _channel_key,
4970 const struct iovec *recv_iov)
4972 struct smbXcli_conn *conn = session->conn;
4973 uint8_t channel_key[16];
4977 return NT_STATUS_INVALID_PARAMETER_MIX;
4980 if (session->smb2_channel.signing_key.length != 0) {
4981 return NT_STATUS_INVALID_PARAMETER_MIX;
4984 ZERO_STRUCT(channel_key);
4985 memcpy(channel_key, _channel_key.data,
4986 MIN(_channel_key.length, sizeof(channel_key)));
4988 session->smb2_channel.signing_key = data_blob_talloc(session,
4990 sizeof(channel_key));
4991 if (session->smb2_channel.signing_key.data == NULL) {
4992 ZERO_STRUCT(channel_key);
4993 return NT_STATUS_NO_MEMORY;
4996 if (conn->protocol >= PROTOCOL_SMB2_24) {
4997 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4998 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
5000 smb2_key_derivation(channel_key, sizeof(channel_key),
5001 label.data, label.length,
5002 context.data, context.length,
5003 session->smb2_channel.signing_key.data);
5005 ZERO_STRUCT(channel_key);
5007 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5008 session->conn->protocol,
5010 if (!NT_STATUS_IS_OK(status)) {
5014 return NT_STATUS_OK;
5017 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
5019 if (session->smb2->should_encrypt) {
5020 return NT_STATUS_OK;
5023 if (session->conn->protocol < PROTOCOL_SMB2_24) {
5024 return NT_STATUS_NOT_SUPPORTED;
5027 if (!(session->conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
5028 return NT_STATUS_NOT_SUPPORTED;
5031 if (session->smb2->signing_key.data == NULL) {
5032 return NT_STATUS_NOT_SUPPORTED;
5034 session->smb2->should_encrypt = true;
5035 return NT_STATUS_OK;
5038 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
5040 struct smbXcli_tcon *tcon;
5042 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
5050 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
5051 uint32_t fs_attributes)
5053 tcon->fs_attributes = fs_attributes;
5056 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
5058 return tcon->fs_attributes;
5061 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
5067 if (tcon->is_smb1) {
5068 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
5075 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
5082 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
5084 return tcon->smb1.tcon_id;
5087 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
5089 tcon->is_smb1 = true;
5090 tcon->smb1.tcon_id = tcon_id;
5093 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5095 uint16_t optional_support,
5096 uint32_t maximal_access,
5097 uint32_t guest_maximal_access,
5098 const char *service,
5099 const char *fs_type)
5101 tcon->is_smb1 = true;
5102 tcon->fs_attributes = 0;
5103 tcon->smb1.tcon_id = tcon_id;
5104 tcon->smb1.optional_support = optional_support;
5105 tcon->smb1.maximal_access = maximal_access;
5106 tcon->smb1.guest_maximal_access = guest_maximal_access;
5108 TALLOC_FREE(tcon->smb1.service);
5109 tcon->smb1.service = talloc_strdup(tcon, service);
5110 if (service != NULL && tcon->smb1.service == NULL) {
5114 TALLOC_FREE(tcon->smb1.fs_type);
5115 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5116 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5123 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5125 return tcon->smb2.tcon_id;
5128 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5130 return tcon->smb2.capabilities;
5133 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5134 struct smbXcli_session *session,
5138 uint32_t capabilities,
5139 uint32_t maximal_access)
5141 tcon->is_smb1 = false;
5142 tcon->fs_attributes = 0;
5143 tcon->smb2.tcon_id = tcon_id;
5144 tcon->smb2.type = type;
5145 tcon->smb2.flags = flags;
5146 tcon->smb2.capabilities = capabilities;
5147 tcon->smb2.maximal_access = maximal_access;
5149 tcon->smb2.should_encrypt = false;
5151 if (session == NULL) {
5155 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5157 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5158 tcon->smb2.should_encrypt = true;
5162 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
5164 return tcon->smb2.should_encrypt;