s3: Avoid a winbind 100% cpu loop
[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                 if (NT_STATUS_EQUAL(status, NT_STATUS_PIPE_BROKEN)) {
291                         /*
292                          * We're dead. No point waiting for trans2
293                          * replies.
294                          */
295                         state->mid = 0;
296                 }
297
298                 cli_smb_req_unset_pending(req);
299
300                 /*
301                  * we need to defer the callback, because we may notify more
302                  * then one caller.
303                  */
304                 tevent_req_defer_callback(req, state->ev);
305                 tevent_req_nterror(req, status);
306         }
307 }
308
309 /*
310  * Fetch a smb request's mid. Only valid after the request has been sent by
311  * cli_smb_req_send().
312  */
313 uint16_t cli_smb_req_mid(struct tevent_req *req)
314 {
315         struct cli_smb_state *state = tevent_req_data(
316                 req, struct cli_smb_state);
317
318         if (state->mid != 0) {
319                 return state->mid;
320         }
321
322         return SVAL(state->header, smb_mid);
323 }
324
325 void cli_smb_req_set_mid(struct tevent_req *req, uint16_t mid)
326 {
327         struct cli_smb_state *state = tevent_req_data(
328                 req, struct cli_smb_state);
329         state->mid = mid;
330 }
331
332 uint32_t cli_smb_req_seqnum(struct tevent_req *req)
333 {
334         struct cli_smb_state *state = tevent_req_data(
335                 req, struct cli_smb_state);
336         return state->seqnum;
337 }
338
339 void cli_smb_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
340 {
341         struct cli_smb_state *state = tevent_req_data(
342                 req, struct cli_smb_state);
343         state->seqnum = seqnum;
344 }
345
346 static size_t iov_len(const struct iovec *iov, int count)
347 {
348         size_t result = 0;
349         int i;
350         for (i=0; i<count; i++) {
351                 result += iov[i].iov_len;
352         }
353         return result;
354 }
355
356 static uint8_t *iov_concat(TALLOC_CTX *mem_ctx, const struct iovec *iov,
357                            int count)
358 {
359         size_t len = iov_len(iov, count);
360         size_t copied;
361         uint8_t *buf;
362         int i;
363
364         buf = talloc_array(mem_ctx, uint8_t, len);
365         if (buf == NULL) {
366                 return NULL;
367         }
368         copied = 0;
369         for (i=0; i<count; i++) {
370                 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
371                 copied += iov[i].iov_len;
372         }
373         return buf;
374 }
375
376 struct tevent_req *cli_smb_req_create(TALLOC_CTX *mem_ctx,
377                                       struct event_context *ev,
378                                       struct cli_state *cli,
379                                       uint8_t smb_command,
380                                       uint8_t additional_flags,
381                                       uint8_t wct, uint16_t *vwv,
382                                       int iov_count,
383                                       struct iovec *bytes_iov)
384 {
385         struct tevent_req *result;
386         struct cli_smb_state *state;
387         struct timeval endtime;
388
389         if (iov_count > MAX_SMB_IOV) {
390                 /*
391                  * Should not happen :-)
392                  */
393                 return NULL;
394         }
395
396         result = tevent_req_create(mem_ctx, &state, struct cli_smb_state);
397         if (result == NULL) {
398                 return NULL;
399         }
400         state->ev = ev;
401         state->cli = cli;
402         state->mid = 0;         /* Set to auto-choose in cli_smb_req_send */
403         state->chain_num = 0;
404         state->chained_requests = NULL;
405
406         cli_setup_packet_buf(cli, (char *)state->header);
407         SCVAL(state->header, smb_com, smb_command);
408         SSVAL(state->header, smb_tid, cli->smb1.tid);
409         SCVAL(state->header, smb_wct, wct);
410
411         state->vwv = vwv;
412
413         SSVAL(state->bytecount_buf, 0, iov_len(bytes_iov, iov_count));
414
415         state->iov[0].iov_base = (void *)state->header;
416         state->iov[0].iov_len  = sizeof(state->header);
417         state->iov[1].iov_base = (void *)state->vwv;
418         state->iov[1].iov_len  = wct * sizeof(uint16_t);
419         state->iov[2].iov_base = (void *)state->bytecount_buf;
420         state->iov[2].iov_len  = sizeof(uint16_t);
421
422         if (iov_count != 0) {
423                 memcpy(&state->iov[3], bytes_iov,
424                        iov_count * sizeof(*bytes_iov));
425         }
426         state->iov_count = iov_count + 3;
427
428         if (cli->timeout) {
429                 endtime = timeval_current_ofs_msec(cli->timeout);
430                 if (!tevent_req_set_endtime(result, ev, endtime)) {
431                         return result;
432                 }
433         }
434
435         switch (smb_command) {
436         case SMBtranss:
437         case SMBtranss2:
438         case SMBnttranss:
439         case SMBntcancel:
440                 state->one_way = true;
441                 break;
442         case SMBlockingX:
443                 if ((wct == 8) &&
444                     (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
445                         state->one_way = true;
446                 }
447                 break;
448         }
449
450         return result;
451 }
452
453 static NTSTATUS cli_signv(struct cli_state *cli, struct iovec *iov, int count,
454                           uint32_t *seqnum)
455 {
456         uint8_t *buf;
457
458         /*
459          * Obvious optimization: Make cli_calculate_sign_mac work with struct
460          * iovec directly. MD5Update would do that just fine.
461          */
462
463         if ((count <= 0) || (iov[0].iov_len < smb_wct)) {
464                 return NT_STATUS_INVALID_PARAMETER;
465         }
466
467         buf = iov_concat(talloc_tos(), iov, count);
468         if (buf == NULL) {
469                 return NT_STATUS_NO_MEMORY;
470         }
471
472         cli_calculate_sign_mac(cli, (char *)buf, seqnum);
473         memcpy(iov[0].iov_base, buf, iov[0].iov_len);
474
475         TALLOC_FREE(buf);
476         return NT_STATUS_OK;
477 }
478
479 static void cli_smb_sent(struct tevent_req *subreq);
480
481 static NTSTATUS cli_smb_req_iov_send(struct tevent_req *req,
482                                      struct cli_smb_state *state,
483                                      struct iovec *iov, int iov_count)
484 {
485         struct tevent_req *subreq;
486         NTSTATUS status;
487
488         if (!cli_state_is_connected(state->cli)) {
489                 return NT_STATUS_CONNECTION_DISCONNECTED;
490         }
491
492         if (iov[0].iov_len < smb_wct) {
493                 return NT_STATUS_INVALID_PARAMETER;
494         }
495
496         if (state->mid != 0) {
497                 SSVAL(iov[0].iov_base, smb_mid, state->mid);
498         } else {
499                 uint16_t mid = cli_alloc_mid(state->cli);
500                 SSVAL(iov[0].iov_base, smb_mid, mid);
501         }
502
503         smb_setlen((char *)iov[0].iov_base, iov_len(iov, iov_count) - 4);
504
505         status = cli_signv(state->cli, iov, iov_count, &state->seqnum);
506
507         if (!NT_STATUS_IS_OK(status)) {
508                 return status;
509         }
510
511         if (cli_state_encryption_on(state->cli)) {
512                 char *buf, *enc_buf;
513
514                 buf = (char *)iov_concat(talloc_tos(), iov, iov_count);
515                 if (buf == NULL) {
516                         return NT_STATUS_NO_MEMORY;
517                 }
518                 status = common_encrypt_buffer(state->cli->trans_enc_state,
519                                                (char *)buf, &enc_buf);
520                 TALLOC_FREE(buf);
521                 if (!NT_STATUS_IS_OK(status)) {
522                         DEBUG(0, ("Error in encrypting client message: %s\n",
523                                   nt_errstr(status)));
524                         return status;
525                 }
526                 buf = (char *)talloc_memdup(state, enc_buf,
527                                             smb_len(enc_buf)+4);
528                 SAFE_FREE(enc_buf);
529                 if (buf == NULL) {
530                         return NT_STATUS_NO_MEMORY;
531                 }
532                 iov[0].iov_base = (void *)buf;
533                 iov[0].iov_len = talloc_get_size(buf);
534                 iov_count = 1;
535         }
536         subreq = writev_send(state, state->ev, state->cli->conn.outgoing,
537                              state->cli->conn.fd, false, iov, iov_count);
538         if (subreq == NULL) {
539                 return NT_STATUS_NO_MEMORY;
540         }
541         tevent_req_set_callback(subreq, cli_smb_sent, req);
542         return NT_STATUS_OK;
543 }
544
545 NTSTATUS cli_smb_req_send(struct tevent_req *req)
546 {
547         struct cli_smb_state *state = tevent_req_data(
548                 req, struct cli_smb_state);
549
550         if (!tevent_req_is_in_progress(req)) {
551                 return NT_STATUS_INTERNAL_ERROR;
552         }
553
554         return cli_smb_req_iov_send(req, state, state->iov, state->iov_count);
555 }
556
557 struct tevent_req *cli_smb_send(TALLOC_CTX *mem_ctx,
558                                 struct event_context *ev,
559                                 struct cli_state *cli,
560                                 uint8_t smb_command,
561                                 uint8_t additional_flags,
562                                 uint8_t wct, uint16_t *vwv,
563                                 uint32_t num_bytes,
564                                 const uint8_t *bytes)
565 {
566         struct tevent_req *req;
567         struct iovec iov;
568         NTSTATUS status;
569
570         iov.iov_base = discard_const_p(void, bytes);
571         iov.iov_len = num_bytes;
572
573         req = cli_smb_req_create(mem_ctx, ev, cli, smb_command,
574                                  additional_flags, wct, vwv, 1, &iov);
575         if (req == NULL) {
576                 return NULL;
577         }
578         if (!tevent_req_is_in_progress(req)) {
579                 return tevent_req_post(req, ev);
580         }
581         status = cli_smb_req_send(req);
582         if (!NT_STATUS_IS_OK(status)) {
583                 tevent_req_nterror(req, status);
584                 return tevent_req_post(req, ev);
585         }
586         return req;
587 }
588
589 static void cli_smb_sent(struct tevent_req *subreq)
590 {
591         struct tevent_req *req = tevent_req_callback_data(
592                 subreq, struct tevent_req);
593         struct cli_smb_state *state = tevent_req_data(
594                 req, struct cli_smb_state);
595         ssize_t nwritten;
596         int err;
597
598         nwritten = writev_recv(subreq, &err);
599         TALLOC_FREE(subreq);
600         if (nwritten == -1) {
601                 NTSTATUS status = map_nt_error_from_unix(err);
602                 cli_state_notify_pending(state->cli, status);
603                 return;
604         }
605
606         if (state->one_way) {
607                 state->inbuf = NULL;
608                 tevent_req_done(req);
609                 return;
610         }
611
612         if (!cli_smb_req_set_pending(req)) {
613                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
614                 return;
615         }
616 }
617
618 static void cli_smb_received(struct tevent_req *subreq)
619 {
620         struct cli_state *cli = tevent_req_callback_data(
621                 subreq, struct cli_state);
622         TALLOC_CTX *frame = talloc_stackframe();
623         NTSTATUS status;
624         uint8_t *inbuf;
625         ssize_t received;
626         int err;
627
628         if (subreq != cli->conn.read_smb_req) {
629                 DEBUG(1, ("Internal error: cli_smb_received called with "
630                           "unexpected subreq\n"));
631                 status = NT_STATUS_INTERNAL_ERROR;
632                 cli_state_notify_pending(cli, status);
633                 TALLOC_FREE(frame);
634                 return;
635         }
636
637         received = read_smb_recv(subreq, frame, &inbuf, &err);
638         TALLOC_FREE(subreq);
639         cli->conn.read_smb_req = NULL;
640         if (received == -1) {
641                 status = map_nt_error_from_unix(err);
642                 cli_state_notify_pending(cli, status);
643                 TALLOC_FREE(frame);
644                 return;
645         }
646
647         status = cli->conn.dispatch_incoming(cli, frame, inbuf);
648         TALLOC_FREE(frame);
649         if (NT_STATUS_IS_OK(status)) {
650                 /*
651                  * We should not do any more processing
652                  * as the dispatch function called
653                  * tevent_req_done().
654                  */
655                 return;
656         } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
657                 /*
658                  * We got an error, so notify all pending requests
659                  */
660                 cli_state_notify_pending(cli, status);
661                 return;
662         }
663
664         /*
665          * We got NT_STATUS_RETRY, so we may ask for a
666          * next incoming pdu.
667          */
668         if (!cli_state_receive_next(cli)) {
669                 cli_state_notify_pending(cli, NT_STATUS_NO_MEMORY);
670         }
671 }
672
673 static NTSTATUS cli_state_dispatch_smb1(struct cli_state *cli,
674                                         TALLOC_CTX *frame,
675                                         uint8_t *inbuf)
676 {
677         struct tevent_req *req;
678         struct cli_smb_state *state;
679         NTSTATUS status;
680         int num_pending;
681         int i;
682         uint16_t mid;
683         bool oplock_break;
684
685         if ((IVAL(inbuf, 4) != 0x424d53ff) /* 0xFF"SMB" */
686             && (SVAL(inbuf, 4) != 0x45ff)) /* 0xFF"E" */ {
687                 DEBUG(10, ("Got non-SMB PDU\n"));
688                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
689         }
690
691         if (cli_state_encryption_on(cli) && (CVAL(inbuf, 0) == 0)) {
692                 uint16_t enc_ctx_num;
693
694                 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
695                 if (!NT_STATUS_IS_OK(status)) {
696                         DEBUG(10, ("get_enc_ctx_num returned %s\n",
697                                    nt_errstr(status)));
698                         return status;
699                 }
700
701                 if (enc_ctx_num != cli->trans_enc_state->enc_ctx_num) {
702                         DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
703                                    enc_ctx_num,
704                                    cli->trans_enc_state->enc_ctx_num));
705                         return NT_STATUS_INVALID_HANDLE;
706                 }
707
708                 status = common_decrypt_buffer(cli->trans_enc_state,
709                                                (char *)inbuf);
710                 if (!NT_STATUS_IS_OK(status)) {
711                         DEBUG(10, ("common_decrypt_buffer returned %s\n",
712                                    nt_errstr(status)));
713                         return status;
714                 }
715         }
716
717         mid = SVAL(inbuf, smb_mid);
718         num_pending = talloc_array_length(cli->conn.pending);
719
720         for (i=0; i<num_pending; i++) {
721                 if (mid == cli_smb_req_mid(cli->conn.pending[i])) {
722                         break;
723                 }
724         }
725         if (i == num_pending) {
726                 /* Dump unexpected reply */
727                 return NT_STATUS_RETRY;
728         }
729
730         oplock_break = false;
731
732         if (mid == 0xffff) {
733                 /*
734                  * Paranoia checks that this is really an oplock break request.
735                  */
736                 oplock_break = (smb_len(inbuf) == 51); /* hdr + 8 words */
737                 oplock_break &= ((CVAL(inbuf, smb_flg) & FLAG_REPLY) == 0);
738                 oplock_break &= (CVAL(inbuf, smb_com) == SMBlockingX);
739                 oplock_break &= (SVAL(inbuf, smb_vwv6) == 0);
740                 oplock_break &= (SVAL(inbuf, smb_vwv7) == 0);
741
742                 if (!oplock_break) {
743                         /* Dump unexpected reply */
744                         return NT_STATUS_RETRY;
745                 }
746         }
747
748         req = cli->conn.pending[i];
749         state = tevent_req_data(req, struct cli_smb_state);
750
751         if (!oplock_break /* oplock breaks are not signed */
752             && !cli_check_sign_mac(cli, (char *)inbuf, state->seqnum+1)) {
753                 DEBUG(10, ("cli_check_sign_mac failed\n"));
754                 return NT_STATUS_ACCESS_DENIED;
755         }
756
757         if (state->chained_requests != NULL) {
758                 struct tevent_req **chain = talloc_move(frame,
759                                             &state->chained_requests);
760                 int num_chained = talloc_array_length(chain);
761
762                 /*
763                  * We steal the inbuf to the chain,
764                  * so that it will stay until all
765                  * requests of the chain are finished.
766                  *
767                  * Each requests in the chain will
768                  * hold a talloc reference to the chain.
769                  * This way we do not expose the talloc_reference()
770                  * behavior to the callers.
771                  */
772                 talloc_steal(chain, inbuf);
773
774                 for (i=0; i<num_chained; i++) {
775                         struct tevent_req **ref;
776
777                         req = chain[i];
778                         state = tevent_req_data(req, struct cli_smb_state);
779
780                         cli_smb_req_unset_pending(req);
781
782                         /*
783                          * as we finish multiple requests here
784                          * we need to defer the callbacks as
785                          * they could destroy our current stack state.
786                          */
787                         tevent_req_defer_callback(req, state->ev);
788
789                         ref = talloc_reference(state, chain);
790                         if (tevent_req_nomem(ref, req)) {
791                                 continue;
792                         }
793
794                         state->inbuf = inbuf;
795                         state->chain_num = i;
796                         state->chain_length = num_chained;
797
798                         tevent_req_done(req);
799                 }
800
801                 return NT_STATUS_RETRY;
802         }
803
804         cli_smb_req_unset_pending(req);
805
806         state->inbuf = talloc_move(state, &inbuf);
807         state->chain_num = 0;
808         state->chain_length = 1;
809
810         if (talloc_array_length(cli->conn.pending) == 0) {
811                 tevent_req_done(req);
812                 return NT_STATUS_OK;
813         }
814
815         tevent_req_defer_callback(req, state->ev);
816         tevent_req_done(req);
817         return NT_STATUS_RETRY;
818 }
819
820 NTSTATUS cli_smb_recv(struct tevent_req *req,
821                       TALLOC_CTX *mem_ctx, uint8_t **pinbuf,
822                       uint8_t min_wct, uint8_t *pwct, uint16_t **pvwv,
823                       uint32_t *pnum_bytes, uint8_t **pbytes)
824 {
825         struct cli_smb_state *state = tevent_req_data(
826                 req, struct cli_smb_state);
827         NTSTATUS status = NT_STATUS_OK;
828         uint8_t cmd, wct;
829         uint16_t num_bytes;
830         size_t wct_ofs, bytes_offset;
831         int i;
832
833         if (tevent_req_is_nterror(req, &status)) {
834                 return status;
835         }
836
837         if (state->inbuf == NULL) {
838                 if (min_wct != 0) {
839                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
840                 }
841                 if (pinbuf) {
842                         *pinbuf = NULL;
843                 }
844                 if (pwct) {
845                         *pwct = 0;
846                 }
847                 if (pvwv) {
848                         *pvwv = NULL;
849                 }
850                 if (pnum_bytes) {
851                         *pnum_bytes = 0;
852                 }
853                 if (pbytes) {
854                         *pbytes = NULL;
855                 }
856                 /* This was a request without a reply */
857                 return NT_STATUS_OK;
858         }
859
860         wct_ofs = smb_wct;
861         cmd = CVAL(state->inbuf, smb_com);
862
863         for (i=0; i<state->chain_num; i++) {
864                 if (i < state->chain_num-1) {
865                         if (cmd == 0xff) {
866                                 return NT_STATUS_REQUEST_ABORTED;
867                         }
868                         if (!is_andx_req(cmd)) {
869                                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
870                         }
871                 }
872
873                 if (!have_andx_command((char *)state->inbuf, wct_ofs)) {
874                         /*
875                          * This request was not completed because a previous
876                          * request in the chain had received an error.
877                          */
878                         return NT_STATUS_REQUEST_ABORTED;
879                 }
880
881                 wct_ofs = SVAL(state->inbuf, wct_ofs + 3);
882
883                 /*
884                  * Skip the all-present length field. No overflow, we've just
885                  * put a 16-bit value into a size_t.
886                  */
887                 wct_ofs += 4;
888
889                 if (wct_ofs+2 > talloc_get_size(state->inbuf)) {
890                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
891                 }
892
893                 cmd = CVAL(state->inbuf, wct_ofs + 1);
894         }
895
896         state->cli->raw_status = cli_pull_raw_error(state->inbuf);
897         if (NT_STATUS_IS_DOS(state->cli->raw_status)) {
898                 uint8_t eclass = NT_STATUS_DOS_CLASS(state->cli->raw_status);
899                 uint16_t ecode = NT_STATUS_DOS_CODE(state->cli->raw_status);
900                 /*
901                  * TODO: is it really a good idea to do a mapping here?
902                  *
903                  * The old cli_pull_error() also does it, so I do not change
904                  * the behavior yet.
905                  */
906                 status = dos_to_ntstatus(eclass, ecode);
907         } else {
908                 status = state->cli->raw_status;
909         }
910
911         if (!have_andx_command((char *)state->inbuf, wct_ofs)) {
912
913                 if ((cmd == SMBsesssetupX)
914                     && NT_STATUS_EQUAL(
915                             status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
916                         /*
917                          * NT_STATUS_MORE_PROCESSING_REQUIRED is a
918                          * valid return code for session setup
919                          */
920                         goto no_err;
921                 }
922
923                 if (NT_STATUS_IS_ERR(status)) {
924                         /*
925                          * The last command takes the error code. All
926                          * further commands down the requested chain
927                          * will get a NT_STATUS_REQUEST_ABORTED.
928                          */
929                         return status;
930                 }
931         }
932
933 no_err:
934
935         wct = CVAL(state->inbuf, wct_ofs);
936         bytes_offset = wct_ofs + 1 + wct * sizeof(uint16_t);
937         num_bytes = SVAL(state->inbuf, bytes_offset);
938
939         if (wct < min_wct) {
940                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
941         }
942
943         /*
944          * wct_ofs is a 16-bit value plus 4, wct is a 8-bit value, num_bytes
945          * is a 16-bit value. So bytes_offset being size_t should be far from
946          * wrapping.
947          */
948         if ((bytes_offset + 2 > talloc_get_size(state->inbuf))
949             || (bytes_offset > 0xffff)) {
950                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
951         }
952
953         if (pwct != NULL) {
954                 *pwct = wct;
955         }
956         if (pvwv != NULL) {
957                 *pvwv = (uint16_t *)(state->inbuf + wct_ofs + 1);
958         }
959         if (pnum_bytes != NULL) {
960                 *pnum_bytes = num_bytes;
961         }
962         if (pbytes != NULL) {
963                 *pbytes = (uint8_t *)state->inbuf + bytes_offset + 2;
964         }
965         if ((mem_ctx != NULL) && (pinbuf != NULL)) {
966                 if (state->chain_num == state->chain_length-1) {
967                         *pinbuf = talloc_move(mem_ctx, &state->inbuf);
968                 } else {
969                         *pinbuf = state->inbuf;
970                 }
971         }
972
973         return status;
974 }
975
976 size_t cli_smb_wct_ofs(struct tevent_req **reqs, int num_reqs)
977 {
978         size_t wct_ofs;
979         int i;
980
981         wct_ofs = smb_wct - 4;
982
983         for (i=0; i<num_reqs; i++) {
984                 struct cli_smb_state *state;
985                 state = tevent_req_data(reqs[i], struct cli_smb_state);
986                 wct_ofs += iov_len(state->iov+1, state->iov_count-1);
987                 wct_ofs = (wct_ofs + 3) & ~3;
988         }
989         return wct_ofs;
990 }
991
992 NTSTATUS cli_smb_chain_send(struct tevent_req **reqs, int num_reqs)
993 {
994         struct cli_smb_state *first_state = tevent_req_data(
995                 reqs[0], struct cli_smb_state);
996         struct cli_smb_state *last_state = tevent_req_data(
997                 reqs[num_reqs-1], struct cli_smb_state);
998         struct cli_smb_state *state;
999         size_t wct_offset;
1000         size_t chain_padding = 0;
1001         int i, iovlen;
1002         struct iovec *iov = NULL;
1003         struct iovec *this_iov;
1004         NTSTATUS status;
1005
1006         iovlen = 0;
1007         for (i=0; i<num_reqs; i++) {
1008                 if (!tevent_req_is_in_progress(reqs[i])) {
1009                         return NT_STATUS_INTERNAL_ERROR;
1010                 }
1011
1012                 state = tevent_req_data(reqs[i], struct cli_smb_state);
1013                 iovlen += state->iov_count;
1014         }
1015
1016         iov = talloc_array(last_state, struct iovec, iovlen);
1017         if (iov == NULL) {
1018                 return NT_STATUS_NO_MEMORY;
1019         }
1020
1021         first_state->chained_requests = (struct tevent_req **)talloc_memdup(
1022                 last_state, reqs, sizeof(*reqs) * num_reqs);
1023         if (first_state->chained_requests == NULL) {
1024                 TALLOC_FREE(iov);
1025                 return NT_STATUS_NO_MEMORY;
1026         }
1027
1028         wct_offset = smb_wct - 4;
1029         this_iov = iov;
1030
1031         for (i=0; i<num_reqs; i++) {
1032                 size_t next_padding = 0;
1033                 uint16_t *vwv;
1034
1035                 state = tevent_req_data(reqs[i], struct cli_smb_state);
1036
1037                 if (i < num_reqs-1) {
1038                         if (!is_andx_req(CVAL(state->header, smb_com))
1039                             || CVAL(state->header, smb_wct) < 2) {
1040                                 TALLOC_FREE(iov);
1041                                 TALLOC_FREE(first_state->chained_requests);
1042                                 return NT_STATUS_INVALID_PARAMETER;
1043                         }
1044                 }
1045
1046                 wct_offset += iov_len(state->iov+1, state->iov_count-1) + 1;
1047                 if ((wct_offset % 4) != 0) {
1048                         next_padding = 4 - (wct_offset % 4);
1049                 }
1050                 wct_offset += next_padding;
1051                 vwv = state->vwv;
1052
1053                 if (i < num_reqs-1) {
1054                         struct cli_smb_state *next_state = tevent_req_data(
1055                                 reqs[i+1], struct cli_smb_state);
1056                         SCVAL(vwv+0, 0, CVAL(next_state->header, smb_com));
1057                         SCVAL(vwv+0, 1, 0);
1058                         SSVAL(vwv+1, 0, wct_offset);
1059                 } else if (is_andx_req(CVAL(state->header, smb_com))) {
1060                         /* properly end the chain */
1061                         SCVAL(vwv+0, 0, 0xff);
1062                         SCVAL(vwv+0, 1, 0xff);
1063                         SSVAL(vwv+1, 0, 0);
1064                 }
1065
1066                 if (i == 0) {
1067                         this_iov[0] = state->iov[0];
1068                 } else {
1069                         /*
1070                          * This one is a bit subtle. We have to add
1071                          * chain_padding bytes between the requests, and we
1072                          * have to also include the wct field of the
1073                          * subsequent requests. We use the subsequent header
1074                          * for the padding, it contains the wct field in its
1075                          * last byte.
1076                          */
1077                         this_iov[0].iov_len = chain_padding+1;
1078                         this_iov[0].iov_base = (void *)&state->header[
1079                                 sizeof(state->header) - this_iov[0].iov_len];
1080                         memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
1081                 }
1082                 memcpy(this_iov+1, state->iov+1,
1083                        sizeof(struct iovec) * (state->iov_count-1));
1084                 this_iov += state->iov_count;
1085                 chain_padding = next_padding;
1086         }
1087
1088         status = cli_smb_req_iov_send(reqs[0], last_state, iov, iovlen);
1089         if (!NT_STATUS_IS_OK(status)) {
1090                 TALLOC_FREE(iov);
1091                 TALLOC_FREE(first_state->chained_requests);
1092                 return status;
1093         }
1094
1095         return NT_STATUS_OK;
1096 }
1097
1098 bool cli_has_async_calls(struct cli_state *cli)
1099 {
1100         return ((tevent_queue_length(cli->conn.outgoing) != 0)
1101                 || (talloc_array_length(cli->conn.pending) != 0));
1102 }