r17020: pass the real error to the failing requests
[ira/wip.git] / source4 / libcli / smb2 / transport.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client transport context management functions
5
6    Copyright (C) Andrew Tridgell 2005
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 "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "lib/socket/socket.h"
28 #include "lib/events/events.h"
29 #include "lib/stream/packet.h"
30 #include "include/dlinklist.h"
31
32
33 /*
34   an event has happened on the socket
35 */
36 static void smb2_transport_event_handler(struct event_context *ev, 
37                                          struct fd_event *fde, 
38                                          uint16_t flags, void *private)
39 {
40         struct smb2_transport *transport = talloc_get_type(private,
41                                                            struct smb2_transport);
42         if (flags & EVENT_FD_READ) {
43                 packet_recv(transport->packet);
44                 return;
45         }
46         if (flags & EVENT_FD_WRITE) {
47                 packet_queue_run(transport->packet);
48         }
49 }
50
51 /*
52   destroy a transport
53  */
54 static int transport_destructor(struct smb2_transport *transport)
55 {
56         smb2_transport_dead(transport, NT_STATUS_LOCAL_DISCONNECT);
57         return 0;
58 }
59
60
61 /*
62   handle receive errors
63 */
64 static void smb2_transport_error(void *private, NTSTATUS status)
65 {
66         struct smb2_transport *transport = talloc_get_type(private, 
67                                                            struct smb2_transport);
68         smb2_transport_dead(transport, status);
69 }
70
71 static NTSTATUS smb2_transport_finish_recv(void *private, DATA_BLOB blob);
72
73 /*
74   create a transport structure based on an established socket
75 */
76 struct smb2_transport *smb2_transport_init(struct smbcli_socket *sock,
77                                            TALLOC_CTX *parent_ctx)
78 {
79         struct smb2_transport *transport;
80
81         transport = talloc_zero(parent_ctx, struct smb2_transport);
82         if (!transport) return NULL;
83
84         transport->socket = talloc_steal(transport, sock);
85
86         /* setup the stream -> packet parser */
87         transport->packet = packet_init(transport);
88         if (transport->packet == NULL) {
89                 talloc_free(transport);
90                 return NULL;
91         }
92         packet_set_private(transport->packet, transport);
93         packet_set_socket(transport->packet, transport->socket->sock);
94         packet_set_callback(transport->packet, smb2_transport_finish_recv);
95         packet_set_full_request(transport->packet, packet_full_request_nbt);
96         packet_set_error_handler(transport->packet, smb2_transport_error);
97         packet_set_event_context(transport->packet, transport->socket->event.ctx);
98         packet_set_nofree(transport->packet);
99
100         /* take over event handling from the socket layer - it only
101            handles events up until we are connected */
102         talloc_free(transport->socket->event.fde);
103         transport->socket->event.fde = event_add_fd(transport->socket->event.ctx,
104                                                     transport->socket,
105                                                     socket_get_fd(transport->socket->sock),
106                                                     EVENT_FD_READ,
107                                                     smb2_transport_event_handler,
108                                                     transport);
109
110         packet_set_fde(transport->packet, transport->socket->event.fde);
111         packet_set_serialise(transport->packet);
112
113         talloc_set_destructor(transport, transport_destructor);
114
115         transport->options.timeout = 30;
116
117         return transport;
118 }
119
120 /*
121   mark the transport as dead
122 */
123 void smb2_transport_dead(struct smb2_transport *transport, NTSTATUS status)
124 {
125         smbcli_sock_dead(transport->socket);
126
127         if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
128                 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
129         }
130
131         /* kill all pending receives */
132         while (transport->pending_recv) {
133                 struct smb2_request *req = transport->pending_recv;
134                 req->state = SMB2_REQUEST_ERROR;
135                 req->status = status;
136                 DLIST_REMOVE(transport->pending_recv, req);
137                 if (req->async.fn) {
138                         req->async.fn(req);
139                 }
140         }
141 }
142
143 /*
144   we have a full request in our receive buffer - match it to a pending request
145   and process
146  */
147 static NTSTATUS smb2_transport_finish_recv(void *private, DATA_BLOB blob)
148 {
149         struct smb2_transport *transport = talloc_get_type(private, 
150                                                              struct smb2_transport);
151         uint8_t *buffer, *hdr;
152         int len;
153         struct smb2_request *req = NULL;
154         uint64_t seqnum;
155         uint16_t buffer_code;
156         uint32_t dynamic_size;
157
158         buffer = blob.data;
159         len = blob.length;
160
161         hdr = buffer+NBT_HDR_SIZE;
162
163         if (len < SMB2_MIN_SIZE) {
164                 DEBUG(1,("Discarding smb2 reply of size %d\n", len));
165                 goto error;
166         }
167
168         seqnum = BVAL(hdr, SMB2_HDR_SEQNUM);
169
170         /* match the incoming request against the list of pending requests */
171         for (req=transport->pending_recv; req; req=req->next) {
172                 if (req->seqnum == seqnum) break;
173         }
174
175         if (!req) {
176                 DEBUG(1,("Discarding unmatched reply with seqnum 0x%llx op %d\n", 
177                          (long long)seqnum, SVAL(hdr, SMB2_HDR_OPCODE)));
178                 goto error;
179         }
180
181         /* fill in the 'in' portion of the matching request */
182         req->in.buffer = buffer;
183         talloc_steal(req, buffer);
184         req->in.size = len;
185         req->in.allocated = req->in.size;
186
187         req->in.hdr       = hdr;
188         req->in.body      = hdr+SMB2_HDR_BODY;
189         req->in.body_size = req->in.size - (SMB2_HDR_BODY+NBT_HDR_SIZE);
190         req->status       = NT_STATUS(IVAL(hdr, SMB2_HDR_STATUS));
191
192         if (NT_STATUS_EQUAL(req->status, STATUS_PENDING)) {
193                 /* the server has helpfully told us that this request is still being
194                    processed. how useful :) */
195                 talloc_free(buffer);
196                 return NT_STATUS_OK;
197         }
198
199         buffer_code = SVAL(req->in.body, 0);
200         req->in.body_fixed = (buffer_code & ~1);
201         req->in.dynamic = NULL;
202         dynamic_size = req->in.body_size - req->in.body_fixed;
203         if (dynamic_size != 0 && (buffer_code & 1)) {
204                 req->in.dynamic = req->in.body + req->in.body_fixed;
205                 if (smb2_oob(&req->in, req->in.dynamic, dynamic_size)) {
206                         DEBUG(1,("SMB2 request invalid dynamic size 0x%x\n", 
207                                  dynamic_size));
208                         goto error;
209                 }
210         }
211
212         DEBUG(2, ("SMB2 RECV seqnum=0x%llx\n", (long long)req->seqnum));
213         dump_data(5, req->in.body, req->in.body_size);
214
215         /* if this request has an async handler then call that to
216            notify that the reply has been received. This might destroy
217            the request so it must happen last */
218         DLIST_REMOVE(transport->pending_recv, req);
219         req->state = SMB2_REQUEST_DONE;
220         if (req->async.fn) {
221                 req->async.fn(req);
222         }
223         return NT_STATUS_OK;
224
225 error:
226         dump_data(5, buffer, len);
227         if (req) {
228                 DLIST_REMOVE(transport->pending_recv, req);
229                 req->state = SMB2_REQUEST_ERROR;
230                 if (req->async.fn) {
231                         req->async.fn(req);
232                 }
233         } else {
234                 talloc_free(buffer);
235         }
236         return NT_STATUS_UNSUCCESSFUL;
237 }
238
239 /*
240   handle timeouts of individual smb requests
241 */
242 static void smb2_timeout_handler(struct event_context *ev, struct timed_event *te, 
243                                  struct timeval t, void *private)
244 {
245         struct smb2_request *req = talloc_get_type(private, struct smb2_request);
246
247         if (req->state == SMB2_REQUEST_RECV) {
248                 DLIST_REMOVE(req->transport->pending_recv, req);
249         }
250         req->status = NT_STATUS_IO_TIMEOUT;
251         req->state = SMB2_REQUEST_ERROR;
252         if (req->async.fn) {
253                 req->async.fn(req);
254         }
255 }
256
257
258 /*
259   destroy a request
260 */
261 static int smb2_request_destructor(struct smb2_request *req)
262 {
263         if (req->state == SMB2_REQUEST_RECV) {
264                 DLIST_REMOVE(req->transport->pending_recv, req);
265         }
266         return 0;
267 }
268
269
270 /*
271   put a request into the send queue
272 */
273 void smb2_transport_send(struct smb2_request *req)
274 {
275         DATA_BLOB blob;
276         NTSTATUS status;
277
278         _smb2_setlen(req->out.buffer, req->out.size - NBT_HDR_SIZE);
279
280         DEBUG(2, ("SMB2 send seqnum=0x%llx\n", (long long)req->seqnum));
281         dump_data(5, req->out.body, req->out.body_size);
282
283         /* check if the transport is dead */
284         if (req->transport->socket->sock == NULL) {
285                 req->state = SMB2_REQUEST_ERROR;
286                 req->status = NT_STATUS_NET_WRITE_FAULT;
287                 return;
288         }
289
290         blob = data_blob_const(req->out.buffer, req->out.size);
291         status = packet_send(req->transport->packet, blob);
292         if (!NT_STATUS_IS_OK(status)) {
293                 req->state = SMB2_REQUEST_ERROR;
294                 req->status = status;
295                 return;
296         }
297
298         req->state = SMB2_REQUEST_RECV;
299         DLIST_ADD(req->transport->pending_recv, req);
300
301         /* add a timeout */
302         if (req->transport->options.timeout) {
303                 event_add_timed(req->transport->socket->event.ctx, req, 
304                                 timeval_current_ofs(req->transport->options.timeout, 0), 
305                                 smb2_timeout_handler, req);
306         }
307
308         talloc_set_destructor(req, smb2_request_destructor);
309 }