2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "../libcli/smb/smb_common.h"
29 #include "../libcli/smb/smb_seal.h"
30 #include "../libcli/smb/smb_signing.h"
31 #include "../libcli/smb/read_smb.h"
32 #include "smbXcli_base.h"
33 #include "librpc/ndr/libndr.h"
37 struct smbXcli_session;
43 struct sockaddr_storage local_ss;
44 struct sockaddr_storage remote_ss;
45 const char *remote_name;
47 struct tevent_queue *outgoing;
48 struct tevent_req **pending;
49 struct tevent_req *read_smb_req;
51 enum protocol_types protocol;
54 bool mandatory_signing;
57 * The incoming dispatch function should return:
58 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
59 * - NT_STATUS_OK, if no more processing is desired, e.g.
60 * the dispatch function called
62 * - All other return values disconnect the connection.
64 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
70 uint32_t capabilities;
75 uint32_t capabilities;
78 uint16_t security_mode;
87 const char *workgroup;
93 uint32_t capabilities;
98 struct smb_signing_state *signing;
99 struct smb_trans_enc_state *trans_enc;
101 struct tevent_req *read_braw_req;
106 uint32_t capabilities;
107 uint16_t security_mode;
112 uint32_t capabilities;
113 uint16_t security_mode;
115 uint32_t max_trans_size;
116 uint32_t max_read_size;
117 uint32_t max_write_size;
124 uint16_t cur_credits;
125 uint16_t max_credits;
128 struct smbXcli_session *sessions;
131 struct smb2cli_session {
133 uint16_t session_flags;
134 DATA_BLOB application_key;
135 DATA_BLOB signing_key;
138 DATA_BLOB encryption_key;
139 DATA_BLOB decryption_key;
142 uint16_t channel_sequence;
145 struct smbXcli_session {
146 struct smbXcli_session *prev, *next;
147 struct smbXcli_conn *conn;
151 DATA_BLOB application_key;
155 struct smb2cli_session *smb2;
158 DATA_BLOB signing_key;
162 * this should be a short term hack
163 * until the upper layers have implemented
166 bool disconnect_expired;
169 struct smbXcli_tcon {
172 uint16_t optional_support;
173 uint32_t maximal_access;
174 uint32_t guest_maximal_access;
183 uint32_t capabilities;
184 uint32_t maximal_access;
189 struct smbXcli_req_state {
190 struct tevent_context *ev;
191 struct smbXcli_conn *conn;
192 struct smbXcli_session *session; /* maybe NULL */
193 struct smbXcli_tcon *tcon; /* maybe NULL */
195 uint8_t length_hdr[4];
202 /* Space for the header including the wct */
203 uint8_t hdr[HDR_VWV];
206 * For normal requests, smb1cli_req_send chooses a mid.
207 * SecondaryV trans requests need to use the mid of the primary
208 * request, so we need a place to store it.
209 * Assume it is set if != 0.
214 uint8_t bytecount_buf[2];
216 #define MAX_SMB_IOV 10
217 /* length_hdr, hdr, words, byte_count, buffers */
218 struct iovec iov[1 + 3 + MAX_SMB_IOV];
223 struct tevent_req **chained_requests;
226 NTSTATUS recv_status;
227 /* always an array of 3 talloc elements */
228 struct iovec *recv_iov;
232 const uint8_t *fixed;
237 uint8_t transform[SMB2_TF_HDR_SIZE];
238 uint8_t hdr[SMB2_HDR_BODY];
239 uint8_t pad[7]; /* padding space for compounding */
242 * always an array of 3 talloc elements
243 * (without a SMB2_TRANSFORM header!)
247 struct iovec *recv_iov;
250 * the expected max for the response dyn_len
252 uint32_t max_dyn_len;
254 uint16_t credit_charge;
258 uint64_t encryption_session_id;
260 bool signing_skipped;
266 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
269 * NT_STATUS_OK, means we do not notify the callers
271 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
273 while (conn->sessions) {
274 conn->sessions->conn = NULL;
275 DLIST_REMOVE(conn->sessions, conn->sessions);
278 if (conn->smb1.trans_enc) {
279 TALLOC_FREE(conn->smb1.trans_enc);
285 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
287 const char *remote_name,
288 enum smb_signing_setting signing_state,
289 uint32_t smb1_capabilities,
290 struct GUID *client_guid,
291 uint32_t smb2_capabilities)
293 struct smbXcli_conn *conn = NULL;
295 struct sockaddr *sa = NULL;
299 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
305 conn->write_fd = dup(fd);
306 if (conn->write_fd == -1) {
310 conn->remote_name = talloc_strdup(conn, remote_name);
311 if (conn->remote_name == NULL) {
316 ss = (void *)&conn->local_ss;
317 sa = (struct sockaddr *)ss;
318 sa_length = sizeof(conn->local_ss);
319 ret = getsockname(fd, sa, &sa_length);
323 ss = (void *)&conn->remote_ss;
324 sa = (struct sockaddr *)ss;
325 sa_length = sizeof(conn->remote_ss);
326 ret = getpeername(fd, sa, &sa_length);
331 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
332 if (conn->outgoing == NULL) {
335 conn->pending = NULL;
337 conn->protocol = PROTOCOL_NONE;
339 switch (signing_state) {
340 case SMB_SIGNING_OFF:
342 conn->allow_signing = false;
343 conn->desire_signing = false;
344 conn->mandatory_signing = false;
346 case SMB_SIGNING_DEFAULT:
347 case SMB_SIGNING_IF_REQUIRED:
348 /* if the server requires it */
349 conn->allow_signing = true;
350 conn->desire_signing = false;
351 conn->mandatory_signing = false;
353 case SMB_SIGNING_REQUIRED:
355 conn->allow_signing = true;
356 conn->desire_signing = true;
357 conn->mandatory_signing = true;
361 conn->smb1.client.capabilities = smb1_capabilities;
362 conn->smb1.client.max_xmit = UINT16_MAX;
364 conn->smb1.capabilities = conn->smb1.client.capabilities;
365 conn->smb1.max_xmit = 1024;
369 /* initialise signing */
370 conn->smb1.signing = smb_signing_init(conn,
372 conn->desire_signing,
373 conn->mandatory_signing);
374 if (!conn->smb1.signing) {
378 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
379 if (conn->mandatory_signing) {
380 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
383 conn->smb2.client.guid = *client_guid;
385 conn->smb2.client.capabilities = smb2_capabilities;
387 conn->smb2.cur_credits = 1;
388 conn->smb2.max_credits = 0;
390 talloc_set_destructor(conn, smbXcli_conn_destructor);
394 if (conn->write_fd != -1) {
395 close(conn->write_fd);
401 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
407 if (conn->read_fd == -1) {
414 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
416 return conn->protocol;
419 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
421 if (conn->protocol >= PROTOCOL_SMB2_02) {
425 if (conn->smb1.capabilities & CAP_UNICODE) {
432 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
434 set_socket_options(conn->read_fd, options);
437 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
439 return &conn->local_ss;
442 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
444 return &conn->remote_ss;
447 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
449 return conn->remote_name;
452 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
454 if (conn->protocol >= PROTOCOL_SMB2_02) {
461 return conn->smb1.server.max_mux;
464 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
466 if (conn->protocol >= PROTOCOL_SMB2_02) {
467 return conn->smb2.server.system_time;
470 return conn->smb1.server.system_time;
473 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
475 if (conn->protocol >= PROTOCOL_SMB2_02) {
476 return &conn->smb2.server.gss_blob;
479 return &conn->smb1.server.gss_blob;
482 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
484 if (conn->protocol >= PROTOCOL_SMB2_02) {
485 return &conn->smb2.server.guid;
488 return &conn->smb1.server.guid;
491 struct smbXcli_conn_samba_suicide_state {
492 struct smbXcli_conn *conn;
497 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
499 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
500 struct tevent_context *ev,
501 struct smbXcli_conn *conn,
504 struct tevent_req *req, *subreq;
505 struct smbXcli_conn_samba_suicide_state *state;
507 req = tevent_req_create(mem_ctx, &state,
508 struct smbXcli_conn_samba_suicide_state);
513 SIVAL(state->buf, 4, 0x74697865);
514 SCVAL(state->buf, 8, exitcode);
515 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
517 state->iov.iov_base = state->buf;
518 state->iov.iov_len = sizeof(state->buf);
520 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
521 false, &state->iov, 1);
522 if (tevent_req_nomem(subreq, req)) {
523 return tevent_req_post(req, ev);
525 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
529 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
531 struct tevent_req *req = tevent_req_callback_data(
532 subreq, struct tevent_req);
533 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
534 req, struct smbXcli_conn_samba_suicide_state);
538 nwritten = writev_recv(subreq, &err);
540 if (nwritten == -1) {
541 NTSTATUS status = map_nt_error_from_unix_common(err);
542 smbXcli_conn_disconnect(state->conn, status);
545 tevent_req_done(req);
548 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
550 return tevent_req_simple_recv_ntstatus(req);
553 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
556 TALLOC_CTX *frame = talloc_stackframe();
557 struct tevent_context *ev;
558 struct tevent_req *req;
559 NTSTATUS status = NT_STATUS_NO_MEMORY;
562 if (smbXcli_conn_has_async_calls(conn)) {
564 * Can't use sync call while an async call is in flight
566 status = NT_STATUS_INVALID_PARAMETER_MIX;
569 ev = samba_tevent_context_init(frame);
573 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
577 ok = tevent_req_poll(req, ev);
579 status = map_nt_error_from_unix_common(errno);
582 status = smbXcli_conn_samba_suicide_recv(req);
588 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
590 return conn->smb1.capabilities;
593 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
595 return conn->smb1.max_xmit;
598 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
601 uint16_t possible = conn->smb1.server.max_mux;
603 pending = tevent_queue_length(conn->outgoing);
604 if (pending >= possible) {
607 pending += talloc_array_length(conn->pending);
608 if (pending >= possible) {
615 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
617 return conn->smb1.server.session_key;
620 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
622 return conn->smb1.server.challenge;
625 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
627 return conn->smb1.server.security_mode;
630 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
632 return conn->smb1.server.readbraw;
635 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
637 return conn->smb1.server.writebraw;
640 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
642 return conn->smb1.server.lockread;
645 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
647 return conn->smb1.server.writeunlock;
650 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
652 return conn->smb1.server.time_zone;
655 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
656 const DATA_BLOB user_session_key,
657 const DATA_BLOB response)
659 return smb_signing_activate(conn->smb1.signing,
664 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
665 const uint8_t *buf, uint32_t seqnum)
667 const uint8_t *hdr = buf + NBT_HDR_SIZE;
668 size_t len = smb_len_nbt(buf);
670 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
673 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
675 return smb_signing_is_active(conn->smb1.signing);
678 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
679 struct smb_trans_enc_state *es)
681 /* Replace the old state, if any. */
682 if (conn->smb1.trans_enc) {
683 TALLOC_FREE(conn->smb1.trans_enc);
685 conn->smb1.trans_enc = es;
688 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
690 return common_encryption_on(conn->smb1.trans_enc);
694 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
696 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
697 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
699 if (NT_STATUS_IS_OK(status)) {
703 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
707 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
711 * Is the SMB command able to hold an AND_X successor
712 * @param[in] cmd The SMB command in question
713 * @retval Can we add a chained request after "cmd"?
715 bool smb1cli_is_andx_req(uint8_t cmd)
735 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
737 size_t num_pending = talloc_array_length(conn->pending);
743 result = conn->smb1.mid++;
744 if ((result == 0) || (result == 0xffff)) {
748 for (i=0; i<num_pending; i++) {
749 if (result == smb1cli_req_mid(conn->pending[i])) {
754 if (i == num_pending) {
760 void smbXcli_req_unset_pending(struct tevent_req *req)
762 struct smbXcli_req_state *state =
764 struct smbXcli_req_state);
765 struct smbXcli_conn *conn = state->conn;
766 size_t num_pending = talloc_array_length(conn->pending);
769 if (state->smb1.mid != 0) {
771 * This is a [nt]trans[2] request which waits
772 * for more than one reply.
777 talloc_set_destructor(req, NULL);
779 if (num_pending == 1) {
781 * The pending read_smb tevent_req is a child of
782 * conn->pending. So if nothing is pending anymore, we need to
783 * delete the socket read fde.
785 TALLOC_FREE(conn->pending);
786 conn->read_smb_req = NULL;
790 for (i=0; i<num_pending; i++) {
791 if (req == conn->pending[i]) {
795 if (i == num_pending) {
797 * Something's seriously broken. Just returning here is the
798 * right thing nevertheless, the point of this routine is to
799 * remove ourselves from conn->pending.
805 * Remove ourselves from the conn->pending array
807 for (; i < (num_pending - 1); i++) {
808 conn->pending[i] = conn->pending[i+1];
812 * No NULL check here, we're shrinking by sizeof(void *), and
813 * talloc_realloc just adjusts the size for this.
815 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
820 static int smbXcli_req_destructor(struct tevent_req *req)
822 struct smbXcli_req_state *state =
824 struct smbXcli_req_state);
827 * Make sure we really remove it from
828 * the pending array on destruction.
831 smbXcli_req_unset_pending(req);
835 static bool smb1cli_req_cancel(struct tevent_req *req);
836 static bool smb2cli_req_cancel(struct tevent_req *req);
838 static bool smbXcli_req_cancel(struct tevent_req *req)
840 struct smbXcli_req_state *state =
842 struct smbXcli_req_state);
844 if (!smbXcli_conn_is_connected(state->conn)) {
848 if (state->conn->protocol == PROTOCOL_NONE) {
852 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
853 return smb2cli_req_cancel(req);
856 return smb1cli_req_cancel(req);
859 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
861 bool smbXcli_req_set_pending(struct tevent_req *req)
863 struct smbXcli_req_state *state =
865 struct smbXcli_req_state);
866 struct smbXcli_conn *conn;
867 struct tevent_req **pending;
872 if (!smbXcli_conn_is_connected(conn)) {
876 num_pending = talloc_array_length(conn->pending);
878 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
880 if (pending == NULL) {
883 pending[num_pending] = req;
884 conn->pending = pending;
885 talloc_set_destructor(req, smbXcli_req_destructor);
886 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
888 if (!smbXcli_conn_receive_next(conn)) {
890 * the caller should notify the current request
892 * And all other pending requests get notified
893 * by smbXcli_conn_disconnect().
895 smbXcli_req_unset_pending(req);
896 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
903 static void smbXcli_conn_received(struct tevent_req *subreq);
905 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
907 size_t num_pending = talloc_array_length(conn->pending);
908 struct tevent_req *req;
909 struct smbXcli_req_state *state;
911 if (conn->read_smb_req != NULL) {
915 if (num_pending == 0) {
916 if (conn->smb2.mid < UINT64_MAX) {
917 /* no more pending requests, so we are done for now */
922 * If there are no more SMB2 requests possible,
923 * because we are out of message ids,
924 * we need to disconnect.
926 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
930 req = conn->pending[0];
931 state = tevent_req_data(req, struct smbXcli_req_state);
934 * We're the first ones, add the read_smb request that waits for the
935 * answer from the server
937 conn->read_smb_req = read_smb_send(conn->pending,
940 if (conn->read_smb_req == NULL) {
943 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
947 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
949 struct smbXcli_session *session;
951 tevent_queue_stop(conn->outgoing);
953 if (conn->read_fd != -1) {
954 close(conn->read_fd);
956 if (conn->write_fd != -1) {
957 close(conn->write_fd);
962 session = conn->sessions;
963 if (talloc_array_length(conn->pending) == 0) {
965 * if we do not have pending requests
966 * there is no need to update the channel_sequence
970 for (; session; session = session->next) {
971 smb2cli_session_increment_channel_sequence(session);
975 * Cancel all pending requests. We do not do a for-loop walking
976 * conn->pending because that array changes in
977 * smbXcli_req_unset_pending.
979 while (talloc_array_length(conn->pending) > 0) {
980 struct tevent_req *req;
981 struct smbXcli_req_state *state;
982 struct tevent_req **chain;
986 req = conn->pending[0];
987 state = tevent_req_data(req, struct smbXcli_req_state);
989 if (state->smb1.chained_requests == NULL) {
991 * We're dead. No point waiting for trans2
996 smbXcli_req_unset_pending(req);
998 if (NT_STATUS_IS_OK(status)) {
999 /* do not notify the callers */
1004 * we need to defer the callback, because we may notify
1005 * more then one caller.
1007 tevent_req_defer_callback(req, state->ev);
1008 tevent_req_nterror(req, status);
1012 chain = talloc_move(conn, &state->smb1.chained_requests);
1013 num_chained = talloc_array_length(chain);
1015 for (i=0; i<num_chained; i++) {
1017 state = tevent_req_data(req, struct smbXcli_req_state);
1020 * We're dead. No point waiting for trans2
1023 state->smb1.mid = 0;
1025 smbXcli_req_unset_pending(req);
1027 if (NT_STATUS_IS_OK(status)) {
1028 /* do not notify the callers */
1033 * we need to defer the callback, because we may notify
1034 * more than one caller.
1036 tevent_req_defer_callback(req, state->ev);
1037 tevent_req_nterror(req, status);
1044 * Fetch a smb request's mid. Only valid after the request has been sent by
1045 * smb1cli_req_send().
1047 uint16_t smb1cli_req_mid(struct tevent_req *req)
1049 struct smbXcli_req_state *state =
1050 tevent_req_data(req,
1051 struct smbXcli_req_state);
1053 if (state->smb1.mid != 0) {
1054 return state->smb1.mid;
1057 return SVAL(state->smb1.hdr, HDR_MID);
1060 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1062 struct smbXcli_req_state *state =
1063 tevent_req_data(req,
1064 struct smbXcli_req_state);
1066 state->smb1.mid = mid;
1069 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1071 struct smbXcli_req_state *state =
1072 tevent_req_data(req,
1073 struct smbXcli_req_state);
1075 return state->smb1.seqnum;
1078 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1080 struct smbXcli_req_state *state =
1081 tevent_req_data(req,
1082 struct smbXcli_req_state);
1084 state->smb1.seqnum = seqnum;
1087 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1091 for (i=0; i<count; i++) {
1092 result += iov[i].iov_len;
1097 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1098 const struct iovec *iov,
1101 size_t len = smbXcli_iov_len(iov, count);
1106 buf = talloc_array(mem_ctx, uint8_t, len);
1111 for (i=0; i<count; i++) {
1112 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1113 copied += iov[i].iov_len;
1118 static void smb1cli_req_flags(enum protocol_types protocol,
1119 uint32_t smb1_capabilities,
1120 uint8_t smb_command,
1121 uint8_t additional_flags,
1122 uint8_t clear_flags,
1124 uint16_t additional_flags2,
1125 uint16_t clear_flags2,
1129 uint16_t flags2 = 0;
1131 if (protocol >= PROTOCOL_LANMAN1) {
1132 flags |= FLAG_CASELESS_PATHNAMES;
1133 flags |= FLAG_CANONICAL_PATHNAMES;
1136 if (protocol >= PROTOCOL_LANMAN2) {
1137 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1138 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1141 if (protocol >= PROTOCOL_NT1) {
1142 flags2 |= FLAGS2_IS_LONG_NAME;
1144 if (smb1_capabilities & CAP_UNICODE) {
1145 flags2 |= FLAGS2_UNICODE_STRINGS;
1147 if (smb1_capabilities & CAP_STATUS32) {
1148 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1150 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1151 flags2 |= FLAGS2_EXTENDED_SECURITY;
1155 flags |= additional_flags;
1156 flags &= ~clear_flags;
1157 flags2 |= additional_flags2;
1158 flags2 &= ~clear_flags2;
1164 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1166 static bool smb1cli_req_cancel(struct tevent_req *req)
1168 struct smbXcli_req_state *state =
1169 tevent_req_data(req,
1170 struct smbXcli_req_state);
1175 struct tevent_req *subreq;
1178 flags = CVAL(state->smb1.hdr, HDR_FLG);
1179 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1180 pid = SVAL(state->smb1.hdr, HDR_PID);
1181 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1182 mid = SVAL(state->smb1.hdr, HDR_MID);
1184 subreq = smb1cli_req_create(state, state->ev,
1194 0, NULL); /* bytes */
1195 if (subreq == NULL) {
1198 smb1cli_req_set_mid(subreq, mid);
1200 status = smb1cli_req_chain_submit(&subreq, 1);
1201 if (!NT_STATUS_IS_OK(status)) {
1202 TALLOC_FREE(subreq);
1205 smb1cli_req_set_mid(subreq, 0);
1207 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1212 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1214 /* we do not care about the result */
1215 TALLOC_FREE(subreq);
1218 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1219 struct tevent_context *ev,
1220 struct smbXcli_conn *conn,
1221 uint8_t smb_command,
1222 uint8_t additional_flags,
1223 uint8_t clear_flags,
1224 uint16_t additional_flags2,
1225 uint16_t clear_flags2,
1226 uint32_t timeout_msec,
1228 struct smbXcli_tcon *tcon,
1229 struct smbXcli_session *session,
1230 uint8_t wct, uint16_t *vwv,
1232 struct iovec *bytes_iov)
1234 struct tevent_req *req;
1235 struct smbXcli_req_state *state;
1237 uint16_t flags2 = 0;
1241 if (iov_count > MAX_SMB_IOV) {
1243 * Should not happen :-)
1248 req = tevent_req_create(mem_ctx, &state,
1249 struct smbXcli_req_state);
1255 state->session = session;
1259 uid = session->smb1.session_id;
1263 tid = tcon->smb1.tcon_id;
1266 state->smb1.recv_cmd = 0xFF;
1267 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1268 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1269 if (state->smb1.recv_iov == NULL) {
1274 smb1cli_req_flags(conn->protocol,
1275 conn->smb1.capabilities,
1284 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1285 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1286 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1287 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1288 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1289 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1290 SSVAL(state->smb1.hdr, HDR_TID, tid);
1291 SSVAL(state->smb1.hdr, HDR_PID, pid);
1292 SSVAL(state->smb1.hdr, HDR_UID, uid);
1293 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1294 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1296 state->smb1.vwv = vwv;
1298 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1300 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1301 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1302 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1303 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1304 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1305 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1306 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1307 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1309 if (iov_count != 0) {
1310 memcpy(&state->smb1.iov[4], bytes_iov,
1311 iov_count * sizeof(*bytes_iov));
1313 state->smb1.iov_count = iov_count + 4;
1315 if (timeout_msec > 0) {
1316 struct timeval endtime;
1318 endtime = timeval_current_ofs_msec(timeout_msec);
1319 if (!tevent_req_set_endtime(req, ev, endtime)) {
1324 switch (smb_command) {
1328 state->one_way = true;
1331 state->one_way = true;
1332 state->smb1.one_way_seqnum = true;
1336 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1337 state->one_way = true;
1345 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1346 struct iovec *iov, int iov_count,
1348 bool one_way_seqnum)
1350 TALLOC_CTX *frame = NULL;
1354 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1355 * iovec directly. MD5Update would do that just fine.
1358 if (iov_count < 4) {
1359 return NT_STATUS_INVALID_PARAMETER_MIX;
1361 if (iov[0].iov_len != NBT_HDR_SIZE) {
1362 return NT_STATUS_INVALID_PARAMETER_MIX;
1364 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1365 return NT_STATUS_INVALID_PARAMETER_MIX;
1367 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1368 return NT_STATUS_INVALID_PARAMETER_MIX;
1370 if (iov[3].iov_len != sizeof(uint16_t)) {
1371 return NT_STATUS_INVALID_PARAMETER_MIX;
1374 frame = talloc_stackframe();
1376 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1378 return NT_STATUS_NO_MEMORY;
1381 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1383 smb_signing_sign_pdu(conn->smb1.signing,
1384 buf, talloc_get_size(buf),
1386 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1389 return NT_STATUS_OK;
1392 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1393 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1394 TALLOC_CTX *tmp_mem,
1397 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1398 struct smbXcli_req_state *state,
1399 struct iovec *iov, int iov_count)
1401 struct tevent_req *subreq;
1406 if (!smbXcli_conn_is_connected(state->conn)) {
1407 return NT_STATUS_CONNECTION_DISCONNECTED;
1410 if (state->conn->protocol > PROTOCOL_NT1) {
1411 return NT_STATUS_REVISION_MISMATCH;
1414 if (iov_count < 4) {
1415 return NT_STATUS_INVALID_PARAMETER_MIX;
1417 if (iov[0].iov_len != NBT_HDR_SIZE) {
1418 return NT_STATUS_INVALID_PARAMETER_MIX;
1420 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1421 return NT_STATUS_INVALID_PARAMETER_MIX;
1423 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1424 return NT_STATUS_INVALID_PARAMETER_MIX;
1426 if (iov[3].iov_len != sizeof(uint16_t)) {
1427 return NT_STATUS_INVALID_PARAMETER_MIX;
1430 cmd = CVAL(iov[1].iov_base, HDR_COM);
1431 if (cmd == SMBreadBraw) {
1432 if (smbXcli_conn_has_async_calls(state->conn)) {
1433 return NT_STATUS_INVALID_PARAMETER_MIX;
1435 state->conn->smb1.read_braw_req = req;
1438 if (state->smb1.mid != 0) {
1439 mid = state->smb1.mid;
1441 mid = smb1cli_alloc_mid(state->conn);
1443 SSVAL(iov[1].iov_base, HDR_MID, mid);
1445 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1447 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1448 &state->smb1.seqnum,
1449 state->smb1.one_way_seqnum);
1451 if (!NT_STATUS_IS_OK(status)) {
1456 * If we supported multiple encrytion contexts
1457 * here we'd look up based on tid.
1459 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1460 char *buf, *enc_buf;
1462 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1464 return NT_STATUS_NO_MEMORY;
1466 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1467 (char *)buf, &enc_buf);
1469 if (!NT_STATUS_IS_OK(status)) {
1470 DEBUG(0, ("Error in encrypting client message: %s\n",
1471 nt_errstr(status)));
1474 buf = (char *)talloc_memdup(state, enc_buf,
1475 smb_len_nbt(enc_buf)+4);
1478 return NT_STATUS_NO_MEMORY;
1480 iov[0].iov_base = (void *)buf;
1481 iov[0].iov_len = talloc_get_size(buf);
1485 if (state->conn->dispatch_incoming == NULL) {
1486 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1489 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1491 subreq = writev_send(state, state->ev, state->conn->outgoing,
1492 state->conn->write_fd, false, iov, iov_count);
1493 if (subreq == NULL) {
1494 return NT_STATUS_NO_MEMORY;
1496 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1497 return NT_STATUS_OK;
1500 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1501 struct tevent_context *ev,
1502 struct smbXcli_conn *conn,
1503 uint8_t smb_command,
1504 uint8_t additional_flags,
1505 uint8_t clear_flags,
1506 uint16_t additional_flags2,
1507 uint16_t clear_flags2,
1508 uint32_t timeout_msec,
1510 struct smbXcli_tcon *tcon,
1511 struct smbXcli_session *session,
1512 uint8_t wct, uint16_t *vwv,
1514 const uint8_t *bytes)
1516 struct tevent_req *req;
1520 iov.iov_base = discard_const_p(void, bytes);
1521 iov.iov_len = num_bytes;
1523 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1524 additional_flags, clear_flags,
1525 additional_flags2, clear_flags2,
1532 if (!tevent_req_is_in_progress(req)) {
1533 return tevent_req_post(req, ev);
1535 status = smb1cli_req_chain_submit(&req, 1);
1536 if (tevent_req_nterror(req, status)) {
1537 return tevent_req_post(req, ev);
1542 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1544 struct tevent_req *req =
1545 tevent_req_callback_data(subreq,
1547 struct smbXcli_req_state *state =
1548 tevent_req_data(req,
1549 struct smbXcli_req_state);
1553 nwritten = writev_recv(subreq, &err);
1554 TALLOC_FREE(subreq);
1555 if (nwritten == -1) {
1556 NTSTATUS status = map_nt_error_from_unix_common(err);
1557 smbXcli_conn_disconnect(state->conn, status);
1561 if (state->one_way) {
1562 state->inbuf = NULL;
1563 tevent_req_done(req);
1567 if (!smbXcli_req_set_pending(req)) {
1568 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1573 static void smbXcli_conn_received(struct tevent_req *subreq)
1575 struct smbXcli_conn *conn =
1576 tevent_req_callback_data(subreq,
1577 struct smbXcli_conn);
1578 TALLOC_CTX *frame = talloc_stackframe();
1584 if (subreq != conn->read_smb_req) {
1585 DEBUG(1, ("Internal error: cli_smb_received called with "
1586 "unexpected subreq\n"));
1587 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1591 conn->read_smb_req = NULL;
1593 received = read_smb_recv(subreq, frame, &inbuf, &err);
1594 TALLOC_FREE(subreq);
1595 if (received == -1) {
1596 status = map_nt_error_from_unix_common(err);
1597 smbXcli_conn_disconnect(conn, status);
1602 status = conn->dispatch_incoming(conn, frame, inbuf);
1604 if (NT_STATUS_IS_OK(status)) {
1606 * We should not do any more processing
1607 * as the dispatch function called
1608 * tevent_req_done().
1613 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1615 * We got an error, so notify all pending requests
1617 smbXcli_conn_disconnect(conn, status);
1622 * We got NT_STATUS_RETRY, so we may ask for a
1623 * next incoming pdu.
1625 if (!smbXcli_conn_receive_next(conn)) {
1626 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1630 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1631 struct iovec **piov, int *pnum_iov)
1642 size_t min_size = MIN_SMB_SIZE;
1644 buflen = smb_len_tcp(buf);
1647 hdr = buf + NBT_HDR_SIZE;
1649 status = smb1cli_pull_raw_error(hdr);
1650 if (NT_STATUS_IS_ERR(status)) {
1652 * This is an ugly hack to support OS/2
1653 * which skips the byte_count in the DATA block
1654 * on some error responses.
1658 min_size -= sizeof(uint16_t);
1661 if (buflen < min_size) {
1662 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1666 * This returns iovec elements in the following order:
1681 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1683 return NT_STATUS_NO_MEMORY;
1685 iov[0].iov_base = hdr;
1686 iov[0].iov_len = HDR_WCT;
1689 cmd = CVAL(hdr, HDR_COM);
1693 size_t len = buflen - taken;
1695 struct iovec *iov_tmp;
1702 * we need at least WCT
1704 needed = sizeof(uint8_t);
1706 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1707 __location__, (int)len, (int)needed));
1712 * Now we check if the specified words are there
1714 wct = CVAL(hdr, wct_ofs);
1715 needed += wct * sizeof(uint16_t);
1717 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1718 __location__, (int)len, (int)needed));
1722 if ((num_iov == 1) &&
1724 NT_STATUS_IS_ERR(status))
1727 * This is an ugly hack to support OS/2
1728 * which skips the byte_count in the DATA block
1729 * on some error responses.
1733 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1735 if (iov_tmp == NULL) {
1737 return NT_STATUS_NO_MEMORY;
1740 cur = &iov[num_iov];
1744 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1746 cur[1].iov_base = cur[0].iov_base;
1753 * we need at least BCC
1755 needed += sizeof(uint16_t);
1757 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1758 __location__, (int)len, (int)needed));
1763 * Now we check if the specified bytes are there
1765 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1766 bcc = SVAL(hdr, bcc_ofs);
1767 needed += bcc * sizeof(uint8_t);
1769 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1770 __location__, (int)len, (int)needed));
1775 * we allocate 2 iovec structures for words and bytes
1777 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1779 if (iov_tmp == NULL) {
1781 return NT_STATUS_NO_MEMORY;
1784 cur = &iov[num_iov];
1787 cur[0].iov_len = wct * sizeof(uint16_t);
1788 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1789 cur[1].iov_len = bcc * sizeof(uint8_t);
1790 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1794 if (!smb1cli_is_andx_req(cmd)) {
1796 * If the current command does not have AndX chanining
1802 if (wct == 0 && bcc == 0) {
1804 * An empty response also ends the chain,
1805 * most likely with an error.
1811 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1812 __location__, (int)wct, (int)cmd));
1815 cmd = CVAL(cur[0].iov_base, 0);
1818 * If it is the end of the chain we are also done.
1822 wct_ofs = SVAL(cur[0].iov_base, 2);
1824 if (wct_ofs < taken) {
1825 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1827 if (wct_ofs > buflen) {
1828 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1832 * we consumed everything up to the start of the next
1838 remaining = buflen - taken;
1840 if (remaining > 0 && num_iov >= 3) {
1842 * The last DATA block gets the remaining
1843 * bytes, this is needed to support
1844 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1846 iov[num_iov-1].iov_len += remaining;
1850 *pnum_iov = num_iov;
1851 return NT_STATUS_OK;
1855 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1858 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1859 TALLOC_CTX *tmp_mem,
1862 struct tevent_req *req;
1863 struct smbXcli_req_state *state;
1870 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1871 size_t len = smb_len_tcp(inbuf);
1872 struct iovec *iov = NULL;
1874 struct tevent_req **chain = NULL;
1875 size_t num_chained = 0;
1876 size_t num_responses = 0;
1878 if (conn->smb1.read_braw_req != NULL) {
1879 req = conn->smb1.read_braw_req;
1880 conn->smb1.read_braw_req = NULL;
1881 state = tevent_req_data(req, struct smbXcli_req_state);
1883 smbXcli_req_unset_pending(req);
1885 if (state->smb1.recv_iov == NULL) {
1887 * For requests with more than
1888 * one response, we have to readd the
1891 state->smb1.recv_iov = talloc_zero_array(state,
1894 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1895 return NT_STATUS_OK;
1899 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1900 state->smb1.recv_iov[0].iov_len = len;
1901 ZERO_STRUCT(state->smb1.recv_iov[1]);
1902 ZERO_STRUCT(state->smb1.recv_iov[2]);
1904 state->smb1.recv_cmd = SMBreadBraw;
1905 state->smb1.recv_status = NT_STATUS_OK;
1906 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1908 tevent_req_done(req);
1909 return NT_STATUS_OK;
1912 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1913 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1914 DEBUG(10, ("Got non-SMB PDU\n"));
1915 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1919 * If we supported multiple encrytion contexts
1920 * here we'd look up based on tid.
1922 if (common_encryption_on(conn->smb1.trans_enc)
1923 && (CVAL(inbuf, 0) == 0)) {
1924 uint16_t enc_ctx_num;
1926 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1927 if (!NT_STATUS_IS_OK(status)) {
1928 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1929 nt_errstr(status)));
1933 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1934 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1936 conn->smb1.trans_enc->enc_ctx_num));
1937 return NT_STATUS_INVALID_HANDLE;
1940 status = common_decrypt_buffer(conn->smb1.trans_enc,
1942 if (!NT_STATUS_IS_OK(status)) {
1943 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1944 nt_errstr(status)));
1947 inhdr = inbuf + NBT_HDR_SIZE;
1948 len = smb_len_nbt(inbuf);
1951 mid = SVAL(inhdr, HDR_MID);
1952 num_pending = talloc_array_length(conn->pending);
1954 for (i=0; i<num_pending; i++) {
1955 if (mid == smb1cli_req_mid(conn->pending[i])) {
1959 if (i == num_pending) {
1960 /* Dump unexpected reply */
1961 return NT_STATUS_RETRY;
1964 oplock_break = false;
1966 if (mid == 0xffff) {
1968 * Paranoia checks that this is really an oplock break request.
1970 oplock_break = (len == 51); /* hdr + 8 words */
1971 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1972 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1973 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1974 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1976 if (!oplock_break) {
1977 /* Dump unexpected reply */
1978 return NT_STATUS_RETRY;
1982 req = conn->pending[i];
1983 state = tevent_req_data(req, struct smbXcli_req_state);
1985 if (!oplock_break /* oplock breaks are not signed */
1986 && !smb_signing_check_pdu(conn->smb1.signing,
1987 inhdr, len, state->smb1.seqnum+1)) {
1988 DEBUG(10, ("cli_check_sign_mac failed\n"));
1989 return NT_STATUS_ACCESS_DENIED;
1992 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1994 if (!NT_STATUS_IS_OK(status)) {
1995 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1996 nt_errstr(status)));
2000 cmd = CVAL(inhdr, HDR_COM);
2001 status = smb1cli_pull_raw_error(inhdr);
2003 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2004 (state->session != NULL) && state->session->disconnect_expired)
2007 * this should be a short term hack
2008 * until the upper layers have implemented
2009 * re-authentication.
2014 if (state->smb1.chained_requests == NULL) {
2016 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2019 smbXcli_req_unset_pending(req);
2021 if (state->smb1.recv_iov == NULL) {
2023 * For requests with more than
2024 * one response, we have to readd the
2027 state->smb1.recv_iov = talloc_zero_array(state,
2030 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2031 return NT_STATUS_OK;
2035 state->smb1.recv_cmd = cmd;
2036 state->smb1.recv_status = status;
2037 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2039 state->smb1.recv_iov[0] = iov[0];
2040 state->smb1.recv_iov[1] = iov[1];
2041 state->smb1.recv_iov[2] = iov[2];
2043 if (talloc_array_length(conn->pending) == 0) {
2044 tevent_req_done(req);
2045 return NT_STATUS_OK;
2048 tevent_req_defer_callback(req, state->ev);
2049 tevent_req_done(req);
2050 return NT_STATUS_RETRY;
2053 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2054 num_chained = talloc_array_length(chain);
2055 num_responses = (num_iov - 1)/2;
2057 if (num_responses > num_chained) {
2058 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2061 for (i=0; i<num_chained; i++) {
2062 size_t iov_idx = 1 + (i*2);
2063 struct iovec *cur = &iov[iov_idx];
2067 state = tevent_req_data(req, struct smbXcli_req_state);
2069 smbXcli_req_unset_pending(req);
2072 * as we finish multiple requests here
2073 * we need to defer the callbacks as
2074 * they could destroy our current stack state.
2076 tevent_req_defer_callback(req, state->ev);
2078 if (i >= num_responses) {
2079 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2083 if (state->smb1.recv_iov == NULL) {
2085 * For requests with more than
2086 * one response, we have to readd the
2089 state->smb1.recv_iov = talloc_zero_array(state,
2092 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2097 state->smb1.recv_cmd = cmd;
2099 if (i == (num_responses - 1)) {
2101 * The last request in the chain gets the status
2103 state->smb1.recv_status = status;
2105 cmd = CVAL(cur[0].iov_base, 0);
2106 state->smb1.recv_status = NT_STATUS_OK;
2109 state->inbuf = inbuf;
2112 * Note: here we use talloc_reference() in a way
2113 * that does not expose it to the caller.
2115 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2116 if (tevent_req_nomem(inbuf_ref, req)) {
2120 /* copy the related buffers */
2121 state->smb1.recv_iov[0] = iov[0];
2122 state->smb1.recv_iov[1] = cur[0];
2123 state->smb1.recv_iov[2] = cur[1];
2125 tevent_req_done(req);
2128 return NT_STATUS_RETRY;
2131 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2132 TALLOC_CTX *mem_ctx,
2133 struct iovec **piov,
2137 uint32_t *pvwv_offset,
2138 uint32_t *pnum_bytes,
2140 uint32_t *pbytes_offset,
2142 const struct smb1cli_req_expected_response *expected,
2143 size_t num_expected)
2145 struct smbXcli_req_state *state =
2146 tevent_req_data(req,
2147 struct smbXcli_req_state);
2148 NTSTATUS status = NT_STATUS_OK;
2149 struct iovec *recv_iov = NULL;
2150 uint8_t *hdr = NULL;
2152 uint32_t vwv_offset = 0;
2153 uint16_t *vwv = NULL;
2154 uint32_t num_bytes = 0;
2155 uint32_t bytes_offset = 0;
2156 uint8_t *bytes = NULL;
2158 bool found_status = false;
2159 bool found_size = false;
2173 if (pvwv_offset != NULL) {
2176 if (pnum_bytes != NULL) {
2179 if (pbytes != NULL) {
2182 if (pbytes_offset != NULL) {
2185 if (pinbuf != NULL) {
2189 if (state->inbuf != NULL) {
2190 recv_iov = state->smb1.recv_iov;
2191 state->smb1.recv_iov = NULL;
2192 if (state->smb1.recv_cmd != SMBreadBraw) {
2193 hdr = (uint8_t *)recv_iov[0].iov_base;
2194 wct = recv_iov[1].iov_len/2;
2195 vwv = (uint16_t *)recv_iov[1].iov_base;
2196 vwv_offset = PTR_DIFF(vwv, hdr);
2197 num_bytes = recv_iov[2].iov_len;
2198 bytes = (uint8_t *)recv_iov[2].iov_base;
2199 bytes_offset = PTR_DIFF(bytes, hdr);
2203 if (tevent_req_is_nterror(req, &status)) {
2204 for (i=0; i < num_expected; i++) {
2205 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2206 found_status = true;
2212 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2218 if (num_expected == 0) {
2219 found_status = true;
2223 status = state->smb1.recv_status;
2225 for (i=0; i < num_expected; i++) {
2226 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2230 found_status = true;
2231 if (expected[i].wct == 0) {
2236 if (expected[i].wct == wct) {
2242 if (!found_status) {
2247 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2251 *piov = talloc_move(mem_ctx, &recv_iov);
2263 if (pvwv_offset != NULL) {
2264 *pvwv_offset = vwv_offset;
2266 if (pnum_bytes != NULL) {
2267 *pnum_bytes = num_bytes;
2269 if (pbytes != NULL) {
2272 if (pbytes_offset != NULL) {
2273 *pbytes_offset = bytes_offset;
2275 if (pinbuf != NULL) {
2276 *pinbuf = state->inbuf;
2282 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2289 for (i=0; i<num_reqs; i++) {
2290 struct smbXcli_req_state *state;
2291 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2292 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2293 state->smb1.iov_count-2);
2294 wct_ofs = (wct_ofs + 3) & ~3;
2299 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2301 struct smbXcli_req_state *first_state =
2302 tevent_req_data(reqs[0],
2303 struct smbXcli_req_state);
2304 struct smbXcli_req_state *state;
2306 size_t chain_padding = 0;
2308 struct iovec *iov = NULL;
2309 struct iovec *this_iov;
2313 if (num_reqs == 1) {
2314 return smb1cli_req_writev_submit(reqs[0], first_state,
2315 first_state->smb1.iov,
2316 first_state->smb1.iov_count);
2320 for (i=0; i<num_reqs; i++) {
2321 if (!tevent_req_is_in_progress(reqs[i])) {
2322 return NT_STATUS_INTERNAL_ERROR;
2325 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2327 if (state->smb1.iov_count < 4) {
2328 return NT_STATUS_INVALID_PARAMETER_MIX;
2333 * The NBT and SMB header
2346 iovlen += state->smb1.iov_count - 2;
2349 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2351 return NT_STATUS_NO_MEMORY;
2354 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2355 first_state, reqs, sizeof(*reqs) * num_reqs);
2356 if (first_state->smb1.chained_requests == NULL) {
2358 return NT_STATUS_NO_MEMORY;
2361 wct_offset = HDR_WCT;
2364 for (i=0; i<num_reqs; i++) {
2365 size_t next_padding = 0;
2368 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2370 if (i < num_reqs-1) {
2371 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2372 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2374 TALLOC_FREE(first_state->smb1.chained_requests);
2375 return NT_STATUS_INVALID_PARAMETER_MIX;
2379 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2380 state->smb1.iov_count-2) + 1;
2381 if ((wct_offset % 4) != 0) {
2382 next_padding = 4 - (wct_offset % 4);
2384 wct_offset += next_padding;
2385 vwv = state->smb1.vwv;
2387 if (i < num_reqs-1) {
2388 struct smbXcli_req_state *next_state =
2389 tevent_req_data(reqs[i+1],
2390 struct smbXcli_req_state);
2391 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2393 SSVAL(vwv+1, 0, wct_offset);
2394 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2395 /* properly end the chain */
2396 SCVAL(vwv+0, 0, 0xff);
2397 SCVAL(vwv+0, 1, 0xff);
2403 * The NBT and SMB header
2405 this_iov[0] = state->smb1.iov[0];
2406 this_iov[1] = state->smb1.iov[1];
2410 * This one is a bit subtle. We have to add
2411 * chain_padding bytes between the requests, and we
2412 * have to also include the wct field of the
2413 * subsequent requests. We use the subsequent header
2414 * for the padding, it contains the wct field in its
2417 this_iov[0].iov_len = chain_padding+1;
2418 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2419 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2420 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2425 * copy the words and bytes
2427 memcpy(this_iov, state->smb1.iov+2,
2428 sizeof(struct iovec) * (state->smb1.iov_count-2));
2429 this_iov += state->smb1.iov_count - 2;
2430 chain_padding = next_padding;
2433 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2434 if (nbt_len > first_state->conn->smb1.max_xmit) {
2436 TALLOC_FREE(first_state->smb1.chained_requests);
2437 return NT_STATUS_INVALID_PARAMETER_MIX;
2440 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2441 if (!NT_STATUS_IS_OK(status)) {
2443 TALLOC_FREE(first_state->smb1.chained_requests);
2447 return NT_STATUS_OK;
2450 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2452 return ((tevent_queue_length(conn->outgoing) != 0)
2453 || (talloc_array_length(conn->pending) != 0));
2456 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2458 uint16_t credits = 1;
2460 if (conn->smb2.cur_credits == 0) {
2461 if (max_dyn_len != NULL) {
2467 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2468 credits = conn->smb2.cur_credits;
2471 if (max_dyn_len != NULL) {
2472 *max_dyn_len = credits * 65536;
2478 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2480 return conn->smb2.server.capabilities;
2483 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2485 return conn->smb2.server.security_mode;
2488 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2490 return conn->smb2.server.max_trans_size;
2493 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2495 return conn->smb2.server.max_read_size;
2498 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2500 return conn->smb2.server.max_write_size;
2503 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2504 uint16_t max_credits)
2506 conn->smb2.max_credits = max_credits;
2509 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2511 static bool smb2cli_req_cancel(struct tevent_req *req)
2513 struct smbXcli_req_state *state =
2514 tevent_req_data(req,
2515 struct smbXcli_req_state);
2516 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2517 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2518 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2519 struct smbXcli_tcon *tcon = state->tcon;
2520 struct smbXcli_session *session = state->session;
2521 uint8_t *fixed = state->smb2.pad;
2522 uint16_t fixed_len = 4;
2523 struct tevent_req *subreq;
2524 struct smbXcli_req_state *substate;
2527 SSVAL(fixed, 0, 0x04);
2530 subreq = smb2cli_req_create(state, state->ev,
2538 if (subreq == NULL) {
2541 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2544 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2545 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2547 flags &= SMB2_HDR_FLAG_ASYNC;
2549 if (flags & SMB2_HDR_FLAG_ASYNC) {
2553 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2554 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2555 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2557 status = smb2cli_req_compound_submit(&subreq, 1);
2558 if (!NT_STATUS_IS_OK(status)) {
2559 TALLOC_FREE(subreq);
2563 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2568 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2570 /* we do not care about the result */
2571 TALLOC_FREE(subreq);
2574 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2575 struct tevent_context *ev,
2576 struct smbXcli_conn *conn,
2578 uint32_t additional_flags,
2579 uint32_t clear_flags,
2580 uint32_t timeout_msec,
2581 struct smbXcli_tcon *tcon,
2582 struct smbXcli_session *session,
2583 const uint8_t *fixed,
2587 uint32_t max_dyn_len)
2589 struct tevent_req *req;
2590 struct smbXcli_req_state *state;
2594 bool use_channel_sequence = false;
2595 uint16_t channel_sequence = 0;
2597 req = tevent_req_create(mem_ctx, &state,
2598 struct smbXcli_req_state);
2605 state->session = session;
2608 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2609 use_channel_sequence = true;
2610 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2611 use_channel_sequence = true;
2615 uid = session->smb2->session_id;
2617 if (use_channel_sequence) {
2618 channel_sequence = session->smb2->channel_sequence;
2621 state->smb2.should_sign = session->smb2->should_sign;
2622 state->smb2.should_encrypt = session->smb2->should_encrypt;
2624 if (cmd == SMB2_OP_SESSSETUP &&
2625 session->smb2->signing_key.length != 0) {
2626 state->smb2.should_sign = true;
2629 if (cmd == SMB2_OP_SESSSETUP &&
2630 session->smb2_channel.signing_key.length == 0) {
2631 state->smb2.should_encrypt = false;
2636 tid = tcon->smb2.tcon_id;
2638 if (tcon->smb2.should_encrypt) {
2639 state->smb2.should_encrypt = true;
2643 if (state->smb2.should_encrypt) {
2644 state->smb2.should_sign = false;
2647 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2648 if (state->smb2.recv_iov == NULL) {
2653 flags |= additional_flags;
2654 flags &= ~clear_flags;
2656 state->smb2.fixed = fixed;
2657 state->smb2.fixed_len = fixed_len;
2658 state->smb2.dyn = dyn;
2659 state->smb2.dyn_len = dyn_len;
2660 state->smb2.max_dyn_len = max_dyn_len;
2662 if (state->smb2.should_encrypt) {
2663 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2664 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2667 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2668 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2669 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2670 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2671 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2672 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2673 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2674 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2677 case SMB2_OP_CANCEL:
2678 state->one_way = true;
2682 * If this is a dummy request, it will have
2683 * UINT64_MAX as message id.
2684 * If we send on break acknowledgement,
2685 * this gets overwritten later.
2687 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2691 if (timeout_msec > 0) {
2692 struct timeval endtime;
2694 endtime = timeval_current_ofs_msec(timeout_msec);
2695 if (!tevent_req_set_endtime(req, ev, endtime)) {
2703 void smb2cli_req_set_notify_async(struct tevent_req *req)
2705 struct smbXcli_req_state *state =
2706 tevent_req_data(req,
2707 struct smbXcli_req_state);
2709 state->smb2.notify_async = true;
2712 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2713 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2714 TALLOC_CTX *tmp_mem,
2717 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2720 struct smbXcli_req_state *state;
2721 struct tevent_req *subreq;
2723 int i, num_iov, nbt_len;
2725 const DATA_BLOB *encryption_key = NULL;
2726 uint64_t encryption_session_id = 0;
2729 * 1 for the nbt length, optional TRANSFORM
2730 * per request: HDR, fixed, dyn, padding
2731 * -1 because the last one does not need padding
2734 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2736 return NT_STATUS_NO_MEMORY;
2743 * the session of the first request that requires encryption
2744 * specifies the encryption key.
2746 for (i=0; i<num_reqs; i++) {
2747 if (!tevent_req_is_in_progress(reqs[i])) {
2748 return NT_STATUS_INTERNAL_ERROR;
2751 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2753 if (!smbXcli_conn_is_connected(state->conn)) {
2754 return NT_STATUS_CONNECTION_DISCONNECTED;
2757 if ((state->conn->protocol != PROTOCOL_NONE) &&
2758 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2759 return NT_STATUS_REVISION_MISMATCH;
2762 if (state->session == NULL) {
2766 if (!state->smb2.should_encrypt) {
2770 encryption_key = &state->session->smb2->encryption_key;
2771 if (encryption_key->length == 0) {
2772 return NT_STATUS_INVALID_PARAMETER_MIX;
2775 encryption_session_id = state->session->smb2->session_id;
2778 iov[num_iov].iov_base = state->smb2.transform;
2779 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2782 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2783 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2784 state->session->smb2->nonce_low);
2785 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2786 state->session->smb2->nonce_high);
2787 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2788 encryption_session_id);
2790 state->session->smb2->nonce_low += 1;
2791 if (state->session->smb2->nonce_low == 0) {
2792 state->session->smb2->nonce_high += 1;
2793 state->session->smb2->nonce_low += 1;
2796 nbt_len += SMB2_TF_HDR_SIZE;
2800 for (i=0; i<num_reqs; i++) {
2809 const DATA_BLOB *signing_key = NULL;
2811 if (!tevent_req_is_in_progress(reqs[i])) {
2812 return NT_STATUS_INTERNAL_ERROR;
2815 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2817 if (!smbXcli_conn_is_connected(state->conn)) {
2818 return NT_STATUS_CONNECTION_DISCONNECTED;
2821 if ((state->conn->protocol != PROTOCOL_NONE) &&
2822 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2823 return NT_STATUS_REVISION_MISMATCH;
2826 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2827 if (opcode == SMB2_OP_CANCEL) {
2831 avail = UINT64_MAX - state->conn->smb2.mid;
2833 return NT_STATUS_CONNECTION_ABORTED;
2836 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2837 uint32_t max_dyn_len = 1;
2839 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2840 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2842 charge = (max_dyn_len - 1)/ 65536 + 1;
2847 charge = MAX(state->smb2.credit_charge, charge);
2849 avail = MIN(avail, state->conn->smb2.cur_credits);
2850 if (avail < charge) {
2851 return NT_STATUS_INTERNAL_ERROR;
2855 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2856 credits = state->conn->smb2.max_credits -
2857 state->conn->smb2.cur_credits;
2859 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2863 mid = state->conn->smb2.mid;
2864 state->conn->smb2.mid += charge;
2865 state->conn->smb2.cur_credits -= charge;
2867 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2868 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2870 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2871 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2874 if (state->session && encryption_key == NULL) {
2876 * We prefer the channel signing key if it is
2879 if (state->smb2.should_sign) {
2880 signing_key = &state->session->smb2_channel.signing_key;
2884 * If it is a channel binding, we already have the main
2885 * signing key and try that one.
2887 if (signing_key && signing_key->length == 0) {
2888 signing_key = &state->session->smb2->signing_key;
2892 * If we do not have any session key yet, we skip the
2893 * signing of SMB2_OP_SESSSETUP requests.
2895 if (signing_key && signing_key->length == 0) {
2901 iov[num_iov].iov_base = state->smb2.hdr;
2902 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2905 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2906 iov[num_iov].iov_len = state->smb2.fixed_len;
2909 if (state->smb2.dyn != NULL) {
2910 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2911 iov[num_iov].iov_len = state->smb2.dyn_len;
2915 reqlen = sizeof(state->smb2.hdr);
2916 reqlen += state->smb2.fixed_len;
2917 reqlen += state->smb2.dyn_len;
2919 if (i < num_reqs-1) {
2920 if ((reqlen % 8) > 0) {
2921 uint8_t pad = 8 - (reqlen % 8);
2922 iov[num_iov].iov_base = state->smb2.pad;
2923 iov[num_iov].iov_len = pad;
2927 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2930 state->smb2.encryption_session_id = encryption_session_id;
2932 if (signing_key != NULL) {
2935 status = smb2_signing_sign_pdu(*signing_key,
2936 state->session->conn->protocol,
2937 &iov[hdr_iov], num_iov - hdr_iov);
2938 if (!NT_STATUS_IS_OK(status)) {
2945 ret = smbXcli_req_set_pending(reqs[i]);
2947 return NT_STATUS_NO_MEMORY;
2951 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2952 _smb_setlen_tcp(state->length_hdr, nbt_len);
2953 iov[0].iov_base = state->length_hdr;
2954 iov[0].iov_len = sizeof(state->length_hdr);
2956 if (encryption_key != NULL) {
2958 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2962 buf = talloc_array(iov, uint8_t, buflen);
2964 return NT_STATUS_NO_MEMORY;
2968 * We copy the buffers before encrypting them,
2969 * this is at least currently needed for the
2970 * to keep state->smb2.hdr.
2972 * Also the callers may expect there buffers
2975 for (vi = tf_iov + 1; vi < num_iov; vi++) {
2976 struct iovec *v = &iov[vi];
2977 const uint8_t *o = (const uint8_t *)v->iov_base;
2979 memcpy(buf, o, v->iov_len);
2980 v->iov_base = (void *)buf;
2984 status = smb2_signing_encrypt_pdu(*encryption_key,
2985 state->conn->protocol,
2986 &iov[tf_iov], num_iov - tf_iov);
2987 if (!NT_STATUS_IS_OK(status)) {
2992 if (state->conn->dispatch_incoming == NULL) {
2993 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2996 subreq = writev_send(state, state->ev, state->conn->outgoing,
2997 state->conn->write_fd, false, iov, num_iov);
2998 if (subreq == NULL) {
2999 return NT_STATUS_NO_MEMORY;
3001 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3002 return NT_STATUS_OK;
3005 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3007 struct smbXcli_req_state *state =
3008 tevent_req_data(req,
3009 struct smbXcli_req_state);
3011 state->smb2.credit_charge = charge;
3014 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3015 struct tevent_context *ev,
3016 struct smbXcli_conn *conn,
3018 uint32_t additional_flags,
3019 uint32_t clear_flags,
3020 uint32_t timeout_msec,
3021 struct smbXcli_tcon *tcon,
3022 struct smbXcli_session *session,
3023 const uint8_t *fixed,
3027 uint32_t max_dyn_len)
3029 struct tevent_req *req;
3032 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3033 additional_flags, clear_flags,
3042 if (!tevent_req_is_in_progress(req)) {
3043 return tevent_req_post(req, ev);
3045 status = smb2cli_req_compound_submit(&req, 1);
3046 if (tevent_req_nterror(req, status)) {
3047 return tevent_req_post(req, ev);
3052 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3054 struct tevent_req *req =
3055 tevent_req_callback_data(subreq,
3057 struct smbXcli_req_state *state =
3058 tevent_req_data(req,
3059 struct smbXcli_req_state);
3063 nwritten = writev_recv(subreq, &err);
3064 TALLOC_FREE(subreq);
3065 if (nwritten == -1) {
3066 /* here, we need to notify all pending requests */
3067 NTSTATUS status = map_nt_error_from_unix_common(err);
3068 smbXcli_conn_disconnect(state->conn, status);
3073 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3076 TALLOC_CTX *mem_ctx,
3077 struct iovec **piov, int *pnum_iov)
3082 uint8_t *first_hdr = buf;
3083 size_t verified_buflen = 0;
3087 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3089 return NT_STATUS_NO_MEMORY;
3092 while (taken < buflen) {
3093 size_t len = buflen - taken;
3094 uint8_t *hdr = first_hdr + taken;
3097 size_t next_command_ofs;
3099 struct iovec *iov_tmp;
3101 if (verified_buflen > taken) {
3102 len = verified_buflen - taken;
3109 DEBUG(10, ("%d bytes left, expected at least %d\n",
3113 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3114 struct smbXcli_session *s;
3116 struct iovec tf_iov[2];
3120 if (len < SMB2_TF_HDR_SIZE) {
3121 DEBUG(10, ("%d bytes left, expected at least %d\n",
3122 (int)len, SMB2_TF_HDR_SIZE));
3126 tf_len = SMB2_TF_HDR_SIZE;
3129 hdr = first_hdr + taken;
3130 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3131 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3133 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3134 DEBUG(10, ("%d bytes left, expected at least %d\n",
3136 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3141 for (; s; s = s->next) {
3142 if (s->smb2->session_id != uid) {
3149 DEBUG(10, ("unknown session_id %llu\n",
3150 (unsigned long long)uid));
3154 tf_iov[0].iov_base = (void *)tf;
3155 tf_iov[0].iov_len = tf_len;
3156 tf_iov[1].iov_base = (void *)hdr;
3157 tf_iov[1].iov_len = enc_len;
3159 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3162 if (!NT_STATUS_IS_OK(status)) {
3167 verified_buflen = taken + enc_len;
3172 * We need the header plus the body length field
3175 if (len < SMB2_HDR_BODY + 2) {
3176 DEBUG(10, ("%d bytes left, expected at least %d\n",
3177 (int)len, SMB2_HDR_BODY));
3180 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3181 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3185 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3186 DEBUG(10, ("Got HDR len %d, expected %d\n",
3187 SVAL(hdr, 4), SMB2_HDR_BODY));
3192 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3193 body_size = SVAL(hdr, SMB2_HDR_BODY);
3195 if (next_command_ofs != 0) {
3196 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3199 if (next_command_ofs > full_size) {
3202 full_size = next_command_ofs;
3204 if (body_size < 2) {
3207 body_size &= 0xfffe;
3209 if (body_size > (full_size - SMB2_HDR_BODY)) {
3213 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3215 if (iov_tmp == NULL) {
3217 return NT_STATUS_NO_MEMORY;
3220 cur = &iov[num_iov];
3223 cur[0].iov_base = tf;
3224 cur[0].iov_len = tf_len;
3225 cur[1].iov_base = hdr;
3226 cur[1].iov_len = SMB2_HDR_BODY;
3227 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3228 cur[2].iov_len = body_size;
3229 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3230 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3236 *pnum_iov = num_iov;
3237 return NT_STATUS_OK;
3241 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3244 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3247 size_t num_pending = talloc_array_length(conn->pending);
3250 for (i=0; i<num_pending; i++) {
3251 struct tevent_req *req = conn->pending[i];
3252 struct smbXcli_req_state *state =
3253 tevent_req_data(req,
3254 struct smbXcli_req_state);
3256 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3263 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3264 TALLOC_CTX *tmp_mem,
3267 struct tevent_req *req;
3268 struct smbXcli_req_state *state = NULL;
3273 struct smbXcli_session *last_session = NULL;
3274 size_t inbuf_len = smb_len_tcp(inbuf);
3276 status = smb2cli_inbuf_parse_compound(conn,
3277 inbuf + NBT_HDR_SIZE,
3281 if (!NT_STATUS_IS_OK(status)) {
3285 for (i=0; i<num_iov; i+=4) {
3286 uint8_t *inbuf_ref = NULL;
3287 struct iovec *cur = &iov[i];
3288 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3289 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3290 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3291 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3292 uint16_t req_opcode;
3294 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3295 uint32_t new_credits;
3296 struct smbXcli_session *session = NULL;
3297 const DATA_BLOB *signing_key = NULL;
3298 bool was_encrypted = false;
3300 new_credits = conn->smb2.cur_credits;
3301 new_credits += credits;
3302 if (new_credits > UINT16_MAX) {
3303 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3305 conn->smb2.cur_credits += credits;
3307 req = smb2cli_conn_find_pending(conn, mid);
3309 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3311 state = tevent_req_data(req, struct smbXcli_req_state);
3313 state->smb2.got_async = false;
3315 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3316 if (opcode != req_opcode) {
3317 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3319 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3321 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3322 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3325 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3326 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3327 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3328 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3331 * async interim responses are not signed,
3332 * even if the SMB2_HDR_FLAG_SIGNED flag
3335 req_flags |= SMB2_HDR_FLAG_ASYNC;
3336 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3337 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3339 if (state->smb2.notify_async) {
3340 state->smb2.got_async = true;
3341 tevent_req_defer_callback(req, state->ev);
3342 tevent_req_notify_callback(req);
3347 session = state->session;
3348 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3349 session = last_session;
3351 last_session = session;
3353 if (state->smb2.should_sign) {
3354 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3355 return NT_STATUS_ACCESS_DENIED;
3359 if (flags & SMB2_HDR_FLAG_SIGNED) {
3360 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3362 if (session == NULL) {
3363 struct smbXcli_session *s;
3365 s = state->conn->sessions;
3366 for (; s; s = s->next) {
3367 if (s->smb2->session_id != uid) {
3376 if (session == NULL) {
3377 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3380 last_session = session;
3381 signing_key = &session->smb2_channel.signing_key;
3384 if (opcode == SMB2_OP_SESSSETUP) {
3386 * We prefer the channel signing key, if it is
3389 * If we do not have a channel signing key yet,
3390 * we try the main signing key, if it is not
3391 * the final response.
3393 if (signing_key && signing_key->length == 0 &&
3394 !NT_STATUS_IS_OK(status)) {
3395 signing_key = &session->smb2->signing_key;
3398 if (signing_key && signing_key->length == 0) {
3400 * If we do not have a session key to
3401 * verify the signature, we defer the
3402 * signing check to the caller.
3404 * The caller gets NT_STATUS_OK, it
3406 * smb2cli_session_set_session_key()
3408 * smb2cli_session_set_channel_key()
3409 * which will check the signature
3410 * with the channel signing key.
3416 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3417 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3418 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3421 * If the response was encrypted in a SMB2_TRANSFORM
3422 * pdu, which belongs to the correct session,
3423 * we do not need to do signing checks
3425 * It could be the session the response belongs to
3426 * or the session that was used to encrypt the
3427 * SMB2_TRANSFORM request.
3429 if ((session && session->smb2->session_id == uid) ||
3430 (state->smb2.encryption_session_id == uid)) {
3432 was_encrypted = true;
3436 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3438 * if the server returns NT_STATUS_USER_SESSION_DELETED
3439 * the response is not signed and we should
3440 * propagate the NT_STATUS_USER_SESSION_DELETED
3441 * status to the caller.
3443 state->smb2.signing_skipped = true;
3447 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3449 * if the server returns
3450 * NT_STATUS_INVALID_PARAMETER
3451 * the response might not be encrypted.
3453 if (state->smb2.should_encrypt && !was_encrypted) {
3454 state->smb2.signing_skipped = true;
3459 if (state->smb2.should_encrypt && !was_encrypted) {
3460 if (!state->smb2.signing_skipped) {
3461 return NT_STATUS_ACCESS_DENIED;
3465 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3466 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3467 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3469 * if the server returns
3470 * NT_STATUS_NETWORK_NAME_DELETED
3471 * NT_STATUS_FILE_CLOSED
3472 * NT_STATUS_INVALID_PARAMETER
3473 * the response might not be signed
3474 * as this happens before the signing checks.
3476 * If server echos the signature (or all zeros)
3477 * we should report the status from the server
3483 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3484 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3487 state->smb2.signing_skipped = true;
3493 static const uint8_t zeros[16];
3495 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3499 state->smb2.signing_skipped = true;
3506 status = smb2_signing_check_pdu(*signing_key,
3507 state->conn->protocol,
3509 if (!NT_STATUS_IS_OK(status)) {
3511 * If the signing check fails, we disconnect
3518 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3519 (session != NULL) && session->disconnect_expired)
3522 * this should be a short term hack
3523 * until the upper layers have implemented
3524 * re-authentication.
3529 smbXcli_req_unset_pending(req);
3532 * There might be more than one response
3533 * we need to defer the notifications
3535 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3540 tevent_req_defer_callback(req, state->ev);
3544 * Note: here we use talloc_reference() in a way
3545 * that does not expose it to the caller.
3547 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3548 if (tevent_req_nomem(inbuf_ref, req)) {
3552 /* copy the related buffers */
3553 state->smb2.recv_iov[0] = cur[1];
3554 state->smb2.recv_iov[1] = cur[2];
3555 state->smb2.recv_iov[2] = cur[3];
3557 tevent_req_done(req);
3561 return NT_STATUS_RETRY;
3564 return NT_STATUS_OK;
3567 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3568 struct iovec **piov,
3569 const struct smb2cli_req_expected_response *expected,
3570 size_t num_expected)
3572 struct smbXcli_req_state *state =
3573 tevent_req_data(req,
3574 struct smbXcli_req_state);
3577 bool found_status = false;
3578 bool found_size = false;
3585 if (state->smb2.got_async) {
3586 return STATUS_PENDING;
3589 if (tevent_req_is_nterror(req, &status)) {
3590 for (i=0; i < num_expected; i++) {
3591 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3592 found_status = true;
3598 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3604 if (num_expected == 0) {
3605 found_status = true;
3609 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3610 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3612 for (i=0; i < num_expected; i++) {
3613 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3617 found_status = true;
3618 if (expected[i].body_size == 0) {
3623 if (expected[i].body_size == body_size) {
3629 if (!found_status) {
3633 if (state->smb2.signing_skipped) {
3634 if (num_expected > 0) {
3635 return NT_STATUS_ACCESS_DENIED;
3637 if (!NT_STATUS_IS_ERR(status)) {
3638 return NT_STATUS_ACCESS_DENIED;
3643 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3647 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3653 static const struct {
3654 enum protocol_types proto;
3655 const char *smb1_name;
3656 } smb1cli_prots[] = {
3657 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3658 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3659 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3660 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3661 {PROTOCOL_LANMAN2, "LM1.2X002"},
3662 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3663 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3664 {PROTOCOL_LANMAN2, "Samba"},
3665 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3666 {PROTOCOL_NT1, "NT LM 0.12"},
3667 {PROTOCOL_SMB2_02, "SMB 2.002"},
3668 {PROTOCOL_SMB2_10, "SMB 2.???"},
3671 static const struct {
3672 enum protocol_types proto;
3673 uint16_t smb2_dialect;
3674 } smb2cli_prots[] = {
3675 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3676 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3677 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3678 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3679 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3680 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
3683 struct smbXcli_negprot_state {
3684 struct smbXcli_conn *conn;
3685 struct tevent_context *ev;
3686 uint32_t timeout_msec;
3687 enum protocol_types min_protocol;
3688 enum protocol_types max_protocol;
3692 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3696 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3697 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3698 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3699 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3700 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3701 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3705 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3706 struct tevent_context *ev,
3707 struct smbXcli_conn *conn,
3708 uint32_t timeout_msec,
3709 enum protocol_types min_protocol,
3710 enum protocol_types max_protocol)
3712 struct tevent_req *req, *subreq;
3713 struct smbXcli_negprot_state *state;
3715 req = tevent_req_create(mem_ctx, &state,
3716 struct smbXcli_negprot_state);
3722 state->timeout_msec = timeout_msec;
3723 state->min_protocol = min_protocol;
3724 state->max_protocol = max_protocol;
3726 if (min_protocol == PROTOCOL_NONE) {
3727 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3728 return tevent_req_post(req, ev);
3731 if (max_protocol == PROTOCOL_NONE) {
3732 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3733 return tevent_req_post(req, ev);
3736 if (min_protocol > max_protocol) {
3737 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3738 return tevent_req_post(req, ev);
3741 if ((min_protocol < PROTOCOL_SMB2_02) &&
3742 (max_protocol < PROTOCOL_SMB2_02)) {
3746 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3748 subreq = smbXcli_negprot_smb1_subreq(state);
3749 if (tevent_req_nomem(subreq, req)) {
3750 return tevent_req_post(req, ev);
3752 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3756 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3757 (max_protocol >= PROTOCOL_SMB2_02)) {
3761 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3763 subreq = smbXcli_negprot_smb2_subreq(state);
3764 if (tevent_req_nomem(subreq, req)) {
3765 return tevent_req_post(req, ev);
3767 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3772 * We send an SMB1 negprot with the SMB2 dialects
3773 * and expect a SMB1 or a SMB2 response.
3775 * smbXcli_negprot_dispatch_incoming() will fix the
3776 * callback to match protocol of the response.
3778 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3780 subreq = smbXcli_negprot_smb1_subreq(state);
3781 if (tevent_req_nomem(subreq, req)) {
3782 return tevent_req_post(req, ev);
3784 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3788 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3790 struct tevent_req *req =
3791 tevent_req_callback_data(subreq,
3796 * we just want the low level error
3798 status = tevent_req_simple_recv_ntstatus(subreq);
3799 TALLOC_FREE(subreq);
3800 if (tevent_req_nterror(req, status)) {
3804 /* this should never happen */
3805 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3808 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3811 DATA_BLOB bytes = data_blob_null;
3815 /* setup the protocol strings */
3816 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3820 if (smb1cli_prots[i].proto < state->min_protocol) {
3824 if (smb1cli_prots[i].proto > state->max_protocol) {
3828 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3834 * We now it is already ascii and
3835 * we want NULL termination.
3837 ok = data_blob_append(state, &bytes,
3838 smb1cli_prots[i].smb1_name,
3839 strlen(smb1cli_prots[i].smb1_name)+1);
3845 smb1cli_req_flags(state->max_protocol,
3846 state->conn->smb1.client.capabilities,
3851 return smb1cli_req_send(state, state->ev, state->conn,
3855 state->timeout_msec,
3856 0xFFFE, 0, NULL, /* pid, tid, session */
3857 0, NULL, /* wct, vwv */
3858 bytes.length, bytes.data);
3861 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3863 struct tevent_req *req =
3864 tevent_req_callback_data(subreq,
3866 struct smbXcli_negprot_state *state =
3867 tevent_req_data(req,
3868 struct smbXcli_negprot_state);
3869 struct smbXcli_conn *conn = state->conn;
3870 struct iovec *recv_iov = NULL;
3879 size_t num_prots = 0;
3881 uint32_t client_capabilities = conn->smb1.client.capabilities;
3882 uint32_t both_capabilities;
3883 uint32_t server_capabilities = 0;
3884 uint32_t capabilities;
3885 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3886 uint32_t server_max_xmit = 0;
3888 uint32_t server_max_mux = 0;
3889 uint16_t server_security_mode = 0;
3890 uint32_t server_session_key = 0;
3891 bool server_readbraw = false;
3892 bool server_writebraw = false;
3893 bool server_lockread = false;
3894 bool server_writeunlock = false;
3895 struct GUID server_guid = GUID_zero();
3896 DATA_BLOB server_gss_blob = data_blob_null;
3897 uint8_t server_challenge[8];
3898 char *server_workgroup = NULL;
3899 char *server_name = NULL;
3900 int server_time_zone = 0;
3901 NTTIME server_system_time = 0;
3902 static const struct smb1cli_req_expected_response expected[] = {
3904 .status = NT_STATUS_OK,
3905 .wct = 0x11, /* NT1 */
3908 .status = NT_STATUS_OK,
3909 .wct = 0x0D, /* LM */
3912 .status = NT_STATUS_OK,
3913 .wct = 0x01, /* CORE */
3917 ZERO_STRUCT(server_challenge);
3919 status = smb1cli_req_recv(subreq, state,
3924 NULL, /* pvwv_offset */
3927 NULL, /* pbytes_offset */
3929 expected, ARRAY_SIZE(expected));
3930 TALLOC_FREE(subreq);
3931 if (tevent_req_nterror(req, status)) {
3935 flags = CVAL(inhdr, HDR_FLG);
3937 protnum = SVAL(vwv, 0);
3939 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3940 if (smb1cli_prots[i].proto < state->min_protocol) {
3944 if (smb1cli_prots[i].proto > state->max_protocol) {
3948 if (protnum != num_prots) {
3953 conn->protocol = smb1cli_prots[i].proto;
3957 if (conn->protocol == PROTOCOL_NONE) {
3958 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3962 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3963 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3964 "and the selected protocol level doesn't support it.\n"));
3965 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3969 if (flags & FLAG_SUPPORT_LOCKREAD) {
3970 server_lockread = true;
3971 server_writeunlock = true;
3974 if (conn->protocol >= PROTOCOL_NT1) {
3975 const char *client_signing = NULL;
3976 bool server_mandatory = false;
3977 bool server_allowed = false;
3978 const char *server_signing = NULL;
3983 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3988 server_security_mode = CVAL(vwv + 1, 0);
3989 server_max_mux = SVAL(vwv + 1, 1);
3990 server_max_xmit = IVAL(vwv + 3, 1);
3991 server_session_key = IVAL(vwv + 7, 1);
3992 server_time_zone = SVALS(vwv + 15, 1);
3993 server_time_zone *= 60;
3994 /* this time arrives in real GMT */
3995 server_system_time = BVAL(vwv + 11, 1);
3996 server_capabilities = IVAL(vwv + 9, 1);
3998 key_len = CVAL(vwv + 16, 1);
4000 if (server_capabilities & CAP_RAW_MODE) {
4001 server_readbraw = true;
4002 server_writebraw = true;
4004 if (server_capabilities & CAP_LOCK_AND_READ) {
4005 server_lockread = true;
4008 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4009 DATA_BLOB blob1, blob2;
4011 if (num_bytes < 16) {
4012 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4016 blob1 = data_blob_const(bytes, 16);
4017 status = GUID_from_data_blob(&blob1, &server_guid);
4018 if (tevent_req_nterror(req, status)) {
4022 blob1 = data_blob_const(bytes+16, num_bytes-16);
4023 blob2 = data_blob_dup_talloc(state, blob1);
4024 if (blob1.length > 0 &&
4025 tevent_req_nomem(blob2.data, req)) {
4028 server_gss_blob = blob2;
4030 DATA_BLOB blob1, blob2;
4032 if (num_bytes < key_len) {
4033 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4037 if (key_len != 0 && key_len != 8) {
4038 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4043 memcpy(server_challenge, bytes, 8);
4046 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4047 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4048 if (blob1.length > 0) {
4051 len = utf16_len_n(blob1.data,
4055 ok = convert_string_talloc(state,
4063 status = map_nt_error_from_unix_common(errno);
4064 tevent_req_nterror(req, status);
4069 blob2.data += blob1.length;
4070 blob2.length -= blob1.length;
4071 if (blob2.length > 0) {
4074 len = utf16_len_n(blob1.data,
4078 ok = convert_string_talloc(state,
4086 status = map_nt_error_from_unix_common(errno);
4087 tevent_req_nterror(req, status);
4093 client_signing = "disabled";
4094 if (conn->allow_signing) {
4095 client_signing = "allowed";
4097 if (conn->mandatory_signing) {
4098 client_signing = "required";
4101 server_signing = "not supported";
4102 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4103 server_signing = "supported";
4104 server_allowed = true;
4105 } else if (conn->mandatory_signing) {
4107 * We have mandatory signing as client
4108 * lets assume the server will look at our
4109 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4110 * flag in the session setup
4112 server_signing = "not announced";
4113 server_allowed = true;
4115 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4116 server_signing = "required";
4117 server_mandatory = true;
4120 ok = smb_signing_set_negotiated(conn->smb1.signing,
4124 DEBUG(1,("cli_negprot: SMB signing is required, "
4125 "but client[%s] and server[%s] mismatch\n",
4126 client_signing, server_signing));
4127 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4131 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4137 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4141 server_security_mode = SVAL(vwv + 1, 0);
4142 server_max_xmit = SVAL(vwv + 2, 0);
4143 server_max_mux = SVAL(vwv + 3, 0);
4144 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4145 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4146 server_session_key = IVAL(vwv + 6, 0);
4147 server_time_zone = SVALS(vwv + 10, 0);
4148 server_time_zone *= 60;
4149 /* this time is converted to GMT by make_unix_date */
4150 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4151 unix_to_nt_time(&server_system_time, t);
4152 key_len = SVAL(vwv + 11, 0);
4154 if (num_bytes < key_len) {
4155 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4159 if (key_len != 0 && key_len != 8) {
4160 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4165 memcpy(server_challenge, bytes, 8);
4168 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4169 if (blob1.length > 0) {
4173 len = utf16_len_n(blob1.data,
4177 ok = convert_string_talloc(state,
4185 status = map_nt_error_from_unix_common(errno);
4186 tevent_req_nterror(req, status);
4192 /* the old core protocol */
4193 server_time_zone = get_time_zone(time(NULL));
4194 server_max_xmit = 1024;
4198 if (server_max_xmit < 1024) {
4199 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4203 if (server_max_mux < 1) {
4204 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4209 * Now calculate the negotiated capabilities
4210 * based on the mask for:
4211 * - client only flags
4212 * - flags used in both directions
4213 * - server only flags
4215 both_capabilities = client_capabilities & server_capabilities;
4216 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4217 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4218 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4220 max_xmit = MIN(client_max_xmit, server_max_xmit);
4222 conn->smb1.server.capabilities = server_capabilities;
4223 conn->smb1.capabilities = capabilities;
4225 conn->smb1.server.max_xmit = server_max_xmit;
4226 conn->smb1.max_xmit = max_xmit;
4228 conn->smb1.server.max_mux = server_max_mux;
4230 conn->smb1.server.security_mode = server_security_mode;
4232 conn->smb1.server.readbraw = server_readbraw;
4233 conn->smb1.server.writebraw = server_writebraw;
4234 conn->smb1.server.lockread = server_lockread;
4235 conn->smb1.server.writeunlock = server_writeunlock;
4237 conn->smb1.server.session_key = server_session_key;
4239 talloc_steal(conn, server_gss_blob.data);
4240 conn->smb1.server.gss_blob = server_gss_blob;
4241 conn->smb1.server.guid = server_guid;
4242 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4243 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4244 conn->smb1.server.name = talloc_move(conn, &server_name);
4246 conn->smb1.server.time_zone = server_time_zone;
4247 conn->smb1.server.system_time = server_system_time;
4249 tevent_req_done(req);
4252 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4256 uint16_t dialect_count = 0;
4258 buf = state->smb2.dyn;
4259 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4260 if (smb2cli_prots[i].proto < state->min_protocol) {
4264 if (smb2cli_prots[i].proto > state->max_protocol) {
4268 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4272 buf = state->smb2.fixed;
4274 SSVAL(buf, 2, dialect_count);
4275 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4276 SSVAL(buf, 6, 0); /* Reserved */
4277 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4278 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4280 SIVAL(buf, 8, 0); /* Capabilities */
4282 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4286 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4288 if (!NT_STATUS_IS_OK(status)) {
4291 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4293 memset(buf+12, 0, 16); /* ClientGuid */
4295 SBVAL(buf, 28, 0); /* ClientStartTime */
4297 return smb2cli_req_send(state, state->ev,
4298 state->conn, SMB2_OP_NEGPROT,
4300 state->timeout_msec,
4301 NULL, NULL, /* tcon, session */
4302 state->smb2.fixed, sizeof(state->smb2.fixed),
4303 state->smb2.dyn, dialect_count*2,
4304 UINT16_MAX); /* max_dyn_len */
4307 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4309 struct tevent_req *req =
4310 tevent_req_callback_data(subreq,
4312 struct smbXcli_negprot_state *state =
4313 tevent_req_data(req,
4314 struct smbXcli_negprot_state);
4315 struct smbXcli_conn *conn = state->conn;
4316 size_t security_offset, security_length;
4322 uint16_t dialect_revision;
4323 static const struct smb2cli_req_expected_response expected[] = {
4325 .status = NT_STATUS_OK,
4330 status = smb2cli_req_recv(subreq, state, &iov,
4331 expected, ARRAY_SIZE(expected));
4332 TALLOC_FREE(subreq);
4333 if (tevent_req_nterror(req, status)) {
4337 body = (uint8_t *)iov[1].iov_base;
4339 dialect_revision = SVAL(body, 4);
4341 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4342 if (smb2cli_prots[i].proto < state->min_protocol) {
4346 if (smb2cli_prots[i].proto > state->max_protocol) {
4350 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4354 conn->protocol = smb2cli_prots[i].proto;
4358 if (conn->protocol == PROTOCOL_NONE) {
4359 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4360 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4364 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4365 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4369 /* make sure we do not loop forever */
4370 state->min_protocol = PROTOCOL_SMB2_02;
4373 * send a SMB2 negprot, in order to negotiate
4376 subreq = smbXcli_negprot_smb2_subreq(state);
4377 if (tevent_req_nomem(subreq, req)) {
4380 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4384 conn->smb2.server.security_mode = SVAL(body, 2);
4386 blob = data_blob_const(body + 8, 16);
4387 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4388 if (tevent_req_nterror(req, status)) {
4392 conn->smb2.server.capabilities = IVAL(body, 24);
4393 conn->smb2.server.max_trans_size= IVAL(body, 28);
4394 conn->smb2.server.max_read_size = IVAL(body, 32);
4395 conn->smb2.server.max_write_size= IVAL(body, 36);
4396 conn->smb2.server.system_time = BVAL(body, 40);
4397 conn->smb2.server.start_time = BVAL(body, 48);
4399 security_offset = SVAL(body, 56);
4400 security_length = SVAL(body, 58);
4402 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4403 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4407 if (security_length > iov[2].iov_len) {
4408 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4412 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4415 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4419 tevent_req_done(req);
4422 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4423 TALLOC_CTX *tmp_mem,
4426 size_t num_pending = talloc_array_length(conn->pending);
4427 struct tevent_req *subreq;
4428 struct smbXcli_req_state *substate;
4429 struct tevent_req *req;
4430 uint32_t protocol_magic;
4431 size_t inbuf_len = smb_len_nbt(inbuf);
4433 if (num_pending != 1) {
4434 return NT_STATUS_INTERNAL_ERROR;
4437 if (inbuf_len < 4) {
4438 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4441 subreq = conn->pending[0];
4442 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4443 req = tevent_req_callback_data(subreq, struct tevent_req);
4445 protocol_magic = IVAL(inbuf, 4);
4447 switch (protocol_magic) {
4449 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4450 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4451 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4454 if (substate->smb2.recv_iov == NULL) {
4456 * For the SMB1 negprot we have move it.
4458 substate->smb2.recv_iov = substate->smb1.recv_iov;
4459 substate->smb1.recv_iov = NULL;
4463 * we got an SMB2 answer, which consumed sequence number 0
4464 * so we need to use 1 as the next one.
4466 * we also need to set the current credits to 0
4467 * as we consumed the initial one. The SMB2 answer
4468 * hopefully grant us a new credit.
4471 conn->smb2.cur_credits = 0;
4472 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4473 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4474 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4477 DEBUG(10, ("Got non-SMB PDU\n"));
4478 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4481 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4483 return tevent_req_simple_recv_ntstatus(req);
4486 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4487 uint32_t timeout_msec,
4488 enum protocol_types min_protocol,
4489 enum protocol_types max_protocol)
4491 TALLOC_CTX *frame = talloc_stackframe();
4492 struct tevent_context *ev;
4493 struct tevent_req *req;
4494 NTSTATUS status = NT_STATUS_NO_MEMORY;
4497 if (smbXcli_conn_has_async_calls(conn)) {
4499 * Can't use sync call while an async call is in flight
4501 status = NT_STATUS_INVALID_PARAMETER_MIX;
4504 ev = samba_tevent_context_init(frame);
4508 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4509 min_protocol, max_protocol);
4513 ok = tevent_req_poll(req, ev);
4515 status = map_nt_error_from_unix_common(errno);
4518 status = smbXcli_negprot_recv(req);
4524 static int smbXcli_session_destructor(struct smbXcli_session *session)
4526 if (session->conn == NULL) {
4530 DLIST_REMOVE(session->conn->sessions, session);
4534 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4535 struct smbXcli_conn *conn)
4537 struct smbXcli_session *session;
4539 session = talloc_zero(mem_ctx, struct smbXcli_session);
4540 if (session == NULL) {
4543 session->smb2 = talloc_zero(session, struct smb2cli_session);
4544 if (session->smb2 == NULL) {
4545 talloc_free(session);
4548 talloc_set_destructor(session, smbXcli_session_destructor);
4550 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4551 session->conn = conn;
4556 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4557 struct smbXcli_session *src)
4559 struct smbXcli_session *session;
4561 session = talloc_zero(mem_ctx, struct smbXcli_session);
4562 if (session == NULL) {
4565 session->smb2 = talloc_zero(session, struct smb2cli_session);
4566 if (session->smb2 == NULL) {
4567 talloc_free(session);
4571 session->conn = src->conn;
4572 *session->smb2 = *src->smb2;
4573 session->smb2_channel = src->smb2_channel;
4574 session->disconnect_expired = src->disconnect_expired;
4576 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4577 talloc_set_destructor(session, smbXcli_session_destructor);
4583 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4584 TALLOC_CTX *mem_ctx,
4587 const DATA_BLOB *application_key;
4589 *key = data_blob_null;
4591 if (session->conn == NULL) {
4592 return NT_STATUS_NO_USER_SESSION_KEY;
4595 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4596 application_key = &session->smb2->application_key;
4598 application_key = &session->smb1.application_key;
4601 if (application_key->length == 0) {
4602 return NT_STATUS_NO_USER_SESSION_KEY;
4605 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4606 if (key->data == NULL) {
4607 return NT_STATUS_NO_MEMORY;
4610 return NT_STATUS_OK;
4613 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4615 session->disconnect_expired = true;
4618 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4620 return session->smb1.session_id;
4623 void smb1cli_session_set_id(struct smbXcli_session *session,
4624 uint16_t session_id)
4626 session->smb1.session_id = session_id;
4629 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4630 const DATA_BLOB _session_key)
4632 struct smbXcli_conn *conn = session->conn;
4633 uint8_t session_key[16];
4636 return NT_STATUS_INVALID_PARAMETER_MIX;
4639 if (session->smb1.application_key.length != 0) {
4641 * TODO: do not allow this...
4643 * return NT_STATUS_INVALID_PARAMETER_MIX;
4645 data_blob_clear_free(&session->smb1.application_key);
4646 session->smb1.protected_key = false;
4649 if (_session_key.length == 0) {
4650 return NT_STATUS_OK;
4653 ZERO_STRUCT(session_key);
4654 memcpy(session_key, _session_key.data,
4655 MIN(_session_key.length, sizeof(session_key)));
4657 session->smb1.application_key = data_blob_talloc(session,
4659 sizeof(session_key));
4660 ZERO_STRUCT(session_key);
4661 if (session->smb1.application_key.data == NULL) {
4662 return NT_STATUS_NO_MEMORY;
4665 session->smb1.protected_key = false;
4667 return NT_STATUS_OK;
4670 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4672 if (session->smb1.protected_key) {
4673 /* already protected */
4674 return NT_STATUS_OK;
4677 if (session->smb1.application_key.length != 16) {
4678 return NT_STATUS_INVALID_PARAMETER_MIX;
4681 smb_key_derivation(session->smb1.application_key.data,
4682 session->smb1.application_key.length,
4683 session->smb1.application_key.data);
4685 session->smb1.protected_key = true;
4687 return NT_STATUS_OK;
4690 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4692 struct smbXcli_conn *conn = session->conn;
4693 uint8_t security_mode = 0;
4696 return security_mode;
4699 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4700 if (conn->mandatory_signing) {
4701 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4704 return security_mode;
4707 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4709 return session->smb2->session_id;
4712 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4714 return session->smb2->session_flags;
4717 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4718 uint64_t session_id,
4719 uint16_t session_flags)
4721 session->smb2->session_id = session_id;
4722 session->smb2->session_flags = session_flags;
4725 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4727 session->smb2->channel_sequence += 1;
4730 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4731 const DATA_BLOB _session_key,
4732 const struct iovec *recv_iov)
4734 struct smbXcli_conn *conn = session->conn;
4735 uint16_t no_sign_flags;
4736 uint8_t session_key[16];
4740 return NT_STATUS_INVALID_PARAMETER_MIX;
4743 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4745 if (session->smb2->session_flags & no_sign_flags) {
4746 session->smb2->should_sign = false;
4747 return NT_STATUS_OK;
4750 if (session->smb2->signing_key.length != 0) {
4751 return NT_STATUS_INVALID_PARAMETER_MIX;
4754 ZERO_STRUCT(session_key);
4755 memcpy(session_key, _session_key.data,
4756 MIN(_session_key.length, sizeof(session_key)));
4758 session->smb2->signing_key = data_blob_talloc(session,
4760 sizeof(session_key));
4761 if (session->smb2->signing_key.data == NULL) {
4762 ZERO_STRUCT(session_key);
4763 return NT_STATUS_NO_MEMORY;
4766 if (conn->protocol >= PROTOCOL_SMB2_24) {
4767 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4768 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4770 smb2_key_derivation(session_key, sizeof(session_key),
4771 label.data, label.length,
4772 context.data, context.length,
4773 session->smb2->signing_key.data);
4776 session->smb2->encryption_key = data_blob_dup_talloc(session,
4777 session->smb2->signing_key);
4778 if (session->smb2->encryption_key.data == NULL) {
4779 ZERO_STRUCT(session_key);
4780 return NT_STATUS_NO_MEMORY;
4783 if (conn->protocol >= PROTOCOL_SMB2_24) {
4784 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4785 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4787 smb2_key_derivation(session_key, sizeof(session_key),
4788 label.data, label.length,
4789 context.data, context.length,
4790 session->smb2->encryption_key.data);
4793 session->smb2->decryption_key = data_blob_dup_talloc(session,
4794 session->smb2->signing_key);
4795 if (session->smb2->decryption_key.data == NULL) {
4796 ZERO_STRUCT(session_key);
4797 return NT_STATUS_NO_MEMORY;
4800 if (conn->protocol >= PROTOCOL_SMB2_24) {
4801 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4802 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4804 smb2_key_derivation(session_key, sizeof(session_key),
4805 label.data, label.length,
4806 context.data, context.length,
4807 session->smb2->decryption_key.data);
4810 session->smb2->application_key = data_blob_dup_talloc(session,
4811 session->smb2->signing_key);
4812 if (session->smb2->application_key.data == NULL) {
4813 ZERO_STRUCT(session_key);
4814 return NT_STATUS_NO_MEMORY;
4817 if (conn->protocol >= PROTOCOL_SMB2_24) {
4818 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4819 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4821 smb2_key_derivation(session_key, sizeof(session_key),
4822 label.data, label.length,
4823 context.data, context.length,
4824 session->smb2->application_key.data);
4826 ZERO_STRUCT(session_key);
4828 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4829 session->smb2->signing_key);
4830 if (session->smb2_channel.signing_key.data == NULL) {
4831 return NT_STATUS_NO_MEMORY;
4834 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4835 session->conn->protocol,
4837 if (!NT_STATUS_IS_OK(status)) {
4841 session->smb2->should_sign = false;
4842 session->smb2->should_encrypt = false;
4844 if (conn->desire_signing) {
4845 session->smb2->should_sign = true;
4848 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4849 session->smb2->should_sign = true;
4852 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4853 session->smb2->should_encrypt = true;
4856 if (conn->protocol < PROTOCOL_SMB2_24) {
4857 session->smb2->should_encrypt = false;
4860 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4861 session->smb2->should_encrypt = false;
4864 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4865 sizeof(session->smb2->nonce_high));
4866 session->smb2->nonce_low = 1;
4868 return NT_STATUS_OK;
4871 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4872 struct smbXcli_session *session1,
4873 struct smbXcli_conn *conn,
4874 struct smbXcli_session **_session2)
4876 struct smbXcli_session *session2;
4878 if (session1->smb2->signing_key.length == 0) {
4879 return NT_STATUS_INVALID_PARAMETER_MIX;
4883 return NT_STATUS_INVALID_PARAMETER_MIX;
4886 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4887 if (session2 == NULL) {
4888 return NT_STATUS_NO_MEMORY;
4890 session2->smb2 = talloc_reference(session2, session1->smb2);
4891 if (session2->smb2 == NULL) {
4892 talloc_free(session2);
4893 return NT_STATUS_NO_MEMORY;
4896 talloc_set_destructor(session2, smbXcli_session_destructor);
4897 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4898 session2->conn = conn;
4900 *_session2 = session2;
4901 return NT_STATUS_OK;
4904 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4905 const DATA_BLOB _channel_key,
4906 const struct iovec *recv_iov)
4908 struct smbXcli_conn *conn = session->conn;
4909 uint8_t channel_key[16];
4913 return NT_STATUS_INVALID_PARAMETER_MIX;
4916 if (session->smb2_channel.signing_key.length != 0) {
4917 return NT_STATUS_INVALID_PARAMETER_MIX;
4920 ZERO_STRUCT(channel_key);
4921 memcpy(channel_key, _channel_key.data,
4922 MIN(_channel_key.length, sizeof(channel_key)));
4924 session->smb2_channel.signing_key = data_blob_talloc(session,
4926 sizeof(channel_key));
4927 if (session->smb2_channel.signing_key.data == NULL) {
4928 ZERO_STRUCT(channel_key);
4929 return NT_STATUS_NO_MEMORY;
4932 if (conn->protocol >= PROTOCOL_SMB2_24) {
4933 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4934 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4936 smb2_key_derivation(channel_key, sizeof(channel_key),
4937 label.data, label.length,
4938 context.data, context.length,
4939 session->smb2_channel.signing_key.data);
4941 ZERO_STRUCT(channel_key);
4943 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4944 session->conn->protocol,
4946 if (!NT_STATUS_IS_OK(status)) {
4950 return NT_STATUS_OK;
4953 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
4955 if (session->smb2->should_encrypt) {
4956 return NT_STATUS_OK;
4959 if (session->conn->protocol < PROTOCOL_SMB2_24) {
4960 return NT_STATUS_NOT_SUPPORTED;
4963 if (!(session->conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4964 return NT_STATUS_NOT_SUPPORTED;
4967 if (session->smb2->signing_key.data == NULL) {
4968 return NT_STATUS_NOT_SUPPORTED;
4970 session->smb2->should_encrypt = true;
4971 return NT_STATUS_OK;
4974 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4976 struct smbXcli_tcon *tcon;
4978 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4986 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4988 return tcon->smb1.tcon_id;
4991 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4993 tcon->smb1.tcon_id = tcon_id;
4996 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4998 uint16_t optional_support,
4999 uint32_t maximal_access,
5000 uint32_t guest_maximal_access,
5001 const char *service,
5002 const char *fs_type)
5004 tcon->smb1.tcon_id = tcon_id;
5005 tcon->smb1.optional_support = optional_support;
5006 tcon->smb1.maximal_access = maximal_access;
5007 tcon->smb1.guest_maximal_access = guest_maximal_access;
5009 TALLOC_FREE(tcon->smb1.service);
5010 tcon->smb1.service = talloc_strdup(tcon, service);
5011 if (service != NULL && tcon->smb1.service == NULL) {
5015 TALLOC_FREE(tcon->smb1.fs_type);
5016 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5017 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5024 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5026 return tcon->smb2.tcon_id;
5029 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5031 return tcon->smb2.capabilities;
5034 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5035 struct smbXcli_session *session,
5039 uint32_t capabilities,
5040 uint32_t maximal_access)
5042 tcon->smb2.tcon_id = tcon_id;
5043 tcon->smb2.type = type;
5044 tcon->smb2.flags = flags;
5045 tcon->smb2.capabilities = capabilities;
5046 tcon->smb2.maximal_access = maximal_access;
5048 tcon->smb2.should_encrypt = false;
5050 if (session == NULL) {
5054 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5056 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5057 tcon->smb2.should_encrypt = true;