efeb328dd93103f2d470396afdf70389ca71d29c
[tridge/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 #include "libsmb/libsmb.h"
22 #include "../lib/async_req/async_sock.h"
23 #include "../lib/util/tevent_ntstatus.h"
24 #include "../lib/util/tevent_unix.h"
25 #include "async_smb.h"
26 #include "smb_crypt.h"
27 #include "libsmb/nmblib.h"
28 #include "read_smb.h"
29
30 static NTSTATUS cli_pull_raw_error(const uint8_t *buf)
31 {
32         uint32_t flags2 = SVAL(buf, smb_flg2);
33         NTSTATUS status = NT_STATUS(IVAL(buf, smb_rcls));
34
35         if (NT_STATUS_IS_OK(status)) {
36                 return NT_STATUS_OK;
37         }
38
39         if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
40                 return status;
41         }
42
43         return NT_STATUS_DOS(CVAL(buf, smb_rcls), SVAL(buf,smb_err));
44 }
45
46 /**
47  * Figure out if there is an andx command behind the current one
48  * @param[in] buf       The smb buffer to look at
49  * @param[in] ofs       The offset to the wct field that is followed by the cmd
50  * @retval Is there a command following?
51  */
52
53 static bool have_andx_command(const char *buf, uint16_t ofs)
54 {
55         uint8_t wct;
56         size_t buflen = talloc_get_size(buf);
57
58         if ((ofs == buflen-1) || (ofs == buflen)) {
59                 return false;
60         }
61
62         wct = CVAL(buf, ofs);
63         if (wct < 2) {
64                 /*
65                  * Not enough space for the command and a following pointer
66                  */
67                 return false;
68         }
69         return (CVAL(buf, ofs+1) != 0xff);
70 }
71
72 #define MAX_SMB_IOV 5
73
74 struct cli_smb_state {
75         struct tevent_context *ev;
76         struct cli_state *cli;
77         uint8_t header[smb_wct+1]; /* Space for the header including the wct */
78
79         /*
80          * For normal requests, cli_smb_req_send chooses a mid. Secondary
81          * trans requests need to use the mid of the primary request, so we
82          * need a place to store it. Assume it's set if != 0.
83          */
84         uint16_t mid;
85
86         uint16_t *vwv;
87         uint8_t bytecount_buf[2];
88
89         struct iovec iov[MAX_SMB_IOV+3];
90         int iov_count;
91
92         uint8_t *inbuf;
93         uint32_t seqnum;
94         int chain_num;
95         int chain_length;
96         struct tevent_req **chained_requests;
97
98         bool one_way;
99 };
100
101 static uint16_t cli_alloc_mid(struct cli_state *cli)
102 {
103         int num_pending = talloc_array_length(cli->conn.pending);
104         uint16_t result;
105
106         while (true) {
107                 int i;
108
109                 result = cli->conn.smb1.mid++;
110                 if ((result == 0) || (result == 0xffff)) {
111                         continue;
112                 }
113
114                 for (i=0; i<num_pending; i++) {
115                         if (result == cli_smb_req_mid(cli->conn.pending[i])) {
116                                 break;
117                         }
118                 }
119
120                 if (i == num_pending) {
121                         return result;
122                 }
123         }
124 }
125
126 void cli_smb_req_unset_pending(struct tevent_req *req)
127 {
128         struct cli_smb_state *state = tevent_req_data(
129                 req, struct cli_smb_state);
130         struct cli_state *cli = state->cli;
131         int num_pending = talloc_array_length(cli->conn.pending);
132         int i;
133
134         if (state->mid != 0) {
135                 /*
136                  * This is a [nt]trans[2] request which waits
137                  * for more than one reply.
138                  */
139                 return;
140         }
141
142         talloc_set_destructor(req, NULL);
143
144         if (num_pending == 1) {
145                 /*
146                  * The pending read_smb tevent_req is a child of
147                  * cli->pending. So if nothing is pending anymore, we need to
148                  * delete the socket read fde.
149                  */
150                 TALLOC_FREE(cli->conn.pending);
151                 cli->conn.read_smb_req = NULL;
152                 return;
153         }
154
155         for (i=0; i<num_pending; i++) {
156                 if (req == cli->conn.pending[i]) {
157                         break;
158                 }
159         }
160         if (i == num_pending) {
161                 /*
162                  * Something's seriously broken. Just returning here is the
163                  * right thing nevertheless, the point of this routine is to
164                  * remove ourselves from cli->conn.pending.
165                  */
166                 return;
167         }
168
169         /*
170          * Remove ourselves from the cli->conn.pending array
171          */
172         for (; i < (num_pending - 1); i++) {
173                 cli->conn.pending[i] = cli->conn.pending[i+1];
174         }
175
176         /*
177          * No NULL check here, we're shrinking by sizeof(void *), and
178          * talloc_realloc just adjusts the size for this.
179          */
180         cli->conn.pending = talloc_realloc(NULL, cli->conn.pending,
181                                            struct tevent_req *,
182                                            num_pending - 1);
183         return;
184 }
185
186 static int cli_smb_req_destructor(struct tevent_req *req)
187 {
188         struct cli_smb_state *state = tevent_req_data(
189                 req, struct cli_smb_state);
190         /*
191          * Make sure we really remove it from
192          * the pending array on destruction.
193          */
194         state->mid = 0;
195         cli_smb_req_unset_pending(req);
196         return 0;
197 }
198
199 static bool cli_state_receive_next(struct cli_state *cli);
200 static void cli_state_notify_pending(struct cli_state *cli, NTSTATUS status);
201
202 bool cli_smb_req_set_pending(struct tevent_req *req)
203 {
204         struct cli_smb_state *state = tevent_req_data(
205                 req, struct cli_smb_state);
206         struct cli_state *cli;
207         struct tevent_req **pending;
208         int num_pending;
209
210         cli = state->cli;
211         num_pending = talloc_array_length(cli->conn.pending);
212
213         pending = talloc_realloc(cli, cli->conn.pending, struct tevent_req *,
214                                  num_pending+1);
215         if (pending == NULL) {
216                 return false;
217         }
218         pending[num_pending] = req;
219         cli->conn.pending = pending;
220         talloc_set_destructor(req, cli_smb_req_destructor);
221
222         if (!cli_state_receive_next(cli)) {
223                 /*
224                  * the caller should notify the current request
225                  *
226                  * And all other pending requests get notified
227                  * by cli_state_notify_pending().
228                  */
229                 cli_smb_req_unset_pending(req);
230                 cli_state_notify_pending(cli, NT_STATUS_NO_MEMORY);
231                 return false;
232         }
233
234         return true;
235 }
236
237 static void cli_smb_received(struct tevent_req *subreq);
238 static NTSTATUS cli_state_dispatch_smb1(struct cli_state *cli,
239                                         TALLOC_CTX *frame,
240                                         uint8_t *inbuf);
241
242 static bool cli_state_receive_next(struct cli_state *cli)
243 {
244         size_t num_pending = talloc_array_length(cli->conn.pending);
245         struct tevent_req *req;
246         struct cli_smb_state *state;
247
248         if (cli->conn.read_smb_req != NULL) {
249                 return true;
250         }
251
252         if (num_pending == 0) {
253                 return true;
254         }
255
256         req = cli->conn.pending[0];
257         state = tevent_req_data(req, struct cli_smb_state);
258
259         cli->conn.dispatch_incoming = cli_state_dispatch_smb1;
260
261         /*
262          * We're the first ones, add the read_smb request that waits for the
263          * answer from the server
264          */
265         cli->conn.read_smb_req = read_smb_send(cli->conn.pending, state->ev,
266                                                cli->conn.fd);
267         if (cli->conn.read_smb_req == NULL) {
268                 return false;
269         }
270         tevent_req_set_callback(cli->conn.read_smb_req, cli_smb_received, cli);
271         return true;
272 }
273
274 static void cli_state_notify_pending(struct cli_state *cli, NTSTATUS status)
275 {
276         cli_state_disconnect(cli);
277
278         /*
279          * Cancel all pending requests. We do not do a for-loop walking
280          * cli->conn.pending because that array changes in
281          * cli_smb_req_destructor().
282          */
283         while (talloc_array_length(cli->conn.pending) > 0) {
284                 struct tevent_req *req;
285                 struct cli_smb_state *state;
286
287                 req = cli->conn.pending[0];
288                 state = tevent_req_data(req, struct cli_smb_state);
289
290                 cli_smb_req_unset_pending(req);
291
292                 /*
293                  * we need to defer the callback, because we may notify more
294                  * then one caller.
295                  */
296                 tevent_req_defer_callback(req, state->ev);
297                 tevent_req_nterror(req, status);
298         }
299 }
300
301 /*
302  * Fetch a smb request's mid. Only valid after the request has been sent by
303  * cli_smb_req_send().
304  */
305 uint16_t cli_smb_req_mid(struct tevent_req *req)
306 {
307         struct cli_smb_state *state = tevent_req_data(
308                 req, struct cli_smb_state);
309
310         if (state->mid != 0) {
311                 return state->mid;
312         }
313
314         return SVAL(state->header, smb_mid);
315 }
316
317 void cli_smb_req_set_mid(struct tevent_req *req, uint16_t mid)
318 {
319         struct cli_smb_state *state = tevent_req_data(
320                 req, struct cli_smb_state);
321         state->mid = mid;
322 }
323
324 uint32_t cli_smb_req_seqnum(struct tevent_req *req)
325 {
326         struct cli_smb_state *state = tevent_req_data(
327                 req, struct cli_smb_state);
328         return state->seqnum;
329 }
330
331 void cli_smb_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
332 {
333         struct cli_smb_state *state = tevent_req_data(
334                 req, struct cli_smb_state);
335         state->seqnum = seqnum;
336 }
337
338 static size_t iov_len(const struct iovec *iov, int count)
339 {
340         size_t result = 0;
341         int i;
342         for (i=0; i<count; i++) {
343                 result += iov[i].iov_len;
344         }
345         return result;
346 }
347
348 static uint8_t *iov_concat(TALLOC_CTX *mem_ctx, const struct iovec *iov,
349                            int count)
350 {
351         size_t len = iov_len(iov, count);
352         size_t copied;
353         uint8_t *buf;
354         int i;
355
356         buf = talloc_array(mem_ctx, uint8_t, len);
357         if (buf == NULL) {
358                 return NULL;
359         }
360         copied = 0;
361         for (i=0; i<count; i++) {
362                 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
363                 copied += iov[i].iov_len;
364         }
365         return buf;
366 }
367
368 struct tevent_req *cli_smb_req_create(TALLOC_CTX *mem_ctx,
369                                       struct event_context *ev,
370                                       struct cli_state *cli,
371                                       uint8_t smb_command,
372                                       uint8_t additional_flags,
373                                       uint8_t wct, uint16_t *vwv,
374                                       int iov_count,
375                                       struct iovec *bytes_iov)
376 {
377         struct tevent_req *result;
378         struct cli_smb_state *state;
379         struct timeval endtime;
380
381         if (iov_count > MAX_SMB_IOV) {
382                 /*
383                  * Should not happen :-)
384                  */
385                 return NULL;
386         }
387
388         result = tevent_req_create(mem_ctx, &state, struct cli_smb_state);
389         if (result == NULL) {
390                 return NULL;
391         }
392         state->ev = ev;
393         state->cli = cli;
394         state->mid = 0;         /* Set to auto-choose in cli_smb_req_send */
395         state->chain_num = 0;
396         state->chained_requests = NULL;
397
398         cli_setup_packet_buf(cli, (char *)state->header);
399         SCVAL(state->header, smb_com, smb_command);
400         SSVAL(state->header, smb_tid, cli->smb1.tid);
401         SCVAL(state->header, smb_wct, wct);
402
403         state->vwv = vwv;
404
405         SSVAL(state->bytecount_buf, 0, iov_len(bytes_iov, iov_count));
406
407         state->iov[0].iov_base = (void *)state->header;
408         state->iov[0].iov_len  = sizeof(state->header);
409         state->iov[1].iov_base = (void *)state->vwv;
410         state->iov[1].iov_len  = wct * sizeof(uint16_t);
411         state->iov[2].iov_base = (void *)state->bytecount_buf;
412         state->iov[2].iov_len  = sizeof(uint16_t);
413
414         if (iov_count != 0) {
415                 memcpy(&state->iov[3], bytes_iov,
416                        iov_count * sizeof(*bytes_iov));
417         }
418         state->iov_count = iov_count + 3;
419
420         if (cli->timeout) {
421                 endtime = timeval_current_ofs_msec(cli->timeout);
422                 if (!tevent_req_set_endtime(result, ev, endtime)) {
423                         return result;
424                 }
425         }
426
427         switch (smb_command) {
428         case SMBtranss:
429         case SMBtranss2:
430         case SMBnttranss:
431         case SMBntcancel:
432                 state->one_way = true;
433                 break;
434         case SMBlockingX:
435                 if ((wct == 8) &&
436                     (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
437                         state->one_way = true;
438                 }
439                 break;
440         }
441
442         return result;
443 }
444
445 static NTSTATUS cli_signv(struct cli_state *cli, struct iovec *iov, int count,
446                           uint32_t *seqnum)
447 {
448         uint8_t *buf;
449
450         /*
451          * Obvious optimization: Make cli_calculate_sign_mac work with struct
452          * iovec directly. MD5Update would do that just fine.
453          */
454
455         if ((count <= 0) || (iov[0].iov_len < smb_wct)) {
456                 return NT_STATUS_INVALID_PARAMETER;
457         }
458
459         buf = iov_concat(talloc_tos(), iov, count);
460         if (buf == NULL) {
461                 return NT_STATUS_NO_MEMORY;
462         }
463
464         cli_calculate_sign_mac(cli, (char *)buf, seqnum);
465         memcpy(iov[0].iov_base, buf, iov[0].iov_len);
466
467         TALLOC_FREE(buf);
468         return NT_STATUS_OK;
469 }
470
471 static void cli_smb_sent(struct tevent_req *subreq);
472
473 static NTSTATUS cli_smb_req_iov_send(struct tevent_req *req,
474                                      struct cli_smb_state *state,
475                                      struct iovec *iov, int iov_count)
476 {
477         struct tevent_req *subreq;
478         NTSTATUS status;
479
480         if (!cli_state_is_connected(state->cli)) {
481                 return NT_STATUS_CONNECTION_DISCONNECTED;
482         }
483
484         if (iov[0].iov_len < smb_wct) {
485                 return NT_STATUS_INVALID_PARAMETER;
486         }
487
488         if (state->mid != 0) {
489                 SSVAL(iov[0].iov_base, smb_mid, state->mid);
490         } else {
491                 uint16_t mid = cli_alloc_mid(state->cli);
492                 SSVAL(iov[0].iov_base, smb_mid, mid);
493         }
494
495         smb_setlen((char *)iov[0].iov_base, iov_len(iov, iov_count) - 4);
496
497         status = cli_signv(state->cli, iov, iov_count, &state->seqnum);
498
499         if (!NT_STATUS_IS_OK(status)) {
500                 return status;
501         }
502
503         if (cli_state_encryption_on(state->cli)) {
504                 char *buf, *enc_buf;
505
506                 buf = (char *)iov_concat(talloc_tos(), iov, iov_count);
507                 if (buf == NULL) {
508                         return NT_STATUS_NO_MEMORY;
509                 }
510                 status = common_encrypt_buffer(state->cli->trans_enc_state,
511                                                (char *)buf, &enc_buf);
512                 TALLOC_FREE(buf);
513                 if (!NT_STATUS_IS_OK(status)) {
514                         DEBUG(0, ("Error in encrypting client message: %s\n",
515                                   nt_errstr(status)));
516                         return status;
517                 }
518                 buf = (char *)talloc_memdup(state, enc_buf,
519                                             smb_len(enc_buf)+4);
520                 SAFE_FREE(enc_buf);
521                 if (buf == NULL) {
522                         return NT_STATUS_NO_MEMORY;
523                 }
524                 iov[0].iov_base = (void *)buf;
525                 iov[0].iov_len = talloc_get_size(buf);
526                 iov_count = 1;
527         }
528         subreq = writev_send(state, state->ev, state->cli->conn.outgoing,
529                              state->cli->conn.fd, false, iov, iov_count);
530         if (subreq == NULL) {
531                 return NT_STATUS_NO_MEMORY;
532         }
533         tevent_req_set_callback(subreq, cli_smb_sent, req);
534         return NT_STATUS_OK;
535 }
536
537 NTSTATUS cli_smb_req_send(struct tevent_req *req)
538 {
539         struct cli_smb_state *state = tevent_req_data(
540                 req, struct cli_smb_state);
541
542         if (!tevent_req_is_in_progress(req)) {
543                 return NT_STATUS_INTERNAL_ERROR;
544         }
545
546         return cli_smb_req_iov_send(req, state, state->iov, state->iov_count);
547 }
548
549 struct tevent_req *cli_smb_send(TALLOC_CTX *mem_ctx,
550                                 struct event_context *ev,
551                                 struct cli_state *cli,
552                                 uint8_t smb_command,
553                                 uint8_t additional_flags,
554                                 uint8_t wct, uint16_t *vwv,
555                                 uint32_t num_bytes,
556                                 const uint8_t *bytes)
557 {
558         struct tevent_req *req;
559         struct iovec iov;
560         NTSTATUS status;
561
562         iov.iov_base = discard_const_p(void, bytes);
563         iov.iov_len = num_bytes;
564
565         req = cli_smb_req_create(mem_ctx, ev, cli, smb_command,
566                                  additional_flags, wct, vwv, 1, &iov);
567         if (req == NULL) {
568                 return NULL;
569         }
570         if (!tevent_req_is_in_progress(req)) {
571                 return tevent_req_post(req, ev);
572         }
573         status = cli_smb_req_send(req);
574         if (!NT_STATUS_IS_OK(status)) {
575                 tevent_req_nterror(req, status);
576                 return tevent_req_post(req, ev);
577         }
578         return req;
579 }
580
581 static void cli_smb_sent(struct tevent_req *subreq)
582 {
583         struct tevent_req *req = tevent_req_callback_data(
584                 subreq, struct tevent_req);
585         struct cli_smb_state *state = tevent_req_data(
586                 req, struct cli_smb_state);
587         ssize_t nwritten;
588         int err;
589
590         nwritten = writev_recv(subreq, &err);
591         TALLOC_FREE(subreq);
592         if (nwritten == -1) {
593                 NTSTATUS status = map_nt_error_from_unix(err);
594                 cli_state_notify_pending(state->cli, status);
595                 return;
596         }
597
598         if (state->one_way) {
599                 state->inbuf = NULL;
600                 tevent_req_done(req);
601                 return;
602         }
603
604         if (!cli_smb_req_set_pending(req)) {
605                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
606                 return;
607         }
608 }
609
610 static void cli_smb_received(struct tevent_req *subreq)
611 {
612         struct cli_state *cli = tevent_req_callback_data(
613                 subreq, struct cli_state);
614         TALLOC_CTX *frame = talloc_stackframe();
615         NTSTATUS status;
616         uint8_t *inbuf;
617         ssize_t received;
618         int err;
619
620         if (subreq != cli->conn.read_smb_req) {
621                 DEBUG(1, ("Internal error: cli_smb_received called with "
622                           "unexpected subreq\n"));
623                 status = NT_STATUS_INTERNAL_ERROR;
624                 cli_state_notify_pending(cli, status);
625                 TALLOC_FREE(frame);
626                 return;
627         }
628
629         received = read_smb_recv(subreq, frame, &inbuf, &err);
630         TALLOC_FREE(subreq);
631         cli->conn.read_smb_req = NULL;
632         if (received == -1) {
633                 status = map_nt_error_from_unix(err);
634                 cli_state_notify_pending(cli, status);
635                 TALLOC_FREE(frame);
636                 return;
637         }
638
639         status = cli->conn.dispatch_incoming(cli, frame, inbuf);
640         TALLOC_FREE(frame);
641         if (NT_STATUS_IS_OK(status)) {
642                 /*
643                  * We should not do any more processing
644                  * as the dispatch function called
645                  * tevent_req_done().
646                  */
647                 return;
648         } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
649                 /*
650                  * We got an error, so notify all pending requests
651                  */
652                 cli_state_notify_pending(cli, status);
653                 return;
654         }
655
656         /*
657          * We got NT_STATUS_RETRY, so we may ask for a
658          * next incoming pdu.
659          */
660         if (!cli_state_receive_next(cli)) {
661                 cli_state_notify_pending(cli, NT_STATUS_NO_MEMORY);
662         }
663 }
664
665 static NTSTATUS cli_state_dispatch_smb1(struct cli_state *cli,
666                                         TALLOC_CTX *frame,
667                                         uint8_t *inbuf)
668 {
669         struct tevent_req *req;
670         struct cli_smb_state *state;
671         NTSTATUS status;
672         int num_pending;
673         int i;
674         uint16_t mid;
675         bool oplock_break;
676
677         if ((IVAL(inbuf, 4) != 0x424d53ff) /* 0xFF"SMB" */
678             && (SVAL(inbuf, 4) != 0x45ff)) /* 0xFF"E" */ {
679                 DEBUG(10, ("Got non-SMB PDU\n"));
680                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
681         }
682
683         if (cli_state_encryption_on(cli) && (CVAL(inbuf, 0) == 0)) {
684                 uint16_t enc_ctx_num;
685
686                 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
687                 if (!NT_STATUS_IS_OK(status)) {
688                         DEBUG(10, ("get_enc_ctx_num returned %s\n",
689                                    nt_errstr(status)));
690                         return status;
691                 }
692
693                 if (enc_ctx_num != cli->trans_enc_state->enc_ctx_num) {
694                         DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
695                                    enc_ctx_num,
696                                    cli->trans_enc_state->enc_ctx_num));
697                         return NT_STATUS_INVALID_HANDLE;
698                 }
699
700                 status = common_decrypt_buffer(cli->trans_enc_state,
701                                                (char *)inbuf);
702                 if (!NT_STATUS_IS_OK(status)) {
703                         DEBUG(10, ("common_decrypt_buffer returned %s\n",
704                                    nt_errstr(status)));
705                         return status;
706                 }
707         }
708
709         mid = SVAL(inbuf, smb_mid);
710         num_pending = talloc_array_length(cli->conn.pending);
711
712         for (i=0; i<num_pending; i++) {
713                 if (mid == cli_smb_req_mid(cli->conn.pending[i])) {
714                         break;
715                 }
716         }
717         if (i == num_pending) {
718                 /* Dump unexpected reply */
719                 return NT_STATUS_RETRY;
720         }
721
722         oplock_break = false;
723
724         if (mid == 0xffff) {
725                 /*
726                  * Paranoia checks that this is really an oplock break request.
727                  */
728                 oplock_break = (smb_len(inbuf) == 51); /* hdr + 8 words */
729                 oplock_break &= ((CVAL(inbuf, smb_flg) & FLAG_REPLY) == 0);
730                 oplock_break &= (CVAL(inbuf, smb_com) == SMBlockingX);
731                 oplock_break &= (SVAL(inbuf, smb_vwv6) == 0);
732                 oplock_break &= (SVAL(inbuf, smb_vwv7) == 0);
733
734                 if (!oplock_break) {
735                         /* Dump unexpected reply */
736                         return NT_STATUS_RETRY;
737                 }
738         }
739
740         req = cli->conn.pending[i];
741         state = tevent_req_data(req, struct cli_smb_state);
742
743         if (!oplock_break /* oplock breaks are not signed */
744             && !cli_check_sign_mac(cli, (char *)inbuf, state->seqnum+1)) {
745                 DEBUG(10, ("cli_check_sign_mac failed\n"));
746                 return NT_STATUS_ACCESS_DENIED;
747         }
748
749         if (state->chained_requests != NULL) {
750                 struct tevent_req **chain = talloc_move(frame,
751                                             &state->chained_requests);
752                 int num_chained = talloc_array_length(chain);
753
754                 /*
755                  * We steal the inbuf to the chain,
756                  * so that it will stay until all
757                  * requests of the chain are finished.
758                  *
759                  * Each requests in the chain will
760                  * hold a talloc reference to the chain.
761                  * This way we do not expose the talloc_reference()
762                  * behavior to the callers.
763                  */
764                 talloc_steal(chain, inbuf);
765
766                 for (i=0; i<num_chained; i++) {
767                         struct tevent_req **ref;
768
769                         req = chain[i];
770                         state = tevent_req_data(req, struct cli_smb_state);
771
772                         cli_smb_req_unset_pending(req);
773
774                         /*
775                          * as we finish multiple requests here
776                          * we need to defer the callbacks as
777                          * they could destroy our current stack state.
778                          */
779                         tevent_req_defer_callback(req, state->ev);
780
781                         ref = talloc_reference(state, chain);
782                         if (tevent_req_nomem(ref, req)) {
783                                 continue;
784                         }
785
786                         state->inbuf = inbuf;
787                         state->chain_num = i;
788                         state->chain_length = num_chained;
789
790                         tevent_req_done(req);
791                 }
792
793                 return NT_STATUS_RETRY;
794         }
795
796         cli_smb_req_unset_pending(req);
797
798         state->inbuf = talloc_move(state, &inbuf);
799         state->chain_num = 0;
800         state->chain_length = 1;
801
802         if (talloc_array_length(cli->conn.pending) == 0) {
803                 tevent_req_done(req);
804                 return NT_STATUS_OK;
805         }
806
807         tevent_req_defer_callback(req, state->ev);
808         tevent_req_done(req);
809         return NT_STATUS_RETRY;
810 }
811
812 NTSTATUS cli_smb_recv(struct tevent_req *req,
813                       TALLOC_CTX *mem_ctx, uint8_t **pinbuf,
814                       uint8_t min_wct, uint8_t *pwct, uint16_t **pvwv,
815                       uint32_t *pnum_bytes, uint8_t **pbytes)
816 {
817         struct cli_smb_state *state = tevent_req_data(
818                 req, struct cli_smb_state);
819         NTSTATUS status = NT_STATUS_OK;
820         uint8_t cmd, wct;
821         uint16_t num_bytes;
822         size_t wct_ofs, bytes_offset;
823         int i;
824
825         if (tevent_req_is_nterror(req, &status)) {
826                 return status;
827         }
828
829         if (state->inbuf == NULL) {
830                 if (min_wct != 0) {
831                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
832                 }
833                 if (pinbuf) {
834                         *pinbuf = NULL;
835                 }
836                 if (pwct) {
837                         *pwct = 0;
838                 }
839                 if (pvwv) {
840                         *pvwv = NULL;
841                 }
842                 if (pnum_bytes) {
843                         *pnum_bytes = 0;
844                 }
845                 if (pbytes) {
846                         *pbytes = NULL;
847                 }
848                 /* This was a request without a reply */
849                 return NT_STATUS_OK;
850         }
851
852         wct_ofs = smb_wct;
853         cmd = CVAL(state->inbuf, smb_com);
854
855         for (i=0; i<state->chain_num; i++) {
856                 if (i < state->chain_num-1) {
857                         if (cmd == 0xff) {
858                                 return NT_STATUS_REQUEST_ABORTED;
859                         }
860                         if (!is_andx_req(cmd)) {
861                                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
862                         }
863                 }
864
865                 if (!have_andx_command((char *)state->inbuf, wct_ofs)) {
866                         /*
867                          * This request was not completed because a previous
868                          * request in the chain had received an error.
869                          */
870                         return NT_STATUS_REQUEST_ABORTED;
871                 }
872
873                 wct_ofs = SVAL(state->inbuf, wct_ofs + 3);
874
875                 /*
876                  * Skip the all-present length field. No overflow, we've just
877                  * put a 16-bit value into a size_t.
878                  */
879                 wct_ofs += 4;
880
881                 if (wct_ofs+2 > talloc_get_size(state->inbuf)) {
882                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
883                 }
884
885                 cmd = CVAL(state->inbuf, wct_ofs + 1);
886         }
887
888         state->cli->raw_status = cli_pull_raw_error(state->inbuf);
889         if (NT_STATUS_IS_DOS(state->cli->raw_status)) {
890                 uint8_t eclass = NT_STATUS_DOS_CLASS(state->cli->raw_status);
891                 uint16_t ecode = NT_STATUS_DOS_CODE(state->cli->raw_status);
892                 /*
893                  * TODO: is it really a good idea to do a mapping here?
894                  *
895                  * The old cli_pull_error() also does it, so I do not change
896                  * the behavior yet.
897                  */
898                 status = dos_to_ntstatus(eclass, ecode);
899         } else {
900                 status = state->cli->raw_status;
901         }
902
903         if (!have_andx_command((char *)state->inbuf, wct_ofs)) {
904
905                 if ((cmd == SMBsesssetupX)
906                     && NT_STATUS_EQUAL(
907                             status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
908                         /*
909                          * NT_STATUS_MORE_PROCESSING_REQUIRED is a
910                          * valid return code for session setup
911                          */
912                         goto no_err;
913                 }
914
915                 if (NT_STATUS_IS_ERR(status)) {
916                         /*
917                          * The last command takes the error code. All
918                          * further commands down the requested chain
919                          * will get a NT_STATUS_REQUEST_ABORTED.
920                          */
921                         return status;
922                 }
923         }
924
925 no_err:
926
927         wct = CVAL(state->inbuf, wct_ofs);
928         bytes_offset = wct_ofs + 1 + wct * sizeof(uint16_t);
929         num_bytes = SVAL(state->inbuf, bytes_offset);
930
931         if (wct < min_wct) {
932                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
933         }
934
935         /*
936          * wct_ofs is a 16-bit value plus 4, wct is a 8-bit value, num_bytes
937          * is a 16-bit value. So bytes_offset being size_t should be far from
938          * wrapping.
939          */
940         if ((bytes_offset + 2 > talloc_get_size(state->inbuf))
941             || (bytes_offset > 0xffff)) {
942                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
943         }
944
945         if (pwct != NULL) {
946                 *pwct = wct;
947         }
948         if (pvwv != NULL) {
949                 *pvwv = (uint16_t *)(state->inbuf + wct_ofs + 1);
950         }
951         if (pnum_bytes != NULL) {
952                 *pnum_bytes = num_bytes;
953         }
954         if (pbytes != NULL) {
955                 *pbytes = (uint8_t *)state->inbuf + bytes_offset + 2;
956         }
957         if ((mem_ctx != NULL) && (pinbuf != NULL)) {
958                 if (state->chain_num == state->chain_length-1) {
959                         *pinbuf = talloc_move(mem_ctx, &state->inbuf);
960                 } else {
961                         *pinbuf = state->inbuf;
962                 }
963         }
964
965         return status;
966 }
967
968 size_t cli_smb_wct_ofs(struct tevent_req **reqs, int num_reqs)
969 {
970         size_t wct_ofs;
971         int i;
972
973         wct_ofs = smb_wct - 4;
974
975         for (i=0; i<num_reqs; i++) {
976                 struct cli_smb_state *state;
977                 state = tevent_req_data(reqs[i], struct cli_smb_state);
978                 wct_ofs += iov_len(state->iov+1, state->iov_count-1);
979                 wct_ofs = (wct_ofs + 3) & ~3;
980         }
981         return wct_ofs;
982 }
983
984 NTSTATUS cli_smb_chain_send(struct tevent_req **reqs, int num_reqs)
985 {
986         struct cli_smb_state *first_state = tevent_req_data(
987                 reqs[0], struct cli_smb_state);
988         struct cli_smb_state *last_state = tevent_req_data(
989                 reqs[num_reqs-1], struct cli_smb_state);
990         struct cli_smb_state *state;
991         size_t wct_offset;
992         size_t chain_padding = 0;
993         int i, iovlen;
994         struct iovec *iov = NULL;
995         struct iovec *this_iov;
996         NTSTATUS status;
997
998         iovlen = 0;
999         for (i=0; i<num_reqs; i++) {
1000                 if (!tevent_req_is_in_progress(reqs[i])) {
1001                         return NT_STATUS_INTERNAL_ERROR;
1002                 }
1003
1004                 state = tevent_req_data(reqs[i], struct cli_smb_state);
1005                 iovlen += state->iov_count;
1006         }
1007
1008         iov = talloc_array(last_state, struct iovec, iovlen);
1009         if (iov == NULL) {
1010                 return NT_STATUS_NO_MEMORY;
1011         }
1012
1013         first_state->chained_requests = (struct tevent_req **)talloc_memdup(
1014                 last_state, reqs, sizeof(*reqs) * num_reqs);
1015         if (first_state->chained_requests == NULL) {
1016                 TALLOC_FREE(iov);
1017                 return NT_STATUS_NO_MEMORY;
1018         }
1019
1020         wct_offset = smb_wct - 4;
1021         this_iov = iov;
1022
1023         for (i=0; i<num_reqs; i++) {
1024                 size_t next_padding = 0;
1025                 uint16_t *vwv;
1026
1027                 state = tevent_req_data(reqs[i], struct cli_smb_state);
1028
1029                 if (i < num_reqs-1) {
1030                         if (!is_andx_req(CVAL(state->header, smb_com))
1031                             || CVAL(state->header, smb_wct) < 2) {
1032                                 TALLOC_FREE(iov);
1033                                 TALLOC_FREE(first_state->chained_requests);
1034                                 return NT_STATUS_INVALID_PARAMETER;
1035                         }
1036                 }
1037
1038                 wct_offset += iov_len(state->iov+1, state->iov_count-1) + 1;
1039                 if ((wct_offset % 4) != 0) {
1040                         next_padding = 4 - (wct_offset % 4);
1041                 }
1042                 wct_offset += next_padding;
1043                 vwv = state->vwv;
1044
1045                 if (i < num_reqs-1) {
1046                         struct cli_smb_state *next_state = tevent_req_data(
1047                                 reqs[i+1], struct cli_smb_state);
1048                         SCVAL(vwv+0, 0, CVAL(next_state->header, smb_com));
1049                         SCVAL(vwv+0, 1, 0);
1050                         SSVAL(vwv+1, 0, wct_offset);
1051                 } else if (is_andx_req(CVAL(state->header, smb_com))) {
1052                         /* properly end the chain */
1053                         SCVAL(vwv+0, 0, 0xff);
1054                         SCVAL(vwv+0, 1, 0xff);
1055                         SSVAL(vwv+1, 0, 0);
1056                 }
1057
1058                 if (i == 0) {
1059                         this_iov[0] = state->iov[0];
1060                 } else {
1061                         /*
1062                          * This one is a bit subtle. We have to add
1063                          * chain_padding bytes between the requests, and we
1064                          * have to also include the wct field of the
1065                          * subsequent requests. We use the subsequent header
1066                          * for the padding, it contains the wct field in its
1067                          * last byte.
1068                          */
1069                         this_iov[0].iov_len = chain_padding+1;
1070                         this_iov[0].iov_base = (void *)&state->header[
1071                                 sizeof(state->header) - this_iov[0].iov_len];
1072                         memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
1073                 }
1074                 memcpy(this_iov+1, state->iov+1,
1075                        sizeof(struct iovec) * (state->iov_count-1));
1076                 this_iov += state->iov_count;
1077                 chain_padding = next_padding;
1078         }
1079
1080         status = cli_smb_req_iov_send(reqs[0], last_state, iov, iovlen);
1081         if (!NT_STATUS_IS_OK(status)) {
1082                 TALLOC_FREE(iov);
1083                 TALLOC_FREE(first_state->chained_requests);
1084                 return status;
1085         }
1086
1087         return NT_STATUS_OK;
1088 }
1089
1090 bool cli_has_async_calls(struct cli_state *cli)
1091 {
1092         return ((tevent_queue_length(cli->conn.outgoing) != 0)
1093                 || (talloc_array_length(cli->conn.pending) != 0));
1094 }