r11714: put in a workaround for a winbind problem volker asked me about. The
[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
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(void *ptr)
53 {
54         struct smbcli_transport *transport = ptr;
55
56         smbcli_transport_dead(transport);
57         return 0;
58 }
59
60
61 /*
62   handle receive errors
63 */
64 static void smbcli_transport_error(void *private, NTSTATUS status)
65 {
66         struct smbcli_transport *transport = talloc_get_type(private, struct smbcli_transport);
67         smbcli_transport_dead(transport);
68 }
69
70 static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob);
71
72 /*
73   create a transport structure based on an established socket
74 */
75 struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock,
76                                                TALLOC_CTX *parent_ctx, BOOL primary)
77 {
78         struct smbcli_transport *transport;
79
80         transport = talloc_zero(parent_ctx, struct smbcli_transport);
81         if (!transport) return NULL;
82
83         if (primary) {
84                 transport->socket = talloc_steal(transport, sock);
85         } else {
86                 transport->socket = talloc_reference(transport, sock);
87         }
88         transport->negotiate.protocol = PROTOCOL_NT1;
89         transport->options.use_spnego = lp_use_spnego() && lp_nt_status_support();
90         transport->options.max_xmit = lp_max_xmit();
91         transport->options.max_mux = lp_maxmux();
92         transport->options.request_timeout = SMB_REQUEST_TIMEOUT;
93
94         transport->negotiate.max_xmit = transport->options.max_xmit;
95
96         /* setup the stream -> packet parser */
97         transport->packet = packet_init(transport);
98         if (transport->packet == NULL) {
99                 talloc_free(transport);
100                 return NULL;
101         }
102         packet_set_private(transport->packet, transport);
103         packet_set_socket(transport->packet, transport->socket->sock);
104         packet_set_callback(transport->packet, smbcli_transport_finish_recv);
105         packet_set_full_request(transport->packet, packet_full_request_nbt);
106         packet_set_error_handler(transport->packet, smbcli_transport_error);
107         packet_set_event_context(transport->packet, transport->socket->event.ctx);
108         packet_set_nofree(transport->packet);
109
110         smbcli_init_signing(transport);
111
112         ZERO_STRUCT(transport->called);
113
114         /* take over event handling from the socket layer - it only
115            handles events up until we are connected */
116         talloc_free(transport->socket->event.fde);
117         transport->socket->event.fde = event_add_fd(transport->socket->event.ctx,
118                                                     transport->socket,
119                                                     socket_get_fd(transport->socket->sock),
120                                                     EVENT_FD_READ,
121                                                     smbcli_transport_event_handler,
122                                                     transport);
123
124         packet_set_fde(transport->packet, transport->socket->event.fde);
125 #if 0
126         /* winbind relies on non-serialised connections for dcerpc bind. Once that is
127            fixed we can go back to serialised connections */
128         packet_set_serialise(transport->packet);
129 #endif
130         talloc_set_destructor(transport, transport_destructor);
131
132         return transport;
133 }
134
135 /*
136   mark the transport as dead
137 */
138 void smbcli_transport_dead(struct smbcli_transport *transport)
139 {
140         smbcli_sock_dead(transport->socket);
141
142         /* kill all pending receives */
143         while (transport->pending_recv) {
144                 struct smbcli_request *req = transport->pending_recv;
145                 req->state = SMBCLI_REQUEST_ERROR;
146                 req->status = NT_STATUS_NET_WRITE_FAULT;
147                 DLIST_REMOVE(transport->pending_recv, req);
148                 if (req->async.fn) {
149                         req->async.fn(req);
150                 }
151         }
152 }
153
154
155 /*
156   send a session request
157 */
158 struct smbcli_request *smbcli_transport_connect_send(struct smbcli_transport *transport,
159                                                      struct nbt_name *calling, 
160                                                      struct nbt_name *called)
161 {
162         uint8_t *p;
163         struct smbcli_request *req;
164         DATA_BLOB calling_blob, called_blob;
165         TALLOC_CTX *tmp_ctx = talloc_new(transport);
166         NTSTATUS status;
167
168         status = nbt_name_dup(transport, called, &transport->called);
169         if (!NT_STATUS_IS_OK(status)) goto failed;
170         
171         status = nbt_name_to_blob(tmp_ctx, &calling_blob, calling);
172         if (!NT_STATUS_IS_OK(status)) goto failed;
173
174         status = nbt_name_to_blob(tmp_ctx, &called_blob, called);
175         if (!NT_STATUS_IS_OK(status)) goto failed;
176
177         /* allocate output buffer */
178         req = smbcli_request_setup_nonsmb(transport, 
179                                           NBT_HDR_SIZE + 
180                                           calling_blob.length + called_blob.length);
181         if (req == NULL) goto failed;
182
183         /* put in the destination name */
184         p = req->out.buffer + NBT_HDR_SIZE;
185         memcpy(p, called_blob.data, called_blob.length);
186         p += called_blob.length;
187
188         memcpy(p, calling_blob.data, calling_blob.length);
189         p += calling_blob.length;
190
191         _smb_setlen(req->out.buffer, PTR_DIFF(p, req->out.buffer)-4);
192         SCVAL(req->out.buffer,0,0x81);
193
194         if (!smbcli_request_send(req)) {
195                 smbcli_request_destroy(req);
196                 goto failed;
197         }
198
199         talloc_free(tmp_ctx);
200         return req;
201
202 failed:
203         talloc_free(tmp_ctx);
204         return NULL;
205 }
206
207 /*
208   map a session request error to a NTSTATUS
209  */
210 static NTSTATUS map_session_refused_error(uint8_t error)
211 {
212         switch (error) {
213         case 0x80:
214         case 0x81:
215                 return NT_STATUS_REMOTE_NOT_LISTENING;
216         case 0x82:
217                 return NT_STATUS_RESOURCE_NAME_NOT_FOUND;
218         case 0x83:
219                 return NT_STATUS_REMOTE_RESOURCES;
220         }
221         return NT_STATUS_UNEXPECTED_IO_ERROR;
222 }
223
224
225 /*
226   finish a smbcli_transport_connect()
227 */
228 NTSTATUS smbcli_transport_connect_recv(struct smbcli_request *req)
229 {
230         NTSTATUS status;
231
232         if (!smbcli_request_receive(req)) {
233                 smbcli_request_destroy(req);
234                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
235         }
236
237         switch (CVAL(req->in.buffer,0)) {
238         case 0x82:
239                 status = NT_STATUS_OK;
240                 break;
241         case 0x83:
242                 status = map_session_refused_error(CVAL(req->in.buffer,4));
243                 break;
244         case 0x84:
245                 DEBUG(1,("Warning: session retarget not supported\n"));
246                 status = NT_STATUS_NOT_SUPPORTED;
247                 break;
248         default:
249                 status = NT_STATUS_UNEXPECTED_IO_ERROR;
250                 break;
251         }
252
253         smbcli_request_destroy(req);
254         return status;
255 }
256
257
258 /*
259   send a session request (if needed)
260 */
261 BOOL smbcli_transport_connect(struct smbcli_transport *transport,
262                               struct nbt_name *calling, 
263                               struct nbt_name *called)
264 {
265         struct smbcli_request *req;
266         NTSTATUS status;
267
268         if (transport->socket->port == 445) {
269                 return True;
270         }
271
272         req = smbcli_transport_connect_send(transport, 
273                                             calling, called);
274         status = smbcli_transport_connect_recv(req);
275         return NT_STATUS_IS_OK(status);
276 }
277
278 /****************************************************************************
279 get next mid in sequence
280 ****************************************************************************/
281 uint16_t smbcli_transport_next_mid(struct smbcli_transport *transport)
282 {
283         uint16_t mid;
284         struct smbcli_request *req;
285
286         mid = transport->next_mid;
287
288 again:
289         /* now check to see if this mid is being used by one of the 
290            pending requests. This is quite efficient because the list is
291            usually very short */
292
293         /* the zero mid is reserved for requests that don't have a mid */
294         if (mid == 0) mid = 1;
295
296         for (req=transport->pending_recv; req; req=req->next) {
297                 if (req->mid == mid) {
298                         mid++;
299                         goto again;
300                 }
301         }
302
303         transport->next_mid = mid+1;
304         return mid;
305 }
306
307 static void idle_handler(struct event_context *ev, 
308                          struct timed_event *te, struct timeval t, void *private)
309 {
310         struct smbcli_transport *transport = talloc_get_type(private,
311                                                              struct smbcli_transport);
312         struct timeval next = timeval_add(&t, 0, transport->idle.period);
313         transport->socket->event.te = event_add_timed(transport->socket->event.ctx, 
314                                                       transport,
315                                                       next,
316                                                       idle_handler, transport);
317         transport->idle.func(transport, transport->idle.private);
318 }
319
320 /*
321   setup the idle handler for a transport
322   the period is in microseconds
323 */
324 void smbcli_transport_idle_handler(struct smbcli_transport *transport, 
325                                    void (*idle_func)(struct smbcli_transport *, void *),
326                                    uint64_t period,
327                                    void *private)
328 {
329         transport->idle.func = idle_func;
330         transport->idle.private = private;
331         transport->idle.period = period;
332
333         if (transport->socket->event.te != NULL) {
334                 talloc_free(transport->socket->event.te);
335         }
336
337         transport->socket->event.te = event_add_timed(transport->socket->event.ctx, 
338                                                       transport,
339                                                       timeval_current_ofs(0, period),
340                                                       idle_handler, transport);
341 }
342
343 /*
344   we have a full request in our receive buffer - match it to a pending request
345   and process
346  */
347 static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob)
348 {
349         struct smbcli_transport *transport = talloc_get_type(private, 
350                                                              struct smbcli_transport);
351         uint8_t *buffer, *hdr, *vwv;
352         int len;
353         uint16_t wct=0, mid = 0, op = 0;
354         struct smbcli_request *req = NULL;
355
356         buffer = blob.data;
357         len = blob.length;
358
359         hdr = buffer+NBT_HDR_SIZE;
360         vwv = hdr + HDR_VWV;
361
362         /* see if it could be an oplock break request */
363         if (smbcli_handle_oplock_break(transport, len, hdr, vwv)) {
364                 talloc_free(buffer);
365                 return NT_STATUS_OK;
366         }
367
368         /* at this point we need to check for a readbraw reply, as
369            these can be any length */
370         if (transport->readbraw_pending) {
371                 transport->readbraw_pending = 0;
372
373                 /* it must match the first entry in the pending queue
374                    as the client is not allowed to have outstanding
375                    readbraw requests */
376                 req = transport->pending_recv;
377                 if (!req) goto error;
378
379                 req->in.buffer = buffer;
380                 talloc_steal(req, buffer);
381                 req->in.size = len;
382                 req->in.allocated = req->in.size;
383                 goto async;
384         }
385
386         if (len >= MIN_SMB_SIZE) {
387                 /* extract the mid for matching to pending requests */
388                 mid = SVAL(hdr, HDR_MID);
389                 wct = CVAL(hdr, HDR_WCT);
390                 op  = CVAL(hdr, HDR_COM);
391         }
392
393         /* match the incoming request against the list of pending requests */
394         for (req=transport->pending_recv; req; req=req->next) {
395                 if (req->mid == mid) break;
396         }
397
398         /* see if it's a ntcancel reply for the current MID */
399         req = smbcli_handle_ntcancel_reply(req, len, hdr);
400
401         if (!req) {
402                 DEBUG(1,("Discarding unmatched reply with mid %d op %d\n", mid, op));
403                 goto error;
404         }
405
406         /* fill in the 'in' portion of the matching request */
407         req->in.buffer = buffer;
408         talloc_steal(req, buffer);
409         req->in.size = len;
410         req->in.allocated = req->in.size;
411
412         /* handle NBT session replies */
413         if (req->in.size >= 4 && req->in.buffer[0] != 0) {
414                 req->status = NT_STATUS_OK;
415                 goto async;
416         }
417
418         /* handle non-SMB replies */
419         if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE) {
420                 req->state = SMBCLI_REQUEST_ERROR;
421                 goto error;
422         }
423
424         if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct)) {
425                 DEBUG(2,("bad reply size for mid %d\n", mid));
426                 req->status = NT_STATUS_UNSUCCESSFUL;
427                 req->state = SMBCLI_REQUEST_ERROR;
428                 goto error;
429         }
430
431         req->in.hdr = hdr;
432         req->in.vwv = vwv;
433         req->in.wct = wct;
434         if (req->in.size >= NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct)) {
435                 req->in.data = req->in.vwv + VWV(wct) + 2;
436                 req->in.data_size = SVAL(req->in.vwv, VWV(wct));
437                 if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct) + req->in.data_size) {
438                         DEBUG(3,("bad data size for mid %d\n", mid));
439                         /* blergh - w2k3 gives a bogus data size values in some
440                            openX replies */
441                         req->in.data_size = req->in.size - (NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct));
442                 }
443         }
444         req->in.ptr = req->in.data;
445         req->flags2 = SVAL(req->in.hdr, HDR_FLG2);
446
447         if (!(req->flags2 & FLAGS2_32_BIT_ERROR_CODES)) {
448                 int class = CVAL(req->in.hdr,HDR_RCLS);
449                 int code = SVAL(req->in.hdr,HDR_ERR);
450                 if (class == 0 && code == 0) {
451                         transport->error.e.nt_status = NT_STATUS_OK;
452                 } else {
453                         transport->error.e.nt_status = NT_STATUS_DOS(class, code);
454                 }
455         } else {
456                 transport->error.e.nt_status = NT_STATUS(IVAL(req->in.hdr, HDR_RCLS));
457         }
458
459         req->status = transport->error.e.nt_status;
460         if (NT_STATUS_IS_OK(req->status)) {
461                 transport->error.etype = ETYPE_NONE;
462         } else {
463                 transport->error.etype = ETYPE_SMB;
464         }
465
466         if (!smbcli_request_check_sign_mac(req)) {
467                 transport->error.etype = ETYPE_SOCKET;
468                 transport->error.e.socket_error = SOCKET_READ_BAD_SIG;
469                 req->state = SMBCLI_REQUEST_ERROR;
470                 req->status = NT_STATUS_ACCESS_DENIED;
471                 goto error;
472         };
473
474 async:
475         /* if this request has an async handler then call that to
476            notify that the reply has been received. This might destroy
477            the request so it must happen last */
478         DLIST_REMOVE(transport->pending_recv, req);
479         req->state = SMBCLI_REQUEST_DONE;
480         if (req->async.fn) {
481                 req->async.fn(req);
482         }
483         return NT_STATUS_OK;
484
485 error:
486         if (req) {
487                 DLIST_REMOVE(transport->pending_recv, req);
488                 req->state = SMBCLI_REQUEST_ERROR;
489                 if (req->async.fn) {
490                         req->async.fn(req);
491                 }
492         } else {
493                 talloc_free(buffer);
494         }
495         return NT_STATUS_OK;
496 }
497
498 /*
499   process some read/write requests that are pending
500   return False if the socket is dead
501 */
502 BOOL smbcli_transport_process(struct smbcli_transport *transport)
503 {
504         NTSTATUS status;
505         size_t npending;
506
507         packet_queue_run(transport->packet);
508         if (transport->socket->sock == NULL) {
509                 return False;
510         }
511
512         status = socket_pending(transport->socket->sock, &npending);
513         if (NT_STATUS_IS_OK(status) && npending > 0) {
514                 packet_recv(transport->packet);
515         }
516         if (transport->socket->sock == NULL) {
517                 return False;
518         }
519         return True;
520 }
521
522 /*
523   handle timeouts of individual smb requests
524 */
525 static void smbcli_timeout_handler(struct event_context *ev, struct timed_event *te, 
526                                    struct timeval t, void *private)
527 {
528         struct smbcli_request *req = talloc_get_type(private, struct smbcli_request);
529
530         if (req->state == SMBCLI_REQUEST_RECV) {
531                 DLIST_REMOVE(req->transport->pending_recv, req);
532         }
533         req->status = NT_STATUS_IO_TIMEOUT;
534         req->state = SMBCLI_REQUEST_ERROR;
535         if (req->async.fn) {
536                 req->async.fn(req);
537         }
538 }
539
540
541 /*
542   destroy a request
543 */
544 static int smbcli_request_destructor(void *ptr)
545 {
546         struct smbcli_request *req = talloc_get_type(ptr, struct smbcli_request);
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 }