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