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