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