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