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