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