libcli/smb: add smbXcli_tcon_is_dfs_share()
[Samba.git] / libcli / smb / smbXcli_base.c
blob616e373330f07a870bdf1b8f2c86a39ba8711872
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"
35 struct smbXcli_conn;
36 struct smbXcli_req;
37 struct smbXcli_session;
38 struct smbXcli_tcon;
40 struct smbXcli_conn {
41 int read_fd;
42 int write_fd;
43 struct sockaddr_storage local_ss;
44 struct sockaddr_storage remote_ss;
45 const char *remote_name;
47 struct tevent_queue *outgoing;
48 struct tevent_req **pending;
49 struct tevent_req *read_smb_req;
51 enum protocol_types protocol;
52 bool allow_signing;
53 bool desire_signing;
54 bool mandatory_signing;
57 * The incoming dispatch function should return:
58 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
59 * - NT_STATUS_OK, if no more processing is desired, e.g.
60 * the dispatch function called
61 * tevent_req_done().
62 * - All other return values disconnect the connection.
64 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
65 TALLOC_CTX *tmp_mem,
66 uint8_t *inbuf);
68 struct {
69 struct {
70 uint32_t capabilities;
71 uint32_t max_xmit;
72 } client;
74 struct {
75 uint32_t capabilities;
76 uint32_t max_xmit;
77 uint16_t max_mux;
78 uint16_t security_mode;
79 bool readbraw;
80 bool writebraw;
81 bool lockread;
82 bool writeunlock;
83 uint32_t session_key;
84 struct GUID guid;
85 DATA_BLOB gss_blob;
86 uint8_t challenge[8];
87 const char *workgroup;
88 const char *name;
89 int time_zone;
90 NTTIME system_time;
91 } server;
93 uint32_t capabilities;
94 uint32_t max_xmit;
96 uint16_t mid;
98 struct smb_signing_state *signing;
99 struct smb_trans_enc_state *trans_enc;
101 struct tevent_req *read_braw_req;
102 } smb1;
104 struct {
105 struct {
106 uint32_t capabilities;
107 uint16_t security_mode;
108 struct GUID guid;
109 } client;
111 struct {
112 uint32_t capabilities;
113 uint16_t security_mode;
114 struct GUID guid;
115 uint32_t max_trans_size;
116 uint32_t max_read_size;
117 uint32_t max_write_size;
118 NTTIME system_time;
119 NTTIME start_time;
120 DATA_BLOB gss_blob;
121 } server;
123 uint64_t mid;
124 uint16_t cur_credits;
125 uint16_t max_credits;
126 } smb2;
128 struct smbXcli_session *sessions;
131 struct smb2cli_session {
132 uint64_t session_id;
133 uint16_t session_flags;
134 DATA_BLOB application_key;
135 DATA_BLOB signing_key;
136 bool should_sign;
137 bool should_encrypt;
138 DATA_BLOB encryption_key;
139 DATA_BLOB decryption_key;
140 uint64_t nonce_high;
141 uint64_t nonce_low;
142 uint16_t channel_sequence;
145 struct smbXcli_session {
146 struct smbXcli_session *prev, *next;
147 struct smbXcli_conn *conn;
149 struct {
150 uint16_t session_id;
151 DATA_BLOB application_key;
152 bool protected_key;
153 } smb1;
155 struct smb2cli_session *smb2;
157 struct {
158 DATA_BLOB signing_key;
159 } smb2_channel;
162 * this should be a short term hack
163 * until the upper layers have implemented
164 * re-authentication.
166 bool disconnect_expired;
169 struct smbXcli_tcon {
170 bool is_smb1;
172 struct {
173 uint16_t tcon_id;
174 uint16_t optional_support;
175 uint32_t maximal_access;
176 uint32_t guest_maximal_access;
177 char *service;
178 char *fs_type;
179 } smb1;
181 struct {
182 uint32_t tcon_id;
183 uint8_t type;
184 uint32_t flags;
185 uint32_t capabilities;
186 uint32_t maximal_access;
187 bool should_encrypt;
188 } smb2;
191 struct smbXcli_req_state {
192 struct tevent_context *ev;
193 struct smbXcli_conn *conn;
194 struct smbXcli_session *session; /* maybe NULL */
195 struct smbXcli_tcon *tcon; /* maybe NULL */
197 uint8_t length_hdr[4];
199 bool one_way;
201 uint8_t *inbuf;
203 struct {
204 /* Space for the header including the wct */
205 uint8_t hdr[HDR_VWV];
208 * For normal requests, smb1cli_req_send chooses a mid.
209 * SecondaryV trans requests need to use the mid of the primary
210 * request, so we need a place to store it.
211 * Assume it is set if != 0.
213 uint16_t mid;
215 uint16_t *vwv;
216 uint8_t bytecount_buf[2];
218 #define MAX_SMB_IOV 10
219 /* length_hdr, hdr, words, byte_count, buffers */
220 struct iovec iov[1 + 3 + MAX_SMB_IOV];
221 int iov_count;
223 bool one_way_seqnum;
224 uint32_t seqnum;
225 struct tevent_req **chained_requests;
227 uint8_t recv_cmd;
228 NTSTATUS recv_status;
229 /* always an array of 3 talloc elements */
230 struct iovec *recv_iov;
231 } smb1;
233 struct {
234 const uint8_t *fixed;
235 uint16_t fixed_len;
236 const uint8_t *dyn;
237 uint32_t dyn_len;
239 uint8_t transform[SMB2_TF_HDR_SIZE];
240 uint8_t hdr[SMB2_HDR_BODY];
241 uint8_t pad[7]; /* padding space for compounding */
244 * always an array of 3 talloc elements
245 * (without a SMB2_TRANSFORM header!)
247 * HDR, BODY, DYN
249 struct iovec *recv_iov;
252 * the expected max for the response dyn_len
254 uint32_t max_dyn_len;
256 uint16_t credit_charge;
258 bool should_sign;
259 bool should_encrypt;
260 uint64_t encryption_session_id;
262 bool signing_skipped;
263 bool notify_async;
264 bool got_async;
265 } smb2;
268 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
271 * NT_STATUS_OK, means we do not notify the callers
273 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
275 while (conn->sessions) {
276 conn->sessions->conn = NULL;
277 DLIST_REMOVE(conn->sessions, conn->sessions);
280 if (conn->smb1.trans_enc) {
281 TALLOC_FREE(conn->smb1.trans_enc);
284 return 0;
287 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
288 int fd,
289 const char *remote_name,
290 enum smb_signing_setting signing_state,
291 uint32_t smb1_capabilities,
292 struct GUID *client_guid,
293 uint32_t smb2_capabilities)
295 struct smbXcli_conn *conn = NULL;
296 void *ss = NULL;
297 struct sockaddr *sa = NULL;
298 socklen_t sa_length;
299 int ret;
301 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
302 if (!conn) {
303 return NULL;
306 conn->read_fd = fd;
307 conn->write_fd = dup(fd);
308 if (conn->write_fd == -1) {
309 goto error;
312 conn->remote_name = talloc_strdup(conn, remote_name);
313 if (conn->remote_name == NULL) {
314 goto error;
318 ss = (void *)&conn->local_ss;
319 sa = (struct sockaddr *)ss;
320 sa_length = sizeof(conn->local_ss);
321 ret = getsockname(fd, sa, &sa_length);
322 if (ret == -1) {
323 goto error;
325 ss = (void *)&conn->remote_ss;
326 sa = (struct sockaddr *)ss;
327 sa_length = sizeof(conn->remote_ss);
328 ret = getpeername(fd, sa, &sa_length);
329 if (ret == -1) {
330 goto error;
333 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
334 if (conn->outgoing == NULL) {
335 goto error;
337 conn->pending = NULL;
339 conn->protocol = PROTOCOL_NONE;
341 switch (signing_state) {
342 case SMB_SIGNING_OFF:
343 /* never */
344 conn->allow_signing = false;
345 conn->desire_signing = false;
346 conn->mandatory_signing = false;
347 break;
348 case SMB_SIGNING_DEFAULT:
349 case SMB_SIGNING_IF_REQUIRED:
350 /* if the server requires it */
351 conn->allow_signing = true;
352 conn->desire_signing = false;
353 conn->mandatory_signing = false;
354 break;
355 case SMB_SIGNING_REQUIRED:
356 /* always */
357 conn->allow_signing = true;
358 conn->desire_signing = true;
359 conn->mandatory_signing = true;
360 break;
363 conn->smb1.client.capabilities = smb1_capabilities;
364 conn->smb1.client.max_xmit = UINT16_MAX;
366 conn->smb1.capabilities = conn->smb1.client.capabilities;
367 conn->smb1.max_xmit = 1024;
369 conn->smb1.mid = 1;
371 /* initialise signing */
372 conn->smb1.signing = smb_signing_init(conn,
373 conn->allow_signing,
374 conn->desire_signing,
375 conn->mandatory_signing);
376 if (!conn->smb1.signing) {
377 goto error;
380 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
381 if (conn->mandatory_signing) {
382 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
384 if (client_guid) {
385 conn->smb2.client.guid = *client_guid;
387 conn->smb2.client.capabilities = smb2_capabilities;
389 conn->smb2.cur_credits = 1;
390 conn->smb2.max_credits = 0;
392 talloc_set_destructor(conn, smbXcli_conn_destructor);
393 return conn;
395 error:
396 if (conn->write_fd != -1) {
397 close(conn->write_fd);
399 TALLOC_FREE(conn);
400 return NULL;
403 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
405 if (conn == NULL) {
406 return false;
409 if (conn->read_fd == -1) {
410 return false;
413 return true;
416 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
418 return conn->protocol;
421 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
423 if (conn->protocol >= PROTOCOL_SMB2_02) {
424 return true;
427 if (conn->smb1.capabilities & CAP_UNICODE) {
428 return true;
431 return false;
434 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
436 set_socket_options(conn->read_fd, options);
439 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
441 return &conn->local_ss;
444 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
446 return &conn->remote_ss;
449 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
451 return conn->remote_name;
454 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
456 if (conn->protocol >= PROTOCOL_SMB2_02) {
458 * TODO...
460 return 1;
463 return conn->smb1.server.max_mux;
466 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
468 if (conn->protocol >= PROTOCOL_SMB2_02) {
469 return conn->smb2.server.system_time;
472 return conn->smb1.server.system_time;
475 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
477 if (conn->protocol >= PROTOCOL_SMB2_02) {
478 return &conn->smb2.server.gss_blob;
481 return &conn->smb1.server.gss_blob;
484 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
486 if (conn->protocol >= PROTOCOL_SMB2_02) {
487 return &conn->smb2.server.guid;
490 return &conn->smb1.server.guid;
493 struct smbXcli_conn_samba_suicide_state {
494 struct smbXcli_conn *conn;
495 struct iovec iov;
496 uint8_t buf[9];
499 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
501 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
502 struct tevent_context *ev,
503 struct smbXcli_conn *conn,
504 uint8_t exitcode)
506 struct tevent_req *req, *subreq;
507 struct smbXcli_conn_samba_suicide_state *state;
509 req = tevent_req_create(mem_ctx, &state,
510 struct smbXcli_conn_samba_suicide_state);
511 if (req == NULL) {
512 return NULL;
514 state->conn = conn;
515 SIVAL(state->buf, 4, 0x74697865);
516 SCVAL(state->buf, 8, exitcode);
517 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
519 state->iov.iov_base = state->buf;
520 state->iov.iov_len = sizeof(state->buf);
522 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
523 false, &state->iov, 1);
524 if (tevent_req_nomem(subreq, req)) {
525 return tevent_req_post(req, ev);
527 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
528 return req;
531 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
533 struct tevent_req *req = tevent_req_callback_data(
534 subreq, struct tevent_req);
535 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
536 req, struct smbXcli_conn_samba_suicide_state);
537 ssize_t nwritten;
538 int err;
540 nwritten = writev_recv(subreq, &err);
541 TALLOC_FREE(subreq);
542 if (nwritten == -1) {
543 NTSTATUS status = map_nt_error_from_unix_common(err);
544 smbXcli_conn_disconnect(state->conn, status);
545 return;
547 tevent_req_done(req);
550 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
552 return tevent_req_simple_recv_ntstatus(req);
555 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
556 uint8_t exitcode)
558 TALLOC_CTX *frame = talloc_stackframe();
559 struct tevent_context *ev;
560 struct tevent_req *req;
561 NTSTATUS status = NT_STATUS_NO_MEMORY;
562 bool ok;
564 if (smbXcli_conn_has_async_calls(conn)) {
566 * Can't use sync call while an async call is in flight
568 status = NT_STATUS_INVALID_PARAMETER_MIX;
569 goto fail;
571 ev = samba_tevent_context_init(frame);
572 if (ev == NULL) {
573 goto fail;
575 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
576 if (req == NULL) {
577 goto fail;
579 ok = tevent_req_poll(req, ev);
580 if (!ok) {
581 status = map_nt_error_from_unix_common(errno);
582 goto fail;
584 status = smbXcli_conn_samba_suicide_recv(req);
585 fail:
586 TALLOC_FREE(frame);
587 return status;
590 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
592 return conn->smb1.capabilities;
595 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
597 return conn->smb1.max_xmit;
600 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
602 size_t pending;
603 uint16_t possible = conn->smb1.server.max_mux;
605 pending = tevent_queue_length(conn->outgoing);
606 if (pending >= possible) {
607 return false;
609 pending += talloc_array_length(conn->pending);
610 if (pending >= possible) {
611 return false;
614 return true;
617 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
619 return conn->smb1.server.session_key;
622 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
624 return conn->smb1.server.challenge;
627 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
629 return conn->smb1.server.security_mode;
632 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
634 return conn->smb1.server.readbraw;
637 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
639 return conn->smb1.server.writebraw;
642 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
644 return conn->smb1.server.lockread;
647 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
649 return conn->smb1.server.writeunlock;
652 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
654 return conn->smb1.server.time_zone;
657 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
658 const DATA_BLOB user_session_key,
659 const DATA_BLOB response)
661 return smb_signing_activate(conn->smb1.signing,
662 user_session_key,
663 response);
666 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
667 const uint8_t *buf, uint32_t seqnum)
669 const uint8_t *hdr = buf + NBT_HDR_SIZE;
670 size_t len = smb_len_nbt(buf);
672 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
675 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
677 return smb_signing_is_active(conn->smb1.signing);
680 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
681 struct smb_trans_enc_state *es)
683 /* Replace the old state, if any. */
684 if (conn->smb1.trans_enc) {
685 TALLOC_FREE(conn->smb1.trans_enc);
687 conn->smb1.trans_enc = es;
690 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
692 return common_encryption_on(conn->smb1.trans_enc);
696 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
698 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
699 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
701 if (NT_STATUS_IS_OK(status)) {
702 return NT_STATUS_OK;
705 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
706 return status;
709 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
713 * Is the SMB command able to hold an AND_X successor
714 * @param[in] cmd The SMB command in question
715 * @retval Can we add a chained request after "cmd"?
717 bool smb1cli_is_andx_req(uint8_t cmd)
719 switch (cmd) {
720 case SMBtconX:
721 case SMBlockingX:
722 case SMBopenX:
723 case SMBreadX:
724 case SMBwriteX:
725 case SMBsesssetupX:
726 case SMBulogoffX:
727 case SMBntcreateX:
728 return true;
729 break;
730 default:
731 break;
734 return false;
737 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
739 size_t num_pending = talloc_array_length(conn->pending);
740 uint16_t result;
742 if (conn->protocol == PROTOCOL_NONE) {
744 * This is what windows sends on the SMB1 Negprot request
745 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
747 return 0;
750 while (true) {
751 size_t i;
753 result = conn->smb1.mid++;
754 if ((result == 0) || (result == 0xffff)) {
755 continue;
758 for (i=0; i<num_pending; i++) {
759 if (result == smb1cli_req_mid(conn->pending[i])) {
760 break;
764 if (i == num_pending) {
765 return result;
770 void smbXcli_req_unset_pending(struct tevent_req *req)
772 struct smbXcli_req_state *state =
773 tevent_req_data(req,
774 struct smbXcli_req_state);
775 struct smbXcli_conn *conn = state->conn;
776 size_t num_pending = talloc_array_length(conn->pending);
777 size_t i;
779 if (state->smb1.mid != 0) {
781 * This is a [nt]trans[2] request which waits
782 * for more than one reply.
784 return;
787 talloc_set_destructor(req, NULL);
789 if (num_pending == 1) {
791 * The pending read_smb tevent_req is a child of
792 * conn->pending. So if nothing is pending anymore, we need to
793 * delete the socket read fde.
795 TALLOC_FREE(conn->pending);
796 conn->read_smb_req = NULL;
797 return;
800 for (i=0; i<num_pending; i++) {
801 if (req == conn->pending[i]) {
802 break;
805 if (i == num_pending) {
807 * Something's seriously broken. Just returning here is the
808 * right thing nevertheless, the point of this routine is to
809 * remove ourselves from conn->pending.
811 return;
815 * Remove ourselves from the conn->pending array
817 for (; i < (num_pending - 1); i++) {
818 conn->pending[i] = conn->pending[i+1];
822 * No NULL check here, we're shrinking by sizeof(void *), and
823 * talloc_realloc just adjusts the size for this.
825 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
826 num_pending - 1);
827 return;
830 static int smbXcli_req_destructor(struct tevent_req *req)
832 struct smbXcli_req_state *state =
833 tevent_req_data(req,
834 struct smbXcli_req_state);
837 * Make sure we really remove it from
838 * the pending array on destruction.
840 state->smb1.mid = 0;
841 smbXcli_req_unset_pending(req);
842 return 0;
845 static bool smb1cli_req_cancel(struct tevent_req *req);
846 static bool smb2cli_req_cancel(struct tevent_req *req);
848 static bool smbXcli_req_cancel(struct tevent_req *req)
850 struct smbXcli_req_state *state =
851 tevent_req_data(req,
852 struct smbXcli_req_state);
854 if (!smbXcli_conn_is_connected(state->conn)) {
855 return false;
858 if (state->conn->protocol == PROTOCOL_NONE) {
859 return false;
862 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
863 return smb2cli_req_cancel(req);
866 return smb1cli_req_cancel(req);
869 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
871 bool smbXcli_req_set_pending(struct tevent_req *req)
873 struct smbXcli_req_state *state =
874 tevent_req_data(req,
875 struct smbXcli_req_state);
876 struct smbXcli_conn *conn;
877 struct tevent_req **pending;
878 size_t num_pending;
880 conn = state->conn;
882 if (!smbXcli_conn_is_connected(conn)) {
883 return false;
886 num_pending = talloc_array_length(conn->pending);
888 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
889 num_pending+1);
890 if (pending == NULL) {
891 return false;
893 pending[num_pending] = req;
894 conn->pending = pending;
895 talloc_set_destructor(req, smbXcli_req_destructor);
896 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
898 if (!smbXcli_conn_receive_next(conn)) {
900 * the caller should notify the current request
902 * And all other pending requests get notified
903 * by smbXcli_conn_disconnect().
905 smbXcli_req_unset_pending(req);
906 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
907 return false;
910 return true;
913 static void smbXcli_conn_received(struct tevent_req *subreq);
915 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
917 size_t num_pending = talloc_array_length(conn->pending);
918 struct tevent_req *req;
919 struct smbXcli_req_state *state;
921 if (conn->read_smb_req != NULL) {
922 return true;
925 if (num_pending == 0) {
926 if (conn->smb2.mid < UINT64_MAX) {
927 /* no more pending requests, so we are done for now */
928 return true;
932 * If there are no more SMB2 requests possible,
933 * because we are out of message ids,
934 * we need to disconnect.
936 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
937 return true;
940 req = conn->pending[0];
941 state = tevent_req_data(req, struct smbXcli_req_state);
944 * We're the first ones, add the read_smb request that waits for the
945 * answer from the server
947 conn->read_smb_req = read_smb_send(conn->pending,
948 state->ev,
949 conn->read_fd);
950 if (conn->read_smb_req == NULL) {
951 return false;
953 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
954 return true;
957 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
959 struct smbXcli_session *session;
961 tevent_queue_stop(conn->outgoing);
963 if (conn->read_fd != -1) {
964 close(conn->read_fd);
966 if (conn->write_fd != -1) {
967 close(conn->write_fd);
969 conn->read_fd = -1;
970 conn->write_fd = -1;
972 session = conn->sessions;
973 if (talloc_array_length(conn->pending) == 0) {
975 * if we do not have pending requests
976 * there is no need to update the channel_sequence
978 session = NULL;
980 for (; session; session = session->next) {
981 smb2cli_session_increment_channel_sequence(session);
985 * Cancel all pending requests. We do not do a for-loop walking
986 * conn->pending because that array changes in
987 * smbXcli_req_unset_pending.
989 while (talloc_array_length(conn->pending) > 0) {
990 struct tevent_req *req;
991 struct smbXcli_req_state *state;
992 struct tevent_req **chain;
993 size_t num_chained;
994 size_t i;
996 req = conn->pending[0];
997 state = tevent_req_data(req, struct smbXcli_req_state);
999 if (state->smb1.chained_requests == NULL) {
1001 * We're dead. No point waiting for trans2
1002 * replies.
1004 state->smb1.mid = 0;
1006 smbXcli_req_unset_pending(req);
1008 if (NT_STATUS_IS_OK(status)) {
1009 /* do not notify the callers */
1010 continue;
1014 * we need to defer the callback, because we may notify
1015 * more then one caller.
1017 tevent_req_defer_callback(req, state->ev);
1018 tevent_req_nterror(req, status);
1019 continue;
1022 chain = talloc_move(conn, &state->smb1.chained_requests);
1023 num_chained = talloc_array_length(chain);
1025 for (i=0; i<num_chained; i++) {
1026 req = chain[i];
1027 state = tevent_req_data(req, struct smbXcli_req_state);
1030 * We're dead. No point waiting for trans2
1031 * replies.
1033 state->smb1.mid = 0;
1035 smbXcli_req_unset_pending(req);
1037 if (NT_STATUS_IS_OK(status)) {
1038 /* do not notify the callers */
1039 continue;
1043 * we need to defer the callback, because we may notify
1044 * more than one caller.
1046 tevent_req_defer_callback(req, state->ev);
1047 tevent_req_nterror(req, status);
1049 TALLOC_FREE(chain);
1054 * Fetch a smb request's mid. Only valid after the request has been sent by
1055 * smb1cli_req_send().
1057 uint16_t smb1cli_req_mid(struct tevent_req *req)
1059 struct smbXcli_req_state *state =
1060 tevent_req_data(req,
1061 struct smbXcli_req_state);
1063 if (state->smb1.mid != 0) {
1064 return state->smb1.mid;
1067 return SVAL(state->smb1.hdr, HDR_MID);
1070 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1072 struct smbXcli_req_state *state =
1073 tevent_req_data(req,
1074 struct smbXcli_req_state);
1076 state->smb1.mid = mid;
1079 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1081 struct smbXcli_req_state *state =
1082 tevent_req_data(req,
1083 struct smbXcli_req_state);
1085 return state->smb1.seqnum;
1088 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1090 struct smbXcli_req_state *state =
1091 tevent_req_data(req,
1092 struct smbXcli_req_state);
1094 state->smb1.seqnum = seqnum;
1097 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1099 size_t result = 0;
1100 int i;
1101 for (i=0; i<count; i++) {
1102 result += iov[i].iov_len;
1104 return result;
1107 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1108 const struct iovec *iov,
1109 int count)
1111 size_t len = smbXcli_iov_len(iov, count);
1112 size_t copied;
1113 uint8_t *buf;
1114 int i;
1116 buf = talloc_array(mem_ctx, uint8_t, len);
1117 if (buf == NULL) {
1118 return NULL;
1120 copied = 0;
1121 for (i=0; i<count; i++) {
1122 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1123 copied += iov[i].iov_len;
1125 return buf;
1128 static void smb1cli_req_flags(enum protocol_types protocol,
1129 uint32_t smb1_capabilities,
1130 uint8_t smb_command,
1131 uint8_t additional_flags,
1132 uint8_t clear_flags,
1133 uint8_t *_flags,
1134 uint16_t additional_flags2,
1135 uint16_t clear_flags2,
1136 uint16_t *_flags2)
1138 uint8_t flags = 0;
1139 uint16_t flags2 = 0;
1141 if (protocol >= PROTOCOL_LANMAN1) {
1142 flags |= FLAG_CASELESS_PATHNAMES;
1143 flags |= FLAG_CANONICAL_PATHNAMES;
1146 if (protocol >= PROTOCOL_LANMAN2) {
1147 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1148 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1151 if (protocol >= PROTOCOL_NT1) {
1152 flags2 |= FLAGS2_IS_LONG_NAME;
1154 if (smb1_capabilities & CAP_UNICODE) {
1155 flags2 |= FLAGS2_UNICODE_STRINGS;
1157 if (smb1_capabilities & CAP_STATUS32) {
1158 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1160 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1161 flags2 |= FLAGS2_EXTENDED_SECURITY;
1165 flags |= additional_flags;
1166 flags &= ~clear_flags;
1167 flags2 |= additional_flags2;
1168 flags2 &= ~clear_flags2;
1170 *_flags = flags;
1171 *_flags2 = flags2;
1174 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1176 static bool smb1cli_req_cancel(struct tevent_req *req)
1178 struct smbXcli_req_state *state =
1179 tevent_req_data(req,
1180 struct smbXcli_req_state);
1181 uint8_t flags;
1182 uint16_t flags2;
1183 uint32_t pid;
1184 uint16_t mid;
1185 struct tevent_req *subreq;
1186 NTSTATUS status;
1188 flags = CVAL(state->smb1.hdr, HDR_FLG);
1189 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1190 pid = SVAL(state->smb1.hdr, HDR_PID);
1191 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1192 mid = SVAL(state->smb1.hdr, HDR_MID);
1194 subreq = smb1cli_req_create(state, state->ev,
1195 state->conn,
1196 SMBntcancel,
1197 flags, 0,
1198 flags2, 0,
1199 0, /* timeout */
1200 pid,
1201 state->tcon,
1202 state->session,
1203 0, NULL, /* vwv */
1204 0, NULL); /* bytes */
1205 if (subreq == NULL) {
1206 return false;
1208 smb1cli_req_set_mid(subreq, mid);
1210 status = smb1cli_req_chain_submit(&subreq, 1);
1211 if (!NT_STATUS_IS_OK(status)) {
1212 TALLOC_FREE(subreq);
1213 return false;
1215 smb1cli_req_set_mid(subreq, 0);
1217 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1219 return true;
1222 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1224 /* we do not care about the result */
1225 TALLOC_FREE(subreq);
1228 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1229 struct tevent_context *ev,
1230 struct smbXcli_conn *conn,
1231 uint8_t smb_command,
1232 uint8_t additional_flags,
1233 uint8_t clear_flags,
1234 uint16_t additional_flags2,
1235 uint16_t clear_flags2,
1236 uint32_t timeout_msec,
1237 uint32_t pid,
1238 struct smbXcli_tcon *tcon,
1239 struct smbXcli_session *session,
1240 uint8_t wct, uint16_t *vwv,
1241 int iov_count,
1242 struct iovec *bytes_iov)
1244 struct tevent_req *req;
1245 struct smbXcli_req_state *state;
1246 uint8_t flags = 0;
1247 uint16_t flags2 = 0;
1248 uint16_t uid = 0;
1249 uint16_t tid = 0;
1251 if (iov_count > MAX_SMB_IOV) {
1253 * Should not happen :-)
1255 return NULL;
1258 req = tevent_req_create(mem_ctx, &state,
1259 struct smbXcli_req_state);
1260 if (req == NULL) {
1261 return NULL;
1263 state->ev = ev;
1264 state->conn = conn;
1265 state->session = session;
1266 state->tcon = tcon;
1268 if (session) {
1269 uid = session->smb1.session_id;
1272 if (tcon) {
1273 tid = tcon->smb1.tcon_id;
1276 state->smb1.recv_cmd = 0xFF;
1277 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1278 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1279 if (state->smb1.recv_iov == NULL) {
1280 TALLOC_FREE(req);
1281 return NULL;
1284 smb1cli_req_flags(conn->protocol,
1285 conn->smb1.capabilities,
1286 smb_command,
1287 additional_flags,
1288 clear_flags,
1289 &flags,
1290 additional_flags2,
1291 clear_flags2,
1292 &flags2);
1294 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1295 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1296 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1297 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1298 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1299 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1300 SSVAL(state->smb1.hdr, HDR_TID, tid);
1301 SSVAL(state->smb1.hdr, HDR_PID, pid);
1302 SSVAL(state->smb1.hdr, HDR_UID, uid);
1303 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1304 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1306 state->smb1.vwv = vwv;
1308 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1310 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1311 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1312 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1313 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1314 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1315 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1316 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1317 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1319 if (iov_count != 0) {
1320 memcpy(&state->smb1.iov[4], bytes_iov,
1321 iov_count * sizeof(*bytes_iov));
1323 state->smb1.iov_count = iov_count + 4;
1325 if (timeout_msec > 0) {
1326 struct timeval endtime;
1328 endtime = timeval_current_ofs_msec(timeout_msec);
1329 if (!tevent_req_set_endtime(req, ev, endtime)) {
1330 return req;
1334 switch (smb_command) {
1335 case SMBtranss:
1336 case SMBtranss2:
1337 case SMBnttranss:
1338 state->one_way = true;
1339 break;
1340 case SMBntcancel:
1341 state->one_way = true;
1342 state->smb1.one_way_seqnum = true;
1343 break;
1344 case SMBlockingX:
1345 if ((wct == 8) &&
1346 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1347 state->one_way = true;
1349 break;
1352 return req;
1355 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1356 struct iovec *iov, int iov_count,
1357 uint32_t *seqnum,
1358 bool one_way_seqnum)
1360 TALLOC_CTX *frame = NULL;
1361 uint8_t *buf;
1364 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1365 * iovec directly. MD5Update would do that just fine.
1368 if (iov_count < 4) {
1369 return NT_STATUS_INVALID_PARAMETER_MIX;
1371 if (iov[0].iov_len != NBT_HDR_SIZE) {
1372 return NT_STATUS_INVALID_PARAMETER_MIX;
1374 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1375 return NT_STATUS_INVALID_PARAMETER_MIX;
1377 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1378 return NT_STATUS_INVALID_PARAMETER_MIX;
1380 if (iov[3].iov_len != sizeof(uint16_t)) {
1381 return NT_STATUS_INVALID_PARAMETER_MIX;
1384 frame = talloc_stackframe();
1386 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1387 if (buf == NULL) {
1388 return NT_STATUS_NO_MEMORY;
1391 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1392 one_way_seqnum);
1393 smb_signing_sign_pdu(conn->smb1.signing,
1394 buf, talloc_get_size(buf),
1395 *seqnum);
1396 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1398 TALLOC_FREE(frame);
1399 return NT_STATUS_OK;
1402 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1403 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1404 TALLOC_CTX *tmp_mem,
1405 uint8_t *inbuf);
1407 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1408 struct smbXcli_req_state *state,
1409 struct iovec *iov, int iov_count)
1411 struct tevent_req *subreq;
1412 NTSTATUS status;
1413 uint8_t cmd;
1414 uint16_t mid;
1416 if (!smbXcli_conn_is_connected(state->conn)) {
1417 return NT_STATUS_CONNECTION_DISCONNECTED;
1420 if (state->conn->protocol > PROTOCOL_NT1) {
1421 return NT_STATUS_REVISION_MISMATCH;
1424 if (iov_count < 4) {
1425 return NT_STATUS_INVALID_PARAMETER_MIX;
1427 if (iov[0].iov_len != NBT_HDR_SIZE) {
1428 return NT_STATUS_INVALID_PARAMETER_MIX;
1430 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1431 return NT_STATUS_INVALID_PARAMETER_MIX;
1433 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1434 return NT_STATUS_INVALID_PARAMETER_MIX;
1436 if (iov[3].iov_len != sizeof(uint16_t)) {
1437 return NT_STATUS_INVALID_PARAMETER_MIX;
1440 cmd = CVAL(iov[1].iov_base, HDR_COM);
1441 if (cmd == SMBreadBraw) {
1442 if (smbXcli_conn_has_async_calls(state->conn)) {
1443 return NT_STATUS_INVALID_PARAMETER_MIX;
1445 state->conn->smb1.read_braw_req = req;
1448 if (state->smb1.mid != 0) {
1449 mid = state->smb1.mid;
1450 } else {
1451 mid = smb1cli_alloc_mid(state->conn);
1453 SSVAL(iov[1].iov_base, HDR_MID, mid);
1455 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1457 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1458 &state->smb1.seqnum,
1459 state->smb1.one_way_seqnum);
1461 if (!NT_STATUS_IS_OK(status)) {
1462 return status;
1466 * If we supported multiple encrytion contexts
1467 * here we'd look up based on tid.
1469 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1470 char *buf, *enc_buf;
1472 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1473 if (buf == NULL) {
1474 return NT_STATUS_NO_MEMORY;
1476 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1477 (char *)buf, &enc_buf);
1478 TALLOC_FREE(buf);
1479 if (!NT_STATUS_IS_OK(status)) {
1480 DEBUG(0, ("Error in encrypting client message: %s\n",
1481 nt_errstr(status)));
1482 return status;
1484 buf = (char *)talloc_memdup(state, enc_buf,
1485 smb_len_nbt(enc_buf)+4);
1486 SAFE_FREE(enc_buf);
1487 if (buf == NULL) {
1488 return NT_STATUS_NO_MEMORY;
1490 iov[0].iov_base = (void *)buf;
1491 iov[0].iov_len = talloc_get_size(buf);
1492 iov_count = 1;
1495 if (state->conn->dispatch_incoming == NULL) {
1496 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1499 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1501 subreq = writev_send(state, state->ev, state->conn->outgoing,
1502 state->conn->write_fd, false, iov, iov_count);
1503 if (subreq == NULL) {
1504 return NT_STATUS_NO_MEMORY;
1506 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1507 return NT_STATUS_OK;
1510 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1511 struct tevent_context *ev,
1512 struct smbXcli_conn *conn,
1513 uint8_t smb_command,
1514 uint8_t additional_flags,
1515 uint8_t clear_flags,
1516 uint16_t additional_flags2,
1517 uint16_t clear_flags2,
1518 uint32_t timeout_msec,
1519 uint32_t pid,
1520 struct smbXcli_tcon *tcon,
1521 struct smbXcli_session *session,
1522 uint8_t wct, uint16_t *vwv,
1523 uint32_t num_bytes,
1524 const uint8_t *bytes)
1526 struct tevent_req *req;
1527 struct iovec iov;
1528 NTSTATUS status;
1530 iov.iov_base = discard_const_p(void, bytes);
1531 iov.iov_len = num_bytes;
1533 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1534 additional_flags, clear_flags,
1535 additional_flags2, clear_flags2,
1536 timeout_msec,
1537 pid, tcon, session,
1538 wct, vwv, 1, &iov);
1539 if (req == NULL) {
1540 return NULL;
1542 if (!tevent_req_is_in_progress(req)) {
1543 return tevent_req_post(req, ev);
1545 status = smb1cli_req_chain_submit(&req, 1);
1546 if (tevent_req_nterror(req, status)) {
1547 return tevent_req_post(req, ev);
1549 return req;
1552 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1554 struct tevent_req *req =
1555 tevent_req_callback_data(subreq,
1556 struct tevent_req);
1557 struct smbXcli_req_state *state =
1558 tevent_req_data(req,
1559 struct smbXcli_req_state);
1560 ssize_t nwritten;
1561 int err;
1563 nwritten = writev_recv(subreq, &err);
1564 TALLOC_FREE(subreq);
1565 if (nwritten == -1) {
1566 NTSTATUS status = map_nt_error_from_unix_common(err);
1567 smbXcli_conn_disconnect(state->conn, status);
1568 return;
1571 if (state->one_way) {
1572 state->inbuf = NULL;
1573 tevent_req_done(req);
1574 return;
1577 if (!smbXcli_req_set_pending(req)) {
1578 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1579 return;
1583 static void smbXcli_conn_received(struct tevent_req *subreq)
1585 struct smbXcli_conn *conn =
1586 tevent_req_callback_data(subreq,
1587 struct smbXcli_conn);
1588 TALLOC_CTX *frame = talloc_stackframe();
1589 NTSTATUS status;
1590 uint8_t *inbuf;
1591 ssize_t received;
1592 int err;
1594 if (subreq != conn->read_smb_req) {
1595 DEBUG(1, ("Internal error: cli_smb_received called with "
1596 "unexpected subreq\n"));
1597 status = NT_STATUS_INTERNAL_ERROR;
1598 smbXcli_conn_disconnect(conn, status);
1599 TALLOC_FREE(frame);
1600 return;
1602 conn->read_smb_req = NULL;
1604 received = read_smb_recv(subreq, frame, &inbuf, &err);
1605 TALLOC_FREE(subreq);
1606 if (received == -1) {
1607 status = map_nt_error_from_unix_common(err);
1608 smbXcli_conn_disconnect(conn, status);
1609 TALLOC_FREE(frame);
1610 return;
1613 status = conn->dispatch_incoming(conn, frame, inbuf);
1614 TALLOC_FREE(frame);
1615 if (NT_STATUS_IS_OK(status)) {
1617 * We should not do any more processing
1618 * as the dispatch function called
1619 * tevent_req_done().
1621 return;
1622 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1624 * We got an error, so notify all pending requests
1626 smbXcli_conn_disconnect(conn, status);
1627 return;
1631 * We got NT_STATUS_RETRY, so we may ask for a
1632 * next incoming pdu.
1634 if (!smbXcli_conn_receive_next(conn)) {
1635 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1639 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1640 struct iovec **piov, int *pnum_iov)
1642 struct iovec *iov;
1643 int num_iov;
1644 size_t buflen;
1645 size_t taken;
1646 size_t remaining;
1647 uint8_t *hdr;
1648 uint8_t cmd;
1649 uint32_t wct_ofs;
1650 NTSTATUS status;
1651 size_t min_size = MIN_SMB_SIZE;
1653 buflen = smb_len_tcp(buf);
1654 taken = 0;
1656 hdr = buf + NBT_HDR_SIZE;
1658 status = smb1cli_pull_raw_error(hdr);
1659 if (NT_STATUS_IS_ERR(status)) {
1661 * This is an ugly hack to support OS/2
1662 * which skips the byte_count in the DATA block
1663 * on some error responses.
1665 * See bug #9096
1667 min_size -= sizeof(uint16_t);
1670 if (buflen < min_size) {
1671 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1675 * This returns iovec elements in the following order:
1677 * - SMB header
1679 * - Parameter Block
1680 * - Data Block
1682 * - Parameter Block
1683 * - Data Block
1685 * - Parameter Block
1686 * - Data Block
1688 num_iov = 1;
1690 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1691 if (iov == NULL) {
1692 return NT_STATUS_NO_MEMORY;
1694 iov[0].iov_base = hdr;
1695 iov[0].iov_len = HDR_WCT;
1696 taken += HDR_WCT;
1698 cmd = CVAL(hdr, HDR_COM);
1699 wct_ofs = HDR_WCT;
1701 while (true) {
1702 size_t len = buflen - taken;
1703 struct iovec *cur;
1704 struct iovec *iov_tmp;
1705 uint8_t wct;
1706 uint32_t bcc_ofs;
1707 uint16_t bcc;
1708 size_t needed;
1711 * we need at least WCT
1713 needed = sizeof(uint8_t);
1714 if (len < needed) {
1715 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1716 __location__, (int)len, (int)needed));
1717 goto inval;
1721 * Now we check if the specified words are there
1723 wct = CVAL(hdr, wct_ofs);
1724 needed += wct * sizeof(uint16_t);
1725 if (len < needed) {
1726 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1727 __location__, (int)len, (int)needed));
1728 goto inval;
1731 if ((num_iov == 1) &&
1732 (len == needed) &&
1733 NT_STATUS_IS_ERR(status))
1736 * This is an ugly hack to support OS/2
1737 * which skips the byte_count in the DATA block
1738 * on some error responses.
1740 * See bug #9096
1742 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1743 num_iov + 2);
1744 if (iov_tmp == NULL) {
1745 TALLOC_FREE(iov);
1746 return NT_STATUS_NO_MEMORY;
1748 iov = iov_tmp;
1749 cur = &iov[num_iov];
1750 num_iov += 2;
1752 cur[0].iov_len = 0;
1753 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1754 cur[1].iov_len = 0;
1755 cur[1].iov_base = cur[0].iov_base;
1757 taken += needed;
1758 break;
1762 * we need at least BCC
1764 needed += sizeof(uint16_t);
1765 if (len < needed) {
1766 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1767 __location__, (int)len, (int)needed));
1768 goto inval;
1772 * Now we check if the specified bytes are there
1774 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1775 bcc = SVAL(hdr, bcc_ofs);
1776 needed += bcc * sizeof(uint8_t);
1777 if (len < needed) {
1778 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1779 __location__, (int)len, (int)needed));
1780 goto inval;
1784 * we allocate 2 iovec structures for words and bytes
1786 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1787 num_iov + 2);
1788 if (iov_tmp == NULL) {
1789 TALLOC_FREE(iov);
1790 return NT_STATUS_NO_MEMORY;
1792 iov = iov_tmp;
1793 cur = &iov[num_iov];
1794 num_iov += 2;
1796 cur[0].iov_len = wct * sizeof(uint16_t);
1797 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1798 cur[1].iov_len = bcc * sizeof(uint8_t);
1799 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1801 taken += needed;
1803 if (!smb1cli_is_andx_req(cmd)) {
1805 * If the current command does not have AndX chanining
1806 * we are done.
1808 break;
1811 if (wct == 0 && bcc == 0) {
1813 * An empty response also ends the chain,
1814 * most likely with an error.
1816 break;
1819 if (wct < 2) {
1820 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1821 __location__, (int)wct, (int)cmd));
1822 goto inval;
1824 cmd = CVAL(cur[0].iov_base, 0);
1825 if (cmd == 0xFF) {
1827 * If it is the end of the chain we are also done.
1829 break;
1831 wct_ofs = SVAL(cur[0].iov_base, 2);
1833 if (wct_ofs < taken) {
1834 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1836 if (wct_ofs > buflen) {
1837 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1841 * we consumed everything up to the start of the next
1842 * parameter block.
1844 taken = wct_ofs;
1847 remaining = buflen - taken;
1849 if (remaining > 0 && num_iov >= 3) {
1851 * The last DATA block gets the remaining
1852 * bytes, this is needed to support
1853 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1855 iov[num_iov-1].iov_len += remaining;
1858 *piov = iov;
1859 *pnum_iov = num_iov;
1860 return NT_STATUS_OK;
1862 inval:
1863 TALLOC_FREE(iov);
1864 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1867 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1868 TALLOC_CTX *tmp_mem,
1869 uint8_t *inbuf)
1871 struct tevent_req *req;
1872 struct smbXcli_req_state *state;
1873 NTSTATUS status;
1874 size_t num_pending;
1875 size_t i;
1876 uint8_t cmd;
1877 uint16_t mid;
1878 bool oplock_break;
1879 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1880 size_t len = smb_len_tcp(inbuf);
1881 struct iovec *iov = NULL;
1882 int num_iov = 0;
1883 struct tevent_req **chain = NULL;
1884 size_t num_chained = 0;
1885 size_t num_responses = 0;
1887 if (conn->smb1.read_braw_req != NULL) {
1888 req = conn->smb1.read_braw_req;
1889 conn->smb1.read_braw_req = NULL;
1890 state = tevent_req_data(req, struct smbXcli_req_state);
1892 smbXcli_req_unset_pending(req);
1894 if (state->smb1.recv_iov == NULL) {
1896 * For requests with more than
1897 * one response, we have to readd the
1898 * recv_iov array.
1900 state->smb1.recv_iov = talloc_zero_array(state,
1901 struct iovec,
1903 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1904 return NT_STATUS_OK;
1908 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1909 state->smb1.recv_iov[0].iov_len = len;
1910 ZERO_STRUCT(state->smb1.recv_iov[1]);
1911 ZERO_STRUCT(state->smb1.recv_iov[2]);
1913 state->smb1.recv_cmd = SMBreadBraw;
1914 state->smb1.recv_status = NT_STATUS_OK;
1915 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1917 tevent_req_done(req);
1918 return NT_STATUS_OK;
1921 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1922 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1923 DEBUG(10, ("Got non-SMB PDU\n"));
1924 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1928 * If we supported multiple encrytion contexts
1929 * here we'd look up based on tid.
1931 if (common_encryption_on(conn->smb1.trans_enc)
1932 && (CVAL(inbuf, 0) == 0)) {
1933 uint16_t enc_ctx_num;
1935 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1936 if (!NT_STATUS_IS_OK(status)) {
1937 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1938 nt_errstr(status)));
1939 return status;
1942 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1943 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1944 enc_ctx_num,
1945 conn->smb1.trans_enc->enc_ctx_num));
1946 return NT_STATUS_INVALID_HANDLE;
1949 status = common_decrypt_buffer(conn->smb1.trans_enc,
1950 (char *)inbuf);
1951 if (!NT_STATUS_IS_OK(status)) {
1952 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1953 nt_errstr(status)));
1954 return status;
1956 inhdr = inbuf + NBT_HDR_SIZE;
1957 len = smb_len_nbt(inbuf);
1960 mid = SVAL(inhdr, HDR_MID);
1961 num_pending = talloc_array_length(conn->pending);
1963 for (i=0; i<num_pending; i++) {
1964 if (mid == smb1cli_req_mid(conn->pending[i])) {
1965 break;
1968 if (i == num_pending) {
1969 /* Dump unexpected reply */
1970 return NT_STATUS_RETRY;
1973 oplock_break = false;
1975 if (mid == 0xffff) {
1977 * Paranoia checks that this is really an oplock break request.
1979 oplock_break = (len == 51); /* hdr + 8 words */
1980 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1981 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1982 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1983 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1985 if (!oplock_break) {
1986 /* Dump unexpected reply */
1987 return NT_STATUS_RETRY;
1991 req = conn->pending[i];
1992 state = tevent_req_data(req, struct smbXcli_req_state);
1994 if (!oplock_break /* oplock breaks are not signed */
1995 && !smb_signing_check_pdu(conn->smb1.signing,
1996 inhdr, len, state->smb1.seqnum+1)) {
1997 DEBUG(10, ("cli_check_sign_mac failed\n"));
1998 return NT_STATUS_ACCESS_DENIED;
2001 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2002 &iov, &num_iov);
2003 if (!NT_STATUS_IS_OK(status)) {
2004 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2005 nt_errstr(status)));
2006 return status;
2009 cmd = CVAL(inhdr, HDR_COM);
2010 status = smb1cli_pull_raw_error(inhdr);
2012 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2013 (state->session != NULL) && state->session->disconnect_expired)
2016 * this should be a short term hack
2017 * until the upper layers have implemented
2018 * re-authentication.
2020 return status;
2023 if (state->smb1.chained_requests == NULL) {
2024 if (num_iov != 3) {
2025 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2028 smbXcli_req_unset_pending(req);
2030 if (state->smb1.recv_iov == NULL) {
2032 * For requests with more than
2033 * one response, we have to readd the
2034 * recv_iov array.
2036 state->smb1.recv_iov = talloc_zero_array(state,
2037 struct iovec,
2039 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2040 return NT_STATUS_OK;
2044 state->smb1.recv_cmd = cmd;
2045 state->smb1.recv_status = status;
2046 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2048 state->smb1.recv_iov[0] = iov[0];
2049 state->smb1.recv_iov[1] = iov[1];
2050 state->smb1.recv_iov[2] = iov[2];
2052 if (talloc_array_length(conn->pending) == 0) {
2053 tevent_req_done(req);
2054 return NT_STATUS_OK;
2057 tevent_req_defer_callback(req, state->ev);
2058 tevent_req_done(req);
2059 return NT_STATUS_RETRY;
2062 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2063 num_chained = talloc_array_length(chain);
2064 num_responses = (num_iov - 1)/2;
2066 if (num_responses > num_chained) {
2067 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2070 for (i=0; i<num_chained; i++) {
2071 size_t iov_idx = 1 + (i*2);
2072 struct iovec *cur = &iov[iov_idx];
2073 uint8_t *inbuf_ref;
2075 req = chain[i];
2076 state = tevent_req_data(req, struct smbXcli_req_state);
2078 smbXcli_req_unset_pending(req);
2081 * as we finish multiple requests here
2082 * we need to defer the callbacks as
2083 * they could destroy our current stack state.
2085 tevent_req_defer_callback(req, state->ev);
2087 if (i >= num_responses) {
2088 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2089 continue;
2092 if (state->smb1.recv_iov == NULL) {
2094 * For requests with more than
2095 * one response, we have to readd the
2096 * recv_iov array.
2098 state->smb1.recv_iov = talloc_zero_array(state,
2099 struct iovec,
2101 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2102 continue;
2106 state->smb1.recv_cmd = cmd;
2108 if (i == (num_responses - 1)) {
2110 * The last request in the chain gets the status
2112 state->smb1.recv_status = status;
2113 } else {
2114 cmd = CVAL(cur[0].iov_base, 0);
2115 state->smb1.recv_status = NT_STATUS_OK;
2118 state->inbuf = inbuf;
2121 * Note: here we use talloc_reference() in a way
2122 * that does not expose it to the caller.
2124 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2125 if (tevent_req_nomem(inbuf_ref, req)) {
2126 continue;
2129 /* copy the related buffers */
2130 state->smb1.recv_iov[0] = iov[0];
2131 state->smb1.recv_iov[1] = cur[0];
2132 state->smb1.recv_iov[2] = cur[1];
2134 tevent_req_done(req);
2137 return NT_STATUS_RETRY;
2140 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2141 TALLOC_CTX *mem_ctx,
2142 struct iovec **piov,
2143 uint8_t **phdr,
2144 uint8_t *pwct,
2145 uint16_t **pvwv,
2146 uint32_t *pvwv_offset,
2147 uint32_t *pnum_bytes,
2148 uint8_t **pbytes,
2149 uint32_t *pbytes_offset,
2150 uint8_t **pinbuf,
2151 const struct smb1cli_req_expected_response *expected,
2152 size_t num_expected)
2154 struct smbXcli_req_state *state =
2155 tevent_req_data(req,
2156 struct smbXcli_req_state);
2157 NTSTATUS status = NT_STATUS_OK;
2158 struct iovec *recv_iov = NULL;
2159 uint8_t *hdr = NULL;
2160 uint8_t wct = 0;
2161 uint32_t vwv_offset = 0;
2162 uint16_t *vwv = NULL;
2163 uint32_t num_bytes = 0;
2164 uint32_t bytes_offset = 0;
2165 uint8_t *bytes = NULL;
2166 size_t i;
2167 bool found_status = false;
2168 bool found_size = false;
2170 if (piov != NULL) {
2171 *piov = NULL;
2173 if (phdr != NULL) {
2174 *phdr = 0;
2176 if (pwct != NULL) {
2177 *pwct = 0;
2179 if (pvwv != NULL) {
2180 *pvwv = NULL;
2182 if (pvwv_offset != NULL) {
2183 *pvwv_offset = 0;
2185 if (pnum_bytes != NULL) {
2186 *pnum_bytes = 0;
2188 if (pbytes != NULL) {
2189 *pbytes = NULL;
2191 if (pbytes_offset != NULL) {
2192 *pbytes_offset = 0;
2194 if (pinbuf != NULL) {
2195 *pinbuf = NULL;
2198 if (state->inbuf != NULL) {
2199 recv_iov = state->smb1.recv_iov;
2200 state->smb1.recv_iov = NULL;
2201 if (state->smb1.recv_cmd != SMBreadBraw) {
2202 hdr = (uint8_t *)recv_iov[0].iov_base;
2203 wct = recv_iov[1].iov_len/2;
2204 vwv = (uint16_t *)recv_iov[1].iov_base;
2205 vwv_offset = PTR_DIFF(vwv, hdr);
2206 num_bytes = recv_iov[2].iov_len;
2207 bytes = (uint8_t *)recv_iov[2].iov_base;
2208 bytes_offset = PTR_DIFF(bytes, hdr);
2212 if (tevent_req_is_nterror(req, &status)) {
2213 for (i=0; i < num_expected; i++) {
2214 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2215 found_status = true;
2216 break;
2220 if (found_status) {
2221 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2224 return status;
2227 if (num_expected == 0) {
2228 found_status = true;
2229 found_size = true;
2232 status = state->smb1.recv_status;
2234 for (i=0; i < num_expected; i++) {
2235 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2236 continue;
2239 found_status = true;
2240 if (expected[i].wct == 0) {
2241 found_size = true;
2242 break;
2245 if (expected[i].wct == wct) {
2246 found_size = true;
2247 break;
2251 if (!found_status) {
2252 return status;
2255 if (!found_size) {
2256 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2259 if (piov != NULL) {
2260 *piov = talloc_move(mem_ctx, &recv_iov);
2263 if (phdr != NULL) {
2264 *phdr = hdr;
2266 if (pwct != NULL) {
2267 *pwct = wct;
2269 if (pvwv != NULL) {
2270 *pvwv = vwv;
2272 if (pvwv_offset != NULL) {
2273 *pvwv_offset = vwv_offset;
2275 if (pnum_bytes != NULL) {
2276 *pnum_bytes = num_bytes;
2278 if (pbytes != NULL) {
2279 *pbytes = bytes;
2281 if (pbytes_offset != NULL) {
2282 *pbytes_offset = bytes_offset;
2284 if (pinbuf != NULL) {
2285 *pinbuf = state->inbuf;
2288 return status;
2291 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2293 size_t wct_ofs;
2294 int i;
2296 wct_ofs = HDR_WCT;
2298 for (i=0; i<num_reqs; i++) {
2299 struct smbXcli_req_state *state;
2300 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2301 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2302 state->smb1.iov_count-2);
2303 wct_ofs = (wct_ofs + 3) & ~3;
2305 return wct_ofs;
2308 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2310 struct smbXcli_req_state *first_state =
2311 tevent_req_data(reqs[0],
2312 struct smbXcli_req_state);
2313 struct smbXcli_req_state *state;
2314 size_t wct_offset;
2315 size_t chain_padding = 0;
2316 int i, iovlen;
2317 struct iovec *iov = NULL;
2318 struct iovec *this_iov;
2319 NTSTATUS status;
2320 size_t nbt_len;
2322 if (num_reqs == 1) {
2323 return smb1cli_req_writev_submit(reqs[0], first_state,
2324 first_state->smb1.iov,
2325 first_state->smb1.iov_count);
2328 iovlen = 0;
2329 for (i=0; i<num_reqs; i++) {
2330 if (!tevent_req_is_in_progress(reqs[i])) {
2331 return NT_STATUS_INTERNAL_ERROR;
2334 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2336 if (state->smb1.iov_count < 4) {
2337 return NT_STATUS_INVALID_PARAMETER_MIX;
2340 if (i == 0) {
2342 * The NBT and SMB header
2344 iovlen += 2;
2345 } else {
2347 * Chain padding
2349 iovlen += 1;
2353 * words and bytes
2355 iovlen += state->smb1.iov_count - 2;
2358 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2359 if (iov == NULL) {
2360 return NT_STATUS_NO_MEMORY;
2363 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2364 first_state, reqs, sizeof(*reqs) * num_reqs);
2365 if (first_state->smb1.chained_requests == NULL) {
2366 TALLOC_FREE(iov);
2367 return NT_STATUS_NO_MEMORY;
2370 wct_offset = HDR_WCT;
2371 this_iov = iov;
2373 for (i=0; i<num_reqs; i++) {
2374 size_t next_padding = 0;
2375 uint16_t *vwv;
2377 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2379 if (i < num_reqs-1) {
2380 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2381 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2382 TALLOC_FREE(iov);
2383 TALLOC_FREE(first_state->smb1.chained_requests);
2384 return NT_STATUS_INVALID_PARAMETER_MIX;
2388 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2389 state->smb1.iov_count-2) + 1;
2390 if ((wct_offset % 4) != 0) {
2391 next_padding = 4 - (wct_offset % 4);
2393 wct_offset += next_padding;
2394 vwv = state->smb1.vwv;
2396 if (i < num_reqs-1) {
2397 struct smbXcli_req_state *next_state =
2398 tevent_req_data(reqs[i+1],
2399 struct smbXcli_req_state);
2400 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2401 SCVAL(vwv+0, 1, 0);
2402 SSVAL(vwv+1, 0, wct_offset);
2403 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2404 /* properly end the chain */
2405 SCVAL(vwv+0, 0, 0xff);
2406 SCVAL(vwv+0, 1, 0xff);
2407 SSVAL(vwv+1, 0, 0);
2410 if (i == 0) {
2412 * The NBT and SMB header
2414 this_iov[0] = state->smb1.iov[0];
2415 this_iov[1] = state->smb1.iov[1];
2416 this_iov += 2;
2417 } else {
2419 * This one is a bit subtle. We have to add
2420 * chain_padding bytes between the requests, and we
2421 * have to also include the wct field of the
2422 * subsequent requests. We use the subsequent header
2423 * for the padding, it contains the wct field in its
2424 * last byte.
2426 this_iov[0].iov_len = chain_padding+1;
2427 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2428 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2429 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2430 this_iov += 1;
2434 * copy the words and bytes
2436 memcpy(this_iov, state->smb1.iov+2,
2437 sizeof(struct iovec) * (state->smb1.iov_count-2));
2438 this_iov += state->smb1.iov_count - 2;
2439 chain_padding = next_padding;
2442 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2443 if (nbt_len > first_state->conn->smb1.max_xmit) {
2444 TALLOC_FREE(iov);
2445 TALLOC_FREE(first_state->smb1.chained_requests);
2446 return NT_STATUS_INVALID_PARAMETER_MIX;
2449 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2450 if (!NT_STATUS_IS_OK(status)) {
2451 TALLOC_FREE(iov);
2452 TALLOC_FREE(first_state->smb1.chained_requests);
2453 return status;
2456 return NT_STATUS_OK;
2459 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2461 return ((tevent_queue_length(conn->outgoing) != 0)
2462 || (talloc_array_length(conn->pending) != 0));
2465 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2467 if (conn->protocol >= PROTOCOL_SMB2_02) {
2468 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2471 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2474 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2476 uint16_t credits = 1;
2478 if (conn->smb2.cur_credits == 0) {
2479 if (max_dyn_len != NULL) {
2480 *max_dyn_len = 0;
2482 return false;
2485 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2486 credits = conn->smb2.cur_credits;
2489 if (max_dyn_len != NULL) {
2490 *max_dyn_len = credits * 65536;
2493 return true;
2496 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2498 return conn->smb2.server.capabilities;
2501 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2503 return conn->smb2.server.security_mode;
2506 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2508 return conn->smb2.server.max_trans_size;
2511 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2513 return conn->smb2.server.max_read_size;
2516 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2518 return conn->smb2.server.max_write_size;
2521 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2522 uint16_t max_credits)
2524 conn->smb2.max_credits = max_credits;
2527 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2529 static bool smb2cli_req_cancel(struct tevent_req *req)
2531 struct smbXcli_req_state *state =
2532 tevent_req_data(req,
2533 struct smbXcli_req_state);
2534 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2535 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2536 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2537 struct smbXcli_tcon *tcon = state->tcon;
2538 struct smbXcli_session *session = state->session;
2539 uint8_t *fixed = state->smb2.pad;
2540 uint16_t fixed_len = 4;
2541 struct tevent_req *subreq;
2542 struct smbXcli_req_state *substate;
2543 NTSTATUS status;
2545 SSVAL(fixed, 0, 0x04);
2546 SSVAL(fixed, 2, 0);
2548 subreq = smb2cli_req_create(state, state->ev,
2549 state->conn,
2550 SMB2_OP_CANCEL,
2551 flags, 0,
2552 0, /* timeout */
2553 tcon, session,
2554 fixed, fixed_len,
2555 NULL, 0, 0);
2556 if (subreq == NULL) {
2557 return false;
2559 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2562 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2563 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2565 flags &= SMB2_HDR_FLAG_ASYNC;
2567 if (flags & SMB2_HDR_FLAG_ASYNC) {
2568 mid = 0;
2571 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2572 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2573 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2575 status = smb2cli_req_compound_submit(&subreq, 1);
2576 if (!NT_STATUS_IS_OK(status)) {
2577 TALLOC_FREE(subreq);
2578 return false;
2581 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2583 return true;
2586 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2588 /* we do not care about the result */
2589 TALLOC_FREE(subreq);
2592 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2593 struct tevent_context *ev,
2594 struct smbXcli_conn *conn,
2595 uint16_t cmd,
2596 uint32_t additional_flags,
2597 uint32_t clear_flags,
2598 uint32_t timeout_msec,
2599 struct smbXcli_tcon *tcon,
2600 struct smbXcli_session *session,
2601 const uint8_t *fixed,
2602 uint16_t fixed_len,
2603 const uint8_t *dyn,
2604 uint32_t dyn_len,
2605 uint32_t max_dyn_len)
2607 struct tevent_req *req;
2608 struct smbXcli_req_state *state;
2609 uint32_t flags = 0;
2610 uint32_t tid = 0;
2611 uint64_t uid = 0;
2612 bool use_channel_sequence = false;
2613 uint16_t channel_sequence = 0;
2615 req = tevent_req_create(mem_ctx, &state,
2616 struct smbXcli_req_state);
2617 if (req == NULL) {
2618 return NULL;
2621 state->ev = ev;
2622 state->conn = conn;
2623 state->session = session;
2624 state->tcon = tcon;
2626 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2627 use_channel_sequence = true;
2628 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2629 use_channel_sequence = true;
2632 if (session) {
2633 uid = session->smb2->session_id;
2635 if (use_channel_sequence) {
2636 channel_sequence = session->smb2->channel_sequence;
2639 state->smb2.should_sign = session->smb2->should_sign;
2640 state->smb2.should_encrypt = session->smb2->should_encrypt;
2642 if (cmd == SMB2_OP_SESSSETUP &&
2643 session->smb2->signing_key.length != 0) {
2644 state->smb2.should_sign = true;
2647 if (cmd == SMB2_OP_SESSSETUP &&
2648 session->smb2_channel.signing_key.length == 0) {
2649 state->smb2.should_encrypt = false;
2653 if (tcon) {
2654 tid = tcon->smb2.tcon_id;
2656 if (tcon->smb2.should_encrypt) {
2657 state->smb2.should_encrypt = true;
2661 if (state->smb2.should_encrypt) {
2662 state->smb2.should_sign = false;
2665 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2666 if (state->smb2.recv_iov == NULL) {
2667 TALLOC_FREE(req);
2668 return NULL;
2671 flags |= additional_flags;
2672 flags &= ~clear_flags;
2674 state->smb2.fixed = fixed;
2675 state->smb2.fixed_len = fixed_len;
2676 state->smb2.dyn = dyn;
2677 state->smb2.dyn_len = dyn_len;
2678 state->smb2.max_dyn_len = max_dyn_len;
2680 if (state->smb2.should_encrypt) {
2681 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2682 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2685 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2686 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2687 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2688 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2689 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2690 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2691 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2692 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2694 switch (cmd) {
2695 case SMB2_OP_CANCEL:
2696 state->one_way = true;
2697 break;
2698 case SMB2_OP_BREAK:
2700 * If this is a dummy request, it will have
2701 * UINT64_MAX as message id.
2702 * If we send on break acknowledgement,
2703 * this gets overwritten later.
2705 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2706 break;
2709 if (timeout_msec > 0) {
2710 struct timeval endtime;
2712 endtime = timeval_current_ofs_msec(timeout_msec);
2713 if (!tevent_req_set_endtime(req, ev, endtime)) {
2714 return req;
2718 return req;
2721 void smb2cli_req_set_notify_async(struct tevent_req *req)
2723 struct smbXcli_req_state *state =
2724 tevent_req_data(req,
2725 struct smbXcli_req_state);
2727 state->smb2.notify_async = true;
2730 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2731 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2732 TALLOC_CTX *tmp_mem,
2733 uint8_t *inbuf);
2735 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2736 int num_reqs)
2738 struct smbXcli_req_state *state;
2739 struct tevent_req *subreq;
2740 struct iovec *iov;
2741 int i, num_iov, nbt_len;
2742 int tf_iov = -1;
2743 const DATA_BLOB *encryption_key = NULL;
2744 uint64_t encryption_session_id = 0;
2747 * 1 for the nbt length, optional TRANSFORM
2748 * per request: HDR, fixed, dyn, padding
2749 * -1 because the last one does not need padding
2752 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2753 if (iov == NULL) {
2754 return NT_STATUS_NO_MEMORY;
2757 num_iov = 1;
2758 nbt_len = 0;
2761 * the session of the first request that requires encryption
2762 * specifies the encryption key.
2764 for (i=0; i<num_reqs; i++) {
2765 if (!tevent_req_is_in_progress(reqs[i])) {
2766 return NT_STATUS_INTERNAL_ERROR;
2769 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2771 if (!smbXcli_conn_is_connected(state->conn)) {
2772 return NT_STATUS_CONNECTION_DISCONNECTED;
2775 if ((state->conn->protocol != PROTOCOL_NONE) &&
2776 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2777 return NT_STATUS_REVISION_MISMATCH;
2780 if (state->session == NULL) {
2781 continue;
2784 if (!state->smb2.should_encrypt) {
2785 continue;
2788 encryption_key = &state->session->smb2->encryption_key;
2789 if (encryption_key->length == 0) {
2790 return NT_STATUS_INVALID_PARAMETER_MIX;
2793 encryption_session_id = state->session->smb2->session_id;
2795 tf_iov = num_iov;
2796 iov[num_iov].iov_base = state->smb2.transform;
2797 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2798 num_iov += 1;
2800 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2801 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2802 state->session->smb2->nonce_low);
2803 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2804 state->session->smb2->nonce_high);
2805 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2806 encryption_session_id);
2808 state->session->smb2->nonce_low += 1;
2809 if (state->session->smb2->nonce_low == 0) {
2810 state->session->smb2->nonce_high += 1;
2811 state->session->smb2->nonce_low += 1;
2814 nbt_len += SMB2_TF_HDR_SIZE;
2815 break;
2818 for (i=0; i<num_reqs; i++) {
2819 int hdr_iov;
2820 size_t reqlen;
2821 bool ret;
2822 uint16_t opcode;
2823 uint64_t avail;
2824 uint16_t charge;
2825 uint16_t credits;
2826 uint64_t mid;
2827 const DATA_BLOB *signing_key = NULL;
2829 if (!tevent_req_is_in_progress(reqs[i])) {
2830 return NT_STATUS_INTERNAL_ERROR;
2833 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2835 if (!smbXcli_conn_is_connected(state->conn)) {
2836 return NT_STATUS_CONNECTION_DISCONNECTED;
2839 if ((state->conn->protocol != PROTOCOL_NONE) &&
2840 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2841 return NT_STATUS_REVISION_MISMATCH;
2844 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2845 if (opcode == SMB2_OP_CANCEL) {
2846 goto skip_credits;
2849 avail = UINT64_MAX - state->conn->smb2.mid;
2850 if (avail < 1) {
2851 return NT_STATUS_CONNECTION_ABORTED;
2854 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2855 uint32_t max_dyn_len = 1;
2857 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2858 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2860 charge = (max_dyn_len - 1)/ 65536 + 1;
2861 } else {
2862 charge = 1;
2865 charge = MAX(state->smb2.credit_charge, charge);
2867 avail = MIN(avail, state->conn->smb2.cur_credits);
2868 if (avail < charge) {
2869 return NT_STATUS_INTERNAL_ERROR;
2872 credits = 0;
2873 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2874 credits = state->conn->smb2.max_credits -
2875 state->conn->smb2.cur_credits;
2877 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2878 credits += 1;
2881 mid = state->conn->smb2.mid;
2882 state->conn->smb2.mid += charge;
2883 state->conn->smb2.cur_credits -= charge;
2885 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2886 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2888 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2889 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2891 skip_credits:
2892 if (state->session && encryption_key == NULL) {
2894 * We prefer the channel signing key if it is
2895 * already there.
2897 if (state->smb2.should_sign) {
2898 signing_key = &state->session->smb2_channel.signing_key;
2902 * If it is a channel binding, we already have the main
2903 * signing key and try that one.
2905 if (signing_key && signing_key->length == 0) {
2906 signing_key = &state->session->smb2->signing_key;
2910 * If we do not have any session key yet, we skip the
2911 * signing of SMB2_OP_SESSSETUP requests.
2913 if (signing_key && signing_key->length == 0) {
2914 signing_key = NULL;
2918 hdr_iov = num_iov;
2919 iov[num_iov].iov_base = state->smb2.hdr;
2920 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2921 num_iov += 1;
2923 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2924 iov[num_iov].iov_len = state->smb2.fixed_len;
2925 num_iov += 1;
2927 if (state->smb2.dyn != NULL) {
2928 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2929 iov[num_iov].iov_len = state->smb2.dyn_len;
2930 num_iov += 1;
2933 reqlen = sizeof(state->smb2.hdr);
2934 reqlen += state->smb2.fixed_len;
2935 reqlen += state->smb2.dyn_len;
2937 if (i < num_reqs-1) {
2938 if ((reqlen % 8) > 0) {
2939 uint8_t pad = 8 - (reqlen % 8);
2940 iov[num_iov].iov_base = state->smb2.pad;
2941 iov[num_iov].iov_len = pad;
2942 num_iov += 1;
2943 reqlen += pad;
2945 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2948 state->smb2.encryption_session_id = encryption_session_id;
2950 if (signing_key != NULL) {
2951 NTSTATUS status;
2953 status = smb2_signing_sign_pdu(*signing_key,
2954 state->session->conn->protocol,
2955 &iov[hdr_iov], num_iov - hdr_iov);
2956 if (!NT_STATUS_IS_OK(status)) {
2957 return status;
2961 nbt_len += reqlen;
2963 ret = smbXcli_req_set_pending(reqs[i]);
2964 if (!ret) {
2965 return NT_STATUS_NO_MEMORY;
2969 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2970 _smb_setlen_tcp(state->length_hdr, nbt_len);
2971 iov[0].iov_base = state->length_hdr;
2972 iov[0].iov_len = sizeof(state->length_hdr);
2974 if (encryption_key != NULL) {
2975 NTSTATUS status;
2976 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2977 uint8_t *buf;
2978 int vi;
2980 buf = talloc_array(iov, uint8_t, buflen);
2981 if (buf == NULL) {
2982 return NT_STATUS_NO_MEMORY;
2986 * We copy the buffers before encrypting them,
2987 * this is at least currently needed for the
2988 * to keep state->smb2.hdr.
2990 * Also the callers may expect there buffers
2991 * to be const.
2993 for (vi = tf_iov + 1; vi < num_iov; vi++) {
2994 struct iovec *v = &iov[vi];
2995 const uint8_t *o = (const uint8_t *)v->iov_base;
2997 memcpy(buf, o, v->iov_len);
2998 v->iov_base = (void *)buf;
2999 buf += v->iov_len;
3002 status = smb2_signing_encrypt_pdu(*encryption_key,
3003 state->conn->protocol,
3004 &iov[tf_iov], num_iov - tf_iov);
3005 if (!NT_STATUS_IS_OK(status)) {
3006 return status;
3010 if (state->conn->dispatch_incoming == NULL) {
3011 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3014 subreq = writev_send(state, state->ev, state->conn->outgoing,
3015 state->conn->write_fd, false, iov, num_iov);
3016 if (subreq == NULL) {
3017 return NT_STATUS_NO_MEMORY;
3019 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3020 return NT_STATUS_OK;
3023 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3025 struct smbXcli_req_state *state =
3026 tevent_req_data(req,
3027 struct smbXcli_req_state);
3029 state->smb2.credit_charge = charge;
3032 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3033 struct tevent_context *ev,
3034 struct smbXcli_conn *conn,
3035 uint16_t cmd,
3036 uint32_t additional_flags,
3037 uint32_t clear_flags,
3038 uint32_t timeout_msec,
3039 struct smbXcli_tcon *tcon,
3040 struct smbXcli_session *session,
3041 const uint8_t *fixed,
3042 uint16_t fixed_len,
3043 const uint8_t *dyn,
3044 uint32_t dyn_len,
3045 uint32_t max_dyn_len)
3047 struct tevent_req *req;
3048 NTSTATUS status;
3050 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3051 additional_flags, clear_flags,
3052 timeout_msec,
3053 tcon, session,
3054 fixed, fixed_len,
3055 dyn, dyn_len,
3056 max_dyn_len);
3057 if (req == NULL) {
3058 return NULL;
3060 if (!tevent_req_is_in_progress(req)) {
3061 return tevent_req_post(req, ev);
3063 status = smb2cli_req_compound_submit(&req, 1);
3064 if (tevent_req_nterror(req, status)) {
3065 return tevent_req_post(req, ev);
3067 return req;
3070 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3072 struct tevent_req *req =
3073 tevent_req_callback_data(subreq,
3074 struct tevent_req);
3075 struct smbXcli_req_state *state =
3076 tevent_req_data(req,
3077 struct smbXcli_req_state);
3078 ssize_t nwritten;
3079 int err;
3081 nwritten = writev_recv(subreq, &err);
3082 TALLOC_FREE(subreq);
3083 if (nwritten == -1) {
3084 /* here, we need to notify all pending requests */
3085 NTSTATUS status = map_nt_error_from_unix_common(err);
3086 smbXcli_conn_disconnect(state->conn, status);
3087 return;
3091 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3092 uint8_t *buf,
3093 size_t buflen,
3094 TALLOC_CTX *mem_ctx,
3095 struct iovec **piov, int *pnum_iov)
3097 struct iovec *iov;
3098 int num_iov = 0;
3099 size_t taken = 0;
3100 uint8_t *first_hdr = buf;
3101 size_t verified_buflen = 0;
3102 uint8_t *tf = NULL;
3103 size_t tf_len = 0;
3105 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3106 if (iov == NULL) {
3107 return NT_STATUS_NO_MEMORY;
3110 while (taken < buflen) {
3111 size_t len = buflen - taken;
3112 uint8_t *hdr = first_hdr + taken;
3113 struct iovec *cur;
3114 size_t full_size;
3115 size_t next_command_ofs;
3116 uint16_t body_size;
3117 struct iovec *iov_tmp;
3119 if (verified_buflen > taken) {
3120 len = verified_buflen - taken;
3121 } else {
3122 tf = NULL;
3123 tf_len = 0;
3126 if (len < 4) {
3127 DEBUG(10, ("%d bytes left, expected at least %d\n",
3128 (int)len, 4));
3129 goto inval;
3131 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3132 struct smbXcli_session *s;
3133 uint64_t uid;
3134 struct iovec tf_iov[2];
3135 size_t enc_len;
3136 NTSTATUS status;
3138 if (len < SMB2_TF_HDR_SIZE) {
3139 DEBUG(10, ("%d bytes left, expected at least %d\n",
3140 (int)len, SMB2_TF_HDR_SIZE));
3141 goto inval;
3143 tf = hdr;
3144 tf_len = SMB2_TF_HDR_SIZE;
3145 taken += tf_len;
3147 hdr = first_hdr + taken;
3148 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3149 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3151 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3152 DEBUG(10, ("%d bytes left, expected at least %d\n",
3153 (int)len,
3154 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3155 goto inval;
3158 s = conn->sessions;
3159 for (; s; s = s->next) {
3160 if (s->smb2->session_id != uid) {
3161 continue;
3163 break;
3166 if (s == NULL) {
3167 DEBUG(10, ("unknown session_id %llu\n",
3168 (unsigned long long)uid));
3169 goto inval;
3172 tf_iov[0].iov_base = (void *)tf;
3173 tf_iov[0].iov_len = tf_len;
3174 tf_iov[1].iov_base = (void *)hdr;
3175 tf_iov[1].iov_len = enc_len;
3177 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3178 conn->protocol,
3179 tf_iov, 2);
3180 if (!NT_STATUS_IS_OK(status)) {
3181 TALLOC_FREE(iov);
3182 return status;
3185 verified_buflen = taken + enc_len;
3186 len = enc_len;
3190 * We need the header plus the body length field
3193 if (len < SMB2_HDR_BODY + 2) {
3194 DEBUG(10, ("%d bytes left, expected at least %d\n",
3195 (int)len, SMB2_HDR_BODY));
3196 goto inval;
3198 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3199 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3200 IVAL(hdr, 0)));
3201 goto inval;
3203 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3204 DEBUG(10, ("Got HDR len %d, expected %d\n",
3205 SVAL(hdr, 4), SMB2_HDR_BODY));
3206 goto inval;
3209 full_size = len;
3210 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3211 body_size = SVAL(hdr, SMB2_HDR_BODY);
3213 if (next_command_ofs != 0) {
3214 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3215 goto inval;
3217 if (next_command_ofs > full_size) {
3218 goto inval;
3220 full_size = next_command_ofs;
3222 if (body_size < 2) {
3223 goto inval;
3225 body_size &= 0xfffe;
3227 if (body_size > (full_size - SMB2_HDR_BODY)) {
3228 goto inval;
3231 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3232 num_iov + 4);
3233 if (iov_tmp == NULL) {
3234 TALLOC_FREE(iov);
3235 return NT_STATUS_NO_MEMORY;
3237 iov = iov_tmp;
3238 cur = &iov[num_iov];
3239 num_iov += 4;
3241 cur[0].iov_base = tf;
3242 cur[0].iov_len = tf_len;
3243 cur[1].iov_base = hdr;
3244 cur[1].iov_len = SMB2_HDR_BODY;
3245 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3246 cur[2].iov_len = body_size;
3247 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3248 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3250 taken += full_size;
3253 *piov = iov;
3254 *pnum_iov = num_iov;
3255 return NT_STATUS_OK;
3257 inval:
3258 TALLOC_FREE(iov);
3259 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3262 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3263 uint64_t mid)
3265 size_t num_pending = talloc_array_length(conn->pending);
3266 size_t i;
3268 for (i=0; i<num_pending; i++) {
3269 struct tevent_req *req = conn->pending[i];
3270 struct smbXcli_req_state *state =
3271 tevent_req_data(req,
3272 struct smbXcli_req_state);
3274 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3275 return req;
3278 return NULL;
3281 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3282 TALLOC_CTX *tmp_mem,
3283 uint8_t *inbuf)
3285 struct tevent_req *req;
3286 struct smbXcli_req_state *state = NULL;
3287 struct iovec *iov;
3288 int i, num_iov;
3289 NTSTATUS status;
3290 bool defer = true;
3291 struct smbXcli_session *last_session = NULL;
3292 size_t inbuf_len = smb_len_tcp(inbuf);
3294 status = smb2cli_inbuf_parse_compound(conn,
3295 inbuf + NBT_HDR_SIZE,
3296 inbuf_len,
3297 tmp_mem,
3298 &iov, &num_iov);
3299 if (!NT_STATUS_IS_OK(status)) {
3300 return status;
3303 for (i=0; i<num_iov; i+=4) {
3304 uint8_t *inbuf_ref = NULL;
3305 struct iovec *cur = &iov[i];
3306 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3307 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3308 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3309 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3310 uint16_t req_opcode;
3311 uint32_t req_flags;
3312 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3313 uint32_t new_credits;
3314 struct smbXcli_session *session = NULL;
3315 const DATA_BLOB *signing_key = NULL;
3316 bool was_encrypted = false;
3318 new_credits = conn->smb2.cur_credits;
3319 new_credits += credits;
3320 if (new_credits > UINT16_MAX) {
3321 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3323 conn->smb2.cur_credits += credits;
3325 req = smb2cli_conn_find_pending(conn, mid);
3326 if (req == NULL) {
3327 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3329 state = tevent_req_data(req, struct smbXcli_req_state);
3331 state->smb2.got_async = false;
3333 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3334 if (opcode != req_opcode) {
3335 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3337 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3339 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3340 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3343 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3344 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3345 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3346 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3349 * async interim responses are not signed,
3350 * even if the SMB2_HDR_FLAG_SIGNED flag
3351 * is set.
3353 req_flags |= SMB2_HDR_FLAG_ASYNC;
3354 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3355 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3357 if (state->smb2.notify_async) {
3358 state->smb2.got_async = true;
3359 tevent_req_defer_callback(req, state->ev);
3360 tevent_req_notify_callback(req);
3362 continue;
3365 session = state->session;
3366 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3367 session = last_session;
3369 last_session = session;
3371 if (state->smb2.should_sign) {
3372 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3373 return NT_STATUS_ACCESS_DENIED;
3377 if (flags & SMB2_HDR_FLAG_SIGNED) {
3378 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3380 if (session == NULL) {
3381 struct smbXcli_session *s;
3383 s = state->conn->sessions;
3384 for (; s; s = s->next) {
3385 if (s->smb2->session_id != uid) {
3386 continue;
3389 session = s;
3390 break;
3394 if (session == NULL) {
3395 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3398 last_session = session;
3399 signing_key = &session->smb2_channel.signing_key;
3402 if (opcode == SMB2_OP_SESSSETUP) {
3404 * We prefer the channel signing key, if it is
3405 * already there.
3407 * If we do not have a channel signing key yet,
3408 * we try the main signing key, if it is not
3409 * the final response.
3411 if (signing_key && signing_key->length == 0 &&
3412 !NT_STATUS_IS_OK(status)) {
3413 signing_key = &session->smb2->signing_key;
3416 if (signing_key && signing_key->length == 0) {
3418 * If we do not have a session key to
3419 * verify the signature, we defer the
3420 * signing check to the caller.
3422 * The caller gets NT_STATUS_OK, it
3423 * has to call
3424 * smb2cli_session_set_session_key()
3425 * or
3426 * smb2cli_session_set_channel_key()
3427 * which will check the signature
3428 * with the channel signing key.
3430 signing_key = NULL;
3434 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3435 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3436 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3439 * If the response was encrypted in a SMB2_TRANSFORM
3440 * pdu, which belongs to the correct session,
3441 * we do not need to do signing checks
3443 * It could be the session the response belongs to
3444 * or the session that was used to encrypt the
3445 * SMB2_TRANSFORM request.
3447 if ((session && session->smb2->session_id == uid) ||
3448 (state->smb2.encryption_session_id == uid)) {
3449 signing_key = NULL;
3450 was_encrypted = true;
3454 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3456 * if the server returns NT_STATUS_USER_SESSION_DELETED
3457 * the response is not signed and we should
3458 * propagate the NT_STATUS_USER_SESSION_DELETED
3459 * status to the caller.
3461 state->smb2.signing_skipped = true;
3462 signing_key = NULL;
3465 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3467 * if the server returns
3468 * NT_STATUS_INVALID_PARAMETER
3469 * the response might not be encrypted.
3471 if (state->smb2.should_encrypt && !was_encrypted) {
3472 state->smb2.signing_skipped = true;
3473 signing_key = NULL;
3477 if (state->smb2.should_encrypt && !was_encrypted) {
3478 if (!state->smb2.signing_skipped) {
3479 return NT_STATUS_ACCESS_DENIED;
3483 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3484 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3485 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3487 * if the server returns
3488 * NT_STATUS_NETWORK_NAME_DELETED
3489 * NT_STATUS_FILE_CLOSED
3490 * NT_STATUS_INVALID_PARAMETER
3491 * the response might not be signed
3492 * as this happens before the signing checks.
3494 * If server echos the signature (or all zeros)
3495 * we should report the status from the server
3496 * to the caller.
3498 if (signing_key) {
3499 int cmp;
3501 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3502 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3503 16);
3504 if (cmp == 0) {
3505 state->smb2.signing_skipped = true;
3506 signing_key = NULL;
3509 if (signing_key) {
3510 int cmp;
3511 static const uint8_t zeros[16];
3513 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3514 zeros,
3515 16);
3516 if (cmp == 0) {
3517 state->smb2.signing_skipped = true;
3518 signing_key = NULL;
3523 if (signing_key) {
3524 status = smb2_signing_check_pdu(*signing_key,
3525 state->conn->protocol,
3526 &cur[1], 3);
3527 if (!NT_STATUS_IS_OK(status)) {
3529 * If the signing check fails, we disconnect
3530 * the connection.
3532 return status;
3536 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3537 (session != NULL) && session->disconnect_expired)
3540 * this should be a short term hack
3541 * until the upper layers have implemented
3542 * re-authentication.
3544 return status;
3547 smbXcli_req_unset_pending(req);
3550 * There might be more than one response
3551 * we need to defer the notifications
3553 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3554 defer = false;
3557 if (defer) {
3558 tevent_req_defer_callback(req, state->ev);
3562 * Note: here we use talloc_reference() in a way
3563 * that does not expose it to the caller.
3565 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3566 if (tevent_req_nomem(inbuf_ref, req)) {
3567 continue;
3570 /* copy the related buffers */
3571 state->smb2.recv_iov[0] = cur[1];
3572 state->smb2.recv_iov[1] = cur[2];
3573 state->smb2.recv_iov[2] = cur[3];
3575 tevent_req_done(req);
3578 if (defer) {
3579 return NT_STATUS_RETRY;
3582 return NT_STATUS_OK;
3585 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3586 struct iovec **piov,
3587 const struct smb2cli_req_expected_response *expected,
3588 size_t num_expected)
3590 struct smbXcli_req_state *state =
3591 tevent_req_data(req,
3592 struct smbXcli_req_state);
3593 NTSTATUS status;
3594 size_t body_size;
3595 bool found_status = false;
3596 bool found_size = false;
3597 size_t i;
3599 if (piov != NULL) {
3600 *piov = NULL;
3603 if (state->smb2.got_async) {
3604 return STATUS_PENDING;
3607 if (tevent_req_is_nterror(req, &status)) {
3608 for (i=0; i < num_expected; i++) {
3609 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3610 found_status = true;
3611 break;
3615 if (found_status) {
3616 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3619 return status;
3622 if (num_expected == 0) {
3623 found_status = true;
3624 found_size = true;
3627 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3628 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3630 for (i=0; i < num_expected; i++) {
3631 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3632 continue;
3635 found_status = true;
3636 if (expected[i].body_size == 0) {
3637 found_size = true;
3638 break;
3641 if (expected[i].body_size == body_size) {
3642 found_size = true;
3643 break;
3647 if (!found_status) {
3648 return status;
3651 if (state->smb2.signing_skipped) {
3652 if (num_expected > 0) {
3653 return NT_STATUS_ACCESS_DENIED;
3655 if (!NT_STATUS_IS_ERR(status)) {
3656 return NT_STATUS_ACCESS_DENIED;
3660 if (!found_size) {
3661 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3664 if (piov != NULL) {
3665 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3668 return status;
3671 static const struct {
3672 enum protocol_types proto;
3673 const char *smb1_name;
3674 } smb1cli_prots[] = {
3675 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3676 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3677 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3678 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3679 {PROTOCOL_LANMAN2, "LM1.2X002"},
3680 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3681 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3682 {PROTOCOL_LANMAN2, "Samba"},
3683 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3684 {PROTOCOL_NT1, "NT LM 0.12"},
3685 {PROTOCOL_SMB2_02, "SMB 2.002"},
3686 {PROTOCOL_SMB2_10, "SMB 2.???"},
3689 static const struct {
3690 enum protocol_types proto;
3691 uint16_t smb2_dialect;
3692 } smb2cli_prots[] = {
3693 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3694 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3695 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3696 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3697 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3700 struct smbXcli_negprot_state {
3701 struct smbXcli_conn *conn;
3702 struct tevent_context *ev;
3703 uint32_t timeout_msec;
3704 enum protocol_types min_protocol;
3705 enum protocol_types max_protocol;
3707 struct {
3708 uint8_t fixed[36];
3709 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3710 } smb2;
3713 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3714 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3715 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3716 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3717 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3718 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3719 TALLOC_CTX *frame,
3720 uint8_t *inbuf);
3722 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3723 struct tevent_context *ev,
3724 struct smbXcli_conn *conn,
3725 uint32_t timeout_msec,
3726 enum protocol_types min_protocol,
3727 enum protocol_types max_protocol)
3729 struct tevent_req *req, *subreq;
3730 struct smbXcli_negprot_state *state;
3732 req = tevent_req_create(mem_ctx, &state,
3733 struct smbXcli_negprot_state);
3734 if (req == NULL) {
3735 return NULL;
3737 state->conn = conn;
3738 state->ev = ev;
3739 state->timeout_msec = timeout_msec;
3740 state->min_protocol = min_protocol;
3741 state->max_protocol = max_protocol;
3743 if (min_protocol == PROTOCOL_NONE) {
3744 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3745 return tevent_req_post(req, ev);
3748 if (max_protocol == PROTOCOL_NONE) {
3749 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3750 return tevent_req_post(req, ev);
3753 if (min_protocol > max_protocol) {
3754 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3755 return tevent_req_post(req, ev);
3758 if ((min_protocol < PROTOCOL_SMB2_02) &&
3759 (max_protocol < PROTOCOL_SMB2_02)) {
3761 * SMB1 only...
3763 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3765 subreq = smbXcli_negprot_smb1_subreq(state);
3766 if (tevent_req_nomem(subreq, req)) {
3767 return tevent_req_post(req, ev);
3769 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3770 return req;
3773 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3774 (max_protocol >= PROTOCOL_SMB2_02)) {
3776 * SMB2 only...
3778 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3780 subreq = smbXcli_negprot_smb2_subreq(state);
3781 if (tevent_req_nomem(subreq, req)) {
3782 return tevent_req_post(req, ev);
3784 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3785 return req;
3789 * We send an SMB1 negprot with the SMB2 dialects
3790 * and expect a SMB1 or a SMB2 response.
3792 * smbXcli_negprot_dispatch_incoming() will fix the
3793 * callback to match protocol of the response.
3795 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3797 subreq = smbXcli_negprot_smb1_subreq(state);
3798 if (tevent_req_nomem(subreq, req)) {
3799 return tevent_req_post(req, ev);
3801 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3802 return req;
3805 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3807 struct tevent_req *req =
3808 tevent_req_callback_data(subreq,
3809 struct tevent_req);
3810 NTSTATUS status;
3813 * we just want the low level error
3815 status = tevent_req_simple_recv_ntstatus(subreq);
3816 TALLOC_FREE(subreq);
3817 if (tevent_req_nterror(req, status)) {
3818 return;
3821 /* this should never happen */
3822 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3825 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3827 size_t i;
3828 DATA_BLOB bytes = data_blob_null;
3829 uint8_t flags;
3830 uint16_t flags2;
3832 /* setup the protocol strings */
3833 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3834 uint8_t c = 2;
3835 bool ok;
3837 if (smb1cli_prots[i].proto < state->min_protocol) {
3838 continue;
3841 if (smb1cli_prots[i].proto > state->max_protocol) {
3842 continue;
3845 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3846 if (!ok) {
3847 return NULL;
3851 * We now it is already ascii and
3852 * we want NULL termination.
3854 ok = data_blob_append(state, &bytes,
3855 smb1cli_prots[i].smb1_name,
3856 strlen(smb1cli_prots[i].smb1_name)+1);
3857 if (!ok) {
3858 return NULL;
3862 smb1cli_req_flags(state->max_protocol,
3863 state->conn->smb1.client.capabilities,
3864 SMBnegprot,
3865 0, 0, &flags,
3866 0, 0, &flags2);
3868 return smb1cli_req_send(state, state->ev, state->conn,
3869 SMBnegprot,
3870 flags, ~flags,
3871 flags2, ~flags2,
3872 state->timeout_msec,
3873 0xFFFE, 0, NULL, /* pid, tid, session */
3874 0, NULL, /* wct, vwv */
3875 bytes.length, bytes.data);
3878 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3880 struct tevent_req *req =
3881 tevent_req_callback_data(subreq,
3882 struct tevent_req);
3883 struct smbXcli_negprot_state *state =
3884 tevent_req_data(req,
3885 struct smbXcli_negprot_state);
3886 struct smbXcli_conn *conn = state->conn;
3887 struct iovec *recv_iov = NULL;
3888 uint8_t *inhdr;
3889 uint8_t wct;
3890 uint16_t *vwv;
3891 uint32_t num_bytes;
3892 uint8_t *bytes;
3893 NTSTATUS status;
3894 uint16_t protnum;
3895 size_t i;
3896 size_t num_prots = 0;
3897 uint8_t flags;
3898 uint32_t client_capabilities = conn->smb1.client.capabilities;
3899 uint32_t both_capabilities;
3900 uint32_t server_capabilities = 0;
3901 uint32_t capabilities;
3902 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3903 uint32_t server_max_xmit = 0;
3904 uint32_t max_xmit;
3905 uint32_t server_max_mux = 0;
3906 uint16_t server_security_mode = 0;
3907 uint32_t server_session_key = 0;
3908 bool server_readbraw = false;
3909 bool server_writebraw = false;
3910 bool server_lockread = false;
3911 bool server_writeunlock = false;
3912 struct GUID server_guid = GUID_zero();
3913 DATA_BLOB server_gss_blob = data_blob_null;
3914 uint8_t server_challenge[8];
3915 char *server_workgroup = NULL;
3916 char *server_name = NULL;
3917 int server_time_zone = 0;
3918 NTTIME server_system_time = 0;
3919 static const struct smb1cli_req_expected_response expected[] = {
3921 .status = NT_STATUS_OK,
3922 .wct = 0x11, /* NT1 */
3925 .status = NT_STATUS_OK,
3926 .wct = 0x0D, /* LM */
3929 .status = NT_STATUS_OK,
3930 .wct = 0x01, /* CORE */
3934 ZERO_STRUCT(server_challenge);
3936 status = smb1cli_req_recv(subreq, state,
3937 &recv_iov,
3938 &inhdr,
3939 &wct,
3940 &vwv,
3941 NULL, /* pvwv_offset */
3942 &num_bytes,
3943 &bytes,
3944 NULL, /* pbytes_offset */
3945 NULL, /* pinbuf */
3946 expected, ARRAY_SIZE(expected));
3947 TALLOC_FREE(subreq);
3948 if (tevent_req_nterror(req, status)) {
3949 return;
3952 flags = CVAL(inhdr, HDR_FLG);
3954 protnum = SVAL(vwv, 0);
3956 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3957 if (smb1cli_prots[i].proto < state->min_protocol) {
3958 continue;
3961 if (smb1cli_prots[i].proto > state->max_protocol) {
3962 continue;
3965 if (protnum != num_prots) {
3966 num_prots++;
3967 continue;
3970 conn->protocol = smb1cli_prots[i].proto;
3971 break;
3974 if (conn->protocol == PROTOCOL_NONE) {
3975 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3976 return;
3979 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3980 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3981 "and the selected protocol level doesn't support it.\n"));
3982 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3983 return;
3986 if (flags & FLAG_SUPPORT_LOCKREAD) {
3987 server_lockread = true;
3988 server_writeunlock = true;
3991 if (conn->protocol >= PROTOCOL_NT1) {
3992 const char *client_signing = NULL;
3993 bool server_mandatory = false;
3994 bool server_allowed = false;
3995 const char *server_signing = NULL;
3996 bool ok;
3997 uint8_t key_len;
3999 if (wct != 0x11) {
4000 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4001 return;
4004 /* NT protocol */
4005 server_security_mode = CVAL(vwv + 1, 0);
4006 server_max_mux = SVAL(vwv + 1, 1);
4007 server_max_xmit = IVAL(vwv + 3, 1);
4008 server_session_key = IVAL(vwv + 7, 1);
4009 server_time_zone = SVALS(vwv + 15, 1);
4010 server_time_zone *= 60;
4011 /* this time arrives in real GMT */
4012 server_system_time = BVAL(vwv + 11, 1);
4013 server_capabilities = IVAL(vwv + 9, 1);
4015 key_len = CVAL(vwv + 16, 1);
4017 if (server_capabilities & CAP_RAW_MODE) {
4018 server_readbraw = true;
4019 server_writebraw = true;
4021 if (server_capabilities & CAP_LOCK_AND_READ) {
4022 server_lockread = true;
4025 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4026 DATA_BLOB blob1, blob2;
4028 if (num_bytes < 16) {
4029 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4030 return;
4033 blob1 = data_blob_const(bytes, 16);
4034 status = GUID_from_data_blob(&blob1, &server_guid);
4035 if (tevent_req_nterror(req, status)) {
4036 return;
4039 blob1 = data_blob_const(bytes+16, num_bytes-16);
4040 blob2 = data_blob_dup_talloc(state, blob1);
4041 if (blob1.length > 0 &&
4042 tevent_req_nomem(blob2.data, req)) {
4043 return;
4045 server_gss_blob = blob2;
4046 } else {
4047 DATA_BLOB blob1, blob2;
4049 if (num_bytes < key_len) {
4050 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4051 return;
4054 if (key_len != 0 && key_len != 8) {
4055 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4056 return;
4059 if (key_len == 8) {
4060 memcpy(server_challenge, bytes, 8);
4063 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4064 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4065 if (blob1.length > 0) {
4066 size_t len;
4068 len = utf16_len_n(blob1.data,
4069 blob1.length);
4070 blob1.length = len;
4072 ok = convert_string_talloc(state,
4073 CH_UTF16LE,
4074 CH_UNIX,
4075 blob1.data,
4076 blob1.length,
4077 &server_workgroup,
4078 &len);
4079 if (!ok) {
4080 status = map_nt_error_from_unix_common(errno);
4081 tevent_req_nterror(req, status);
4082 return;
4086 blob2.data += blob1.length;
4087 blob2.length -= blob1.length;
4088 if (blob2.length > 0) {
4089 size_t len;
4091 len = utf16_len_n(blob1.data,
4092 blob1.length);
4093 blob1.length = len;
4095 ok = convert_string_talloc(state,
4096 CH_UTF16LE,
4097 CH_UNIX,
4098 blob2.data,
4099 blob2.length,
4100 &server_name,
4101 &len);
4102 if (!ok) {
4103 status = map_nt_error_from_unix_common(errno);
4104 tevent_req_nterror(req, status);
4105 return;
4110 client_signing = "disabled";
4111 if (conn->allow_signing) {
4112 client_signing = "allowed";
4114 if (conn->mandatory_signing) {
4115 client_signing = "required";
4118 server_signing = "not supported";
4119 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4120 server_signing = "supported";
4121 server_allowed = true;
4122 } else if (conn->mandatory_signing) {
4124 * We have mandatory signing as client
4125 * lets assume the server will look at our
4126 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4127 * flag in the session setup
4129 server_signing = "not announced";
4130 server_allowed = true;
4132 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4133 server_signing = "required";
4134 server_mandatory = true;
4137 ok = smb_signing_set_negotiated(conn->smb1.signing,
4138 server_allowed,
4139 server_mandatory);
4140 if (!ok) {
4141 DEBUG(1,("cli_negprot: SMB signing is required, "
4142 "but client[%s] and server[%s] mismatch\n",
4143 client_signing, server_signing));
4144 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4145 return;
4148 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4149 DATA_BLOB blob1;
4150 uint8_t key_len;
4151 time_t t;
4153 if (wct != 0x0D) {
4154 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4155 return;
4158 server_security_mode = SVAL(vwv + 1, 0);
4159 server_max_xmit = SVAL(vwv + 2, 0);
4160 server_max_mux = SVAL(vwv + 3, 0);
4161 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4162 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4163 server_session_key = IVAL(vwv + 6, 0);
4164 server_time_zone = SVALS(vwv + 10, 0);
4165 server_time_zone *= 60;
4166 /* this time is converted to GMT by make_unix_date */
4167 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4168 unix_to_nt_time(&server_system_time, t);
4169 key_len = SVAL(vwv + 11, 0);
4171 if (num_bytes < key_len) {
4172 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4173 return;
4176 if (key_len != 0 && key_len != 8) {
4177 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4178 return;
4181 if (key_len == 8) {
4182 memcpy(server_challenge, bytes, 8);
4185 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4186 if (blob1.length > 0) {
4187 size_t len;
4188 bool ok;
4190 len = utf16_len_n(blob1.data,
4191 blob1.length);
4192 blob1.length = len;
4194 ok = convert_string_talloc(state,
4195 CH_DOS,
4196 CH_UNIX,
4197 blob1.data,
4198 blob1.length,
4199 &server_workgroup,
4200 &len);
4201 if (!ok) {
4202 status = map_nt_error_from_unix_common(errno);
4203 tevent_req_nterror(req, status);
4204 return;
4208 } else {
4209 /* the old core protocol */
4210 server_time_zone = get_time_zone(time(NULL));
4211 server_max_xmit = 1024;
4212 server_max_mux = 1;
4215 if (server_max_xmit < 1024) {
4216 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4217 return;
4220 if (server_max_mux < 1) {
4221 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4222 return;
4226 * Now calculate the negotiated capabilities
4227 * based on the mask for:
4228 * - client only flags
4229 * - flags used in both directions
4230 * - server only flags
4232 both_capabilities = client_capabilities & server_capabilities;
4233 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4234 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4235 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4237 max_xmit = MIN(client_max_xmit, server_max_xmit);
4239 conn->smb1.server.capabilities = server_capabilities;
4240 conn->smb1.capabilities = capabilities;
4242 conn->smb1.server.max_xmit = server_max_xmit;
4243 conn->smb1.max_xmit = max_xmit;
4245 conn->smb1.server.max_mux = server_max_mux;
4247 conn->smb1.server.security_mode = server_security_mode;
4249 conn->smb1.server.readbraw = server_readbraw;
4250 conn->smb1.server.writebraw = server_writebraw;
4251 conn->smb1.server.lockread = server_lockread;
4252 conn->smb1.server.writeunlock = server_writeunlock;
4254 conn->smb1.server.session_key = server_session_key;
4256 talloc_steal(conn, server_gss_blob.data);
4257 conn->smb1.server.gss_blob = server_gss_blob;
4258 conn->smb1.server.guid = server_guid;
4259 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4260 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4261 conn->smb1.server.name = talloc_move(conn, &server_name);
4263 conn->smb1.server.time_zone = server_time_zone;
4264 conn->smb1.server.system_time = server_system_time;
4266 tevent_req_done(req);
4269 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4271 size_t i;
4272 uint8_t *buf;
4273 uint16_t dialect_count = 0;
4275 buf = state->smb2.dyn;
4276 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4277 if (smb2cli_prots[i].proto < state->min_protocol) {
4278 continue;
4281 if (smb2cli_prots[i].proto > state->max_protocol) {
4282 continue;
4285 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4286 dialect_count++;
4289 buf = state->smb2.fixed;
4290 SSVAL(buf, 0, 36);
4291 SSVAL(buf, 2, dialect_count);
4292 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4293 SSVAL(buf, 6, 0); /* Reserved */
4294 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4295 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4296 } else {
4297 SIVAL(buf, 8, 0); /* Capabilities */
4299 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4300 NTSTATUS status;
4301 DATA_BLOB blob;
4303 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4304 state, &blob);
4305 if (!NT_STATUS_IS_OK(status)) {
4306 return NULL;
4308 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4309 } else {
4310 memset(buf+12, 0, 16); /* ClientGuid */
4312 SBVAL(buf, 28, 0); /* ClientStartTime */
4314 return smb2cli_req_send(state, state->ev,
4315 state->conn, SMB2_OP_NEGPROT,
4316 0, 0, /* flags */
4317 state->timeout_msec,
4318 NULL, NULL, /* tcon, session */
4319 state->smb2.fixed, sizeof(state->smb2.fixed),
4320 state->smb2.dyn, dialect_count*2,
4321 UINT16_MAX); /* max_dyn_len */
4324 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4326 struct tevent_req *req =
4327 tevent_req_callback_data(subreq,
4328 struct tevent_req);
4329 struct smbXcli_negprot_state *state =
4330 tevent_req_data(req,
4331 struct smbXcli_negprot_state);
4332 struct smbXcli_conn *conn = state->conn;
4333 size_t security_offset, security_length;
4334 DATA_BLOB blob;
4335 NTSTATUS status;
4336 struct iovec *iov;
4337 uint8_t *body;
4338 size_t i;
4339 uint16_t dialect_revision;
4340 static const struct smb2cli_req_expected_response expected[] = {
4342 .status = NT_STATUS_OK,
4343 .body_size = 0x41
4347 status = smb2cli_req_recv(subreq, state, &iov,
4348 expected, ARRAY_SIZE(expected));
4349 TALLOC_FREE(subreq);
4350 if (tevent_req_nterror(req, status)) {
4351 return;
4354 body = (uint8_t *)iov[1].iov_base;
4356 dialect_revision = SVAL(body, 4);
4358 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4359 if (smb2cli_prots[i].proto < state->min_protocol) {
4360 continue;
4363 if (smb2cli_prots[i].proto > state->max_protocol) {
4364 continue;
4367 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4368 continue;
4371 conn->protocol = smb2cli_prots[i].proto;
4372 break;
4375 if (conn->protocol == PROTOCOL_NONE) {
4376 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4377 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4378 return;
4381 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4382 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4383 return;
4386 /* make sure we do not loop forever */
4387 state->min_protocol = PROTOCOL_SMB2_02;
4390 * send a SMB2 negprot, in order to negotiate
4391 * the SMB2 dialect.
4393 subreq = smbXcli_negprot_smb2_subreq(state);
4394 if (tevent_req_nomem(subreq, req)) {
4395 return;
4397 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4398 return;
4401 conn->smb2.server.security_mode = SVAL(body, 2);
4403 blob = data_blob_const(body + 8, 16);
4404 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4405 if (tevent_req_nterror(req, status)) {
4406 return;
4409 conn->smb2.server.capabilities = IVAL(body, 24);
4410 conn->smb2.server.max_trans_size= IVAL(body, 28);
4411 conn->smb2.server.max_read_size = IVAL(body, 32);
4412 conn->smb2.server.max_write_size= IVAL(body, 36);
4413 conn->smb2.server.system_time = BVAL(body, 40);
4414 conn->smb2.server.start_time = BVAL(body, 48);
4416 security_offset = SVAL(body, 56);
4417 security_length = SVAL(body, 58);
4419 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4420 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4421 return;
4424 if (security_length > iov[2].iov_len) {
4425 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4426 return;
4429 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4430 iov[2].iov_base,
4431 security_length);
4432 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4433 return;
4436 tevent_req_done(req);
4439 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4440 TALLOC_CTX *tmp_mem,
4441 uint8_t *inbuf)
4443 size_t num_pending = talloc_array_length(conn->pending);
4444 struct tevent_req *subreq;
4445 struct smbXcli_req_state *substate;
4446 struct tevent_req *req;
4447 uint32_t protocol_magic;
4448 size_t inbuf_len = smb_len_nbt(inbuf);
4450 if (num_pending != 1) {
4451 return NT_STATUS_INTERNAL_ERROR;
4454 if (inbuf_len < 4) {
4455 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4458 subreq = conn->pending[0];
4459 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4460 req = tevent_req_callback_data(subreq, struct tevent_req);
4462 protocol_magic = IVAL(inbuf, 4);
4464 switch (protocol_magic) {
4465 case SMB_MAGIC:
4466 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4467 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4468 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4470 case SMB2_MAGIC:
4471 if (substate->smb2.recv_iov == NULL) {
4473 * For the SMB1 negprot we have move it.
4475 substate->smb2.recv_iov = substate->smb1.recv_iov;
4476 substate->smb1.recv_iov = NULL;
4480 * we got an SMB2 answer, which consumed sequence number 0
4481 * so we need to use 1 as the next one.
4483 * we also need to set the current credits to 0
4484 * as we consumed the initial one. The SMB2 answer
4485 * hopefully grant us a new credit.
4487 conn->smb2.mid = 1;
4488 conn->smb2.cur_credits = 0;
4489 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4490 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4491 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4494 DEBUG(10, ("Got non-SMB PDU\n"));
4495 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4498 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4500 return tevent_req_simple_recv_ntstatus(req);
4503 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4504 uint32_t timeout_msec,
4505 enum protocol_types min_protocol,
4506 enum protocol_types max_protocol)
4508 TALLOC_CTX *frame = talloc_stackframe();
4509 struct tevent_context *ev;
4510 struct tevent_req *req;
4511 NTSTATUS status = NT_STATUS_NO_MEMORY;
4512 bool ok;
4514 if (smbXcli_conn_has_async_calls(conn)) {
4516 * Can't use sync call while an async call is in flight
4518 status = NT_STATUS_INVALID_PARAMETER_MIX;
4519 goto fail;
4521 ev = samba_tevent_context_init(frame);
4522 if (ev == NULL) {
4523 goto fail;
4525 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4526 min_protocol, max_protocol);
4527 if (req == NULL) {
4528 goto fail;
4530 ok = tevent_req_poll(req, ev);
4531 if (!ok) {
4532 status = map_nt_error_from_unix_common(errno);
4533 goto fail;
4535 status = smbXcli_negprot_recv(req);
4536 fail:
4537 TALLOC_FREE(frame);
4538 return status;
4541 static int smbXcli_session_destructor(struct smbXcli_session *session)
4543 if (session->conn == NULL) {
4544 return 0;
4547 DLIST_REMOVE(session->conn->sessions, session);
4548 return 0;
4551 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4552 struct smbXcli_conn *conn)
4554 struct smbXcli_session *session;
4556 session = talloc_zero(mem_ctx, struct smbXcli_session);
4557 if (session == NULL) {
4558 return NULL;
4560 session->smb2 = talloc_zero(session, struct smb2cli_session);
4561 if (session->smb2 == NULL) {
4562 talloc_free(session);
4563 return NULL;
4565 talloc_set_destructor(session, smbXcli_session_destructor);
4567 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4568 session->conn = conn;
4570 return session;
4573 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4574 struct smbXcli_session *src)
4576 struct smbXcli_session *session;
4578 session = talloc_zero(mem_ctx, struct smbXcli_session);
4579 if (session == NULL) {
4580 return NULL;
4582 session->smb2 = talloc_zero(session, struct smb2cli_session);
4583 if (session->smb2 == NULL) {
4584 talloc_free(session);
4585 return NULL;
4588 session->conn = src->conn;
4589 *session->smb2 = *src->smb2;
4590 session->smb2_channel = src->smb2_channel;
4591 session->disconnect_expired = src->disconnect_expired;
4593 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4594 talloc_set_destructor(session, smbXcli_session_destructor);
4596 return session;
4600 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4601 TALLOC_CTX *mem_ctx,
4602 DATA_BLOB *key)
4604 const DATA_BLOB *application_key;
4606 *key = data_blob_null;
4608 if (session->conn == NULL) {
4609 return NT_STATUS_NO_USER_SESSION_KEY;
4612 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4613 application_key = &session->smb2->application_key;
4614 } else {
4615 application_key = &session->smb1.application_key;
4618 if (application_key->length == 0) {
4619 return NT_STATUS_NO_USER_SESSION_KEY;
4622 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4623 if (key->data == NULL) {
4624 return NT_STATUS_NO_MEMORY;
4627 return NT_STATUS_OK;
4630 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4632 session->disconnect_expired = true;
4635 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4637 return session->smb1.session_id;
4640 void smb1cli_session_set_id(struct smbXcli_session *session,
4641 uint16_t session_id)
4643 session->smb1.session_id = session_id;
4646 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4647 const DATA_BLOB _session_key)
4649 struct smbXcli_conn *conn = session->conn;
4650 uint8_t session_key[16];
4652 if (conn == NULL) {
4653 return NT_STATUS_INVALID_PARAMETER_MIX;
4656 if (session->smb1.application_key.length != 0) {
4658 * TODO: do not allow this...
4660 * return NT_STATUS_INVALID_PARAMETER_MIX;
4662 data_blob_clear_free(&session->smb1.application_key);
4663 session->smb1.protected_key = false;
4666 if (_session_key.length == 0) {
4667 return NT_STATUS_OK;
4670 ZERO_STRUCT(session_key);
4671 memcpy(session_key, _session_key.data,
4672 MIN(_session_key.length, sizeof(session_key)));
4674 session->smb1.application_key = data_blob_talloc(session,
4675 session_key,
4676 sizeof(session_key));
4677 ZERO_STRUCT(session_key);
4678 if (session->smb1.application_key.data == NULL) {
4679 return NT_STATUS_NO_MEMORY;
4682 session->smb1.protected_key = false;
4684 return NT_STATUS_OK;
4687 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4689 if (session->smb1.protected_key) {
4690 /* already protected */
4691 return NT_STATUS_OK;
4694 if (session->smb1.application_key.length != 16) {
4695 return NT_STATUS_INVALID_PARAMETER_MIX;
4698 smb_key_derivation(session->smb1.application_key.data,
4699 session->smb1.application_key.length,
4700 session->smb1.application_key.data);
4702 session->smb1.protected_key = true;
4704 return NT_STATUS_OK;
4707 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4709 struct smbXcli_conn *conn = session->conn;
4710 uint8_t security_mode = 0;
4712 if (conn == NULL) {
4713 return security_mode;
4716 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4717 if (conn->mandatory_signing) {
4718 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4721 return security_mode;
4724 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4726 return session->smb2->session_id;
4729 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4731 return session->smb2->session_flags;
4734 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4735 uint64_t session_id,
4736 uint16_t session_flags)
4738 session->smb2->session_id = session_id;
4739 session->smb2->session_flags = session_flags;
4742 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4744 session->smb2->channel_sequence += 1;
4747 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4748 const DATA_BLOB _session_key,
4749 const struct iovec *recv_iov)
4751 struct smbXcli_conn *conn = session->conn;
4752 uint16_t no_sign_flags;
4753 uint8_t session_key[16];
4754 bool check_signature = true;
4755 uint32_t hdr_flags;
4756 NTSTATUS status;
4758 if (conn == NULL) {
4759 return NT_STATUS_INVALID_PARAMETER_MIX;
4762 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
4763 return NT_STATUS_INVALID_PARAMETER_MIX;
4766 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4768 if (session->smb2->session_flags & no_sign_flags) {
4769 session->smb2->should_sign = false;
4770 return NT_STATUS_OK;
4773 if (session->smb2->signing_key.length != 0) {
4774 return NT_STATUS_INVALID_PARAMETER_MIX;
4777 ZERO_STRUCT(session_key);
4778 memcpy(session_key, _session_key.data,
4779 MIN(_session_key.length, sizeof(session_key)));
4781 session->smb2->signing_key = data_blob_talloc(session,
4782 session_key,
4783 sizeof(session_key));
4784 if (session->smb2->signing_key.data == NULL) {
4785 ZERO_STRUCT(session_key);
4786 return NT_STATUS_NO_MEMORY;
4789 if (conn->protocol >= PROTOCOL_SMB2_24) {
4790 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4791 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4793 smb2_key_derivation(session_key, sizeof(session_key),
4794 label.data, label.length,
4795 context.data, context.length,
4796 session->smb2->signing_key.data);
4799 session->smb2->encryption_key = data_blob_dup_talloc(session,
4800 session->smb2->signing_key);
4801 if (session->smb2->encryption_key.data == NULL) {
4802 ZERO_STRUCT(session_key);
4803 return NT_STATUS_NO_MEMORY;
4806 if (conn->protocol >= PROTOCOL_SMB2_24) {
4807 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4808 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4810 smb2_key_derivation(session_key, sizeof(session_key),
4811 label.data, label.length,
4812 context.data, context.length,
4813 session->smb2->encryption_key.data);
4816 session->smb2->decryption_key = data_blob_dup_talloc(session,
4817 session->smb2->signing_key);
4818 if (session->smb2->decryption_key.data == NULL) {
4819 ZERO_STRUCT(session_key);
4820 return NT_STATUS_NO_MEMORY;
4823 if (conn->protocol >= PROTOCOL_SMB2_24) {
4824 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4825 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4827 smb2_key_derivation(session_key, sizeof(session_key),
4828 label.data, label.length,
4829 context.data, context.length,
4830 session->smb2->decryption_key.data);
4833 session->smb2->application_key = data_blob_dup_talloc(session,
4834 session->smb2->signing_key);
4835 if (session->smb2->application_key.data == NULL) {
4836 ZERO_STRUCT(session_key);
4837 return NT_STATUS_NO_MEMORY;
4840 if (conn->protocol >= PROTOCOL_SMB2_24) {
4841 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4842 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4844 smb2_key_derivation(session_key, sizeof(session_key),
4845 label.data, label.length,
4846 context.data, context.length,
4847 session->smb2->application_key.data);
4849 ZERO_STRUCT(session_key);
4851 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4852 session->smb2->signing_key);
4853 if (session->smb2_channel.signing_key.data == NULL) {
4854 return NT_STATUS_NO_MEMORY;
4857 check_signature = conn->mandatory_signing;
4859 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
4860 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
4862 * Sadly some vendors don't sign the
4863 * final SMB2 session setup response
4865 * At least Windows and Samba are always doing this
4866 * if there's a session key available.
4868 * We only check the signature if it's mandatory
4869 * or SMB2_HDR_FLAG_SIGNED is provided.
4871 check_signature = true;
4874 if (check_signature) {
4875 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4876 session->conn->protocol,
4877 recv_iov, 3);
4878 if (!NT_STATUS_IS_OK(status)) {
4879 return status;
4883 session->smb2->should_sign = false;
4884 session->smb2->should_encrypt = false;
4886 if (conn->desire_signing) {
4887 session->smb2->should_sign = true;
4890 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4891 session->smb2->should_sign = true;
4894 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4895 session->smb2->should_encrypt = true;
4898 if (conn->protocol < PROTOCOL_SMB2_24) {
4899 session->smb2->should_encrypt = false;
4902 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4903 session->smb2->should_encrypt = false;
4906 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4907 sizeof(session->smb2->nonce_high));
4908 session->smb2->nonce_low = 1;
4910 return NT_STATUS_OK;
4913 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4914 struct smbXcli_session *session1,
4915 struct smbXcli_conn *conn,
4916 struct smbXcli_session **_session2)
4918 struct smbXcli_session *session2;
4920 if (session1->smb2->signing_key.length == 0) {
4921 return NT_STATUS_INVALID_PARAMETER_MIX;
4924 if (conn == NULL) {
4925 return NT_STATUS_INVALID_PARAMETER_MIX;
4928 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4929 if (session2 == NULL) {
4930 return NT_STATUS_NO_MEMORY;
4932 session2->smb2 = talloc_reference(session2, session1->smb2);
4933 if (session2->smb2 == NULL) {
4934 talloc_free(session2);
4935 return NT_STATUS_NO_MEMORY;
4938 talloc_set_destructor(session2, smbXcli_session_destructor);
4939 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4940 session2->conn = conn;
4942 *_session2 = session2;
4943 return NT_STATUS_OK;
4946 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4947 const DATA_BLOB _channel_key,
4948 const struct iovec *recv_iov)
4950 struct smbXcli_conn *conn = session->conn;
4951 uint8_t channel_key[16];
4952 NTSTATUS status;
4954 if (conn == NULL) {
4955 return NT_STATUS_INVALID_PARAMETER_MIX;
4958 if (session->smb2_channel.signing_key.length != 0) {
4959 return NT_STATUS_INVALID_PARAMETER_MIX;
4962 ZERO_STRUCT(channel_key);
4963 memcpy(channel_key, _channel_key.data,
4964 MIN(_channel_key.length, sizeof(channel_key)));
4966 session->smb2_channel.signing_key = data_blob_talloc(session,
4967 channel_key,
4968 sizeof(channel_key));
4969 if (session->smb2_channel.signing_key.data == NULL) {
4970 ZERO_STRUCT(channel_key);
4971 return NT_STATUS_NO_MEMORY;
4974 if (conn->protocol >= PROTOCOL_SMB2_24) {
4975 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4976 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4978 smb2_key_derivation(channel_key, sizeof(channel_key),
4979 label.data, label.length,
4980 context.data, context.length,
4981 session->smb2_channel.signing_key.data);
4983 ZERO_STRUCT(channel_key);
4985 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4986 session->conn->protocol,
4987 recv_iov, 3);
4988 if (!NT_STATUS_IS_OK(status)) {
4989 return status;
4992 return NT_STATUS_OK;
4995 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
4997 if (session->smb2->should_encrypt) {
4998 return NT_STATUS_OK;
5001 if (session->conn->protocol < PROTOCOL_SMB2_24) {
5002 return NT_STATUS_NOT_SUPPORTED;
5005 if (!(session->conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
5006 return NT_STATUS_NOT_SUPPORTED;
5009 if (session->smb2->signing_key.data == NULL) {
5010 return NT_STATUS_NOT_SUPPORTED;
5012 session->smb2->should_encrypt = true;
5013 return NT_STATUS_OK;
5016 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
5018 struct smbXcli_tcon *tcon;
5020 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
5021 if (tcon == NULL) {
5022 return NULL;
5025 return tcon;
5028 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
5030 if (tcon == NULL) {
5031 return false;
5034 if (tcon->is_smb1) {
5035 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
5036 return true;
5039 return false;
5042 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
5043 return true;
5046 return false;
5049 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
5051 return tcon->smb1.tcon_id;
5054 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
5056 tcon->is_smb1 = true;
5057 tcon->smb1.tcon_id = tcon_id;
5060 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5061 uint16_t tcon_id,
5062 uint16_t optional_support,
5063 uint32_t maximal_access,
5064 uint32_t guest_maximal_access,
5065 const char *service,
5066 const char *fs_type)
5068 tcon->is_smb1 = true;
5069 tcon->smb1.tcon_id = tcon_id;
5070 tcon->smb1.optional_support = optional_support;
5071 tcon->smb1.maximal_access = maximal_access;
5072 tcon->smb1.guest_maximal_access = guest_maximal_access;
5074 TALLOC_FREE(tcon->smb1.service);
5075 tcon->smb1.service = talloc_strdup(tcon, service);
5076 if (service != NULL && tcon->smb1.service == NULL) {
5077 return false;
5080 TALLOC_FREE(tcon->smb1.fs_type);
5081 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5082 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5083 return false;
5086 return true;
5089 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5091 return tcon->smb2.tcon_id;
5094 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5096 return tcon->smb2.capabilities;
5099 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5100 struct smbXcli_session *session,
5101 uint32_t tcon_id,
5102 uint8_t type,
5103 uint32_t flags,
5104 uint32_t capabilities,
5105 uint32_t maximal_access)
5107 tcon->is_smb1 = false;
5108 tcon->smb2.tcon_id = tcon_id;
5109 tcon->smb2.type = type;
5110 tcon->smb2.flags = flags;
5111 tcon->smb2.capabilities = capabilities;
5112 tcon->smb2.maximal_access = maximal_access;
5114 tcon->smb2.should_encrypt = false;
5116 if (session == NULL) {
5117 return;
5120 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5122 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5123 tcon->smb2.should_encrypt = true;