s3:libsmb: Plumb cli_smb2_unlink() inside cli_unlink().
[Samba.git] / libcli / smb / smbXcli_base.c
blobb502f4f70543ac719baf0ad09c7e301c1f62430f
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 struct {
171 uint16_t tcon_id;
172 uint16_t optional_support;
173 uint32_t maximal_access;
174 uint32_t guest_maximal_access;
175 char *service;
176 char *fs_type;
177 } smb1;
179 struct {
180 uint32_t tcon_id;
181 uint8_t type;
182 uint32_t flags;
183 uint32_t capabilities;
184 uint32_t maximal_access;
185 bool should_encrypt;
186 } smb2;
189 struct smbXcli_req_state {
190 struct tevent_context *ev;
191 struct smbXcli_conn *conn;
192 struct smbXcli_session *session; /* maybe NULL */
193 struct smbXcli_tcon *tcon; /* maybe NULL */
195 uint8_t length_hdr[4];
197 bool one_way;
199 uint8_t *inbuf;
201 struct {
202 /* Space for the header including the wct */
203 uint8_t hdr[HDR_VWV];
206 * For normal requests, smb1cli_req_send chooses a mid.
207 * SecondaryV trans requests need to use the mid of the primary
208 * request, so we need a place to store it.
209 * Assume it is set if != 0.
211 uint16_t mid;
213 uint16_t *vwv;
214 uint8_t bytecount_buf[2];
216 #define MAX_SMB_IOV 10
217 /* length_hdr, hdr, words, byte_count, buffers */
218 struct iovec iov[1 + 3 + MAX_SMB_IOV];
219 int iov_count;
221 bool one_way_seqnum;
222 uint32_t seqnum;
223 struct tevent_req **chained_requests;
225 uint8_t recv_cmd;
226 NTSTATUS recv_status;
227 /* always an array of 3 talloc elements */
228 struct iovec *recv_iov;
229 } smb1;
231 struct {
232 const uint8_t *fixed;
233 uint16_t fixed_len;
234 const uint8_t *dyn;
235 uint32_t dyn_len;
237 uint8_t transform[SMB2_TF_HDR_SIZE];
238 uint8_t hdr[SMB2_HDR_BODY];
239 uint8_t pad[7]; /* padding space for compounding */
242 * always an array of 3 talloc elements
243 * (without a SMB2_TRANSFORM header!)
245 * HDR, BODY, DYN
247 struct iovec *recv_iov;
250 * the expected max for the response dyn_len
252 uint32_t max_dyn_len;
254 uint16_t credit_charge;
256 bool should_sign;
257 bool should_encrypt;
258 uint64_t encryption_session_id;
260 bool signing_skipped;
261 bool notify_async;
262 bool got_async;
263 } smb2;
266 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
269 * NT_STATUS_OK, means we do not notify the callers
271 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
273 while (conn->sessions) {
274 conn->sessions->conn = NULL;
275 DLIST_REMOVE(conn->sessions, conn->sessions);
278 if (conn->smb1.trans_enc) {
279 TALLOC_FREE(conn->smb1.trans_enc);
282 return 0;
285 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
286 int fd,
287 const char *remote_name,
288 enum smb_signing_setting signing_state,
289 uint32_t smb1_capabilities,
290 struct GUID *client_guid,
291 uint32_t smb2_capabilities)
293 struct smbXcli_conn *conn = NULL;
294 void *ss = NULL;
295 struct sockaddr *sa = NULL;
296 socklen_t sa_length;
297 int ret;
299 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
300 if (!conn) {
301 return NULL;
304 conn->read_fd = fd;
305 conn->write_fd = dup(fd);
306 if (conn->write_fd == -1) {
307 goto error;
310 conn->remote_name = talloc_strdup(conn, remote_name);
311 if (conn->remote_name == NULL) {
312 goto error;
316 ss = (void *)&conn->local_ss;
317 sa = (struct sockaddr *)ss;
318 sa_length = sizeof(conn->local_ss);
319 ret = getsockname(fd, sa, &sa_length);
320 if (ret == -1) {
321 goto error;
323 ss = (void *)&conn->remote_ss;
324 sa = (struct sockaddr *)ss;
325 sa_length = sizeof(conn->remote_ss);
326 ret = getpeername(fd, sa, &sa_length);
327 if (ret == -1) {
328 goto error;
331 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
332 if (conn->outgoing == NULL) {
333 goto error;
335 conn->pending = NULL;
337 conn->protocol = PROTOCOL_NONE;
339 switch (signing_state) {
340 case SMB_SIGNING_OFF:
341 /* never */
342 conn->allow_signing = false;
343 conn->desire_signing = false;
344 conn->mandatory_signing = false;
345 break;
346 case SMB_SIGNING_DEFAULT:
347 case SMB_SIGNING_IF_REQUIRED:
348 /* if the server requires it */
349 conn->allow_signing = true;
350 conn->desire_signing = false;
351 conn->mandatory_signing = false;
352 break;
353 case SMB_SIGNING_REQUIRED:
354 /* always */
355 conn->allow_signing = true;
356 conn->desire_signing = true;
357 conn->mandatory_signing = true;
358 break;
361 conn->smb1.client.capabilities = smb1_capabilities;
362 conn->smb1.client.max_xmit = UINT16_MAX;
364 conn->smb1.capabilities = conn->smb1.client.capabilities;
365 conn->smb1.max_xmit = 1024;
367 conn->smb1.mid = 1;
369 /* initialise signing */
370 conn->smb1.signing = smb_signing_init(conn,
371 conn->allow_signing,
372 conn->desire_signing,
373 conn->mandatory_signing);
374 if (!conn->smb1.signing) {
375 goto error;
378 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
379 if (conn->mandatory_signing) {
380 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
382 if (client_guid) {
383 conn->smb2.client.guid = *client_guid;
385 conn->smb2.client.capabilities = smb2_capabilities;
387 conn->smb2.cur_credits = 1;
388 conn->smb2.max_credits = 0;
390 talloc_set_destructor(conn, smbXcli_conn_destructor);
391 return conn;
393 error:
394 if (conn->write_fd != -1) {
395 close(conn->write_fd);
397 TALLOC_FREE(conn);
398 return NULL;
401 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
403 if (conn == NULL) {
404 return false;
407 if (conn->read_fd == -1) {
408 return false;
411 return true;
414 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
416 return conn->protocol;
419 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
421 if (conn->protocol >= PROTOCOL_SMB2_02) {
422 return true;
425 if (conn->smb1.capabilities & CAP_UNICODE) {
426 return true;
429 return false;
432 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
434 set_socket_options(conn->read_fd, options);
437 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
439 return &conn->local_ss;
442 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
444 return &conn->remote_ss;
447 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
449 return conn->remote_name;
452 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
454 if (conn->protocol >= PROTOCOL_SMB2_02) {
456 * TODO...
458 return 1;
461 return conn->smb1.server.max_mux;
464 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
466 if (conn->protocol >= PROTOCOL_SMB2_02) {
467 return conn->smb2.server.system_time;
470 return conn->smb1.server.system_time;
473 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
475 if (conn->protocol >= PROTOCOL_SMB2_02) {
476 return &conn->smb2.server.gss_blob;
479 return &conn->smb1.server.gss_blob;
482 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
484 if (conn->protocol >= PROTOCOL_SMB2_02) {
485 return &conn->smb2.server.guid;
488 return &conn->smb1.server.guid;
491 struct smbXcli_conn_samba_suicide_state {
492 struct smbXcli_conn *conn;
493 struct iovec iov;
494 uint8_t buf[9];
497 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
499 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
500 struct tevent_context *ev,
501 struct smbXcli_conn *conn,
502 uint8_t exitcode)
504 struct tevent_req *req, *subreq;
505 struct smbXcli_conn_samba_suicide_state *state;
507 req = tevent_req_create(mem_ctx, &state,
508 struct smbXcli_conn_samba_suicide_state);
509 if (req == NULL) {
510 return NULL;
512 state->conn = conn;
513 SIVAL(state->buf, 4, 0x74697865);
514 SCVAL(state->buf, 8, exitcode);
515 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
517 state->iov.iov_base = state->buf;
518 state->iov.iov_len = sizeof(state->buf);
520 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
521 false, &state->iov, 1);
522 if (tevent_req_nomem(subreq, req)) {
523 return tevent_req_post(req, ev);
525 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
526 return req;
529 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
531 struct tevent_req *req = tevent_req_callback_data(
532 subreq, struct tevent_req);
533 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
534 req, struct smbXcli_conn_samba_suicide_state);
535 ssize_t nwritten;
536 int err;
538 nwritten = writev_recv(subreq, &err);
539 TALLOC_FREE(subreq);
540 if (nwritten == -1) {
541 NTSTATUS status = map_nt_error_from_unix_common(err);
542 smbXcli_conn_disconnect(state->conn, status);
543 return;
545 tevent_req_done(req);
548 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
550 return tevent_req_simple_recv_ntstatus(req);
553 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
554 uint8_t exitcode)
556 TALLOC_CTX *frame = talloc_stackframe();
557 struct tevent_context *ev;
558 struct tevent_req *req;
559 NTSTATUS status = NT_STATUS_NO_MEMORY;
560 bool ok;
562 if (smbXcli_conn_has_async_calls(conn)) {
564 * Can't use sync call while an async call is in flight
566 status = NT_STATUS_INVALID_PARAMETER_MIX;
567 goto fail;
569 ev = samba_tevent_context_init(frame);
570 if (ev == NULL) {
571 goto fail;
573 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
574 if (req == NULL) {
575 goto fail;
577 ok = tevent_req_poll(req, ev);
578 if (!ok) {
579 status = map_nt_error_from_unix_common(errno);
580 goto fail;
582 status = smbXcli_conn_samba_suicide_recv(req);
583 fail:
584 TALLOC_FREE(frame);
585 return status;
588 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
590 return conn->smb1.capabilities;
593 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
595 return conn->smb1.max_xmit;
598 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
600 size_t pending;
601 uint16_t possible = conn->smb1.server.max_mux;
603 pending = tevent_queue_length(conn->outgoing);
604 if (pending >= possible) {
605 return false;
607 pending += talloc_array_length(conn->pending);
608 if (pending >= possible) {
609 return false;
612 return true;
615 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
617 return conn->smb1.server.session_key;
620 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
622 return conn->smb1.server.challenge;
625 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
627 return conn->smb1.server.security_mode;
630 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
632 return conn->smb1.server.readbraw;
635 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
637 return conn->smb1.server.writebraw;
640 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
642 return conn->smb1.server.lockread;
645 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
647 return conn->smb1.server.writeunlock;
650 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
652 return conn->smb1.server.time_zone;
655 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
656 const DATA_BLOB user_session_key,
657 const DATA_BLOB response)
659 return smb_signing_activate(conn->smb1.signing,
660 user_session_key,
661 response);
664 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
665 const uint8_t *buf, uint32_t seqnum)
667 const uint8_t *hdr = buf + NBT_HDR_SIZE;
668 size_t len = smb_len_nbt(buf);
670 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
673 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
675 return smb_signing_is_active(conn->smb1.signing);
678 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
679 struct smb_trans_enc_state *es)
681 /* Replace the old state, if any. */
682 if (conn->smb1.trans_enc) {
683 TALLOC_FREE(conn->smb1.trans_enc);
685 conn->smb1.trans_enc = es;
688 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
690 return common_encryption_on(conn->smb1.trans_enc);
694 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
696 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
697 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
699 if (NT_STATUS_IS_OK(status)) {
700 return NT_STATUS_OK;
703 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
704 return status;
707 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
711 * Is the SMB command able to hold an AND_X successor
712 * @param[in] cmd The SMB command in question
713 * @retval Can we add a chained request after "cmd"?
715 bool smb1cli_is_andx_req(uint8_t cmd)
717 switch (cmd) {
718 case SMBtconX:
719 case SMBlockingX:
720 case SMBopenX:
721 case SMBreadX:
722 case SMBwriteX:
723 case SMBsesssetupX:
724 case SMBulogoffX:
725 case SMBntcreateX:
726 return true;
727 break;
728 default:
729 break;
732 return false;
735 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
737 size_t num_pending = talloc_array_length(conn->pending);
738 uint16_t result;
740 while (true) {
741 size_t i;
743 result = conn->smb1.mid++;
744 if ((result == 0) || (result == 0xffff)) {
745 continue;
748 for (i=0; i<num_pending; i++) {
749 if (result == smb1cli_req_mid(conn->pending[i])) {
750 break;
754 if (i == num_pending) {
755 return result;
760 void smbXcli_req_unset_pending(struct tevent_req *req)
762 struct smbXcli_req_state *state =
763 tevent_req_data(req,
764 struct smbXcli_req_state);
765 struct smbXcli_conn *conn = state->conn;
766 size_t num_pending = talloc_array_length(conn->pending);
767 size_t i;
769 if (state->smb1.mid != 0) {
771 * This is a [nt]trans[2] request which waits
772 * for more than one reply.
774 return;
777 talloc_set_destructor(req, NULL);
779 if (num_pending == 1) {
781 * The pending read_smb tevent_req is a child of
782 * conn->pending. So if nothing is pending anymore, we need to
783 * delete the socket read fde.
785 TALLOC_FREE(conn->pending);
786 conn->read_smb_req = NULL;
787 return;
790 for (i=0; i<num_pending; i++) {
791 if (req == conn->pending[i]) {
792 break;
795 if (i == num_pending) {
797 * Something's seriously broken. Just returning here is the
798 * right thing nevertheless, the point of this routine is to
799 * remove ourselves from conn->pending.
801 return;
805 * Remove ourselves from the conn->pending array
807 for (; i < (num_pending - 1); i++) {
808 conn->pending[i] = conn->pending[i+1];
812 * No NULL check here, we're shrinking by sizeof(void *), and
813 * talloc_realloc just adjusts the size for this.
815 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
816 num_pending - 1);
817 return;
820 static int smbXcli_req_destructor(struct tevent_req *req)
822 struct smbXcli_req_state *state =
823 tevent_req_data(req,
824 struct smbXcli_req_state);
827 * Make sure we really remove it from
828 * the pending array on destruction.
830 state->smb1.mid = 0;
831 smbXcli_req_unset_pending(req);
832 return 0;
835 static bool smb1cli_req_cancel(struct tevent_req *req);
836 static bool smb2cli_req_cancel(struct tevent_req *req);
838 static bool smbXcli_req_cancel(struct tevent_req *req)
840 struct smbXcli_req_state *state =
841 tevent_req_data(req,
842 struct smbXcli_req_state);
844 if (!smbXcli_conn_is_connected(state->conn)) {
845 return false;
848 if (state->conn->protocol == PROTOCOL_NONE) {
849 return false;
852 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
853 return smb2cli_req_cancel(req);
856 return smb1cli_req_cancel(req);
859 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
861 bool smbXcli_req_set_pending(struct tevent_req *req)
863 struct smbXcli_req_state *state =
864 tevent_req_data(req,
865 struct smbXcli_req_state);
866 struct smbXcli_conn *conn;
867 struct tevent_req **pending;
868 size_t num_pending;
870 conn = state->conn;
872 if (!smbXcli_conn_is_connected(conn)) {
873 return false;
876 num_pending = talloc_array_length(conn->pending);
878 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
879 num_pending+1);
880 if (pending == NULL) {
881 return false;
883 pending[num_pending] = req;
884 conn->pending = pending;
885 talloc_set_destructor(req, smbXcli_req_destructor);
886 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
888 if (!smbXcli_conn_receive_next(conn)) {
890 * the caller should notify the current request
892 * And all other pending requests get notified
893 * by smbXcli_conn_disconnect().
895 smbXcli_req_unset_pending(req);
896 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
897 return false;
900 return true;
903 static void smbXcli_conn_received(struct tevent_req *subreq);
905 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
907 size_t num_pending = talloc_array_length(conn->pending);
908 struct tevent_req *req;
909 struct smbXcli_req_state *state;
911 if (conn->read_smb_req != NULL) {
912 return true;
915 if (num_pending == 0) {
916 if (conn->smb2.mid < UINT64_MAX) {
917 /* no more pending requests, so we are done for now */
918 return true;
922 * If there are no more SMB2 requests possible,
923 * because we are out of message ids,
924 * we need to disconnect.
926 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
927 return true;
930 req = conn->pending[0];
931 state = tevent_req_data(req, struct smbXcli_req_state);
934 * We're the first ones, add the read_smb request that waits for the
935 * answer from the server
937 conn->read_smb_req = read_smb_send(conn->pending,
938 state->ev,
939 conn->read_fd);
940 if (conn->read_smb_req == NULL) {
941 return false;
943 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
944 return true;
947 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
949 struct smbXcli_session *session;
951 tevent_queue_stop(conn->outgoing);
953 if (conn->read_fd != -1) {
954 close(conn->read_fd);
956 if (conn->write_fd != -1) {
957 close(conn->write_fd);
959 conn->read_fd = -1;
960 conn->write_fd = -1;
962 session = conn->sessions;
963 if (talloc_array_length(conn->pending) == 0) {
965 * if we do not have pending requests
966 * there is no need to update the channel_sequence
968 session = NULL;
970 for (; session; session = session->next) {
971 smb2cli_session_increment_channel_sequence(session);
975 * Cancel all pending requests. We do not do a for-loop walking
976 * conn->pending because that array changes in
977 * smbXcli_req_unset_pending.
979 while (talloc_array_length(conn->pending) > 0) {
980 struct tevent_req *req;
981 struct smbXcli_req_state *state;
982 struct tevent_req **chain;
983 size_t num_chained;
984 size_t i;
986 req = conn->pending[0];
987 state = tevent_req_data(req, struct smbXcli_req_state);
989 if (state->smb1.chained_requests == NULL) {
991 * We're dead. No point waiting for trans2
992 * replies.
994 state->smb1.mid = 0;
996 smbXcli_req_unset_pending(req);
998 if (NT_STATUS_IS_OK(status)) {
999 /* do not notify the callers */
1000 continue;
1004 * we need to defer the callback, because we may notify
1005 * more then one caller.
1007 tevent_req_defer_callback(req, state->ev);
1008 tevent_req_nterror(req, status);
1009 continue;
1012 chain = talloc_move(conn, &state->smb1.chained_requests);
1013 num_chained = talloc_array_length(chain);
1015 for (i=0; i<num_chained; i++) {
1016 req = chain[i];
1017 state = tevent_req_data(req, struct smbXcli_req_state);
1020 * We're dead. No point waiting for trans2
1021 * replies.
1023 state->smb1.mid = 0;
1025 smbXcli_req_unset_pending(req);
1027 if (NT_STATUS_IS_OK(status)) {
1028 /* do not notify the callers */
1029 continue;
1033 * we need to defer the callback, because we may notify
1034 * more than one caller.
1036 tevent_req_defer_callback(req, state->ev);
1037 tevent_req_nterror(req, status);
1039 TALLOC_FREE(chain);
1044 * Fetch a smb request's mid. Only valid after the request has been sent by
1045 * smb1cli_req_send().
1047 uint16_t smb1cli_req_mid(struct tevent_req *req)
1049 struct smbXcli_req_state *state =
1050 tevent_req_data(req,
1051 struct smbXcli_req_state);
1053 if (state->smb1.mid != 0) {
1054 return state->smb1.mid;
1057 return SVAL(state->smb1.hdr, HDR_MID);
1060 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1062 struct smbXcli_req_state *state =
1063 tevent_req_data(req,
1064 struct smbXcli_req_state);
1066 state->smb1.mid = mid;
1069 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1071 struct smbXcli_req_state *state =
1072 tevent_req_data(req,
1073 struct smbXcli_req_state);
1075 return state->smb1.seqnum;
1078 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1080 struct smbXcli_req_state *state =
1081 tevent_req_data(req,
1082 struct smbXcli_req_state);
1084 state->smb1.seqnum = seqnum;
1087 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1089 size_t result = 0;
1090 int i;
1091 for (i=0; i<count; i++) {
1092 result += iov[i].iov_len;
1094 return result;
1097 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1098 const struct iovec *iov,
1099 int count)
1101 size_t len = smbXcli_iov_len(iov, count);
1102 size_t copied;
1103 uint8_t *buf;
1104 int i;
1106 buf = talloc_array(mem_ctx, uint8_t, len);
1107 if (buf == NULL) {
1108 return NULL;
1110 copied = 0;
1111 for (i=0; i<count; i++) {
1112 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1113 copied += iov[i].iov_len;
1115 return buf;
1118 static void smb1cli_req_flags(enum protocol_types protocol,
1119 uint32_t smb1_capabilities,
1120 uint8_t smb_command,
1121 uint8_t additional_flags,
1122 uint8_t clear_flags,
1123 uint8_t *_flags,
1124 uint16_t additional_flags2,
1125 uint16_t clear_flags2,
1126 uint16_t *_flags2)
1128 uint8_t flags = 0;
1129 uint16_t flags2 = 0;
1131 if (protocol >= PROTOCOL_LANMAN1) {
1132 flags |= FLAG_CASELESS_PATHNAMES;
1133 flags |= FLAG_CANONICAL_PATHNAMES;
1136 if (protocol >= PROTOCOL_LANMAN2) {
1137 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1138 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1141 if (protocol >= PROTOCOL_NT1) {
1142 flags2 |= FLAGS2_IS_LONG_NAME;
1144 if (smb1_capabilities & CAP_UNICODE) {
1145 flags2 |= FLAGS2_UNICODE_STRINGS;
1147 if (smb1_capabilities & CAP_STATUS32) {
1148 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1150 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1151 flags2 |= FLAGS2_EXTENDED_SECURITY;
1155 flags |= additional_flags;
1156 flags &= ~clear_flags;
1157 flags2 |= additional_flags2;
1158 flags2 &= ~clear_flags2;
1160 *_flags = flags;
1161 *_flags2 = flags2;
1164 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1166 static bool smb1cli_req_cancel(struct tevent_req *req)
1168 struct smbXcli_req_state *state =
1169 tevent_req_data(req,
1170 struct smbXcli_req_state);
1171 uint8_t flags;
1172 uint16_t flags2;
1173 uint32_t pid;
1174 uint16_t mid;
1175 struct tevent_req *subreq;
1176 NTSTATUS status;
1178 flags = CVAL(state->smb1.hdr, HDR_FLG);
1179 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1180 pid = SVAL(state->smb1.hdr, HDR_PID);
1181 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1182 mid = SVAL(state->smb1.hdr, HDR_MID);
1184 subreq = smb1cli_req_create(state, state->ev,
1185 state->conn,
1186 SMBntcancel,
1187 flags, 0,
1188 flags2, 0,
1189 0, /* timeout */
1190 pid,
1191 state->tcon,
1192 state->session,
1193 0, NULL, /* vwv */
1194 0, NULL); /* bytes */
1195 if (subreq == NULL) {
1196 return false;
1198 smb1cli_req_set_mid(subreq, mid);
1200 status = smb1cli_req_chain_submit(&subreq, 1);
1201 if (!NT_STATUS_IS_OK(status)) {
1202 TALLOC_FREE(subreq);
1203 return false;
1205 smb1cli_req_set_mid(subreq, 0);
1207 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1209 return true;
1212 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1214 /* we do not care about the result */
1215 TALLOC_FREE(subreq);
1218 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1219 struct tevent_context *ev,
1220 struct smbXcli_conn *conn,
1221 uint8_t smb_command,
1222 uint8_t additional_flags,
1223 uint8_t clear_flags,
1224 uint16_t additional_flags2,
1225 uint16_t clear_flags2,
1226 uint32_t timeout_msec,
1227 uint32_t pid,
1228 struct smbXcli_tcon *tcon,
1229 struct smbXcli_session *session,
1230 uint8_t wct, uint16_t *vwv,
1231 int iov_count,
1232 struct iovec *bytes_iov)
1234 struct tevent_req *req;
1235 struct smbXcli_req_state *state;
1236 uint8_t flags = 0;
1237 uint16_t flags2 = 0;
1238 uint16_t uid = 0;
1239 uint16_t tid = 0;
1241 if (iov_count > MAX_SMB_IOV) {
1243 * Should not happen :-)
1245 return NULL;
1248 req = tevent_req_create(mem_ctx, &state,
1249 struct smbXcli_req_state);
1250 if (req == NULL) {
1251 return NULL;
1253 state->ev = ev;
1254 state->conn = conn;
1255 state->session = session;
1256 state->tcon = tcon;
1258 if (session) {
1259 uid = session->smb1.session_id;
1262 if (tcon) {
1263 tid = tcon->smb1.tcon_id;
1266 state->smb1.recv_cmd = 0xFF;
1267 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1268 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1269 if (state->smb1.recv_iov == NULL) {
1270 TALLOC_FREE(req);
1271 return NULL;
1274 smb1cli_req_flags(conn->protocol,
1275 conn->smb1.capabilities,
1276 smb_command,
1277 additional_flags,
1278 clear_flags,
1279 &flags,
1280 additional_flags2,
1281 clear_flags2,
1282 &flags2);
1284 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1285 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1286 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1287 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1288 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1289 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1290 SSVAL(state->smb1.hdr, HDR_TID, tid);
1291 SSVAL(state->smb1.hdr, HDR_PID, pid);
1292 SSVAL(state->smb1.hdr, HDR_UID, uid);
1293 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1294 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1296 state->smb1.vwv = vwv;
1298 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1300 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1301 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1302 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1303 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1304 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1305 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1306 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1307 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1309 if (iov_count != 0) {
1310 memcpy(&state->smb1.iov[4], bytes_iov,
1311 iov_count * sizeof(*bytes_iov));
1313 state->smb1.iov_count = iov_count + 4;
1315 if (timeout_msec > 0) {
1316 struct timeval endtime;
1318 endtime = timeval_current_ofs_msec(timeout_msec);
1319 if (!tevent_req_set_endtime(req, ev, endtime)) {
1320 return req;
1324 switch (smb_command) {
1325 case SMBtranss:
1326 case SMBtranss2:
1327 case SMBnttranss:
1328 state->one_way = true;
1329 break;
1330 case SMBntcancel:
1331 state->one_way = true;
1332 state->smb1.one_way_seqnum = true;
1333 break;
1334 case SMBlockingX:
1335 if ((wct == 8) &&
1336 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1337 state->one_way = true;
1339 break;
1342 return req;
1345 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1346 struct iovec *iov, int iov_count,
1347 uint32_t *seqnum,
1348 bool one_way_seqnum)
1350 TALLOC_CTX *frame = NULL;
1351 uint8_t *buf;
1354 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1355 * iovec directly. MD5Update would do that just fine.
1358 if (iov_count < 4) {
1359 return NT_STATUS_INVALID_PARAMETER_MIX;
1361 if (iov[0].iov_len != NBT_HDR_SIZE) {
1362 return NT_STATUS_INVALID_PARAMETER_MIX;
1364 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1365 return NT_STATUS_INVALID_PARAMETER_MIX;
1367 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1368 return NT_STATUS_INVALID_PARAMETER_MIX;
1370 if (iov[3].iov_len != sizeof(uint16_t)) {
1371 return NT_STATUS_INVALID_PARAMETER_MIX;
1374 frame = talloc_stackframe();
1376 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1377 if (buf == NULL) {
1378 return NT_STATUS_NO_MEMORY;
1381 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1382 one_way_seqnum);
1383 smb_signing_sign_pdu(conn->smb1.signing,
1384 buf, talloc_get_size(buf),
1385 *seqnum);
1386 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1388 TALLOC_FREE(frame);
1389 return NT_STATUS_OK;
1392 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1393 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1394 TALLOC_CTX *tmp_mem,
1395 uint8_t *inbuf);
1397 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1398 struct smbXcli_req_state *state,
1399 struct iovec *iov, int iov_count)
1401 struct tevent_req *subreq;
1402 NTSTATUS status;
1403 uint8_t cmd;
1404 uint16_t mid;
1406 if (!smbXcli_conn_is_connected(state->conn)) {
1407 return NT_STATUS_CONNECTION_DISCONNECTED;
1410 if (state->conn->protocol > PROTOCOL_NT1) {
1411 return NT_STATUS_REVISION_MISMATCH;
1414 if (iov_count < 4) {
1415 return NT_STATUS_INVALID_PARAMETER_MIX;
1417 if (iov[0].iov_len != NBT_HDR_SIZE) {
1418 return NT_STATUS_INVALID_PARAMETER_MIX;
1420 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1421 return NT_STATUS_INVALID_PARAMETER_MIX;
1423 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1424 return NT_STATUS_INVALID_PARAMETER_MIX;
1426 if (iov[3].iov_len != sizeof(uint16_t)) {
1427 return NT_STATUS_INVALID_PARAMETER_MIX;
1430 cmd = CVAL(iov[1].iov_base, HDR_COM);
1431 if (cmd == SMBreadBraw) {
1432 if (smbXcli_conn_has_async_calls(state->conn)) {
1433 return NT_STATUS_INVALID_PARAMETER_MIX;
1435 state->conn->smb1.read_braw_req = req;
1438 if (state->smb1.mid != 0) {
1439 mid = state->smb1.mid;
1440 } else {
1441 mid = smb1cli_alloc_mid(state->conn);
1443 SSVAL(iov[1].iov_base, HDR_MID, mid);
1445 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1447 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1448 &state->smb1.seqnum,
1449 state->smb1.one_way_seqnum);
1451 if (!NT_STATUS_IS_OK(status)) {
1452 return status;
1456 * If we supported multiple encrytion contexts
1457 * here we'd look up based on tid.
1459 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1460 char *buf, *enc_buf;
1462 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1463 if (buf == NULL) {
1464 return NT_STATUS_NO_MEMORY;
1466 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1467 (char *)buf, &enc_buf);
1468 TALLOC_FREE(buf);
1469 if (!NT_STATUS_IS_OK(status)) {
1470 DEBUG(0, ("Error in encrypting client message: %s\n",
1471 nt_errstr(status)));
1472 return status;
1474 buf = (char *)talloc_memdup(state, enc_buf,
1475 smb_len_nbt(enc_buf)+4);
1476 SAFE_FREE(enc_buf);
1477 if (buf == NULL) {
1478 return NT_STATUS_NO_MEMORY;
1480 iov[0].iov_base = (void *)buf;
1481 iov[0].iov_len = talloc_get_size(buf);
1482 iov_count = 1;
1485 if (state->conn->dispatch_incoming == NULL) {
1486 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1489 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1491 subreq = writev_send(state, state->ev, state->conn->outgoing,
1492 state->conn->write_fd, false, iov, iov_count);
1493 if (subreq == NULL) {
1494 return NT_STATUS_NO_MEMORY;
1496 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1497 return NT_STATUS_OK;
1500 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1501 struct tevent_context *ev,
1502 struct smbXcli_conn *conn,
1503 uint8_t smb_command,
1504 uint8_t additional_flags,
1505 uint8_t clear_flags,
1506 uint16_t additional_flags2,
1507 uint16_t clear_flags2,
1508 uint32_t timeout_msec,
1509 uint32_t pid,
1510 struct smbXcli_tcon *tcon,
1511 struct smbXcli_session *session,
1512 uint8_t wct, uint16_t *vwv,
1513 uint32_t num_bytes,
1514 const uint8_t *bytes)
1516 struct tevent_req *req;
1517 struct iovec iov;
1518 NTSTATUS status;
1520 iov.iov_base = discard_const_p(void, bytes);
1521 iov.iov_len = num_bytes;
1523 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1524 additional_flags, clear_flags,
1525 additional_flags2, clear_flags2,
1526 timeout_msec,
1527 pid, tcon, session,
1528 wct, vwv, 1, &iov);
1529 if (req == NULL) {
1530 return NULL;
1532 if (!tevent_req_is_in_progress(req)) {
1533 return tevent_req_post(req, ev);
1535 status = smb1cli_req_chain_submit(&req, 1);
1536 if (tevent_req_nterror(req, status)) {
1537 return tevent_req_post(req, ev);
1539 return req;
1542 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1544 struct tevent_req *req =
1545 tevent_req_callback_data(subreq,
1546 struct tevent_req);
1547 struct smbXcli_req_state *state =
1548 tevent_req_data(req,
1549 struct smbXcli_req_state);
1550 ssize_t nwritten;
1551 int err;
1553 nwritten = writev_recv(subreq, &err);
1554 TALLOC_FREE(subreq);
1555 if (nwritten == -1) {
1556 NTSTATUS status = map_nt_error_from_unix_common(err);
1557 smbXcli_conn_disconnect(state->conn, status);
1558 return;
1561 if (state->one_way) {
1562 state->inbuf = NULL;
1563 tevent_req_done(req);
1564 return;
1567 if (!smbXcli_req_set_pending(req)) {
1568 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1569 return;
1573 static void smbXcli_conn_received(struct tevent_req *subreq)
1575 struct smbXcli_conn *conn =
1576 tevent_req_callback_data(subreq,
1577 struct smbXcli_conn);
1578 TALLOC_CTX *frame = talloc_stackframe();
1579 NTSTATUS status;
1580 uint8_t *inbuf;
1581 ssize_t received;
1582 int err;
1584 if (subreq != conn->read_smb_req) {
1585 DEBUG(1, ("Internal error: cli_smb_received called with "
1586 "unexpected subreq\n"));
1587 status = NT_STATUS_INTERNAL_ERROR;
1588 smbXcli_conn_disconnect(conn, status);
1589 TALLOC_FREE(frame);
1590 return;
1592 conn->read_smb_req = NULL;
1594 received = read_smb_recv(subreq, frame, &inbuf, &err);
1595 TALLOC_FREE(subreq);
1596 if (received == -1) {
1597 status = map_nt_error_from_unix_common(err);
1598 smbXcli_conn_disconnect(conn, status);
1599 TALLOC_FREE(frame);
1600 return;
1603 status = conn->dispatch_incoming(conn, frame, inbuf);
1604 TALLOC_FREE(frame);
1605 if (NT_STATUS_IS_OK(status)) {
1607 * We should not do any more processing
1608 * as the dispatch function called
1609 * tevent_req_done().
1611 return;
1612 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1614 * We got an error, so notify all pending requests
1616 smbXcli_conn_disconnect(conn, status);
1617 return;
1621 * We got NT_STATUS_RETRY, so we may ask for a
1622 * next incoming pdu.
1624 if (!smbXcli_conn_receive_next(conn)) {
1625 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1629 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1630 struct iovec **piov, int *pnum_iov)
1632 struct iovec *iov;
1633 int num_iov;
1634 size_t buflen;
1635 size_t taken;
1636 size_t remaining;
1637 uint8_t *hdr;
1638 uint8_t cmd;
1639 uint32_t wct_ofs;
1640 NTSTATUS status;
1641 size_t min_size = MIN_SMB_SIZE;
1643 buflen = smb_len_tcp(buf);
1644 taken = 0;
1646 hdr = buf + NBT_HDR_SIZE;
1648 status = smb1cli_pull_raw_error(hdr);
1649 if (NT_STATUS_IS_ERR(status)) {
1651 * This is an ugly hack to support OS/2
1652 * which skips the byte_count in the DATA block
1653 * on some error responses.
1655 * See bug #9096
1657 min_size -= sizeof(uint16_t);
1660 if (buflen < min_size) {
1661 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1665 * This returns iovec elements in the following order:
1667 * - SMB header
1669 * - Parameter Block
1670 * - Data Block
1672 * - Parameter Block
1673 * - Data Block
1675 * - Parameter Block
1676 * - Data Block
1678 num_iov = 1;
1680 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1681 if (iov == NULL) {
1682 return NT_STATUS_NO_MEMORY;
1684 iov[0].iov_base = hdr;
1685 iov[0].iov_len = HDR_WCT;
1686 taken += HDR_WCT;
1688 cmd = CVAL(hdr, HDR_COM);
1689 wct_ofs = HDR_WCT;
1691 while (true) {
1692 size_t len = buflen - taken;
1693 struct iovec *cur;
1694 struct iovec *iov_tmp;
1695 uint8_t wct;
1696 uint32_t bcc_ofs;
1697 uint16_t bcc;
1698 size_t needed;
1701 * we need at least WCT
1703 needed = sizeof(uint8_t);
1704 if (len < needed) {
1705 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1706 __location__, (int)len, (int)needed));
1707 goto inval;
1711 * Now we check if the specified words are there
1713 wct = CVAL(hdr, wct_ofs);
1714 needed += wct * sizeof(uint16_t);
1715 if (len < needed) {
1716 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1717 __location__, (int)len, (int)needed));
1718 goto inval;
1721 if ((num_iov == 1) &&
1722 (len == needed) &&
1723 NT_STATUS_IS_ERR(status))
1726 * This is an ugly hack to support OS/2
1727 * which skips the byte_count in the DATA block
1728 * on some error responses.
1730 * See bug #9096
1732 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1733 num_iov + 2);
1734 if (iov_tmp == NULL) {
1735 TALLOC_FREE(iov);
1736 return NT_STATUS_NO_MEMORY;
1738 iov = iov_tmp;
1739 cur = &iov[num_iov];
1740 num_iov += 2;
1742 cur[0].iov_len = 0;
1743 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1744 cur[1].iov_len = 0;
1745 cur[1].iov_base = cur[0].iov_base;
1747 taken += needed;
1748 break;
1752 * we need at least BCC
1754 needed += sizeof(uint16_t);
1755 if (len < needed) {
1756 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1757 __location__, (int)len, (int)needed));
1758 goto inval;
1762 * Now we check if the specified bytes are there
1764 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1765 bcc = SVAL(hdr, bcc_ofs);
1766 needed += bcc * sizeof(uint8_t);
1767 if (len < needed) {
1768 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1769 __location__, (int)len, (int)needed));
1770 goto inval;
1774 * we allocate 2 iovec structures for words and bytes
1776 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1777 num_iov + 2);
1778 if (iov_tmp == NULL) {
1779 TALLOC_FREE(iov);
1780 return NT_STATUS_NO_MEMORY;
1782 iov = iov_tmp;
1783 cur = &iov[num_iov];
1784 num_iov += 2;
1786 cur[0].iov_len = wct * sizeof(uint16_t);
1787 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1788 cur[1].iov_len = bcc * sizeof(uint8_t);
1789 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1791 taken += needed;
1793 if (!smb1cli_is_andx_req(cmd)) {
1795 * If the current command does not have AndX chanining
1796 * we are done.
1798 break;
1801 if (wct == 0 && bcc == 0) {
1803 * An empty response also ends the chain,
1804 * most likely with an error.
1806 break;
1809 if (wct < 2) {
1810 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1811 __location__, (int)wct, (int)cmd));
1812 goto inval;
1814 cmd = CVAL(cur[0].iov_base, 0);
1815 if (cmd == 0xFF) {
1817 * If it is the end of the chain we are also done.
1819 break;
1821 wct_ofs = SVAL(cur[0].iov_base, 2);
1823 if (wct_ofs < taken) {
1824 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1826 if (wct_ofs > buflen) {
1827 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1831 * we consumed everything up to the start of the next
1832 * parameter block.
1834 taken = wct_ofs;
1837 remaining = buflen - taken;
1839 if (remaining > 0 && num_iov >= 3) {
1841 * The last DATA block gets the remaining
1842 * bytes, this is needed to support
1843 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1845 iov[num_iov-1].iov_len += remaining;
1848 *piov = iov;
1849 *pnum_iov = num_iov;
1850 return NT_STATUS_OK;
1852 inval:
1853 TALLOC_FREE(iov);
1854 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1857 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1858 TALLOC_CTX *tmp_mem,
1859 uint8_t *inbuf)
1861 struct tevent_req *req;
1862 struct smbXcli_req_state *state;
1863 NTSTATUS status;
1864 size_t num_pending;
1865 size_t i;
1866 uint8_t cmd;
1867 uint16_t mid;
1868 bool oplock_break;
1869 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1870 size_t len = smb_len_tcp(inbuf);
1871 struct iovec *iov = NULL;
1872 int num_iov = 0;
1873 struct tevent_req **chain = NULL;
1874 size_t num_chained = 0;
1875 size_t num_responses = 0;
1877 if (conn->smb1.read_braw_req != NULL) {
1878 req = conn->smb1.read_braw_req;
1879 conn->smb1.read_braw_req = NULL;
1880 state = tevent_req_data(req, struct smbXcli_req_state);
1882 smbXcli_req_unset_pending(req);
1884 if (state->smb1.recv_iov == NULL) {
1886 * For requests with more than
1887 * one response, we have to readd the
1888 * recv_iov array.
1890 state->smb1.recv_iov = talloc_zero_array(state,
1891 struct iovec,
1893 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1894 return NT_STATUS_OK;
1898 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1899 state->smb1.recv_iov[0].iov_len = len;
1900 ZERO_STRUCT(state->smb1.recv_iov[1]);
1901 ZERO_STRUCT(state->smb1.recv_iov[2]);
1903 state->smb1.recv_cmd = SMBreadBraw;
1904 state->smb1.recv_status = NT_STATUS_OK;
1905 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1907 tevent_req_done(req);
1908 return NT_STATUS_OK;
1911 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1912 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1913 DEBUG(10, ("Got non-SMB PDU\n"));
1914 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1918 * If we supported multiple encrytion contexts
1919 * here we'd look up based on tid.
1921 if (common_encryption_on(conn->smb1.trans_enc)
1922 && (CVAL(inbuf, 0) == 0)) {
1923 uint16_t enc_ctx_num;
1925 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1926 if (!NT_STATUS_IS_OK(status)) {
1927 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1928 nt_errstr(status)));
1929 return status;
1932 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1933 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1934 enc_ctx_num,
1935 conn->smb1.trans_enc->enc_ctx_num));
1936 return NT_STATUS_INVALID_HANDLE;
1939 status = common_decrypt_buffer(conn->smb1.trans_enc,
1940 (char *)inbuf);
1941 if (!NT_STATUS_IS_OK(status)) {
1942 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1943 nt_errstr(status)));
1944 return status;
1946 inhdr = inbuf + NBT_HDR_SIZE;
1947 len = smb_len_nbt(inbuf);
1950 mid = SVAL(inhdr, HDR_MID);
1951 num_pending = talloc_array_length(conn->pending);
1953 for (i=0; i<num_pending; i++) {
1954 if (mid == smb1cli_req_mid(conn->pending[i])) {
1955 break;
1958 if (i == num_pending) {
1959 /* Dump unexpected reply */
1960 return NT_STATUS_RETRY;
1963 oplock_break = false;
1965 if (mid == 0xffff) {
1967 * Paranoia checks that this is really an oplock break request.
1969 oplock_break = (len == 51); /* hdr + 8 words */
1970 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1971 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1972 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1973 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1975 if (!oplock_break) {
1976 /* Dump unexpected reply */
1977 return NT_STATUS_RETRY;
1981 req = conn->pending[i];
1982 state = tevent_req_data(req, struct smbXcli_req_state);
1984 if (!oplock_break /* oplock breaks are not signed */
1985 && !smb_signing_check_pdu(conn->smb1.signing,
1986 inhdr, len, state->smb1.seqnum+1)) {
1987 DEBUG(10, ("cli_check_sign_mac failed\n"));
1988 return NT_STATUS_ACCESS_DENIED;
1991 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1992 &iov, &num_iov);
1993 if (!NT_STATUS_IS_OK(status)) {
1994 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1995 nt_errstr(status)));
1996 return status;
1999 cmd = CVAL(inhdr, HDR_COM);
2000 status = smb1cli_pull_raw_error(inhdr);
2002 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2003 (state->session != NULL) && state->session->disconnect_expired)
2006 * this should be a short term hack
2007 * until the upper layers have implemented
2008 * re-authentication.
2010 return status;
2013 if (state->smb1.chained_requests == NULL) {
2014 if (num_iov != 3) {
2015 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2018 smbXcli_req_unset_pending(req);
2020 if (state->smb1.recv_iov == NULL) {
2022 * For requests with more than
2023 * one response, we have to readd the
2024 * recv_iov array.
2026 state->smb1.recv_iov = talloc_zero_array(state,
2027 struct iovec,
2029 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2030 return NT_STATUS_OK;
2034 state->smb1.recv_cmd = cmd;
2035 state->smb1.recv_status = status;
2036 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2038 state->smb1.recv_iov[0] = iov[0];
2039 state->smb1.recv_iov[1] = iov[1];
2040 state->smb1.recv_iov[2] = iov[2];
2042 if (talloc_array_length(conn->pending) == 0) {
2043 tevent_req_done(req);
2044 return NT_STATUS_OK;
2047 tevent_req_defer_callback(req, state->ev);
2048 tevent_req_done(req);
2049 return NT_STATUS_RETRY;
2052 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2053 num_chained = talloc_array_length(chain);
2054 num_responses = (num_iov - 1)/2;
2056 if (num_responses > num_chained) {
2057 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2060 for (i=0; i<num_chained; i++) {
2061 size_t iov_idx = 1 + (i*2);
2062 struct iovec *cur = &iov[iov_idx];
2063 uint8_t *inbuf_ref;
2065 req = chain[i];
2066 state = tevent_req_data(req, struct smbXcli_req_state);
2068 smbXcli_req_unset_pending(req);
2071 * as we finish multiple requests here
2072 * we need to defer the callbacks as
2073 * they could destroy our current stack state.
2075 tevent_req_defer_callback(req, state->ev);
2077 if (i >= num_responses) {
2078 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2079 continue;
2082 if (state->smb1.recv_iov == NULL) {
2084 * For requests with more than
2085 * one response, we have to readd the
2086 * recv_iov array.
2088 state->smb1.recv_iov = talloc_zero_array(state,
2089 struct iovec,
2091 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2092 continue;
2096 state->smb1.recv_cmd = cmd;
2098 if (i == (num_responses - 1)) {
2100 * The last request in the chain gets the status
2102 state->smb1.recv_status = status;
2103 } else {
2104 cmd = CVAL(cur[0].iov_base, 0);
2105 state->smb1.recv_status = NT_STATUS_OK;
2108 state->inbuf = inbuf;
2111 * Note: here we use talloc_reference() in a way
2112 * that does not expose it to the caller.
2114 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2115 if (tevent_req_nomem(inbuf_ref, req)) {
2116 continue;
2119 /* copy the related buffers */
2120 state->smb1.recv_iov[0] = iov[0];
2121 state->smb1.recv_iov[1] = cur[0];
2122 state->smb1.recv_iov[2] = cur[1];
2124 tevent_req_done(req);
2127 return NT_STATUS_RETRY;
2130 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2131 TALLOC_CTX *mem_ctx,
2132 struct iovec **piov,
2133 uint8_t **phdr,
2134 uint8_t *pwct,
2135 uint16_t **pvwv,
2136 uint32_t *pvwv_offset,
2137 uint32_t *pnum_bytes,
2138 uint8_t **pbytes,
2139 uint32_t *pbytes_offset,
2140 uint8_t **pinbuf,
2141 const struct smb1cli_req_expected_response *expected,
2142 size_t num_expected)
2144 struct smbXcli_req_state *state =
2145 tevent_req_data(req,
2146 struct smbXcli_req_state);
2147 NTSTATUS status = NT_STATUS_OK;
2148 struct iovec *recv_iov = NULL;
2149 uint8_t *hdr = NULL;
2150 uint8_t wct = 0;
2151 uint32_t vwv_offset = 0;
2152 uint16_t *vwv = NULL;
2153 uint32_t num_bytes = 0;
2154 uint32_t bytes_offset = 0;
2155 uint8_t *bytes = NULL;
2156 size_t i;
2157 bool found_status = false;
2158 bool found_size = false;
2160 if (piov != NULL) {
2161 *piov = NULL;
2163 if (phdr != NULL) {
2164 *phdr = 0;
2166 if (pwct != NULL) {
2167 *pwct = 0;
2169 if (pvwv != NULL) {
2170 *pvwv = NULL;
2172 if (pvwv_offset != NULL) {
2173 *pvwv_offset = 0;
2175 if (pnum_bytes != NULL) {
2176 *pnum_bytes = 0;
2178 if (pbytes != NULL) {
2179 *pbytes = NULL;
2181 if (pbytes_offset != NULL) {
2182 *pbytes_offset = 0;
2184 if (pinbuf != NULL) {
2185 *pinbuf = NULL;
2188 if (state->inbuf != NULL) {
2189 recv_iov = state->smb1.recv_iov;
2190 state->smb1.recv_iov = NULL;
2191 if (state->smb1.recv_cmd != SMBreadBraw) {
2192 hdr = (uint8_t *)recv_iov[0].iov_base;
2193 wct = recv_iov[1].iov_len/2;
2194 vwv = (uint16_t *)recv_iov[1].iov_base;
2195 vwv_offset = PTR_DIFF(vwv, hdr);
2196 num_bytes = recv_iov[2].iov_len;
2197 bytes = (uint8_t *)recv_iov[2].iov_base;
2198 bytes_offset = PTR_DIFF(bytes, hdr);
2202 if (tevent_req_is_nterror(req, &status)) {
2203 for (i=0; i < num_expected; i++) {
2204 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2205 found_status = true;
2206 break;
2210 if (found_status) {
2211 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2214 return status;
2217 if (num_expected == 0) {
2218 found_status = true;
2219 found_size = true;
2222 status = state->smb1.recv_status;
2224 for (i=0; i < num_expected; i++) {
2225 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2226 continue;
2229 found_status = true;
2230 if (expected[i].wct == 0) {
2231 found_size = true;
2232 break;
2235 if (expected[i].wct == wct) {
2236 found_size = true;
2237 break;
2241 if (!found_status) {
2242 return status;
2245 if (!found_size) {
2246 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2249 if (piov != NULL) {
2250 *piov = talloc_move(mem_ctx, &recv_iov);
2253 if (phdr != NULL) {
2254 *phdr = hdr;
2256 if (pwct != NULL) {
2257 *pwct = wct;
2259 if (pvwv != NULL) {
2260 *pvwv = vwv;
2262 if (pvwv_offset != NULL) {
2263 *pvwv_offset = vwv_offset;
2265 if (pnum_bytes != NULL) {
2266 *pnum_bytes = num_bytes;
2268 if (pbytes != NULL) {
2269 *pbytes = bytes;
2271 if (pbytes_offset != NULL) {
2272 *pbytes_offset = bytes_offset;
2274 if (pinbuf != NULL) {
2275 *pinbuf = state->inbuf;
2278 return status;
2281 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2283 size_t wct_ofs;
2284 int i;
2286 wct_ofs = HDR_WCT;
2288 for (i=0; i<num_reqs; i++) {
2289 struct smbXcli_req_state *state;
2290 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2291 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2292 state->smb1.iov_count-2);
2293 wct_ofs = (wct_ofs + 3) & ~3;
2295 return wct_ofs;
2298 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2300 struct smbXcli_req_state *first_state =
2301 tevent_req_data(reqs[0],
2302 struct smbXcli_req_state);
2303 struct smbXcli_req_state *state;
2304 size_t wct_offset;
2305 size_t chain_padding = 0;
2306 int i, iovlen;
2307 struct iovec *iov = NULL;
2308 struct iovec *this_iov;
2309 NTSTATUS status;
2310 size_t nbt_len;
2312 if (num_reqs == 1) {
2313 return smb1cli_req_writev_submit(reqs[0], first_state,
2314 first_state->smb1.iov,
2315 first_state->smb1.iov_count);
2318 iovlen = 0;
2319 for (i=0; i<num_reqs; i++) {
2320 if (!tevent_req_is_in_progress(reqs[i])) {
2321 return NT_STATUS_INTERNAL_ERROR;
2324 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2326 if (state->smb1.iov_count < 4) {
2327 return NT_STATUS_INVALID_PARAMETER_MIX;
2330 if (i == 0) {
2332 * The NBT and SMB header
2334 iovlen += 2;
2335 } else {
2337 * Chain padding
2339 iovlen += 1;
2343 * words and bytes
2345 iovlen += state->smb1.iov_count - 2;
2348 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2349 if (iov == NULL) {
2350 return NT_STATUS_NO_MEMORY;
2353 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2354 first_state, reqs, sizeof(*reqs) * num_reqs);
2355 if (first_state->smb1.chained_requests == NULL) {
2356 TALLOC_FREE(iov);
2357 return NT_STATUS_NO_MEMORY;
2360 wct_offset = HDR_WCT;
2361 this_iov = iov;
2363 for (i=0; i<num_reqs; i++) {
2364 size_t next_padding = 0;
2365 uint16_t *vwv;
2367 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2369 if (i < num_reqs-1) {
2370 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2371 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2372 TALLOC_FREE(iov);
2373 TALLOC_FREE(first_state->smb1.chained_requests);
2374 return NT_STATUS_INVALID_PARAMETER_MIX;
2378 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2379 state->smb1.iov_count-2) + 1;
2380 if ((wct_offset % 4) != 0) {
2381 next_padding = 4 - (wct_offset % 4);
2383 wct_offset += next_padding;
2384 vwv = state->smb1.vwv;
2386 if (i < num_reqs-1) {
2387 struct smbXcli_req_state *next_state =
2388 tevent_req_data(reqs[i+1],
2389 struct smbXcli_req_state);
2390 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2391 SCVAL(vwv+0, 1, 0);
2392 SSVAL(vwv+1, 0, wct_offset);
2393 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2394 /* properly end the chain */
2395 SCVAL(vwv+0, 0, 0xff);
2396 SCVAL(vwv+0, 1, 0xff);
2397 SSVAL(vwv+1, 0, 0);
2400 if (i == 0) {
2402 * The NBT and SMB header
2404 this_iov[0] = state->smb1.iov[0];
2405 this_iov[1] = state->smb1.iov[1];
2406 this_iov += 2;
2407 } else {
2409 * This one is a bit subtle. We have to add
2410 * chain_padding bytes between the requests, and we
2411 * have to also include the wct field of the
2412 * subsequent requests. We use the subsequent header
2413 * for the padding, it contains the wct field in its
2414 * last byte.
2416 this_iov[0].iov_len = chain_padding+1;
2417 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2418 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2419 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2420 this_iov += 1;
2424 * copy the words and bytes
2426 memcpy(this_iov, state->smb1.iov+2,
2427 sizeof(struct iovec) * (state->smb1.iov_count-2));
2428 this_iov += state->smb1.iov_count - 2;
2429 chain_padding = next_padding;
2432 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2433 if (nbt_len > first_state->conn->smb1.max_xmit) {
2434 TALLOC_FREE(iov);
2435 TALLOC_FREE(first_state->smb1.chained_requests);
2436 return NT_STATUS_INVALID_PARAMETER_MIX;
2439 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2440 if (!NT_STATUS_IS_OK(status)) {
2441 TALLOC_FREE(iov);
2442 TALLOC_FREE(first_state->smb1.chained_requests);
2443 return status;
2446 return NT_STATUS_OK;
2449 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2451 return ((tevent_queue_length(conn->outgoing) != 0)
2452 || (talloc_array_length(conn->pending) != 0));
2455 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2457 uint16_t credits = 1;
2459 if (conn->smb2.cur_credits == 0) {
2460 if (max_dyn_len != NULL) {
2461 *max_dyn_len = 0;
2463 return false;
2466 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2467 credits = conn->smb2.cur_credits;
2470 if (max_dyn_len != NULL) {
2471 *max_dyn_len = credits * 65536;
2474 return true;
2477 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2479 return conn->smb2.server.capabilities;
2482 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2484 return conn->smb2.server.security_mode;
2487 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2489 return conn->smb2.server.max_trans_size;
2492 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2494 return conn->smb2.server.max_read_size;
2497 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2499 return conn->smb2.server.max_write_size;
2502 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2503 uint16_t max_credits)
2505 conn->smb2.max_credits = max_credits;
2508 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2510 static bool smb2cli_req_cancel(struct tevent_req *req)
2512 struct smbXcli_req_state *state =
2513 tevent_req_data(req,
2514 struct smbXcli_req_state);
2515 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2516 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2517 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2518 struct smbXcli_tcon *tcon = state->tcon;
2519 struct smbXcli_session *session = state->session;
2520 uint8_t *fixed = state->smb2.pad;
2521 uint16_t fixed_len = 4;
2522 struct tevent_req *subreq;
2523 struct smbXcli_req_state *substate;
2524 NTSTATUS status;
2526 SSVAL(fixed, 0, 0x04);
2527 SSVAL(fixed, 2, 0);
2529 subreq = smb2cli_req_create(state, state->ev,
2530 state->conn,
2531 SMB2_OP_CANCEL,
2532 flags, 0,
2533 0, /* timeout */
2534 tcon, session,
2535 fixed, fixed_len,
2536 NULL, 0, 0);
2537 if (subreq == NULL) {
2538 return false;
2540 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2543 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2544 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2546 flags &= SMB2_HDR_FLAG_ASYNC;
2548 if (flags & SMB2_HDR_FLAG_ASYNC) {
2549 mid = 0;
2552 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2553 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2554 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2556 status = smb2cli_req_compound_submit(&subreq, 1);
2557 if (!NT_STATUS_IS_OK(status)) {
2558 TALLOC_FREE(subreq);
2559 return false;
2562 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2564 return true;
2567 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2569 /* we do not care about the result */
2570 TALLOC_FREE(subreq);
2573 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2574 struct tevent_context *ev,
2575 struct smbXcli_conn *conn,
2576 uint16_t cmd,
2577 uint32_t additional_flags,
2578 uint32_t clear_flags,
2579 uint32_t timeout_msec,
2580 struct smbXcli_tcon *tcon,
2581 struct smbXcli_session *session,
2582 const uint8_t *fixed,
2583 uint16_t fixed_len,
2584 const uint8_t *dyn,
2585 uint32_t dyn_len,
2586 uint32_t max_dyn_len)
2588 struct tevent_req *req;
2589 struct smbXcli_req_state *state;
2590 uint32_t flags = 0;
2591 uint32_t tid = 0;
2592 uint64_t uid = 0;
2593 bool use_channel_sequence = false;
2594 uint16_t channel_sequence = 0;
2596 req = tevent_req_create(mem_ctx, &state,
2597 struct smbXcli_req_state);
2598 if (req == NULL) {
2599 return NULL;
2602 state->ev = ev;
2603 state->conn = conn;
2604 state->session = session;
2605 state->tcon = tcon;
2607 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2608 use_channel_sequence = true;
2609 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2610 use_channel_sequence = true;
2613 if (session) {
2614 uid = session->smb2->session_id;
2616 if (use_channel_sequence) {
2617 channel_sequence = session->smb2->channel_sequence;
2620 state->smb2.should_sign = session->smb2->should_sign;
2621 state->smb2.should_encrypt = session->smb2->should_encrypt;
2623 if (cmd == SMB2_OP_SESSSETUP &&
2624 session->smb2->signing_key.length != 0) {
2625 state->smb2.should_sign = true;
2628 if (cmd == SMB2_OP_SESSSETUP &&
2629 session->smb2_channel.signing_key.length == 0) {
2630 state->smb2.should_encrypt = false;
2634 if (tcon) {
2635 tid = tcon->smb2.tcon_id;
2637 if (tcon->smb2.should_encrypt) {
2638 state->smb2.should_encrypt = true;
2642 if (state->smb2.should_encrypt) {
2643 state->smb2.should_sign = false;
2646 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2647 if (state->smb2.recv_iov == NULL) {
2648 TALLOC_FREE(req);
2649 return NULL;
2652 flags |= additional_flags;
2653 flags &= ~clear_flags;
2655 state->smb2.fixed = fixed;
2656 state->smb2.fixed_len = fixed_len;
2657 state->smb2.dyn = dyn;
2658 state->smb2.dyn_len = dyn_len;
2659 state->smb2.max_dyn_len = max_dyn_len;
2661 if (state->smb2.should_encrypt) {
2662 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2663 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2666 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2667 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2668 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2669 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2670 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2671 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2672 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2673 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2675 switch (cmd) {
2676 case SMB2_OP_CANCEL:
2677 state->one_way = true;
2678 break;
2679 case SMB2_OP_BREAK:
2681 * If this is a dummy request, it will have
2682 * UINT64_MAX as message id.
2683 * If we send on break acknowledgement,
2684 * this gets overwritten later.
2686 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2687 break;
2690 if (timeout_msec > 0) {
2691 struct timeval endtime;
2693 endtime = timeval_current_ofs_msec(timeout_msec);
2694 if (!tevent_req_set_endtime(req, ev, endtime)) {
2695 return req;
2699 return req;
2702 void smb2cli_req_set_notify_async(struct tevent_req *req)
2704 struct smbXcli_req_state *state =
2705 tevent_req_data(req,
2706 struct smbXcli_req_state);
2708 state->smb2.notify_async = true;
2711 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2712 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2713 TALLOC_CTX *tmp_mem,
2714 uint8_t *inbuf);
2716 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2717 int num_reqs)
2719 struct smbXcli_req_state *state;
2720 struct tevent_req *subreq;
2721 struct iovec *iov;
2722 int i, num_iov, nbt_len;
2723 int tf_iov = -1;
2724 const DATA_BLOB *encryption_key = NULL;
2725 uint64_t encryption_session_id = 0;
2728 * 1 for the nbt length, optional TRANSFORM
2729 * per request: HDR, fixed, dyn, padding
2730 * -1 because the last one does not need padding
2733 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2734 if (iov == NULL) {
2735 return NT_STATUS_NO_MEMORY;
2738 num_iov = 1;
2739 nbt_len = 0;
2742 * the session of the first request that requires encryption
2743 * specifies the encryption key.
2745 for (i=0; i<num_reqs; i++) {
2746 if (!tevent_req_is_in_progress(reqs[i])) {
2747 return NT_STATUS_INTERNAL_ERROR;
2750 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2752 if (!smbXcli_conn_is_connected(state->conn)) {
2753 return NT_STATUS_CONNECTION_DISCONNECTED;
2756 if ((state->conn->protocol != PROTOCOL_NONE) &&
2757 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2758 return NT_STATUS_REVISION_MISMATCH;
2761 if (state->session == NULL) {
2762 continue;
2765 if (!state->smb2.should_encrypt) {
2766 continue;
2769 encryption_key = &state->session->smb2->encryption_key;
2770 if (encryption_key->length == 0) {
2771 return NT_STATUS_INVALID_PARAMETER_MIX;
2774 encryption_session_id = state->session->smb2->session_id;
2776 tf_iov = num_iov;
2777 iov[num_iov].iov_base = state->smb2.transform;
2778 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2779 num_iov += 1;
2781 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2782 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2783 state->session->smb2->nonce_low);
2784 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2785 state->session->smb2->nonce_high);
2786 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2787 encryption_session_id);
2789 state->session->smb2->nonce_low += 1;
2790 if (state->session->smb2->nonce_low == 0) {
2791 state->session->smb2->nonce_high += 1;
2792 state->session->smb2->nonce_low += 1;
2795 nbt_len += SMB2_TF_HDR_SIZE;
2796 break;
2799 for (i=0; i<num_reqs; i++) {
2800 int hdr_iov;
2801 size_t reqlen;
2802 bool ret;
2803 uint16_t opcode;
2804 uint64_t avail;
2805 uint16_t charge;
2806 uint16_t credits;
2807 uint64_t mid;
2808 const DATA_BLOB *signing_key = NULL;
2810 if (!tevent_req_is_in_progress(reqs[i])) {
2811 return NT_STATUS_INTERNAL_ERROR;
2814 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2816 if (!smbXcli_conn_is_connected(state->conn)) {
2817 return NT_STATUS_CONNECTION_DISCONNECTED;
2820 if ((state->conn->protocol != PROTOCOL_NONE) &&
2821 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2822 return NT_STATUS_REVISION_MISMATCH;
2825 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2826 if (opcode == SMB2_OP_CANCEL) {
2827 goto skip_credits;
2830 avail = UINT64_MAX - state->conn->smb2.mid;
2831 if (avail < 1) {
2832 return NT_STATUS_CONNECTION_ABORTED;
2835 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2836 uint32_t max_dyn_len = 1;
2838 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2839 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2841 charge = (max_dyn_len - 1)/ 65536 + 1;
2842 } else {
2843 charge = 1;
2846 charge = MAX(state->smb2.credit_charge, charge);
2848 avail = MIN(avail, state->conn->smb2.cur_credits);
2849 if (avail < charge) {
2850 return NT_STATUS_INTERNAL_ERROR;
2853 credits = 0;
2854 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2855 credits = state->conn->smb2.max_credits -
2856 state->conn->smb2.cur_credits;
2858 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2859 credits += 1;
2862 mid = state->conn->smb2.mid;
2863 state->conn->smb2.mid += charge;
2864 state->conn->smb2.cur_credits -= charge;
2866 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2867 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2869 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2870 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2872 skip_credits:
2873 if (state->session && encryption_key == NULL) {
2875 * We prefer the channel signing key if it is
2876 * already there.
2878 if (state->smb2.should_sign) {
2879 signing_key = &state->session->smb2_channel.signing_key;
2883 * If it is a channel binding, we already have the main
2884 * signing key and try that one.
2886 if (signing_key && signing_key->length == 0) {
2887 signing_key = &state->session->smb2->signing_key;
2891 * If we do not have any session key yet, we skip the
2892 * signing of SMB2_OP_SESSSETUP requests.
2894 if (signing_key && signing_key->length == 0) {
2895 signing_key = NULL;
2899 hdr_iov = num_iov;
2900 iov[num_iov].iov_base = state->smb2.hdr;
2901 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2902 num_iov += 1;
2904 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2905 iov[num_iov].iov_len = state->smb2.fixed_len;
2906 num_iov += 1;
2908 if (state->smb2.dyn != NULL) {
2909 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2910 iov[num_iov].iov_len = state->smb2.dyn_len;
2911 num_iov += 1;
2914 reqlen = sizeof(state->smb2.hdr);
2915 reqlen += state->smb2.fixed_len;
2916 reqlen += state->smb2.dyn_len;
2918 if (i < num_reqs-1) {
2919 if ((reqlen % 8) > 0) {
2920 uint8_t pad = 8 - (reqlen % 8);
2921 iov[num_iov].iov_base = state->smb2.pad;
2922 iov[num_iov].iov_len = pad;
2923 num_iov += 1;
2924 reqlen += pad;
2926 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2929 state->smb2.encryption_session_id = encryption_session_id;
2931 if (signing_key != NULL) {
2932 NTSTATUS status;
2934 status = smb2_signing_sign_pdu(*signing_key,
2935 state->session->conn->protocol,
2936 &iov[hdr_iov], num_iov - hdr_iov);
2937 if (!NT_STATUS_IS_OK(status)) {
2938 return status;
2942 nbt_len += reqlen;
2944 ret = smbXcli_req_set_pending(reqs[i]);
2945 if (!ret) {
2946 return NT_STATUS_NO_MEMORY;
2950 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2951 _smb_setlen_tcp(state->length_hdr, nbt_len);
2952 iov[0].iov_base = state->length_hdr;
2953 iov[0].iov_len = sizeof(state->length_hdr);
2955 if (encryption_key != NULL) {
2956 NTSTATUS status;
2957 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2958 uint8_t *buf;
2959 int vi;
2961 buf = talloc_array(iov, uint8_t, buflen);
2962 if (buf == NULL) {
2963 return NT_STATUS_NO_MEMORY;
2967 * We copy the buffers before encrypting them,
2968 * this is at least currently needed for the
2969 * to keep state->smb2.hdr.
2971 * Also the callers may expect there buffers
2972 * to be const.
2974 for (vi = tf_iov + 1; vi < num_iov; vi++) {
2975 struct iovec *v = &iov[vi];
2976 const uint8_t *o = (const uint8_t *)v->iov_base;
2978 memcpy(buf, o, v->iov_len);
2979 v->iov_base = (void *)buf;
2980 buf += v->iov_len;
2983 status = smb2_signing_encrypt_pdu(*encryption_key,
2984 state->conn->protocol,
2985 &iov[tf_iov], num_iov - tf_iov);
2986 if (!NT_STATUS_IS_OK(status)) {
2987 return status;
2991 if (state->conn->dispatch_incoming == NULL) {
2992 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2995 subreq = writev_send(state, state->ev, state->conn->outgoing,
2996 state->conn->write_fd, false, iov, num_iov);
2997 if (subreq == NULL) {
2998 return NT_STATUS_NO_MEMORY;
3000 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3001 return NT_STATUS_OK;
3004 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3006 struct smbXcli_req_state *state =
3007 tevent_req_data(req,
3008 struct smbXcli_req_state);
3010 state->smb2.credit_charge = charge;
3013 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3014 struct tevent_context *ev,
3015 struct smbXcli_conn *conn,
3016 uint16_t cmd,
3017 uint32_t additional_flags,
3018 uint32_t clear_flags,
3019 uint32_t timeout_msec,
3020 struct smbXcli_tcon *tcon,
3021 struct smbXcli_session *session,
3022 const uint8_t *fixed,
3023 uint16_t fixed_len,
3024 const uint8_t *dyn,
3025 uint32_t dyn_len,
3026 uint32_t max_dyn_len)
3028 struct tevent_req *req;
3029 NTSTATUS status;
3031 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3032 additional_flags, clear_flags,
3033 timeout_msec,
3034 tcon, session,
3035 fixed, fixed_len,
3036 dyn, dyn_len,
3037 max_dyn_len);
3038 if (req == NULL) {
3039 return NULL;
3041 if (!tevent_req_is_in_progress(req)) {
3042 return tevent_req_post(req, ev);
3044 status = smb2cli_req_compound_submit(&req, 1);
3045 if (tevent_req_nterror(req, status)) {
3046 return tevent_req_post(req, ev);
3048 return req;
3051 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3053 struct tevent_req *req =
3054 tevent_req_callback_data(subreq,
3055 struct tevent_req);
3056 struct smbXcli_req_state *state =
3057 tevent_req_data(req,
3058 struct smbXcli_req_state);
3059 ssize_t nwritten;
3060 int err;
3062 nwritten = writev_recv(subreq, &err);
3063 TALLOC_FREE(subreq);
3064 if (nwritten == -1) {
3065 /* here, we need to notify all pending requests */
3066 NTSTATUS status = map_nt_error_from_unix_common(err);
3067 smbXcli_conn_disconnect(state->conn, status);
3068 return;
3072 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3073 uint8_t *buf,
3074 size_t buflen,
3075 TALLOC_CTX *mem_ctx,
3076 struct iovec **piov, int *pnum_iov)
3078 struct iovec *iov;
3079 int num_iov = 0;
3080 size_t taken = 0;
3081 uint8_t *first_hdr = buf;
3082 size_t verified_buflen = 0;
3083 uint8_t *tf = NULL;
3084 size_t tf_len = 0;
3086 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3087 if (iov == NULL) {
3088 return NT_STATUS_NO_MEMORY;
3091 while (taken < buflen) {
3092 size_t len = buflen - taken;
3093 uint8_t *hdr = first_hdr + taken;
3094 struct iovec *cur;
3095 size_t full_size;
3096 size_t next_command_ofs;
3097 uint16_t body_size;
3098 struct iovec *iov_tmp;
3100 if (verified_buflen > taken) {
3101 len = verified_buflen - taken;
3102 } else {
3103 tf = NULL;
3104 tf_len = 0;
3107 if (len < 4) {
3108 DEBUG(10, ("%d bytes left, expected at least %d\n",
3109 (int)len, 4));
3110 goto inval;
3112 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3113 struct smbXcli_session *s;
3114 uint64_t uid;
3115 struct iovec tf_iov[2];
3116 size_t enc_len;
3117 NTSTATUS status;
3119 if (len < SMB2_TF_HDR_SIZE) {
3120 DEBUG(10, ("%d bytes left, expected at least %d\n",
3121 (int)len, SMB2_TF_HDR_SIZE));
3122 goto inval;
3124 tf = hdr;
3125 tf_len = SMB2_TF_HDR_SIZE;
3126 taken += tf_len;
3128 hdr = first_hdr + taken;
3129 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3130 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3132 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3133 DEBUG(10, ("%d bytes left, expected at least %d\n",
3134 (int)len,
3135 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3136 goto inval;
3139 s = conn->sessions;
3140 for (; s; s = s->next) {
3141 if (s->smb2->session_id != uid) {
3142 continue;
3144 break;
3147 if (s == NULL) {
3148 DEBUG(10, ("unknown session_id %llu\n",
3149 (unsigned long long)uid));
3150 goto inval;
3153 tf_iov[0].iov_base = (void *)tf;
3154 tf_iov[0].iov_len = tf_len;
3155 tf_iov[1].iov_base = (void *)hdr;
3156 tf_iov[1].iov_len = enc_len;
3158 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3159 conn->protocol,
3160 tf_iov, 2);
3161 if (!NT_STATUS_IS_OK(status)) {
3162 TALLOC_FREE(iov);
3163 return status;
3166 verified_buflen = taken + enc_len;
3167 len = enc_len;
3171 * We need the header plus the body length field
3174 if (len < SMB2_HDR_BODY + 2) {
3175 DEBUG(10, ("%d bytes left, expected at least %d\n",
3176 (int)len, SMB2_HDR_BODY));
3177 goto inval;
3179 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3180 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3181 IVAL(hdr, 0)));
3182 goto inval;
3184 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3185 DEBUG(10, ("Got HDR len %d, expected %d\n",
3186 SVAL(hdr, 4), SMB2_HDR_BODY));
3187 goto inval;
3190 full_size = len;
3191 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3192 body_size = SVAL(hdr, SMB2_HDR_BODY);
3194 if (next_command_ofs != 0) {
3195 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3196 goto inval;
3198 if (next_command_ofs > full_size) {
3199 goto inval;
3201 full_size = next_command_ofs;
3203 if (body_size < 2) {
3204 goto inval;
3206 body_size &= 0xfffe;
3208 if (body_size > (full_size - SMB2_HDR_BODY)) {
3209 goto inval;
3212 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3213 num_iov + 4);
3214 if (iov_tmp == NULL) {
3215 TALLOC_FREE(iov);
3216 return NT_STATUS_NO_MEMORY;
3218 iov = iov_tmp;
3219 cur = &iov[num_iov];
3220 num_iov += 4;
3222 cur[0].iov_base = tf;
3223 cur[0].iov_len = tf_len;
3224 cur[1].iov_base = hdr;
3225 cur[1].iov_len = SMB2_HDR_BODY;
3226 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3227 cur[2].iov_len = body_size;
3228 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3229 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3231 taken += full_size;
3234 *piov = iov;
3235 *pnum_iov = num_iov;
3236 return NT_STATUS_OK;
3238 inval:
3239 TALLOC_FREE(iov);
3240 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3243 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3244 uint64_t mid)
3246 size_t num_pending = talloc_array_length(conn->pending);
3247 size_t i;
3249 for (i=0; i<num_pending; i++) {
3250 struct tevent_req *req = conn->pending[i];
3251 struct smbXcli_req_state *state =
3252 tevent_req_data(req,
3253 struct smbXcli_req_state);
3255 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3256 return req;
3259 return NULL;
3262 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3263 TALLOC_CTX *tmp_mem,
3264 uint8_t *inbuf)
3266 struct tevent_req *req;
3267 struct smbXcli_req_state *state = NULL;
3268 struct iovec *iov;
3269 int i, num_iov;
3270 NTSTATUS status;
3271 bool defer = true;
3272 struct smbXcli_session *last_session = NULL;
3273 size_t inbuf_len = smb_len_tcp(inbuf);
3275 status = smb2cli_inbuf_parse_compound(conn,
3276 inbuf + NBT_HDR_SIZE,
3277 inbuf_len,
3278 tmp_mem,
3279 &iov, &num_iov);
3280 if (!NT_STATUS_IS_OK(status)) {
3281 return status;
3284 for (i=0; i<num_iov; i+=4) {
3285 uint8_t *inbuf_ref = NULL;
3286 struct iovec *cur = &iov[i];
3287 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3288 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3289 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3290 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3291 uint16_t req_opcode;
3292 uint32_t req_flags;
3293 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3294 uint32_t new_credits;
3295 struct smbXcli_session *session = NULL;
3296 const DATA_BLOB *signing_key = NULL;
3297 bool was_encrypted = false;
3299 new_credits = conn->smb2.cur_credits;
3300 new_credits += credits;
3301 if (new_credits > UINT16_MAX) {
3302 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3304 conn->smb2.cur_credits += credits;
3306 req = smb2cli_conn_find_pending(conn, mid);
3307 if (req == NULL) {
3308 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3310 state = tevent_req_data(req, struct smbXcli_req_state);
3312 state->smb2.got_async = false;
3314 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3315 if (opcode != req_opcode) {
3316 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3318 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3320 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3321 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3324 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3325 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3326 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3327 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3330 * async interim responses are not signed,
3331 * even if the SMB2_HDR_FLAG_SIGNED flag
3332 * is set.
3334 req_flags |= SMB2_HDR_FLAG_ASYNC;
3335 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3336 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3338 if (state->smb2.notify_async) {
3339 state->smb2.got_async = true;
3340 tevent_req_defer_callback(req, state->ev);
3341 tevent_req_notify_callback(req);
3343 continue;
3346 session = state->session;
3347 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3348 session = last_session;
3350 last_session = session;
3352 if (state->smb2.should_sign) {
3353 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3354 return NT_STATUS_ACCESS_DENIED;
3358 if (flags & SMB2_HDR_FLAG_SIGNED) {
3359 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3361 if (session == NULL) {
3362 struct smbXcli_session *s;
3364 s = state->conn->sessions;
3365 for (; s; s = s->next) {
3366 if (s->smb2->session_id != uid) {
3367 continue;
3370 session = s;
3371 break;
3375 if (session == NULL) {
3376 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3379 last_session = session;
3380 signing_key = &session->smb2_channel.signing_key;
3383 if (opcode == SMB2_OP_SESSSETUP) {
3385 * We prefer the channel signing key, if it is
3386 * already there.
3388 * If we do not have a channel signing key yet,
3389 * we try the main signing key, if it is not
3390 * the final response.
3392 if (signing_key && signing_key->length == 0 &&
3393 !NT_STATUS_IS_OK(status)) {
3394 signing_key = &session->smb2->signing_key;
3397 if (signing_key && signing_key->length == 0) {
3399 * If we do not have a session key to
3400 * verify the signature, we defer the
3401 * signing check to the caller.
3403 * The caller gets NT_STATUS_OK, it
3404 * has to call
3405 * smb2cli_session_set_session_key()
3406 * or
3407 * smb2cli_session_set_channel_key()
3408 * which will check the signature
3409 * with the channel signing key.
3411 signing_key = NULL;
3415 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3416 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3417 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3420 * If the response was encrypted in a SMB2_TRANSFORM
3421 * pdu, which belongs to the correct session,
3422 * we do not need to do signing checks
3424 * It could be the session the response belongs to
3425 * or the session that was used to encrypt the
3426 * SMB2_TRANSFORM request.
3428 if ((session && session->smb2->session_id == uid) ||
3429 (state->smb2.encryption_session_id == uid)) {
3430 signing_key = NULL;
3431 was_encrypted = true;
3435 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3437 * if the server returns NT_STATUS_USER_SESSION_DELETED
3438 * the response is not signed and we should
3439 * propagate the NT_STATUS_USER_SESSION_DELETED
3440 * status to the caller.
3442 state->smb2.signing_skipped = true;
3443 signing_key = NULL;
3446 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3448 * if the server returns
3449 * NT_STATUS_INVALID_PARAMETER
3450 * the response might not be encrypted.
3452 if (state->smb2.should_encrypt && !was_encrypted) {
3453 state->smb2.signing_skipped = true;
3454 signing_key = NULL;
3458 if (state->smb2.should_encrypt && !was_encrypted) {
3459 if (!state->smb2.signing_skipped) {
3460 return NT_STATUS_ACCESS_DENIED;
3464 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3465 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3466 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3468 * if the server returns
3469 * NT_STATUS_NETWORK_NAME_DELETED
3470 * NT_STATUS_FILE_CLOSED
3471 * NT_STATUS_INVALID_PARAMETER
3472 * the response might not be signed
3473 * as this happens before the signing checks.
3475 * If server echos the signature (or all zeros)
3476 * we should report the status from the server
3477 * to the caller.
3479 if (signing_key) {
3480 int cmp;
3482 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3483 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3484 16);
3485 if (cmp == 0) {
3486 state->smb2.signing_skipped = true;
3487 signing_key = NULL;
3490 if (signing_key) {
3491 int cmp;
3492 static const uint8_t zeros[16];
3494 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3495 zeros,
3496 16);
3497 if (cmp == 0) {
3498 state->smb2.signing_skipped = true;
3499 signing_key = NULL;
3504 if (signing_key) {
3505 status = smb2_signing_check_pdu(*signing_key,
3506 state->conn->protocol,
3507 &cur[1], 3);
3508 if (!NT_STATUS_IS_OK(status)) {
3510 * If the signing check fails, we disconnect
3511 * the connection.
3513 return status;
3517 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3518 (session != NULL) && session->disconnect_expired)
3521 * this should be a short term hack
3522 * until the upper layers have implemented
3523 * re-authentication.
3525 return status;
3528 smbXcli_req_unset_pending(req);
3531 * There might be more than one response
3532 * we need to defer the notifications
3534 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3535 defer = false;
3538 if (defer) {
3539 tevent_req_defer_callback(req, state->ev);
3543 * Note: here we use talloc_reference() in a way
3544 * that does not expose it to the caller.
3546 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3547 if (tevent_req_nomem(inbuf_ref, req)) {
3548 continue;
3551 /* copy the related buffers */
3552 state->smb2.recv_iov[0] = cur[1];
3553 state->smb2.recv_iov[1] = cur[2];
3554 state->smb2.recv_iov[2] = cur[3];
3556 tevent_req_done(req);
3559 if (defer) {
3560 return NT_STATUS_RETRY;
3563 return NT_STATUS_OK;
3566 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3567 struct iovec **piov,
3568 const struct smb2cli_req_expected_response *expected,
3569 size_t num_expected)
3571 struct smbXcli_req_state *state =
3572 tevent_req_data(req,
3573 struct smbXcli_req_state);
3574 NTSTATUS status;
3575 size_t body_size;
3576 bool found_status = false;
3577 bool found_size = false;
3578 size_t i;
3580 if (piov != NULL) {
3581 *piov = NULL;
3584 if (state->smb2.got_async) {
3585 return STATUS_PENDING;
3588 if (tevent_req_is_nterror(req, &status)) {
3589 for (i=0; i < num_expected; i++) {
3590 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3591 found_status = true;
3592 break;
3596 if (found_status) {
3597 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3600 return status;
3603 if (num_expected == 0) {
3604 found_status = true;
3605 found_size = true;
3608 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3609 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3611 for (i=0; i < num_expected; i++) {
3612 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3613 continue;
3616 found_status = true;
3617 if (expected[i].body_size == 0) {
3618 found_size = true;
3619 break;
3622 if (expected[i].body_size == body_size) {
3623 found_size = true;
3624 break;
3628 if (!found_status) {
3629 return status;
3632 if (state->smb2.signing_skipped) {
3633 if (num_expected > 0) {
3634 return NT_STATUS_ACCESS_DENIED;
3636 if (!NT_STATUS_IS_ERR(status)) {
3637 return NT_STATUS_ACCESS_DENIED;
3641 if (!found_size) {
3642 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3645 if (piov != NULL) {
3646 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3649 return status;
3652 static const struct {
3653 enum protocol_types proto;
3654 const char *smb1_name;
3655 } smb1cli_prots[] = {
3656 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3657 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3658 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3659 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3660 {PROTOCOL_LANMAN2, "LM1.2X002"},
3661 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3662 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3663 {PROTOCOL_LANMAN2, "Samba"},
3664 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3665 {PROTOCOL_NT1, "NT LM 0.12"},
3666 {PROTOCOL_SMB2_02, "SMB 2.002"},
3667 {PROTOCOL_SMB2_10, "SMB 2.???"},
3670 static const struct {
3671 enum protocol_types proto;
3672 uint16_t smb2_dialect;
3673 } smb2cli_prots[] = {
3674 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3675 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3676 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3677 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3678 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3681 struct smbXcli_negprot_state {
3682 struct smbXcli_conn *conn;
3683 struct tevent_context *ev;
3684 uint32_t timeout_msec;
3685 enum protocol_types min_protocol;
3686 enum protocol_types max_protocol;
3688 struct {
3689 uint8_t fixed[36];
3690 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3691 } smb2;
3694 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3695 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3696 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3697 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3698 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3699 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3700 TALLOC_CTX *frame,
3701 uint8_t *inbuf);
3703 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3704 struct tevent_context *ev,
3705 struct smbXcli_conn *conn,
3706 uint32_t timeout_msec,
3707 enum protocol_types min_protocol,
3708 enum protocol_types max_protocol)
3710 struct tevent_req *req, *subreq;
3711 struct smbXcli_negprot_state *state;
3713 req = tevent_req_create(mem_ctx, &state,
3714 struct smbXcli_negprot_state);
3715 if (req == NULL) {
3716 return NULL;
3718 state->conn = conn;
3719 state->ev = ev;
3720 state->timeout_msec = timeout_msec;
3721 state->min_protocol = min_protocol;
3722 state->max_protocol = max_protocol;
3724 if (min_protocol == PROTOCOL_NONE) {
3725 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3726 return tevent_req_post(req, ev);
3729 if (max_protocol == PROTOCOL_NONE) {
3730 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3731 return tevent_req_post(req, ev);
3734 if (min_protocol > max_protocol) {
3735 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3736 return tevent_req_post(req, ev);
3739 if ((min_protocol < PROTOCOL_SMB2_02) &&
3740 (max_protocol < PROTOCOL_SMB2_02)) {
3742 * SMB1 only...
3744 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3746 subreq = smbXcli_negprot_smb1_subreq(state);
3747 if (tevent_req_nomem(subreq, req)) {
3748 return tevent_req_post(req, ev);
3750 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3751 return req;
3754 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3755 (max_protocol >= PROTOCOL_SMB2_02)) {
3757 * SMB2 only...
3759 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3761 subreq = smbXcli_negprot_smb2_subreq(state);
3762 if (tevent_req_nomem(subreq, req)) {
3763 return tevent_req_post(req, ev);
3765 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3766 return req;
3770 * We send an SMB1 negprot with the SMB2 dialects
3771 * and expect a SMB1 or a SMB2 response.
3773 * smbXcli_negprot_dispatch_incoming() will fix the
3774 * callback to match protocol of the response.
3776 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3778 subreq = smbXcli_negprot_smb1_subreq(state);
3779 if (tevent_req_nomem(subreq, req)) {
3780 return tevent_req_post(req, ev);
3782 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3783 return req;
3786 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3788 struct tevent_req *req =
3789 tevent_req_callback_data(subreq,
3790 struct tevent_req);
3791 NTSTATUS status;
3794 * we just want the low level error
3796 status = tevent_req_simple_recv_ntstatus(subreq);
3797 TALLOC_FREE(subreq);
3798 if (tevent_req_nterror(req, status)) {
3799 return;
3802 /* this should never happen */
3803 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3806 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3808 size_t i;
3809 DATA_BLOB bytes = data_blob_null;
3810 uint8_t flags;
3811 uint16_t flags2;
3813 /* setup the protocol strings */
3814 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3815 uint8_t c = 2;
3816 bool ok;
3818 if (smb1cli_prots[i].proto < state->min_protocol) {
3819 continue;
3822 if (smb1cli_prots[i].proto > state->max_protocol) {
3823 continue;
3826 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3827 if (!ok) {
3828 return NULL;
3832 * We now it is already ascii and
3833 * we want NULL termination.
3835 ok = data_blob_append(state, &bytes,
3836 smb1cli_prots[i].smb1_name,
3837 strlen(smb1cli_prots[i].smb1_name)+1);
3838 if (!ok) {
3839 return NULL;
3843 smb1cli_req_flags(state->max_protocol,
3844 state->conn->smb1.client.capabilities,
3845 SMBnegprot,
3846 0, 0, &flags,
3847 0, 0, &flags2);
3849 return smb1cli_req_send(state, state->ev, state->conn,
3850 SMBnegprot,
3851 flags, ~flags,
3852 flags2, ~flags2,
3853 state->timeout_msec,
3854 0xFFFE, 0, NULL, /* pid, tid, session */
3855 0, NULL, /* wct, vwv */
3856 bytes.length, bytes.data);
3859 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3861 struct tevent_req *req =
3862 tevent_req_callback_data(subreq,
3863 struct tevent_req);
3864 struct smbXcli_negprot_state *state =
3865 tevent_req_data(req,
3866 struct smbXcli_negprot_state);
3867 struct smbXcli_conn *conn = state->conn;
3868 struct iovec *recv_iov = NULL;
3869 uint8_t *inhdr;
3870 uint8_t wct;
3871 uint16_t *vwv;
3872 uint32_t num_bytes;
3873 uint8_t *bytes;
3874 NTSTATUS status;
3875 uint16_t protnum;
3876 size_t i;
3877 size_t num_prots = 0;
3878 uint8_t flags;
3879 uint32_t client_capabilities = conn->smb1.client.capabilities;
3880 uint32_t both_capabilities;
3881 uint32_t server_capabilities = 0;
3882 uint32_t capabilities;
3883 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3884 uint32_t server_max_xmit = 0;
3885 uint32_t max_xmit;
3886 uint32_t server_max_mux = 0;
3887 uint16_t server_security_mode = 0;
3888 uint32_t server_session_key = 0;
3889 bool server_readbraw = false;
3890 bool server_writebraw = false;
3891 bool server_lockread = false;
3892 bool server_writeunlock = false;
3893 struct GUID server_guid = GUID_zero();
3894 DATA_BLOB server_gss_blob = data_blob_null;
3895 uint8_t server_challenge[8];
3896 char *server_workgroup = NULL;
3897 char *server_name = NULL;
3898 int server_time_zone = 0;
3899 NTTIME server_system_time = 0;
3900 static const struct smb1cli_req_expected_response expected[] = {
3902 .status = NT_STATUS_OK,
3903 .wct = 0x11, /* NT1 */
3906 .status = NT_STATUS_OK,
3907 .wct = 0x0D, /* LM */
3910 .status = NT_STATUS_OK,
3911 .wct = 0x01, /* CORE */
3915 ZERO_STRUCT(server_challenge);
3917 status = smb1cli_req_recv(subreq, state,
3918 &recv_iov,
3919 &inhdr,
3920 &wct,
3921 &vwv,
3922 NULL, /* pvwv_offset */
3923 &num_bytes,
3924 &bytes,
3925 NULL, /* pbytes_offset */
3926 NULL, /* pinbuf */
3927 expected, ARRAY_SIZE(expected));
3928 TALLOC_FREE(subreq);
3929 if (tevent_req_nterror(req, status)) {
3930 return;
3933 flags = CVAL(inhdr, HDR_FLG);
3935 protnum = SVAL(vwv, 0);
3937 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3938 if (smb1cli_prots[i].proto < state->min_protocol) {
3939 continue;
3942 if (smb1cli_prots[i].proto > state->max_protocol) {
3943 continue;
3946 if (protnum != num_prots) {
3947 num_prots++;
3948 continue;
3951 conn->protocol = smb1cli_prots[i].proto;
3952 break;
3955 if (conn->protocol == PROTOCOL_NONE) {
3956 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3957 return;
3960 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3961 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3962 "and the selected protocol level doesn't support it.\n"));
3963 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3964 return;
3967 if (flags & FLAG_SUPPORT_LOCKREAD) {
3968 server_lockread = true;
3969 server_writeunlock = true;
3972 if (conn->protocol >= PROTOCOL_NT1) {
3973 const char *client_signing = NULL;
3974 bool server_mandatory = false;
3975 bool server_allowed = false;
3976 const char *server_signing = NULL;
3977 bool ok;
3978 uint8_t key_len;
3980 if (wct != 0x11) {
3981 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3982 return;
3985 /* NT protocol */
3986 server_security_mode = CVAL(vwv + 1, 0);
3987 server_max_mux = SVAL(vwv + 1, 1);
3988 server_max_xmit = IVAL(vwv + 3, 1);
3989 server_session_key = IVAL(vwv + 7, 1);
3990 server_time_zone = SVALS(vwv + 15, 1);
3991 server_time_zone *= 60;
3992 /* this time arrives in real GMT */
3993 server_system_time = BVAL(vwv + 11, 1);
3994 server_capabilities = IVAL(vwv + 9, 1);
3996 key_len = CVAL(vwv + 16, 1);
3998 if (server_capabilities & CAP_RAW_MODE) {
3999 server_readbraw = true;
4000 server_writebraw = true;
4002 if (server_capabilities & CAP_LOCK_AND_READ) {
4003 server_lockread = true;
4006 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4007 DATA_BLOB blob1, blob2;
4009 if (num_bytes < 16) {
4010 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4011 return;
4014 blob1 = data_blob_const(bytes, 16);
4015 status = GUID_from_data_blob(&blob1, &server_guid);
4016 if (tevent_req_nterror(req, status)) {
4017 return;
4020 blob1 = data_blob_const(bytes+16, num_bytes-16);
4021 blob2 = data_blob_dup_talloc(state, blob1);
4022 if (blob1.length > 0 &&
4023 tevent_req_nomem(blob2.data, req)) {
4024 return;
4026 server_gss_blob = blob2;
4027 } else {
4028 DATA_BLOB blob1, blob2;
4030 if (num_bytes < key_len) {
4031 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4032 return;
4035 if (key_len != 0 && key_len != 8) {
4036 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4037 return;
4040 if (key_len == 8) {
4041 memcpy(server_challenge, bytes, 8);
4044 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4045 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4046 if (blob1.length > 0) {
4047 size_t len;
4049 len = utf16_len_n(blob1.data,
4050 blob1.length);
4051 blob1.length = len;
4053 ok = convert_string_talloc(state,
4054 CH_UTF16LE,
4055 CH_UNIX,
4056 blob1.data,
4057 blob1.length,
4058 &server_workgroup,
4059 &len);
4060 if (!ok) {
4061 status = map_nt_error_from_unix_common(errno);
4062 tevent_req_nterror(req, status);
4063 return;
4067 blob2.data += blob1.length;
4068 blob2.length -= blob1.length;
4069 if (blob2.length > 0) {
4070 size_t len;
4072 len = utf16_len_n(blob1.data,
4073 blob1.length);
4074 blob1.length = len;
4076 ok = convert_string_talloc(state,
4077 CH_UTF16LE,
4078 CH_UNIX,
4079 blob2.data,
4080 blob2.length,
4081 &server_name,
4082 &len);
4083 if (!ok) {
4084 status = map_nt_error_from_unix_common(errno);
4085 tevent_req_nterror(req, status);
4086 return;
4091 client_signing = "disabled";
4092 if (conn->allow_signing) {
4093 client_signing = "allowed";
4095 if (conn->mandatory_signing) {
4096 client_signing = "required";
4099 server_signing = "not supported";
4100 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4101 server_signing = "supported";
4102 server_allowed = true;
4103 } else if (conn->mandatory_signing) {
4105 * We have mandatory signing as client
4106 * lets assume the server will look at our
4107 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4108 * flag in the session setup
4110 server_signing = "not announced";
4111 server_allowed = true;
4113 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4114 server_signing = "required";
4115 server_mandatory = true;
4118 ok = smb_signing_set_negotiated(conn->smb1.signing,
4119 server_allowed,
4120 server_mandatory);
4121 if (!ok) {
4122 DEBUG(1,("cli_negprot: SMB signing is required, "
4123 "but client[%s] and server[%s] mismatch\n",
4124 client_signing, server_signing));
4125 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4126 return;
4129 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4130 DATA_BLOB blob1;
4131 uint8_t key_len;
4132 time_t t;
4134 if (wct != 0x0D) {
4135 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4136 return;
4139 server_security_mode = SVAL(vwv + 1, 0);
4140 server_max_xmit = SVAL(vwv + 2, 0);
4141 server_max_mux = SVAL(vwv + 3, 0);
4142 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4143 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4144 server_session_key = IVAL(vwv + 6, 0);
4145 server_time_zone = SVALS(vwv + 10, 0);
4146 server_time_zone *= 60;
4147 /* this time is converted to GMT by make_unix_date */
4148 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4149 unix_to_nt_time(&server_system_time, t);
4150 key_len = SVAL(vwv + 11, 0);
4152 if (num_bytes < key_len) {
4153 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4154 return;
4157 if (key_len != 0 && key_len != 8) {
4158 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4159 return;
4162 if (key_len == 8) {
4163 memcpy(server_challenge, bytes, 8);
4166 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4167 if (blob1.length > 0) {
4168 size_t len;
4169 bool ok;
4171 len = utf16_len_n(blob1.data,
4172 blob1.length);
4173 blob1.length = len;
4175 ok = convert_string_talloc(state,
4176 CH_DOS,
4177 CH_UNIX,
4178 blob1.data,
4179 blob1.length,
4180 &server_workgroup,
4181 &len);
4182 if (!ok) {
4183 status = map_nt_error_from_unix_common(errno);
4184 tevent_req_nterror(req, status);
4185 return;
4189 } else {
4190 /* the old core protocol */
4191 server_time_zone = get_time_zone(time(NULL));
4192 server_max_xmit = 1024;
4193 server_max_mux = 1;
4196 if (server_max_xmit < 1024) {
4197 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4198 return;
4201 if (server_max_mux < 1) {
4202 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4203 return;
4207 * Now calculate the negotiated capabilities
4208 * based on the mask for:
4209 * - client only flags
4210 * - flags used in both directions
4211 * - server only flags
4213 both_capabilities = client_capabilities & server_capabilities;
4214 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4215 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4216 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4218 max_xmit = MIN(client_max_xmit, server_max_xmit);
4220 conn->smb1.server.capabilities = server_capabilities;
4221 conn->smb1.capabilities = capabilities;
4223 conn->smb1.server.max_xmit = server_max_xmit;
4224 conn->smb1.max_xmit = max_xmit;
4226 conn->smb1.server.max_mux = server_max_mux;
4228 conn->smb1.server.security_mode = server_security_mode;
4230 conn->smb1.server.readbraw = server_readbraw;
4231 conn->smb1.server.writebraw = server_writebraw;
4232 conn->smb1.server.lockread = server_lockread;
4233 conn->smb1.server.writeunlock = server_writeunlock;
4235 conn->smb1.server.session_key = server_session_key;
4237 talloc_steal(conn, server_gss_blob.data);
4238 conn->smb1.server.gss_blob = server_gss_blob;
4239 conn->smb1.server.guid = server_guid;
4240 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4241 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4242 conn->smb1.server.name = talloc_move(conn, &server_name);
4244 conn->smb1.server.time_zone = server_time_zone;
4245 conn->smb1.server.system_time = server_system_time;
4247 tevent_req_done(req);
4250 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4252 size_t i;
4253 uint8_t *buf;
4254 uint16_t dialect_count = 0;
4256 buf = state->smb2.dyn;
4257 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4258 if (smb2cli_prots[i].proto < state->min_protocol) {
4259 continue;
4262 if (smb2cli_prots[i].proto > state->max_protocol) {
4263 continue;
4266 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4267 dialect_count++;
4270 buf = state->smb2.fixed;
4271 SSVAL(buf, 0, 36);
4272 SSVAL(buf, 2, dialect_count);
4273 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4274 SSVAL(buf, 6, 0); /* Reserved */
4275 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4276 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4277 } else {
4278 SIVAL(buf, 8, 0); /* Capabilities */
4280 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4281 NTSTATUS status;
4282 DATA_BLOB blob;
4284 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4285 state, &blob);
4286 if (!NT_STATUS_IS_OK(status)) {
4287 return NULL;
4289 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4290 } else {
4291 memset(buf+12, 0, 16); /* ClientGuid */
4293 SBVAL(buf, 28, 0); /* ClientStartTime */
4295 return smb2cli_req_send(state, state->ev,
4296 state->conn, SMB2_OP_NEGPROT,
4297 0, 0, /* flags */
4298 state->timeout_msec,
4299 NULL, NULL, /* tcon, session */
4300 state->smb2.fixed, sizeof(state->smb2.fixed),
4301 state->smb2.dyn, dialect_count*2,
4302 UINT16_MAX); /* max_dyn_len */
4305 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4307 struct tevent_req *req =
4308 tevent_req_callback_data(subreq,
4309 struct tevent_req);
4310 struct smbXcli_negprot_state *state =
4311 tevent_req_data(req,
4312 struct smbXcli_negprot_state);
4313 struct smbXcli_conn *conn = state->conn;
4314 size_t security_offset, security_length;
4315 DATA_BLOB blob;
4316 NTSTATUS status;
4317 struct iovec *iov;
4318 uint8_t *body;
4319 size_t i;
4320 uint16_t dialect_revision;
4321 static const struct smb2cli_req_expected_response expected[] = {
4323 .status = NT_STATUS_OK,
4324 .body_size = 0x41
4328 status = smb2cli_req_recv(subreq, state, &iov,
4329 expected, ARRAY_SIZE(expected));
4330 TALLOC_FREE(subreq);
4331 if (tevent_req_nterror(req, status)) {
4332 return;
4335 body = (uint8_t *)iov[1].iov_base;
4337 dialect_revision = SVAL(body, 4);
4339 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4340 if (smb2cli_prots[i].proto < state->min_protocol) {
4341 continue;
4344 if (smb2cli_prots[i].proto > state->max_protocol) {
4345 continue;
4348 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4349 continue;
4352 conn->protocol = smb2cli_prots[i].proto;
4353 break;
4356 if (conn->protocol == PROTOCOL_NONE) {
4357 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4358 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4359 return;
4362 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4363 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4364 return;
4367 /* make sure we do not loop forever */
4368 state->min_protocol = PROTOCOL_SMB2_02;
4371 * send a SMB2 negprot, in order to negotiate
4372 * the SMB2 dialect.
4374 subreq = smbXcli_negprot_smb2_subreq(state);
4375 if (tevent_req_nomem(subreq, req)) {
4376 return;
4378 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4379 return;
4382 conn->smb2.server.security_mode = SVAL(body, 2);
4384 blob = data_blob_const(body + 8, 16);
4385 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4386 if (tevent_req_nterror(req, status)) {
4387 return;
4390 conn->smb2.server.capabilities = IVAL(body, 24);
4391 conn->smb2.server.max_trans_size= IVAL(body, 28);
4392 conn->smb2.server.max_read_size = IVAL(body, 32);
4393 conn->smb2.server.max_write_size= IVAL(body, 36);
4394 conn->smb2.server.system_time = BVAL(body, 40);
4395 conn->smb2.server.start_time = BVAL(body, 48);
4397 security_offset = SVAL(body, 56);
4398 security_length = SVAL(body, 58);
4400 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4401 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4402 return;
4405 if (security_length > iov[2].iov_len) {
4406 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4407 return;
4410 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4411 iov[2].iov_base,
4412 security_length);
4413 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4414 return;
4417 tevent_req_done(req);
4420 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4421 TALLOC_CTX *tmp_mem,
4422 uint8_t *inbuf)
4424 size_t num_pending = talloc_array_length(conn->pending);
4425 struct tevent_req *subreq;
4426 struct smbXcli_req_state *substate;
4427 struct tevent_req *req;
4428 uint32_t protocol_magic;
4429 size_t inbuf_len = smb_len_nbt(inbuf);
4431 if (num_pending != 1) {
4432 return NT_STATUS_INTERNAL_ERROR;
4435 if (inbuf_len < 4) {
4436 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4439 subreq = conn->pending[0];
4440 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4441 req = tevent_req_callback_data(subreq, struct tevent_req);
4443 protocol_magic = IVAL(inbuf, 4);
4445 switch (protocol_magic) {
4446 case SMB_MAGIC:
4447 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4448 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4449 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4451 case SMB2_MAGIC:
4452 if (substate->smb2.recv_iov == NULL) {
4454 * For the SMB1 negprot we have move it.
4456 substate->smb2.recv_iov = substate->smb1.recv_iov;
4457 substate->smb1.recv_iov = NULL;
4461 * we got an SMB2 answer, which consumed sequence number 0
4462 * so we need to use 1 as the next one.
4464 * we also need to set the current credits to 0
4465 * as we consumed the initial one. The SMB2 answer
4466 * hopefully grant us a new credit.
4468 conn->smb2.mid = 1;
4469 conn->smb2.cur_credits = 0;
4470 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4471 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4472 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4475 DEBUG(10, ("Got non-SMB PDU\n"));
4476 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4479 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4481 return tevent_req_simple_recv_ntstatus(req);
4484 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4485 uint32_t timeout_msec,
4486 enum protocol_types min_protocol,
4487 enum protocol_types max_protocol)
4489 TALLOC_CTX *frame = talloc_stackframe();
4490 struct tevent_context *ev;
4491 struct tevent_req *req;
4492 NTSTATUS status = NT_STATUS_NO_MEMORY;
4493 bool ok;
4495 if (smbXcli_conn_has_async_calls(conn)) {
4497 * Can't use sync call while an async call is in flight
4499 status = NT_STATUS_INVALID_PARAMETER_MIX;
4500 goto fail;
4502 ev = samba_tevent_context_init(frame);
4503 if (ev == NULL) {
4504 goto fail;
4506 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4507 min_protocol, max_protocol);
4508 if (req == NULL) {
4509 goto fail;
4511 ok = tevent_req_poll(req, ev);
4512 if (!ok) {
4513 status = map_nt_error_from_unix_common(errno);
4514 goto fail;
4516 status = smbXcli_negprot_recv(req);
4517 fail:
4518 TALLOC_FREE(frame);
4519 return status;
4522 static int smbXcli_session_destructor(struct smbXcli_session *session)
4524 if (session->conn == NULL) {
4525 return 0;
4528 DLIST_REMOVE(session->conn->sessions, session);
4529 return 0;
4532 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4533 struct smbXcli_conn *conn)
4535 struct smbXcli_session *session;
4537 session = talloc_zero(mem_ctx, struct smbXcli_session);
4538 if (session == NULL) {
4539 return NULL;
4541 session->smb2 = talloc_zero(session, struct smb2cli_session);
4542 if (session->smb2 == NULL) {
4543 talloc_free(session);
4544 return NULL;
4546 talloc_set_destructor(session, smbXcli_session_destructor);
4548 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4549 session->conn = conn;
4551 return session;
4554 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4555 struct smbXcli_session *src)
4557 struct smbXcli_session *session;
4559 session = talloc_zero(mem_ctx, struct smbXcli_session);
4560 if (session == NULL) {
4561 return NULL;
4563 session->smb2 = talloc_zero(session, struct smb2cli_session);
4564 if (session->smb2 == NULL) {
4565 talloc_free(session);
4566 return NULL;
4569 session->conn = src->conn;
4570 *session->smb2 = *src->smb2;
4571 session->smb2_channel = src->smb2_channel;
4572 session->disconnect_expired = src->disconnect_expired;
4574 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4575 talloc_set_destructor(session, smbXcli_session_destructor);
4577 return session;
4581 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4582 TALLOC_CTX *mem_ctx,
4583 DATA_BLOB *key)
4585 const DATA_BLOB *application_key;
4587 *key = data_blob_null;
4589 if (session->conn == NULL) {
4590 return NT_STATUS_NO_USER_SESSION_KEY;
4593 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4594 application_key = &session->smb2->application_key;
4595 } else {
4596 application_key = &session->smb1.application_key;
4599 if (application_key->length == 0) {
4600 return NT_STATUS_NO_USER_SESSION_KEY;
4603 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4604 if (key->data == NULL) {
4605 return NT_STATUS_NO_MEMORY;
4608 return NT_STATUS_OK;
4611 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4613 session->disconnect_expired = true;
4616 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4618 return session->smb1.session_id;
4621 void smb1cli_session_set_id(struct smbXcli_session *session,
4622 uint16_t session_id)
4624 session->smb1.session_id = session_id;
4627 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4628 const DATA_BLOB _session_key)
4630 struct smbXcli_conn *conn = session->conn;
4631 uint8_t session_key[16];
4633 if (conn == NULL) {
4634 return NT_STATUS_INVALID_PARAMETER_MIX;
4637 if (session->smb1.application_key.length != 0) {
4639 * TODO: do not allow this...
4641 * return NT_STATUS_INVALID_PARAMETER_MIX;
4643 data_blob_clear_free(&session->smb1.application_key);
4644 session->smb1.protected_key = false;
4647 if (_session_key.length == 0) {
4648 return NT_STATUS_OK;
4651 ZERO_STRUCT(session_key);
4652 memcpy(session_key, _session_key.data,
4653 MIN(_session_key.length, sizeof(session_key)));
4655 session->smb1.application_key = data_blob_talloc(session,
4656 session_key,
4657 sizeof(session_key));
4658 ZERO_STRUCT(session_key);
4659 if (session->smb1.application_key.data == NULL) {
4660 return NT_STATUS_NO_MEMORY;
4663 session->smb1.protected_key = false;
4665 return NT_STATUS_OK;
4668 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4670 if (session->smb1.protected_key) {
4671 /* already protected */
4672 return NT_STATUS_OK;
4675 if (session->smb1.application_key.length != 16) {
4676 return NT_STATUS_INVALID_PARAMETER_MIX;
4679 smb_key_derivation(session->smb1.application_key.data,
4680 session->smb1.application_key.length,
4681 session->smb1.application_key.data);
4683 session->smb1.protected_key = true;
4685 return NT_STATUS_OK;
4688 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4690 struct smbXcli_conn *conn = session->conn;
4691 uint8_t security_mode = 0;
4693 if (conn == NULL) {
4694 return security_mode;
4697 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4698 if (conn->mandatory_signing) {
4699 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4702 return security_mode;
4705 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4707 return session->smb2->session_id;
4710 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4712 return session->smb2->session_flags;
4715 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4716 uint64_t session_id,
4717 uint16_t session_flags)
4719 session->smb2->session_id = session_id;
4720 session->smb2->session_flags = session_flags;
4723 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4725 session->smb2->channel_sequence += 1;
4728 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4729 const DATA_BLOB _session_key,
4730 const struct iovec *recv_iov)
4732 struct smbXcli_conn *conn = session->conn;
4733 uint16_t no_sign_flags;
4734 uint8_t session_key[16];
4735 NTSTATUS status;
4737 if (conn == NULL) {
4738 return NT_STATUS_INVALID_PARAMETER_MIX;
4741 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4743 if (session->smb2->session_flags & no_sign_flags) {
4744 session->smb2->should_sign = false;
4745 return NT_STATUS_OK;
4748 if (session->smb2->signing_key.length != 0) {
4749 return NT_STATUS_INVALID_PARAMETER_MIX;
4752 ZERO_STRUCT(session_key);
4753 memcpy(session_key, _session_key.data,
4754 MIN(_session_key.length, sizeof(session_key)));
4756 session->smb2->signing_key = data_blob_talloc(session,
4757 session_key,
4758 sizeof(session_key));
4759 if (session->smb2->signing_key.data == NULL) {
4760 ZERO_STRUCT(session_key);
4761 return NT_STATUS_NO_MEMORY;
4764 if (conn->protocol >= PROTOCOL_SMB2_24) {
4765 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4766 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4768 smb2_key_derivation(session_key, sizeof(session_key),
4769 label.data, label.length,
4770 context.data, context.length,
4771 session->smb2->signing_key.data);
4774 session->smb2->encryption_key = data_blob_dup_talloc(session,
4775 session->smb2->signing_key);
4776 if (session->smb2->encryption_key.data == NULL) {
4777 ZERO_STRUCT(session_key);
4778 return NT_STATUS_NO_MEMORY;
4781 if (conn->protocol >= PROTOCOL_SMB2_24) {
4782 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4783 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4785 smb2_key_derivation(session_key, sizeof(session_key),
4786 label.data, label.length,
4787 context.data, context.length,
4788 session->smb2->encryption_key.data);
4791 session->smb2->decryption_key = data_blob_dup_talloc(session,
4792 session->smb2->signing_key);
4793 if (session->smb2->decryption_key.data == NULL) {
4794 ZERO_STRUCT(session_key);
4795 return NT_STATUS_NO_MEMORY;
4798 if (conn->protocol >= PROTOCOL_SMB2_24) {
4799 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4800 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4802 smb2_key_derivation(session_key, sizeof(session_key),
4803 label.data, label.length,
4804 context.data, context.length,
4805 session->smb2->decryption_key.data);
4808 session->smb2->application_key = data_blob_dup_talloc(session,
4809 session->smb2->signing_key);
4810 if (session->smb2->application_key.data == NULL) {
4811 ZERO_STRUCT(session_key);
4812 return NT_STATUS_NO_MEMORY;
4815 if (conn->protocol >= PROTOCOL_SMB2_24) {
4816 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4817 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4819 smb2_key_derivation(session_key, sizeof(session_key),
4820 label.data, label.length,
4821 context.data, context.length,
4822 session->smb2->application_key.data);
4824 ZERO_STRUCT(session_key);
4826 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4827 session->smb2->signing_key);
4828 if (session->smb2_channel.signing_key.data == NULL) {
4829 return NT_STATUS_NO_MEMORY;
4832 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4833 session->conn->protocol,
4834 recv_iov, 3);
4835 if (!NT_STATUS_IS_OK(status)) {
4836 return status;
4839 session->smb2->should_sign = false;
4840 session->smb2->should_encrypt = false;
4842 if (conn->desire_signing) {
4843 session->smb2->should_sign = true;
4846 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4847 session->smb2->should_sign = true;
4850 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4851 session->smb2->should_encrypt = true;
4854 if (conn->protocol < PROTOCOL_SMB2_24) {
4855 session->smb2->should_encrypt = false;
4858 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4859 session->smb2->should_encrypt = false;
4862 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4863 sizeof(session->smb2->nonce_high));
4864 session->smb2->nonce_low = 1;
4866 return NT_STATUS_OK;
4869 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4870 struct smbXcli_session *session1,
4871 struct smbXcli_conn *conn,
4872 struct smbXcli_session **_session2)
4874 struct smbXcli_session *session2;
4876 if (session1->smb2->signing_key.length == 0) {
4877 return NT_STATUS_INVALID_PARAMETER_MIX;
4880 if (conn == NULL) {
4881 return NT_STATUS_INVALID_PARAMETER_MIX;
4884 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4885 if (session2 == NULL) {
4886 return NT_STATUS_NO_MEMORY;
4888 session2->smb2 = talloc_reference(session2, session1->smb2);
4889 if (session2->smb2 == NULL) {
4890 talloc_free(session2);
4891 return NT_STATUS_NO_MEMORY;
4894 talloc_set_destructor(session2, smbXcli_session_destructor);
4895 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4896 session2->conn = conn;
4898 *_session2 = session2;
4899 return NT_STATUS_OK;
4902 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4903 const DATA_BLOB _channel_key,
4904 const struct iovec *recv_iov)
4906 struct smbXcli_conn *conn = session->conn;
4907 uint8_t channel_key[16];
4908 NTSTATUS status;
4910 if (conn == NULL) {
4911 return NT_STATUS_INVALID_PARAMETER_MIX;
4914 if (session->smb2_channel.signing_key.length != 0) {
4915 return NT_STATUS_INVALID_PARAMETER_MIX;
4918 ZERO_STRUCT(channel_key);
4919 memcpy(channel_key, _channel_key.data,
4920 MIN(_channel_key.length, sizeof(channel_key)));
4922 session->smb2_channel.signing_key = data_blob_talloc(session,
4923 channel_key,
4924 sizeof(channel_key));
4925 if (session->smb2_channel.signing_key.data == NULL) {
4926 ZERO_STRUCT(channel_key);
4927 return NT_STATUS_NO_MEMORY;
4930 if (conn->protocol >= PROTOCOL_SMB2_24) {
4931 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4932 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4934 smb2_key_derivation(channel_key, sizeof(channel_key),
4935 label.data, label.length,
4936 context.data, context.length,
4937 session->smb2_channel.signing_key.data);
4939 ZERO_STRUCT(channel_key);
4941 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4942 session->conn->protocol,
4943 recv_iov, 3);
4944 if (!NT_STATUS_IS_OK(status)) {
4945 return status;
4948 return NT_STATUS_OK;
4951 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4953 struct smbXcli_tcon *tcon;
4955 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4956 if (tcon == NULL) {
4957 return NULL;
4960 return tcon;
4963 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4965 return tcon->smb1.tcon_id;
4968 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4970 tcon->smb1.tcon_id = tcon_id;
4973 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4974 uint16_t tcon_id,
4975 uint16_t optional_support,
4976 uint32_t maximal_access,
4977 uint32_t guest_maximal_access,
4978 const char *service,
4979 const char *fs_type)
4981 tcon->smb1.tcon_id = tcon_id;
4982 tcon->smb1.optional_support = optional_support;
4983 tcon->smb1.maximal_access = maximal_access;
4984 tcon->smb1.guest_maximal_access = guest_maximal_access;
4986 TALLOC_FREE(tcon->smb1.service);
4987 tcon->smb1.service = talloc_strdup(tcon, service);
4988 if (service != NULL && tcon->smb1.service == NULL) {
4989 return false;
4992 TALLOC_FREE(tcon->smb1.fs_type);
4993 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
4994 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
4995 return false;
4998 return true;
5001 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5003 return tcon->smb2.tcon_id;
5006 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5008 return tcon->smb2.capabilities;
5011 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5012 struct smbXcli_session *session,
5013 uint32_t tcon_id,
5014 uint8_t type,
5015 uint32_t flags,
5016 uint32_t capabilities,
5017 uint32_t maximal_access)
5019 tcon->smb2.tcon_id = tcon_id;
5020 tcon->smb2.type = type;
5021 tcon->smb2.flags = flags;
5022 tcon->smb2.capabilities = capabilities;
5023 tcon->smb2.maximal_access = maximal_access;
5025 tcon->smb2.should_encrypt = false;
5027 if (session == NULL) {
5028 return;
5031 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5033 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5034 tcon->smb2.should_encrypt = true;