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;
146 struct smbXcli_session {
147 struct smbXcli_session *prev, *next;
148 struct smbXcli_conn *conn;
152 DATA_BLOB application_key;
156 struct smb2cli_session *smb2;
159 DATA_BLOB signing_key;
163 * this should be a short term hack
164 * until the upper layers have implemented
167 bool disconnect_expired;
170 struct smbXcli_tcon {
172 uint32_t fs_attributes;
176 uint16_t optional_support;
177 uint32_t maximal_access;
178 uint32_t guest_maximal_access;
187 uint32_t capabilities;
188 uint32_t maximal_access;
194 struct smbXcli_req_state {
195 struct tevent_context *ev;
196 struct smbXcli_conn *conn;
197 struct smbXcli_session *session; /* maybe NULL */
198 struct smbXcli_tcon *tcon; /* maybe NULL */
200 uint8_t length_hdr[4];
207 /* Space for the header including the wct */
208 uint8_t hdr[HDR_VWV];
211 * For normal requests, smb1cli_req_send chooses a mid.
212 * SecondaryV trans requests need to use the mid of the primary
213 * request, so we need a place to store it.
214 * Assume it is set if != 0.
219 uint8_t bytecount_buf[2];
221 #define MAX_SMB_IOV 10
222 /* length_hdr, hdr, words, byte_count, buffers */
223 struct iovec iov[1 + 3 + MAX_SMB_IOV];
228 struct tevent_req **chained_requests;
231 NTSTATUS recv_status;
232 /* always an array of 3 talloc elements */
233 struct iovec *recv_iov;
237 const uint8_t *fixed;
242 uint8_t transform[SMB2_TF_HDR_SIZE];
243 uint8_t hdr[SMB2_HDR_BODY];
244 uint8_t pad[7]; /* padding space for compounding */
247 * always an array of 3 talloc elements
248 * (without a SMB2_TRANSFORM header!)
252 struct iovec *recv_iov;
255 * the expected max for the response dyn_len
257 uint32_t max_dyn_len;
259 uint16_t credit_charge;
263 uint64_t encryption_session_id;
265 bool signing_skipped;
271 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
274 * NT_STATUS_OK, means we do not notify the callers
276 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
278 while (conn->sessions) {
279 conn->sessions->conn = NULL;
280 DLIST_REMOVE(conn->sessions, conn->sessions);
283 if (conn->smb1.trans_enc) {
284 TALLOC_FREE(conn->smb1.trans_enc);
290 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
292 const char *remote_name,
293 enum smb_signing_setting signing_state,
294 uint32_t smb1_capabilities,
295 struct GUID *client_guid,
296 uint32_t smb2_capabilities)
298 struct smbXcli_conn *conn = NULL;
300 struct sockaddr *sa = NULL;
304 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
310 conn->write_fd = dup(fd);
311 if (conn->write_fd == -1) {
315 conn->remote_name = talloc_strdup(conn, remote_name);
316 if (conn->remote_name == NULL) {
321 ss = (void *)&conn->local_ss;
322 sa = (struct sockaddr *)ss;
323 sa_length = sizeof(conn->local_ss);
324 ret = getsockname(fd, sa, &sa_length);
328 ss = (void *)&conn->remote_ss;
329 sa = (struct sockaddr *)ss;
330 sa_length = sizeof(conn->remote_ss);
331 ret = getpeername(fd, sa, &sa_length);
336 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
337 if (conn->outgoing == NULL) {
340 conn->pending = NULL;
342 conn->protocol = PROTOCOL_NONE;
344 switch (signing_state) {
345 case SMB_SIGNING_OFF:
347 conn->allow_signing = false;
348 conn->desire_signing = false;
349 conn->mandatory_signing = false;
351 case SMB_SIGNING_DEFAULT:
352 case SMB_SIGNING_IF_REQUIRED:
353 /* if the server requires it */
354 conn->allow_signing = true;
355 conn->desire_signing = false;
356 conn->mandatory_signing = false;
358 case SMB_SIGNING_REQUIRED:
360 conn->allow_signing = true;
361 conn->desire_signing = true;
362 conn->mandatory_signing = true;
366 conn->smb1.client.capabilities = smb1_capabilities;
367 conn->smb1.client.max_xmit = UINT16_MAX;
369 conn->smb1.capabilities = conn->smb1.client.capabilities;
370 conn->smb1.max_xmit = 1024;
374 /* initialise signing */
375 conn->smb1.signing = smb_signing_init(conn,
377 conn->desire_signing,
378 conn->mandatory_signing);
379 if (!conn->smb1.signing) {
383 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
384 if (conn->mandatory_signing) {
385 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
388 conn->smb2.client.guid = *client_guid;
390 conn->smb2.client.capabilities = smb2_capabilities;
392 conn->smb2.cur_credits = 1;
393 conn->smb2.max_credits = 0;
395 talloc_set_destructor(conn, smbXcli_conn_destructor);
399 if (conn->write_fd != -1) {
400 close(conn->write_fd);
406 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
412 if (conn->read_fd == -1) {
419 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
421 return conn->protocol;
424 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
426 if (conn->protocol >= PROTOCOL_SMB2_02) {
430 if (conn->smb1.capabilities & CAP_UNICODE) {
437 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
439 set_socket_options(conn->read_fd, options);
442 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
444 return &conn->local_ss;
447 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
449 return &conn->remote_ss;
452 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
454 return conn->remote_name;
457 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
459 if (conn->protocol >= PROTOCOL_SMB2_02) {
466 return conn->smb1.server.max_mux;
469 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
471 if (conn->protocol >= PROTOCOL_SMB2_02) {
472 return conn->smb2.server.system_time;
475 return conn->smb1.server.system_time;
478 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
480 if (conn->protocol >= PROTOCOL_SMB2_02) {
481 return &conn->smb2.server.gss_blob;
484 return &conn->smb1.server.gss_blob;
487 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
489 if (conn->protocol >= PROTOCOL_SMB2_02) {
490 return &conn->smb2.server.guid;
493 return &conn->smb1.server.guid;
496 struct smbXcli_conn_samba_suicide_state {
497 struct smbXcli_conn *conn;
502 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
504 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
505 struct tevent_context *ev,
506 struct smbXcli_conn *conn,
509 struct tevent_req *req, *subreq;
510 struct smbXcli_conn_samba_suicide_state *state;
512 req = tevent_req_create(mem_ctx, &state,
513 struct smbXcli_conn_samba_suicide_state);
518 SIVAL(state->buf, 4, 0x74697865);
519 SCVAL(state->buf, 8, exitcode);
520 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
522 state->iov.iov_base = state->buf;
523 state->iov.iov_len = sizeof(state->buf);
525 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
526 false, &state->iov, 1);
527 if (tevent_req_nomem(subreq, req)) {
528 return tevent_req_post(req, ev);
530 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
534 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
536 struct tevent_req *req = tevent_req_callback_data(
537 subreq, struct tevent_req);
538 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
539 req, struct smbXcli_conn_samba_suicide_state);
543 nwritten = writev_recv(subreq, &err);
545 if (nwritten == -1) {
546 NTSTATUS status = map_nt_error_from_unix_common(err);
547 smbXcli_conn_disconnect(state->conn, status);
550 tevent_req_done(req);
553 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
555 return tevent_req_simple_recv_ntstatus(req);
558 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
561 TALLOC_CTX *frame = talloc_stackframe();
562 struct tevent_context *ev;
563 struct tevent_req *req;
564 NTSTATUS status = NT_STATUS_NO_MEMORY;
567 if (smbXcli_conn_has_async_calls(conn)) {
569 * Can't use sync call while an async call is in flight
571 status = NT_STATUS_INVALID_PARAMETER_MIX;
574 ev = samba_tevent_context_init(frame);
578 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
582 ok = tevent_req_poll(req, ev);
584 status = map_nt_error_from_unix_common(errno);
587 status = smbXcli_conn_samba_suicide_recv(req);
593 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
595 return conn->smb1.capabilities;
598 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
600 return conn->smb1.max_xmit;
603 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
606 uint16_t possible = conn->smb1.server.max_mux;
608 pending = tevent_queue_length(conn->outgoing);
609 if (pending >= possible) {
612 pending += talloc_array_length(conn->pending);
613 if (pending >= possible) {
620 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
622 return conn->smb1.server.session_key;
625 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
627 return conn->smb1.server.challenge;
630 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
632 return conn->smb1.server.security_mode;
635 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
637 return conn->smb1.server.readbraw;
640 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
642 return conn->smb1.server.writebraw;
645 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
647 return conn->smb1.server.lockread;
650 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
652 return conn->smb1.server.writeunlock;
655 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
657 return conn->smb1.server.time_zone;
660 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
661 const DATA_BLOB user_session_key,
662 const DATA_BLOB response)
664 return smb_signing_activate(conn->smb1.signing,
669 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
670 const uint8_t *buf, uint32_t seqnum)
672 const uint8_t *hdr = buf + NBT_HDR_SIZE;
673 size_t len = smb_len_nbt(buf);
675 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
678 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
680 return smb_signing_is_active(conn->smb1.signing);
683 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
684 struct smb_trans_enc_state *es)
686 /* Replace the old state, if any. */
687 if (conn->smb1.trans_enc) {
688 TALLOC_FREE(conn->smb1.trans_enc);
690 conn->smb1.trans_enc = es;
693 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
695 return common_encryption_on(conn->smb1.trans_enc);
699 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
701 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
702 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
704 if (NT_STATUS_IS_OK(status)) {
708 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
712 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
716 * Is the SMB command able to hold an AND_X successor
717 * @param[in] cmd The SMB command in question
718 * @retval Can we add a chained request after "cmd"?
720 bool smb1cli_is_andx_req(uint8_t cmd)
740 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
742 size_t num_pending = talloc_array_length(conn->pending);
745 if (conn->protocol == PROTOCOL_NONE) {
747 * This is what windows sends on the SMB1 Negprot request
748 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
756 result = conn->smb1.mid++;
757 if ((result == 0) || (result == 0xffff)) {
761 for (i=0; i<num_pending; i++) {
762 if (result == smb1cli_req_mid(conn->pending[i])) {
767 if (i == num_pending) {
773 void smbXcli_req_unset_pending(struct tevent_req *req)
775 struct smbXcli_req_state *state =
777 struct smbXcli_req_state);
778 struct smbXcli_conn *conn = state->conn;
779 size_t num_pending = talloc_array_length(conn->pending);
782 if (state->smb1.mid != 0) {
784 * This is a [nt]trans[2] request which waits
785 * for more than one reply.
790 tevent_req_set_cleanup_fn(req, NULL);
792 if (num_pending == 1) {
794 * The pending read_smb tevent_req is a child of
795 * conn->pending. So if nothing is pending anymore, we need to
796 * delete the socket read fde.
798 TALLOC_FREE(conn->pending);
799 conn->read_smb_req = NULL;
803 for (i=0; i<num_pending; i++) {
804 if (req == conn->pending[i]) {
808 if (i == num_pending) {
810 * Something's seriously broken. Just returning here is the
811 * right thing nevertheless, the point of this routine is to
812 * remove ourselves from conn->pending.
818 * Remove ourselves from the conn->pending array
820 for (; i < (num_pending - 1); i++) {
821 conn->pending[i] = conn->pending[i+1];
825 * No NULL check here, we're shrinking by sizeof(void *), and
826 * talloc_realloc just adjusts the size for this.
828 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
833 static void smbXcli_req_cleanup(struct tevent_req *req,
834 enum tevent_req_state req_state)
836 struct smbXcli_req_state *state =
838 struct smbXcli_req_state);
841 case TEVENT_REQ_RECEIVED:
843 * Make sure we really remove it from
844 * the pending array on destruction.
847 smbXcli_req_unset_pending(req);
854 static bool smb1cli_req_cancel(struct tevent_req *req);
855 static bool smb2cli_req_cancel(struct tevent_req *req);
857 static bool smbXcli_req_cancel(struct tevent_req *req)
859 struct smbXcli_req_state *state =
861 struct smbXcli_req_state);
863 if (!smbXcli_conn_is_connected(state->conn)) {
867 if (state->conn->protocol == PROTOCOL_NONE) {
871 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
872 return smb2cli_req_cancel(req);
875 return smb1cli_req_cancel(req);
878 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
880 bool smbXcli_req_set_pending(struct tevent_req *req)
882 struct smbXcli_req_state *state =
884 struct smbXcli_req_state);
885 struct smbXcli_conn *conn;
886 struct tevent_req **pending;
891 if (!smbXcli_conn_is_connected(conn)) {
895 num_pending = talloc_array_length(conn->pending);
897 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
899 if (pending == NULL) {
902 pending[num_pending] = req;
903 conn->pending = pending;
904 tevent_req_set_cleanup_fn(req, smbXcli_req_cleanup);
905 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
907 if (!smbXcli_conn_receive_next(conn)) {
909 * the caller should notify the current request
911 * And all other pending requests get notified
912 * by smbXcli_conn_disconnect().
914 smbXcli_req_unset_pending(req);
915 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
922 static void smbXcli_conn_received(struct tevent_req *subreq);
924 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
926 size_t num_pending = talloc_array_length(conn->pending);
927 struct tevent_req *req;
928 struct smbXcli_req_state *state;
930 if (conn->read_smb_req != NULL) {
934 if (num_pending == 0) {
935 if (conn->smb2.mid < UINT64_MAX) {
936 /* no more pending requests, so we are done for now */
941 * If there are no more SMB2 requests possible,
942 * because we are out of message ids,
943 * we need to disconnect.
945 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
949 req = conn->pending[0];
950 state = tevent_req_data(req, struct smbXcli_req_state);
953 * We're the first ones, add the read_smb request that waits for the
954 * answer from the server
956 conn->read_smb_req = read_smb_send(conn->pending,
959 if (conn->read_smb_req == NULL) {
962 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
966 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
968 struct smbXcli_session *session;
970 tevent_queue_stop(conn->outgoing);
972 if (conn->read_fd != -1) {
973 close(conn->read_fd);
975 if (conn->write_fd != -1) {
976 close(conn->write_fd);
981 session = conn->sessions;
982 if (talloc_array_length(conn->pending) == 0) {
984 * if we do not have pending requests
985 * there is no need to update the channel_sequence
989 for (; session; session = session->next) {
990 smb2cli_session_increment_channel_sequence(session);
994 * Cancel all pending requests. We do not do a for-loop walking
995 * conn->pending because that array changes in
996 * smbXcli_req_unset_pending.
998 while (talloc_array_length(conn->pending) > 0) {
999 struct tevent_req *req;
1000 struct smbXcli_req_state *state;
1001 struct tevent_req **chain;
1005 req = conn->pending[0];
1006 state = tevent_req_data(req, struct smbXcli_req_state);
1008 if (state->smb1.chained_requests == NULL) {
1010 * We're dead. No point waiting for trans2
1013 state->smb1.mid = 0;
1015 smbXcli_req_unset_pending(req);
1017 if (NT_STATUS_IS_OK(status)) {
1018 /* do not notify the callers */
1023 * we need to defer the callback, because we may notify
1024 * more then one caller.
1026 tevent_req_defer_callback(req, state->ev);
1027 tevent_req_nterror(req, status);
1031 chain = talloc_move(conn, &state->smb1.chained_requests);
1032 num_chained = talloc_array_length(chain);
1034 for (i=0; i<num_chained; i++) {
1036 state = tevent_req_data(req, struct smbXcli_req_state);
1039 * We're dead. No point waiting for trans2
1042 state->smb1.mid = 0;
1044 smbXcli_req_unset_pending(req);
1046 if (NT_STATUS_IS_OK(status)) {
1047 /* do not notify the callers */
1052 * we need to defer the callback, because we may notify
1053 * more than one caller.
1055 tevent_req_defer_callback(req, state->ev);
1056 tevent_req_nterror(req, status);
1063 * Fetch a smb request's mid. Only valid after the request has been sent by
1064 * smb1cli_req_send().
1066 uint16_t smb1cli_req_mid(struct tevent_req *req)
1068 struct smbXcli_req_state *state =
1069 tevent_req_data(req,
1070 struct smbXcli_req_state);
1072 if (state->smb1.mid != 0) {
1073 return state->smb1.mid;
1076 return SVAL(state->smb1.hdr, HDR_MID);
1079 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1081 struct smbXcli_req_state *state =
1082 tevent_req_data(req,
1083 struct smbXcli_req_state);
1085 state->smb1.mid = mid;
1088 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1090 struct smbXcli_req_state *state =
1091 tevent_req_data(req,
1092 struct smbXcli_req_state);
1094 return state->smb1.seqnum;
1097 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1099 struct smbXcli_req_state *state =
1100 tevent_req_data(req,
1101 struct smbXcli_req_state);
1103 state->smb1.seqnum = seqnum;
1106 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1110 for (i=0; i<count; i++) {
1111 result += iov[i].iov_len;
1116 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1117 const struct iovec *iov,
1120 size_t len = smbXcli_iov_len(iov, count);
1125 buf = talloc_array(mem_ctx, uint8_t, len);
1130 for (i=0; i<count; i++) {
1131 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1132 copied += iov[i].iov_len;
1137 static void smb1cli_req_flags(enum protocol_types protocol,
1138 uint32_t smb1_capabilities,
1139 uint8_t smb_command,
1140 uint8_t additional_flags,
1141 uint8_t clear_flags,
1143 uint16_t additional_flags2,
1144 uint16_t clear_flags2,
1148 uint16_t flags2 = 0;
1150 if (protocol >= PROTOCOL_LANMAN1) {
1151 flags |= FLAG_CASELESS_PATHNAMES;
1152 flags |= FLAG_CANONICAL_PATHNAMES;
1155 if (protocol >= PROTOCOL_LANMAN2) {
1156 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1157 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1160 if (protocol >= PROTOCOL_NT1) {
1161 flags2 |= FLAGS2_IS_LONG_NAME;
1163 if (smb1_capabilities & CAP_UNICODE) {
1164 flags2 |= FLAGS2_UNICODE_STRINGS;
1166 if (smb1_capabilities & CAP_STATUS32) {
1167 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1169 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1170 flags2 |= FLAGS2_EXTENDED_SECURITY;
1174 flags |= additional_flags;
1175 flags &= ~clear_flags;
1176 flags2 |= additional_flags2;
1177 flags2 &= ~clear_flags2;
1183 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1185 static bool smb1cli_req_cancel(struct tevent_req *req)
1187 struct smbXcli_req_state *state =
1188 tevent_req_data(req,
1189 struct smbXcli_req_state);
1194 struct tevent_req *subreq;
1197 flags = CVAL(state->smb1.hdr, HDR_FLG);
1198 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1199 pid = SVAL(state->smb1.hdr, HDR_PID);
1200 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1201 mid = SVAL(state->smb1.hdr, HDR_MID);
1203 subreq = smb1cli_req_create(state, state->ev,
1213 0, NULL); /* bytes */
1214 if (subreq == NULL) {
1217 smb1cli_req_set_mid(subreq, mid);
1219 status = smb1cli_req_chain_submit(&subreq, 1);
1220 if (!NT_STATUS_IS_OK(status)) {
1221 TALLOC_FREE(subreq);
1224 smb1cli_req_set_mid(subreq, 0);
1226 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1231 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1233 /* we do not care about the result */
1234 TALLOC_FREE(subreq);
1237 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1238 struct tevent_context *ev,
1239 struct smbXcli_conn *conn,
1240 uint8_t smb_command,
1241 uint8_t additional_flags,
1242 uint8_t clear_flags,
1243 uint16_t additional_flags2,
1244 uint16_t clear_flags2,
1245 uint32_t timeout_msec,
1247 struct smbXcli_tcon *tcon,
1248 struct smbXcli_session *session,
1249 uint8_t wct, uint16_t *vwv,
1251 struct iovec *bytes_iov)
1253 struct tevent_req *req;
1254 struct smbXcli_req_state *state;
1256 uint16_t flags2 = 0;
1260 if (iov_count > MAX_SMB_IOV) {
1262 * Should not happen :-)
1267 req = tevent_req_create(mem_ctx, &state,
1268 struct smbXcli_req_state);
1274 state->session = session;
1278 uid = session->smb1.session_id;
1282 tid = tcon->smb1.tcon_id;
1284 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1285 clear_flags |= FLAG_CASELESS_PATHNAMES;
1287 /* Default setting, case insensitive. */
1288 additional_flags |= FLAG_CASELESS_PATHNAMES;
1291 if (smbXcli_conn_dfs_supported(conn) &&
1292 smbXcli_tcon_is_dfs_share(tcon))
1294 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1298 state->smb1.recv_cmd = 0xFF;
1299 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1300 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1301 if (state->smb1.recv_iov == NULL) {
1306 smb1cli_req_flags(conn->protocol,
1307 conn->smb1.capabilities,
1316 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1317 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1318 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1319 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1320 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1321 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1322 SSVAL(state->smb1.hdr, HDR_TID, tid);
1323 SSVAL(state->smb1.hdr, HDR_PID, pid);
1324 SSVAL(state->smb1.hdr, HDR_UID, uid);
1325 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1326 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1328 state->smb1.vwv = vwv;
1330 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1332 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1333 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1334 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1335 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1336 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1337 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1338 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1339 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1341 if (iov_count != 0) {
1342 memcpy(&state->smb1.iov[4], bytes_iov,
1343 iov_count * sizeof(*bytes_iov));
1345 state->smb1.iov_count = iov_count + 4;
1347 if (timeout_msec > 0) {
1348 struct timeval endtime;
1350 endtime = timeval_current_ofs_msec(timeout_msec);
1351 if (!tevent_req_set_endtime(req, ev, endtime)) {
1356 switch (smb_command) {
1360 state->one_way = true;
1363 state->one_way = true;
1364 state->smb1.one_way_seqnum = true;
1368 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1369 state->one_way = true;
1377 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1378 struct iovec *iov, int iov_count,
1380 bool one_way_seqnum)
1382 TALLOC_CTX *frame = NULL;
1386 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1387 * iovec directly. MD5Update would do that just fine.
1390 if (iov_count < 4) {
1391 return NT_STATUS_INVALID_PARAMETER_MIX;
1393 if (iov[0].iov_len != NBT_HDR_SIZE) {
1394 return NT_STATUS_INVALID_PARAMETER_MIX;
1396 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1397 return NT_STATUS_INVALID_PARAMETER_MIX;
1399 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1400 return NT_STATUS_INVALID_PARAMETER_MIX;
1402 if (iov[3].iov_len != sizeof(uint16_t)) {
1403 return NT_STATUS_INVALID_PARAMETER_MIX;
1406 frame = talloc_stackframe();
1408 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1410 return NT_STATUS_NO_MEMORY;
1413 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1415 smb_signing_sign_pdu(conn->smb1.signing,
1416 buf, talloc_get_size(buf),
1418 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1421 return NT_STATUS_OK;
1424 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1425 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1426 TALLOC_CTX *tmp_mem,
1429 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1430 struct smbXcli_req_state *state,
1431 struct iovec *iov, int iov_count)
1433 struct tevent_req *subreq;
1438 if (!smbXcli_conn_is_connected(state->conn)) {
1439 return NT_STATUS_CONNECTION_DISCONNECTED;
1442 if (state->conn->protocol > PROTOCOL_NT1) {
1443 return NT_STATUS_REVISION_MISMATCH;
1446 if (iov_count < 4) {
1447 return NT_STATUS_INVALID_PARAMETER_MIX;
1449 if (iov[0].iov_len != NBT_HDR_SIZE) {
1450 return NT_STATUS_INVALID_PARAMETER_MIX;
1452 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1453 return NT_STATUS_INVALID_PARAMETER_MIX;
1455 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1456 return NT_STATUS_INVALID_PARAMETER_MIX;
1458 if (iov[3].iov_len != sizeof(uint16_t)) {
1459 return NT_STATUS_INVALID_PARAMETER_MIX;
1462 cmd = CVAL(iov[1].iov_base, HDR_COM);
1463 if (cmd == SMBreadBraw) {
1464 if (smbXcli_conn_has_async_calls(state->conn)) {
1465 return NT_STATUS_INVALID_PARAMETER_MIX;
1467 state->conn->smb1.read_braw_req = req;
1470 if (state->smb1.mid != 0) {
1471 mid = state->smb1.mid;
1473 mid = smb1cli_alloc_mid(state->conn);
1475 SSVAL(iov[1].iov_base, HDR_MID, mid);
1477 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1479 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1480 &state->smb1.seqnum,
1481 state->smb1.one_way_seqnum);
1483 if (!NT_STATUS_IS_OK(status)) {
1488 * If we supported multiple encrytion contexts
1489 * here we'd look up based on tid.
1491 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1492 char *buf, *enc_buf;
1494 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1496 return NT_STATUS_NO_MEMORY;
1498 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1499 (char *)buf, &enc_buf);
1501 if (!NT_STATUS_IS_OK(status)) {
1502 DEBUG(0, ("Error in encrypting client message: %s\n",
1503 nt_errstr(status)));
1506 buf = (char *)talloc_memdup(state, enc_buf,
1507 smb_len_nbt(enc_buf)+4);
1510 return NT_STATUS_NO_MEMORY;
1512 iov[0].iov_base = (void *)buf;
1513 iov[0].iov_len = talloc_get_size(buf);
1517 if (state->conn->dispatch_incoming == NULL) {
1518 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1521 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1523 subreq = writev_send(state, state->ev, state->conn->outgoing,
1524 state->conn->write_fd, false, iov, iov_count);
1525 if (subreq == NULL) {
1526 return NT_STATUS_NO_MEMORY;
1528 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1529 return NT_STATUS_OK;
1532 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1533 struct tevent_context *ev,
1534 struct smbXcli_conn *conn,
1535 uint8_t smb_command,
1536 uint8_t additional_flags,
1537 uint8_t clear_flags,
1538 uint16_t additional_flags2,
1539 uint16_t clear_flags2,
1540 uint32_t timeout_msec,
1542 struct smbXcli_tcon *tcon,
1543 struct smbXcli_session *session,
1544 uint8_t wct, uint16_t *vwv,
1546 const uint8_t *bytes)
1548 struct tevent_req *req;
1552 iov.iov_base = discard_const_p(void, bytes);
1553 iov.iov_len = num_bytes;
1555 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1556 additional_flags, clear_flags,
1557 additional_flags2, clear_flags2,
1564 if (!tevent_req_is_in_progress(req)) {
1565 return tevent_req_post(req, ev);
1567 status = smb1cli_req_chain_submit(&req, 1);
1568 if (tevent_req_nterror(req, status)) {
1569 return tevent_req_post(req, ev);
1574 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1576 struct tevent_req *req =
1577 tevent_req_callback_data(subreq,
1579 struct smbXcli_req_state *state =
1580 tevent_req_data(req,
1581 struct smbXcli_req_state);
1585 nwritten = writev_recv(subreq, &err);
1586 TALLOC_FREE(subreq);
1587 if (nwritten == -1) {
1588 NTSTATUS status = map_nt_error_from_unix_common(err);
1589 smbXcli_conn_disconnect(state->conn, status);
1593 if (state->one_way) {
1594 state->inbuf = NULL;
1595 tevent_req_done(req);
1599 if (!smbXcli_req_set_pending(req)) {
1600 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1605 static void smbXcli_conn_received(struct tevent_req *subreq)
1607 struct smbXcli_conn *conn =
1608 tevent_req_callback_data(subreq,
1609 struct smbXcli_conn);
1610 TALLOC_CTX *frame = talloc_stackframe();
1616 if (subreq != conn->read_smb_req) {
1617 DEBUG(1, ("Internal error: cli_smb_received called with "
1618 "unexpected subreq\n"));
1619 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1623 conn->read_smb_req = NULL;
1625 received = read_smb_recv(subreq, frame, &inbuf, &err);
1626 TALLOC_FREE(subreq);
1627 if (received == -1) {
1628 status = map_nt_error_from_unix_common(err);
1629 smbXcli_conn_disconnect(conn, status);
1634 status = conn->dispatch_incoming(conn, frame, inbuf);
1636 if (NT_STATUS_IS_OK(status)) {
1638 * We should not do any more processing
1639 * as the dispatch function called
1640 * tevent_req_done().
1645 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1647 * We got an error, so notify all pending requests
1649 smbXcli_conn_disconnect(conn, status);
1654 * We got NT_STATUS_RETRY, so we may ask for a
1655 * next incoming pdu.
1657 if (!smbXcli_conn_receive_next(conn)) {
1658 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1662 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1663 struct iovec **piov, int *pnum_iov)
1674 size_t min_size = MIN_SMB_SIZE;
1676 buflen = smb_len_tcp(buf);
1679 hdr = buf + NBT_HDR_SIZE;
1681 status = smb1cli_pull_raw_error(hdr);
1682 if (NT_STATUS_IS_ERR(status)) {
1684 * This is an ugly hack to support OS/2
1685 * which skips the byte_count in the DATA block
1686 * on some error responses.
1690 min_size -= sizeof(uint16_t);
1693 if (buflen < min_size) {
1694 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1698 * This returns iovec elements in the following order:
1713 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1715 return NT_STATUS_NO_MEMORY;
1717 iov[0].iov_base = hdr;
1718 iov[0].iov_len = HDR_WCT;
1721 cmd = CVAL(hdr, HDR_COM);
1725 size_t len = buflen - taken;
1727 struct iovec *iov_tmp;
1734 * we need at least WCT
1736 needed = sizeof(uint8_t);
1738 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1739 __location__, (int)len, (int)needed));
1744 * Now we check if the specified words are there
1746 wct = CVAL(hdr, wct_ofs);
1747 needed += wct * sizeof(uint16_t);
1749 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1750 __location__, (int)len, (int)needed));
1754 if ((num_iov == 1) &&
1756 NT_STATUS_IS_ERR(status))
1759 * This is an ugly hack to support OS/2
1760 * which skips the byte_count in the DATA block
1761 * on some error responses.
1765 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1767 if (iov_tmp == NULL) {
1769 return NT_STATUS_NO_MEMORY;
1772 cur = &iov[num_iov];
1776 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1778 cur[1].iov_base = cur[0].iov_base;
1785 * we need at least BCC
1787 needed += sizeof(uint16_t);
1789 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1790 __location__, (int)len, (int)needed));
1795 * Now we check if the specified bytes are there
1797 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1798 bcc = SVAL(hdr, bcc_ofs);
1799 needed += bcc * sizeof(uint8_t);
1801 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1802 __location__, (int)len, (int)needed));
1807 * we allocate 2 iovec structures for words and bytes
1809 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1811 if (iov_tmp == NULL) {
1813 return NT_STATUS_NO_MEMORY;
1816 cur = &iov[num_iov];
1819 cur[0].iov_len = wct * sizeof(uint16_t);
1820 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1821 cur[1].iov_len = bcc * sizeof(uint8_t);
1822 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1826 if (!smb1cli_is_andx_req(cmd)) {
1828 * If the current command does not have AndX chanining
1834 if (wct == 0 && bcc == 0) {
1836 * An empty response also ends the chain,
1837 * most likely with an error.
1843 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1844 __location__, (int)wct, (int)cmd));
1847 cmd = CVAL(cur[0].iov_base, 0);
1850 * If it is the end of the chain we are also done.
1854 wct_ofs = SVAL(cur[0].iov_base, 2);
1856 if (wct_ofs < taken) {
1857 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1859 if (wct_ofs > buflen) {
1860 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1864 * we consumed everything up to the start of the next
1870 remaining = buflen - taken;
1872 if (remaining > 0 && num_iov >= 3) {
1874 * The last DATA block gets the remaining
1875 * bytes, this is needed to support
1876 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1878 iov[num_iov-1].iov_len += remaining;
1882 *pnum_iov = num_iov;
1883 return NT_STATUS_OK;
1887 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1890 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1891 TALLOC_CTX *tmp_mem,
1894 struct tevent_req *req;
1895 struct smbXcli_req_state *state;
1902 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1903 size_t len = smb_len_tcp(inbuf);
1904 struct iovec *iov = NULL;
1906 struct tevent_req **chain = NULL;
1907 size_t num_chained = 0;
1908 size_t num_responses = 0;
1910 if (conn->smb1.read_braw_req != NULL) {
1911 req = conn->smb1.read_braw_req;
1912 conn->smb1.read_braw_req = NULL;
1913 state = tevent_req_data(req, struct smbXcli_req_state);
1915 smbXcli_req_unset_pending(req);
1917 if (state->smb1.recv_iov == NULL) {
1919 * For requests with more than
1920 * one response, we have to readd the
1923 state->smb1.recv_iov = talloc_zero_array(state,
1926 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1927 return NT_STATUS_OK;
1931 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1932 state->smb1.recv_iov[0].iov_len = len;
1933 ZERO_STRUCT(state->smb1.recv_iov[1]);
1934 ZERO_STRUCT(state->smb1.recv_iov[2]);
1936 state->smb1.recv_cmd = SMBreadBraw;
1937 state->smb1.recv_status = NT_STATUS_OK;
1938 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1940 tevent_req_done(req);
1941 return NT_STATUS_OK;
1944 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1945 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1946 DEBUG(10, ("Got non-SMB PDU\n"));
1947 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1951 * If we supported multiple encrytion contexts
1952 * here we'd look up based on tid.
1954 if (common_encryption_on(conn->smb1.trans_enc)
1955 && (CVAL(inbuf, 0) == 0)) {
1956 uint16_t enc_ctx_num;
1958 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1959 if (!NT_STATUS_IS_OK(status)) {
1960 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1961 nt_errstr(status)));
1965 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1966 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1968 conn->smb1.trans_enc->enc_ctx_num));
1969 return NT_STATUS_INVALID_HANDLE;
1972 status = common_decrypt_buffer(conn->smb1.trans_enc,
1974 if (!NT_STATUS_IS_OK(status)) {
1975 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1976 nt_errstr(status)));
1979 inhdr = inbuf + NBT_HDR_SIZE;
1980 len = smb_len_nbt(inbuf);
1983 mid = SVAL(inhdr, HDR_MID);
1984 num_pending = talloc_array_length(conn->pending);
1986 for (i=0; i<num_pending; i++) {
1987 if (mid == smb1cli_req_mid(conn->pending[i])) {
1991 if (i == num_pending) {
1992 /* Dump unexpected reply */
1993 return NT_STATUS_RETRY;
1996 oplock_break = false;
1998 if (mid == 0xffff) {
2000 * Paranoia checks that this is really an oplock break request.
2002 oplock_break = (len == 51); /* hdr + 8 words */
2003 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2004 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2005 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2006 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2008 if (!oplock_break) {
2009 /* Dump unexpected reply */
2010 return NT_STATUS_RETRY;
2014 req = conn->pending[i];
2015 state = tevent_req_data(req, struct smbXcli_req_state);
2017 if (!oplock_break /* oplock breaks are not signed */
2018 && !smb_signing_check_pdu(conn->smb1.signing,
2019 inhdr, len, state->smb1.seqnum+1)) {
2020 DEBUG(10, ("cli_check_sign_mac failed\n"));
2021 return NT_STATUS_ACCESS_DENIED;
2024 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2026 if (!NT_STATUS_IS_OK(status)) {
2027 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2028 nt_errstr(status)));
2032 cmd = CVAL(inhdr, HDR_COM);
2033 status = smb1cli_pull_raw_error(inhdr);
2035 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2036 (state->session != NULL) && state->session->disconnect_expired)
2039 * this should be a short term hack
2040 * until the upper layers have implemented
2041 * re-authentication.
2046 if (state->smb1.chained_requests == NULL) {
2048 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2051 smbXcli_req_unset_pending(req);
2053 if (state->smb1.recv_iov == NULL) {
2055 * For requests with more than
2056 * one response, we have to readd the
2059 state->smb1.recv_iov = talloc_zero_array(state,
2062 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2063 return NT_STATUS_OK;
2067 state->smb1.recv_cmd = cmd;
2068 state->smb1.recv_status = status;
2069 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2071 state->smb1.recv_iov[0] = iov[0];
2072 state->smb1.recv_iov[1] = iov[1];
2073 state->smb1.recv_iov[2] = iov[2];
2075 if (talloc_array_length(conn->pending) == 0) {
2076 tevent_req_done(req);
2077 return NT_STATUS_OK;
2080 tevent_req_defer_callback(req, state->ev);
2081 tevent_req_done(req);
2082 return NT_STATUS_RETRY;
2085 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2086 num_chained = talloc_array_length(chain);
2087 num_responses = (num_iov - 1)/2;
2089 if (num_responses > num_chained) {
2090 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2093 for (i=0; i<num_chained; i++) {
2094 size_t iov_idx = 1 + (i*2);
2095 struct iovec *cur = &iov[iov_idx];
2099 state = tevent_req_data(req, struct smbXcli_req_state);
2101 smbXcli_req_unset_pending(req);
2104 * as we finish multiple requests here
2105 * we need to defer the callbacks as
2106 * they could destroy our current stack state.
2108 tevent_req_defer_callback(req, state->ev);
2110 if (i >= num_responses) {
2111 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2115 if (state->smb1.recv_iov == NULL) {
2117 * For requests with more than
2118 * one response, we have to readd the
2121 state->smb1.recv_iov = talloc_zero_array(state,
2124 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2129 state->smb1.recv_cmd = cmd;
2131 if (i == (num_responses - 1)) {
2133 * The last request in the chain gets the status
2135 state->smb1.recv_status = status;
2137 cmd = CVAL(cur[0].iov_base, 0);
2138 state->smb1.recv_status = NT_STATUS_OK;
2141 state->inbuf = inbuf;
2144 * Note: here we use talloc_reference() in a way
2145 * that does not expose it to the caller.
2147 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2148 if (tevent_req_nomem(inbuf_ref, req)) {
2152 /* copy the related buffers */
2153 state->smb1.recv_iov[0] = iov[0];
2154 state->smb1.recv_iov[1] = cur[0];
2155 state->smb1.recv_iov[2] = cur[1];
2157 tevent_req_done(req);
2160 return NT_STATUS_RETRY;
2163 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2164 TALLOC_CTX *mem_ctx,
2165 struct iovec **piov,
2169 uint32_t *pvwv_offset,
2170 uint32_t *pnum_bytes,
2172 uint32_t *pbytes_offset,
2174 const struct smb1cli_req_expected_response *expected,
2175 size_t num_expected)
2177 struct smbXcli_req_state *state =
2178 tevent_req_data(req,
2179 struct smbXcli_req_state);
2180 NTSTATUS status = NT_STATUS_OK;
2181 struct iovec *recv_iov = NULL;
2182 uint8_t *hdr = NULL;
2184 uint32_t vwv_offset = 0;
2185 uint16_t *vwv = NULL;
2186 uint32_t num_bytes = 0;
2187 uint32_t bytes_offset = 0;
2188 uint8_t *bytes = NULL;
2190 bool found_status = false;
2191 bool found_size = false;
2205 if (pvwv_offset != NULL) {
2208 if (pnum_bytes != NULL) {
2211 if (pbytes != NULL) {
2214 if (pbytes_offset != NULL) {
2217 if (pinbuf != NULL) {
2221 if (state->inbuf != NULL) {
2222 recv_iov = state->smb1.recv_iov;
2223 state->smb1.recv_iov = NULL;
2224 if (state->smb1.recv_cmd != SMBreadBraw) {
2225 hdr = (uint8_t *)recv_iov[0].iov_base;
2226 wct = recv_iov[1].iov_len/2;
2227 vwv = (uint16_t *)recv_iov[1].iov_base;
2228 vwv_offset = PTR_DIFF(vwv, hdr);
2229 num_bytes = recv_iov[2].iov_len;
2230 bytes = (uint8_t *)recv_iov[2].iov_base;
2231 bytes_offset = PTR_DIFF(bytes, hdr);
2235 if (tevent_req_is_nterror(req, &status)) {
2236 for (i=0; i < num_expected; i++) {
2237 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2238 found_status = true;
2244 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2250 if (num_expected == 0) {
2251 found_status = true;
2255 status = state->smb1.recv_status;
2257 for (i=0; i < num_expected; i++) {
2258 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2262 found_status = true;
2263 if (expected[i].wct == 0) {
2268 if (expected[i].wct == wct) {
2274 if (!found_status) {
2279 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2283 *piov = talloc_move(mem_ctx, &recv_iov);
2295 if (pvwv_offset != NULL) {
2296 *pvwv_offset = vwv_offset;
2298 if (pnum_bytes != NULL) {
2299 *pnum_bytes = num_bytes;
2301 if (pbytes != NULL) {
2304 if (pbytes_offset != NULL) {
2305 *pbytes_offset = bytes_offset;
2307 if (pinbuf != NULL) {
2308 *pinbuf = state->inbuf;
2314 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2321 for (i=0; i<num_reqs; i++) {
2322 struct smbXcli_req_state *state;
2323 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2324 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2325 state->smb1.iov_count-2);
2326 wct_ofs = (wct_ofs + 3) & ~3;
2331 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2333 struct smbXcli_req_state *first_state =
2334 tevent_req_data(reqs[0],
2335 struct smbXcli_req_state);
2336 struct smbXcli_req_state *state;
2338 size_t chain_padding = 0;
2340 struct iovec *iov = NULL;
2341 struct iovec *this_iov;
2345 if (num_reqs == 1) {
2346 return smb1cli_req_writev_submit(reqs[0], first_state,
2347 first_state->smb1.iov,
2348 first_state->smb1.iov_count);
2352 for (i=0; i<num_reqs; i++) {
2353 if (!tevent_req_is_in_progress(reqs[i])) {
2354 return NT_STATUS_INTERNAL_ERROR;
2357 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2359 if (state->smb1.iov_count < 4) {
2360 return NT_STATUS_INVALID_PARAMETER_MIX;
2365 * The NBT and SMB header
2378 iovlen += state->smb1.iov_count - 2;
2381 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2383 return NT_STATUS_NO_MEMORY;
2386 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2387 first_state, reqs, sizeof(*reqs) * num_reqs);
2388 if (first_state->smb1.chained_requests == NULL) {
2390 return NT_STATUS_NO_MEMORY;
2393 wct_offset = HDR_WCT;
2396 for (i=0; i<num_reqs; i++) {
2397 size_t next_padding = 0;
2400 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2402 if (i < num_reqs-1) {
2403 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2404 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2406 TALLOC_FREE(first_state->smb1.chained_requests);
2407 return NT_STATUS_INVALID_PARAMETER_MIX;
2411 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2412 state->smb1.iov_count-2) + 1;
2413 if ((wct_offset % 4) != 0) {
2414 next_padding = 4 - (wct_offset % 4);
2416 wct_offset += next_padding;
2417 vwv = state->smb1.vwv;
2419 if (i < num_reqs-1) {
2420 struct smbXcli_req_state *next_state =
2421 tevent_req_data(reqs[i+1],
2422 struct smbXcli_req_state);
2423 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2425 SSVAL(vwv+1, 0, wct_offset);
2426 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2427 /* properly end the chain */
2428 SCVAL(vwv+0, 0, 0xff);
2429 SCVAL(vwv+0, 1, 0xff);
2435 * The NBT and SMB header
2437 this_iov[0] = state->smb1.iov[0];
2438 this_iov[1] = state->smb1.iov[1];
2442 * This one is a bit subtle. We have to add
2443 * chain_padding bytes between the requests, and we
2444 * have to also include the wct field of the
2445 * subsequent requests. We use the subsequent header
2446 * for the padding, it contains the wct field in its
2449 this_iov[0].iov_len = chain_padding+1;
2450 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2451 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2452 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2457 * copy the words and bytes
2459 memcpy(this_iov, state->smb1.iov+2,
2460 sizeof(struct iovec) * (state->smb1.iov_count-2));
2461 this_iov += state->smb1.iov_count - 2;
2462 chain_padding = next_padding;
2465 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2466 if (nbt_len > first_state->conn->smb1.max_xmit) {
2468 TALLOC_FREE(first_state->smb1.chained_requests);
2469 return NT_STATUS_INVALID_PARAMETER_MIX;
2472 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2473 if (!NT_STATUS_IS_OK(status)) {
2475 TALLOC_FREE(first_state->smb1.chained_requests);
2479 return NT_STATUS_OK;
2482 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2484 return ((tevent_queue_length(conn->outgoing) != 0)
2485 || (talloc_array_length(conn->pending) != 0));
2488 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2490 if (conn->protocol >= PROTOCOL_SMB2_02) {
2491 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2494 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2497 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2499 uint16_t credits = 1;
2501 if (conn->smb2.cur_credits == 0) {
2502 if (max_dyn_len != NULL) {
2508 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2509 credits = conn->smb2.cur_credits;
2512 if (max_dyn_len != NULL) {
2513 *max_dyn_len = credits * 65536;
2519 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2521 return conn->smb2.server.capabilities;
2524 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2526 return conn->smb2.server.security_mode;
2529 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2531 return conn->smb2.server.max_trans_size;
2534 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2536 return conn->smb2.server.max_read_size;
2539 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2541 return conn->smb2.server.max_write_size;
2544 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2545 uint16_t max_credits)
2547 conn->smb2.max_credits = max_credits;
2550 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2552 static bool smb2cli_req_cancel(struct tevent_req *req)
2554 struct smbXcli_req_state *state =
2555 tevent_req_data(req,
2556 struct smbXcli_req_state);
2557 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2558 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2559 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2560 struct smbXcli_tcon *tcon = state->tcon;
2561 struct smbXcli_session *session = state->session;
2562 uint8_t *fixed = state->smb2.pad;
2563 uint16_t fixed_len = 4;
2564 struct tevent_req *subreq;
2565 struct smbXcli_req_state *substate;
2568 SSVAL(fixed, 0, 0x04);
2571 subreq = smb2cli_req_create(state, state->ev,
2579 if (subreq == NULL) {
2582 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2585 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2586 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2588 flags &= SMB2_HDR_FLAG_ASYNC;
2590 if (flags & SMB2_HDR_FLAG_ASYNC) {
2594 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2595 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2596 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2598 status = smb2cli_req_compound_submit(&subreq, 1);
2599 if (!NT_STATUS_IS_OK(status)) {
2600 TALLOC_FREE(subreq);
2604 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2609 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2611 /* we do not care about the result */
2612 TALLOC_FREE(subreq);
2615 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2616 struct tevent_context *ev,
2617 struct smbXcli_conn *conn,
2619 uint32_t additional_flags,
2620 uint32_t clear_flags,
2621 uint32_t timeout_msec,
2622 struct smbXcli_tcon *tcon,
2623 struct smbXcli_session *session,
2624 const uint8_t *fixed,
2628 uint32_t max_dyn_len)
2630 struct tevent_req *req;
2631 struct smbXcli_req_state *state;
2635 bool use_channel_sequence = false;
2636 uint16_t channel_sequence = 0;
2637 bool use_replay_flag = false;
2639 req = tevent_req_create(mem_ctx, &state,
2640 struct smbXcli_req_state);
2647 state->session = session;
2650 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2651 use_channel_sequence = true;
2652 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2653 use_channel_sequence = true;
2656 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_00) {
2657 use_replay_flag = true;
2661 uid = session->smb2->session_id;
2663 if (use_channel_sequence) {
2664 channel_sequence = session->smb2->channel_sequence;
2667 if (use_replay_flag && session->smb2->replay_active) {
2668 additional_flags |= SMB2_HDR_FLAG_REPLAY_OPERATION;
2671 state->smb2.should_sign = session->smb2->should_sign;
2672 state->smb2.should_encrypt = session->smb2->should_encrypt;
2674 if (cmd == SMB2_OP_SESSSETUP &&
2675 session->smb2->signing_key.length != 0) {
2676 state->smb2.should_sign = true;
2679 if (cmd == SMB2_OP_SESSSETUP &&
2680 session->smb2_channel.signing_key.length == 0) {
2681 state->smb2.should_encrypt = false;
2684 if (additional_flags & SMB2_HDR_FLAG_SIGNED) {
2685 if (session->smb2_channel.signing_key.length == 0) {
2686 tevent_req_nterror(req, NT_STATUS_NO_USER_SESSION_KEY);
2690 additional_flags &= ~SMB2_HDR_FLAG_SIGNED;
2691 state->smb2.should_sign = true;
2696 tid = tcon->smb2.tcon_id;
2698 if (tcon->smb2.should_sign) {
2699 state->smb2.should_sign = true;
2701 if (tcon->smb2.should_encrypt) {
2702 state->smb2.should_encrypt = true;
2706 if (state->smb2.should_encrypt) {
2707 state->smb2.should_sign = false;
2710 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2711 if (state->smb2.recv_iov == NULL) {
2716 flags |= additional_flags;
2717 flags &= ~clear_flags;
2719 state->smb2.fixed = fixed;
2720 state->smb2.fixed_len = fixed_len;
2721 state->smb2.dyn = dyn;
2722 state->smb2.dyn_len = dyn_len;
2723 state->smb2.max_dyn_len = max_dyn_len;
2725 if (state->smb2.should_encrypt) {
2726 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2727 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2730 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2731 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2732 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2733 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2734 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2735 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2736 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2737 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2740 case SMB2_OP_CANCEL:
2741 state->one_way = true;
2745 * If this is a dummy request, it will have
2746 * UINT64_MAX as message id.
2747 * If we send on break acknowledgement,
2748 * this gets overwritten later.
2750 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2754 if (timeout_msec > 0) {
2755 struct timeval endtime;
2757 endtime = timeval_current_ofs_msec(timeout_msec);
2758 if (!tevent_req_set_endtime(req, ev, endtime)) {
2766 void smb2cli_req_set_notify_async(struct tevent_req *req)
2768 struct smbXcli_req_state *state =
2769 tevent_req_data(req,
2770 struct smbXcli_req_state);
2772 state->smb2.notify_async = true;
2775 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2776 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2777 TALLOC_CTX *tmp_mem,
2780 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2783 struct smbXcli_req_state *state;
2784 struct tevent_req *subreq;
2786 int i, num_iov, nbt_len;
2788 const DATA_BLOB *encryption_key = NULL;
2789 uint64_t encryption_session_id = 0;
2792 * 1 for the nbt length, optional TRANSFORM
2793 * per request: HDR, fixed, dyn, padding
2794 * -1 because the last one does not need padding
2797 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2799 return NT_STATUS_NO_MEMORY;
2806 * the session of the first request that requires encryption
2807 * specifies the encryption key.
2809 for (i=0; i<num_reqs; i++) {
2810 if (!tevent_req_is_in_progress(reqs[i])) {
2811 return NT_STATUS_INTERNAL_ERROR;
2814 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2816 if (!smbXcli_conn_is_connected(state->conn)) {
2817 return NT_STATUS_CONNECTION_DISCONNECTED;
2820 if ((state->conn->protocol != PROTOCOL_NONE) &&
2821 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2822 return NT_STATUS_REVISION_MISMATCH;
2825 if (state->session == NULL) {
2829 if (!state->smb2.should_encrypt) {
2833 encryption_key = &state->session->smb2->encryption_key;
2834 if (encryption_key->length == 0) {
2835 return NT_STATUS_INVALID_PARAMETER_MIX;
2838 encryption_session_id = state->session->smb2->session_id;
2841 iov[num_iov].iov_base = state->smb2.transform;
2842 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2845 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2846 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2847 state->session->smb2->nonce_low);
2848 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2849 state->session->smb2->nonce_high);
2850 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2851 encryption_session_id);
2853 state->session->smb2->nonce_low += 1;
2854 if (state->session->smb2->nonce_low == 0) {
2855 state->session->smb2->nonce_high += 1;
2856 state->session->smb2->nonce_low += 1;
2859 nbt_len += SMB2_TF_HDR_SIZE;
2863 for (i=0; i<num_reqs; i++) {
2872 const DATA_BLOB *signing_key = NULL;
2874 if (!tevent_req_is_in_progress(reqs[i])) {
2875 return NT_STATUS_INTERNAL_ERROR;
2878 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2880 if (!smbXcli_conn_is_connected(state->conn)) {
2881 return NT_STATUS_CONNECTION_DISCONNECTED;
2884 if ((state->conn->protocol != PROTOCOL_NONE) &&
2885 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2886 return NT_STATUS_REVISION_MISMATCH;
2889 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2890 if (opcode == SMB2_OP_CANCEL) {
2894 avail = UINT64_MAX - state->conn->smb2.mid;
2896 return NT_STATUS_CONNECTION_ABORTED;
2899 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2900 uint32_t max_dyn_len = 1;
2902 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2903 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2905 charge = (max_dyn_len - 1)/ 65536 + 1;
2910 charge = MAX(state->smb2.credit_charge, charge);
2912 avail = MIN(avail, state->conn->smb2.cur_credits);
2913 if (avail < charge) {
2914 return NT_STATUS_INTERNAL_ERROR;
2918 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2919 credits = state->conn->smb2.max_credits -
2920 state->conn->smb2.cur_credits;
2922 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2926 mid = state->conn->smb2.mid;
2927 state->conn->smb2.mid += charge;
2928 state->conn->smb2.cur_credits -= charge;
2930 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2931 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2933 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2934 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2937 if (state->session && encryption_key == NULL) {
2939 * We prefer the channel signing key if it is
2942 if (state->smb2.should_sign) {
2943 signing_key = &state->session->smb2_channel.signing_key;
2947 * If it is a channel binding, we already have the main
2948 * signing key and try that one.
2950 if (signing_key && signing_key->length == 0) {
2951 signing_key = &state->session->smb2->signing_key;
2955 * If we do not have any session key yet, we skip the
2956 * signing of SMB2_OP_SESSSETUP requests.
2958 if (signing_key && signing_key->length == 0) {
2964 iov[num_iov].iov_base = state->smb2.hdr;
2965 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2968 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2969 iov[num_iov].iov_len = state->smb2.fixed_len;
2972 if (state->smb2.dyn != NULL) {
2973 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2974 iov[num_iov].iov_len = state->smb2.dyn_len;
2978 reqlen = sizeof(state->smb2.hdr);
2979 reqlen += state->smb2.fixed_len;
2980 reqlen += state->smb2.dyn_len;
2982 if (i < num_reqs-1) {
2983 if ((reqlen % 8) > 0) {
2984 uint8_t pad = 8 - (reqlen % 8);
2985 iov[num_iov].iov_base = state->smb2.pad;
2986 iov[num_iov].iov_len = pad;
2990 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2993 state->smb2.encryption_session_id = encryption_session_id;
2995 if (signing_key != NULL) {
2998 status = smb2_signing_sign_pdu(*signing_key,
2999 state->session->conn->protocol,
3000 &iov[hdr_iov], num_iov - hdr_iov);
3001 if (!NT_STATUS_IS_OK(status)) {
3008 ret = smbXcli_req_set_pending(reqs[i]);
3010 return NT_STATUS_NO_MEMORY;
3014 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
3015 _smb_setlen_tcp(state->length_hdr, nbt_len);
3016 iov[0].iov_base = state->length_hdr;
3017 iov[0].iov_len = sizeof(state->length_hdr);
3019 if (encryption_key != NULL) {
3021 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3025 buf = talloc_array(iov, uint8_t, buflen);
3027 return NT_STATUS_NO_MEMORY;
3031 * We copy the buffers before encrypting them,
3032 * this is at least currently needed for the
3033 * to keep state->smb2.hdr.
3035 * Also the callers may expect there buffers
3038 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3039 struct iovec *v = &iov[vi];
3040 const uint8_t *o = (const uint8_t *)v->iov_base;
3042 memcpy(buf, o, v->iov_len);
3043 v->iov_base = (void *)buf;
3047 status = smb2_signing_encrypt_pdu(*encryption_key,
3048 state->conn->protocol,
3049 &iov[tf_iov], num_iov - tf_iov);
3050 if (!NT_STATUS_IS_OK(status)) {
3055 if (state->conn->dispatch_incoming == NULL) {
3056 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3059 subreq = writev_send(state, state->ev, state->conn->outgoing,
3060 state->conn->write_fd, false, iov, num_iov);
3061 if (subreq == NULL) {
3062 return NT_STATUS_NO_MEMORY;
3064 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3065 return NT_STATUS_OK;
3068 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3070 struct smbXcli_req_state *state =
3071 tevent_req_data(req,
3072 struct smbXcli_req_state);
3074 state->smb2.credit_charge = charge;
3077 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3078 struct tevent_context *ev,
3079 struct smbXcli_conn *conn,
3081 uint32_t additional_flags,
3082 uint32_t clear_flags,
3083 uint32_t timeout_msec,
3084 struct smbXcli_tcon *tcon,
3085 struct smbXcli_session *session,
3086 const uint8_t *fixed,
3090 uint32_t max_dyn_len)
3092 struct tevent_req *req;
3095 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3096 additional_flags, clear_flags,
3105 if (!tevent_req_is_in_progress(req)) {
3106 return tevent_req_post(req, ev);
3108 status = smb2cli_req_compound_submit(&req, 1);
3109 if (tevent_req_nterror(req, status)) {
3110 return tevent_req_post(req, ev);
3115 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3117 struct tevent_req *req =
3118 tevent_req_callback_data(subreq,
3120 struct smbXcli_req_state *state =
3121 tevent_req_data(req,
3122 struct smbXcli_req_state);
3126 nwritten = writev_recv(subreq, &err);
3127 TALLOC_FREE(subreq);
3128 if (nwritten == -1) {
3129 /* here, we need to notify all pending requests */
3130 NTSTATUS status = map_nt_error_from_unix_common(err);
3131 smbXcli_conn_disconnect(state->conn, status);
3136 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3139 TALLOC_CTX *mem_ctx,
3140 struct iovec **piov, int *pnum_iov)
3145 uint8_t *first_hdr = buf;
3146 size_t verified_buflen = 0;
3150 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3152 return NT_STATUS_NO_MEMORY;
3155 while (taken < buflen) {
3156 size_t len = buflen - taken;
3157 uint8_t *hdr = first_hdr + taken;
3160 size_t next_command_ofs;
3162 struct iovec *iov_tmp;
3164 if (verified_buflen > taken) {
3165 len = verified_buflen - taken;
3172 DEBUG(10, ("%d bytes left, expected at least %d\n",
3176 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3177 struct smbXcli_session *s;
3179 struct iovec tf_iov[2];
3183 if (len < SMB2_TF_HDR_SIZE) {
3184 DEBUG(10, ("%d bytes left, expected at least %d\n",
3185 (int)len, SMB2_TF_HDR_SIZE));
3189 tf_len = SMB2_TF_HDR_SIZE;
3192 hdr = first_hdr + taken;
3193 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3194 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3196 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3197 DEBUG(10, ("%d bytes left, expected at least %d\n",
3199 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3204 for (; s; s = s->next) {
3205 if (s->smb2->session_id != uid) {
3212 DEBUG(10, ("unknown session_id %llu\n",
3213 (unsigned long long)uid));
3217 tf_iov[0].iov_base = (void *)tf;
3218 tf_iov[0].iov_len = tf_len;
3219 tf_iov[1].iov_base = (void *)hdr;
3220 tf_iov[1].iov_len = enc_len;
3222 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3225 if (!NT_STATUS_IS_OK(status)) {
3230 verified_buflen = taken + enc_len;
3235 * We need the header plus the body length field
3238 if (len < SMB2_HDR_BODY + 2) {
3239 DEBUG(10, ("%d bytes left, expected at least %d\n",
3240 (int)len, SMB2_HDR_BODY));
3243 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3244 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3248 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3249 DEBUG(10, ("Got HDR len %d, expected %d\n",
3250 SVAL(hdr, 4), SMB2_HDR_BODY));
3255 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3256 body_size = SVAL(hdr, SMB2_HDR_BODY);
3258 if (next_command_ofs != 0) {
3259 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3262 if (next_command_ofs > full_size) {
3265 full_size = next_command_ofs;
3267 if (body_size < 2) {
3270 body_size &= 0xfffe;
3272 if (body_size > (full_size - SMB2_HDR_BODY)) {
3276 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3278 if (iov_tmp == NULL) {
3280 return NT_STATUS_NO_MEMORY;
3283 cur = &iov[num_iov];
3286 cur[0].iov_base = tf;
3287 cur[0].iov_len = tf_len;
3288 cur[1].iov_base = hdr;
3289 cur[1].iov_len = SMB2_HDR_BODY;
3290 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3291 cur[2].iov_len = body_size;
3292 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3293 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3299 *pnum_iov = num_iov;
3300 return NT_STATUS_OK;
3304 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3307 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3310 size_t num_pending = talloc_array_length(conn->pending);
3313 for (i=0; i<num_pending; i++) {
3314 struct tevent_req *req = conn->pending[i];
3315 struct smbXcli_req_state *state =
3316 tevent_req_data(req,
3317 struct smbXcli_req_state);
3319 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3326 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3327 TALLOC_CTX *tmp_mem,
3330 struct tevent_req *req;
3331 struct smbXcli_req_state *state = NULL;
3336 struct smbXcli_session *last_session = NULL;
3337 size_t inbuf_len = smb_len_tcp(inbuf);
3339 status = smb2cli_inbuf_parse_compound(conn,
3340 inbuf + NBT_HDR_SIZE,
3344 if (!NT_STATUS_IS_OK(status)) {
3348 for (i=0; i<num_iov; i+=4) {
3349 uint8_t *inbuf_ref = NULL;
3350 struct iovec *cur = &iov[i];
3351 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3352 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3353 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3354 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3355 uint16_t req_opcode;
3357 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3358 uint32_t new_credits;
3359 struct smbXcli_session *session = NULL;
3360 const DATA_BLOB *signing_key = NULL;
3361 bool was_encrypted = false;
3363 new_credits = conn->smb2.cur_credits;
3364 new_credits += credits;
3365 if (new_credits > UINT16_MAX) {
3366 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3368 conn->smb2.cur_credits += credits;
3370 req = smb2cli_conn_find_pending(conn, mid);
3372 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3374 state = tevent_req_data(req, struct smbXcli_req_state);
3376 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3377 if (opcode != req_opcode) {
3378 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3380 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3382 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3383 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3386 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3387 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3388 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3389 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3391 if (state->smb2.got_async) {
3392 /* We only expect one STATUS_PENDING response */
3393 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3395 state->smb2.got_async = true;
3398 * async interim responses are not signed,
3399 * even if the SMB2_HDR_FLAG_SIGNED flag
3402 req_flags |= SMB2_HDR_FLAG_ASYNC;
3403 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3404 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3406 if (state->smb2.notify_async) {
3407 tevent_req_defer_callback(req, state->ev);
3408 tevent_req_notify_callback(req);
3413 session = state->session;
3414 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3415 session = last_session;
3417 last_session = session;
3419 if (state->smb2.should_sign) {
3420 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3421 return NT_STATUS_ACCESS_DENIED;
3425 if (flags & SMB2_HDR_FLAG_SIGNED) {
3426 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3428 if (session == NULL) {
3429 struct smbXcli_session *s;
3431 s = state->conn->sessions;
3432 for (; s; s = s->next) {
3433 if (s->smb2->session_id != uid) {
3442 if (session == NULL) {
3443 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3446 last_session = session;
3447 signing_key = &session->smb2_channel.signing_key;
3450 if (opcode == SMB2_OP_SESSSETUP) {
3452 * We prefer the channel signing key, if it is
3455 * If we do not have a channel signing key yet,
3456 * we try the main signing key, if it is not
3457 * the final response.
3459 if (signing_key && signing_key->length == 0 &&
3460 !NT_STATUS_IS_OK(status)) {
3461 signing_key = &session->smb2->signing_key;
3464 if (signing_key && signing_key->length == 0) {
3466 * If we do not have a session key to
3467 * verify the signature, we defer the
3468 * signing check to the caller.
3470 * The caller gets NT_STATUS_OK, it
3472 * smb2cli_session_set_session_key()
3474 * smb2cli_session_set_channel_key()
3475 * which will check the signature
3476 * with the channel signing key.
3482 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3483 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3484 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3487 * If the response was encrypted in a SMB2_TRANSFORM
3488 * pdu, which belongs to the correct session,
3489 * we do not need to do signing checks
3491 * It could be the session the response belongs to
3492 * or the session that was used to encrypt the
3493 * SMB2_TRANSFORM request.
3495 if ((session && session->smb2->session_id == uid) ||
3496 (state->smb2.encryption_session_id == uid)) {
3498 was_encrypted = true;
3502 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3504 * if the server returns NT_STATUS_USER_SESSION_DELETED
3505 * the response is not signed and we should
3506 * propagate the NT_STATUS_USER_SESSION_DELETED
3507 * status to the caller.
3509 state->smb2.signing_skipped = true;
3513 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3515 * if the server returns
3516 * NT_STATUS_INVALID_PARAMETER
3517 * the response might not be encrypted.
3519 if (state->smb2.should_encrypt && !was_encrypted) {
3520 state->smb2.signing_skipped = true;
3525 if (state->smb2.should_encrypt && !was_encrypted) {
3526 if (!state->smb2.signing_skipped) {
3527 return NT_STATUS_ACCESS_DENIED;
3531 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3532 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3533 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3535 * if the server returns
3536 * NT_STATUS_NETWORK_NAME_DELETED
3537 * NT_STATUS_FILE_CLOSED
3538 * NT_STATUS_INVALID_PARAMETER
3539 * the response might not be signed
3540 * as this happens before the signing checks.
3542 * If server echos the signature (or all zeros)
3543 * we should report the status from the server
3549 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3550 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3553 state->smb2.signing_skipped = true;
3559 static const uint8_t zeros[16];
3561 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3565 state->smb2.signing_skipped = true;
3572 status = smb2_signing_check_pdu(*signing_key,
3573 state->conn->protocol,
3575 if (!NT_STATUS_IS_OK(status)) {
3577 * If the signing check fails, we disconnect
3584 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3585 (session != NULL) && session->disconnect_expired)
3588 * this should be a short term hack
3589 * until the upper layers have implemented
3590 * re-authentication.
3595 smbXcli_req_unset_pending(req);
3598 * There might be more than one response
3599 * we need to defer the notifications
3601 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3606 tevent_req_defer_callback(req, state->ev);
3610 * Note: here we use talloc_reference() in a way
3611 * that does not expose it to the caller.
3613 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3614 if (tevent_req_nomem(inbuf_ref, req)) {
3618 /* copy the related buffers */
3619 state->smb2.recv_iov[0] = cur[1];
3620 state->smb2.recv_iov[1] = cur[2];
3621 state->smb2.recv_iov[2] = cur[3];
3623 tevent_req_done(req);
3627 return NT_STATUS_RETRY;
3630 return NT_STATUS_OK;
3633 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3634 struct iovec **piov,
3635 const struct smb2cli_req_expected_response *expected,
3636 size_t num_expected)
3638 struct smbXcli_req_state *state =
3639 tevent_req_data(req,
3640 struct smbXcli_req_state);
3643 bool found_status = false;
3644 bool found_size = false;
3651 if (tevent_req_is_in_progress(req) && state->smb2.got_async) {
3652 return STATUS_PENDING;
3655 if (tevent_req_is_nterror(req, &status)) {
3656 for (i=0; i < num_expected; i++) {
3657 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3658 found_status = true;
3664 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3670 if (num_expected == 0) {
3671 found_status = true;
3675 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3676 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3678 for (i=0; i < num_expected; i++) {
3679 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3683 found_status = true;
3684 if (expected[i].body_size == 0) {
3689 if (expected[i].body_size == body_size) {
3695 if (!found_status) {
3699 if (state->smb2.signing_skipped) {
3700 if (num_expected > 0) {
3701 return NT_STATUS_ACCESS_DENIED;
3703 if (!NT_STATUS_IS_ERR(status)) {
3704 return NT_STATUS_ACCESS_DENIED;
3709 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3713 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3719 static const struct {
3720 enum protocol_types proto;
3721 const char *smb1_name;
3722 } smb1cli_prots[] = {
3723 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3724 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3725 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3726 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3727 {PROTOCOL_LANMAN2, "LM1.2X002"},
3728 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3729 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3730 {PROTOCOL_LANMAN2, "Samba"},
3731 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3732 {PROTOCOL_NT1, "NT LM 0.12"},
3733 {PROTOCOL_SMB2_02, "SMB 2.002"},
3734 {PROTOCOL_SMB2_10, "SMB 2.???"},
3737 static const struct {
3738 enum protocol_types proto;
3739 uint16_t smb2_dialect;
3740 } smb2cli_prots[] = {
3741 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3742 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3743 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3744 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3745 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3746 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
3749 struct smbXcli_negprot_state {
3750 struct smbXcli_conn *conn;
3751 struct tevent_context *ev;
3752 uint32_t timeout_msec;
3753 enum protocol_types min_protocol;
3754 enum protocol_types max_protocol;
3758 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3762 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3763 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3764 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3765 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3766 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3767 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3771 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3772 struct tevent_context *ev,
3773 struct smbXcli_conn *conn,
3774 uint32_t timeout_msec,
3775 enum protocol_types min_protocol,
3776 enum protocol_types max_protocol)
3778 struct tevent_req *req, *subreq;
3779 struct smbXcli_negprot_state *state;
3781 req = tevent_req_create(mem_ctx, &state,
3782 struct smbXcli_negprot_state);
3788 state->timeout_msec = timeout_msec;
3789 state->min_protocol = min_protocol;
3790 state->max_protocol = max_protocol;
3792 if (min_protocol == PROTOCOL_NONE) {
3793 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3794 return tevent_req_post(req, ev);
3797 if (max_protocol == PROTOCOL_NONE) {
3798 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3799 return tevent_req_post(req, ev);
3802 if (min_protocol > max_protocol) {
3803 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3804 return tevent_req_post(req, ev);
3807 if ((min_protocol < PROTOCOL_SMB2_02) &&
3808 (max_protocol < PROTOCOL_SMB2_02)) {
3812 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3814 subreq = smbXcli_negprot_smb1_subreq(state);
3815 if (tevent_req_nomem(subreq, req)) {
3816 return tevent_req_post(req, ev);
3818 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3822 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3823 (max_protocol >= PROTOCOL_SMB2_02)) {
3827 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3829 subreq = smbXcli_negprot_smb2_subreq(state);
3830 if (tevent_req_nomem(subreq, req)) {
3831 return tevent_req_post(req, ev);
3833 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3838 * We send an SMB1 negprot with the SMB2 dialects
3839 * and expect a SMB1 or a SMB2 response.
3841 * smbXcli_negprot_dispatch_incoming() will fix the
3842 * callback to match protocol of the response.
3844 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3846 subreq = smbXcli_negprot_smb1_subreq(state);
3847 if (tevent_req_nomem(subreq, req)) {
3848 return tevent_req_post(req, ev);
3850 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3854 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3856 struct tevent_req *req =
3857 tevent_req_callback_data(subreq,
3862 * we just want the low level error
3864 status = tevent_req_simple_recv_ntstatus(subreq);
3865 TALLOC_FREE(subreq);
3866 if (tevent_req_nterror(req, status)) {
3870 /* this should never happen */
3871 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3874 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3877 DATA_BLOB bytes = data_blob_null;
3881 /* setup the protocol strings */
3882 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3886 if (smb1cli_prots[i].proto < state->min_protocol) {
3890 if (smb1cli_prots[i].proto > state->max_protocol) {
3894 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3900 * We now it is already ascii and
3901 * we want NULL termination.
3903 ok = data_blob_append(state, &bytes,
3904 smb1cli_prots[i].smb1_name,
3905 strlen(smb1cli_prots[i].smb1_name)+1);
3911 smb1cli_req_flags(state->max_protocol,
3912 state->conn->smb1.client.capabilities,
3917 return smb1cli_req_send(state, state->ev, state->conn,
3921 state->timeout_msec,
3922 0xFFFE, 0, NULL, /* pid, tid, session */
3923 0, NULL, /* wct, vwv */
3924 bytes.length, bytes.data);
3927 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3929 struct tevent_req *req =
3930 tevent_req_callback_data(subreq,
3932 struct smbXcli_negprot_state *state =
3933 tevent_req_data(req,
3934 struct smbXcli_negprot_state);
3935 struct smbXcli_conn *conn = state->conn;
3936 struct iovec *recv_iov = NULL;
3945 size_t num_prots = 0;
3947 uint32_t client_capabilities = conn->smb1.client.capabilities;
3948 uint32_t both_capabilities;
3949 uint32_t server_capabilities = 0;
3950 uint32_t capabilities;
3951 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3952 uint32_t server_max_xmit = 0;
3954 uint32_t server_max_mux = 0;
3955 uint16_t server_security_mode = 0;
3956 uint32_t server_session_key = 0;
3957 bool server_readbraw = false;
3958 bool server_writebraw = false;
3959 bool server_lockread = false;
3960 bool server_writeunlock = false;
3961 struct GUID server_guid = GUID_zero();
3962 DATA_BLOB server_gss_blob = data_blob_null;
3963 uint8_t server_challenge[8];
3964 char *server_workgroup = NULL;
3965 char *server_name = NULL;
3966 int server_time_zone = 0;
3967 NTTIME server_system_time = 0;
3968 static const struct smb1cli_req_expected_response expected[] = {
3970 .status = NT_STATUS_OK,
3971 .wct = 0x11, /* NT1 */
3974 .status = NT_STATUS_OK,
3975 .wct = 0x0D, /* LM */
3978 .status = NT_STATUS_OK,
3979 .wct = 0x01, /* CORE */
3983 ZERO_STRUCT(server_challenge);
3985 status = smb1cli_req_recv(subreq, state,
3990 NULL, /* pvwv_offset */
3993 NULL, /* pbytes_offset */
3995 expected, ARRAY_SIZE(expected));
3996 TALLOC_FREE(subreq);
3997 if (tevent_req_nterror(req, status)) {
4001 flags = CVAL(inhdr, HDR_FLG);
4003 protnum = SVAL(vwv, 0);
4005 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4006 if (smb1cli_prots[i].proto < state->min_protocol) {
4010 if (smb1cli_prots[i].proto > state->max_protocol) {
4014 if (protnum != num_prots) {
4019 conn->protocol = smb1cli_prots[i].proto;
4023 if (conn->protocol == PROTOCOL_NONE) {
4024 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4028 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4029 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4030 "and the selected protocol level doesn't support it.\n"));
4031 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4035 if (flags & FLAG_SUPPORT_LOCKREAD) {
4036 server_lockread = true;
4037 server_writeunlock = true;
4040 if (conn->protocol >= PROTOCOL_NT1) {
4041 const char *client_signing = NULL;
4042 bool server_mandatory = false;
4043 bool server_allowed = false;
4044 const char *server_signing = NULL;
4049 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4054 server_security_mode = CVAL(vwv + 1, 0);
4055 server_max_mux = SVAL(vwv + 1, 1);
4056 server_max_xmit = IVAL(vwv + 3, 1);
4057 server_session_key = IVAL(vwv + 7, 1);
4058 server_time_zone = SVALS(vwv + 15, 1);
4059 server_time_zone *= 60;
4060 /* this time arrives in real GMT */
4061 server_system_time = BVAL(vwv + 11, 1);
4062 server_capabilities = IVAL(vwv + 9, 1);
4064 key_len = CVAL(vwv + 16, 1);
4066 if (server_capabilities & CAP_RAW_MODE) {
4067 server_readbraw = true;
4068 server_writebraw = true;
4070 if (server_capabilities & CAP_LOCK_AND_READ) {
4071 server_lockread = true;
4074 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4075 DATA_BLOB blob1, blob2;
4077 if (num_bytes < 16) {
4078 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4082 blob1 = data_blob_const(bytes, 16);
4083 status = GUID_from_data_blob(&blob1, &server_guid);
4084 if (tevent_req_nterror(req, status)) {
4088 blob1 = data_blob_const(bytes+16, num_bytes-16);
4089 blob2 = data_blob_dup_talloc(state, blob1);
4090 if (blob1.length > 0 &&
4091 tevent_req_nomem(blob2.data, req)) {
4094 server_gss_blob = blob2;
4096 DATA_BLOB blob1, blob2;
4098 if (num_bytes < key_len) {
4099 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4103 if (key_len != 0 && key_len != 8) {
4104 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4109 memcpy(server_challenge, bytes, 8);
4112 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4113 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4114 if (blob1.length > 0) {
4117 len = utf16_len_n(blob1.data,
4121 ok = convert_string_talloc(state,
4129 status = map_nt_error_from_unix_common(errno);
4130 tevent_req_nterror(req, status);
4135 blob2.data += blob1.length;
4136 blob2.length -= blob1.length;
4137 if (blob2.length > 0) {
4140 len = utf16_len_n(blob1.data,
4144 ok = convert_string_talloc(state,
4152 status = map_nt_error_from_unix_common(errno);
4153 tevent_req_nterror(req, status);
4159 client_signing = "disabled";
4160 if (conn->allow_signing) {
4161 client_signing = "allowed";
4163 if (conn->mandatory_signing) {
4164 client_signing = "required";
4167 server_signing = "not supported";
4168 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4169 server_signing = "supported";
4170 server_allowed = true;
4171 } else if (conn->mandatory_signing) {
4173 * We have mandatory signing as client
4174 * lets assume the server will look at our
4175 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4176 * flag in the session setup
4178 server_signing = "not announced";
4179 server_allowed = true;
4181 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4182 server_signing = "required";
4183 server_mandatory = true;
4186 ok = smb_signing_set_negotiated(conn->smb1.signing,
4190 DEBUG(1,("cli_negprot: SMB signing is required, "
4191 "but client[%s] and server[%s] mismatch\n",
4192 client_signing, server_signing));
4193 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4197 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4203 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4207 server_security_mode = SVAL(vwv + 1, 0);
4208 server_max_xmit = SVAL(vwv + 2, 0);
4209 server_max_mux = SVAL(vwv + 3, 0);
4210 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4211 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4212 server_session_key = IVAL(vwv + 6, 0);
4213 server_time_zone = SVALS(vwv + 10, 0);
4214 server_time_zone *= 60;
4215 /* this time is converted to GMT by make_unix_date */
4216 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4217 unix_to_nt_time(&server_system_time, t);
4218 key_len = SVAL(vwv + 11, 0);
4220 if (num_bytes < key_len) {
4221 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4225 if (key_len != 0 && key_len != 8) {
4226 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4231 memcpy(server_challenge, bytes, 8);
4234 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4235 if (blob1.length > 0) {
4239 len = utf16_len_n(blob1.data,
4243 ok = convert_string_talloc(state,
4251 status = map_nt_error_from_unix_common(errno);
4252 tevent_req_nterror(req, status);
4258 /* the old core protocol */
4259 server_time_zone = get_time_zone(time(NULL));
4260 server_max_xmit = 1024;
4264 if (server_max_xmit < 1024) {
4265 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4269 if (server_max_mux < 1) {
4270 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4275 * Now calculate the negotiated capabilities
4276 * based on the mask for:
4277 * - client only flags
4278 * - flags used in both directions
4279 * - server only flags
4281 both_capabilities = client_capabilities & server_capabilities;
4282 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4283 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4284 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4286 max_xmit = MIN(client_max_xmit, server_max_xmit);
4288 conn->smb1.server.capabilities = server_capabilities;
4289 conn->smb1.capabilities = capabilities;
4291 conn->smb1.server.max_xmit = server_max_xmit;
4292 conn->smb1.max_xmit = max_xmit;
4294 conn->smb1.server.max_mux = server_max_mux;
4296 conn->smb1.server.security_mode = server_security_mode;
4298 conn->smb1.server.readbraw = server_readbraw;
4299 conn->smb1.server.writebraw = server_writebraw;
4300 conn->smb1.server.lockread = server_lockread;
4301 conn->smb1.server.writeunlock = server_writeunlock;
4303 conn->smb1.server.session_key = server_session_key;
4305 talloc_steal(conn, server_gss_blob.data);
4306 conn->smb1.server.gss_blob = server_gss_blob;
4307 conn->smb1.server.guid = server_guid;
4308 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4309 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4310 conn->smb1.server.name = talloc_move(conn, &server_name);
4312 conn->smb1.server.time_zone = server_time_zone;
4313 conn->smb1.server.system_time = server_system_time;
4315 tevent_req_done(req);
4318 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4322 uint16_t dialect_count = 0;
4324 buf = state->smb2.dyn;
4325 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4326 if (smb2cli_prots[i].proto < state->min_protocol) {
4330 if (smb2cli_prots[i].proto > state->max_protocol) {
4334 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4338 buf = state->smb2.fixed;
4340 SSVAL(buf, 2, dialect_count);
4341 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4342 SSVAL(buf, 6, 0); /* Reserved */
4343 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4344 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4346 SIVAL(buf, 8, 0); /* Capabilities */
4348 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4352 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4354 if (!NT_STATUS_IS_OK(status)) {
4357 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4359 memset(buf+12, 0, 16); /* ClientGuid */
4361 SBVAL(buf, 28, 0); /* ClientStartTime */
4363 return smb2cli_req_send(state, state->ev,
4364 state->conn, SMB2_OP_NEGPROT,
4366 state->timeout_msec,
4367 NULL, NULL, /* tcon, session */
4368 state->smb2.fixed, sizeof(state->smb2.fixed),
4369 state->smb2.dyn, dialect_count*2,
4370 UINT16_MAX); /* max_dyn_len */
4373 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4375 struct tevent_req *req =
4376 tevent_req_callback_data(subreq,
4378 struct smbXcli_negprot_state *state =
4379 tevent_req_data(req,
4380 struct smbXcli_negprot_state);
4381 struct smbXcli_conn *conn = state->conn;
4382 size_t security_offset, security_length;
4388 uint16_t dialect_revision;
4389 static const struct smb2cli_req_expected_response expected[] = {
4391 .status = NT_STATUS_OK,
4396 status = smb2cli_req_recv(subreq, state, &iov,
4397 expected, ARRAY_SIZE(expected));
4398 TALLOC_FREE(subreq);
4399 if (tevent_req_nterror(req, status)) {
4403 body = (uint8_t *)iov[1].iov_base;
4405 dialect_revision = SVAL(body, 4);
4407 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4408 if (smb2cli_prots[i].proto < state->min_protocol) {
4412 if (smb2cli_prots[i].proto > state->max_protocol) {
4416 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4420 conn->protocol = smb2cli_prots[i].proto;
4424 if (conn->protocol == PROTOCOL_NONE) {
4425 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4426 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4430 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4431 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4435 /* make sure we do not loop forever */
4436 state->min_protocol = PROTOCOL_SMB2_02;
4439 * send a SMB2 negprot, in order to negotiate
4442 subreq = smbXcli_negprot_smb2_subreq(state);
4443 if (tevent_req_nomem(subreq, req)) {
4446 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4450 conn->smb2.server.security_mode = SVAL(body, 2);
4452 blob = data_blob_const(body + 8, 16);
4453 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4454 if (tevent_req_nterror(req, status)) {
4458 conn->smb2.server.capabilities = IVAL(body, 24);
4459 conn->smb2.server.max_trans_size= IVAL(body, 28);
4460 conn->smb2.server.max_read_size = IVAL(body, 32);
4461 conn->smb2.server.max_write_size= IVAL(body, 36);
4462 conn->smb2.server.system_time = BVAL(body, 40);
4463 conn->smb2.server.start_time = BVAL(body, 48);
4465 security_offset = SVAL(body, 56);
4466 security_length = SVAL(body, 58);
4468 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4469 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4473 if (security_length > iov[2].iov_len) {
4474 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4478 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4481 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4485 tevent_req_done(req);
4488 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4489 TALLOC_CTX *tmp_mem,
4492 size_t num_pending = talloc_array_length(conn->pending);
4493 struct tevent_req *subreq;
4494 struct smbXcli_req_state *substate;
4495 struct tevent_req *req;
4496 uint32_t protocol_magic;
4497 size_t inbuf_len = smb_len_nbt(inbuf);
4499 if (num_pending != 1) {
4500 return NT_STATUS_INTERNAL_ERROR;
4503 if (inbuf_len < 4) {
4504 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4507 subreq = conn->pending[0];
4508 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4509 req = tevent_req_callback_data(subreq, struct tevent_req);
4511 protocol_magic = IVAL(inbuf, 4);
4513 switch (protocol_magic) {
4515 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4516 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4517 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4520 if (substate->smb2.recv_iov == NULL) {
4522 * For the SMB1 negprot we have move it.
4524 substate->smb2.recv_iov = substate->smb1.recv_iov;
4525 substate->smb1.recv_iov = NULL;
4529 * we got an SMB2 answer, which consumed sequence number 0
4530 * so we need to use 1 as the next one.
4532 * we also need to set the current credits to 0
4533 * as we consumed the initial one. The SMB2 answer
4534 * hopefully grant us a new credit.
4537 conn->smb2.cur_credits = 0;
4538 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4539 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4540 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4543 DEBUG(10, ("Got non-SMB PDU\n"));
4544 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4547 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4549 return tevent_req_simple_recv_ntstatus(req);
4552 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4553 uint32_t timeout_msec,
4554 enum protocol_types min_protocol,
4555 enum protocol_types max_protocol)
4557 TALLOC_CTX *frame = talloc_stackframe();
4558 struct tevent_context *ev;
4559 struct tevent_req *req;
4560 NTSTATUS status = NT_STATUS_NO_MEMORY;
4563 if (smbXcli_conn_has_async_calls(conn)) {
4565 * Can't use sync call while an async call is in flight
4567 status = NT_STATUS_INVALID_PARAMETER_MIX;
4570 ev = samba_tevent_context_init(frame);
4574 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4575 min_protocol, max_protocol);
4579 ok = tevent_req_poll(req, ev);
4581 status = map_nt_error_from_unix_common(errno);
4584 status = smbXcli_negprot_recv(req);
4590 static int smbXcli_session_destructor(struct smbXcli_session *session)
4592 if (session->conn == NULL) {
4596 DLIST_REMOVE(session->conn->sessions, session);
4600 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4601 struct smbXcli_conn *conn)
4603 struct smbXcli_session *session;
4605 session = talloc_zero(mem_ctx, struct smbXcli_session);
4606 if (session == NULL) {
4609 session->smb2 = talloc_zero(session, struct smb2cli_session);
4610 if (session->smb2 == NULL) {
4611 talloc_free(session);
4614 talloc_set_destructor(session, smbXcli_session_destructor);
4616 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4617 session->conn = conn;
4622 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4623 struct smbXcli_session *src)
4625 struct smbXcli_session *session;
4627 session = talloc_zero(mem_ctx, struct smbXcli_session);
4628 if (session == NULL) {
4631 session->smb2 = talloc_zero(session, struct smb2cli_session);
4632 if (session->smb2 == NULL) {
4633 talloc_free(session);
4637 session->conn = src->conn;
4638 *session->smb2 = *src->smb2;
4639 session->smb2_channel = src->smb2_channel;
4640 session->disconnect_expired = src->disconnect_expired;
4642 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4643 talloc_set_destructor(session, smbXcli_session_destructor);
4648 bool smbXcli_session_is_authenticated(struct smbXcli_session *session)
4650 const DATA_BLOB *application_key;
4652 if (session->conn == NULL) {
4657 * If we have an application key we had a session key negotiated
4660 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4661 application_key = &session->smb2->application_key;
4663 application_key = &session->smb1.application_key;
4666 if (application_key->length == 0) {
4673 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4674 TALLOC_CTX *mem_ctx,
4677 const DATA_BLOB *application_key;
4679 *key = data_blob_null;
4681 if (session->conn == NULL) {
4682 return NT_STATUS_NO_USER_SESSION_KEY;
4685 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4686 application_key = &session->smb2->application_key;
4688 application_key = &session->smb1.application_key;
4691 if (application_key->length == 0) {
4692 return NT_STATUS_NO_USER_SESSION_KEY;
4695 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4696 if (key->data == NULL) {
4697 return NT_STATUS_NO_MEMORY;
4700 return NT_STATUS_OK;
4703 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4705 session->disconnect_expired = true;
4708 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4710 return session->smb1.session_id;
4713 void smb1cli_session_set_id(struct smbXcli_session *session,
4714 uint16_t session_id)
4716 session->smb1.session_id = session_id;
4719 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4720 const DATA_BLOB _session_key)
4722 struct smbXcli_conn *conn = session->conn;
4723 uint8_t session_key[16];
4726 return NT_STATUS_INVALID_PARAMETER_MIX;
4729 if (session->smb1.application_key.length != 0) {
4731 * TODO: do not allow this...
4733 * return NT_STATUS_INVALID_PARAMETER_MIX;
4735 data_blob_clear_free(&session->smb1.application_key);
4736 session->smb1.protected_key = false;
4739 if (_session_key.length == 0) {
4740 return NT_STATUS_OK;
4743 ZERO_STRUCT(session_key);
4744 memcpy(session_key, _session_key.data,
4745 MIN(_session_key.length, sizeof(session_key)));
4747 session->smb1.application_key = data_blob_talloc(session,
4749 sizeof(session_key));
4750 ZERO_STRUCT(session_key);
4751 if (session->smb1.application_key.data == NULL) {
4752 return NT_STATUS_NO_MEMORY;
4755 session->smb1.protected_key = false;
4757 return NT_STATUS_OK;
4760 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4762 if (session->smb1.protected_key) {
4763 /* already protected */
4764 return NT_STATUS_OK;
4767 if (session->smb1.application_key.length != 16) {
4768 return NT_STATUS_INVALID_PARAMETER_MIX;
4771 smb_key_derivation(session->smb1.application_key.data,
4772 session->smb1.application_key.length,
4773 session->smb1.application_key.data);
4775 session->smb1.protected_key = true;
4777 return NT_STATUS_OK;
4780 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4782 struct smbXcli_conn *conn = session->conn;
4783 uint8_t security_mode = 0;
4786 return security_mode;
4789 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4790 if (conn->mandatory_signing) {
4791 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4794 return security_mode;
4797 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4799 return session->smb2->session_id;
4802 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4804 return session->smb2->session_flags;
4807 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4808 uint64_t session_id,
4809 uint16_t session_flags)
4811 session->smb2->session_id = session_id;
4812 session->smb2->session_flags = session_flags;
4815 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4817 session->smb2->channel_sequence += 1;
4820 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session *session,
4821 uint16_t channel_sequence)
4825 prev_cs = session->smb2->channel_sequence;
4826 session->smb2->channel_sequence = channel_sequence;
4831 void smb2cli_session_start_replay(struct smbXcli_session *session)
4833 session->smb2->replay_active = true;
4836 void smb2cli_session_stop_replay(struct smbXcli_session *session)
4838 session->smb2->replay_active = false;
4841 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4842 const DATA_BLOB _session_key,
4843 const struct iovec *recv_iov)
4845 struct smbXcli_conn *conn = session->conn;
4846 uint16_t no_sign_flags;
4847 uint8_t session_key[16];
4848 bool check_signature = true;
4853 return NT_STATUS_INVALID_PARAMETER_MIX;
4856 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
4857 return NT_STATUS_INVALID_PARAMETER_MIX;
4860 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4862 if (session->smb2->session_flags & no_sign_flags) {
4863 session->smb2->should_sign = false;
4864 return NT_STATUS_OK;
4867 if (session->smb2->signing_key.length != 0) {
4868 return NT_STATUS_INVALID_PARAMETER_MIX;
4871 ZERO_STRUCT(session_key);
4872 memcpy(session_key, _session_key.data,
4873 MIN(_session_key.length, sizeof(session_key)));
4875 session->smb2->signing_key = data_blob_talloc(session,
4877 sizeof(session_key));
4878 if (session->smb2->signing_key.data == NULL) {
4879 ZERO_STRUCT(session_key);
4880 return NT_STATUS_NO_MEMORY;
4883 if (conn->protocol >= PROTOCOL_SMB2_24) {
4884 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4885 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4887 smb2_key_derivation(session_key, sizeof(session_key),
4888 label.data, label.length,
4889 context.data, context.length,
4890 session->smb2->signing_key.data);
4893 session->smb2->encryption_key = data_blob_dup_talloc(session,
4894 session->smb2->signing_key);
4895 if (session->smb2->encryption_key.data == NULL) {
4896 ZERO_STRUCT(session_key);
4897 return NT_STATUS_NO_MEMORY;
4900 if (conn->protocol >= PROTOCOL_SMB2_24) {
4901 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4902 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4904 smb2_key_derivation(session_key, sizeof(session_key),
4905 label.data, label.length,
4906 context.data, context.length,
4907 session->smb2->encryption_key.data);
4910 session->smb2->decryption_key = data_blob_dup_talloc(session,
4911 session->smb2->signing_key);
4912 if (session->smb2->decryption_key.data == NULL) {
4913 ZERO_STRUCT(session_key);
4914 return NT_STATUS_NO_MEMORY;
4917 if (conn->protocol >= PROTOCOL_SMB2_24) {
4918 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4919 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4921 smb2_key_derivation(session_key, sizeof(session_key),
4922 label.data, label.length,
4923 context.data, context.length,
4924 session->smb2->decryption_key.data);
4927 session->smb2->application_key = data_blob_dup_talloc(session,
4928 session->smb2->signing_key);
4929 if (session->smb2->application_key.data == NULL) {
4930 ZERO_STRUCT(session_key);
4931 return NT_STATUS_NO_MEMORY;
4934 if (conn->protocol >= PROTOCOL_SMB2_24) {
4935 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4936 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4938 smb2_key_derivation(session_key, sizeof(session_key),
4939 label.data, label.length,
4940 context.data, context.length,
4941 session->smb2->application_key.data);
4943 ZERO_STRUCT(session_key);
4945 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4946 session->smb2->signing_key);
4947 if (session->smb2_channel.signing_key.data == NULL) {
4948 return NT_STATUS_NO_MEMORY;
4951 check_signature = conn->mandatory_signing;
4953 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
4954 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
4956 * Sadly some vendors don't sign the
4957 * final SMB2 session setup response
4959 * At least Windows and Samba are always doing this
4960 * if there's a session key available.
4962 * We only check the signature if it's mandatory
4963 * or SMB2_HDR_FLAG_SIGNED is provided.
4965 check_signature = true;
4968 if (check_signature) {
4969 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4970 session->conn->protocol,
4972 if (!NT_STATUS_IS_OK(status)) {
4977 session->smb2->should_sign = false;
4978 session->smb2->should_encrypt = false;
4980 if (conn->desire_signing) {
4981 session->smb2->should_sign = true;
4984 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4985 session->smb2->should_sign = true;
4988 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4989 session->smb2->should_encrypt = true;
4992 if (conn->protocol < PROTOCOL_SMB2_24) {
4993 session->smb2->should_encrypt = false;
4996 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4997 session->smb2->should_encrypt = false;
5000 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
5001 sizeof(session->smb2->nonce_high));
5002 session->smb2->nonce_low = 1;
5004 return NT_STATUS_OK;
5007 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
5008 struct smbXcli_session *session1,
5009 struct smbXcli_conn *conn,
5010 struct smbXcli_session **_session2)
5012 struct smbXcli_session *session2;
5014 if (session1->smb2->signing_key.length == 0) {
5015 return NT_STATUS_INVALID_PARAMETER_MIX;
5019 return NT_STATUS_INVALID_PARAMETER_MIX;
5022 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
5023 if (session2 == NULL) {
5024 return NT_STATUS_NO_MEMORY;
5026 session2->smb2 = talloc_reference(session2, session1->smb2);
5027 if (session2->smb2 == NULL) {
5028 talloc_free(session2);
5029 return NT_STATUS_NO_MEMORY;
5032 talloc_set_destructor(session2, smbXcli_session_destructor);
5033 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
5034 session2->conn = conn;
5036 *_session2 = session2;
5037 return NT_STATUS_OK;
5040 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
5041 const DATA_BLOB _channel_key,
5042 const struct iovec *recv_iov)
5044 struct smbXcli_conn *conn = session->conn;
5045 uint8_t channel_key[16];
5049 return NT_STATUS_INVALID_PARAMETER_MIX;
5052 if (session->smb2_channel.signing_key.length != 0) {
5053 return NT_STATUS_INVALID_PARAMETER_MIX;
5056 ZERO_STRUCT(channel_key);
5057 memcpy(channel_key, _channel_key.data,
5058 MIN(_channel_key.length, sizeof(channel_key)));
5060 session->smb2_channel.signing_key = data_blob_talloc(session,
5062 sizeof(channel_key));
5063 if (session->smb2_channel.signing_key.data == NULL) {
5064 ZERO_STRUCT(channel_key);
5065 return NT_STATUS_NO_MEMORY;
5068 if (conn->protocol >= PROTOCOL_SMB2_24) {
5069 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
5070 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
5072 smb2_key_derivation(channel_key, sizeof(channel_key),
5073 label.data, label.length,
5074 context.data, context.length,
5075 session->smb2_channel.signing_key.data);
5077 ZERO_STRUCT(channel_key);
5079 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5080 session->conn->protocol,
5082 if (!NT_STATUS_IS_OK(status)) {
5086 return NT_STATUS_OK;
5089 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
5091 if (session->smb2->should_encrypt) {
5092 return NT_STATUS_OK;
5095 if (session->conn->protocol < PROTOCOL_SMB2_24) {
5096 return NT_STATUS_NOT_SUPPORTED;
5099 if (!(session->conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
5100 return NT_STATUS_NOT_SUPPORTED;
5103 if (session->smb2->signing_key.data == NULL) {
5104 return NT_STATUS_NOT_SUPPORTED;
5106 session->smb2->should_encrypt = true;
5107 return NT_STATUS_OK;
5110 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
5112 struct smbXcli_tcon *tcon;
5114 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
5122 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
5123 uint32_t fs_attributes)
5125 tcon->fs_attributes = fs_attributes;
5128 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
5130 return tcon->fs_attributes;
5133 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
5139 if (tcon->is_smb1) {
5140 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
5147 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
5154 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
5156 return tcon->smb1.tcon_id;
5159 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
5161 tcon->is_smb1 = true;
5162 tcon->smb1.tcon_id = tcon_id;
5165 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5167 uint16_t optional_support,
5168 uint32_t maximal_access,
5169 uint32_t guest_maximal_access,
5170 const char *service,
5171 const char *fs_type)
5173 tcon->is_smb1 = true;
5174 tcon->fs_attributes = 0;
5175 tcon->smb1.tcon_id = tcon_id;
5176 tcon->smb1.optional_support = optional_support;
5177 tcon->smb1.maximal_access = maximal_access;
5178 tcon->smb1.guest_maximal_access = guest_maximal_access;
5180 TALLOC_FREE(tcon->smb1.service);
5181 tcon->smb1.service = talloc_strdup(tcon, service);
5182 if (service != NULL && tcon->smb1.service == NULL) {
5186 TALLOC_FREE(tcon->smb1.fs_type);
5187 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5188 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5195 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5197 return tcon->smb2.tcon_id;
5200 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5202 return tcon->smb2.capabilities;
5205 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5206 struct smbXcli_session *session,
5210 uint32_t capabilities,
5211 uint32_t maximal_access)
5213 tcon->is_smb1 = false;
5214 tcon->fs_attributes = 0;
5215 tcon->smb2.tcon_id = tcon_id;
5216 tcon->smb2.type = type;
5217 tcon->smb2.flags = flags;
5218 tcon->smb2.capabilities = capabilities;
5219 tcon->smb2.maximal_access = maximal_access;
5221 tcon->smb2.should_sign = false;
5222 tcon->smb2.should_encrypt = false;
5224 if (session == NULL) {
5228 tcon->smb2.should_sign = session->smb2->should_sign;
5229 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5231 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5232 tcon->smb2.should_encrypt = true;
5236 void smb2cli_tcon_should_sign(struct smbXcli_tcon *tcon,
5239 tcon->smb2.should_sign = should_sign;
5242 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon *tcon)
5244 if (tcon->smb2.should_encrypt) {
5248 return tcon->smb2.should_sign;
5251 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon *tcon,
5252 bool should_encrypt)
5254 tcon->smb2.should_encrypt = should_encrypt;
5257 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
5259 return tcon->smb2.should_encrypt;