r2655: fixed an error in the shutdown of the sock->transport->session->tree
[jelmer/samba4-debian.git] / source / libcli / raw / clitransport.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB client transport context management functions
4    Copyright (C) Andrew Tridgell 1994-2003
5    Copyright (C) James Myers 2003 <myersjj@samba.org>
6    
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 2 of the License, or
10    (at your option) any later version.
11    
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.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24
25 static void smbcli_transport_process_recv(struct smbcli_transport *transport);
26 static void smbcli_transport_process_send(struct smbcli_transport *transport);
27
28 /*
29   an event has happened on the socket
30 */
31 static void smbcli_transport_event_handler(struct event_context *ev, struct fd_event *fde, 
32                                         time_t t, uint16_t flags)
33 {
34         struct smbcli_transport *transport = fde->private;
35
36         if (flags & EVENT_FD_READ) {
37                 smbcli_transport_process_recv(transport);
38         }
39         if (flags & EVENT_FD_WRITE) {
40                 smbcli_transport_process_send(transport);
41         }
42 }
43
44 /*
45   create a transport structure based on an established socket
46 */
47 struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock)
48 {
49         struct smbcli_transport *transport;
50         struct fd_event fde;
51
52         transport = talloc_p(sock, struct smbcli_transport);
53         if (!transport) return NULL;
54
55         ZERO_STRUCTP(transport);
56
57         transport->event.ctx = event_context_init();
58         if (transport->event.ctx == NULL) {
59                 talloc_free(transport);
60                 return NULL;
61         }
62
63         transport->socket = sock;
64         transport->negotiate.protocol = PROTOCOL_NT1;
65         transport->options.use_spnego = lp_use_spnego();
66         transport->negotiate.max_xmit = ~0;
67         
68         smbcli_init_signing(transport);
69
70         transport->socket->reference_count++;
71
72         ZERO_STRUCT(transport->called);
73
74         fde.fd = sock->fd;
75         fde.flags = EVENT_FD_READ;
76         fde.handler = smbcli_transport_event_handler;
77         fde.private = transport;
78         fde.ref_count = 1;
79
80         transport->event.fde = event_add_fd(transport->event.ctx, &fde);
81
82         return transport;
83 }
84
85 /*
86   decrease reference count on a transport, and destroy if it becomes
87   zero
88 */
89 void smbcli_transport_close(struct smbcli_transport *transport)
90 {
91         transport->reference_count--;
92         if (transport->reference_count <= 0) {
93                 event_remove_fd(transport->event.ctx, transport->event.fde);
94                 event_remove_timed(transport->event.ctx, transport->event.te);
95                 event_context_destroy(transport->event.ctx);
96                 smbcli_sock_close(transport->socket);
97         }
98 }
99
100 /*
101   mark the transport as dead
102 */
103 void smbcli_transport_dead(struct smbcli_transport *transport)
104 {
105         smbcli_sock_dead(transport->socket);
106
107         /* all pending sends become errors */
108         while (transport->pending_send) {
109                 struct smbcli_request *req = transport->pending_send;
110                 req->state = SMBCLI_REQUEST_ERROR;
111                 req->status = NT_STATUS_NET_WRITE_FAULT;
112                 DLIST_REMOVE(transport->pending_send, req);
113                 if (req->async.fn) {
114                         req->async.fn(req);
115                 }
116         }
117
118         /* as do all pending receives */
119         while (transport->pending_recv) {
120                 struct smbcli_request *req = transport->pending_recv;
121                 req->state = SMBCLI_REQUEST_ERROR;
122                 req->status = NT_STATUS_NET_WRITE_FAULT;
123                 DLIST_REMOVE(transport->pending_recv, req);
124                 if (req->async.fn) {
125                         req->async.fn(req);
126                 }
127         }
128 }
129
130
131 /*
132   enable select for write on a transport
133 */
134 static void smbcli_transport_write_enable(struct smbcli_transport *transport)
135 {
136         transport->event.fde->flags |= EVENT_FD_WRITE;
137 }
138
139 /*
140   disable select for write on a transport
141 */
142 static void smbcli_transport_write_disable(struct smbcli_transport *transport)
143 {
144         transport->event.fde->flags &= ~EVENT_FD_WRITE;
145 }
146
147 /****************************************************************************
148 send a session request (if appropriate)
149 ****************************************************************************/
150 BOOL smbcli_transport_connect(struct smbcli_transport *transport,
151                            struct nmb_name *calling, 
152                            struct nmb_name *called)
153 {
154         char *p;
155         int len = NBT_HDR_SIZE;
156         struct smbcli_request *req;
157
158         if (called) {
159                 transport->called = *called;
160         }
161
162         /* 445 doesn't have session request */
163         if (transport->socket->port == 445) {
164                 return True;
165         }
166
167         /* allocate output buffer */
168         req = smbcli_request_setup_nonsmb(transport, NBT_HDR_SIZE + 2*nbt_mangled_name_len());
169
170         /* put in the destination name */
171         p = req->out.buffer + NBT_HDR_SIZE;
172         name_mangle(called->name, p, called->name_type);
173         len += name_len(p);
174
175         /* and my name */
176         p = req->out.buffer+len;
177         name_mangle(calling->name, p, calling->name_type);
178         len += name_len(p);
179
180         _smb_setlen(req->out.buffer,len-4);
181         SCVAL(req->out.buffer,0,0x81);
182
183         if (!smbcli_request_send(req) ||
184             !smbcli_request_receive(req)) {
185                 smbcli_request_destroy(req);
186                 return False;
187         }
188         
189         if (CVAL(req->in.buffer,0) != 0x82) {
190                 transport->error.etype = ETYPE_NBT;
191                 transport->error.e.nbt_error = CVAL(req->in.buffer,4);
192                 smbcli_request_destroy(req);
193                 return False;
194         }
195
196         smbcli_request_destroy(req);
197         return True;
198 }
199
200
201 /****************************************************************************
202 get next mid in sequence
203 ****************************************************************************/
204 uint16_t smbcli_transport_next_mid(struct smbcli_transport *transport)
205 {
206         uint16_t mid;
207         struct smbcli_request *req;
208
209         mid = transport->next_mid;
210
211 again:
212         /* now check to see if this mid is being used by one of the 
213            pending requests. This is quite efficient because the list is
214            usually very short */
215
216         /* the zero mid is reserved for requests that don't have a mid */
217         if (mid == 0) mid = 1;
218
219         for (req=transport->pending_recv; req; req=req->next) {
220                 if (req->mid == mid) {
221                         mid++;
222                         goto again;
223                 }
224         }
225
226         transport->next_mid = mid+1;
227         return mid;
228 }
229
230 static void idle_handler(struct event_context *ev, 
231                          struct timed_event *te, time_t t)
232 {
233         struct smbcli_transport *transport = te->private;
234         te->next_event = t + transport->idle.period;
235         transport->idle.func(transport, transport->idle.private);
236 }
237
238 /*
239   setup the idle handler for a transport
240   the period is in seconds
241 */
242 void smbcli_transport_idle_handler(struct smbcli_transport *transport, 
243                                 void (*idle_func)(struct smbcli_transport *, void *),
244                                 uint_t period,
245                                 void *private)
246 {
247         struct timed_event te;
248         transport->idle.func = idle_func;
249         transport->idle.private = private;
250         transport->idle.period = period;
251
252         if (transport->event.te != NULL) {
253                 event_remove_timed(transport->event.ctx, transport->event.te);
254         }
255
256         te.next_event = time(NULL) + period;
257         te.handler = idle_handler;
258         te.private = transport;
259         transport->event.te = event_add_timed(transport->event.ctx, &te);
260 }
261
262 /*
263   process some pending sends
264 */
265 static void smbcli_transport_process_send(struct smbcli_transport *transport)
266 {
267         while (transport->pending_send) {
268                 struct smbcli_request *req = transport->pending_send;
269                 ssize_t ret;
270                 ret = smbcli_sock_write(transport->socket, req->out.buffer, req->out.size);
271                 if (ret == -1) {
272                         if (errno == EAGAIN || errno == EINTR) {
273                                 return;
274                         }
275                         smbcli_transport_dead(transport);
276                         return;
277                 }
278                 req->out.buffer += ret;
279                 req->out.size -= ret;
280                 if (req->out.size == 0) {
281                         DLIST_REMOVE(transport->pending_send, req);
282                         if (req->one_way_request) {
283                                 req->state = SMBCLI_REQUEST_DONE;
284                                 smbcli_request_destroy(req);
285                         } else {
286                                 req->state = SMBCLI_REQUEST_RECV;
287                                 DLIST_ADD(transport->pending_recv, req);
288                         }
289                 }
290         }
291
292         /* we're out of requests to send, so don't wait for write
293            events any more */
294         smbcli_transport_write_disable(transport);
295 }
296
297 /*
298   we have a full request in our receive buffer - match it to a pending request
299   and process
300  */
301 static void smbcli_transport_finish_recv(struct smbcli_transport *transport)
302 {
303         uint8_t *buffer, *hdr, *vwv;
304         int len;
305         uint16_t wct=0, mid = 0;
306         struct smbcli_request *req;
307
308         buffer = transport->recv_buffer.buffer;
309         len = transport->recv_buffer.req_size;
310
311         ZERO_STRUCT(transport->recv_buffer);
312
313         hdr = buffer+NBT_HDR_SIZE;
314         vwv = hdr + HDR_VWV;
315
316         /* see if it could be an oplock break request */
317         if (handle_oplock_break(transport, len, hdr, vwv)) {
318                 talloc_free(buffer);
319                 return;
320         }
321
322         /* at this point we need to check for a readbraw reply, as
323            these can be any length */
324         if (transport->readbraw_pending) {
325                 transport->readbraw_pending = 0;
326
327                 /* it must match the first entry in the pending queue
328                    as the client is not allowed to have outstanding
329                    readbraw requests */
330                 req = transport->pending_recv;
331                 if (!req) goto error;
332
333                 req->in.buffer = buffer;
334                 talloc_steal(req, buffer);
335                 req->in.size = len;
336                 req->in.allocated = req->in.size;
337                 goto async;
338         }
339
340         if (len >= MIN_SMB_SIZE) {
341                 /* extract the mid for matching to pending requests */
342                 mid = SVAL(hdr, HDR_MID);
343                 wct = CVAL(hdr, HDR_WCT);
344         }
345
346         /* match the incoming request against the list of pending requests */
347         for (req=transport->pending_recv; req; req=req->next) {
348                 if (req->mid == mid) break;
349         }
350
351         if (!req) {
352                 DEBUG(1,("Discarding unmatched reply with mid %d\n", mid));
353                 goto error;
354         }
355
356         /* fill in the 'in' portion of the matching request */
357         req->in.buffer = buffer;
358         talloc_steal(req, buffer);
359         req->in.size = len;
360         req->in.allocated = req->in.size;
361
362         /* handle NBT session replies */
363         if (req->in.size >= 4 && req->in.buffer[0] != 0) {
364                 req->status = NT_STATUS_OK;
365                 goto async;
366         }
367
368         /* handle non-SMB replies */
369         if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE) {
370                 req->state = SMBCLI_REQUEST_ERROR;
371                 goto error;
372         }
373
374         if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct)) {
375                 DEBUG(2,("bad reply size for mid %d\n", mid));
376                 req->status = NT_STATUS_UNSUCCESSFUL;
377                 req->state = SMBCLI_REQUEST_ERROR;
378                 goto error;
379         }
380
381         req->in.hdr = hdr;
382         req->in.vwv = vwv;
383         req->in.wct = wct;
384         if (req->in.size >= NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct)) {
385                 req->in.data = req->in.vwv + VWV(wct) + 2;
386                 req->in.data_size = SVAL(req->in.vwv, VWV(wct));
387                 if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct) + req->in.data_size) {
388                         DEBUG(3,("bad data size for mid %d\n", mid));
389                         /* blergh - w2k3 gives a bogus data size values in some
390                            openX replies */
391                         req->in.data_size = req->in.size - (NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct));
392                 }
393         }
394         req->in.ptr = req->in.data;
395         req->flags2 = SVAL(req->in.hdr, HDR_FLG2);
396
397         if (!(req->flags2 & FLAGS2_32_BIT_ERROR_CODES)) {
398                 transport->error.etype = ETYPE_DOS;
399                 transport->error.e.dos.eclass = CVAL(req->in.hdr,HDR_RCLS);
400                 transport->error.e.dos.ecode = SVAL(req->in.hdr,HDR_ERR);
401                 req->status = dos_to_ntstatus(transport->error.e.dos.eclass, 
402                                               transport->error.e.dos.ecode);
403         } else {
404                 transport->error.etype = ETYPE_NT;
405                 transport->error.e.nt_status = NT_STATUS(IVAL(req->in.hdr, HDR_RCLS));
406                 req->status = transport->error.e.nt_status;
407         }
408
409         if (!smbcli_request_check_sign_mac(req)) {
410                 transport->error.etype = ETYPE_SOCKET;
411                 transport->error.e.socket_error = SOCKET_READ_BAD_SIG;
412                 req->state = SMBCLI_REQUEST_ERROR;
413                 goto error;
414         };
415
416 async:
417         /* if this request has an async handler then call that to
418            notify that the reply has been received. This might destroy
419            the request so it must happen last */
420         DLIST_REMOVE(transport->pending_recv, req);
421         req->state = SMBCLI_REQUEST_DONE;
422         if (req->async.fn) {
423                 req->async.fn(req);
424         }
425         return;
426
427 error:
428         if (req) {
429                 DLIST_REMOVE(transport->pending_recv, req);
430                 req->state = SMBCLI_REQUEST_ERROR;
431         }
432 }
433
434 /*
435   process some pending receives
436 */
437 static void smbcli_transport_process_recv(struct smbcli_transport *transport)
438 {
439         /* a incoming packet goes through 2 stages - first we read the
440            4 byte header, which tells us how much more is coming. Then
441            we read the rest */
442         if (transport->recv_buffer.received < NBT_HDR_SIZE) {
443                 ssize_t ret;
444                 ret = smbcli_sock_read(transport->socket, 
445                                     transport->recv_buffer.header + 
446                                     transport->recv_buffer.received,
447                                     NBT_HDR_SIZE - transport->recv_buffer.received);
448                 if (ret == 0) {
449                         smbcli_transport_dead(transport);
450                         return;
451                 }
452                 if (ret == -1) {
453                         if (errno == EINTR || errno == EAGAIN) {
454                                 return;
455                         }
456                         smbcli_transport_dead(transport);
457                         return;
458                 }
459
460                 transport->recv_buffer.received += ret;
461
462                 if (transport->recv_buffer.received == NBT_HDR_SIZE) {
463                         /* we've got a full header */
464                         transport->recv_buffer.req_size = smb_len(transport->recv_buffer.header) + NBT_HDR_SIZE;
465                         transport->recv_buffer.buffer = talloc(transport,
466                                                                NBT_HDR_SIZE+transport->recv_buffer.req_size);
467                         if (transport->recv_buffer.buffer == NULL) {
468                                 smbcli_transport_dead(transport);
469                                 return;
470                         }
471                         memcpy(transport->recv_buffer.buffer, transport->recv_buffer.header, NBT_HDR_SIZE);
472                 }
473         }
474
475         if (transport->recv_buffer.received < transport->recv_buffer.req_size) {
476                 ssize_t ret;
477                 ret = smbcli_sock_read(transport->socket, 
478                                     transport->recv_buffer.buffer + 
479                                     transport->recv_buffer.received,
480                                     transport->recv_buffer.req_size - 
481                                     transport->recv_buffer.received);
482                 if (ret == -1) {
483                         if (errno == EINTR || errno == EAGAIN) {
484                                 return;
485                         }
486                         smbcli_transport_dead(transport);
487                         return;
488                 }
489                 transport->recv_buffer.received += ret;
490         }
491
492         if (transport->recv_buffer.received != 0 &&
493             transport->recv_buffer.received == transport->recv_buffer.req_size) {
494                 smbcli_transport_finish_recv(transport);
495         }
496 }
497
498 /*
499   process some read/write requests that are pending
500   return False if the socket is dead
501 */
502 BOOL smbcli_transport_process(struct smbcli_transport *transport)
503 {
504         smbcli_transport_process_send(transport);
505         smbcli_transport_process_recv(transport);
506         if (transport->socket->fd == -1) {
507                 return False;
508         }
509         return True;
510 }
511
512
513
514 /*
515   put a request into the send queue
516 */
517 void smbcli_transport_send(struct smbcli_request *req)
518 {
519         /* check if the transport is dead */
520         if (req->transport->socket->fd == -1) {
521                 req->state = SMBCLI_REQUEST_ERROR;
522                 req->status = NT_STATUS_NET_WRITE_FAULT;
523                 return;
524         }
525
526         /* put it on the outgoing socket queue */
527         req->state = SMBCLI_REQUEST_SEND;
528         DLIST_ADD_END(req->transport->pending_send, req, struct smbcli_request *);
529
530         /* make sure we look for write events */
531         smbcli_transport_write_enable(req->transport);
532 }