r17020: pass the real error to the failing requests
[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 #include "lib/stream/packet.h"
29 #include "librpc/gen_ndr/ndr_nbt.h"
30
31
32 /*
33   an event has happened on the socket
34 */
35 static void smbcli_transport_event_handler(struct event_context *ev, 
36                                            struct fd_event *fde, 
37                                            uint16_t flags, void *private)
38 {
39         struct smbcli_transport *transport = talloc_get_type(private,
40                                                              struct smbcli_transport);
41         if (flags & EVENT_FD_READ) {
42                 packet_recv(transport->packet);
43                 return;
44         }
45         if (flags & EVENT_FD_WRITE) {
46                 packet_queue_run(transport->packet);
47         }
48 }
49
50 /*
51   destroy a transport
52  */
53 static int transport_destructor(struct smbcli_transport *transport)
54 {
55         smbcli_transport_dead(transport, NT_STATUS_LOCAL_DISCONNECT);
56         return 0;
57 }
58
59
60 /*
61   handle receive errors
62 */
63 static void smbcli_transport_error(void *private, NTSTATUS status)
64 {
65         struct smbcli_transport *transport = talloc_get_type(private, struct smbcli_transport);
66         smbcli_transport_dead(transport, status);
67 }
68
69 static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob);
70
71 /*
72   create a transport structure based on an established socket
73 */
74 struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock,
75                                                TALLOC_CTX *parent_ctx, BOOL primary)
76 {
77         struct smbcli_transport *transport;
78
79         transport = talloc_zero(parent_ctx, struct smbcli_transport);
80         if (!transport) return NULL;
81
82         if (primary) {
83                 transport->socket = talloc_steal(transport, sock);
84         } else {
85                 transport->socket = talloc_reference(transport, sock);
86         }
87         transport->negotiate.protocol = PROTOCOL_NT1;
88         transport->options.use_spnego = lp_use_spnego() && lp_nt_status_support();
89         transport->options.max_xmit = lp_max_xmit();
90         transport->options.max_mux = lp_maxmux();
91         transport->options.request_timeout = SMB_REQUEST_TIMEOUT;
92
93         transport->negotiate.max_xmit = transport->options.max_xmit;
94
95         /* setup the stream -> packet parser */
96         transport->packet = packet_init(transport);
97         if (transport->packet == NULL) {
98                 talloc_free(transport);
99                 return NULL;
100         }
101         packet_set_private(transport->packet, transport);
102         packet_set_socket(transport->packet, transport->socket->sock);
103         packet_set_callback(transport->packet, smbcli_transport_finish_recv);
104         packet_set_full_request(transport->packet, packet_full_request_nbt);
105         packet_set_error_handler(transport->packet, smbcli_transport_error);
106         packet_set_event_context(transport->packet, transport->socket->event.ctx);
107         packet_set_nofree(transport->packet);
108
109         smbcli_init_signing(transport);
110
111         ZERO_STRUCT(transport->called);
112
113         /* take over event handling from the socket layer - it only
114            handles events up until we are connected */
115         talloc_free(transport->socket->event.fde);
116         transport->socket->event.fde = event_add_fd(transport->socket->event.ctx,
117                                                     transport->socket->sock,
118                                                     socket_get_fd(transport->socket->sock),
119                                                     EVENT_FD_READ,
120                                                     smbcli_transport_event_handler,
121                                                     transport);
122
123         packet_set_fde(transport->packet, transport->socket->event.fde);
124         packet_set_serialise(transport->packet);
125         talloc_set_destructor(transport, transport_destructor);
126
127         return transport;
128 }
129
130 /*
131   mark the transport as dead
132 */
133 void smbcli_transport_dead(struct smbcli_transport *transport, NTSTATUS status)
134 {
135         smbcli_sock_dead(transport->socket);
136
137         if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
138                 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
139         }
140
141         /* kill all pending receives */
142         while (transport->pending_recv) {
143                 struct smbcli_request *req = transport->pending_recv;
144                 req->state = SMBCLI_REQUEST_ERROR;
145                 req->status = status;
146                 DLIST_REMOVE(transport->pending_recv, req);
147                 if (req->async.fn) {
148                         req->async.fn(req);
149                 }
150         }
151 }
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) - NBT_HDR_SIZE);
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   we have a full request in our receive buffer - match it to a pending request
344   and process
345  */
346 static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob)
347 {
348         struct smbcli_transport *transport = talloc_get_type(private, 
349                                                              struct smbcli_transport);
350         uint8_t *buffer, *hdr, *vwv;
351         int len;
352         uint16_t wct=0, mid = 0, op = 0;
353         struct smbcli_request *req = NULL;
354
355         buffer = blob.data;
356         len = blob.length;
357
358         hdr = buffer+NBT_HDR_SIZE;
359         vwv = hdr + HDR_VWV;
360
361         /* see if it could be an oplock break request */
362         if (smbcli_handle_oplock_break(transport, len, hdr, vwv)) {
363                 talloc_free(buffer);
364                 return NT_STATUS_OK;
365         }
366
367         /* at this point we need to check for a readbraw reply, as
368            these can be any length */
369         if (transport->readbraw_pending) {
370                 transport->readbraw_pending = 0;
371
372                 /* it must match the first entry in the pending queue
373                    as the client is not allowed to have outstanding
374                    readbraw requests */
375                 req = transport->pending_recv;
376                 if (!req) goto error;
377
378                 req->in.buffer = buffer;
379                 talloc_steal(req, buffer);
380                 req->in.size = len;
381                 req->in.allocated = req->in.size;
382                 goto async;
383         }
384
385         if (len >= MIN_SMB_SIZE) {
386                 /* extract the mid for matching to pending requests */
387                 mid = SVAL(hdr, HDR_MID);
388                 wct = CVAL(hdr, HDR_WCT);
389                 op  = CVAL(hdr, HDR_COM);
390         }
391
392         /* match the incoming request against the list of pending requests */
393         for (req=transport->pending_recv; req; req=req->next) {
394                 if (req->mid == mid) break;
395         }
396
397         /* see if it's a ntcancel reply for the current MID */
398         req = smbcli_handle_ntcancel_reply(req, len, hdr);
399
400         if (!req) {
401                 DEBUG(1,("Discarding unmatched reply with mid %d op %d\n", mid, op));
402                 goto error;
403         }
404
405         /* fill in the 'in' portion of the matching request */
406         req->in.buffer = buffer;
407         talloc_steal(req, buffer);
408         req->in.size = len;
409         req->in.allocated = req->in.size;
410
411         /* handle NBT session replies */
412         if (req->in.size >= 4 && req->in.buffer[0] != 0) {
413                 req->status = NT_STATUS_OK;
414                 goto async;
415         }
416
417         /* handle non-SMB replies */
418         if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE) {
419                 req->state = SMBCLI_REQUEST_ERROR;
420                 goto error;
421         }
422
423         if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct)) {
424                 DEBUG(2,("bad reply size for mid %d\n", mid));
425                 req->status = NT_STATUS_UNSUCCESSFUL;
426                 req->state = SMBCLI_REQUEST_ERROR;
427                 goto error;
428         }
429
430         req->in.hdr = hdr;
431         req->in.vwv = vwv;
432         req->in.wct = wct;
433         if (req->in.size >= NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct)) {
434                 req->in.data = req->in.vwv + VWV(wct) + 2;
435                 req->in.data_size = SVAL(req->in.vwv, VWV(wct));
436                 if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct) + req->in.data_size) {
437                         DEBUG(3,("bad data size for mid %d\n", mid));
438                         /* blergh - w2k3 gives a bogus data size values in some
439                            openX replies */
440                         req->in.data_size = req->in.size - (NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct));
441                 }
442         }
443         req->in.ptr = req->in.data;
444         req->flags2 = SVAL(req->in.hdr, HDR_FLG2);
445
446         if (!(req->flags2 & FLAGS2_32_BIT_ERROR_CODES)) {
447                 int class = CVAL(req->in.hdr,HDR_RCLS);
448                 int code = SVAL(req->in.hdr,HDR_ERR);
449                 if (class == 0 && code == 0) {
450                         transport->error.e.nt_status = NT_STATUS_OK;
451                 } else {
452                         transport->error.e.nt_status = NT_STATUS_DOS(class, code);
453                 }
454         } else {
455                 transport->error.e.nt_status = NT_STATUS(IVAL(req->in.hdr, HDR_RCLS));
456         }
457
458         req->status = transport->error.e.nt_status;
459         if (NT_STATUS_IS_OK(req->status)) {
460                 transport->error.etype = ETYPE_NONE;
461         } else {
462                 transport->error.etype = ETYPE_SMB;
463         }
464
465         if (!smbcli_request_check_sign_mac(req)) {
466                 transport->error.etype = ETYPE_SOCKET;
467                 transport->error.e.socket_error = SOCKET_READ_BAD_SIG;
468                 req->state = SMBCLI_REQUEST_ERROR;
469                 req->status = NT_STATUS_ACCESS_DENIED;
470                 goto error;
471         };
472
473 async:
474         /* if this request has an async handler then call that to
475            notify that the reply has been received. This might destroy
476            the request so it must happen last */
477         DLIST_REMOVE(transport->pending_recv, req);
478         req->state = SMBCLI_REQUEST_DONE;
479         if (req->async.fn) {
480                 req->async.fn(req);
481         }
482         return NT_STATUS_OK;
483
484 error:
485         if (req) {
486                 DLIST_REMOVE(transport->pending_recv, req);
487                 req->state = SMBCLI_REQUEST_ERROR;
488                 if (req->async.fn) {
489                         req->async.fn(req);
490                 }
491         } else {
492                 talloc_free(buffer);
493         }
494         return NT_STATUS_OK;
495 }
496
497 /*
498   process some read/write requests that are pending
499   return False if the socket is dead
500 */
501 BOOL smbcli_transport_process(struct smbcli_transport *transport)
502 {
503         NTSTATUS status;
504         size_t npending;
505
506         packet_queue_run(transport->packet);
507         if (transport->socket->sock == NULL) {
508                 return False;
509         }
510
511         status = socket_pending(transport->socket->sock, &npending);
512         if (NT_STATUS_IS_OK(status) && npending > 0) {
513                 packet_recv(transport->packet);
514         }
515         if (transport->socket->sock == NULL) {
516                 return False;
517         }
518         return True;
519 }
520
521 /*
522   handle timeouts of individual smb requests
523 */
524 static void smbcli_timeout_handler(struct event_context *ev, struct timed_event *te, 
525                                    struct timeval t, void *private)
526 {
527         struct smbcli_request *req = talloc_get_type(private, struct smbcli_request);
528
529         if (req->state == SMBCLI_REQUEST_RECV) {
530                 DLIST_REMOVE(req->transport->pending_recv, req);
531         }
532         req->status = NT_STATUS_IO_TIMEOUT;
533         req->state = SMBCLI_REQUEST_ERROR;
534         if (req->async.fn) {
535                 req->async.fn(req);
536         }
537 }
538
539
540 /*
541   destroy a request
542 */
543 static int smbcli_request_destructor(struct smbcli_request *req)
544 {
545         if (req->state == SMBCLI_REQUEST_RECV) {
546                 DLIST_REMOVE(req->transport->pending_recv, req);
547         }
548         return 0;
549 }
550
551
552 /*
553   put a request into the send queue
554 */
555 void smbcli_transport_send(struct smbcli_request *req)
556 {
557         DATA_BLOB blob;
558         NTSTATUS status;
559
560         /* check if the transport is dead */
561         if (req->transport->socket->sock == NULL) {
562                 req->state = SMBCLI_REQUEST_ERROR;
563                 req->status = NT_STATUS_NET_WRITE_FAULT;
564                 return;
565         }
566
567         blob = data_blob_const(req->out.buffer, req->out.size);
568         status = packet_send(req->transport->packet, blob);
569         if (!NT_STATUS_IS_OK(status)) {
570                 req->state = SMBCLI_REQUEST_ERROR;
571                 req->status = status;
572                 return;
573         }
574
575         if (req->one_way_request) {
576                 req->state = SMBCLI_REQUEST_DONE;
577                 smbcli_request_destroy(req);
578                 return;
579         }
580
581         req->state = SMBCLI_REQUEST_RECV;
582         DLIST_ADD(req->transport->pending_recv, req);
583
584         /* add a timeout */
585         if (req->transport->options.request_timeout) {
586                 event_add_timed(req->transport->socket->event.ctx, req, 
587                                 timeval_current_ofs(req->transport->options.request_timeout, 0), 
588                                 smbcli_timeout_handler, req);
589         }
590
591         talloc_set_destructor(req, smbcli_request_destructor);
592 }