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