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