pam_winbind: fix warn_pwd_expire implementation.
[Samba.git] / libcli / smb / smbXcli_base.c
blob0ceb1ddef88f1eedb33f7609c994c9ca336c9f78
1 /*
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/>.
21 #include "includes.h"
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"
34 #include "local.h"
36 struct smbXcli_conn;
37 struct smbXcli_req;
38 struct smbXcli_session;
39 struct smbXcli_tcon;
41 struct smbXcli_conn {
42 int read_fd;
43 int write_fd;
44 struct sockaddr_storage local_ss;
45 struct sockaddr_storage remote_ss;
46 const char *remote_name;
48 struct tevent_queue *outgoing;
49 struct tevent_req **pending;
50 struct tevent_req *read_smb_req;
52 enum protocol_types protocol;
53 bool allow_signing;
54 bool desire_signing;
55 bool mandatory_signing;
58 * The incoming dispatch function should return:
59 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
60 * - NT_STATUS_OK, if no more processing is desired, e.g.
61 * the dispatch function called
62 * tevent_req_done().
63 * - All other return values disconnect the connection.
65 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
66 TALLOC_CTX *tmp_mem,
67 uint8_t *inbuf);
69 struct {
70 struct {
71 uint32_t capabilities;
72 uint32_t max_xmit;
73 } client;
75 struct {
76 uint32_t capabilities;
77 uint32_t max_xmit;
78 uint16_t max_mux;
79 uint16_t security_mode;
80 bool readbraw;
81 bool writebraw;
82 bool lockread;
83 bool writeunlock;
84 uint32_t session_key;
85 struct GUID guid;
86 DATA_BLOB gss_blob;
87 uint8_t challenge[8];
88 const char *workgroup;
89 const char *name;
90 int time_zone;
91 NTTIME system_time;
92 } server;
94 uint32_t capabilities;
95 uint32_t max_xmit;
97 uint16_t mid;
99 struct smb_signing_state *signing;
100 struct smb_trans_enc_state *trans_enc;
102 struct tevent_req *read_braw_req;
103 } smb1;
105 struct {
106 struct {
107 uint32_t capabilities;
108 uint16_t security_mode;
109 struct GUID guid;
110 } client;
112 struct {
113 uint32_t capabilities;
114 uint16_t security_mode;
115 struct GUID guid;
116 uint32_t max_trans_size;
117 uint32_t max_read_size;
118 uint32_t max_write_size;
119 NTTIME system_time;
120 NTTIME start_time;
121 DATA_BLOB gss_blob;
122 } server;
124 uint64_t mid;
125 uint16_t cur_credits;
126 uint16_t max_credits;
127 } smb2;
129 struct smbXcli_session *sessions;
132 struct smb2cli_session {
133 uint64_t session_id;
134 uint16_t session_flags;
135 DATA_BLOB application_key;
136 DATA_BLOB signing_key;
137 bool should_sign;
138 bool should_encrypt;
139 DATA_BLOB encryption_key;
140 DATA_BLOB decryption_key;
141 uint64_t nonce_high;
142 uint64_t nonce_low;
143 uint16_t channel_sequence;
146 struct smbXcli_session {
147 struct smbXcli_session *prev, *next;
148 struct smbXcli_conn *conn;
150 struct {
151 uint16_t session_id;
152 DATA_BLOB application_key;
153 bool protected_key;
154 } smb1;
156 struct smb2cli_session *smb2;
158 struct {
159 DATA_BLOB signing_key;
160 } smb2_channel;
163 * this should be a short term hack
164 * until the upper layers have implemented
165 * re-authentication.
167 bool disconnect_expired;
170 struct smbXcli_tcon {
171 bool is_smb1;
172 uint32_t fs_attributes;
174 struct {
175 uint16_t tcon_id;
176 uint16_t optional_support;
177 uint32_t maximal_access;
178 uint32_t guest_maximal_access;
179 char *service;
180 char *fs_type;
181 } smb1;
183 struct {
184 uint32_t tcon_id;
185 uint8_t type;
186 uint32_t flags;
187 uint32_t capabilities;
188 uint32_t maximal_access;
189 bool should_encrypt;
190 } smb2;
193 struct smbXcli_req_state {
194 struct tevent_context *ev;
195 struct smbXcli_conn *conn;
196 struct smbXcli_session *session; /* maybe NULL */
197 struct smbXcli_tcon *tcon; /* maybe NULL */
199 uint8_t length_hdr[4];
201 bool one_way;
203 uint8_t *inbuf;
205 struct {
206 /* Space for the header including the wct */
207 uint8_t hdr[HDR_VWV];
210 * For normal requests, smb1cli_req_send chooses a mid.
211 * SecondaryV trans requests need to use the mid of the primary
212 * request, so we need a place to store it.
213 * Assume it is set if != 0.
215 uint16_t mid;
217 uint16_t *vwv;
218 uint8_t bytecount_buf[2];
220 #define MAX_SMB_IOV 10
221 /* length_hdr, hdr, words, byte_count, buffers */
222 struct iovec iov[1 + 3 + MAX_SMB_IOV];
223 int iov_count;
225 bool one_way_seqnum;
226 uint32_t seqnum;
227 struct tevent_req **chained_requests;
229 uint8_t recv_cmd;
230 NTSTATUS recv_status;
231 /* always an array of 3 talloc elements */
232 struct iovec *recv_iov;
233 } smb1;
235 struct {
236 const uint8_t *fixed;
237 uint16_t fixed_len;
238 const uint8_t *dyn;
239 uint32_t dyn_len;
241 uint8_t transform[SMB2_TF_HDR_SIZE];
242 uint8_t hdr[SMB2_HDR_BODY];
243 uint8_t pad[7]; /* padding space for compounding */
246 * always an array of 3 talloc elements
247 * (without a SMB2_TRANSFORM header!)
249 * HDR, BODY, DYN
251 struct iovec *recv_iov;
254 * the expected max for the response dyn_len
256 uint32_t max_dyn_len;
258 uint16_t credit_charge;
260 bool should_sign;
261 bool should_encrypt;
262 uint64_t encryption_session_id;
264 bool signing_skipped;
265 bool notify_async;
266 bool got_async;
267 } smb2;
270 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
273 * NT_STATUS_OK, means we do not notify the callers
275 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
277 while (conn->sessions) {
278 conn->sessions->conn = NULL;
279 DLIST_REMOVE(conn->sessions, conn->sessions);
282 if (conn->smb1.trans_enc) {
283 TALLOC_FREE(conn->smb1.trans_enc);
286 return 0;
289 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
290 int fd,
291 const char *remote_name,
292 enum smb_signing_setting signing_state,
293 uint32_t smb1_capabilities,
294 struct GUID *client_guid,
295 uint32_t smb2_capabilities)
297 struct smbXcli_conn *conn = NULL;
298 void *ss = NULL;
299 struct sockaddr *sa = NULL;
300 socklen_t sa_length;
301 int ret;
303 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
304 if (!conn) {
305 return NULL;
308 conn->read_fd = fd;
309 conn->write_fd = dup(fd);
310 if (conn->write_fd == -1) {
311 goto error;
314 conn->remote_name = talloc_strdup(conn, remote_name);
315 if (conn->remote_name == NULL) {
316 goto error;
320 ss = (void *)&conn->local_ss;
321 sa = (struct sockaddr *)ss;
322 sa_length = sizeof(conn->local_ss);
323 ret = getsockname(fd, sa, &sa_length);
324 if (ret == -1) {
325 goto error;
327 ss = (void *)&conn->remote_ss;
328 sa = (struct sockaddr *)ss;
329 sa_length = sizeof(conn->remote_ss);
330 ret = getpeername(fd, sa, &sa_length);
331 if (ret == -1) {
332 goto error;
335 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
336 if (conn->outgoing == NULL) {
337 goto error;
339 conn->pending = NULL;
341 conn->protocol = PROTOCOL_NONE;
343 switch (signing_state) {
344 case SMB_SIGNING_OFF:
345 /* never */
346 conn->allow_signing = false;
347 conn->desire_signing = false;
348 conn->mandatory_signing = false;
349 break;
350 case SMB_SIGNING_DEFAULT:
351 case SMB_SIGNING_IF_REQUIRED:
352 /* if the server requires it */
353 conn->allow_signing = true;
354 conn->desire_signing = false;
355 conn->mandatory_signing = false;
356 break;
357 case SMB_SIGNING_REQUIRED:
358 /* always */
359 conn->allow_signing = true;
360 conn->desire_signing = true;
361 conn->mandatory_signing = true;
362 break;
365 conn->smb1.client.capabilities = smb1_capabilities;
366 conn->smb1.client.max_xmit = UINT16_MAX;
368 conn->smb1.capabilities = conn->smb1.client.capabilities;
369 conn->smb1.max_xmit = 1024;
371 conn->smb1.mid = 1;
373 /* initialise signing */
374 conn->smb1.signing = smb_signing_init(conn,
375 conn->allow_signing,
376 conn->desire_signing,
377 conn->mandatory_signing);
378 if (!conn->smb1.signing) {
379 goto error;
382 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
383 if (conn->mandatory_signing) {
384 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
386 if (client_guid) {
387 conn->smb2.client.guid = *client_guid;
389 conn->smb2.client.capabilities = smb2_capabilities;
391 conn->smb2.cur_credits = 1;
392 conn->smb2.max_credits = 0;
394 talloc_set_destructor(conn, smbXcli_conn_destructor);
395 return conn;
397 error:
398 if (conn->write_fd != -1) {
399 close(conn->write_fd);
401 TALLOC_FREE(conn);
402 return NULL;
405 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
407 if (conn == NULL) {
408 return false;
411 if (conn->read_fd == -1) {
412 return false;
415 return true;
418 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
420 return conn->protocol;
423 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
425 if (conn->protocol >= PROTOCOL_SMB2_02) {
426 return true;
429 if (conn->smb1.capabilities & CAP_UNICODE) {
430 return true;
433 return false;
436 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
438 set_socket_options(conn->read_fd, options);
441 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
443 return &conn->local_ss;
446 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
448 return &conn->remote_ss;
451 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
453 return conn->remote_name;
456 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
458 if (conn->protocol >= PROTOCOL_SMB2_02) {
460 * TODO...
462 return 1;
465 return conn->smb1.server.max_mux;
468 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
470 if (conn->protocol >= PROTOCOL_SMB2_02) {
471 return conn->smb2.server.system_time;
474 return conn->smb1.server.system_time;
477 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
479 if (conn->protocol >= PROTOCOL_SMB2_02) {
480 return &conn->smb2.server.gss_blob;
483 return &conn->smb1.server.gss_blob;
486 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
488 if (conn->protocol >= PROTOCOL_SMB2_02) {
489 return &conn->smb2.server.guid;
492 return &conn->smb1.server.guid;
495 struct smbXcli_conn_samba_suicide_state {
496 struct smbXcli_conn *conn;
497 struct iovec iov;
498 uint8_t buf[9];
501 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
503 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
504 struct tevent_context *ev,
505 struct smbXcli_conn *conn,
506 uint8_t exitcode)
508 struct tevent_req *req, *subreq;
509 struct smbXcli_conn_samba_suicide_state *state;
511 req = tevent_req_create(mem_ctx, &state,
512 struct smbXcli_conn_samba_suicide_state);
513 if (req == NULL) {
514 return NULL;
516 state->conn = conn;
517 SIVAL(state->buf, 4, 0x74697865);
518 SCVAL(state->buf, 8, exitcode);
519 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
521 state->iov.iov_base = state->buf;
522 state->iov.iov_len = sizeof(state->buf);
524 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
525 false, &state->iov, 1);
526 if (tevent_req_nomem(subreq, req)) {
527 return tevent_req_post(req, ev);
529 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
530 return req;
533 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
535 struct tevent_req *req = tevent_req_callback_data(
536 subreq, struct tevent_req);
537 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
538 req, struct smbXcli_conn_samba_suicide_state);
539 ssize_t nwritten;
540 int err;
542 nwritten = writev_recv(subreq, &err);
543 TALLOC_FREE(subreq);
544 if (nwritten == -1) {
545 NTSTATUS status = map_nt_error_from_unix_common(err);
546 smbXcli_conn_disconnect(state->conn, status);
547 return;
549 tevent_req_done(req);
552 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
554 return tevent_req_simple_recv_ntstatus(req);
557 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
558 uint8_t exitcode)
560 TALLOC_CTX *frame = talloc_stackframe();
561 struct tevent_context *ev;
562 struct tevent_req *req;
563 NTSTATUS status = NT_STATUS_NO_MEMORY;
564 bool ok;
566 if (smbXcli_conn_has_async_calls(conn)) {
568 * Can't use sync call while an async call is in flight
570 status = NT_STATUS_INVALID_PARAMETER_MIX;
571 goto fail;
573 ev = samba_tevent_context_init(frame);
574 if (ev == NULL) {
575 goto fail;
577 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
578 if (req == NULL) {
579 goto fail;
581 ok = tevent_req_poll(req, ev);
582 if (!ok) {
583 status = map_nt_error_from_unix_common(errno);
584 goto fail;
586 status = smbXcli_conn_samba_suicide_recv(req);
587 fail:
588 TALLOC_FREE(frame);
589 return status;
592 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
594 return conn->smb1.capabilities;
597 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
599 return conn->smb1.max_xmit;
602 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
604 size_t pending;
605 uint16_t possible = conn->smb1.server.max_mux;
607 pending = tevent_queue_length(conn->outgoing);
608 if (pending >= possible) {
609 return false;
611 pending += talloc_array_length(conn->pending);
612 if (pending >= possible) {
613 return false;
616 return true;
619 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
621 return conn->smb1.server.session_key;
624 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
626 return conn->smb1.server.challenge;
629 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
631 return conn->smb1.server.security_mode;
634 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
636 return conn->smb1.server.readbraw;
639 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
641 return conn->smb1.server.writebraw;
644 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
646 return conn->smb1.server.lockread;
649 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
651 return conn->smb1.server.writeunlock;
654 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
656 return conn->smb1.server.time_zone;
659 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
660 const DATA_BLOB user_session_key,
661 const DATA_BLOB response)
663 return smb_signing_activate(conn->smb1.signing,
664 user_session_key,
665 response);
668 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
669 const uint8_t *buf, uint32_t seqnum)
671 const uint8_t *hdr = buf + NBT_HDR_SIZE;
672 size_t len = smb_len_nbt(buf);
674 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
677 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
679 return smb_signing_is_active(conn->smb1.signing);
682 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
683 struct smb_trans_enc_state *es)
685 /* Replace the old state, if any. */
686 if (conn->smb1.trans_enc) {
687 TALLOC_FREE(conn->smb1.trans_enc);
689 conn->smb1.trans_enc = es;
692 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
694 return common_encryption_on(conn->smb1.trans_enc);
698 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
700 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
701 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
703 if (NT_STATUS_IS_OK(status)) {
704 return NT_STATUS_OK;
707 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
708 return status;
711 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
715 * Is the SMB command able to hold an AND_X successor
716 * @param[in] cmd The SMB command in question
717 * @retval Can we add a chained request after "cmd"?
719 bool smb1cli_is_andx_req(uint8_t cmd)
721 switch (cmd) {
722 case SMBtconX:
723 case SMBlockingX:
724 case SMBopenX:
725 case SMBreadX:
726 case SMBwriteX:
727 case SMBsesssetupX:
728 case SMBulogoffX:
729 case SMBntcreateX:
730 return true;
731 break;
732 default:
733 break;
736 return false;
739 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
741 size_t num_pending = talloc_array_length(conn->pending);
742 uint16_t result;
744 if (conn->protocol == PROTOCOL_NONE) {
746 * This is what windows sends on the SMB1 Negprot request
747 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
749 return 0;
752 while (true) {
753 size_t i;
755 result = conn->smb1.mid++;
756 if ((result == 0) || (result == 0xffff)) {
757 continue;
760 for (i=0; i<num_pending; i++) {
761 if (result == smb1cli_req_mid(conn->pending[i])) {
762 break;
766 if (i == num_pending) {
767 return result;
772 void smbXcli_req_unset_pending(struct tevent_req *req)
774 struct smbXcli_req_state *state =
775 tevent_req_data(req,
776 struct smbXcli_req_state);
777 struct smbXcli_conn *conn = state->conn;
778 size_t num_pending = talloc_array_length(conn->pending);
779 size_t i;
781 if (state->smb1.mid != 0) {
783 * This is a [nt]trans[2] request which waits
784 * for more than one reply.
786 return;
789 talloc_set_destructor(req, NULL);
791 if (num_pending == 1) {
793 * The pending read_smb tevent_req is a child of
794 * conn->pending. So if nothing is pending anymore, we need to
795 * delete the socket read fde.
797 TALLOC_FREE(conn->pending);
798 conn->read_smb_req = NULL;
799 return;
802 for (i=0; i<num_pending; i++) {
803 if (req == conn->pending[i]) {
804 break;
807 if (i == num_pending) {
809 * Something's seriously broken. Just returning here is the
810 * right thing nevertheless, the point of this routine is to
811 * remove ourselves from conn->pending.
813 return;
817 * Remove ourselves from the conn->pending array
819 for (; i < (num_pending - 1); i++) {
820 conn->pending[i] = conn->pending[i+1];
824 * No NULL check here, we're shrinking by sizeof(void *), and
825 * talloc_realloc just adjusts the size for this.
827 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
828 num_pending - 1);
829 return;
832 static int smbXcli_req_destructor(struct tevent_req *req)
834 struct smbXcli_req_state *state =
835 tevent_req_data(req,
836 struct smbXcli_req_state);
839 * Make sure we really remove it from
840 * the pending array on destruction.
842 state->smb1.mid = 0;
843 smbXcli_req_unset_pending(req);
844 return 0;
847 static bool smb1cli_req_cancel(struct tevent_req *req);
848 static bool smb2cli_req_cancel(struct tevent_req *req);
850 static bool smbXcli_req_cancel(struct tevent_req *req)
852 struct smbXcli_req_state *state =
853 tevent_req_data(req,
854 struct smbXcli_req_state);
856 if (!smbXcli_conn_is_connected(state->conn)) {
857 return false;
860 if (state->conn->protocol == PROTOCOL_NONE) {
861 return false;
864 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
865 return smb2cli_req_cancel(req);
868 return smb1cli_req_cancel(req);
871 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
873 bool smbXcli_req_set_pending(struct tevent_req *req)
875 struct smbXcli_req_state *state =
876 tevent_req_data(req,
877 struct smbXcli_req_state);
878 struct smbXcli_conn *conn;
879 struct tevent_req **pending;
880 size_t num_pending;
882 conn = state->conn;
884 if (!smbXcli_conn_is_connected(conn)) {
885 return false;
888 num_pending = talloc_array_length(conn->pending);
890 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
891 num_pending+1);
892 if (pending == NULL) {
893 return false;
895 pending[num_pending] = req;
896 conn->pending = pending;
897 talloc_set_destructor(req, smbXcli_req_destructor);
898 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
900 if (!smbXcli_conn_receive_next(conn)) {
902 * the caller should notify the current request
904 * And all other pending requests get notified
905 * by smbXcli_conn_disconnect().
907 smbXcli_req_unset_pending(req);
908 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
909 return false;
912 return true;
915 static void smbXcli_conn_received(struct tevent_req *subreq);
917 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
919 size_t num_pending = talloc_array_length(conn->pending);
920 struct tevent_req *req;
921 struct smbXcli_req_state *state;
923 if (conn->read_smb_req != NULL) {
924 return true;
927 if (num_pending == 0) {
928 if (conn->smb2.mid < UINT64_MAX) {
929 /* no more pending requests, so we are done for now */
930 return true;
934 * If there are no more SMB2 requests possible,
935 * because we are out of message ids,
936 * we need to disconnect.
938 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
939 return true;
942 req = conn->pending[0];
943 state = tevent_req_data(req, struct smbXcli_req_state);
946 * We're the first ones, add the read_smb request that waits for the
947 * answer from the server
949 conn->read_smb_req = read_smb_send(conn->pending,
950 state->ev,
951 conn->read_fd);
952 if (conn->read_smb_req == NULL) {
953 return false;
955 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
956 return true;
959 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
961 struct smbXcli_session *session;
963 tevent_queue_stop(conn->outgoing);
965 if (conn->read_fd != -1) {
966 close(conn->read_fd);
968 if (conn->write_fd != -1) {
969 close(conn->write_fd);
971 conn->read_fd = -1;
972 conn->write_fd = -1;
974 session = conn->sessions;
975 if (talloc_array_length(conn->pending) == 0) {
977 * if we do not have pending requests
978 * there is no need to update the channel_sequence
980 session = NULL;
982 for (; session; session = session->next) {
983 smb2cli_session_increment_channel_sequence(session);
987 * Cancel all pending requests. We do not do a for-loop walking
988 * conn->pending because that array changes in
989 * smbXcli_req_unset_pending.
991 while (talloc_array_length(conn->pending) > 0) {
992 struct tevent_req *req;
993 struct smbXcli_req_state *state;
994 struct tevent_req **chain;
995 size_t num_chained;
996 size_t i;
998 req = conn->pending[0];
999 state = tevent_req_data(req, struct smbXcli_req_state);
1001 if (state->smb1.chained_requests == NULL) {
1003 * We're dead. No point waiting for trans2
1004 * replies.
1006 state->smb1.mid = 0;
1008 smbXcli_req_unset_pending(req);
1010 if (NT_STATUS_IS_OK(status)) {
1011 /* do not notify the callers */
1012 continue;
1016 * we need to defer the callback, because we may notify
1017 * more then one caller.
1019 tevent_req_defer_callback(req, state->ev);
1020 tevent_req_nterror(req, status);
1021 continue;
1024 chain = talloc_move(conn, &state->smb1.chained_requests);
1025 num_chained = talloc_array_length(chain);
1027 for (i=0; i<num_chained; i++) {
1028 req = chain[i];
1029 state = tevent_req_data(req, struct smbXcli_req_state);
1032 * We're dead. No point waiting for trans2
1033 * replies.
1035 state->smb1.mid = 0;
1037 smbXcli_req_unset_pending(req);
1039 if (NT_STATUS_IS_OK(status)) {
1040 /* do not notify the callers */
1041 continue;
1045 * we need to defer the callback, because we may notify
1046 * more than one caller.
1048 tevent_req_defer_callback(req, state->ev);
1049 tevent_req_nterror(req, status);
1051 TALLOC_FREE(chain);
1056 * Fetch a smb request's mid. Only valid after the request has been sent by
1057 * smb1cli_req_send().
1059 uint16_t smb1cli_req_mid(struct tevent_req *req)
1061 struct smbXcli_req_state *state =
1062 tevent_req_data(req,
1063 struct smbXcli_req_state);
1065 if (state->smb1.mid != 0) {
1066 return state->smb1.mid;
1069 return SVAL(state->smb1.hdr, HDR_MID);
1072 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1074 struct smbXcli_req_state *state =
1075 tevent_req_data(req,
1076 struct smbXcli_req_state);
1078 state->smb1.mid = mid;
1081 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1083 struct smbXcli_req_state *state =
1084 tevent_req_data(req,
1085 struct smbXcli_req_state);
1087 return state->smb1.seqnum;
1090 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1092 struct smbXcli_req_state *state =
1093 tevent_req_data(req,
1094 struct smbXcli_req_state);
1096 state->smb1.seqnum = seqnum;
1099 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1101 size_t result = 0;
1102 int i;
1103 for (i=0; i<count; i++) {
1104 result += iov[i].iov_len;
1106 return result;
1109 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1110 const struct iovec *iov,
1111 int count)
1113 size_t len = smbXcli_iov_len(iov, count);
1114 size_t copied;
1115 uint8_t *buf;
1116 int i;
1118 buf = talloc_array(mem_ctx, uint8_t, len);
1119 if (buf == NULL) {
1120 return NULL;
1122 copied = 0;
1123 for (i=0; i<count; i++) {
1124 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1125 copied += iov[i].iov_len;
1127 return buf;
1130 static void smb1cli_req_flags(enum protocol_types protocol,
1131 uint32_t smb1_capabilities,
1132 uint8_t smb_command,
1133 uint8_t additional_flags,
1134 uint8_t clear_flags,
1135 uint8_t *_flags,
1136 uint16_t additional_flags2,
1137 uint16_t clear_flags2,
1138 uint16_t *_flags2)
1140 uint8_t flags = 0;
1141 uint16_t flags2 = 0;
1143 if (protocol >= PROTOCOL_LANMAN1) {
1144 flags |= FLAG_CASELESS_PATHNAMES;
1145 flags |= FLAG_CANONICAL_PATHNAMES;
1148 if (protocol >= PROTOCOL_LANMAN2) {
1149 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1150 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1153 if (protocol >= PROTOCOL_NT1) {
1154 flags2 |= FLAGS2_IS_LONG_NAME;
1156 if (smb1_capabilities & CAP_UNICODE) {
1157 flags2 |= FLAGS2_UNICODE_STRINGS;
1159 if (smb1_capabilities & CAP_STATUS32) {
1160 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1162 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1163 flags2 |= FLAGS2_EXTENDED_SECURITY;
1167 flags |= additional_flags;
1168 flags &= ~clear_flags;
1169 flags2 |= additional_flags2;
1170 flags2 &= ~clear_flags2;
1172 *_flags = flags;
1173 *_flags2 = flags2;
1176 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1178 static bool smb1cli_req_cancel(struct tevent_req *req)
1180 struct smbXcli_req_state *state =
1181 tevent_req_data(req,
1182 struct smbXcli_req_state);
1183 uint8_t flags;
1184 uint16_t flags2;
1185 uint32_t pid;
1186 uint16_t mid;
1187 struct tevent_req *subreq;
1188 NTSTATUS status;
1190 flags = CVAL(state->smb1.hdr, HDR_FLG);
1191 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1192 pid = SVAL(state->smb1.hdr, HDR_PID);
1193 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1194 mid = SVAL(state->smb1.hdr, HDR_MID);
1196 subreq = smb1cli_req_create(state, state->ev,
1197 state->conn,
1198 SMBntcancel,
1199 flags, 0,
1200 flags2, 0,
1201 0, /* timeout */
1202 pid,
1203 state->tcon,
1204 state->session,
1205 0, NULL, /* vwv */
1206 0, NULL); /* bytes */
1207 if (subreq == NULL) {
1208 return false;
1210 smb1cli_req_set_mid(subreq, mid);
1212 status = smb1cli_req_chain_submit(&subreq, 1);
1213 if (!NT_STATUS_IS_OK(status)) {
1214 TALLOC_FREE(subreq);
1215 return false;
1217 smb1cli_req_set_mid(subreq, 0);
1219 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1221 return true;
1224 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1226 /* we do not care about the result */
1227 TALLOC_FREE(subreq);
1230 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1231 struct tevent_context *ev,
1232 struct smbXcli_conn *conn,
1233 uint8_t smb_command,
1234 uint8_t additional_flags,
1235 uint8_t clear_flags,
1236 uint16_t additional_flags2,
1237 uint16_t clear_flags2,
1238 uint32_t timeout_msec,
1239 uint32_t pid,
1240 struct smbXcli_tcon *tcon,
1241 struct smbXcli_session *session,
1242 uint8_t wct, uint16_t *vwv,
1243 int iov_count,
1244 struct iovec *bytes_iov)
1246 struct tevent_req *req;
1247 struct smbXcli_req_state *state;
1248 uint8_t flags = 0;
1249 uint16_t flags2 = 0;
1250 uint16_t uid = 0;
1251 uint16_t tid = 0;
1253 if (iov_count > MAX_SMB_IOV) {
1255 * Should not happen :-)
1257 return NULL;
1260 req = tevent_req_create(mem_ctx, &state,
1261 struct smbXcli_req_state);
1262 if (req == NULL) {
1263 return NULL;
1265 state->ev = ev;
1266 state->conn = conn;
1267 state->session = session;
1268 state->tcon = tcon;
1270 if (session) {
1271 uid = session->smb1.session_id;
1274 if (tcon) {
1275 tid = tcon->smb1.tcon_id;
1277 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1278 clear_flags |= FLAG_CASELESS_PATHNAMES;
1279 } else {
1280 /* Default setting, case insensitive. */
1281 additional_flags |= FLAG_CASELESS_PATHNAMES;
1284 if (smbXcli_conn_dfs_supported(conn) &&
1285 smbXcli_tcon_is_dfs_share(tcon))
1287 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1291 state->smb1.recv_cmd = 0xFF;
1292 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1293 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1294 if (state->smb1.recv_iov == NULL) {
1295 TALLOC_FREE(req);
1296 return NULL;
1299 smb1cli_req_flags(conn->protocol,
1300 conn->smb1.capabilities,
1301 smb_command,
1302 additional_flags,
1303 clear_flags,
1304 &flags,
1305 additional_flags2,
1306 clear_flags2,
1307 &flags2);
1309 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1310 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1311 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1312 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1313 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1314 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1315 SSVAL(state->smb1.hdr, HDR_TID, tid);
1316 SSVAL(state->smb1.hdr, HDR_PID, pid);
1317 SSVAL(state->smb1.hdr, HDR_UID, uid);
1318 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1319 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1321 state->smb1.vwv = vwv;
1323 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1325 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1326 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1327 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1328 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1329 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1330 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1331 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1332 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1334 if (iov_count != 0) {
1335 memcpy(&state->smb1.iov[4], bytes_iov,
1336 iov_count * sizeof(*bytes_iov));
1338 state->smb1.iov_count = iov_count + 4;
1340 if (timeout_msec > 0) {
1341 struct timeval endtime;
1343 endtime = timeval_current_ofs_msec(timeout_msec);
1344 if (!tevent_req_set_endtime(req, ev, endtime)) {
1345 return req;
1349 switch (smb_command) {
1350 case SMBtranss:
1351 case SMBtranss2:
1352 case SMBnttranss:
1353 state->one_way = true;
1354 break;
1355 case SMBntcancel:
1356 state->one_way = true;
1357 state->smb1.one_way_seqnum = true;
1358 break;
1359 case SMBlockingX:
1360 if ((wct == 8) &&
1361 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1362 state->one_way = true;
1364 break;
1367 return req;
1370 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1371 struct iovec *iov, int iov_count,
1372 uint32_t *seqnum,
1373 bool one_way_seqnum)
1375 TALLOC_CTX *frame = NULL;
1376 uint8_t *buf;
1379 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1380 * iovec directly. MD5Update would do that just fine.
1383 if (iov_count < 4) {
1384 return NT_STATUS_INVALID_PARAMETER_MIX;
1386 if (iov[0].iov_len != NBT_HDR_SIZE) {
1387 return NT_STATUS_INVALID_PARAMETER_MIX;
1389 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1390 return NT_STATUS_INVALID_PARAMETER_MIX;
1392 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1393 return NT_STATUS_INVALID_PARAMETER_MIX;
1395 if (iov[3].iov_len != sizeof(uint16_t)) {
1396 return NT_STATUS_INVALID_PARAMETER_MIX;
1399 frame = talloc_stackframe();
1401 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1402 if (buf == NULL) {
1403 return NT_STATUS_NO_MEMORY;
1406 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1407 one_way_seqnum);
1408 smb_signing_sign_pdu(conn->smb1.signing,
1409 buf, talloc_get_size(buf),
1410 *seqnum);
1411 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1413 TALLOC_FREE(frame);
1414 return NT_STATUS_OK;
1417 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1418 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1419 TALLOC_CTX *tmp_mem,
1420 uint8_t *inbuf);
1422 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1423 struct smbXcli_req_state *state,
1424 struct iovec *iov, int iov_count)
1426 struct tevent_req *subreq;
1427 NTSTATUS status;
1428 uint8_t cmd;
1429 uint16_t mid;
1431 if (!smbXcli_conn_is_connected(state->conn)) {
1432 return NT_STATUS_CONNECTION_DISCONNECTED;
1435 if (state->conn->protocol > PROTOCOL_NT1) {
1436 return NT_STATUS_REVISION_MISMATCH;
1439 if (iov_count < 4) {
1440 return NT_STATUS_INVALID_PARAMETER_MIX;
1442 if (iov[0].iov_len != NBT_HDR_SIZE) {
1443 return NT_STATUS_INVALID_PARAMETER_MIX;
1445 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1446 return NT_STATUS_INVALID_PARAMETER_MIX;
1448 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1449 return NT_STATUS_INVALID_PARAMETER_MIX;
1451 if (iov[3].iov_len != sizeof(uint16_t)) {
1452 return NT_STATUS_INVALID_PARAMETER_MIX;
1455 cmd = CVAL(iov[1].iov_base, HDR_COM);
1456 if (cmd == SMBreadBraw) {
1457 if (smbXcli_conn_has_async_calls(state->conn)) {
1458 return NT_STATUS_INVALID_PARAMETER_MIX;
1460 state->conn->smb1.read_braw_req = req;
1463 if (state->smb1.mid != 0) {
1464 mid = state->smb1.mid;
1465 } else {
1466 mid = smb1cli_alloc_mid(state->conn);
1468 SSVAL(iov[1].iov_base, HDR_MID, mid);
1470 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1472 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1473 &state->smb1.seqnum,
1474 state->smb1.one_way_seqnum);
1476 if (!NT_STATUS_IS_OK(status)) {
1477 return status;
1481 * If we supported multiple encrytion contexts
1482 * here we'd look up based on tid.
1484 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1485 char *buf, *enc_buf;
1487 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1488 if (buf == NULL) {
1489 return NT_STATUS_NO_MEMORY;
1491 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1492 (char *)buf, &enc_buf);
1493 TALLOC_FREE(buf);
1494 if (!NT_STATUS_IS_OK(status)) {
1495 DEBUG(0, ("Error in encrypting client message: %s\n",
1496 nt_errstr(status)));
1497 return status;
1499 buf = (char *)talloc_memdup(state, enc_buf,
1500 smb_len_nbt(enc_buf)+4);
1501 SAFE_FREE(enc_buf);
1502 if (buf == NULL) {
1503 return NT_STATUS_NO_MEMORY;
1505 iov[0].iov_base = (void *)buf;
1506 iov[0].iov_len = talloc_get_size(buf);
1507 iov_count = 1;
1510 if (state->conn->dispatch_incoming == NULL) {
1511 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1514 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1516 subreq = writev_send(state, state->ev, state->conn->outgoing,
1517 state->conn->write_fd, false, iov, iov_count);
1518 if (subreq == NULL) {
1519 return NT_STATUS_NO_MEMORY;
1521 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1522 return NT_STATUS_OK;
1525 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1526 struct tevent_context *ev,
1527 struct smbXcli_conn *conn,
1528 uint8_t smb_command,
1529 uint8_t additional_flags,
1530 uint8_t clear_flags,
1531 uint16_t additional_flags2,
1532 uint16_t clear_flags2,
1533 uint32_t timeout_msec,
1534 uint32_t pid,
1535 struct smbXcli_tcon *tcon,
1536 struct smbXcli_session *session,
1537 uint8_t wct, uint16_t *vwv,
1538 uint32_t num_bytes,
1539 const uint8_t *bytes)
1541 struct tevent_req *req;
1542 struct iovec iov;
1543 NTSTATUS status;
1545 iov.iov_base = discard_const_p(void, bytes);
1546 iov.iov_len = num_bytes;
1548 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1549 additional_flags, clear_flags,
1550 additional_flags2, clear_flags2,
1551 timeout_msec,
1552 pid, tcon, session,
1553 wct, vwv, 1, &iov);
1554 if (req == NULL) {
1555 return NULL;
1557 if (!tevent_req_is_in_progress(req)) {
1558 return tevent_req_post(req, ev);
1560 status = smb1cli_req_chain_submit(&req, 1);
1561 if (tevent_req_nterror(req, status)) {
1562 return tevent_req_post(req, ev);
1564 return req;
1567 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1569 struct tevent_req *req =
1570 tevent_req_callback_data(subreq,
1571 struct tevent_req);
1572 struct smbXcli_req_state *state =
1573 tevent_req_data(req,
1574 struct smbXcli_req_state);
1575 ssize_t nwritten;
1576 int err;
1578 nwritten = writev_recv(subreq, &err);
1579 TALLOC_FREE(subreq);
1580 if (nwritten == -1) {
1581 NTSTATUS status = map_nt_error_from_unix_common(err);
1582 smbXcli_conn_disconnect(state->conn, status);
1583 return;
1586 if (state->one_way) {
1587 state->inbuf = NULL;
1588 tevent_req_done(req);
1589 return;
1592 if (!smbXcli_req_set_pending(req)) {
1593 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1594 return;
1598 static void smbXcli_conn_received(struct tevent_req *subreq)
1600 struct smbXcli_conn *conn =
1601 tevent_req_callback_data(subreq,
1602 struct smbXcli_conn);
1603 TALLOC_CTX *frame = talloc_stackframe();
1604 NTSTATUS status;
1605 uint8_t *inbuf;
1606 ssize_t received;
1607 int err;
1609 if (subreq != conn->read_smb_req) {
1610 DEBUG(1, ("Internal error: cli_smb_received called with "
1611 "unexpected subreq\n"));
1612 status = NT_STATUS_INTERNAL_ERROR;
1613 smbXcli_conn_disconnect(conn, status);
1614 TALLOC_FREE(frame);
1615 return;
1617 conn->read_smb_req = NULL;
1619 received = read_smb_recv(subreq, frame, &inbuf, &err);
1620 TALLOC_FREE(subreq);
1621 if (received == -1) {
1622 status = map_nt_error_from_unix_common(err);
1623 smbXcli_conn_disconnect(conn, status);
1624 TALLOC_FREE(frame);
1625 return;
1628 status = conn->dispatch_incoming(conn, frame, inbuf);
1629 TALLOC_FREE(frame);
1630 if (NT_STATUS_IS_OK(status)) {
1632 * We should not do any more processing
1633 * as the dispatch function called
1634 * tevent_req_done().
1636 return;
1637 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1639 * We got an error, so notify all pending requests
1641 smbXcli_conn_disconnect(conn, status);
1642 return;
1646 * We got NT_STATUS_RETRY, so we may ask for a
1647 * next incoming pdu.
1649 if (!smbXcli_conn_receive_next(conn)) {
1650 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1654 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1655 struct iovec **piov, int *pnum_iov)
1657 struct iovec *iov;
1658 int num_iov;
1659 size_t buflen;
1660 size_t taken;
1661 size_t remaining;
1662 uint8_t *hdr;
1663 uint8_t cmd;
1664 uint32_t wct_ofs;
1665 NTSTATUS status;
1666 size_t min_size = MIN_SMB_SIZE;
1668 buflen = smb_len_tcp(buf);
1669 taken = 0;
1671 hdr = buf + NBT_HDR_SIZE;
1673 status = smb1cli_pull_raw_error(hdr);
1674 if (NT_STATUS_IS_ERR(status)) {
1676 * This is an ugly hack to support OS/2
1677 * which skips the byte_count in the DATA block
1678 * on some error responses.
1680 * See bug #9096
1682 min_size -= sizeof(uint16_t);
1685 if (buflen < min_size) {
1686 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1690 * This returns iovec elements in the following order:
1692 * - SMB header
1694 * - Parameter Block
1695 * - Data Block
1697 * - Parameter Block
1698 * - Data Block
1700 * - Parameter Block
1701 * - Data Block
1703 num_iov = 1;
1705 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1706 if (iov == NULL) {
1707 return NT_STATUS_NO_MEMORY;
1709 iov[0].iov_base = hdr;
1710 iov[0].iov_len = HDR_WCT;
1711 taken += HDR_WCT;
1713 cmd = CVAL(hdr, HDR_COM);
1714 wct_ofs = HDR_WCT;
1716 while (true) {
1717 size_t len = buflen - taken;
1718 struct iovec *cur;
1719 struct iovec *iov_tmp;
1720 uint8_t wct;
1721 uint32_t bcc_ofs;
1722 uint16_t bcc;
1723 size_t needed;
1726 * we need at least WCT
1728 needed = sizeof(uint8_t);
1729 if (len < needed) {
1730 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1731 __location__, (int)len, (int)needed));
1732 goto inval;
1736 * Now we check if the specified words are there
1738 wct = CVAL(hdr, wct_ofs);
1739 needed += wct * sizeof(uint16_t);
1740 if (len < needed) {
1741 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1742 __location__, (int)len, (int)needed));
1743 goto inval;
1746 if ((num_iov == 1) &&
1747 (len == needed) &&
1748 NT_STATUS_IS_ERR(status))
1751 * This is an ugly hack to support OS/2
1752 * which skips the byte_count in the DATA block
1753 * on some error responses.
1755 * See bug #9096
1757 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1758 num_iov + 2);
1759 if (iov_tmp == NULL) {
1760 TALLOC_FREE(iov);
1761 return NT_STATUS_NO_MEMORY;
1763 iov = iov_tmp;
1764 cur = &iov[num_iov];
1765 num_iov += 2;
1767 cur[0].iov_len = 0;
1768 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1769 cur[1].iov_len = 0;
1770 cur[1].iov_base = cur[0].iov_base;
1772 taken += needed;
1773 break;
1777 * we need at least BCC
1779 needed += sizeof(uint16_t);
1780 if (len < needed) {
1781 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1782 __location__, (int)len, (int)needed));
1783 goto inval;
1787 * Now we check if the specified bytes are there
1789 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1790 bcc = SVAL(hdr, bcc_ofs);
1791 needed += bcc * sizeof(uint8_t);
1792 if (len < needed) {
1793 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1794 __location__, (int)len, (int)needed));
1795 goto inval;
1799 * we allocate 2 iovec structures for words and bytes
1801 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1802 num_iov + 2);
1803 if (iov_tmp == NULL) {
1804 TALLOC_FREE(iov);
1805 return NT_STATUS_NO_MEMORY;
1807 iov = iov_tmp;
1808 cur = &iov[num_iov];
1809 num_iov += 2;
1811 cur[0].iov_len = wct * sizeof(uint16_t);
1812 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1813 cur[1].iov_len = bcc * sizeof(uint8_t);
1814 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1816 taken += needed;
1818 if (!smb1cli_is_andx_req(cmd)) {
1820 * If the current command does not have AndX chanining
1821 * we are done.
1823 break;
1826 if (wct == 0 && bcc == 0) {
1828 * An empty response also ends the chain,
1829 * most likely with an error.
1831 break;
1834 if (wct < 2) {
1835 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1836 __location__, (int)wct, (int)cmd));
1837 goto inval;
1839 cmd = CVAL(cur[0].iov_base, 0);
1840 if (cmd == 0xFF) {
1842 * If it is the end of the chain we are also done.
1844 break;
1846 wct_ofs = SVAL(cur[0].iov_base, 2);
1848 if (wct_ofs < taken) {
1849 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1851 if (wct_ofs > buflen) {
1852 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1856 * we consumed everything up to the start of the next
1857 * parameter block.
1859 taken = wct_ofs;
1862 remaining = buflen - taken;
1864 if (remaining > 0 && num_iov >= 3) {
1866 * The last DATA block gets the remaining
1867 * bytes, this is needed to support
1868 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1870 iov[num_iov-1].iov_len += remaining;
1873 *piov = iov;
1874 *pnum_iov = num_iov;
1875 return NT_STATUS_OK;
1877 inval:
1878 TALLOC_FREE(iov);
1879 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1882 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1883 TALLOC_CTX *tmp_mem,
1884 uint8_t *inbuf)
1886 struct tevent_req *req;
1887 struct smbXcli_req_state *state;
1888 NTSTATUS status;
1889 size_t num_pending;
1890 size_t i;
1891 uint8_t cmd;
1892 uint16_t mid;
1893 bool oplock_break;
1894 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1895 size_t len = smb_len_tcp(inbuf);
1896 struct iovec *iov = NULL;
1897 int num_iov = 0;
1898 struct tevent_req **chain = NULL;
1899 size_t num_chained = 0;
1900 size_t num_responses = 0;
1902 if (conn->smb1.read_braw_req != NULL) {
1903 req = conn->smb1.read_braw_req;
1904 conn->smb1.read_braw_req = NULL;
1905 state = tevent_req_data(req, struct smbXcli_req_state);
1907 smbXcli_req_unset_pending(req);
1909 if (state->smb1.recv_iov == NULL) {
1911 * For requests with more than
1912 * one response, we have to readd the
1913 * recv_iov array.
1915 state->smb1.recv_iov = talloc_zero_array(state,
1916 struct iovec,
1918 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1919 return NT_STATUS_OK;
1923 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1924 state->smb1.recv_iov[0].iov_len = len;
1925 ZERO_STRUCT(state->smb1.recv_iov[1]);
1926 ZERO_STRUCT(state->smb1.recv_iov[2]);
1928 state->smb1.recv_cmd = SMBreadBraw;
1929 state->smb1.recv_status = NT_STATUS_OK;
1930 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1932 tevent_req_done(req);
1933 return NT_STATUS_OK;
1936 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1937 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1938 DEBUG(10, ("Got non-SMB PDU\n"));
1939 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1943 * If we supported multiple encrytion contexts
1944 * here we'd look up based on tid.
1946 if (common_encryption_on(conn->smb1.trans_enc)
1947 && (CVAL(inbuf, 0) == 0)) {
1948 uint16_t enc_ctx_num;
1950 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1951 if (!NT_STATUS_IS_OK(status)) {
1952 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1953 nt_errstr(status)));
1954 return status;
1957 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1958 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1959 enc_ctx_num,
1960 conn->smb1.trans_enc->enc_ctx_num));
1961 return NT_STATUS_INVALID_HANDLE;
1964 status = common_decrypt_buffer(conn->smb1.trans_enc,
1965 (char *)inbuf);
1966 if (!NT_STATUS_IS_OK(status)) {
1967 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1968 nt_errstr(status)));
1969 return status;
1971 inhdr = inbuf + NBT_HDR_SIZE;
1972 len = smb_len_nbt(inbuf);
1975 mid = SVAL(inhdr, HDR_MID);
1976 num_pending = talloc_array_length(conn->pending);
1978 for (i=0; i<num_pending; i++) {
1979 if (mid == smb1cli_req_mid(conn->pending[i])) {
1980 break;
1983 if (i == num_pending) {
1984 /* Dump unexpected reply */
1985 return NT_STATUS_RETRY;
1988 oplock_break = false;
1990 if (mid == 0xffff) {
1992 * Paranoia checks that this is really an oplock break request.
1994 oplock_break = (len == 51); /* hdr + 8 words */
1995 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1996 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1997 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1998 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2000 if (!oplock_break) {
2001 /* Dump unexpected reply */
2002 return NT_STATUS_RETRY;
2006 req = conn->pending[i];
2007 state = tevent_req_data(req, struct smbXcli_req_state);
2009 if (!oplock_break /* oplock breaks are not signed */
2010 && !smb_signing_check_pdu(conn->smb1.signing,
2011 inhdr, len, state->smb1.seqnum+1)) {
2012 DEBUG(10, ("cli_check_sign_mac failed\n"));
2013 return NT_STATUS_ACCESS_DENIED;
2016 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2017 &iov, &num_iov);
2018 if (!NT_STATUS_IS_OK(status)) {
2019 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2020 nt_errstr(status)));
2021 return status;
2024 cmd = CVAL(inhdr, HDR_COM);
2025 status = smb1cli_pull_raw_error(inhdr);
2027 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2028 (state->session != NULL) && state->session->disconnect_expired)
2031 * this should be a short term hack
2032 * until the upper layers have implemented
2033 * re-authentication.
2035 return status;
2038 if (state->smb1.chained_requests == NULL) {
2039 if (num_iov != 3) {
2040 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2043 smbXcli_req_unset_pending(req);
2045 if (state->smb1.recv_iov == NULL) {
2047 * For requests with more than
2048 * one response, we have to readd the
2049 * recv_iov array.
2051 state->smb1.recv_iov = talloc_zero_array(state,
2052 struct iovec,
2054 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2055 return NT_STATUS_OK;
2059 state->smb1.recv_cmd = cmd;
2060 state->smb1.recv_status = status;
2061 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2063 state->smb1.recv_iov[0] = iov[0];
2064 state->smb1.recv_iov[1] = iov[1];
2065 state->smb1.recv_iov[2] = iov[2];
2067 if (talloc_array_length(conn->pending) == 0) {
2068 tevent_req_done(req);
2069 return NT_STATUS_OK;
2072 tevent_req_defer_callback(req, state->ev);
2073 tevent_req_done(req);
2074 return NT_STATUS_RETRY;
2077 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2078 num_chained = talloc_array_length(chain);
2079 num_responses = (num_iov - 1)/2;
2081 if (num_responses > num_chained) {
2082 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2085 for (i=0; i<num_chained; i++) {
2086 size_t iov_idx = 1 + (i*2);
2087 struct iovec *cur = &iov[iov_idx];
2088 uint8_t *inbuf_ref;
2090 req = chain[i];
2091 state = tevent_req_data(req, struct smbXcli_req_state);
2093 smbXcli_req_unset_pending(req);
2096 * as we finish multiple requests here
2097 * we need to defer the callbacks as
2098 * they could destroy our current stack state.
2100 tevent_req_defer_callback(req, state->ev);
2102 if (i >= num_responses) {
2103 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2104 continue;
2107 if (state->smb1.recv_iov == NULL) {
2109 * For requests with more than
2110 * one response, we have to readd the
2111 * recv_iov array.
2113 state->smb1.recv_iov = talloc_zero_array(state,
2114 struct iovec,
2116 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2117 continue;
2121 state->smb1.recv_cmd = cmd;
2123 if (i == (num_responses - 1)) {
2125 * The last request in the chain gets the status
2127 state->smb1.recv_status = status;
2128 } else {
2129 cmd = CVAL(cur[0].iov_base, 0);
2130 state->smb1.recv_status = NT_STATUS_OK;
2133 state->inbuf = inbuf;
2136 * Note: here we use talloc_reference() in a way
2137 * that does not expose it to the caller.
2139 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2140 if (tevent_req_nomem(inbuf_ref, req)) {
2141 continue;
2144 /* copy the related buffers */
2145 state->smb1.recv_iov[0] = iov[0];
2146 state->smb1.recv_iov[1] = cur[0];
2147 state->smb1.recv_iov[2] = cur[1];
2149 tevent_req_done(req);
2152 return NT_STATUS_RETRY;
2155 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2156 TALLOC_CTX *mem_ctx,
2157 struct iovec **piov,
2158 uint8_t **phdr,
2159 uint8_t *pwct,
2160 uint16_t **pvwv,
2161 uint32_t *pvwv_offset,
2162 uint32_t *pnum_bytes,
2163 uint8_t **pbytes,
2164 uint32_t *pbytes_offset,
2165 uint8_t **pinbuf,
2166 const struct smb1cli_req_expected_response *expected,
2167 size_t num_expected)
2169 struct smbXcli_req_state *state =
2170 tevent_req_data(req,
2171 struct smbXcli_req_state);
2172 NTSTATUS status = NT_STATUS_OK;
2173 struct iovec *recv_iov = NULL;
2174 uint8_t *hdr = NULL;
2175 uint8_t wct = 0;
2176 uint32_t vwv_offset = 0;
2177 uint16_t *vwv = NULL;
2178 uint32_t num_bytes = 0;
2179 uint32_t bytes_offset = 0;
2180 uint8_t *bytes = NULL;
2181 size_t i;
2182 bool found_status = false;
2183 bool found_size = false;
2185 if (piov != NULL) {
2186 *piov = NULL;
2188 if (phdr != NULL) {
2189 *phdr = 0;
2191 if (pwct != NULL) {
2192 *pwct = 0;
2194 if (pvwv != NULL) {
2195 *pvwv = NULL;
2197 if (pvwv_offset != NULL) {
2198 *pvwv_offset = 0;
2200 if (pnum_bytes != NULL) {
2201 *pnum_bytes = 0;
2203 if (pbytes != NULL) {
2204 *pbytes = NULL;
2206 if (pbytes_offset != NULL) {
2207 *pbytes_offset = 0;
2209 if (pinbuf != NULL) {
2210 *pinbuf = NULL;
2213 if (state->inbuf != NULL) {
2214 recv_iov = state->smb1.recv_iov;
2215 state->smb1.recv_iov = NULL;
2216 if (state->smb1.recv_cmd != SMBreadBraw) {
2217 hdr = (uint8_t *)recv_iov[0].iov_base;
2218 wct = recv_iov[1].iov_len/2;
2219 vwv = (uint16_t *)recv_iov[1].iov_base;
2220 vwv_offset = PTR_DIFF(vwv, hdr);
2221 num_bytes = recv_iov[2].iov_len;
2222 bytes = (uint8_t *)recv_iov[2].iov_base;
2223 bytes_offset = PTR_DIFF(bytes, hdr);
2227 if (tevent_req_is_nterror(req, &status)) {
2228 for (i=0; i < num_expected; i++) {
2229 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2230 found_status = true;
2231 break;
2235 if (found_status) {
2236 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2239 return status;
2242 if (num_expected == 0) {
2243 found_status = true;
2244 found_size = true;
2247 status = state->smb1.recv_status;
2249 for (i=0; i < num_expected; i++) {
2250 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2251 continue;
2254 found_status = true;
2255 if (expected[i].wct == 0) {
2256 found_size = true;
2257 break;
2260 if (expected[i].wct == wct) {
2261 found_size = true;
2262 break;
2266 if (!found_status) {
2267 return status;
2270 if (!found_size) {
2271 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2274 if (piov != NULL) {
2275 *piov = talloc_move(mem_ctx, &recv_iov);
2278 if (phdr != NULL) {
2279 *phdr = hdr;
2281 if (pwct != NULL) {
2282 *pwct = wct;
2284 if (pvwv != NULL) {
2285 *pvwv = vwv;
2287 if (pvwv_offset != NULL) {
2288 *pvwv_offset = vwv_offset;
2290 if (pnum_bytes != NULL) {
2291 *pnum_bytes = num_bytes;
2293 if (pbytes != NULL) {
2294 *pbytes = bytes;
2296 if (pbytes_offset != NULL) {
2297 *pbytes_offset = bytes_offset;
2299 if (pinbuf != NULL) {
2300 *pinbuf = state->inbuf;
2303 return status;
2306 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2308 size_t wct_ofs;
2309 int i;
2311 wct_ofs = HDR_WCT;
2313 for (i=0; i<num_reqs; i++) {
2314 struct smbXcli_req_state *state;
2315 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2316 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2317 state->smb1.iov_count-2);
2318 wct_ofs = (wct_ofs + 3) & ~3;
2320 return wct_ofs;
2323 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2325 struct smbXcli_req_state *first_state =
2326 tevent_req_data(reqs[0],
2327 struct smbXcli_req_state);
2328 struct smbXcli_req_state *state;
2329 size_t wct_offset;
2330 size_t chain_padding = 0;
2331 int i, iovlen;
2332 struct iovec *iov = NULL;
2333 struct iovec *this_iov;
2334 NTSTATUS status;
2335 size_t nbt_len;
2337 if (num_reqs == 1) {
2338 return smb1cli_req_writev_submit(reqs[0], first_state,
2339 first_state->smb1.iov,
2340 first_state->smb1.iov_count);
2343 iovlen = 0;
2344 for (i=0; i<num_reqs; i++) {
2345 if (!tevent_req_is_in_progress(reqs[i])) {
2346 return NT_STATUS_INTERNAL_ERROR;
2349 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2351 if (state->smb1.iov_count < 4) {
2352 return NT_STATUS_INVALID_PARAMETER_MIX;
2355 if (i == 0) {
2357 * The NBT and SMB header
2359 iovlen += 2;
2360 } else {
2362 * Chain padding
2364 iovlen += 1;
2368 * words and bytes
2370 iovlen += state->smb1.iov_count - 2;
2373 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2374 if (iov == NULL) {
2375 return NT_STATUS_NO_MEMORY;
2378 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2379 first_state, reqs, sizeof(*reqs) * num_reqs);
2380 if (first_state->smb1.chained_requests == NULL) {
2381 TALLOC_FREE(iov);
2382 return NT_STATUS_NO_MEMORY;
2385 wct_offset = HDR_WCT;
2386 this_iov = iov;
2388 for (i=0; i<num_reqs; i++) {
2389 size_t next_padding = 0;
2390 uint16_t *vwv;
2392 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2394 if (i < num_reqs-1) {
2395 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2396 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2397 TALLOC_FREE(iov);
2398 TALLOC_FREE(first_state->smb1.chained_requests);
2399 return NT_STATUS_INVALID_PARAMETER_MIX;
2403 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2404 state->smb1.iov_count-2) + 1;
2405 if ((wct_offset % 4) != 0) {
2406 next_padding = 4 - (wct_offset % 4);
2408 wct_offset += next_padding;
2409 vwv = state->smb1.vwv;
2411 if (i < num_reqs-1) {
2412 struct smbXcli_req_state *next_state =
2413 tevent_req_data(reqs[i+1],
2414 struct smbXcli_req_state);
2415 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2416 SCVAL(vwv+0, 1, 0);
2417 SSVAL(vwv+1, 0, wct_offset);
2418 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2419 /* properly end the chain */
2420 SCVAL(vwv+0, 0, 0xff);
2421 SCVAL(vwv+0, 1, 0xff);
2422 SSVAL(vwv+1, 0, 0);
2425 if (i == 0) {
2427 * The NBT and SMB header
2429 this_iov[0] = state->smb1.iov[0];
2430 this_iov[1] = state->smb1.iov[1];
2431 this_iov += 2;
2432 } else {
2434 * This one is a bit subtle. We have to add
2435 * chain_padding bytes between the requests, and we
2436 * have to also include the wct field of the
2437 * subsequent requests. We use the subsequent header
2438 * for the padding, it contains the wct field in its
2439 * last byte.
2441 this_iov[0].iov_len = chain_padding+1;
2442 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2443 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2444 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2445 this_iov += 1;
2449 * copy the words and bytes
2451 memcpy(this_iov, state->smb1.iov+2,
2452 sizeof(struct iovec) * (state->smb1.iov_count-2));
2453 this_iov += state->smb1.iov_count - 2;
2454 chain_padding = next_padding;
2457 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2458 if (nbt_len > first_state->conn->smb1.max_xmit) {
2459 TALLOC_FREE(iov);
2460 TALLOC_FREE(first_state->smb1.chained_requests);
2461 return NT_STATUS_INVALID_PARAMETER_MIX;
2464 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2465 if (!NT_STATUS_IS_OK(status)) {
2466 TALLOC_FREE(iov);
2467 TALLOC_FREE(first_state->smb1.chained_requests);
2468 return status;
2471 return NT_STATUS_OK;
2474 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2476 return ((tevent_queue_length(conn->outgoing) != 0)
2477 || (talloc_array_length(conn->pending) != 0));
2480 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2482 if (conn->protocol >= PROTOCOL_SMB2_02) {
2483 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2486 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2489 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2491 uint16_t credits = 1;
2493 if (conn->smb2.cur_credits == 0) {
2494 if (max_dyn_len != NULL) {
2495 *max_dyn_len = 0;
2497 return false;
2500 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2501 credits = conn->smb2.cur_credits;
2504 if (max_dyn_len != NULL) {
2505 *max_dyn_len = credits * 65536;
2508 return true;
2511 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2513 return conn->smb2.server.capabilities;
2516 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2518 return conn->smb2.server.security_mode;
2521 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2523 return conn->smb2.server.max_trans_size;
2526 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2528 return conn->smb2.server.max_read_size;
2531 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2533 return conn->smb2.server.max_write_size;
2536 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2537 uint16_t max_credits)
2539 conn->smb2.max_credits = max_credits;
2542 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2544 static bool smb2cli_req_cancel(struct tevent_req *req)
2546 struct smbXcli_req_state *state =
2547 tevent_req_data(req,
2548 struct smbXcli_req_state);
2549 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2550 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2551 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2552 struct smbXcli_tcon *tcon = state->tcon;
2553 struct smbXcli_session *session = state->session;
2554 uint8_t *fixed = state->smb2.pad;
2555 uint16_t fixed_len = 4;
2556 struct tevent_req *subreq;
2557 struct smbXcli_req_state *substate;
2558 NTSTATUS status;
2560 SSVAL(fixed, 0, 0x04);
2561 SSVAL(fixed, 2, 0);
2563 subreq = smb2cli_req_create(state, state->ev,
2564 state->conn,
2565 SMB2_OP_CANCEL,
2566 flags, 0,
2567 0, /* timeout */
2568 tcon, session,
2569 fixed, fixed_len,
2570 NULL, 0, 0);
2571 if (subreq == NULL) {
2572 return false;
2574 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2577 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2578 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2580 flags &= SMB2_HDR_FLAG_ASYNC;
2582 if (flags & SMB2_HDR_FLAG_ASYNC) {
2583 mid = 0;
2586 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2587 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2588 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2590 status = smb2cli_req_compound_submit(&subreq, 1);
2591 if (!NT_STATUS_IS_OK(status)) {
2592 TALLOC_FREE(subreq);
2593 return false;
2596 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2598 return true;
2601 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2603 /* we do not care about the result */
2604 TALLOC_FREE(subreq);
2607 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2608 struct tevent_context *ev,
2609 struct smbXcli_conn *conn,
2610 uint16_t cmd,
2611 uint32_t additional_flags,
2612 uint32_t clear_flags,
2613 uint32_t timeout_msec,
2614 struct smbXcli_tcon *tcon,
2615 struct smbXcli_session *session,
2616 const uint8_t *fixed,
2617 uint16_t fixed_len,
2618 const uint8_t *dyn,
2619 uint32_t dyn_len,
2620 uint32_t max_dyn_len)
2622 struct tevent_req *req;
2623 struct smbXcli_req_state *state;
2624 uint32_t flags = 0;
2625 uint32_t tid = 0;
2626 uint64_t uid = 0;
2627 bool use_channel_sequence = false;
2628 uint16_t channel_sequence = 0;
2630 req = tevent_req_create(mem_ctx, &state,
2631 struct smbXcli_req_state);
2632 if (req == NULL) {
2633 return NULL;
2636 state->ev = ev;
2637 state->conn = conn;
2638 state->session = session;
2639 state->tcon = tcon;
2641 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2642 use_channel_sequence = true;
2643 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2644 use_channel_sequence = true;
2647 if (session) {
2648 uid = session->smb2->session_id;
2650 if (use_channel_sequence) {
2651 channel_sequence = session->smb2->channel_sequence;
2654 state->smb2.should_sign = session->smb2->should_sign;
2655 state->smb2.should_encrypt = session->smb2->should_encrypt;
2657 if (cmd == SMB2_OP_SESSSETUP &&
2658 session->smb2->signing_key.length != 0) {
2659 state->smb2.should_sign = true;
2662 if (cmd == SMB2_OP_SESSSETUP &&
2663 session->smb2_channel.signing_key.length == 0) {
2664 state->smb2.should_encrypt = false;
2668 if (tcon) {
2669 tid = tcon->smb2.tcon_id;
2671 if (tcon->smb2.should_encrypt) {
2672 state->smb2.should_encrypt = true;
2676 if (state->smb2.should_encrypt) {
2677 state->smb2.should_sign = false;
2680 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2681 if (state->smb2.recv_iov == NULL) {
2682 TALLOC_FREE(req);
2683 return NULL;
2686 flags |= additional_flags;
2687 flags &= ~clear_flags;
2689 state->smb2.fixed = fixed;
2690 state->smb2.fixed_len = fixed_len;
2691 state->smb2.dyn = dyn;
2692 state->smb2.dyn_len = dyn_len;
2693 state->smb2.max_dyn_len = max_dyn_len;
2695 if (state->smb2.should_encrypt) {
2696 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2697 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2700 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2701 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2702 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2703 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2704 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2705 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2706 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2707 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2709 switch (cmd) {
2710 case SMB2_OP_CANCEL:
2711 state->one_way = true;
2712 break;
2713 case SMB2_OP_BREAK:
2715 * If this is a dummy request, it will have
2716 * UINT64_MAX as message id.
2717 * If we send on break acknowledgement,
2718 * this gets overwritten later.
2720 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2721 break;
2724 if (timeout_msec > 0) {
2725 struct timeval endtime;
2727 endtime = timeval_current_ofs_msec(timeout_msec);
2728 if (!tevent_req_set_endtime(req, ev, endtime)) {
2729 return req;
2733 return req;
2736 void smb2cli_req_set_notify_async(struct tevent_req *req)
2738 struct smbXcli_req_state *state =
2739 tevent_req_data(req,
2740 struct smbXcli_req_state);
2742 state->smb2.notify_async = true;
2745 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2746 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2747 TALLOC_CTX *tmp_mem,
2748 uint8_t *inbuf);
2750 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2751 int num_reqs)
2753 struct smbXcli_req_state *state;
2754 struct tevent_req *subreq;
2755 struct iovec *iov;
2756 int i, num_iov, nbt_len;
2757 int tf_iov = -1;
2758 const DATA_BLOB *encryption_key = NULL;
2759 uint64_t encryption_session_id = 0;
2762 * 1 for the nbt length, optional TRANSFORM
2763 * per request: HDR, fixed, dyn, padding
2764 * -1 because the last one does not need padding
2767 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2768 if (iov == NULL) {
2769 return NT_STATUS_NO_MEMORY;
2772 num_iov = 1;
2773 nbt_len = 0;
2776 * the session of the first request that requires encryption
2777 * specifies the encryption key.
2779 for (i=0; i<num_reqs; i++) {
2780 if (!tevent_req_is_in_progress(reqs[i])) {
2781 return NT_STATUS_INTERNAL_ERROR;
2784 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2786 if (!smbXcli_conn_is_connected(state->conn)) {
2787 return NT_STATUS_CONNECTION_DISCONNECTED;
2790 if ((state->conn->protocol != PROTOCOL_NONE) &&
2791 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2792 return NT_STATUS_REVISION_MISMATCH;
2795 if (state->session == NULL) {
2796 continue;
2799 if (!state->smb2.should_encrypt) {
2800 continue;
2803 encryption_key = &state->session->smb2->encryption_key;
2804 if (encryption_key->length == 0) {
2805 return NT_STATUS_INVALID_PARAMETER_MIX;
2808 encryption_session_id = state->session->smb2->session_id;
2810 tf_iov = num_iov;
2811 iov[num_iov].iov_base = state->smb2.transform;
2812 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2813 num_iov += 1;
2815 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2816 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2817 state->session->smb2->nonce_low);
2818 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2819 state->session->smb2->nonce_high);
2820 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2821 encryption_session_id);
2823 state->session->smb2->nonce_low += 1;
2824 if (state->session->smb2->nonce_low == 0) {
2825 state->session->smb2->nonce_high += 1;
2826 state->session->smb2->nonce_low += 1;
2829 nbt_len += SMB2_TF_HDR_SIZE;
2830 break;
2833 for (i=0; i<num_reqs; i++) {
2834 int hdr_iov;
2835 size_t reqlen;
2836 bool ret;
2837 uint16_t opcode;
2838 uint64_t avail;
2839 uint16_t charge;
2840 uint16_t credits;
2841 uint64_t mid;
2842 const DATA_BLOB *signing_key = NULL;
2844 if (!tevent_req_is_in_progress(reqs[i])) {
2845 return NT_STATUS_INTERNAL_ERROR;
2848 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2850 if (!smbXcli_conn_is_connected(state->conn)) {
2851 return NT_STATUS_CONNECTION_DISCONNECTED;
2854 if ((state->conn->protocol != PROTOCOL_NONE) &&
2855 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2856 return NT_STATUS_REVISION_MISMATCH;
2859 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2860 if (opcode == SMB2_OP_CANCEL) {
2861 goto skip_credits;
2864 avail = UINT64_MAX - state->conn->smb2.mid;
2865 if (avail < 1) {
2866 return NT_STATUS_CONNECTION_ABORTED;
2869 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2870 uint32_t max_dyn_len = 1;
2872 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2873 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2875 charge = (max_dyn_len - 1)/ 65536 + 1;
2876 } else {
2877 charge = 1;
2880 charge = MAX(state->smb2.credit_charge, charge);
2882 avail = MIN(avail, state->conn->smb2.cur_credits);
2883 if (avail < charge) {
2884 return NT_STATUS_INTERNAL_ERROR;
2887 credits = 0;
2888 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2889 credits = state->conn->smb2.max_credits -
2890 state->conn->smb2.cur_credits;
2892 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2893 credits += 1;
2896 mid = state->conn->smb2.mid;
2897 state->conn->smb2.mid += charge;
2898 state->conn->smb2.cur_credits -= charge;
2900 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2901 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2903 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2904 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2906 skip_credits:
2907 if (state->session && encryption_key == NULL) {
2909 * We prefer the channel signing key if it is
2910 * already there.
2912 if (state->smb2.should_sign) {
2913 signing_key = &state->session->smb2_channel.signing_key;
2917 * If it is a channel binding, we already have the main
2918 * signing key and try that one.
2920 if (signing_key && signing_key->length == 0) {
2921 signing_key = &state->session->smb2->signing_key;
2925 * If we do not have any session key yet, we skip the
2926 * signing of SMB2_OP_SESSSETUP requests.
2928 if (signing_key && signing_key->length == 0) {
2929 signing_key = NULL;
2933 hdr_iov = num_iov;
2934 iov[num_iov].iov_base = state->smb2.hdr;
2935 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2936 num_iov += 1;
2938 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2939 iov[num_iov].iov_len = state->smb2.fixed_len;
2940 num_iov += 1;
2942 if (state->smb2.dyn != NULL) {
2943 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2944 iov[num_iov].iov_len = state->smb2.dyn_len;
2945 num_iov += 1;
2948 reqlen = sizeof(state->smb2.hdr);
2949 reqlen += state->smb2.fixed_len;
2950 reqlen += state->smb2.dyn_len;
2952 if (i < num_reqs-1) {
2953 if ((reqlen % 8) > 0) {
2954 uint8_t pad = 8 - (reqlen % 8);
2955 iov[num_iov].iov_base = state->smb2.pad;
2956 iov[num_iov].iov_len = pad;
2957 num_iov += 1;
2958 reqlen += pad;
2960 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2963 state->smb2.encryption_session_id = encryption_session_id;
2965 if (signing_key != NULL) {
2966 NTSTATUS status;
2968 status = smb2_signing_sign_pdu(*signing_key,
2969 state->session->conn->protocol,
2970 &iov[hdr_iov], num_iov - hdr_iov);
2971 if (!NT_STATUS_IS_OK(status)) {
2972 return status;
2976 nbt_len += reqlen;
2978 ret = smbXcli_req_set_pending(reqs[i]);
2979 if (!ret) {
2980 return NT_STATUS_NO_MEMORY;
2984 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2985 _smb_setlen_tcp(state->length_hdr, nbt_len);
2986 iov[0].iov_base = state->length_hdr;
2987 iov[0].iov_len = sizeof(state->length_hdr);
2989 if (encryption_key != NULL) {
2990 NTSTATUS status;
2991 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2992 uint8_t *buf;
2993 int vi;
2995 buf = talloc_array(iov, uint8_t, buflen);
2996 if (buf == NULL) {
2997 return NT_STATUS_NO_MEMORY;
3001 * We copy the buffers before encrypting them,
3002 * this is at least currently needed for the
3003 * to keep state->smb2.hdr.
3005 * Also the callers may expect there buffers
3006 * to be const.
3008 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3009 struct iovec *v = &iov[vi];
3010 const uint8_t *o = (const uint8_t *)v->iov_base;
3012 memcpy(buf, o, v->iov_len);
3013 v->iov_base = (void *)buf;
3014 buf += v->iov_len;
3017 status = smb2_signing_encrypt_pdu(*encryption_key,
3018 state->conn->protocol,
3019 &iov[tf_iov], num_iov - tf_iov);
3020 if (!NT_STATUS_IS_OK(status)) {
3021 return status;
3025 if (state->conn->dispatch_incoming == NULL) {
3026 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3029 subreq = writev_send(state, state->ev, state->conn->outgoing,
3030 state->conn->write_fd, false, iov, num_iov);
3031 if (subreq == NULL) {
3032 return NT_STATUS_NO_MEMORY;
3034 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3035 return NT_STATUS_OK;
3038 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3040 struct smbXcli_req_state *state =
3041 tevent_req_data(req,
3042 struct smbXcli_req_state);
3044 state->smb2.credit_charge = charge;
3047 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3048 struct tevent_context *ev,
3049 struct smbXcli_conn *conn,
3050 uint16_t cmd,
3051 uint32_t additional_flags,
3052 uint32_t clear_flags,
3053 uint32_t timeout_msec,
3054 struct smbXcli_tcon *tcon,
3055 struct smbXcli_session *session,
3056 const uint8_t *fixed,
3057 uint16_t fixed_len,
3058 const uint8_t *dyn,
3059 uint32_t dyn_len,
3060 uint32_t max_dyn_len)
3062 struct tevent_req *req;
3063 NTSTATUS status;
3065 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3066 additional_flags, clear_flags,
3067 timeout_msec,
3068 tcon, session,
3069 fixed, fixed_len,
3070 dyn, dyn_len,
3071 max_dyn_len);
3072 if (req == NULL) {
3073 return NULL;
3075 if (!tevent_req_is_in_progress(req)) {
3076 return tevent_req_post(req, ev);
3078 status = smb2cli_req_compound_submit(&req, 1);
3079 if (tevent_req_nterror(req, status)) {
3080 return tevent_req_post(req, ev);
3082 return req;
3085 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3087 struct tevent_req *req =
3088 tevent_req_callback_data(subreq,
3089 struct tevent_req);
3090 struct smbXcli_req_state *state =
3091 tevent_req_data(req,
3092 struct smbXcli_req_state);
3093 ssize_t nwritten;
3094 int err;
3096 nwritten = writev_recv(subreq, &err);
3097 TALLOC_FREE(subreq);
3098 if (nwritten == -1) {
3099 /* here, we need to notify all pending requests */
3100 NTSTATUS status = map_nt_error_from_unix_common(err);
3101 smbXcli_conn_disconnect(state->conn, status);
3102 return;
3106 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3107 uint8_t *buf,
3108 size_t buflen,
3109 TALLOC_CTX *mem_ctx,
3110 struct iovec **piov, int *pnum_iov)
3112 struct iovec *iov;
3113 int num_iov = 0;
3114 size_t taken = 0;
3115 uint8_t *first_hdr = buf;
3116 size_t verified_buflen = 0;
3117 uint8_t *tf = NULL;
3118 size_t tf_len = 0;
3120 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3121 if (iov == NULL) {
3122 return NT_STATUS_NO_MEMORY;
3125 while (taken < buflen) {
3126 size_t len = buflen - taken;
3127 uint8_t *hdr = first_hdr + taken;
3128 struct iovec *cur;
3129 size_t full_size;
3130 size_t next_command_ofs;
3131 uint16_t body_size;
3132 struct iovec *iov_tmp;
3134 if (verified_buflen > taken) {
3135 len = verified_buflen - taken;
3136 } else {
3137 tf = NULL;
3138 tf_len = 0;
3141 if (len < 4) {
3142 DEBUG(10, ("%d bytes left, expected at least %d\n",
3143 (int)len, 4));
3144 goto inval;
3146 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3147 struct smbXcli_session *s;
3148 uint64_t uid;
3149 struct iovec tf_iov[2];
3150 size_t enc_len;
3151 NTSTATUS status;
3153 if (len < SMB2_TF_HDR_SIZE) {
3154 DEBUG(10, ("%d bytes left, expected at least %d\n",
3155 (int)len, SMB2_TF_HDR_SIZE));
3156 goto inval;
3158 tf = hdr;
3159 tf_len = SMB2_TF_HDR_SIZE;
3160 taken += tf_len;
3162 hdr = first_hdr + taken;
3163 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3164 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3166 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3167 DEBUG(10, ("%d bytes left, expected at least %d\n",
3168 (int)len,
3169 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3170 goto inval;
3173 s = conn->sessions;
3174 for (; s; s = s->next) {
3175 if (s->smb2->session_id != uid) {
3176 continue;
3178 break;
3181 if (s == NULL) {
3182 DEBUG(10, ("unknown session_id %llu\n",
3183 (unsigned long long)uid));
3184 goto inval;
3187 tf_iov[0].iov_base = (void *)tf;
3188 tf_iov[0].iov_len = tf_len;
3189 tf_iov[1].iov_base = (void *)hdr;
3190 tf_iov[1].iov_len = enc_len;
3192 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3193 conn->protocol,
3194 tf_iov, 2);
3195 if (!NT_STATUS_IS_OK(status)) {
3196 TALLOC_FREE(iov);
3197 return status;
3200 verified_buflen = taken + enc_len;
3201 len = enc_len;
3205 * We need the header plus the body length field
3208 if (len < SMB2_HDR_BODY + 2) {
3209 DEBUG(10, ("%d bytes left, expected at least %d\n",
3210 (int)len, SMB2_HDR_BODY));
3211 goto inval;
3213 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3214 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3215 IVAL(hdr, 0)));
3216 goto inval;
3218 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3219 DEBUG(10, ("Got HDR len %d, expected %d\n",
3220 SVAL(hdr, 4), SMB2_HDR_BODY));
3221 goto inval;
3224 full_size = len;
3225 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3226 body_size = SVAL(hdr, SMB2_HDR_BODY);
3228 if (next_command_ofs != 0) {
3229 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3230 goto inval;
3232 if (next_command_ofs > full_size) {
3233 goto inval;
3235 full_size = next_command_ofs;
3237 if (body_size < 2) {
3238 goto inval;
3240 body_size &= 0xfffe;
3242 if (body_size > (full_size - SMB2_HDR_BODY)) {
3243 goto inval;
3246 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3247 num_iov + 4);
3248 if (iov_tmp == NULL) {
3249 TALLOC_FREE(iov);
3250 return NT_STATUS_NO_MEMORY;
3252 iov = iov_tmp;
3253 cur = &iov[num_iov];
3254 num_iov += 4;
3256 cur[0].iov_base = tf;
3257 cur[0].iov_len = tf_len;
3258 cur[1].iov_base = hdr;
3259 cur[1].iov_len = SMB2_HDR_BODY;
3260 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3261 cur[2].iov_len = body_size;
3262 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3263 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3265 taken += full_size;
3268 *piov = iov;
3269 *pnum_iov = num_iov;
3270 return NT_STATUS_OK;
3272 inval:
3273 TALLOC_FREE(iov);
3274 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3277 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3278 uint64_t mid)
3280 size_t num_pending = talloc_array_length(conn->pending);
3281 size_t i;
3283 for (i=0; i<num_pending; i++) {
3284 struct tevent_req *req = conn->pending[i];
3285 struct smbXcli_req_state *state =
3286 tevent_req_data(req,
3287 struct smbXcli_req_state);
3289 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3290 return req;
3293 return NULL;
3296 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3297 TALLOC_CTX *tmp_mem,
3298 uint8_t *inbuf)
3300 struct tevent_req *req;
3301 struct smbXcli_req_state *state = NULL;
3302 struct iovec *iov;
3303 int i, num_iov;
3304 NTSTATUS status;
3305 bool defer = true;
3306 struct smbXcli_session *last_session = NULL;
3307 size_t inbuf_len = smb_len_tcp(inbuf);
3309 status = smb2cli_inbuf_parse_compound(conn,
3310 inbuf + NBT_HDR_SIZE,
3311 inbuf_len,
3312 tmp_mem,
3313 &iov, &num_iov);
3314 if (!NT_STATUS_IS_OK(status)) {
3315 return status;
3318 for (i=0; i<num_iov; i+=4) {
3319 uint8_t *inbuf_ref = NULL;
3320 struct iovec *cur = &iov[i];
3321 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3322 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3323 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3324 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3325 uint16_t req_opcode;
3326 uint32_t req_flags;
3327 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3328 uint32_t new_credits;
3329 struct smbXcli_session *session = NULL;
3330 const DATA_BLOB *signing_key = NULL;
3331 bool was_encrypted = false;
3333 new_credits = conn->smb2.cur_credits;
3334 new_credits += credits;
3335 if (new_credits > UINT16_MAX) {
3336 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3338 conn->smb2.cur_credits += credits;
3340 req = smb2cli_conn_find_pending(conn, mid);
3341 if (req == NULL) {
3342 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3344 state = tevent_req_data(req, struct smbXcli_req_state);
3346 state->smb2.got_async = false;
3348 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3349 if (opcode != req_opcode) {
3350 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3352 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3354 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3355 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3358 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3359 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3360 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3361 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3364 * async interim responses are not signed,
3365 * even if the SMB2_HDR_FLAG_SIGNED flag
3366 * is set.
3368 req_flags |= SMB2_HDR_FLAG_ASYNC;
3369 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3370 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3372 if (state->smb2.notify_async) {
3373 state->smb2.got_async = true;
3374 tevent_req_defer_callback(req, state->ev);
3375 tevent_req_notify_callback(req);
3377 continue;
3380 session = state->session;
3381 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3382 session = last_session;
3384 last_session = session;
3386 if (state->smb2.should_sign) {
3387 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3388 return NT_STATUS_ACCESS_DENIED;
3392 if (flags & SMB2_HDR_FLAG_SIGNED) {
3393 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3395 if (session == NULL) {
3396 struct smbXcli_session *s;
3398 s = state->conn->sessions;
3399 for (; s; s = s->next) {
3400 if (s->smb2->session_id != uid) {
3401 continue;
3404 session = s;
3405 break;
3409 if (session == NULL) {
3410 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3413 last_session = session;
3414 signing_key = &session->smb2_channel.signing_key;
3417 if (opcode == SMB2_OP_SESSSETUP) {
3419 * We prefer the channel signing key, if it is
3420 * already there.
3422 * If we do not have a channel signing key yet,
3423 * we try the main signing key, if it is not
3424 * the final response.
3426 if (signing_key && signing_key->length == 0 &&
3427 !NT_STATUS_IS_OK(status)) {
3428 signing_key = &session->smb2->signing_key;
3431 if (signing_key && signing_key->length == 0) {
3433 * If we do not have a session key to
3434 * verify the signature, we defer the
3435 * signing check to the caller.
3437 * The caller gets NT_STATUS_OK, it
3438 * has to call
3439 * smb2cli_session_set_session_key()
3440 * or
3441 * smb2cli_session_set_channel_key()
3442 * which will check the signature
3443 * with the channel signing key.
3445 signing_key = NULL;
3449 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3450 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3451 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3454 * If the response was encrypted in a SMB2_TRANSFORM
3455 * pdu, which belongs to the correct session,
3456 * we do not need to do signing checks
3458 * It could be the session the response belongs to
3459 * or the session that was used to encrypt the
3460 * SMB2_TRANSFORM request.
3462 if ((session && session->smb2->session_id == uid) ||
3463 (state->smb2.encryption_session_id == uid)) {
3464 signing_key = NULL;
3465 was_encrypted = true;
3469 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3471 * if the server returns NT_STATUS_USER_SESSION_DELETED
3472 * the response is not signed and we should
3473 * propagate the NT_STATUS_USER_SESSION_DELETED
3474 * status to the caller.
3476 state->smb2.signing_skipped = true;
3477 signing_key = NULL;
3480 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3482 * if the server returns
3483 * NT_STATUS_INVALID_PARAMETER
3484 * the response might not be encrypted.
3486 if (state->smb2.should_encrypt && !was_encrypted) {
3487 state->smb2.signing_skipped = true;
3488 signing_key = NULL;
3492 if (state->smb2.should_encrypt && !was_encrypted) {
3493 if (!state->smb2.signing_skipped) {
3494 return NT_STATUS_ACCESS_DENIED;
3498 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3499 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3500 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3502 * if the server returns
3503 * NT_STATUS_NETWORK_NAME_DELETED
3504 * NT_STATUS_FILE_CLOSED
3505 * NT_STATUS_INVALID_PARAMETER
3506 * the response might not be signed
3507 * as this happens before the signing checks.
3509 * If server echos the signature (or all zeros)
3510 * we should report the status from the server
3511 * to the caller.
3513 if (signing_key) {
3514 int cmp;
3516 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3517 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3518 16);
3519 if (cmp == 0) {
3520 state->smb2.signing_skipped = true;
3521 signing_key = NULL;
3524 if (signing_key) {
3525 int cmp;
3526 static const uint8_t zeros[16];
3528 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3529 zeros,
3530 16);
3531 if (cmp == 0) {
3532 state->smb2.signing_skipped = true;
3533 signing_key = NULL;
3538 if (signing_key) {
3539 status = smb2_signing_check_pdu(*signing_key,
3540 state->conn->protocol,
3541 &cur[1], 3);
3542 if (!NT_STATUS_IS_OK(status)) {
3544 * If the signing check fails, we disconnect
3545 * the connection.
3547 return status;
3551 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3552 (session != NULL) && session->disconnect_expired)
3555 * this should be a short term hack
3556 * until the upper layers have implemented
3557 * re-authentication.
3559 return status;
3562 smbXcli_req_unset_pending(req);
3565 * There might be more than one response
3566 * we need to defer the notifications
3568 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3569 defer = false;
3572 if (defer) {
3573 tevent_req_defer_callback(req, state->ev);
3577 * Note: here we use talloc_reference() in a way
3578 * that does not expose it to the caller.
3580 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3581 if (tevent_req_nomem(inbuf_ref, req)) {
3582 continue;
3585 /* copy the related buffers */
3586 state->smb2.recv_iov[0] = cur[1];
3587 state->smb2.recv_iov[1] = cur[2];
3588 state->smb2.recv_iov[2] = cur[3];
3590 tevent_req_done(req);
3593 if (defer) {
3594 return NT_STATUS_RETRY;
3597 return NT_STATUS_OK;
3600 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3601 struct iovec **piov,
3602 const struct smb2cli_req_expected_response *expected,
3603 size_t num_expected)
3605 struct smbXcli_req_state *state =
3606 tevent_req_data(req,
3607 struct smbXcli_req_state);
3608 NTSTATUS status;
3609 size_t body_size;
3610 bool found_status = false;
3611 bool found_size = false;
3612 size_t i;
3614 if (piov != NULL) {
3615 *piov = NULL;
3618 if (state->smb2.got_async) {
3619 return STATUS_PENDING;
3622 if (tevent_req_is_nterror(req, &status)) {
3623 for (i=0; i < num_expected; i++) {
3624 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3625 found_status = true;
3626 break;
3630 if (found_status) {
3631 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3634 return status;
3637 if (num_expected == 0) {
3638 found_status = true;
3639 found_size = true;
3642 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3643 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3645 for (i=0; i < num_expected; i++) {
3646 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3647 continue;
3650 found_status = true;
3651 if (expected[i].body_size == 0) {
3652 found_size = true;
3653 break;
3656 if (expected[i].body_size == body_size) {
3657 found_size = true;
3658 break;
3662 if (!found_status) {
3663 return status;
3666 if (state->smb2.signing_skipped) {
3667 if (num_expected > 0) {
3668 return NT_STATUS_ACCESS_DENIED;
3670 if (!NT_STATUS_IS_ERR(status)) {
3671 return NT_STATUS_ACCESS_DENIED;
3675 if (!found_size) {
3676 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3679 if (piov != NULL) {
3680 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3683 return status;
3686 static const struct {
3687 enum protocol_types proto;
3688 const char *smb1_name;
3689 } smb1cli_prots[] = {
3690 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3691 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3692 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3693 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3694 {PROTOCOL_LANMAN2, "LM1.2X002"},
3695 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3696 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3697 {PROTOCOL_LANMAN2, "Samba"},
3698 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3699 {PROTOCOL_NT1, "NT LM 0.12"},
3700 {PROTOCOL_SMB2_02, "SMB 2.002"},
3701 {PROTOCOL_SMB2_10, "SMB 2.???"},
3704 static const struct {
3705 enum protocol_types proto;
3706 uint16_t smb2_dialect;
3707 } smb2cli_prots[] = {
3708 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3709 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3710 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3711 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3712 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3715 struct smbXcli_negprot_state {
3716 struct smbXcli_conn *conn;
3717 struct tevent_context *ev;
3718 uint32_t timeout_msec;
3719 enum protocol_types min_protocol;
3720 enum protocol_types max_protocol;
3722 struct {
3723 uint8_t fixed[36];
3724 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3725 } smb2;
3728 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3729 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3730 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3731 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3732 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3733 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3734 TALLOC_CTX *frame,
3735 uint8_t *inbuf);
3737 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3738 struct tevent_context *ev,
3739 struct smbXcli_conn *conn,
3740 uint32_t timeout_msec,
3741 enum protocol_types min_protocol,
3742 enum protocol_types max_protocol)
3744 struct tevent_req *req, *subreq;
3745 struct smbXcli_negprot_state *state;
3747 req = tevent_req_create(mem_ctx, &state,
3748 struct smbXcli_negprot_state);
3749 if (req == NULL) {
3750 return NULL;
3752 state->conn = conn;
3753 state->ev = ev;
3754 state->timeout_msec = timeout_msec;
3755 state->min_protocol = min_protocol;
3756 state->max_protocol = max_protocol;
3758 if (min_protocol == PROTOCOL_NONE) {
3759 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3760 return tevent_req_post(req, ev);
3763 if (max_protocol == PROTOCOL_NONE) {
3764 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3765 return tevent_req_post(req, ev);
3768 if (min_protocol > max_protocol) {
3769 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3770 return tevent_req_post(req, ev);
3773 if ((min_protocol < PROTOCOL_SMB2_02) &&
3774 (max_protocol < PROTOCOL_SMB2_02)) {
3776 * SMB1 only...
3778 conn->dispatch_incoming = smb1cli_conn_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_smb1_done, req);
3785 return req;
3788 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3789 (max_protocol >= PROTOCOL_SMB2_02)) {
3791 * SMB2 only...
3793 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3796 * As we're starting with an SMB2 negprot, emulate Windows
3797 * and ask for 31 credits in the initial SMB2 negprot.
3798 * If we don't and leave requested credits at
3799 * zero, MacOSX servers return zero credits on
3800 * the negprot reply and we fail to connect.
3802 smb2cli_conn_set_max_credits(conn,
3803 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK);
3805 subreq = smbXcli_negprot_smb2_subreq(state);
3806 if (tevent_req_nomem(subreq, req)) {
3807 return tevent_req_post(req, ev);
3809 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3810 return req;
3814 * We send an SMB1 negprot with the SMB2 dialects
3815 * and expect a SMB1 or a SMB2 response.
3817 * smbXcli_negprot_dispatch_incoming() will fix the
3818 * callback to match protocol of the response.
3820 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3822 subreq = smbXcli_negprot_smb1_subreq(state);
3823 if (tevent_req_nomem(subreq, req)) {
3824 return tevent_req_post(req, ev);
3826 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3827 return req;
3830 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3832 struct tevent_req *req =
3833 tevent_req_callback_data(subreq,
3834 struct tevent_req);
3835 NTSTATUS status;
3838 * we just want the low level error
3840 status = tevent_req_simple_recv_ntstatus(subreq);
3841 TALLOC_FREE(subreq);
3842 if (tevent_req_nterror(req, status)) {
3843 return;
3846 /* this should never happen */
3847 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3850 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3852 size_t i;
3853 DATA_BLOB bytes = data_blob_null;
3854 uint8_t flags;
3855 uint16_t flags2;
3857 /* setup the protocol strings */
3858 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3859 uint8_t c = 2;
3860 bool ok;
3862 if (smb1cli_prots[i].proto < state->min_protocol) {
3863 continue;
3866 if (smb1cli_prots[i].proto > state->max_protocol) {
3867 continue;
3870 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3871 if (!ok) {
3872 return NULL;
3876 * We now it is already ascii and
3877 * we want NULL termination.
3879 ok = data_blob_append(state, &bytes,
3880 smb1cli_prots[i].smb1_name,
3881 strlen(smb1cli_prots[i].smb1_name)+1);
3882 if (!ok) {
3883 return NULL;
3887 smb1cli_req_flags(state->max_protocol,
3888 state->conn->smb1.client.capabilities,
3889 SMBnegprot,
3890 0, 0, &flags,
3891 0, 0, &flags2);
3893 return smb1cli_req_send(state, state->ev, state->conn,
3894 SMBnegprot,
3895 flags, ~flags,
3896 flags2, ~flags2,
3897 state->timeout_msec,
3898 0xFFFE, 0, NULL, /* pid, tid, session */
3899 0, NULL, /* wct, vwv */
3900 bytes.length, bytes.data);
3903 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3905 struct tevent_req *req =
3906 tevent_req_callback_data(subreq,
3907 struct tevent_req);
3908 struct smbXcli_negprot_state *state =
3909 tevent_req_data(req,
3910 struct smbXcli_negprot_state);
3911 struct smbXcli_conn *conn = state->conn;
3912 struct iovec *recv_iov = NULL;
3913 uint8_t *inhdr;
3914 uint8_t wct;
3915 uint16_t *vwv;
3916 uint32_t num_bytes;
3917 uint8_t *bytes;
3918 NTSTATUS status;
3919 uint16_t protnum;
3920 size_t i;
3921 size_t num_prots = 0;
3922 uint8_t flags;
3923 uint32_t client_capabilities = conn->smb1.client.capabilities;
3924 uint32_t both_capabilities;
3925 uint32_t server_capabilities = 0;
3926 uint32_t capabilities;
3927 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3928 uint32_t server_max_xmit = 0;
3929 uint32_t max_xmit;
3930 uint32_t server_max_mux = 0;
3931 uint16_t server_security_mode = 0;
3932 uint32_t server_session_key = 0;
3933 bool server_readbraw = false;
3934 bool server_writebraw = false;
3935 bool server_lockread = false;
3936 bool server_writeunlock = false;
3937 struct GUID server_guid = GUID_zero();
3938 DATA_BLOB server_gss_blob = data_blob_null;
3939 uint8_t server_challenge[8];
3940 char *server_workgroup = NULL;
3941 char *server_name = NULL;
3942 int server_time_zone = 0;
3943 NTTIME server_system_time = 0;
3944 static const struct smb1cli_req_expected_response expected[] = {
3946 .status = NT_STATUS_OK,
3947 .wct = 0x11, /* NT1 */
3950 .status = NT_STATUS_OK,
3951 .wct = 0x0D, /* LM */
3954 .status = NT_STATUS_OK,
3955 .wct = 0x01, /* CORE */
3959 ZERO_STRUCT(server_challenge);
3961 status = smb1cli_req_recv(subreq, state,
3962 &recv_iov,
3963 &inhdr,
3964 &wct,
3965 &vwv,
3966 NULL, /* pvwv_offset */
3967 &num_bytes,
3968 &bytes,
3969 NULL, /* pbytes_offset */
3970 NULL, /* pinbuf */
3971 expected, ARRAY_SIZE(expected));
3972 TALLOC_FREE(subreq);
3973 if (tevent_req_nterror(req, status)) {
3974 return;
3977 flags = CVAL(inhdr, HDR_FLG);
3979 protnum = SVAL(vwv, 0);
3981 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3982 if (smb1cli_prots[i].proto < state->min_protocol) {
3983 continue;
3986 if (smb1cli_prots[i].proto > state->max_protocol) {
3987 continue;
3990 if (protnum != num_prots) {
3991 num_prots++;
3992 continue;
3995 conn->protocol = smb1cli_prots[i].proto;
3996 break;
3999 if (conn->protocol == PROTOCOL_NONE) {
4000 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4001 return;
4004 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4005 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4006 "and the selected protocol level doesn't support it.\n"));
4007 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4008 return;
4011 if (flags & FLAG_SUPPORT_LOCKREAD) {
4012 server_lockread = true;
4013 server_writeunlock = true;
4016 if (conn->protocol >= PROTOCOL_NT1) {
4017 const char *client_signing = NULL;
4018 bool server_mandatory = false;
4019 bool server_allowed = false;
4020 const char *server_signing = NULL;
4021 bool ok;
4022 uint8_t key_len;
4024 if (wct != 0x11) {
4025 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4026 return;
4029 /* NT protocol */
4030 server_security_mode = CVAL(vwv + 1, 0);
4031 server_max_mux = SVAL(vwv + 1, 1);
4032 server_max_xmit = IVAL(vwv + 3, 1);
4033 server_session_key = IVAL(vwv + 7, 1);
4034 server_time_zone = SVALS(vwv + 15, 1);
4035 server_time_zone *= 60;
4036 /* this time arrives in real GMT */
4037 server_system_time = BVAL(vwv + 11, 1);
4038 server_capabilities = IVAL(vwv + 9, 1);
4040 key_len = CVAL(vwv + 16, 1);
4042 if (server_capabilities & CAP_RAW_MODE) {
4043 server_readbraw = true;
4044 server_writebraw = true;
4046 if (server_capabilities & CAP_LOCK_AND_READ) {
4047 server_lockread = true;
4050 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4051 DATA_BLOB blob1, blob2;
4053 if (num_bytes < 16) {
4054 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4055 return;
4058 blob1 = data_blob_const(bytes, 16);
4059 status = GUID_from_data_blob(&blob1, &server_guid);
4060 if (tevent_req_nterror(req, status)) {
4061 return;
4064 blob1 = data_blob_const(bytes+16, num_bytes-16);
4065 blob2 = data_blob_dup_talloc(state, blob1);
4066 if (blob1.length > 0 &&
4067 tevent_req_nomem(blob2.data, req)) {
4068 return;
4070 server_gss_blob = blob2;
4071 } else {
4072 DATA_BLOB blob1, blob2;
4074 if (num_bytes < key_len) {
4075 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4076 return;
4079 if (key_len != 0 && key_len != 8) {
4080 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4081 return;
4084 if (key_len == 8) {
4085 memcpy(server_challenge, bytes, 8);
4088 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4089 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4090 if (blob1.length > 0) {
4091 size_t len;
4093 len = utf16_len_n(blob1.data,
4094 blob1.length);
4095 blob1.length = len;
4097 ok = convert_string_talloc(state,
4098 CH_UTF16LE,
4099 CH_UNIX,
4100 blob1.data,
4101 blob1.length,
4102 &server_workgroup,
4103 &len);
4104 if (!ok) {
4105 status = map_nt_error_from_unix_common(errno);
4106 tevent_req_nterror(req, status);
4107 return;
4111 blob2.data += blob1.length;
4112 blob2.length -= blob1.length;
4113 if (blob2.length > 0) {
4114 size_t len;
4116 len = utf16_len_n(blob1.data,
4117 blob1.length);
4118 blob1.length = len;
4120 ok = convert_string_talloc(state,
4121 CH_UTF16LE,
4122 CH_UNIX,
4123 blob2.data,
4124 blob2.length,
4125 &server_name,
4126 &len);
4127 if (!ok) {
4128 status = map_nt_error_from_unix_common(errno);
4129 tevent_req_nterror(req, status);
4130 return;
4135 client_signing = "disabled";
4136 if (conn->allow_signing) {
4137 client_signing = "allowed";
4139 if (conn->mandatory_signing) {
4140 client_signing = "required";
4143 server_signing = "not supported";
4144 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4145 server_signing = "supported";
4146 server_allowed = true;
4147 } else if (conn->mandatory_signing) {
4149 * We have mandatory signing as client
4150 * lets assume the server will look at our
4151 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4152 * flag in the session setup
4154 server_signing = "not announced";
4155 server_allowed = true;
4157 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4158 server_signing = "required";
4159 server_mandatory = true;
4162 ok = smb_signing_set_negotiated(conn->smb1.signing,
4163 server_allowed,
4164 server_mandatory);
4165 if (!ok) {
4166 DEBUG(1,("cli_negprot: SMB signing is required, "
4167 "but client[%s] and server[%s] mismatch\n",
4168 client_signing, server_signing));
4169 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4170 return;
4173 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4174 DATA_BLOB blob1;
4175 uint8_t key_len;
4176 time_t t;
4178 if (wct != 0x0D) {
4179 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4180 return;
4183 server_security_mode = SVAL(vwv + 1, 0);
4184 server_max_xmit = SVAL(vwv + 2, 0);
4185 server_max_mux = SVAL(vwv + 3, 0);
4186 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4187 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4188 server_session_key = IVAL(vwv + 6, 0);
4189 server_time_zone = SVALS(vwv + 10, 0);
4190 server_time_zone *= 60;
4191 /* this time is converted to GMT by make_unix_date */
4192 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4193 unix_to_nt_time(&server_system_time, t);
4194 key_len = SVAL(vwv + 11, 0);
4196 if (num_bytes < key_len) {
4197 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4198 return;
4201 if (key_len != 0 && key_len != 8) {
4202 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4203 return;
4206 if (key_len == 8) {
4207 memcpy(server_challenge, bytes, 8);
4210 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4211 if (blob1.length > 0) {
4212 size_t len;
4213 bool ok;
4215 len = utf16_len_n(blob1.data,
4216 blob1.length);
4217 blob1.length = len;
4219 ok = convert_string_talloc(state,
4220 CH_DOS,
4221 CH_UNIX,
4222 blob1.data,
4223 blob1.length,
4224 &server_workgroup,
4225 &len);
4226 if (!ok) {
4227 status = map_nt_error_from_unix_common(errno);
4228 tevent_req_nterror(req, status);
4229 return;
4233 } else {
4234 /* the old core protocol */
4235 server_time_zone = get_time_zone(time(NULL));
4236 server_max_xmit = 1024;
4237 server_max_mux = 1;
4240 if (server_max_xmit < 1024) {
4241 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4242 return;
4245 if (server_max_mux < 1) {
4246 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4247 return;
4251 * Now calculate the negotiated capabilities
4252 * based on the mask for:
4253 * - client only flags
4254 * - flags used in both directions
4255 * - server only flags
4257 both_capabilities = client_capabilities & server_capabilities;
4258 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4259 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4260 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4262 max_xmit = MIN(client_max_xmit, server_max_xmit);
4264 conn->smb1.server.capabilities = server_capabilities;
4265 conn->smb1.capabilities = capabilities;
4267 conn->smb1.server.max_xmit = server_max_xmit;
4268 conn->smb1.max_xmit = max_xmit;
4270 conn->smb1.server.max_mux = server_max_mux;
4272 conn->smb1.server.security_mode = server_security_mode;
4274 conn->smb1.server.readbraw = server_readbraw;
4275 conn->smb1.server.writebraw = server_writebraw;
4276 conn->smb1.server.lockread = server_lockread;
4277 conn->smb1.server.writeunlock = server_writeunlock;
4279 conn->smb1.server.session_key = server_session_key;
4281 talloc_steal(conn, server_gss_blob.data);
4282 conn->smb1.server.gss_blob = server_gss_blob;
4283 conn->smb1.server.guid = server_guid;
4284 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4285 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4286 conn->smb1.server.name = talloc_move(conn, &server_name);
4288 conn->smb1.server.time_zone = server_time_zone;
4289 conn->smb1.server.system_time = server_system_time;
4291 tevent_req_done(req);
4294 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4296 size_t i;
4297 uint8_t *buf;
4298 uint16_t dialect_count = 0;
4300 buf = state->smb2.dyn;
4301 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4302 if (smb2cli_prots[i].proto < state->min_protocol) {
4303 continue;
4306 if (smb2cli_prots[i].proto > state->max_protocol) {
4307 continue;
4310 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4311 dialect_count++;
4314 buf = state->smb2.fixed;
4315 SSVAL(buf, 0, 36);
4316 SSVAL(buf, 2, dialect_count);
4317 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4318 SSVAL(buf, 6, 0); /* Reserved */
4319 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4320 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4321 } else {
4322 SIVAL(buf, 8, 0); /* Capabilities */
4324 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4325 NTSTATUS status;
4326 DATA_BLOB blob;
4328 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4329 state, &blob);
4330 if (!NT_STATUS_IS_OK(status)) {
4331 return NULL;
4333 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4334 } else {
4335 memset(buf+12, 0, 16); /* ClientGuid */
4337 SBVAL(buf, 28, 0); /* ClientStartTime */
4339 return smb2cli_req_send(state, state->ev,
4340 state->conn, SMB2_OP_NEGPROT,
4341 0, 0, /* flags */
4342 state->timeout_msec,
4343 NULL, NULL, /* tcon, session */
4344 state->smb2.fixed, sizeof(state->smb2.fixed),
4345 state->smb2.dyn, dialect_count*2,
4346 UINT16_MAX); /* max_dyn_len */
4349 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4351 struct tevent_req *req =
4352 tevent_req_callback_data(subreq,
4353 struct tevent_req);
4354 struct smbXcli_negprot_state *state =
4355 tevent_req_data(req,
4356 struct smbXcli_negprot_state);
4357 struct smbXcli_conn *conn = state->conn;
4358 size_t security_offset, security_length;
4359 DATA_BLOB blob;
4360 NTSTATUS status;
4361 struct iovec *iov;
4362 uint8_t *body;
4363 size_t i;
4364 uint16_t dialect_revision;
4365 static const struct smb2cli_req_expected_response expected[] = {
4367 .status = NT_STATUS_OK,
4368 .body_size = 0x41
4372 status = smb2cli_req_recv(subreq, state, &iov,
4373 expected, ARRAY_SIZE(expected));
4374 TALLOC_FREE(subreq);
4375 if (tevent_req_nterror(req, status)) {
4376 return;
4379 body = (uint8_t *)iov[1].iov_base;
4381 dialect_revision = SVAL(body, 4);
4383 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4384 if (smb2cli_prots[i].proto < state->min_protocol) {
4385 continue;
4388 if (smb2cli_prots[i].proto > state->max_protocol) {
4389 continue;
4392 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4393 continue;
4396 conn->protocol = smb2cli_prots[i].proto;
4397 break;
4400 if (conn->protocol == PROTOCOL_NONE) {
4401 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4402 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4403 return;
4406 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4407 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4408 return;
4411 /* make sure we do not loop forever */
4412 state->min_protocol = PROTOCOL_SMB2_02;
4415 * send a SMB2 negprot, in order to negotiate
4416 * the SMB2 dialect.
4418 subreq = smbXcli_negprot_smb2_subreq(state);
4419 if (tevent_req_nomem(subreq, req)) {
4420 return;
4422 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4423 return;
4426 conn->smb2.server.security_mode = SVAL(body, 2);
4428 blob = data_blob_const(body + 8, 16);
4429 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4430 if (tevent_req_nterror(req, status)) {
4431 return;
4434 conn->smb2.server.capabilities = IVAL(body, 24);
4435 conn->smb2.server.max_trans_size= IVAL(body, 28);
4436 conn->smb2.server.max_read_size = IVAL(body, 32);
4437 conn->smb2.server.max_write_size= IVAL(body, 36);
4438 conn->smb2.server.system_time = BVAL(body, 40);
4439 conn->smb2.server.start_time = BVAL(body, 48);
4441 security_offset = SVAL(body, 56);
4442 security_length = SVAL(body, 58);
4444 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4445 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4446 return;
4449 if (security_length > iov[2].iov_len) {
4450 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4451 return;
4454 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4455 iov[2].iov_base,
4456 security_length);
4457 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4458 return;
4461 tevent_req_done(req);
4464 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4465 TALLOC_CTX *tmp_mem,
4466 uint8_t *inbuf)
4468 size_t num_pending = talloc_array_length(conn->pending);
4469 struct tevent_req *subreq;
4470 struct smbXcli_req_state *substate;
4471 struct tevent_req *req;
4472 uint32_t protocol_magic;
4473 size_t inbuf_len = smb_len_nbt(inbuf);
4475 if (num_pending != 1) {
4476 return NT_STATUS_INTERNAL_ERROR;
4479 if (inbuf_len < 4) {
4480 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4483 subreq = conn->pending[0];
4484 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4485 req = tevent_req_callback_data(subreq, struct tevent_req);
4487 protocol_magic = IVAL(inbuf, 4);
4489 switch (protocol_magic) {
4490 case SMB_MAGIC:
4491 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4492 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4493 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4495 case SMB2_MAGIC:
4496 if (substate->smb2.recv_iov == NULL) {
4498 * For the SMB1 negprot we have move it.
4500 substate->smb2.recv_iov = substate->smb1.recv_iov;
4501 substate->smb1.recv_iov = NULL;
4505 * we got an SMB2 answer, which consumed sequence number 0
4506 * so we need to use 1 as the next one.
4508 * we also need to set the current credits to 0
4509 * as we consumed the initial one. The SMB2 answer
4510 * hopefully grant us a new credit.
4512 conn->smb2.mid = 1;
4513 conn->smb2.cur_credits = 0;
4514 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4515 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4516 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4519 DEBUG(10, ("Got non-SMB PDU\n"));
4520 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4523 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4525 return tevent_req_simple_recv_ntstatus(req);
4528 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4529 uint32_t timeout_msec,
4530 enum protocol_types min_protocol,
4531 enum protocol_types max_protocol)
4533 TALLOC_CTX *frame = talloc_stackframe();
4534 struct tevent_context *ev;
4535 struct tevent_req *req;
4536 NTSTATUS status = NT_STATUS_NO_MEMORY;
4537 bool ok;
4539 if (smbXcli_conn_has_async_calls(conn)) {
4541 * Can't use sync call while an async call is in flight
4543 status = NT_STATUS_INVALID_PARAMETER_MIX;
4544 goto fail;
4546 ev = samba_tevent_context_init(frame);
4547 if (ev == NULL) {
4548 goto fail;
4550 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4551 min_protocol, max_protocol);
4552 if (req == NULL) {
4553 goto fail;
4555 ok = tevent_req_poll(req, ev);
4556 if (!ok) {
4557 status = map_nt_error_from_unix_common(errno);
4558 goto fail;
4560 status = smbXcli_negprot_recv(req);
4561 fail:
4562 TALLOC_FREE(frame);
4563 return status;
4566 static int smbXcli_session_destructor(struct smbXcli_session *session)
4568 if (session->conn == NULL) {
4569 return 0;
4572 DLIST_REMOVE(session->conn->sessions, session);
4573 return 0;
4576 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4577 struct smbXcli_conn *conn)
4579 struct smbXcli_session *session;
4581 session = talloc_zero(mem_ctx, struct smbXcli_session);
4582 if (session == NULL) {
4583 return NULL;
4585 session->smb2 = talloc_zero(session, struct smb2cli_session);
4586 if (session->smb2 == NULL) {
4587 talloc_free(session);
4588 return NULL;
4590 talloc_set_destructor(session, smbXcli_session_destructor);
4592 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4593 session->conn = conn;
4595 return session;
4598 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4599 struct smbXcli_session *src)
4601 struct smbXcli_session *session;
4603 session = talloc_zero(mem_ctx, struct smbXcli_session);
4604 if (session == NULL) {
4605 return NULL;
4607 session->smb2 = talloc_zero(session, struct smb2cli_session);
4608 if (session->smb2 == NULL) {
4609 talloc_free(session);
4610 return NULL;
4613 session->conn = src->conn;
4614 *session->smb2 = *src->smb2;
4615 session->smb2_channel = src->smb2_channel;
4616 session->disconnect_expired = src->disconnect_expired;
4618 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4619 talloc_set_destructor(session, smbXcli_session_destructor);
4621 return session;
4625 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4626 TALLOC_CTX *mem_ctx,
4627 DATA_BLOB *key)
4629 const DATA_BLOB *application_key;
4631 *key = data_blob_null;
4633 if (session->conn == NULL) {
4634 return NT_STATUS_NO_USER_SESSION_KEY;
4637 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4638 application_key = &session->smb2->application_key;
4639 } else {
4640 application_key = &session->smb1.application_key;
4643 if (application_key->length == 0) {
4644 return NT_STATUS_NO_USER_SESSION_KEY;
4647 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4648 if (key->data == NULL) {
4649 return NT_STATUS_NO_MEMORY;
4652 return NT_STATUS_OK;
4655 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4657 session->disconnect_expired = true;
4660 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4662 return session->smb1.session_id;
4665 void smb1cli_session_set_id(struct smbXcli_session *session,
4666 uint16_t session_id)
4668 session->smb1.session_id = session_id;
4671 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4672 const DATA_BLOB _session_key)
4674 struct smbXcli_conn *conn = session->conn;
4675 uint8_t session_key[16];
4677 if (conn == NULL) {
4678 return NT_STATUS_INVALID_PARAMETER_MIX;
4681 if (session->smb1.application_key.length != 0) {
4683 * TODO: do not allow this...
4685 * return NT_STATUS_INVALID_PARAMETER_MIX;
4687 data_blob_clear_free(&session->smb1.application_key);
4688 session->smb1.protected_key = false;
4691 if (_session_key.length == 0) {
4692 return NT_STATUS_OK;
4695 ZERO_STRUCT(session_key);
4696 memcpy(session_key, _session_key.data,
4697 MIN(_session_key.length, sizeof(session_key)));
4699 session->smb1.application_key = data_blob_talloc(session,
4700 session_key,
4701 sizeof(session_key));
4702 ZERO_STRUCT(session_key);
4703 if (session->smb1.application_key.data == NULL) {
4704 return NT_STATUS_NO_MEMORY;
4707 session->smb1.protected_key = false;
4709 return NT_STATUS_OK;
4712 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4714 if (session->smb1.protected_key) {
4715 /* already protected */
4716 return NT_STATUS_OK;
4719 if (session->smb1.application_key.length != 16) {
4720 return NT_STATUS_INVALID_PARAMETER_MIX;
4723 smb_key_derivation(session->smb1.application_key.data,
4724 session->smb1.application_key.length,
4725 session->smb1.application_key.data);
4727 session->smb1.protected_key = true;
4729 return NT_STATUS_OK;
4732 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4734 struct smbXcli_conn *conn = session->conn;
4735 uint8_t security_mode = 0;
4737 if (conn == NULL) {
4738 return security_mode;
4741 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4742 if (conn->mandatory_signing) {
4743 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4746 return security_mode;
4749 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4751 return session->smb2->session_id;
4754 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4756 return session->smb2->session_flags;
4759 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4760 uint64_t session_id,
4761 uint16_t session_flags)
4763 session->smb2->session_id = session_id;
4764 session->smb2->session_flags = session_flags;
4767 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4769 session->smb2->channel_sequence += 1;
4772 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4773 const DATA_BLOB _session_key,
4774 const struct iovec *recv_iov)
4776 struct smbXcli_conn *conn = session->conn;
4777 uint16_t no_sign_flags;
4778 uint8_t session_key[16];
4779 bool check_signature = true;
4780 uint32_t hdr_flags;
4781 NTSTATUS status;
4783 if (conn == NULL) {
4784 return NT_STATUS_INVALID_PARAMETER_MIX;
4787 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
4788 return NT_STATUS_INVALID_PARAMETER_MIX;
4791 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4793 if (session->smb2->session_flags & no_sign_flags) {
4794 session->smb2->should_sign = false;
4795 return NT_STATUS_OK;
4798 if (session->smb2->signing_key.length != 0) {
4799 return NT_STATUS_INVALID_PARAMETER_MIX;
4802 ZERO_STRUCT(session_key);
4803 memcpy(session_key, _session_key.data,
4804 MIN(_session_key.length, sizeof(session_key)));
4806 session->smb2->signing_key = data_blob_talloc(session,
4807 session_key,
4808 sizeof(session_key));
4809 if (session->smb2->signing_key.data == NULL) {
4810 ZERO_STRUCT(session_key);
4811 return NT_STATUS_NO_MEMORY;
4814 if (conn->protocol >= PROTOCOL_SMB2_24) {
4815 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4816 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4818 smb2_key_derivation(session_key, sizeof(session_key),
4819 label.data, label.length,
4820 context.data, context.length,
4821 session->smb2->signing_key.data);
4824 session->smb2->encryption_key = data_blob_dup_talloc(session,
4825 session->smb2->signing_key);
4826 if (session->smb2->encryption_key.data == NULL) {
4827 ZERO_STRUCT(session_key);
4828 return NT_STATUS_NO_MEMORY;
4831 if (conn->protocol >= PROTOCOL_SMB2_24) {
4832 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4833 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4835 smb2_key_derivation(session_key, sizeof(session_key),
4836 label.data, label.length,
4837 context.data, context.length,
4838 session->smb2->encryption_key.data);
4841 session->smb2->decryption_key = data_blob_dup_talloc(session,
4842 session->smb2->signing_key);
4843 if (session->smb2->decryption_key.data == NULL) {
4844 ZERO_STRUCT(session_key);
4845 return NT_STATUS_NO_MEMORY;
4848 if (conn->protocol >= PROTOCOL_SMB2_24) {
4849 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4850 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4852 smb2_key_derivation(session_key, sizeof(session_key),
4853 label.data, label.length,
4854 context.data, context.length,
4855 session->smb2->decryption_key.data);
4858 session->smb2->application_key = data_blob_dup_talloc(session,
4859 session->smb2->signing_key);
4860 if (session->smb2->application_key.data == NULL) {
4861 ZERO_STRUCT(session_key);
4862 return NT_STATUS_NO_MEMORY;
4865 if (conn->protocol >= PROTOCOL_SMB2_24) {
4866 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4867 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4869 smb2_key_derivation(session_key, sizeof(session_key),
4870 label.data, label.length,
4871 context.data, context.length,
4872 session->smb2->application_key.data);
4874 ZERO_STRUCT(session_key);
4876 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4877 session->smb2->signing_key);
4878 if (session->smb2_channel.signing_key.data == NULL) {
4879 return NT_STATUS_NO_MEMORY;
4882 check_signature = conn->mandatory_signing;
4884 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
4885 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
4887 * Sadly some vendors don't sign the
4888 * final SMB2 session setup response
4890 * At least Windows and Samba are always doing this
4891 * if there's a session key available.
4893 * We only check the signature if it's mandatory
4894 * or SMB2_HDR_FLAG_SIGNED is provided.
4896 check_signature = true;
4899 if (check_signature) {
4900 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4901 session->conn->protocol,
4902 recv_iov, 3);
4903 if (!NT_STATUS_IS_OK(status)) {
4904 return status;
4908 session->smb2->should_sign = false;
4909 session->smb2->should_encrypt = false;
4911 if (conn->desire_signing) {
4912 session->smb2->should_sign = true;
4915 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4916 session->smb2->should_sign = true;
4919 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4920 session->smb2->should_encrypt = true;
4923 if (conn->protocol < PROTOCOL_SMB2_24) {
4924 session->smb2->should_encrypt = false;
4927 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4928 session->smb2->should_encrypt = false;
4931 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4932 sizeof(session->smb2->nonce_high));
4933 session->smb2->nonce_low = 1;
4935 return NT_STATUS_OK;
4938 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4939 struct smbXcli_session *session1,
4940 struct smbXcli_conn *conn,
4941 struct smbXcli_session **_session2)
4943 struct smbXcli_session *session2;
4945 if (session1->smb2->signing_key.length == 0) {
4946 return NT_STATUS_INVALID_PARAMETER_MIX;
4949 if (conn == NULL) {
4950 return NT_STATUS_INVALID_PARAMETER_MIX;
4953 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4954 if (session2 == NULL) {
4955 return NT_STATUS_NO_MEMORY;
4957 session2->smb2 = talloc_reference(session2, session1->smb2);
4958 if (session2->smb2 == NULL) {
4959 talloc_free(session2);
4960 return NT_STATUS_NO_MEMORY;
4963 talloc_set_destructor(session2, smbXcli_session_destructor);
4964 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4965 session2->conn = conn;
4967 *_session2 = session2;
4968 return NT_STATUS_OK;
4971 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4972 const DATA_BLOB _channel_key,
4973 const struct iovec *recv_iov)
4975 struct smbXcli_conn *conn = session->conn;
4976 uint8_t channel_key[16];
4977 NTSTATUS status;
4979 if (conn == NULL) {
4980 return NT_STATUS_INVALID_PARAMETER_MIX;
4983 if (session->smb2_channel.signing_key.length != 0) {
4984 return NT_STATUS_INVALID_PARAMETER_MIX;
4987 ZERO_STRUCT(channel_key);
4988 memcpy(channel_key, _channel_key.data,
4989 MIN(_channel_key.length, sizeof(channel_key)));
4991 session->smb2_channel.signing_key = data_blob_talloc(session,
4992 channel_key,
4993 sizeof(channel_key));
4994 if (session->smb2_channel.signing_key.data == NULL) {
4995 ZERO_STRUCT(channel_key);
4996 return NT_STATUS_NO_MEMORY;
4999 if (conn->protocol >= PROTOCOL_SMB2_24) {
5000 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
5001 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
5003 smb2_key_derivation(channel_key, sizeof(channel_key),
5004 label.data, label.length,
5005 context.data, context.length,
5006 session->smb2_channel.signing_key.data);
5008 ZERO_STRUCT(channel_key);
5010 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5011 session->conn->protocol,
5012 recv_iov, 3);
5013 if (!NT_STATUS_IS_OK(status)) {
5014 return status;
5017 return NT_STATUS_OK;
5020 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
5022 if (session->smb2->should_encrypt) {
5023 return NT_STATUS_OK;
5026 if (session->conn->protocol < PROTOCOL_SMB2_24) {
5027 return NT_STATUS_NOT_SUPPORTED;
5030 if (!(session->conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
5031 return NT_STATUS_NOT_SUPPORTED;
5034 if (session->smb2->signing_key.data == NULL) {
5035 return NT_STATUS_NOT_SUPPORTED;
5037 session->smb2->should_encrypt = true;
5038 return NT_STATUS_OK;
5041 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
5043 struct smbXcli_tcon *tcon;
5045 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
5046 if (tcon == NULL) {
5047 return NULL;
5050 return tcon;
5053 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
5054 uint32_t fs_attributes)
5056 tcon->fs_attributes = fs_attributes;
5059 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
5061 return tcon->fs_attributes;
5064 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
5066 if (tcon == NULL) {
5067 return false;
5070 if (tcon->is_smb1) {
5071 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
5072 return true;
5075 return false;
5078 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
5079 return true;
5082 return false;
5085 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
5087 return tcon->smb1.tcon_id;
5090 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
5092 tcon->is_smb1 = true;
5093 tcon->smb1.tcon_id = tcon_id;
5096 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5097 uint16_t tcon_id,
5098 uint16_t optional_support,
5099 uint32_t maximal_access,
5100 uint32_t guest_maximal_access,
5101 const char *service,
5102 const char *fs_type)
5104 tcon->is_smb1 = true;
5105 tcon->fs_attributes = 0;
5106 tcon->smb1.tcon_id = tcon_id;
5107 tcon->smb1.optional_support = optional_support;
5108 tcon->smb1.maximal_access = maximal_access;
5109 tcon->smb1.guest_maximal_access = guest_maximal_access;
5111 TALLOC_FREE(tcon->smb1.service);
5112 tcon->smb1.service = talloc_strdup(tcon, service);
5113 if (service != NULL && tcon->smb1.service == NULL) {
5114 return false;
5117 TALLOC_FREE(tcon->smb1.fs_type);
5118 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5119 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5120 return false;
5123 return true;
5126 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5128 return tcon->smb2.tcon_id;
5131 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5133 return tcon->smb2.capabilities;
5136 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5137 struct smbXcli_session *session,
5138 uint32_t tcon_id,
5139 uint8_t type,
5140 uint32_t flags,
5141 uint32_t capabilities,
5142 uint32_t maximal_access)
5144 tcon->is_smb1 = false;
5145 tcon->fs_attributes = 0;
5146 tcon->smb2.tcon_id = tcon_id;
5147 tcon->smb2.type = type;
5148 tcon->smb2.flags = flags;
5149 tcon->smb2.capabilities = capabilities;
5150 tcon->smb2.maximal_access = maximal_access;
5152 tcon->smb2.should_encrypt = false;
5154 if (session == NULL) {
5155 return;
5158 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5160 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5161 tcon->smb2.should_encrypt = true;