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