Merge commit 'origin/v3-2-test' into v3-2-stable
[jra/samba/.git] / source3 / libsmb / async_smb.c
1 /*
2    Unix SMB/CIFS implementation.
3    Infrastructure for async SMB client requests
4    Copyright (C) Volker Lendecke 2008
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 /*
23  * Fetch an error out of a NBT packet
24  */
25
26 NTSTATUS cli_pull_error(char *buf)
27 {
28         uint32_t flags2 = SVAL(buf, smb_flg2);
29
30         if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
31                 return NT_STATUS(IVAL(buf, smb_rcls));
32         }
33
34         return NT_STATUS_DOS(CVAL(buf, smb_rcls), SVAL(buf,smb_err));
35 }
36
37 /*
38  * Compatibility helper for the sync APIs: Fake NTSTATUS in cli->inbuf
39  */
40
41 void cli_set_error(struct cli_state *cli, NTSTATUS status)
42 {
43         uint32_t flags2 = SVAL(cli->inbuf, smb_flg2);
44
45         if (NT_STATUS_IS_DOS(status)) {
46                 SSVAL(cli->inbuf, smb_flg2,
47                       flags2 & ~FLAGS2_32_BIT_ERROR_CODES);
48                 SCVAL(cli->inbuf, smb_rcls, NT_STATUS_DOS_CLASS(status));
49                 SSVAL(cli->inbuf, smb_err, NT_STATUS_DOS_CODE(status));
50                 return;
51         }
52
53         SSVAL(cli->inbuf, smb_flg2, flags2 | FLAGS2_32_BIT_ERROR_CODES);
54         SIVAL(cli->inbuf, smb_rcls, NT_STATUS_V(status));
55         return;
56 }
57
58 /*
59  * Allocate a new mid
60  */
61
62 static uint16_t cli_new_mid(struct cli_state *cli)
63 {
64         uint16_t result;
65         struct cli_request *req;
66
67         while (true) {
68                 result = cli->mid++;
69                 if (result == 0) {
70                         continue;
71                 }
72
73                 for (req = cli->outstanding_requests; req; req = req->next) {
74                         if (result == req->mid) {
75                                 break;
76                         }
77                 }
78
79                 if (req == NULL) {
80                         return result;
81                 }
82         }
83 }
84
85 static char *cli_request_print(TALLOC_CTX *mem_ctx, struct async_req *req)
86 {
87         char *result = async_req_print(mem_ctx, req);
88         struct cli_request *cli_req = cli_request_get(req);
89
90         if (result == NULL) {
91                 return NULL;
92         }
93
94         return talloc_asprintf_append_buffer(
95                 result, "mid=%d\n", cli_req->mid);
96 }
97
98 static int cli_request_destructor(struct cli_request *req)
99 {
100         if (req->enc_state != NULL) {
101                 common_free_enc_buffer(req->enc_state, req->outbuf);
102         }
103         DLIST_REMOVE(req->cli->outstanding_requests, req);
104         return 0;
105 }
106
107 /*
108  * Create a fresh async smb request
109  */
110
111 struct async_req *cli_request_new(TALLOC_CTX *mem_ctx,
112                                   struct event_context *ev,
113                                   struct cli_state *cli,
114                                   uint8_t num_words, size_t num_bytes,
115                                   struct cli_request **preq)
116 {
117         struct async_req *result;
118         struct cli_request *cli_req;
119         size_t bufsize = smb_size + num_words * 2 + num_bytes;
120
121         result = async_req_new(mem_ctx, ev);
122         if (result == NULL) {
123                 return NULL;
124         }
125
126         cli_req = (struct cli_request *)talloc_size(
127                 result, sizeof(*cli_req) + bufsize);
128         if (cli_req == NULL) {
129                 TALLOC_FREE(result);
130                 return NULL;
131         }
132         talloc_set_name_const(cli_req, "struct cli_request");
133         result->private_data = cli_req;
134         result->print = cli_request_print;
135
136         cli_req->async = result;
137         cli_req->cli = cli;
138         cli_req->outbuf = ((char *)cli_req + sizeof(*cli_req));
139         cli_req->sent = 0;
140         cli_req->mid = cli_new_mid(cli);
141         cli_req->inbuf = NULL;
142         cli_req->enc_state = NULL;
143
144         SCVAL(cli_req->outbuf, smb_wct, num_words);
145         SSVAL(cli_req->outbuf, smb_vwv + num_words * 2, num_bytes);
146
147         DLIST_ADD_END(cli->outstanding_requests, cli_req,
148                       struct cli_request *);
149         talloc_set_destructor(cli_req, cli_request_destructor);
150
151         DEBUG(10, ("cli_request_new: mid=%d\n", cli_req->mid));
152
153         *preq = cli_req;
154         return result;
155 }
156
157 /*
158  * Convenience function to get the SMB part out of an async_req
159  */
160
161 struct cli_request *cli_request_get(struct async_req *req)
162 {
163         if (req == NULL) {
164                 return NULL;
165         }
166         return talloc_get_type_abort(req->private_data, struct cli_request);
167 }
168
169 /*
170  * A PDU has arrived on cli->evt_inbuf
171  */
172
173 static void handle_incoming_pdu(struct cli_state *cli)
174 {
175         struct cli_request *req;
176         uint16_t mid;
177         size_t raw_pdu_len, buf_len, pdu_len;
178         size_t rest_len;
179         NTSTATUS status;
180
181         /*
182          * The encrypted PDU len might differ from the unencrypted one
183          */
184         raw_pdu_len = smb_len(cli->evt_inbuf) + 4;
185
186         /*
187          * TODO: Handle oplock break requests
188          */
189
190         if (cli_encryption_on(cli) && CVAL(cli->evt_inbuf, 0) == 0) {
191                 uint16_t enc_ctx_num;
192
193                 status = get_enc_ctx_num((uint8_t *)cli->evt_inbuf,
194                                          &enc_ctx_num);
195                 if (!NT_STATUS_IS_OK(status)) {
196                         DEBUG(10, ("get_enc_ctx_num returned %s\n",
197                                    nt_errstr(status)));
198                         goto invalidate_requests;
199                 }
200
201                 if (enc_ctx_num != cli->trans_enc_state->enc_ctx_num) {
202                         DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
203                                    enc_ctx_num,
204                                    cli->trans_enc_state->enc_ctx_num));
205                         status = NT_STATUS_INVALID_HANDLE;
206                         goto invalidate_requests;
207                 }
208
209                 status = common_decrypt_buffer(cli->trans_enc_state,
210                                                cli->evt_inbuf);
211                 if (!NT_STATUS_IS_OK(status)) {
212                         DEBUG(10, ("common_decrypt_buffer returned %s\n",
213                                    nt_errstr(status)));
214                         goto invalidate_requests;
215                 }
216         }
217
218         if (!cli_check_sign_mac(cli, cli->evt_inbuf)) {
219                 DEBUG(10, ("cli_check_sign_mac failed\n"));
220                 status = NT_STATUS_ACCESS_DENIED;
221                 goto invalidate_requests;
222         }
223
224         mid = SVAL(cli->evt_inbuf, smb_mid);
225
226         DEBUG(10, ("handle_incoming_pdu: got mid %d\n", mid));
227
228         for (req = cli->outstanding_requests; req; req = req->next) {
229                 if (req->mid == mid) {
230                         break;
231                 }
232         }
233
234         buf_len = talloc_get_size(cli->evt_inbuf);
235         pdu_len = smb_len(cli->evt_inbuf) + 4;
236         rest_len = buf_len - raw_pdu_len;
237
238         if (req == NULL) {
239                 DEBUG(3, ("Request for mid %d not found, dumping PDU\n", mid));
240
241                 memmove(cli->evt_inbuf, cli->evt_inbuf + raw_pdu_len,
242                         buf_len - raw_pdu_len);
243
244                 cli->evt_inbuf = TALLOC_REALLOC_ARRAY(NULL, cli->evt_inbuf,
245                                                       char, rest_len);
246                 return;
247         }
248
249         if (buf_len == pdu_len) {
250                 /*
251                  * Optimal case: Exactly one PDU was in the socket buffer
252                  */
253                 req->inbuf = talloc_move(req, &cli->evt_inbuf);
254                 goto done;
255         }
256
257         DEBUG(11, ("buf_len = %d, pdu_len = %d, splitting buffer\n",
258                    (int)buf_len, (int)pdu_len));
259
260         if (pdu_len < rest_len) {
261                 /*
262                  * The PDU is shorter, talloc_memdup that one.
263                  */
264                 req->inbuf = (char *)talloc_memdup(
265                         req, cli->evt_inbuf, pdu_len);
266
267                 memmove(cli->evt_inbuf,
268                         cli->evt_inbuf + raw_pdu_len,
269                         buf_len - raw_pdu_len);
270
271                 cli->evt_inbuf = TALLOC_REALLOC_ARRAY(
272                         NULL, cli->evt_inbuf, char, rest_len);
273         }
274         else {
275                 /*
276                  * The PDU is larger than the rest,
277                  * talloc_memdup the rest
278                  */
279                 req->inbuf = talloc_move(req, &cli->evt_inbuf);
280
281                 cli->evt_inbuf = (char *)talloc_memdup(
282                         cli, req->inbuf + raw_pdu_len,
283                         rest_len);
284         }
285
286         if ((req->inbuf == NULL) || (cli->evt_inbuf == NULL)) {
287                 status = NT_STATUS_NO_MEMORY;
288                 goto invalidate_requests;
289         }
290
291  done:
292         async_req_done(req->async);
293         return;
294
295  invalidate_requests:
296
297         DEBUG(10, ("handle_incoming_pdu: Aborting with %s\n",
298                    nt_errstr(status)));
299
300         for (req = cli->outstanding_requests; req; req = req->next) {
301                 async_req_error(req->async, status);
302         }
303         return;
304 }
305
306 /*
307  * fd event callback. This is the basic connection to the socket
308  */
309
310 static void cli_state_handler(struct event_context *event_ctx,
311                               struct fd_event *event, uint16 flags, void *p)
312 {
313         struct cli_state *cli = (struct cli_state *)p;
314         struct cli_request *req;
315
316         DEBUG(11, ("cli_state_handler called with flags %d\n", flags));
317
318         if (flags & EVENT_FD_READ) {
319                 int res, available;
320                 size_t old_size, new_size;
321                 char *tmp;
322
323                 res = ioctl(cli->fd, FIONREAD, &available);
324                 if (res == -1) {
325                         DEBUG(10, ("ioctl(FIONREAD) failed: %s\n",
326                                    strerror(errno)));
327                         goto sock_error;
328                 }
329
330                 if (available == 0) {
331                         /* EOF */
332                         goto sock_error;
333                 }
334
335                 old_size = talloc_get_size(cli->evt_inbuf);
336                 new_size = old_size + available;
337
338                 if (new_size < old_size) {
339                         /* wrap */
340                         goto sock_error;
341                 }
342
343                 tmp = TALLOC_REALLOC_ARRAY(cli, cli->evt_inbuf, char,
344                                            new_size);
345                 if (tmp == NULL) {
346                         /* nomem */
347                         goto sock_error;
348                 }
349                 cli->evt_inbuf = tmp;
350
351                 res = recv(cli->fd, cli->evt_inbuf + old_size, available, 0);
352                 if (res == -1) {
353                         DEBUG(10, ("recv failed: %s\n", strerror(errno)));
354                         goto sock_error;
355                 }
356
357                 DEBUG(11, ("cli_state_handler: received %d bytes, "
358                            "smb_len(evt_inbuf) = %d\n", (int)res,
359                            smb_len(cli->evt_inbuf)));
360
361                 /* recv *might* have returned less than announced */
362                 new_size = old_size + res;
363
364                 /* shrink, so I don't expect errors here */
365                 cli->evt_inbuf = TALLOC_REALLOC_ARRAY(cli, cli->evt_inbuf,
366                                                       char, new_size);
367
368                 while ((cli->evt_inbuf != NULL)
369                        && ((smb_len(cli->evt_inbuf) + 4) <= new_size)) {
370                         /*
371                          * we've got a complete NBT level PDU in evt_inbuf
372                          */
373                         handle_incoming_pdu(cli);
374                         new_size = talloc_get_size(cli->evt_inbuf);
375                 }
376         }
377
378         if (flags & EVENT_FD_WRITE) {
379                 size_t to_send;
380                 ssize_t sent;
381
382                 for (req = cli->outstanding_requests; req; req = req->next) {
383                         to_send = smb_len(req->outbuf)+4;
384                         if (to_send > req->sent) {
385                                 break;
386                         }
387                 }
388
389                 if (req == NULL) {
390                         event_fd_set_not_writeable(event);
391                         return;
392                 }
393
394                 sent = send(cli->fd, req->outbuf + req->sent,
395                             to_send - req->sent, 0);
396
397                 if (sent < 0) {
398                         goto sock_error;
399                 }
400
401                 req->sent += sent;
402
403                 if (req->sent == to_send) {
404                         return;
405                 }
406         }
407         return;
408
409  sock_error:
410         for (req = cli->outstanding_requests; req; req = req->next) {
411                 req->async->state = ASYNC_REQ_ERROR;
412                 req->async->status = map_nt_error_from_unix(errno);
413         }
414         TALLOC_FREE(cli->fd_event);
415         close(cli->fd);
416         cli->fd = -1;
417 }
418
419 /*
420  * Holder for a talloc_destructor, we need to zero out the pointers in cli
421  * when deleting
422  */
423 struct cli_tmp_event {
424         struct cli_state *cli;
425 };
426
427 static int cli_tmp_event_destructor(struct cli_tmp_event *e)
428 {
429         TALLOC_FREE(e->cli->fd_event);
430         TALLOC_FREE(e->cli->event_ctx);
431         return 0;
432 }
433
434 /*
435  * Create a temporary event context for use in the sync helper functions
436  */
437
438 struct cli_tmp_event *cli_tmp_event_ctx(TALLOC_CTX *mem_ctx,
439                                         struct cli_state *cli)
440 {
441         struct cli_tmp_event *state;
442
443         if (cli->event_ctx != NULL) {
444                 return NULL;
445         }
446
447         state = talloc(mem_ctx, struct cli_tmp_event);
448         if (state == NULL) {
449                 return NULL;
450         }
451         state->cli = cli;
452         talloc_set_destructor(state, cli_tmp_event_destructor);
453
454         cli->event_ctx = event_context_init(state);
455         if (cli->event_ctx == NULL) {
456                 TALLOC_FREE(state);
457                 return NULL;
458         }
459
460         cli->fd_event = event_add_fd(cli->event_ctx, state, cli->fd,
461                                      EVENT_FD_READ, cli_state_handler, cli);
462         if (cli->fd_event == NULL) {
463                 TALLOC_FREE(state);
464                 return NULL;
465         }
466         return state;
467 }
468
469 /*
470  * Attach an event context permanently to a cli_struct
471  */
472
473 NTSTATUS cli_add_event_ctx(struct cli_state *cli,
474                            struct event_context *event_ctx)
475 {
476         cli->event_ctx = event_ctx;
477         cli->fd_event = event_add_fd(event_ctx, cli, cli->fd, EVENT_FD_READ,
478                                      cli_state_handler, cli);
479         if (cli->fd_event == NULL) {
480                 return NT_STATUS_NO_MEMORY;
481         }
482         return NT_STATUS_OK;
483 }