Add async cli_pull support
[samba.git] / source3 / libsmb / clireadwrite.c
1 /*
2    Unix SMB/CIFS implementation.
3    client file read/write routines
4    Copyright (C) Andrew Tridgell 1994-1998
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 /****************************************************************************
23   Calculate the recommended read buffer size
24 ****************************************************************************/
25 static size_t cli_read_max_bufsize(struct cli_state *cli)
26 {
27         if (!client_is_signing_on(cli) && !cli_encryption_on(cli) == false
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  * Send a read&x request
41  */
42
43 struct async_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
44                                      struct cli_state *cli, int fnum,
45                                      off_t offset, size_t size)
46 {
47         struct async_req *result;
48         struct cli_request *req;
49         bool bigoffset = False;
50         char *enc_buf;
51
52         if (size > cli_read_max_bufsize(cli)) {
53                 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
54                           "size=%d\n", (int)size,
55                           (int)cli_read_max_bufsize(cli)));
56                 return NULL;
57         }
58
59         result = cli_request_new(mem_ctx, cli->event_ctx, cli, 12, 0, &req);
60         if (result == NULL) {
61                 DEBUG(0, ("cli_request_new failed\n"));
62                 return NULL;
63         }
64
65         req = cli_request_get(result);
66
67         req->data.read.ofs = offset;
68         req->data.read.size = size;
69         req->data.read.received = 0;
70         req->data.read.rcvbuf = NULL;
71
72         if ((SMB_BIG_UINT)offset >> 32)
73                 bigoffset = True;
74
75         cli_set_message(req->outbuf, bigoffset ? 12 : 10, 0, False);
76
77         SCVAL(req->outbuf,smb_com,SMBreadX);
78         SSVAL(req->outbuf,smb_tid,cli->cnum);
79         cli_setup_packet_buf(cli, req->outbuf);
80
81         SCVAL(req->outbuf,smb_vwv0,0xFF);
82         SCVAL(req->outbuf,smb_vwv0+1,0);
83         SSVAL(req->outbuf,smb_vwv1,0);
84         SSVAL(req->outbuf,smb_vwv2,fnum);
85         SIVAL(req->outbuf,smb_vwv3,offset);
86         SSVAL(req->outbuf,smb_vwv5,size);
87         SSVAL(req->outbuf,smb_vwv6,size);
88         SSVAL(req->outbuf,smb_vwv7,(size >> 16));
89         SSVAL(req->outbuf,smb_vwv8,0);
90         SSVAL(req->outbuf,smb_vwv9,0);
91         SSVAL(req->outbuf,smb_mid,req->mid);
92
93         if (bigoffset) {
94                 SIVAL(req->outbuf, smb_vwv10,
95                       (((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
96         }
97
98         cli_calculate_sign_mac(cli, req->outbuf);
99
100         event_fd_set_writeable(cli->fd_event);
101
102         if (cli_encryption_on(cli)) {
103                 NTSTATUS status;
104                 status = cli_encrypt_message(cli, req->outbuf, &enc_buf);
105                 if (!NT_STATUS_IS_OK(status)) {
106                         DEBUG(0, ("Error in encrypting client message. "
107                                   "Error %s\n", nt_errstr(status)));
108                         TALLOC_FREE(req);
109                         return NULL;
110                 }
111                 req->outbuf = enc_buf;
112                 req->enc_state = cli->trans_enc_state;
113         }
114
115         return result;
116 }
117
118 /*
119  * Pull the data out of a finished async read_and_x request. rcvbuf is
120  * talloced from the request, so better make sure that you copy it away before
121  * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
122  * talloc_move it!
123  */
124
125 NTSTATUS cli_read_andx_recv(struct async_req *req, ssize_t *received,
126                             uint8_t **rcvbuf)
127 {
128         struct cli_request *cli_req = cli_request_get(req);
129         NTSTATUS status;
130         size_t size;
131
132         SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
133         if (req->state == ASYNC_REQ_ERROR) {
134                 return req->status;
135         }
136
137         status = cli_pull_error(cli_req->inbuf);
138
139         if (!NT_STATUS_IS_OK(status)) {
140                 return status;
141         }
142
143         /* size is the number of bytes the server returned.
144          * Might be zero. */
145         size = SVAL(cli_req->inbuf, smb_vwv5);
146         size |= (((unsigned int)(SVAL(cli_req->inbuf, smb_vwv7))) << 16);
147
148         if (size > cli_req->data.read.size) {
149                 DEBUG(5,("server returned more than we wanted!\n"));
150                 return NT_STATUS_UNEXPECTED_IO_ERROR;
151         }
152
153         if (size < 0) {
154                 DEBUG(5,("read return < 0!\n"));
155                 return NT_STATUS_UNEXPECTED_IO_ERROR;
156         }
157
158         *rcvbuf = (uint8_t *)
159                 (smb_base(cli_req->inbuf) + SVAL(cli_req->inbuf, smb_vwv6));
160         *received = size;
161         return NT_STATUS_OK;
162 }
163
164 /*
165  * Parallel read support.
166  *
167  * cli_pull sends as many read&x requests as the server would allow via
168  * max_mux at a time. When replies flow back in, the data is written into
169  * the callback function "sink" in the right order.
170  */
171
172 struct cli_pull_state {
173         struct async_req *req;
174
175         struct cli_state *cli;
176         uint16_t fnum;
177         off_t start_offset;
178         size_t size;
179
180         NTSTATUS (*sink)(char *buf, size_t n, void *priv);
181         void *priv;
182
183         size_t chunk_size;
184
185         /*
186          * Outstanding requests
187          */
188         int num_reqs;
189         struct async_req **reqs;
190
191         /*
192          * For how many bytes did we send requests already?
193          */
194         off_t requested;
195
196         /*
197          * Next request index to push into "sink". This walks around the "req"
198          * array, taking care that the requests are pushed to "sink" in the
199          * right order. If necessary (i.e. replies don't come in in the right
200          * order), replies are held back in "reqs".
201          */
202         int top_req;
203
204         /*
205          * How many bytes did we push into "sink"?
206          */
207
208         off_t pushed;
209 };
210
211 static char *cli_pull_print(TALLOC_CTX *mem_ctx, struct async_req *req)
212 {
213         struct cli_pull_state *state = talloc_get_type_abort(
214                 req->private_data, struct cli_pull_state);
215         char *result;
216
217         result = async_req_print(mem_ctx, req);
218         if (result == NULL) {
219                 return NULL;
220         }
221
222         return talloc_asprintf_append_buffer(
223                 result, "num_reqs=%d, top_req=%d",
224                 state->num_reqs, state->top_req);
225 }
226
227 static void cli_pull_read_done(struct async_req *read_req);
228
229 /*
230  * Prepare an async pull request
231  */
232
233 struct async_req *cli_pull_send(TALLOC_CTX *mem_ctx, struct cli_state *cli,
234                                 uint16_t fnum, off_t start_offset,
235                                 size_t size, size_t window_size,
236                                 NTSTATUS (*sink)(char *buf, size_t n,
237                                                  void *priv),
238                                 void *priv)
239 {
240         struct async_req *result;
241         struct cli_pull_state *state;
242         int i;
243
244         result = async_req_new(mem_ctx, cli->event_ctx);
245         if (result == NULL) {
246                 goto failed;
247         }
248         state = talloc(result, struct cli_pull_state);
249         if (state == NULL) {
250                 goto failed;
251         }
252         result->private_data = state;
253         result->print = cli_pull_print;
254         state->req = result;
255
256         state->cli = cli;
257         state->fnum = fnum;
258         state->start_offset = start_offset;
259         state->size = size;
260         state->sink = sink;
261         state->priv = priv;
262
263         state->pushed = 0;
264         state->top_req = 0;
265         state->chunk_size = cli_read_max_bufsize(cli);
266
267         state->num_reqs = MAX(window_size/state->chunk_size, 1);
268         state->num_reqs = MIN(state->num_reqs, cli->max_mux);
269
270         state->reqs = TALLOC_ZERO_ARRAY(state, struct async_req *,
271                                         state->num_reqs);
272         if (state->reqs == NULL) {
273                 goto failed;
274         }
275
276         state->requested = 0;
277
278         for (i=0; i<state->num_reqs; i++) {
279                 size_t size_left, request_thistime;
280
281                 if (state->requested >= size) {
282                         state->num_reqs = i;
283                         break;
284                 }
285
286                 size_left = size - state->requested;
287                 request_thistime = MIN(size_left, state->chunk_size);
288
289                 state->reqs[i] = cli_read_andx_send(
290                         state->reqs, cli, fnum,
291                         state->start_offset + state->requested,
292                         request_thistime);
293
294                 if (state->reqs[i] == NULL) {
295                         goto failed;
296                 }
297
298                 state->reqs[i]->async.fn = cli_pull_read_done;
299                 state->reqs[i]->async.priv = result;
300
301                 state->requested += request_thistime;
302         }
303         return result;
304
305 failed:
306         TALLOC_FREE(result);
307         return NULL;
308 }
309
310 /*
311  * Handle incoming read replies, push the data into sink and send out new
312  * requests if necessary.
313  */
314
315 static void cli_pull_read_done(struct async_req *read_req)
316 {
317         struct async_req *pull_req = talloc_get_type_abort(
318                 read_req->async.priv, struct async_req);
319         struct cli_pull_state *state = talloc_get_type_abort(
320                 pull_req->private_data, struct cli_pull_state);
321         struct cli_request *read_state = cli_request_get(read_req);
322         NTSTATUS status;
323
324         status = cli_read_andx_recv(read_req, &read_state->data.read.received,
325                                     &read_state->data.read.rcvbuf);
326         if (!NT_STATUS_IS_OK(status)) {
327                 async_req_error(state->req, status);
328                 return;
329         }
330
331         /*
332          * This loop is the one to take care of out-of-order replies. All
333          * pending requests are in state->reqs, state->reqs[top_req] is the
334          * one that is to be pushed next. If however a request later than
335          * top_req is replied to, then we can't push yet. If top_req is
336          * replied to at a later point then, we need to push all the finished
337          * requests.
338          */
339
340         while (state->reqs[state->top_req] != NULL) {
341                 struct cli_request *top_read;
342
343                 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
344                            state->top_req));
345
346                 if (state->reqs[state->top_req]->state < ASYNC_REQ_DONE) {
347                         DEBUG(11, ("cli_pull_read_done: top request not yet "
348                                    "done\n"));
349                         return;
350                 }
351
352                 top_read = cli_request_get(state->reqs[state->top_req]);
353
354                 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
355                            "pushed\n", (int)top_read->data.read.received,
356                            (int)state->pushed));
357
358                 status = state->sink((char *)top_read->data.read.rcvbuf,
359                                      top_read->data.read.received,
360                                      state->priv);
361                 if (!NT_STATUS_IS_OK(status)) {
362                         async_req_error(state->req, status);
363                         return;
364                 }
365                 state->pushed += top_read->data.read.received;
366
367                 TALLOC_FREE(state->reqs[state->top_req]);
368
369                 if (state->requested < state->size) {
370                         struct async_req *new_req;
371                         size_t size_left, request_thistime;
372
373                         size_left = state->size - state->requested;
374                         request_thistime = MIN(size_left, state->chunk_size);
375
376                         DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
377                                    "at %d, position %d\n",
378                                    (int)request_thistime,
379                                    (int)(state->start_offset
380                                          + state->requested),
381                                    state->top_req));
382
383                         new_req = cli_read_andx_send(
384                                 state->reqs, state->cli, state->fnum,
385                                 state->start_offset + state->requested,
386                                 request_thistime);
387
388                         if (async_req_nomem(new_req, state->req)) {
389                                 return;
390                         }
391
392                         new_req->async.fn = cli_pull_read_done;
393                         new_req->async.priv = pull_req;
394
395                         state->reqs[state->top_req] = new_req;
396                         state->requested += request_thistime;
397                 }
398
399                 state->top_req = (state->top_req+1) % state->num_reqs;
400         }
401
402         async_req_done(pull_req);
403 }
404
405 NTSTATUS cli_pull_recv(struct async_req *req, ssize_t *received)
406 {
407         struct cli_pull_state *state = talloc_get_type_abort(
408                 req->private_data, struct cli_pull_state);
409
410         SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
411         if (req->state == ASYNC_REQ_ERROR) {
412                 return req->status;
413         }
414         *received = state->pushed;
415         return NT_STATUS_OK;
416 }
417
418 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
419                   off_t start_offset, size_t size, size_t window_size,
420                   NTSTATUS (*sink)(char *buf, size_t n, void *priv),
421                   void *priv, ssize_t *received)
422 {
423         TALLOC_CTX *frame = talloc_stackframe();
424         struct async_req *req;
425         NTSTATUS result = NT_STATUS_NO_MEMORY;
426
427         if (cli_tmp_event_ctx(frame, cli) == NULL) {
428                 goto nomem;
429         }
430
431         req = cli_pull_send(frame, cli, fnum, start_offset, size, window_size,
432                             sink, priv);
433         if (req == NULL) {
434                 goto nomem;
435         }
436
437         while (req->state < ASYNC_REQ_DONE) {
438                 event_loop_once(cli->event_ctx);
439         }
440
441         result = cli_pull_recv(req, received);
442  nomem:
443         TALLOC_FREE(frame);
444         return result;
445 }
446
447 /****************************************************************************
448 Issue a single SMBread and don't wait for a reply.
449 ****************************************************************************/
450
451 static bool cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
452                            size_t size, int i)
453 {
454         bool bigoffset = False;
455
456         memset(cli->outbuf,'\0',smb_size);
457         memset(cli->inbuf,'\0',smb_size);
458
459         if ((SMB_BIG_UINT)offset >> 32)
460                 bigoffset = True;
461
462         cli_set_message(cli->outbuf,bigoffset ? 12 : 10,0,True);
463
464         SCVAL(cli->outbuf,smb_com,SMBreadX);
465         SSVAL(cli->outbuf,smb_tid,cli->cnum);
466         cli_setup_packet(cli);
467
468         SCVAL(cli->outbuf,smb_vwv0,0xFF);
469         SSVAL(cli->outbuf,smb_vwv2,fnum);
470         SIVAL(cli->outbuf,smb_vwv3,offset);
471         SSVAL(cli->outbuf,smb_vwv5,size);
472         SSVAL(cli->outbuf,smb_vwv6,size);
473         SSVAL(cli->outbuf,smb_vwv7,(size >> 16));
474         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
475
476         if (bigoffset) {
477                 SIVAL(cli->outbuf,smb_vwv10,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
478         }
479
480         return cli_send_smb(cli);
481 }
482
483 /****************************************************************************
484   Read size bytes at offset offset using SMBreadX.
485 ****************************************************************************/
486
487 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
488 {
489         char *p;
490         size_t size2;
491         size_t readsize;
492         ssize_t total = 0;
493         /* We can only do direct reads if not signing or encrypting. */
494         bool direct_reads = !client_is_signing_on(cli) && !cli_encryption_on(cli);
495
496         if (size == 0)
497                 return 0;
498
499         /*
500          * Set readsize to the maximum size we can handle in one readX,
501          * rounded down to a multiple of 1024.
502          */
503
504         if (client_is_signing_on(cli) == false &&
505                         cli_encryption_on(cli) == false &&
506                         (cli->posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
507                 readsize = CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
508         } else if (cli->capabilities & CAP_LARGE_READX) {
509                 if (cli->is_samba) {
510                         readsize = CLI_SAMBA_MAX_LARGE_READX_SIZE;
511                 } else {
512                         readsize = CLI_WINDOWS_MAX_LARGE_READX_SIZE;
513                 }
514         } else {
515                 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
516         }
517
518         while (total < size) {
519                 readsize = MIN(readsize, size-total);
520
521                 /* Issue a read and receive a reply */
522
523                 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
524                         return -1;
525
526                 if (direct_reads) {
527                         if (!cli_receive_smb_readX_header(cli))
528                                 return -1;
529                 } else {
530                         if (!cli_receive_smb(cli))
531                                 return -1;
532                 }
533
534                 /* Check for error.  Make sure to check for DOS and NT
535                    errors. */
536
537                 if (cli_is_error(cli)) {
538                         bool recoverable_error = False;
539                         NTSTATUS status = NT_STATUS_OK;
540                         uint8 eclass = 0;
541                         uint32 ecode = 0;
542
543                         if (cli_is_nt_error(cli))
544                                 status = cli_nt_error(cli);
545                         else
546                                 cli_dos_error(cli, &eclass, &ecode);
547
548                         /*
549                          * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
550                          * recoverable error, plus we have valid data in the
551                          * packet so don't error out here.
552                          */
553
554                         if ((eclass == ERRDOS && ecode == ERRmoredata) ||
555                             NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
556                                 recoverable_error = True;
557
558                         if (!recoverable_error)
559                                 return -1;
560                 }
561
562                 /* size2 is the number of bytes the server returned.
563                  * Might be zero. */
564                 size2 = SVAL(cli->inbuf, smb_vwv5);
565                 size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7))) << 16);
566
567                 if (size2 > readsize) {
568                         DEBUG(5,("server returned more than we wanted!\n"));
569                         return -1;
570                 } else if (size2 < 0) {
571                         DEBUG(5,("read return < 0!\n"));
572                         return -1;
573                 }
574
575                 if (size2) {
576                         /* smb_vwv6 is the offset in the packet of the returned
577                          * data bytes. Only valid if size2 != 0. */
578
579                         if (!direct_reads) {
580                                 /* Copy data into buffer */
581                                 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
582                                 memcpy(buf + total, p, size2);
583                         } else {
584                                 /* Ensure the remaining data matches the return size. */
585                                 ssize_t toread = smb_len_large(cli->inbuf) - SVAL(cli->inbuf,smb_vwv6);
586
587                                 /* Ensure the size is correct. */
588                                 if (toread != size2) {
589                                         DEBUG(5,("direct read logic fail toread (%d) != size2 (%u)\n",
590                                                 (int)toread, (unsigned int)size2 ));
591                                         return -1;
592                                 }
593
594                                 /* Read data directly into buffer */
595                                 toread = cli_receive_smb_data(cli,buf+total,size2);
596                                 if (toread != size2) {
597                                         DEBUG(5,("direct read read failure toread (%d) != size2 (%u)\n",
598                                                 (int)toread, (unsigned int)size2 ));
599                                         return -1;
600                                 }
601                         }
602                 }
603
604                 total += size2;
605                 offset += size2;
606
607                 /*
608                  * If the server returned less than we asked for we're at EOF.
609                  */
610
611                 if (size2 < readsize)
612                         break;
613         }
614
615         return total;
616 }
617
618 #if 0  /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
619
620 /* This call is INCOMPATIBLE with SMB signing.  If you remove the #if 0
621    you must fix ensure you don't attempt to sign the packets - data
622    *will* be currupted */
623
624 /****************************************************************************
625 Issue a single SMBreadraw and don't wait for a reply.
626 ****************************************************************************/
627
628 static bool cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset, 
629                            size_t size, int i)
630 {
631
632         if (!cli->sign_info.use_smb_signing) {
633                 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
634                 return False;
635         }
636         
637         memset(cli->outbuf,'\0',smb_size);
638         memset(cli->inbuf,'\0',smb_size);
639
640         cli_set_message(cli->outbuf,10,0,True);
641                 
642         SCVAL(cli->outbuf,smb_com,SMBreadbraw);
643         SSVAL(cli->outbuf,smb_tid,cli->cnum);
644         cli_setup_packet(cli);
645
646         SSVAL(cli->outbuf,smb_vwv0,fnum);
647         SIVAL(cli->outbuf,smb_vwv1,offset);
648         SSVAL(cli->outbuf,smb_vwv2,size);
649         SSVAL(cli->outbuf,smb_vwv3,size);
650         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
651
652         return cli_send_smb(cli);
653 }
654
655 /****************************************************************************
656  Tester for the readraw call.
657 ****************************************************************************/
658
659 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
660 {
661         char *p;
662         int size2;
663         size_t readsize;
664         ssize_t total = 0;
665
666         if (size == 0) 
667                 return 0;
668
669         /*
670          * Set readsize to the maximum size we can handle in one readraw.
671          */
672
673         readsize = 0xFFFF;
674
675         while (total < size) {
676                 readsize = MIN(readsize, size-total);
677
678                 /* Issue a read and receive a reply */
679
680                 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
681                         return -1;
682
683                 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
684                         return -1;
685
686                 size2 = smb_len(cli->inbuf);
687
688                 if (size2 > readsize) {
689                         DEBUG(5,("server returned more than we wanted!\n"));
690                         return -1;
691                 } else if (size2 < 0) {
692                         DEBUG(5,("read return < 0!\n"));
693                         return -1;
694                 }
695
696                 /* Copy data into buffer */
697
698                 if (size2) {
699                         p = cli->inbuf + 4;
700                         memcpy(buf + total, p, size2);
701                 }
702
703                 total += size2;
704                 offset += size2;
705
706                 /*
707                  * If the server returned less than we asked for we're at EOF.
708                  */
709
710                 if (size2 < readsize)
711                         break;
712         }
713
714         return total;
715 }
716 #endif
717
718 /****************************************************************************
719  Issue a single SMBwrite and don't wait for a reply.
720 ****************************************************************************/
721
722 static bool cli_issue_write(struct cli_state *cli,
723                                 int fnum,
724                                 off_t offset,
725                                 uint16 mode,
726                                 const char *buf,
727                                 size_t size,
728                                 int i)
729 {
730         char *p;
731         bool large_writex = false;
732         /* We can only do direct writes if not signing and not encrypting. */
733         bool direct_writes = !client_is_signing_on(cli) && !cli_encryption_on(cli);
734
735         if (!direct_writes && size + 1 > cli->bufsize) {
736                 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
737                 if (!cli->outbuf) {
738                         return False;
739                 }
740                 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
741                 if (cli->inbuf == NULL) {
742                         SAFE_FREE(cli->outbuf);
743                         return False;
744                 }
745                 cli->bufsize = size + 1024;
746         }
747
748         memset(cli->outbuf,'\0',smb_size);
749         memset(cli->inbuf,'\0',smb_size);
750
751         if (cli->capabilities & CAP_LARGE_FILES) {
752                 large_writex = True;
753         }
754
755         if (large_writex) {
756                 cli_set_message(cli->outbuf,14,0,True);
757         } else {
758                 cli_set_message(cli->outbuf,12,0,True);
759         }
760
761         SCVAL(cli->outbuf,smb_com,SMBwriteX);
762         SSVAL(cli->outbuf,smb_tid,cli->cnum);
763         cli_setup_packet(cli);
764
765         SCVAL(cli->outbuf,smb_vwv0,0xFF);
766         SSVAL(cli->outbuf,smb_vwv2,fnum);
767
768         SIVAL(cli->outbuf,smb_vwv3,offset);
769         SIVAL(cli->outbuf,smb_vwv5,0);
770         SSVAL(cli->outbuf,smb_vwv7,mode);
771
772         SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
773         /*
774          * According to CIFS-TR-1p00, this following field should only
775          * be set if CAP_LARGE_WRITEX is set. We should check this
776          * locally. However, this check might already have been
777          * done by our callers.
778          */
779         SSVAL(cli->outbuf,smb_vwv9,(size>>16));
780         SSVAL(cli->outbuf,smb_vwv10,size);
781         /* +1 is pad byte. */
782         SSVAL(cli->outbuf,smb_vwv11,
783               smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
784
785         if (large_writex) {
786                 SIVAL(cli->outbuf,smb_vwv12,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
787         }
788
789         p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
790         *p++ = '\0'; /* pad byte. */
791         if (!direct_writes) {
792                 memcpy(p, buf, size);
793         }
794         if (size > 0x1FFFF) {
795                 /* This is a POSIX 14 word large write. */
796                 set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
797                 _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
798         } else {
799                 cli_setup_bcc(cli, p+size);
800         }
801
802         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
803
804         show_msg(cli->outbuf);
805         if (direct_writes) {
806                 /* For direct writes we now need to write the data
807                  * directly out of buf. */
808                 return cli_send_smb_direct_writeX(cli, buf, size);
809         } else {
810                 return cli_send_smb(cli);
811         }
812 }
813
814 /****************************************************************************
815   write to a file
816   write_mode: 0x0001 disallow write cacheing
817               0x0002 return bytes remaining
818               0x0004 use raw named pipe protocol
819               0x0008 start of message mode named pipe protocol
820 ****************************************************************************/
821
822 ssize_t cli_write(struct cli_state *cli,
823                  int fnum, uint16 write_mode,
824                  const char *buf, off_t offset, size_t size)
825 {
826         ssize_t bwritten = 0;
827         unsigned int issued = 0;
828         unsigned int received = 0;
829         int mpx = 1;
830         size_t writesize;
831         int blocks;
832
833         if(cli->max_mux > 1) {
834                 mpx = cli->max_mux-1;
835         } else {
836                 mpx = 1;
837         }
838
839         /* Default (small) writesize. */
840         writesize = (cli->max_xmit - (smb_size+32)) & ~1023;
841
842         if (write_mode == 0 &&
843                         !client_is_signing_on(cli) &&
844                         !cli_encryption_on(cli) &&
845                         (cli->posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
846                         (cli->capabilities & CAP_LARGE_FILES)) {
847                 /* Only do massive writes if we can do them direct
848                  * with no signing or encrypting - not on a pipe. */
849                 writesize = CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
850         } else if (cli->capabilities & CAP_LARGE_WRITEX) {
851                 if (cli->is_samba) {
852                         writesize = CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
853                 } else if (!client_is_signing_on(cli)) {
854                         /* Windows restricts signed writes to max_xmit.
855                          * Found by Volker. */
856                         writesize = CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
857                 }
858         }
859
860         blocks = (size + (writesize-1)) / writesize;
861
862         while (received < blocks) {
863
864                 while ((issued - received < mpx) && (issued < blocks)) {
865                         ssize_t bsent = issued * writesize;
866                         ssize_t size1 = MIN(writesize, size - bsent);
867
868                         if (!cli_issue_write(cli, fnum, offset + bsent,
869                                         write_mode,
870                                         buf + bsent,
871                                         size1, issued))
872                                 return -1;
873                         issued++;
874                 }
875
876                 if (!cli_receive_smb(cli)) {
877                         return bwritten;
878                 }
879
880                 received++;
881
882                 if (cli_is_error(cli))
883                         break;
884
885                 bwritten += SVAL(cli->inbuf, smb_vwv2);
886                 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
887         }
888
889         while (received < issued && cli_receive_smb(cli)) {
890                 received++;
891         }
892
893         return bwritten;
894 }
895
896 /****************************************************************************
897   write to a file using a SMBwrite and not bypassing 0 byte writes
898 ****************************************************************************/
899
900 ssize_t cli_smbwrite(struct cli_state *cli,
901                      int fnum, char *buf, off_t offset, size_t size1)
902 {
903         char *p;
904         ssize_t total = 0;
905
906         do {
907                 size_t size = MIN(size1, cli->max_xmit - 48);
908
909                 memset(cli->outbuf,'\0',smb_size);
910                 memset(cli->inbuf,'\0',smb_size);
911
912                 cli_set_message(cli->outbuf,5, 0,True);
913
914                 SCVAL(cli->outbuf,smb_com,SMBwrite);
915                 SSVAL(cli->outbuf,smb_tid,cli->cnum);
916                 cli_setup_packet(cli);
917
918                 SSVAL(cli->outbuf,smb_vwv0,fnum);
919                 SSVAL(cli->outbuf,smb_vwv1,size);
920                 SIVAL(cli->outbuf,smb_vwv2,offset);
921                 SSVAL(cli->outbuf,smb_vwv4,0);
922
923                 p = smb_buf(cli->outbuf);
924                 *p++ = 1;
925                 SSVAL(p, 0, size); p += 2;
926                 memcpy(p, buf + total, size); p += size;
927
928                 cli_setup_bcc(cli, p);
929
930                 if (!cli_send_smb(cli))
931                         return -1;
932
933                 if (!cli_receive_smb(cli))
934                         return -1;
935
936                 if (cli_is_error(cli))
937                         return -1;
938
939                 size = SVAL(cli->inbuf,smb_vwv0);
940                 if (size == 0)
941                         break;
942
943                 size1 -= size;
944                 total += size;
945                 offset += size;
946
947         } while (size1);
948
949         return total;
950 }