s3-talloc Change TALLOC_ARRAY() to talloc_array()
[sfrench/samba-autobuild/.git] / source3 / libsmb / clireadwrite.c
1 /*
2    Unix SMB/CIFS implementation.
3    client file read/write routines
4    Copyright (C) Andrew Tridgell 1994-1998
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/util/tevent_ntstatus.h"
23 #include "async_smb.h"
24 #include "trans2.h"
25
26 /****************************************************************************
27   Calculate the recommended read buffer size
28 ****************************************************************************/
29 static size_t cli_read_max_bufsize(struct cli_state *cli)
30 {
31         if (!client_is_signing_on(cli) && !cli_encryption_on(cli)
32             && (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
33                 return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
34         }
35         if (cli->capabilities & CAP_LARGE_READX) {
36                 return cli->is_samba
37                         ? CLI_SAMBA_MAX_LARGE_READX_SIZE
38                         : CLI_WINDOWS_MAX_LARGE_READX_SIZE;
39         }
40         return (cli->max_xmit - (smb_size+32)) & ~1023;
41 }
42
43 /****************************************************************************
44   Calculate the recommended write buffer size
45 ****************************************************************************/
46 static size_t cli_write_max_bufsize(struct cli_state *cli, uint16_t write_mode)
47 {
48         if (write_mode == 0 &&
49             !client_is_signing_on(cli) &&
50             !cli_encryption_on(cli) &&
51             (cli->server_posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
52             (cli->capabilities & CAP_LARGE_FILES)) {
53                 /* Only do massive writes if we can do them direct
54                  * with no signing or encrypting - not on a pipe. */
55                 return CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
56         }
57
58         if (cli->is_samba) {
59                 return CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
60         }
61
62         if (((cli->capabilities & CAP_LARGE_WRITEX) == 0)
63             || client_is_signing_on(cli)
64             || strequal(cli->dev, "LPT1:")) {
65
66                 /*
67                  * Printer devices are restricted to max_xmit writesize in
68                  * Vista and XPSP3 as are signing connections.
69                  */
70
71                 return (cli->max_xmit - (smb_size+32)) & ~1023;
72         }
73
74         return CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
75 }
76
77 struct cli_read_andx_state {
78         size_t size;
79         uint16_t vwv[12];
80         NTSTATUS status;
81         size_t received;
82         uint8_t *buf;
83 };
84
85 static void cli_read_andx_done(struct tevent_req *subreq);
86
87 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
88                                         struct event_context *ev,
89                                         struct cli_state *cli, uint16_t fnum,
90                                         off_t offset, size_t size,
91                                         struct tevent_req **psmbreq)
92 {
93         struct tevent_req *req, *subreq;
94         struct cli_read_andx_state *state;
95         uint8_t wct = 10;
96
97         if (size > cli_read_max_bufsize(cli)) {
98                 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
99                           "size=%d\n", (int)size,
100                           (int)cli_read_max_bufsize(cli)));
101                 return NULL;
102         }
103
104         req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
105         if (req == NULL) {
106                 return NULL;
107         }
108         state->size = size;
109
110         SCVAL(state->vwv + 0, 0, 0xFF);
111         SCVAL(state->vwv + 0, 1, 0);
112         SSVAL(state->vwv + 1, 0, 0);
113         SSVAL(state->vwv + 2, 0, fnum);
114         SIVAL(state->vwv + 3, 0, offset);
115         SSVAL(state->vwv + 5, 0, size);
116         SSVAL(state->vwv + 6, 0, size);
117         SSVAL(state->vwv + 7, 0, (size >> 16));
118         SSVAL(state->vwv + 8, 0, 0);
119         SSVAL(state->vwv + 9, 0, 0);
120
121         if ((uint64_t)offset >> 32) {
122                 SIVAL(state->vwv + 10, 0,
123                       (((uint64_t)offset)>>32) & 0xffffffff);
124                 wct += 2;
125         }
126
127         subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
128                                     state->vwv, 0, NULL);
129         if (subreq == NULL) {
130                 TALLOC_FREE(req);
131                 return NULL;
132         }
133         tevent_req_set_callback(subreq, cli_read_andx_done, req);
134         *psmbreq = subreq;
135         return req;
136 }
137
138 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
139                                       struct event_context *ev,
140                                       struct cli_state *cli, uint16_t fnum,
141                                       off_t offset, size_t size)
142 {
143         struct tevent_req *req, *subreq;
144         NTSTATUS status;
145
146         req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
147                                    &subreq);
148         if (req == NULL) {
149                 return NULL;
150         }
151
152         status = cli_smb_req_send(subreq);
153         if (tevent_req_nterror(req, status)) {
154                 return tevent_req_post(req, ev);
155         }
156         return req;
157 }
158
159 static void cli_read_andx_done(struct tevent_req *subreq)
160 {
161         struct tevent_req *req = tevent_req_callback_data(
162                 subreq, struct tevent_req);
163         struct cli_read_andx_state *state = tevent_req_data(
164                 req, struct cli_read_andx_state);
165         uint8_t *inbuf;
166         uint8_t wct;
167         uint16_t *vwv;
168         uint32_t num_bytes;
169         uint8_t *bytes;
170
171         state->status = cli_smb_recv(subreq, state, &inbuf, 12, &wct, &vwv,
172                                      &num_bytes, &bytes);
173         TALLOC_FREE(subreq);
174         if (NT_STATUS_IS_ERR(state->status)) {
175                 tevent_req_nterror(req, state->status);
176                 return;
177         }
178
179         /* size is the number of bytes the server returned.
180          * Might be zero. */
181         state->received = SVAL(vwv + 5, 0);
182         state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
183
184         if (state->received > state->size) {
185                 DEBUG(5,("server returned more than we wanted!\n"));
186                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
187                 return;
188         }
189
190         /*
191          * bcc field must be valid for small reads, for large reads the 16-bit
192          * bcc field can't be correct.
193          */
194
195         if ((state->received < 0xffff) && (state->received > num_bytes)) {
196                 DEBUG(5, ("server announced more bytes than sent\n"));
197                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
198                 return;
199         }
200
201         state->buf = discard_const_p(uint8_t, smb_base(inbuf)) + SVAL(vwv+6, 0);
202
203         if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
204             || ((state->received != 0) && (state->buf < bytes))) {
205                 DEBUG(5, ("server returned invalid read&x data offset\n"));
206                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
207                 return;
208         }
209         tevent_req_done(req);
210 }
211
212 /*
213  * Pull the data out of a finished async read_and_x request. rcvbuf is
214  * talloced from the request, so better make sure that you copy it away before
215  * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
216  * talloc_move it!
217  */
218
219 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
220                             uint8_t **rcvbuf)
221 {
222         struct cli_read_andx_state *state = tevent_req_data(
223                 req, struct cli_read_andx_state);
224         NTSTATUS status;
225
226         if (tevent_req_is_nterror(req, &status)) {
227                 return status;
228         }
229         *received = state->received;
230         *rcvbuf = state->buf;
231         return NT_STATUS_OK;
232 }
233
234 struct cli_readall_state {
235         struct tevent_context *ev;
236         struct cli_state *cli;
237         uint16_t fnum;
238         off_t start_offset;
239         size_t size;
240         size_t received;
241         uint8_t *buf;
242 };
243
244 static void cli_readall_done(struct tevent_req *subreq);
245
246 static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
247                                            struct event_context *ev,
248                                            struct cli_state *cli,
249                                            uint16_t fnum,
250                                            off_t offset, size_t size)
251 {
252         struct tevent_req *req, *subreq;
253         struct cli_readall_state *state;
254
255         req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
256         if (req == NULL) {
257                 return NULL;
258         }
259         state->ev = ev;
260         state->cli = cli;
261         state->fnum = fnum;
262         state->start_offset = offset;
263         state->size = size;
264         state->received = 0;
265         state->buf = NULL;
266
267         subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
268         if (tevent_req_nomem(subreq, req)) {
269                 return tevent_req_post(req, ev);
270         }
271         tevent_req_set_callback(subreq, cli_readall_done, req);
272         return req;
273 }
274
275 static void cli_readall_done(struct tevent_req *subreq)
276 {
277         struct tevent_req *req = tevent_req_callback_data(
278                 subreq, struct tevent_req);
279         struct cli_readall_state *state = tevent_req_data(
280                 req, struct cli_readall_state);
281         ssize_t received;
282         uint8_t *buf;
283         NTSTATUS status;
284
285         status = cli_read_andx_recv(subreq, &received, &buf);
286         if (tevent_req_nterror(req, status)) {
287                 return;
288         }
289
290         if (received == 0) {
291                 /* EOF */
292                 tevent_req_done(req);
293                 return;
294         }
295
296         if ((state->received == 0) && (received == state->size)) {
297                 /* Ideal case: Got it all in one run */
298                 state->buf = buf;
299                 state->received += received;
300                 tevent_req_done(req);
301                 return;
302         }
303
304         /*
305          * We got a short read, issue a read for the
306          * rest. Unfortunately we have to allocate the buffer
307          * ourselves now, as our caller expects to receive a single
308          * buffer. cli_read_andx does it from the buffer received from
309          * the net, but with a short read we have to put it together
310          * from several reads.
311          */
312
313         if (state->buf == NULL) {
314                 state->buf = talloc_array(state, uint8_t, state->size);
315                 if (tevent_req_nomem(state->buf, req)) {
316                         return;
317                 }
318         }
319         memcpy(state->buf + state->received, buf, received);
320         state->received += received;
321
322         TALLOC_FREE(subreq);
323
324         if (state->received >= state->size) {
325                 tevent_req_done(req);
326                 return;
327         }
328
329         subreq = cli_read_andx_send(state, state->ev, state->cli, state->fnum,
330                                     state->start_offset + state->received,
331                                     state->size - state->received);
332         if (tevent_req_nomem(subreq, req)) {
333                 return;
334         }
335         tevent_req_set_callback(subreq, cli_readall_done, req);
336 }
337
338 static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
339                                  uint8_t **rcvbuf)
340 {
341         struct cli_readall_state *state = tevent_req_data(
342                 req, struct cli_readall_state);
343         NTSTATUS status;
344
345         if (tevent_req_is_nterror(req, &status)) {
346                 return status;
347         }
348         *received = state->received;
349         *rcvbuf = state->buf;
350         return NT_STATUS_OK;
351 }
352
353 struct cli_pull_subreq {
354         struct tevent_req *req;
355         ssize_t received;
356         uint8_t *buf;
357 };
358
359 /*
360  * Parallel read support.
361  *
362  * cli_pull sends as many read&x requests as the server would allow via
363  * max_mux at a time. When replies flow back in, the data is written into
364  * the callback function "sink" in the right order.
365  */
366
367 struct cli_pull_state {
368         struct tevent_req *req;
369
370         struct event_context *ev;
371         struct cli_state *cli;
372         uint16_t fnum;
373         off_t start_offset;
374         SMB_OFF_T size;
375
376         NTSTATUS (*sink)(char *buf, size_t n, void *priv);
377         void *priv;
378
379         size_t chunk_size;
380
381         /*
382          * Outstanding requests
383          */
384         int num_reqs;
385         struct cli_pull_subreq *reqs;
386
387         /*
388          * For how many bytes did we send requests already?
389          */
390         SMB_OFF_T requested;
391
392         /*
393          * Next request index to push into "sink". This walks around the "req"
394          * array, taking care that the requests are pushed to "sink" in the
395          * right order. If necessary (i.e. replies don't come in in the right
396          * order), replies are held back in "reqs".
397          */
398         int top_req;
399
400         /*
401          * How many bytes did we push into "sink"?
402          */
403
404         SMB_OFF_T pushed;
405 };
406
407 static char *cli_pull_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
408 {
409         struct cli_pull_state *state = tevent_req_data(
410                 req, struct cli_pull_state);
411         char *result;
412
413         result = tevent_req_default_print(req, mem_ctx);
414         if (result == NULL) {
415                 return NULL;
416         }
417
418         return talloc_asprintf_append_buffer(
419                 result, "num_reqs=%d, top_req=%d",
420                 state->num_reqs, state->top_req);
421 }
422
423 static void cli_pull_read_done(struct tevent_req *read_req);
424
425 /*
426  * Prepare an async pull request
427  */
428
429 struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
430                                  struct event_context *ev,
431                                  struct cli_state *cli,
432                                  uint16_t fnum, off_t start_offset,
433                                  SMB_OFF_T size, size_t window_size,
434                                  NTSTATUS (*sink)(char *buf, size_t n,
435                                                   void *priv),
436                                  void *priv)
437 {
438         struct tevent_req *req;
439         struct cli_pull_state *state;
440         int i;
441
442         req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
443         if (req == NULL) {
444                 return NULL;
445         }
446         tevent_req_set_print_fn(req, cli_pull_print);
447         state->req = req;
448
449         state->cli = cli;
450         state->ev = ev;
451         state->fnum = fnum;
452         state->start_offset = start_offset;
453         state->size = size;
454         state->sink = sink;
455         state->priv = priv;
456
457         state->pushed = 0;
458         state->top_req = 0;
459
460         if (size == 0) {
461                 tevent_req_done(req);
462                 return tevent_req_post(req, ev);
463         }
464
465         state->chunk_size = cli_read_max_bufsize(cli);
466
467         state->num_reqs = MAX(window_size/state->chunk_size, 1);
468         state->num_reqs = MIN(state->num_reqs, cli->max_mux);
469
470         state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_pull_subreq,
471                                         state->num_reqs);
472         if (state->reqs == NULL) {
473                 goto failed;
474         }
475
476         state->requested = 0;
477
478         for (i=0; i<state->num_reqs; i++) {
479                 struct cli_pull_subreq *subreq = &state->reqs[i];
480                 SMB_OFF_T size_left;
481                 size_t request_thistime;
482
483                 if (state->requested >= size) {
484                         state->num_reqs = i;
485                         break;
486                 }
487
488                 size_left = size - state->requested;
489                 request_thistime = MIN(size_left, state->chunk_size);
490
491                 subreq->req = cli_readall_send(
492                         state->reqs, ev, cli, fnum,
493                         state->start_offset + state->requested,
494                         request_thistime);
495
496                 if (subreq->req == NULL) {
497                         goto failed;
498                 }
499                 tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
500                 state->requested += request_thistime;
501         }
502         return req;
503
504 failed:
505         TALLOC_FREE(req);
506         return NULL;
507 }
508
509 /*
510  * Handle incoming read replies, push the data into sink and send out new
511  * requests if necessary.
512  */
513
514 static void cli_pull_read_done(struct tevent_req *subreq)
515 {
516         struct tevent_req *req = tevent_req_callback_data(
517                 subreq, struct tevent_req);
518         struct cli_pull_state *state = tevent_req_data(
519                 req, struct cli_pull_state);
520         struct cli_pull_subreq *pull_subreq = NULL;
521         NTSTATUS status;
522         int i;
523
524         for (i = 0; i < state->num_reqs; i++) {
525                 pull_subreq = &state->reqs[i];
526                 if (subreq == pull_subreq->req) {
527                         break;
528                 }
529         }
530         if (i == state->num_reqs) {
531                 /* Huh -- received something we did not send?? */
532                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
533                 return;
534         }
535
536         status = cli_readall_recv(subreq, &pull_subreq->received,
537                                   &pull_subreq->buf);
538         if (!NT_STATUS_IS_OK(status)) {
539                 tevent_req_nterror(state->req, status);
540                 return;
541         }
542
543         /*
544          * This loop is the one to take care of out-of-order replies. All
545          * pending requests are in state->reqs, state->reqs[top_req] is the
546          * one that is to be pushed next. If however a request later than
547          * top_req is replied to, then we can't push yet. If top_req is
548          * replied to at a later point then, we need to push all the finished
549          * requests.
550          */
551
552         while (state->reqs[state->top_req].req != NULL) {
553                 struct cli_pull_subreq *top_subreq;
554
555                 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
556                            state->top_req));
557
558                 top_subreq = &state->reqs[state->top_req];
559
560                 if (tevent_req_is_in_progress(top_subreq->req)) {
561                         DEBUG(11, ("cli_pull_read_done: top request not yet "
562                                    "done\n"));
563                         return;
564                 }
565
566                 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
567                            "pushed\n", (int)top_subreq->received,
568                            (int)state->pushed));
569
570                 status = state->sink((char *)top_subreq->buf,
571                                      top_subreq->received, state->priv);
572                 if (tevent_req_nterror(state->req, status)) {
573                         return;
574                 }
575                 state->pushed += top_subreq->received;
576
577                 TALLOC_FREE(state->reqs[state->top_req].req);
578
579                 if (state->requested < state->size) {
580                         struct tevent_req *new_req;
581                         SMB_OFF_T size_left;
582                         size_t request_thistime;
583
584                         size_left = state->size - state->requested;
585                         request_thistime = MIN(size_left, state->chunk_size);
586
587                         DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
588                                    "at %d, position %d\n",
589                                    (int)request_thistime,
590                                    (int)(state->start_offset
591                                          + state->requested),
592                                    state->top_req));
593
594                         new_req = cli_readall_send(
595                                 state->reqs, state->ev, state->cli,
596                                 state->fnum,
597                                 state->start_offset + state->requested,
598                                 request_thistime);
599
600                         if (tevent_req_nomem(new_req, state->req)) {
601                                 return;
602                         }
603                         tevent_req_set_callback(new_req, cli_pull_read_done,
604                                                 req);
605
606                         state->reqs[state->top_req].req = new_req;
607                         state->requested += request_thistime;
608                 }
609
610                 state->top_req = (state->top_req+1) % state->num_reqs;
611         }
612
613         tevent_req_done(req);
614 }
615
616 NTSTATUS cli_pull_recv(struct tevent_req *req, SMB_OFF_T *received)
617 {
618         struct cli_pull_state *state = tevent_req_data(
619                 req, struct cli_pull_state);
620         NTSTATUS status;
621
622         if (tevent_req_is_nterror(req, &status)) {
623                 return status;
624         }
625         *received = state->pushed;
626         return NT_STATUS_OK;
627 }
628
629 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
630                   off_t start_offset, SMB_OFF_T size, size_t window_size,
631                   NTSTATUS (*sink)(char *buf, size_t n, void *priv),
632                   void *priv, SMB_OFF_T *received)
633 {
634         TALLOC_CTX *frame = talloc_stackframe();
635         struct event_context *ev;
636         struct tevent_req *req;
637         NTSTATUS status = NT_STATUS_OK;
638
639         if (cli_has_async_calls(cli)) {
640                 /*
641                  * Can't use sync call while an async call is in flight
642                  */
643                 status = NT_STATUS_INVALID_PARAMETER;
644                 goto fail;
645         }
646
647         ev = event_context_init(frame);
648         if (ev == NULL) {
649                 status = NT_STATUS_NO_MEMORY;
650                 goto fail;
651         }
652
653         req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
654                             window_size, sink, priv);
655         if (req == NULL) {
656                 status = NT_STATUS_NO_MEMORY;
657                 goto fail;
658         }
659
660         if (!tevent_req_poll(req, ev)) {
661                 status = map_nt_error_from_unix(errno);
662                 goto fail;
663         }
664
665         status = cli_pull_recv(req, received);
666  fail:
667         TALLOC_FREE(frame);
668         return status;
669 }
670
671 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
672 {
673         char **pbuf = (char **)priv;
674         memcpy(*pbuf, buf, n);
675         *pbuf += n;
676         return NT_STATUS_OK;
677 }
678
679 ssize_t cli_read(struct cli_state *cli, uint16_t fnum, char *buf,
680                  off_t offset, size_t size)
681 {
682         NTSTATUS status;
683         SMB_OFF_T ret;
684
685         status = cli_pull(cli, fnum, offset, size, size,
686                           cli_read_sink, &buf, &ret);
687         if (!NT_STATUS_IS_OK(status)) {
688                 return -1;
689         }
690         return ret;
691 }
692
693 /****************************************************************************
694   write to a file using a SMBwrite and not bypassing 0 byte writes
695 ****************************************************************************/
696
697 NTSTATUS cli_smbwrite(struct cli_state *cli, uint16_t fnum, char *buf,
698                       off_t offset, size_t size1, size_t *ptotal)
699 {
700         uint8_t *bytes;
701         ssize_t total = 0;
702
703         /*
704          * 3 bytes prefix
705          */
706
707         bytes = talloc_array(talloc_tos(), uint8_t, 3);
708         if (bytes == NULL) {
709                 return NT_STATUS_NO_MEMORY;
710         }
711         bytes[0] = 1;
712
713         do {
714                 size_t size = MIN(size1, cli->max_xmit - 48);
715                 struct tevent_req *req;
716                 uint16_t vwv[5];
717                 uint16_t *ret_vwv;
718                 NTSTATUS status;
719
720                 SSVAL(vwv+0, 0, fnum);
721                 SSVAL(vwv+1, 0, size);
722                 SIVAL(vwv+2, 0, offset);
723                 SSVAL(vwv+4, 0, 0);
724
725                 bytes = talloc_realloc(talloc_tos(), bytes, uint8_t,
726                                              size+3);
727                 if (bytes == NULL) {
728                         return NT_STATUS_NO_MEMORY;
729                 }
730                 SSVAL(bytes, 1, size);
731                 memcpy(bytes + 3, buf + total, size);
732
733                 status = cli_smb(talloc_tos(), cli, SMBwrite, 0, 5, vwv,
734                                  size+3, bytes, &req, 1, NULL, &ret_vwv,
735                                  NULL, NULL);
736                 if (!NT_STATUS_IS_OK(status)) {
737                         TALLOC_FREE(bytes);
738                         return status;
739                 }
740
741                 size = SVAL(ret_vwv+0, 0);
742                 TALLOC_FREE(req);
743                 if (size == 0) {
744                         break;
745                 }
746                 size1 -= size;
747                 total += size;
748                 offset += size;
749
750         } while (size1);
751
752         TALLOC_FREE(bytes);
753
754         if (ptotal != NULL) {
755                 *ptotal = total;
756         }
757         return NT_STATUS_OK;
758 }
759
760 /*
761  * Send a write&x request
762  */
763
764 struct cli_write_andx_state {
765         size_t size;
766         uint16_t vwv[14];
767         size_t written;
768         uint8_t pad;
769         struct iovec iov[2];
770 };
771
772 static void cli_write_andx_done(struct tevent_req *subreq);
773
774 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
775                                          struct event_context *ev,
776                                          struct cli_state *cli, uint16_t fnum,
777                                          uint16_t mode, const uint8_t *buf,
778                                          off_t offset, size_t size,
779                                          struct tevent_req **reqs_before,
780                                          int num_reqs_before,
781                                          struct tevent_req **psmbreq)
782 {
783         struct tevent_req *req, *subreq;
784         struct cli_write_andx_state *state;
785         bool bigoffset = ((cli->capabilities & CAP_LARGE_FILES) != 0);
786         uint8_t wct = bigoffset ? 14 : 12;
787         size_t max_write = cli_write_max_bufsize(cli, mode);
788         uint16_t *vwv;
789
790         req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
791         if (req == NULL) {
792                 return NULL;
793         }
794
795         size = MIN(size, max_write);
796
797         vwv = state->vwv;
798
799         SCVAL(vwv+0, 0, 0xFF);
800         SCVAL(vwv+0, 1, 0);
801         SSVAL(vwv+1, 0, 0);
802         SSVAL(vwv+2, 0, fnum);
803         SIVAL(vwv+3, 0, offset);
804         SIVAL(vwv+5, 0, 0);
805         SSVAL(vwv+7, 0, mode);
806         SSVAL(vwv+8, 0, 0);
807         SSVAL(vwv+9, 0, (size>>16));
808         SSVAL(vwv+10, 0, size);
809
810         SSVAL(vwv+11, 0,
811               cli_smb_wct_ofs(reqs_before, num_reqs_before)
812               + 1               /* the wct field */
813               + wct * 2         /* vwv */
814               + 2               /* num_bytes field */
815               + 1               /* pad */);
816
817         if (bigoffset) {
818                 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
819         }
820
821         state->pad = 0;
822         state->iov[0].iov_base = (void *)&state->pad;
823         state->iov[0].iov_len = 1;
824         state->iov[1].iov_base = discard_const_p(void, buf);
825         state->iov[1].iov_len = size;
826
827         subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
828                                     2, state->iov);
829         if (tevent_req_nomem(subreq, req)) {
830                 return tevent_req_post(req, ev);
831         }
832         tevent_req_set_callback(subreq, cli_write_andx_done, req);
833         *psmbreq = subreq;
834         return req;
835 }
836
837 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
838                                        struct event_context *ev,
839                                        struct cli_state *cli, uint16_t fnum,
840                                        uint16_t mode, const uint8_t *buf,
841                                        off_t offset, size_t size)
842 {
843         struct tevent_req *req, *subreq;
844         NTSTATUS status;
845
846         req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
847                                     size, NULL, 0, &subreq);
848         if (req == NULL) {
849                 return NULL;
850         }
851
852         status = cli_smb_req_send(subreq);
853         if (tevent_req_nterror(req, status)) {
854                 return tevent_req_post(req, ev);
855         }
856         return req;
857 }
858
859 static void cli_write_andx_done(struct tevent_req *subreq)
860 {
861         struct tevent_req *req = tevent_req_callback_data(
862                 subreq, struct tevent_req);
863         struct cli_write_andx_state *state = tevent_req_data(
864                 req, struct cli_write_andx_state);
865         uint8_t wct;
866         uint16_t *vwv;
867         uint8_t *inbuf;
868         NTSTATUS status;
869
870         status = cli_smb_recv(subreq, state, &inbuf, 6, &wct, &vwv,
871                               NULL, NULL);
872         TALLOC_FREE(subreq);
873         if (NT_STATUS_IS_ERR(status)) {
874                 tevent_req_nterror(req, status);
875                 return;
876         }
877         state->written = SVAL(vwv+2, 0);
878         state->written |= SVAL(vwv+4, 0)<<16;
879         tevent_req_done(req);
880 }
881
882 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
883 {
884         struct cli_write_andx_state *state = tevent_req_data(
885                 req, struct cli_write_andx_state);
886         NTSTATUS status;
887
888         if (tevent_req_is_nterror(req, &status)) {
889                 return status;
890         }
891         *pwritten = state->written;
892         return NT_STATUS_OK;
893 }
894
895 struct cli_writeall_state {
896         struct event_context *ev;
897         struct cli_state *cli;
898         uint16_t fnum;
899         uint16_t mode;
900         const uint8_t *buf;
901         off_t offset;
902         size_t size;
903         size_t written;
904 };
905
906 static void cli_writeall_written(struct tevent_req *req);
907
908 static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
909                                             struct event_context *ev,
910                                             struct cli_state *cli,
911                                             uint16_t fnum,
912                                             uint16_t mode,
913                                             const uint8_t *buf,
914                                             off_t offset, size_t size)
915 {
916         struct tevent_req *req, *subreq;
917         struct cli_writeall_state *state;
918
919         req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
920         if (req == NULL) {
921                 return NULL;
922         }
923         state->ev = ev;
924         state->cli = cli;
925         state->fnum = fnum;
926         state->mode = mode;
927         state->buf = buf;
928         state->offset = offset;
929         state->size = size;
930         state->written = 0;
931
932         subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
933                                      state->mode, state->buf, state->offset,
934                                      state->size);
935         if (tevent_req_nomem(subreq, req)) {
936                 return tevent_req_post(req, ev);
937         }
938         tevent_req_set_callback(subreq, cli_writeall_written, req);
939         return req;
940 }
941
942 static void cli_writeall_written(struct tevent_req *subreq)
943 {
944         struct tevent_req *req = tevent_req_callback_data(
945                 subreq, struct tevent_req);
946         struct cli_writeall_state *state = tevent_req_data(
947                 req, struct cli_writeall_state);
948         NTSTATUS status;
949         size_t written, to_write;
950
951         status = cli_write_andx_recv(subreq, &written);
952         TALLOC_FREE(subreq);
953         if (tevent_req_nterror(req, status)) {
954                 return;
955         }
956
957         state->written += written;
958
959         if (state->written > state->size) {
960                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
961                 return;
962         }
963
964         to_write = state->size - state->written;
965
966         if (to_write == 0) {
967                 tevent_req_done(req);
968                 return;
969         }
970
971         subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
972                                      state->mode,
973                                      state->buf + state->written,
974                                      state->offset + state->written, to_write);
975         if (tevent_req_nomem(subreq, req)) {
976                 return;
977         }
978         tevent_req_set_callback(subreq, cli_writeall_written, req);
979 }
980
981 static NTSTATUS cli_writeall_recv(struct tevent_req *req,
982                                   size_t *pwritten)
983 {
984         struct cli_writeall_state *state = tevent_req_data(
985                 req, struct cli_writeall_state);
986         NTSTATUS status;
987
988         if (tevent_req_is_nterror(req, &status)) {
989                 return status;
990         }
991         if (pwritten != NULL) {
992                 *pwritten = state->written;
993         }
994         return NT_STATUS_OK;
995 }
996
997 NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
998                       const uint8_t *buf, off_t offset, size_t size,
999                       size_t *pwritten)
1000 {
1001         TALLOC_CTX *frame = talloc_stackframe();
1002         struct event_context *ev;
1003         struct tevent_req *req;
1004         NTSTATUS status = NT_STATUS_NO_MEMORY;
1005
1006         if (cli_has_async_calls(cli)) {
1007                 /*
1008                  * Can't use sync call while an async call is in flight
1009                  */
1010                 status = NT_STATUS_INVALID_PARAMETER;
1011                 goto fail;
1012         }
1013         ev = event_context_init(frame);
1014         if (ev == NULL) {
1015                 goto fail;
1016         }
1017         req = cli_writeall_send(frame, ev, cli, fnum, mode, buf, offset, size);
1018         if (req == NULL) {
1019                 goto fail;
1020         }
1021         if (!tevent_req_poll(req, ev)) {
1022                 status = map_nt_error_from_unix(errno);
1023                 goto fail;
1024         }
1025         status = cli_writeall_recv(req, pwritten);
1026  fail:
1027         TALLOC_FREE(frame);
1028         return status;
1029 }
1030
1031 struct cli_push_write_state {
1032         struct tevent_req *req;/* This is the main request! Not the subreq */
1033         uint32_t idx;
1034         off_t ofs;
1035         uint8_t *buf;
1036         size_t size;
1037 };
1038
1039 struct cli_push_state {
1040         struct event_context *ev;
1041         struct cli_state *cli;
1042         uint16_t fnum;
1043         uint16_t mode;
1044         off_t start_offset;
1045         size_t window_size;
1046
1047         size_t (*source)(uint8_t *buf, size_t n, void *priv);
1048         void *priv;
1049
1050         bool eof;
1051
1052         size_t chunk_size;
1053         off_t next_offset;
1054
1055         /*
1056          * Outstanding requests
1057          */
1058         uint32_t pending;
1059         uint32_t num_reqs;
1060         struct cli_push_write_state **reqs;
1061 };
1062
1063 static void cli_push_written(struct tevent_req *req);
1064
1065 static bool cli_push_write_setup(struct tevent_req *req,
1066                                  struct cli_push_state *state,
1067                                  uint32_t idx)
1068 {
1069         struct cli_push_write_state *substate;
1070         struct tevent_req *subreq;
1071
1072         substate = talloc(state->reqs, struct cli_push_write_state);
1073         if (!substate) {
1074                 return false;
1075         }
1076         substate->req = req;
1077         substate->idx = idx;
1078         substate->ofs = state->next_offset;
1079         substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
1080         if (!substate->buf) {
1081                 talloc_free(substate);
1082                 return false;
1083         }
1084         substate->size = state->source(substate->buf,
1085                                        state->chunk_size,
1086                                        state->priv);
1087         if (substate->size == 0) {
1088                 state->eof = true;
1089                 /* nothing to send */
1090                 talloc_free(substate);
1091                 return true;
1092         }
1093
1094         subreq = cli_writeall_send(substate,
1095                                    state->ev, state->cli,
1096                                    state->fnum, state->mode,
1097                                    substate->buf,
1098                                    substate->ofs,
1099                                    substate->size);
1100         if (!subreq) {
1101                 talloc_free(substate);
1102                 return false;
1103         }
1104         tevent_req_set_callback(subreq, cli_push_written, substate);
1105
1106         state->reqs[idx] = substate;
1107         state->pending += 1;
1108         state->next_offset += substate->size;
1109
1110         return true;
1111 }
1112
1113 struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1114                                  struct cli_state *cli,
1115                                  uint16_t fnum, uint16_t mode,
1116                                  off_t start_offset, size_t window_size,
1117                                  size_t (*source)(uint8_t *buf, size_t n,
1118                                                   void *priv),
1119                                  void *priv)
1120 {
1121         struct tevent_req *req;
1122         struct cli_push_state *state;
1123         uint32_t i;
1124
1125         req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
1126         if (req == NULL) {
1127                 return NULL;
1128         }
1129         state->cli = cli;
1130         state->ev = ev;
1131         state->fnum = fnum;
1132         state->start_offset = start_offset;
1133         state->mode = mode;
1134         state->source = source;
1135         state->priv = priv;
1136         state->eof = false;
1137         state->pending = 0;
1138         state->next_offset = start_offset;
1139
1140         state->chunk_size = cli_write_max_bufsize(cli, mode);
1141
1142         if (window_size == 0) {
1143                 window_size = cli->max_mux * state->chunk_size;
1144         }
1145         state->num_reqs = window_size/state->chunk_size;
1146         if ((window_size % state->chunk_size) > 0) {
1147                 state->num_reqs += 1;
1148         }
1149         state->num_reqs = MIN(state->num_reqs, cli->max_mux);
1150         state->num_reqs = MAX(state->num_reqs, 1);
1151
1152         state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_push_write_state *,
1153                                         state->num_reqs);
1154         if (state->reqs == NULL) {
1155                 goto failed;
1156         }
1157
1158         for (i=0; i<state->num_reqs; i++) {
1159                 if (!cli_push_write_setup(req, state, i)) {
1160                         goto failed;
1161                 }
1162
1163                 if (state->eof) {
1164                         break;
1165                 }
1166         }
1167
1168         if (state->pending == 0) {
1169                 tevent_req_done(req);
1170                 return tevent_req_post(req, ev);
1171         }
1172
1173         return req;
1174
1175  failed:
1176         tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1177         return tevent_req_post(req, ev);
1178 }
1179
1180 static void cli_push_written(struct tevent_req *subreq)
1181 {
1182         struct cli_push_write_state *substate = tevent_req_callback_data(
1183                 subreq, struct cli_push_write_state);
1184         struct tevent_req *req = substate->req;
1185         struct cli_push_state *state = tevent_req_data(
1186                 req, struct cli_push_state);
1187         NTSTATUS status;
1188         uint32_t idx = substate->idx;
1189
1190         state->reqs[idx] = NULL;
1191         state->pending -= 1;
1192
1193         status = cli_writeall_recv(subreq, NULL);
1194         TALLOC_FREE(subreq);
1195         TALLOC_FREE(substate);
1196         if (tevent_req_nterror(req, status)) {
1197                 return;
1198         }
1199
1200         if (!state->eof) {
1201                 if (!cli_push_write_setup(req, state, idx)) {
1202                         tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1203                         return;
1204                 }
1205         }
1206
1207         if (state->pending == 0) {
1208                 tevent_req_done(req);
1209                 return;
1210         }
1211 }
1212
1213 NTSTATUS cli_push_recv(struct tevent_req *req)
1214 {
1215         return tevent_req_simple_recv_ntstatus(req);
1216 }
1217
1218 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1219                   off_t start_offset, size_t window_size,
1220                   size_t (*source)(uint8_t *buf, size_t n, void *priv),
1221                   void *priv)
1222 {
1223         TALLOC_CTX *frame = talloc_stackframe();
1224         struct event_context *ev;
1225         struct tevent_req *req;
1226         NTSTATUS status = NT_STATUS_OK;
1227
1228         if (cli_has_async_calls(cli)) {
1229                 /*
1230                  * Can't use sync call while an async call is in flight
1231                  */
1232                 status = NT_STATUS_INVALID_PARAMETER;
1233                 goto fail;
1234         }
1235
1236         ev = event_context_init(frame);
1237         if (ev == NULL) {
1238                 status = NT_STATUS_NO_MEMORY;
1239                 goto fail;
1240         }
1241
1242         req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1243                             window_size, source, priv);
1244         if (req == NULL) {
1245                 status = NT_STATUS_NO_MEMORY;
1246                 goto fail;
1247         }
1248
1249         if (!tevent_req_poll(req, ev)) {
1250                 status = map_nt_error_from_unix(errno);
1251                 goto fail;
1252         }
1253
1254         status = cli_push_recv(req);
1255  fail:
1256         TALLOC_FREE(frame);
1257         return status;
1258 }