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