s4:libcli/smb2: remove unused dependency to LIBPACKET
[Samba/gebeck_regimport.git] / source4 / libcli / smb2 / transport.c
bloba3845a60cbc255a3423e917a886e695fa5f733d4
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;
51 struct GUID client_guid;
52 uint32_t smb2_capabilities = 0;
54 transport = talloc_zero(parent_ctx, struct smb2_transport);
55 if (!transport) return NULL;
57 transport->ev = sock->event.ctx;
58 transport->options = *options;
60 TALLOC_FREE(sock->event.fde);
61 TALLOC_FREE(sock->event.te);
63 client_guid = GUID_random();
65 /* TODO: hand this in via the options? */
66 smb2_capabilities = SMB2_CAP_ALL;
68 transport->conn = smbXcli_conn_create(transport,
69 sock->sock->fd,
70 sock->hostname,
71 options->signing,
72 0, /* smb1_capabilities */
73 &client_guid,
74 smb2_capabilities);
75 if (transport->conn == NULL) {
76 talloc_free(transport);
77 return NULL;
79 sock->sock->fd = -1;
80 TALLOC_FREE(sock);
82 talloc_set_destructor(transport, transport_destructor);
84 return transport;
88 mark the transport as dead
90 void smb2_transport_dead(struct smb2_transport *transport, NTSTATUS status)
92 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
93 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
95 if (NT_STATUS_IS_OK(status)) {
96 status = NT_STATUS_LOCAL_DISCONNECT;
99 smbXcli_conn_disconnect(transport->conn, status);
102 static void smb2_request_done(struct tevent_req *subreq);
103 static void smb2_transport_break_handler(struct tevent_req *subreq);
106 put a request into the send queue
108 void smb2_transport_send(struct smb2_request *req)
110 NTSTATUS status;
111 struct smb2_transport *transport = req->transport;
112 struct tevent_req **reqs = transport->compound.reqs;
113 size_t num_reqs = talloc_array_length(reqs);
114 size_t i;
115 uint16_t cmd = SVAL(req->out.hdr, SMB2_HDR_OPCODE);
116 uint32_t additional_flags = IVAL(req->out.hdr, SMB2_HDR_FLAGS);
117 uint32_t clear_flags = 0;
118 uint32_t pid = IVAL(req->out.hdr, SMB2_HDR_PID);
119 uint32_t tid = IVAL(req->out.hdr, SMB2_HDR_TID);
120 struct smbXcli_session *session = NULL;
121 bool need_pending_break = false;
122 size_t hdr_ofs;
123 size_t pdu_len;
124 DATA_BLOB body = data_blob_null;
125 DATA_BLOB dyn = data_blob_null;
126 uint32_t timeout_msec = transport->options.request_timeout * 1000;
128 if (transport->oplock.handler) {
129 need_pending_break = true;
132 if (transport->lease.handler) {
133 need_pending_break = true;
136 if (transport->break_subreq) {
137 need_pending_break = false;
140 if (need_pending_break) {
141 struct tevent_req *subreq;
143 subreq = smb2cli_req_create(transport,
144 transport->ev,
145 transport->conn,
146 SMB2_OP_BREAK,
147 0, /* additional_flags */
148 0, /*clear_flags */
149 0, /* timeout_msec */
150 0, /* pid */
151 0, /* tid */
152 NULL, /* session */
153 NULL, /* body */
154 0, /* body_fixed */
155 NULL, /* dyn */
156 0); /* dyn_len */
157 if (subreq != NULL) {
158 smbXcli_req_set_pending(subreq);
159 tevent_req_set_callback(subreq,
160 smb2_transport_break_handler,
161 transport);
162 transport->break_subreq = subreq;
166 if (req->session) {
167 session = req->session->smbXcli;
170 if (transport->compound.related) {
171 additional_flags |= SMB2_HDR_FLAG_CHAINED;
174 hdr_ofs = PTR_DIFF(req->out.hdr, req->out.buffer);
175 pdu_len = req->out.size - hdr_ofs;
176 body.data = req->out.body;
177 body.length = req->out.body_fixed;
178 dyn.data = req->out.body + req->out.body_fixed;
179 dyn.length = pdu_len - (SMB2_HDR_BODY + req->out.body_fixed);
181 req->subreq = smb2cli_req_create(req,
182 transport->ev,
183 transport->conn,
184 cmd,
185 additional_flags,
186 clear_flags,
187 timeout_msec,
188 pid,
189 tid,
190 session,
191 body.data, body.length,
192 dyn.data, dyn.length);
193 if (req->subreq == NULL) {
194 req->state = SMB2_REQUEST_ERROR;
195 req->status = NT_STATUS_NO_MEMORY;
196 return;
199 if (!tevent_req_is_in_progress(req->subreq)) {
200 req->state = SMB2_REQUEST_ERROR;
201 req->status = NT_STATUS_INTERNAL_ERROR;/* TODO */
202 return;
205 tevent_req_set_callback(req->subreq, smb2_request_done, req);
207 smb2cli_req_set_notify_async(req->subreq);
208 if (req->credit_charge) {
209 smb2cli_req_set_credit_charge(req->subreq, req->credit_charge);
212 ZERO_STRUCT(req->out);
213 req->state = SMB2_REQUEST_RECV;
215 if (num_reqs > 0) {
216 for (i=0; i < num_reqs; i++) {
217 if (reqs[i] != NULL) {
218 continue;
221 reqs[i] = req->subreq;
222 i++;
223 break;
226 if (i < num_reqs) {
227 return;
229 } else {
230 reqs = &req->subreq;
231 num_reqs = 1;
233 status = smb2cli_req_compound_submit(reqs, num_reqs);
235 TALLOC_FREE(transport->compound.reqs);
237 if (!NT_STATUS_IS_OK(status)) {
238 req->status = status;
239 req->state = SMB2_REQUEST_ERROR;
240 smbXcli_conn_disconnect(transport->conn, status);
244 static void smb2_request_done(struct tevent_req *subreq)
246 struct smb2_request *req =
247 tevent_req_callback_data(subreq,
248 struct smb2_request);
249 ssize_t len;
250 size_t i;
252 req->recv_iov = NULL;
254 req->status = smb2cli_req_recv(req->subreq, req, &req->recv_iov, NULL, 0);
255 if (NT_STATUS_EQUAL(req->status, STATUS_PENDING)) {
256 req->cancel.can_cancel = true;
257 return;
259 TALLOC_FREE(req->subreq);
260 if (!NT_STATUS_IS_OK(req->status)) {
261 if (req->recv_iov == NULL) {
262 req->state = SMB2_REQUEST_ERROR;
263 if (req->async.fn) {
264 req->async.fn(req);
266 return;
270 len = req->recv_iov[0].iov_len;
271 for (i=1; i < 3; i++) {
272 uint8_t *p = req->recv_iov[i-1].iov_base;
273 uint8_t *c1 = req->recv_iov[i].iov_base;
274 uint8_t *c2 = p + req->recv_iov[i-1].iov_len;
276 len += req->recv_iov[i].iov_len;
278 if (req->recv_iov[i].iov_len == 0) {
279 continue;
282 if (c1 != c2) {
283 req->status = NT_STATUS_INTERNAL_ERROR;
284 req->state = SMB2_REQUEST_ERROR;
285 if (req->async.fn) {
286 req->async.fn(req);
288 return;
292 req->in.buffer = req->recv_iov[0].iov_base;
293 req->in.size = len;
294 req->in.allocated = req->in.size;
296 req->in.hdr = req->recv_iov[0].iov_base;
297 req->in.body = req->recv_iov[1].iov_base;
298 req->in.dynamic = req->recv_iov[2].iov_base;
299 req->in.body_fixed = req->recv_iov[1].iov_len;
300 req->in.body_size = req->in.body_fixed;
301 req->in.body_size += req->recv_iov[2].iov_len;
303 smb2_setup_bufinfo(req);
305 req->state = SMB2_REQUEST_DONE;
306 if (req->async.fn) {
307 req->async.fn(req);
311 static void smb2_transport_break_handler(struct tevent_req *subreq)
313 struct smb2_transport *transport =
314 tevent_req_callback_data(subreq,
315 struct smb2_transport);
316 NTSTATUS status;
317 uint8_t *hdr;
318 uint8_t *body;
319 uint16_t len = 0;
320 bool lease;
321 struct iovec *recv_iov = NULL;
323 transport->break_subreq = NULL;
325 status = smb2cli_req_recv(subreq, transport, &recv_iov, NULL, 0);
326 TALLOC_FREE(subreq);
327 if (!NT_STATUS_IS_OK(status)) {
328 TALLOC_FREE(recv_iov);
329 smb2_transport_dead(transport, status);
330 return;
334 * Setup the subreq to handle the
335 * next incoming SMB2 Break.
337 subreq = smb2cli_req_create(transport,
338 transport->ev,
339 transport->conn,
340 SMB2_OP_BREAK,
341 0, /* additional_flags */
342 0, /*clear_flags */
343 0, /* timeout_msec */
344 0, /* pid */
345 0, /* tid */
346 NULL, /* session */
347 NULL, /* body */
348 0, /* body_fixed */
349 NULL, /* dyn */
350 0); /* dyn_len */
351 if (subreq != NULL) {
352 smbXcli_req_set_pending(subreq);
353 tevent_req_set_callback(subreq,
354 smb2_transport_break_handler,
355 transport);
356 transport->break_subreq = subreq;
359 hdr = recv_iov[0].iov_base;
360 body = recv_iov[1].iov_base;
362 len = recv_iov[1].iov_len;
363 if (recv_iov[1].iov_len >= 2) {
364 len = CVAL(body, 0x00);
365 if (len != recv_iov[1].iov_len) {
366 len = recv_iov[1].iov_len;
370 if (len == 24) {
371 lease = false;
372 } else if (len == 44) {
373 lease = true;
374 } else {
375 DEBUG(1,("Discarding smb2 oplock reply of invalid size %u\n",
376 (unsigned)len));
377 TALLOC_FREE(recv_iov);
378 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
379 smb2_transport_dead(transport, status);
380 return;
383 if (!lease && transport->oplock.handler) {
384 struct smb2_handle h;
385 uint8_t level;
387 level = CVAL(body, 0x02);
388 smb2_pull_handle(body+0x08, &h);
390 TALLOC_FREE(recv_iov);
392 transport->oplock.handler(transport, &h, level,
393 transport->oplock.private_data);
394 } else if (lease && transport->lease.handler) {
395 struct smb2_lease_break lb;
397 ZERO_STRUCT(lb);
398 lb.break_flags = SVAL(body, 0x4);
399 memcpy(&lb.current_lease.lease_key, body+0x8,
400 sizeof(struct smb2_lease_key));
401 lb.current_lease.lease_state = SVAL(body, 0x18);
402 lb.new_lease_state = SVAL(body, 0x1C);
403 lb.break_reason = SVAL(body, 0x20);
404 lb.access_mask_hint = SVAL(body, 0x24);
405 lb.share_mask_hint = SVAL(body, 0x28);
407 TALLOC_FREE(recv_iov);
409 transport->lease.handler(transport, &lb,
410 transport->lease.private_data);
411 } else {
412 DEBUG(5,("Got SMB2 %s break with no handler\n",
413 lease ? "lease" : "oplock"));
415 TALLOC_FREE(recv_iov);
418 NTSTATUS smb2_transport_compound_start(struct smb2_transport *transport,
419 uint32_t num)
421 TALLOC_FREE(transport->compound.reqs);
422 ZERO_STRUCT(transport->compound);
424 transport->compound.reqs = talloc_zero_array(transport,
425 struct tevent_req *,
426 num);
427 if (transport->compound.reqs == NULL) {
428 return NT_STATUS_NO_MEMORY;
431 return NT_STATUS_OK;
434 void smb2_transport_compound_set_related(struct smb2_transport *transport,
435 bool related)
437 transport->compound.related = related;
440 void smb2_transport_credits_ask_num(struct smb2_transport *transport,
441 uint16_t ask_num)
443 smb2cli_conn_set_max_credits(transport->conn, ask_num);
446 static void idle_handler(struct tevent_context *ev,
447 struct tevent_timer *te, struct timeval t, void *private_data)
449 struct smb2_transport *transport = talloc_get_type(private_data,
450 struct smb2_transport);
451 struct timeval next;
453 transport->idle.func(transport, transport->idle.private_data);
455 next = timeval_current_ofs_usec(transport->idle.period);
456 transport->idle.te = tevent_add_timer(transport->ev,
457 transport,
458 next,
459 idle_handler,
460 transport);
464 setup the idle handler for a transport
465 the period is in microseconds
467 void smb2_transport_idle_handler(struct smb2_transport *transport,
468 void (*idle_func)(struct smb2_transport *, void *),
469 uint64_t period,
470 void *private_data)
472 TALLOC_FREE(transport->idle.te);
474 transport->idle.func = idle_func;
475 transport->idle.private_data = private_data;
476 transport->idle.period = period;
478 transport->idle.te = tevent_add_timer(transport->ev,
479 transport,
480 timeval_current_ofs_usec(period),
481 idle_handler,
482 transport);