r1634: to get signing right for async requests we must send requests in
[jelmer/samba4-debian.git] / source / libcli / raw / clitransport.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB client transport context management functions
4    Copyright (C) Andrew Tridgell 1994-2003
5    Copyright (C) James Myers 2003 <myersjj@samba.org>
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /*
25   an event has happened on the socket
26 */
27 static void cli_transport_event_handler(struct event_context *ev, struct fd_event *fde, 
28                                         time_t t, uint16_t flags)
29 {
30         struct cli_transport *transport = fde->private;
31
32         cli_transport_process(transport);
33 }
34
35 /*
36   create a transport structure based on an established socket
37 */
38 struct cli_transport *cli_transport_init(struct cli_socket *sock)
39 {
40         TALLOC_CTX *mem_ctx;
41         struct cli_transport *transport;
42         struct fd_event fde;
43
44         mem_ctx = talloc_init("cli_transport");
45         if (!mem_ctx) return NULL;
46
47         transport = talloc_zero(mem_ctx, sizeof(*transport));
48         if (!transport) return NULL;
49
50         transport->event.ctx = event_context_init();
51         if (transport->event.ctx == NULL) {
52                 talloc_destroy(mem_ctx);
53                 return NULL;
54         }
55
56         transport->mem_ctx = mem_ctx;
57         transport->socket = sock;
58         transport->negotiate.protocol = PROTOCOL_NT1;
59         transport->options.use_spnego = lp_use_spnego();
60         transport->negotiate.max_xmit = ~0;
61         
62         cli_init_signing(transport);
63
64         transport->socket->reference_count++;
65
66         ZERO_STRUCT(transport->called);
67
68         fde.fd = sock->fd;
69         fde.flags = EVENT_FD_READ;
70         fde.handler = cli_transport_event_handler;
71         fde.private = transport;
72         fde.ref_count = 1;
73
74         transport->event.fde = event_add_fd(transport->event.ctx, &fde);
75
76         return transport;
77 }
78
79 /*
80   decrease reference count on a transport, and destroy if it becomes
81   zero
82 */
83 void cli_transport_close(struct cli_transport *transport)
84 {
85         transport->reference_count--;
86         if (transport->reference_count <= 0) {
87                 cli_sock_close(transport->socket);
88                 event_remove_fd(transport->event.ctx, transport->event.fde);
89                 event_remove_timed(transport->event.ctx, transport->event.te);
90                 event_context_destroy(transport->event.ctx);
91                 talloc_destroy(transport->mem_ctx);
92         }
93 }
94
95 /*
96   mark the transport as dead
97 */
98 void cli_transport_dead(struct cli_transport *transport)
99 {
100         cli_sock_dead(transport->socket);
101 }
102
103
104 /*
105   enable select for write on a transport
106 */
107 static void cli_transport_write_enable(struct cli_transport *transport)
108 {
109         transport->event.fde->flags |= EVENT_FD_WRITE;
110 }
111
112 /*
113   disable select for write on a transport
114 */
115 static void cli_transport_write_disable(struct cli_transport *transport)
116 {
117         transport->event.fde->flags &= ~EVENT_FD_WRITE;
118 }
119
120 /****************************************************************************
121 send a session request (if appropriate)
122 ****************************************************************************/
123 BOOL cli_transport_connect(struct cli_transport *transport,
124                            struct nmb_name *calling, 
125                            struct nmb_name *called)
126 {
127         char *p;
128         int len = NBT_HDR_SIZE;
129         struct cli_request *req;
130
131         if (called) {
132                 transport->called = *called;
133         }
134
135         /* 445 doesn't have session request */
136         if (transport->socket->port == 445) {
137                 return True;
138         }
139
140         /* allocate output buffer */
141         req = cli_request_setup_nonsmb(transport, NBT_HDR_SIZE + 2*nbt_mangled_name_len());
142
143         /* put in the destination name */
144         p = req->out.buffer + NBT_HDR_SIZE;
145         name_mangle(called->name, p, called->name_type);
146         len += name_len(p);
147
148         /* and my name */
149         p = req->out.buffer+len;
150         name_mangle(calling->name, p, calling->name_type);
151         len += name_len(p);
152
153         _smb_setlen(req->out.buffer,len-4);
154         SCVAL(req->out.buffer,0,0x81);
155
156         if (!cli_request_send(req) ||
157             !cli_request_receive(req)) {
158                 cli_request_destroy(req);
159                 return False;
160         }
161         
162         if (CVAL(req->in.buffer,0) != 0x82) {
163                 transport->error.etype = ETYPE_NBT;
164                 transport->error.e.nbt_error = CVAL(req->in.buffer,4);
165                 cli_request_destroy(req);
166                 return False;
167         }
168
169         cli_request_destroy(req);
170         return True;
171 }
172
173
174 /****************************************************************************
175 get next mid in sequence
176 ****************************************************************************/
177 uint16_t cli_transport_next_mid(struct cli_transport *transport)
178 {
179         uint16_t mid;
180         struct cli_request *req;
181
182         mid = transport->next_mid;
183
184 again:
185         /* now check to see if this mid is being used by one of the 
186            pending requests. This is quite efficient because the list is
187            usually very short */
188
189         /* the zero mid is reserved for requests that don't have a mid */
190         if (mid == 0) mid = 1;
191
192         for (req=transport->pending_recv; req; req=req->next) {
193                 if (req->mid == mid) {
194                         mid++;
195                         goto again;
196                 }
197         }
198
199         transport->next_mid = mid+1;
200         return mid;
201 }
202
203 static void idle_handler(struct event_context *ev, 
204                          struct timed_event *te, time_t t)
205 {
206         struct cli_transport *transport = te->private;
207         te->next_event = t + transport->idle.period;
208         transport->idle.func(transport, transport->idle.private);
209 }
210
211 /*
212   setup the idle handler for a transport
213   the period is in seconds
214 */
215 void cli_transport_idle_handler(struct cli_transport *transport, 
216                                 void (*idle_func)(struct cli_transport *, void *),
217                                 uint_t period,
218                                 void *private)
219 {
220         struct timed_event te;
221         transport->idle.func = idle_func;
222         transport->idle.private = private;
223         transport->idle.period = period;
224
225         if (transport->event.te != NULL) {
226                 event_remove_timed(transport->event.ctx, transport->event.te);
227         }
228
229         te.next_event = time(NULL) + period;
230         te.handler = idle_handler;
231         te.private = transport;
232         transport->event.te = event_add_timed(transport->event.ctx, &te);
233 }
234
235 /*
236   process some pending sends
237 */
238 static void cli_transport_process_send(struct cli_transport *transport)
239 {
240         while (transport->pending_send) {
241                 struct cli_request *req = transport->pending_send;
242                 ssize_t ret;
243                 ret = cli_sock_write(transport->socket, req->out.buffer, req->out.size);
244                 if (ret == -1) {
245                         if (errno == EAGAIN || errno == EINTR) {
246                                 return;
247                         }
248                         cli_transport_dead(transport);
249                 }
250                 req->out.buffer += ret;
251                 req->out.size -= ret;
252                 if (req->out.size == 0) {
253                         DLIST_REMOVE(transport->pending_send, req);
254                         if (req->one_way_request) {
255                                 req->state = CLI_REQUEST_DONE;
256                                 cli_request_destroy(req);
257                         } else {
258                                 req->state = CLI_REQUEST_RECV;
259                                 DLIST_ADD(transport->pending_recv, req);
260                         }
261                 }
262         }
263
264         /* we're out of requests to send, so don't wait for write
265            events any more */
266         cli_transport_write_disable(transport);
267 }
268
269 /*
270   we have a full request in our receive buffer - match it to a pending request
271   and process
272  */
273 static void cli_transport_finish_recv(struct cli_transport *transport)
274 {
275         uint8_t *buffer, *hdr, *vwv;
276         int len;
277         uint16_t wct, mid = 0;
278         struct cli_request *req;
279
280         buffer = transport->recv_buffer.buffer;
281         len = transport->recv_buffer.req_size;
282
283         ZERO_STRUCT(transport->recv_buffer);
284
285         hdr = buffer+NBT_HDR_SIZE;
286         vwv = hdr + HDR_VWV;
287
288         /* see if it could be an oplock break request */
289         if (handle_oplock_break(transport, len, hdr, vwv)) {
290                 talloc_free(transport->mem_ctx, buffer);
291                 return;
292         }
293
294         /* at this point we need to check for a readbraw reply, as
295            these can be any length */
296         if (transport->readbraw_pending) {
297                 transport->readbraw_pending = 0;
298
299                 /* it must match the first entry in the pending queue
300                    as the client is not allowed to have outstanding
301                    readbraw requests */
302                 req = transport->pending_recv;
303                 if (!req) goto error;
304
305                 req->in.buffer = buffer;
306                 talloc_steal(transport->mem_ctx, req->mem_ctx, buffer);
307                 req->in.size = len;
308                 req->in.allocated = req->in.size;
309                 goto async;
310         }
311
312         if (len >= MIN_SMB_SIZE) {
313                 /* extract the mid for matching to pending requests */
314                 mid = SVAL(hdr, HDR_MID);
315                 wct = CVAL(hdr, HDR_WCT);
316         }
317
318         /* match the incoming request against the list of pending requests */
319         for (req=transport->pending_recv; req; req=req->next) {
320                 if (req->mid == mid) break;
321         }
322
323         if (!req) {
324                 DEBUG(1,("Discarding unmatched reply with mid %d\n", mid));
325                 goto error;
326         }
327
328         /* fill in the 'in' portion of the matching request */
329         req->in.buffer = buffer;
330         talloc_steal(transport->mem_ctx, req->mem_ctx, buffer);
331         req->in.size = len;
332         req->in.allocated = req->in.size;
333
334         /* handle non-SMB replies */
335         if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE) {
336                 req->state = CLI_REQUEST_ERROR;
337                 goto error;
338         }
339
340         if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct)) {
341                 DEBUG(2,("bad reply size for mid %d\n", mid));
342                 req->status = NT_STATUS_UNSUCCESSFUL;
343                 req->state = CLI_REQUEST_ERROR;
344                 goto error;
345         }
346
347         req->in.hdr = hdr;
348         req->in.vwv = vwv;
349         req->in.wct = wct;
350         if (req->in.size >= NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct)) {
351                 req->in.data = req->in.vwv + VWV(wct) + 2;
352                 req->in.data_size = SVAL(req->in.vwv, VWV(wct));
353                 if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct) + req->in.data_size) {
354                         DEBUG(3,("bad data size for mid %d\n", mid));
355                         /* blergh - w2k3 gives a bogus data size values in some
356                            openX replies */
357                         req->in.data_size = req->in.size - (NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct));
358                 }
359         }
360         req->in.ptr = req->in.data;
361         req->flags2 = SVAL(req->in.hdr, HDR_FLG2);
362
363         if (!(req->flags2 & FLAGS2_32_BIT_ERROR_CODES)) {
364                 transport->error.etype = ETYPE_DOS;
365                 transport->error.e.dos.eclass = CVAL(req->in.hdr,HDR_RCLS);
366                 transport->error.e.dos.ecode = SVAL(req->in.hdr,HDR_ERR);
367                 req->status = dos_to_ntstatus(transport->error.e.dos.eclass, 
368                                               transport->error.e.dos.ecode);
369         } else {
370                 transport->error.etype = ETYPE_NT;
371                 transport->error.e.nt_status = NT_STATUS(IVAL(req->in.hdr, HDR_RCLS));
372                 req->status = transport->error.e.nt_status;
373         }
374
375         if (!cli_request_check_sign_mac(req)) {
376                 transport->error.etype = ETYPE_SOCKET;
377                 transport->error.e.socket_error = SOCKET_READ_BAD_SIG;
378                 req->state = CLI_REQUEST_ERROR;
379                 goto error;
380         };
381
382 async:
383         /* if this request has an async handler then call that to
384            notify that the reply has been received. This might destroy
385            the request so it must happen last */
386         DLIST_REMOVE(transport->pending_recv, req);
387         req->state = CLI_REQUEST_DONE;
388         if (req->async.fn) {
389                 req->async.fn(req);
390         }
391         return;
392
393 error:
394         if (req) {
395                 DLIST_REMOVE(transport->pending_recv, req);
396                 req->state = CLI_REQUEST_ERROR;
397         }
398 }
399
400 /*
401   process some pending receives
402 */
403 static void cli_transport_process_recv(struct cli_transport *transport)
404 {
405         /* a incoming packet goes through 2 stages - first we read the
406            4 byte header, which tells us how much more is coming. Then
407            we read the rest */
408         if (transport->recv_buffer.received < NBT_HDR_SIZE) {
409                 ssize_t ret;
410                 ret = cli_sock_read(transport->socket, 
411                                     transport->recv_buffer.header + 
412                                     transport->recv_buffer.received,
413                                     NBT_HDR_SIZE - transport->recv_buffer.received);
414                 if (ret == -1) {
415                         if (errno == EINTR || errno == EAGAIN) {
416                                 return;
417                         }
418                         cli_transport_dead(transport);
419                         return;
420                 }
421
422                 transport->recv_buffer.received += ret;
423
424                 if (transport->recv_buffer.received == NBT_HDR_SIZE) {
425                         /* we've got a full header */
426                         transport->recv_buffer.req_size = smb_len(transport->recv_buffer.header) + NBT_HDR_SIZE;
427                         transport->recv_buffer.buffer = talloc(transport->mem_ctx,
428                                                                NBT_HDR_SIZE+transport->recv_buffer.req_size);
429                         if (transport->recv_buffer.buffer == NULL) {
430                                 cli_transport_dead(transport);
431                                 return;
432                         }
433                         memcpy(transport->recv_buffer.buffer, transport->recv_buffer.header, NBT_HDR_SIZE);
434                 }
435         }
436
437         if (transport->recv_buffer.received < transport->recv_buffer.req_size) {
438                 ssize_t ret;
439                 ret = cli_sock_read(transport->socket, 
440                                     transport->recv_buffer.buffer + 
441                                     transport->recv_buffer.received,
442                                     transport->recv_buffer.req_size - 
443                                     transport->recv_buffer.received);
444                 if (ret == -1) {
445                         if (errno == EINTR || errno == EAGAIN) {
446                                 return;
447                         }
448                         cli_transport_dead(transport);
449                         return;
450                 }
451                 transport->recv_buffer.received += ret;
452         }
453
454         if (transport->recv_buffer.received != 0 &&
455             transport->recv_buffer.received == transport->recv_buffer.req_size) {
456                 cli_transport_finish_recv(transport);
457         }
458 }
459
460 /*
461   process some read/write requests that are pending
462   return False if the socket is dead
463 */
464 BOOL cli_transport_process(struct cli_transport *transport)
465 {
466         cli_transport_process_send(transport);
467         cli_transport_process_recv(transport);
468         if (transport->socket->fd == -1) {
469                 return False;
470         }
471         return True;
472 }
473
474
475
476 /*
477   put a request into the send queue
478 */
479 void cli_transport_send(struct cli_request *req)
480 {
481         /* put it on the outgoing socket queue */
482         req->state = CLI_REQUEST_SEND;
483         DLIST_ADD_END(req->transport->pending_send, req, struct cli_request *);
484
485         /* make sure we look for write events */
486         cli_transport_write_enable(req->transport);
487 }