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