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