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