libsmb: Add cli_create_send/recv
[Samba.git] / source4 / libcli / smb2 / transport.c
blob9b0c1461fd9a8649d1e8302c1202eb3bd411731c
1 /*
2 Unix SMB/CIFS implementation.
4 SMB2 client transport context management functions
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/network.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/raw/raw_proto.h"
26 #include "libcli/smb2/smb2.h"
27 #include "libcli/smb2/smb2_calls.h"
28 #include "lib/socket/socket.h"
29 #include "lib/events/events.h"
30 #include "../lib/util/dlinklist.h"
31 #include "../libcli/smb/smbXcli_base.h"
32 #include "librpc/ndr/libndr.h"
35 destroy a transport
37 static int transport_destructor(struct smb2_transport *transport)
39 smb2_transport_dead(transport, NT_STATUS_LOCAL_DISCONNECT);
40 return 0;
44 create a transport structure based on an established socket
46 struct smb2_transport *smb2_transport_init(struct smbcli_socket *sock,
47 TALLOC_CTX *parent_ctx,
48 struct smbcli_options *options)
50 struct smb2_transport *transport;
52 transport = talloc_zero(parent_ctx, struct smb2_transport);
53 if (!transport) return NULL;
55 transport->ev = sock->event.ctx;
56 transport->options = *options;
58 TALLOC_FREE(sock->event.fde);
59 TALLOC_FREE(sock->event.te);
61 transport->conn = smbXcli_conn_create(transport,
62 sock->sock->fd,
63 sock->hostname,
64 options->signing,
65 0, /* smb1_capabilities */
66 &options->client_guid,
67 options->smb2_capabilities);
68 if (transport->conn == NULL) {
69 talloc_free(transport);
70 return NULL;
72 sock->sock->fd = -1;
73 TALLOC_FREE(sock);
75 talloc_set_destructor(transport, transport_destructor);
77 return transport;
81 mark the transport as dead
83 void smb2_transport_dead(struct smb2_transport *transport, NTSTATUS status)
85 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
86 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
88 if (NT_STATUS_IS_OK(status)) {
89 status = NT_STATUS_LOCAL_DISCONNECT;
92 smbXcli_conn_disconnect(transport->conn, status);
95 static void smb2_request_done(struct tevent_req *subreq);
96 static void smb2_transport_break_handler(struct tevent_req *subreq);
99 put a request into the send queue
101 void smb2_transport_send(struct smb2_request *req)
103 NTSTATUS status;
104 struct smb2_transport *transport = req->transport;
105 struct tevent_req **reqs = transport->compound.reqs;
106 size_t num_reqs = talloc_array_length(reqs);
107 size_t i;
108 uint16_t cmd = SVAL(req->out.hdr, SMB2_HDR_OPCODE);
109 uint32_t additional_flags = IVAL(req->out.hdr, SMB2_HDR_FLAGS);
110 uint32_t clear_flags = 0;
111 struct smbXcli_tcon *tcon = NULL;
112 struct smbXcli_session *session = NULL;
113 bool need_pending_break = false;
114 size_t hdr_ofs;
115 size_t pdu_len;
116 DATA_BLOB body = data_blob_null;
117 DATA_BLOB dyn = data_blob_null;
118 uint32_t timeout_msec = transport->options.request_timeout * 1000;
120 if (transport->oplock.handler) {
121 need_pending_break = true;
124 if (transport->lease.handler) {
125 need_pending_break = true;
128 if (transport->break_subreq) {
129 need_pending_break = false;
132 if (need_pending_break) {
133 struct tevent_req *subreq;
135 subreq = smb2cli_req_create(transport,
136 transport->ev,
137 transport->conn,
138 SMB2_OP_BREAK,
139 0, /* additional_flags */
140 0, /*clear_flags */
141 0, /* timeout_msec */
142 NULL, /* tcon */
143 NULL, /* session */
144 NULL, /* body */
145 0, /* body_fixed */
146 NULL, /* dyn */
147 0, /* dyn_len */
148 0); /* max_dyn_len */
149 if (subreq != NULL) {
150 smbXcli_req_set_pending(subreq);
151 tevent_req_set_callback(subreq,
152 smb2_transport_break_handler,
153 transport);
154 transport->break_subreq = subreq;
158 if (req->session) {
159 session = req->session->smbXcli;
162 if (req->tree) {
163 tcon = req->tree->smbXcli;
166 if (transport->compound.related) {
167 additional_flags |= SMB2_HDR_FLAG_CHAINED;
170 hdr_ofs = PTR_DIFF(req->out.hdr, req->out.buffer);
171 pdu_len = req->out.size - hdr_ofs;
172 body.data = req->out.body;
173 body.length = req->out.body_fixed;
174 dyn.data = req->out.body + req->out.body_fixed;
175 dyn.length = pdu_len - (SMB2_HDR_BODY + req->out.body_fixed);
177 req->subreq = smb2cli_req_create(req,
178 transport->ev,
179 transport->conn,
180 cmd,
181 additional_flags,
182 clear_flags,
183 timeout_msec,
184 tcon,
185 session,
186 body.data, body.length,
187 dyn.data, dyn.length,
188 0); /* max_dyn_len */
189 if (req->subreq == NULL) {
190 req->state = SMB2_REQUEST_ERROR;
191 req->status = NT_STATUS_NO_MEMORY;
192 return;
195 if (!tevent_req_is_in_progress(req->subreq)) {
196 req->state = SMB2_REQUEST_ERROR;
197 req->status = NT_STATUS_INTERNAL_ERROR;/* TODO */
198 return;
201 tevent_req_set_callback(req->subreq, smb2_request_done, req);
203 smb2cli_req_set_notify_async(req->subreq);
204 if (req->credit_charge) {
205 smb2cli_req_set_credit_charge(req->subreq, req->credit_charge);
208 ZERO_STRUCT(req->out);
209 req->state = SMB2_REQUEST_RECV;
211 if (num_reqs > 0) {
212 for (i=0; i < num_reqs; i++) {
213 if (reqs[i] != NULL) {
214 continue;
217 reqs[i] = req->subreq;
218 i++;
219 break;
222 if (i < num_reqs) {
223 return;
225 } else {
226 reqs = &req->subreq;
227 num_reqs = 1;
229 status = smb2cli_req_compound_submit(reqs, num_reqs);
231 TALLOC_FREE(transport->compound.reqs);
232 transport->compound.related = false;
234 if (!NT_STATUS_IS_OK(status)) {
235 req->status = status;
236 req->state = SMB2_REQUEST_ERROR;
237 smbXcli_conn_disconnect(transport->conn, status);
241 static void smb2_request_done(struct tevent_req *subreq)
243 struct smb2_request *req =
244 tevent_req_callback_data(subreq,
245 struct smb2_request);
246 ssize_t len;
247 size_t i;
249 req->recv_iov = NULL;
251 req->status = smb2cli_req_recv(req->subreq, req, &req->recv_iov, NULL, 0);
252 if (NT_STATUS_EQUAL(req->status, STATUS_PENDING)) {
253 req->cancel.can_cancel = true;
254 return;
256 TALLOC_FREE(req->subreq);
257 if (!NT_STATUS_IS_OK(req->status)) {
258 if (req->recv_iov == NULL) {
259 req->state = SMB2_REQUEST_ERROR;
260 if (req->async.fn) {
261 req->async.fn(req);
263 return;
267 len = req->recv_iov[0].iov_len;
268 for (i=1; i < 3; i++) {
269 uint8_t *p = req->recv_iov[i-1].iov_base;
270 uint8_t *c1 = req->recv_iov[i].iov_base;
271 uint8_t *c2 = p + req->recv_iov[i-1].iov_len;
273 len += req->recv_iov[i].iov_len;
275 if (req->recv_iov[i].iov_len == 0) {
276 continue;
279 if (c1 != c2) {
280 req->status = NT_STATUS_INTERNAL_ERROR;
281 req->state = SMB2_REQUEST_ERROR;
282 if (req->async.fn) {
283 req->async.fn(req);
285 return;
289 req->in.buffer = req->recv_iov[0].iov_base;
290 req->in.size = len;
291 req->in.allocated = req->in.size;
293 req->in.hdr = req->recv_iov[0].iov_base;
294 req->in.body = req->recv_iov[1].iov_base;
295 req->in.dynamic = req->recv_iov[2].iov_base;
296 req->in.body_fixed = req->recv_iov[1].iov_len;
297 req->in.body_size = req->in.body_fixed;
298 req->in.body_size += req->recv_iov[2].iov_len;
300 smb2_setup_bufinfo(req);
302 req->state = SMB2_REQUEST_DONE;
303 if (req->async.fn) {
304 req->async.fn(req);
308 static void smb2_transport_break_handler(struct tevent_req *subreq)
310 struct smb2_transport *transport =
311 tevent_req_callback_data(subreq,
312 struct smb2_transport);
313 NTSTATUS status;
314 uint8_t *body;
315 uint16_t len = 0;
316 bool lease;
317 struct iovec *recv_iov = NULL;
319 transport->break_subreq = NULL;
321 status = smb2cli_req_recv(subreq, transport, &recv_iov, NULL, 0);
322 TALLOC_FREE(subreq);
323 if (!NT_STATUS_IS_OK(status)) {
324 TALLOC_FREE(recv_iov);
325 smb2_transport_dead(transport, status);
326 return;
330 * Setup the subreq to handle the
331 * next incoming SMB2 Break.
333 subreq = smb2cli_req_create(transport,
334 transport->ev,
335 transport->conn,
336 SMB2_OP_BREAK,
337 0, /* additional_flags */
338 0, /*clear_flags */
339 0, /* timeout_msec */
340 NULL, /* tcon */
341 NULL, /* session */
342 NULL, /* body */
343 0, /* body_fixed */
344 NULL, /* dyn */
345 0, /* dyn_len */
346 0); /* max_dyn_len */
347 if (subreq != NULL) {
348 smbXcli_req_set_pending(subreq);
349 tevent_req_set_callback(subreq,
350 smb2_transport_break_handler,
351 transport);
352 transport->break_subreq = subreq;
355 body = recv_iov[1].iov_base;
357 len = recv_iov[1].iov_len;
358 if (recv_iov[1].iov_len >= 2) {
359 len = CVAL(body, 0x00);
360 if (len != recv_iov[1].iov_len) {
361 len = recv_iov[1].iov_len;
365 if (len == 24) {
366 lease = false;
367 } else if (len == 44) {
368 lease = true;
369 } else {
370 DEBUG(1,("Discarding smb2 oplock reply of invalid size %u\n",
371 (unsigned)len));
372 TALLOC_FREE(recv_iov);
373 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
374 smb2_transport_dead(transport, status);
375 return;
378 if (!lease && transport->oplock.handler) {
379 struct smb2_handle h;
380 uint8_t level;
382 level = CVAL(body, 0x02);
383 smb2_pull_handle(body+0x08, &h);
385 TALLOC_FREE(recv_iov);
387 transport->oplock.handler(transport, &h, level,
388 transport->oplock.private_data);
389 } else if (lease && transport->lease.handler) {
390 struct smb2_lease_break lb;
392 ZERO_STRUCT(lb);
393 lb.break_flags = SVAL(body, 0x4);
394 memcpy(&lb.current_lease.lease_key, body+0x8,
395 sizeof(struct smb2_lease_key));
396 lb.current_lease.lease_state = SVAL(body, 0x18);
397 lb.new_lease_state = SVAL(body, 0x1C);
398 lb.break_reason = SVAL(body, 0x20);
399 lb.access_mask_hint = SVAL(body, 0x24);
400 lb.share_mask_hint = SVAL(body, 0x28);
402 TALLOC_FREE(recv_iov);
404 transport->lease.handler(transport, &lb,
405 transport->lease.private_data);
406 } else {
407 DEBUG(5,("Got SMB2 %s break with no handler\n",
408 lease ? "lease" : "oplock"));
410 TALLOC_FREE(recv_iov);
413 NTSTATUS smb2_transport_compound_start(struct smb2_transport *transport,
414 uint32_t num)
416 TALLOC_FREE(transport->compound.reqs);
417 ZERO_STRUCT(transport->compound);
419 transport->compound.reqs = talloc_zero_array(transport,
420 struct tevent_req *,
421 num);
422 if (transport->compound.reqs == NULL) {
423 return NT_STATUS_NO_MEMORY;
426 return NT_STATUS_OK;
429 void smb2_transport_compound_set_related(struct smb2_transport *transport,
430 bool related)
432 transport->compound.related = related;
435 void smb2_transport_credits_ask_num(struct smb2_transport *transport,
436 uint16_t ask_num)
438 smb2cli_conn_set_max_credits(transport->conn, ask_num);
441 static void idle_handler(struct tevent_context *ev,
442 struct tevent_timer *te, struct timeval t, void *private_data)
444 struct smb2_transport *transport = talloc_get_type(private_data,
445 struct smb2_transport);
446 struct timeval next;
448 transport->idle.func(transport, transport->idle.private_data);
450 next = timeval_current_ofs_usec(transport->idle.period);
451 transport->idle.te = tevent_add_timer(transport->ev,
452 transport,
453 next,
454 idle_handler,
455 transport);
459 setup the idle handler for a transport
460 the period is in microseconds
462 void smb2_transport_idle_handler(struct smb2_transport *transport,
463 void (*idle_func)(struct smb2_transport *, void *),
464 uint64_t period,
465 void *private_data)
467 TALLOC_FREE(transport->idle.te);
469 transport->idle.func = idle_func;
470 transport->idle.private_data = private_data;
471 transport->idle.period = period;
473 transport->idle.te = tevent_add_timer(transport->ev,
474 transport,
475 timeval_current_ofs_usec(period),
476 idle_handler,
477 transport);