Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-test
[samba.git] / source4 / libcli / smb2 / transport.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client transport context management functions
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "lib/socket/socket.h"
27 #include "lib/events/events.h"
28 #include "lib/stream/packet.h"
29 #include "lib/util/dlinklist.h"
30
31
32 /*
33   an event has happened on the socket
34 */
35 static void smb2_transport_event_handler(struct event_context *ev, 
36                                          struct fd_event *fde, 
37                                          uint16_t flags, void *private)
38 {
39         struct smb2_transport *transport = talloc_get_type(private,
40                                                            struct smb2_transport);
41         if (flags & EVENT_FD_READ) {
42                 packet_recv(transport->packet);
43                 return;
44         }
45         if (flags & EVENT_FD_WRITE) {
46                 packet_queue_run(transport->packet);
47         }
48 }
49
50 /*
51   destroy a transport
52  */
53 static int transport_destructor(struct smb2_transport *transport)
54 {
55         smb2_transport_dead(transport, NT_STATUS_LOCAL_DISCONNECT);
56         return 0;
57 }
58
59
60 /*
61   handle receive errors
62 */
63 static void smb2_transport_error(void *private, NTSTATUS status)
64 {
65         struct smb2_transport *transport = talloc_get_type(private, 
66                                                            struct smb2_transport);
67         smb2_transport_dead(transport, status);
68 }
69
70 static NTSTATUS smb2_transport_finish_recv(void *private, DATA_BLOB blob);
71
72 /*
73   create a transport structure based on an established socket
74 */
75 struct smb2_transport *smb2_transport_init(struct smbcli_socket *sock,
76                                            TALLOC_CTX *parent_ctx)
77 {
78         struct smb2_transport *transport;
79
80         transport = talloc_zero(parent_ctx, struct smb2_transport);
81         if (!transport) return NULL;
82
83         transport->socket = talloc_steal(transport, sock);
84
85         /* setup the stream -> packet parser */
86         transport->packet = packet_init(transport);
87         if (transport->packet == NULL) {
88                 talloc_free(transport);
89                 return NULL;
90         }
91         packet_set_private(transport->packet, transport);
92         packet_set_socket(transport->packet, transport->socket->sock);
93         packet_set_callback(transport->packet, smb2_transport_finish_recv);
94         packet_set_full_request(transport->packet, packet_full_request_nbt);
95         packet_set_error_handler(transport->packet, smb2_transport_error);
96         packet_set_event_context(transport->packet, transport->socket->event.ctx);
97         packet_set_nofree(transport->packet);
98
99         /* take over event handling from the socket layer - it only
100            handles events up until we are connected */
101         talloc_free(transport->socket->event.fde);
102         transport->socket->event.fde = event_add_fd(transport->socket->event.ctx,
103                                                     transport->socket,
104                                                     socket_get_fd(transport->socket->sock),
105                                                     EVENT_FD_READ,
106                                                     smb2_transport_event_handler,
107                                                     transport);
108
109         packet_set_fde(transport->packet, transport->socket->event.fde);
110         packet_set_serialise(transport->packet);
111
112         talloc_set_destructor(transport, transport_destructor);
113
114         transport->options.timeout = 30;
115
116         return transport;
117 }
118
119 /*
120   mark the transport as dead
121 */
122 void smb2_transport_dead(struct smb2_transport *transport, NTSTATUS status)
123 {
124         smbcli_sock_dead(transport->socket);
125
126         if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
127                 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
128         }
129
130         /* kill all pending receives */
131         while (transport->pending_recv) {
132                 struct smb2_request *req = transport->pending_recv;
133                 req->state = SMB2_REQUEST_ERROR;
134                 req->status = status;
135                 DLIST_REMOVE(transport->pending_recv, req);
136                 if (req->async.fn) {
137                         req->async.fn(req);
138                 }
139         }
140 }
141
142 /*
143   we have a full request in our receive buffer - match it to a pending request
144   and process
145  */
146 static NTSTATUS smb2_transport_finish_recv(void *private, DATA_BLOB blob)
147 {
148         struct smb2_transport *transport = talloc_get_type(private, 
149                                                              struct smb2_transport);
150         uint8_t *buffer, *hdr;
151         int len;
152         struct smb2_request *req = NULL;
153         uint64_t seqnum;
154         uint32_t flags;
155         uint16_t buffer_code;
156         uint32_t dynamic_size;
157         uint32_t i;
158
159         buffer = blob.data;
160         len = blob.length;
161
162         hdr = buffer+NBT_HDR_SIZE;
163
164         if (len < SMB2_MIN_SIZE) {
165                 DEBUG(1,("Discarding smb2 reply of size %d\n", len));
166                 goto error;
167         }
168
169         flags   = IVAL(hdr, SMB2_HDR_FLAGS);
170         seqnum  = BVAL(hdr, SMB2_HDR_MESSAGE_ID);
171
172         /* match the incoming request against the list of pending requests */
173         for (req=transport->pending_recv; req; req=req->next) {
174                 if (req->seqnum == seqnum) break;
175         }
176
177         if (!req) {
178                 DEBUG(1,("Discarding unmatched reply with seqnum 0x%llx op %d\n", 
179                          (long long)seqnum, SVAL(hdr, SMB2_HDR_OPCODE)));
180                 goto error;
181         }
182
183         /* fill in the 'in' portion of the matching request */
184         req->in.buffer = buffer;
185         talloc_steal(req, buffer);
186         req->in.size = len;
187         req->in.allocated = req->in.size;
188
189         req->in.hdr       = hdr;
190         req->in.body      = hdr+SMB2_HDR_BODY;
191         req->in.body_size = req->in.size - (SMB2_HDR_BODY+NBT_HDR_SIZE);
192         req->status       = NT_STATUS(IVAL(hdr, SMB2_HDR_STATUS));
193
194         if (NT_STATUS_EQUAL(req->status, STATUS_PENDING)) {
195                 if (flags & 0x00000002) {
196                         req->cancel.can_cancel = true;
197                         req->cancel.pending_id = IVAL(hdr, SMB2_HDR_PID);
198                         for (i=0; i< req->cancel.do_cancel; i++) {
199                                 smb2_cancel(req);
200                         }
201                 }
202                 talloc_free(buffer);
203                 return NT_STATUS_OK;
204         }
205
206         buffer_code = SVAL(req->in.body, 0);
207         req->in.body_fixed = (buffer_code & ~1);
208         req->in.dynamic = NULL;
209         dynamic_size = req->in.body_size - req->in.body_fixed;
210         if (dynamic_size != 0 && (buffer_code & 1)) {
211                 req->in.dynamic = req->in.body + req->in.body_fixed;
212                 if (smb2_oob(&req->in, req->in.dynamic, dynamic_size)) {
213                         DEBUG(1,("SMB2 request invalid dynamic size 0x%x\n", 
214                                  dynamic_size));
215                         goto error;
216                 }
217         }
218
219         smb2_setup_bufinfo(req);
220
221         DEBUG(2, ("SMB2 RECV seqnum=0x%llx\n", (long long)req->seqnum));
222         dump_data(5, req->in.body, req->in.body_size);
223
224         /* if this request has an async handler then call that to
225            notify that the reply has been received. This might destroy
226            the request so it must happen last */
227         DLIST_REMOVE(transport->pending_recv, req);
228         req->state = SMB2_REQUEST_DONE;
229         if (req->async.fn) {
230                 req->async.fn(req);
231         }
232         return NT_STATUS_OK;
233
234 error:
235         dump_data(5, buffer, len);
236         if (req) {
237                 DLIST_REMOVE(transport->pending_recv, req);
238                 req->state = SMB2_REQUEST_ERROR;
239                 if (req->async.fn) {
240                         req->async.fn(req);
241                 }
242         } else {
243                 talloc_free(buffer);
244         }
245         return NT_STATUS_UNSUCCESSFUL;
246 }
247
248 /*
249   handle timeouts of individual smb requests
250 */
251 static void smb2_timeout_handler(struct event_context *ev, struct timed_event *te, 
252                                  struct timeval t, void *private)
253 {
254         struct smb2_request *req = talloc_get_type(private, struct smb2_request);
255
256         if (req->state == SMB2_REQUEST_RECV) {
257                 DLIST_REMOVE(req->transport->pending_recv, req);
258         }
259         req->status = NT_STATUS_IO_TIMEOUT;
260         req->state = SMB2_REQUEST_ERROR;
261         if (req->async.fn) {
262                 req->async.fn(req);
263         }
264 }
265
266
267 /*
268   destroy a request
269 */
270 static int smb2_request_destructor(struct smb2_request *req)
271 {
272         if (req->state == SMB2_REQUEST_RECV) {
273                 DLIST_REMOVE(req->transport->pending_recv, req);
274         }
275         return 0;
276 }
277
278
279 /*
280   put a request into the send queue
281 */
282 void smb2_transport_send(struct smb2_request *req)
283 {
284         DATA_BLOB blob;
285         NTSTATUS status;
286
287         _smb2_setlen(req->out.buffer, req->out.size - NBT_HDR_SIZE);
288
289         DEBUG(2, ("SMB2 send seqnum=0x%llx\n", (long long)req->seqnum));
290         dump_data(5, req->out.body, req->out.body_size);
291
292         /* check if the transport is dead */
293         if (req->transport->socket->sock == NULL) {
294                 req->state = SMB2_REQUEST_ERROR;
295                 req->status = NT_STATUS_NET_WRITE_FAULT;
296                 return;
297         }
298
299         blob = data_blob_const(req->out.buffer, req->out.size);
300         status = packet_send(req->transport->packet, blob);
301         if (!NT_STATUS_IS_OK(status)) {
302                 req->state = SMB2_REQUEST_ERROR;
303                 req->status = status;
304                 return;
305         }
306
307         req->state = SMB2_REQUEST_RECV;
308         DLIST_ADD(req->transport->pending_recv, req);
309
310         /* add a timeout */
311         if (req->transport->options.timeout) {
312                 event_add_timed(req->transport->socket->event.ctx, req, 
313                                 timeval_current_ofs(req->transport->options.timeout, 0), 
314                                 smb2_timeout_handler, req);
315         }
316
317         talloc_set_destructor(req, smb2_request_destructor);
318 }
319
320 static void idle_handler(struct event_context *ev, 
321                          struct timed_event *te, struct timeval t, void *private)
322 {
323         struct smb2_transport *transport = talloc_get_type(private,
324                                                            struct smb2_transport);
325         struct timeval next = timeval_add(&t, 0, transport->idle.period);
326         transport->socket->event.te = event_add_timed(transport->socket->event.ctx, 
327                                                       transport,
328                                                       next,
329                                                       idle_handler, transport);
330         transport->idle.func(transport, transport->idle.private);
331 }
332
333 /*
334   setup the idle handler for a transport
335   the period is in microseconds
336 */
337 void smb2_transport_idle_handler(struct smb2_transport *transport, 
338                                  void (*idle_func)(struct smb2_transport *, void *),
339                                  uint64_t period,
340                                  void *private)
341 {
342         transport->idle.func = idle_func;
343         transport->idle.private = private;
344         transport->idle.period = period;
345
346         if (transport->socket->event.te != NULL) {
347                 talloc_free(transport->socket->event.te);
348         }
349
350         transport->socket->event.te = event_add_timed(transport->socket->event.ctx, 
351                                                       transport,
352                                                       timeval_current_ofs(0, period),
353                                                       idle_handler, transport);
354 }