cc982683d0896807cef3b1fb51142f9aaf842cf3
[kai/samba.git] / source3 / libsmb / clireadwrite.c
1 /*
2    Unix SMB/CIFS implementation.
3    client file read/write routines
4    Copyright (C) Andrew Tridgell 1994-1998
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 /****************************************************************************
23   Calculate the recommended read buffer size
24 ****************************************************************************/
25 static size_t cli_read_max_bufsize(struct cli_state *cli)
26 {
27         if (!client_is_signing_on(cli) && !cli_encryption_on(cli)
28             && (cli->posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
29                 return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
30         }
31         if (cli->capabilities & CAP_LARGE_READX) {
32                 return cli->is_samba
33                         ? CLI_SAMBA_MAX_LARGE_READX_SIZE
34                         : CLI_WINDOWS_MAX_LARGE_READX_SIZE;
35         }
36         return (cli->max_xmit - (smb_size+32)) & ~1023;
37 }
38
39 /*
40  * Send a read&x request
41  */
42
43 struct async_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
44                                      struct event_context *ev,
45                                      struct cli_state *cli, int fnum,
46                                      off_t offset, size_t size)
47 {
48         struct async_req *result;
49         struct cli_request *req;
50         bool bigoffset = False;
51
52         uint16_t vwv[12];
53         uint8_t wct = 10;
54
55         if (size > cli_read_max_bufsize(cli)) {
56                 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
57                           "size=%d\n", (int)size,
58                           (int)cli_read_max_bufsize(cli)));
59                 return NULL;
60         }
61
62         SCVAL(vwv + 0, 0, 0xFF);
63         SCVAL(vwv + 0, 1, 0);
64         SSVAL(vwv + 1, 0, 0);
65         SSVAL(vwv + 2, 0, fnum);
66         SIVAL(vwv + 3, 0, offset);
67         SSVAL(vwv + 5, 0, size);
68         SSVAL(vwv + 6, 0, size);
69         SSVAL(vwv + 7, 0, (size >> 16));
70         SSVAL(vwv + 8, 0, 0);
71         SSVAL(vwv + 9, 0, 0);
72
73         if ((uint64_t)offset >> 32) {
74                 bigoffset = True;
75                 SIVAL(vwv + 10, 0,
76                       (((uint64_t)offset)>>32) & 0xffffffff);
77                 wct += 2;
78         }
79
80         result = cli_request_send(mem_ctx, ev, cli, SMBreadX, 0, wct, vwv,
81                                   0, NULL);
82         if (result == NULL) {
83                 return NULL;
84         }
85
86         req = talloc_get_type_abort(result->private_data, struct cli_request);
87
88         req->data.read.ofs = offset;
89         req->data.read.size = size;
90         req->data.read.received = 0;
91         req->data.read.rcvbuf = NULL;
92
93         return result;
94 }
95
96 /*
97  * Pull the data out of a finished async read_and_x request. rcvbuf is
98  * talloced from the request, so better make sure that you copy it away before
99  * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
100  * talloc_move it!
101  */
102
103 NTSTATUS cli_read_andx_recv(struct async_req *req, ssize_t *received,
104                             uint8_t **rcvbuf)
105 {
106         struct cli_request *cli_req = talloc_get_type_abort(
107                 req->private_data, struct cli_request);
108         uint8_t wct;
109         uint16_t *vwv;
110         uint16_t num_bytes;
111         uint8_t *bytes;
112         NTSTATUS status;
113         size_t size;
114
115         SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
116         if (req->state == ASYNC_REQ_ERROR) {
117                 return req->status;
118         }
119
120         status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
121
122         if (NT_STATUS_IS_ERR(status)) {
123                 return status;
124         }
125
126         if (wct < 12) {
127                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
128         }
129
130         /* size is the number of bytes the server returned.
131          * Might be zero. */
132         size = SVAL(vwv + 5, 0);
133         size |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
134
135         if (size > cli_req->data.read.size) {
136                 DEBUG(5,("server returned more than we wanted!\n"));
137                 return NT_STATUS_UNEXPECTED_IO_ERROR;
138         }
139
140         *rcvbuf = (uint8_t *)(smb_base(cli_req->inbuf) + SVAL(vwv + 6, 0));
141         *received = size;
142         return NT_STATUS_OK;
143 }
144
145 /*
146  * Parallel read support.
147  *
148  * cli_pull sends as many read&x requests as the server would allow via
149  * max_mux at a time. When replies flow back in, the data is written into
150  * the callback function "sink" in the right order.
151  */
152
153 struct cli_pull_state {
154         struct async_req *req;
155
156         struct event_context *ev;
157         struct cli_state *cli;
158         uint16_t fnum;
159         off_t start_offset;
160         SMB_OFF_T size;
161
162         NTSTATUS (*sink)(char *buf, size_t n, void *priv);
163         void *priv;
164
165         size_t chunk_size;
166
167         /*
168          * Outstanding requests
169          */
170         int num_reqs;
171         struct async_req **reqs;
172
173         /*
174          * For how many bytes did we send requests already?
175          */
176         SMB_OFF_T requested;
177
178         /*
179          * Next request index to push into "sink". This walks around the "req"
180          * array, taking care that the requests are pushed to "sink" in the
181          * right order. If necessary (i.e. replies don't come in in the right
182          * order), replies are held back in "reqs".
183          */
184         int top_req;
185
186         /*
187          * How many bytes did we push into "sink"?
188          */
189
190         SMB_OFF_T pushed;
191 };
192
193 static char *cli_pull_print(TALLOC_CTX *mem_ctx, struct async_req *req)
194 {
195         struct cli_pull_state *state = talloc_get_type_abort(
196                 req->private_data, struct cli_pull_state);
197         char *result;
198
199         result = async_req_print(mem_ctx, req);
200         if (result == NULL) {
201                 return NULL;
202         }
203
204         return talloc_asprintf_append_buffer(
205                 result, "num_reqs=%d, top_req=%d",
206                 state->num_reqs, state->top_req);
207 }
208
209 static void cli_pull_read_done(struct async_req *read_req);
210
211 /*
212  * Prepare an async pull request
213  */
214
215 struct async_req *cli_pull_send(TALLOC_CTX *mem_ctx,
216                                 struct event_context *ev,
217                                 struct cli_state *cli,
218                                 uint16_t fnum, off_t start_offset,
219                                 SMB_OFF_T size, size_t window_size,
220                                 NTSTATUS (*sink)(char *buf, size_t n,
221                                                  void *priv),
222                                 void *priv)
223 {
224         struct async_req *result;
225         struct cli_pull_state *state;
226         int i;
227
228         result = async_req_new(mem_ctx, ev);
229         if (result == NULL) {
230                 goto failed;
231         }
232         state = talloc(result, struct cli_pull_state);
233         if (state == NULL) {
234                 goto failed;
235         }
236         result->private_data = state;
237         result->print = cli_pull_print;
238         state->req = result;
239
240         state->cli = cli;
241         state->ev = ev;
242         state->fnum = fnum;
243         state->start_offset = start_offset;
244         state->size = size;
245         state->sink = sink;
246         state->priv = priv;
247
248         state->pushed = 0;
249         state->top_req = 0;
250
251         if (size == 0) {
252                 if (!async_post_status(result, NT_STATUS_OK)) {
253                         goto failed;
254                 }
255                 return result;
256         }
257
258         state->chunk_size = cli_read_max_bufsize(cli);
259
260         state->num_reqs = MAX(window_size/state->chunk_size, 1);
261         state->num_reqs = MIN(state->num_reqs, cli->max_mux);
262
263         state->reqs = TALLOC_ZERO_ARRAY(state, struct async_req *,
264                                         state->num_reqs);
265         if (state->reqs == NULL) {
266                 goto failed;
267         }
268
269         state->requested = 0;
270
271         for (i=0; i<state->num_reqs; i++) {
272                 SMB_OFF_T size_left;
273                 size_t request_thistime;
274
275                 if (state->requested >= size) {
276                         state->num_reqs = i;
277                         break;
278                 }
279
280                 size_left = size - state->requested;
281                 request_thistime = MIN(size_left, state->chunk_size);
282
283                 state->reqs[i] = cli_read_andx_send(
284                         state->reqs, ev, cli, fnum,
285                         state->start_offset + state->requested,
286                         request_thistime);
287
288                 if (state->reqs[i] == NULL) {
289                         goto failed;
290                 }
291
292                 state->reqs[i]->async.fn = cli_pull_read_done;
293                 state->reqs[i]->async.priv = result;
294
295                 state->requested += request_thistime;
296         }
297         return result;
298
299 failed:
300         TALLOC_FREE(result);
301         return NULL;
302 }
303
304 /*
305  * Handle incoming read replies, push the data into sink and send out new
306  * requests if necessary.
307  */
308
309 static void cli_pull_read_done(struct async_req *read_req)
310 {
311         struct async_req *pull_req = talloc_get_type_abort(
312                 read_req->async.priv, struct async_req);
313         struct cli_pull_state *state = talloc_get_type_abort(
314                 pull_req->private_data, struct cli_pull_state);
315         struct cli_request *read_state = talloc_get_type_abort(
316                 read_req->private_data, struct cli_request);
317         NTSTATUS status;
318
319         status = cli_read_andx_recv(read_req, &read_state->data.read.received,
320                                     &read_state->data.read.rcvbuf);
321         if (!NT_STATUS_IS_OK(status)) {
322                 async_req_error(state->req, status);
323                 return;
324         }
325
326         /*
327          * This loop is the one to take care of out-of-order replies. All
328          * pending requests are in state->reqs, state->reqs[top_req] is the
329          * one that is to be pushed next. If however a request later than
330          * top_req is replied to, then we can't push yet. If top_req is
331          * replied to at a later point then, we need to push all the finished
332          * requests.
333          */
334
335         while (state->reqs[state->top_req] != NULL) {
336                 struct cli_request *top_read;
337
338                 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
339                            state->top_req));
340
341                 if (state->reqs[state->top_req]->state < ASYNC_REQ_DONE) {
342                         DEBUG(11, ("cli_pull_read_done: top request not yet "
343                                    "done\n"));
344                         return;
345                 }
346
347                 top_read = talloc_get_type_abort(
348                         state->reqs[state->top_req]->private_data,
349                         struct cli_request);
350
351                 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
352                            "pushed\n", (int)top_read->data.read.received,
353                            (int)state->pushed));
354
355                 status = state->sink((char *)top_read->data.read.rcvbuf,
356                                      top_read->data.read.received,
357                                      state->priv);
358                 if (!NT_STATUS_IS_OK(status)) {
359                         async_req_error(state->req, status);
360                         return;
361                 }
362                 state->pushed += top_read->data.read.received;
363
364                 TALLOC_FREE(state->reqs[state->top_req]);
365
366                 if (state->requested < state->size) {
367                         struct async_req *new_req;
368                         SMB_OFF_T size_left;
369                         size_t request_thistime;
370
371                         size_left = state->size - state->requested;
372                         request_thistime = MIN(size_left, state->chunk_size);
373
374                         DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
375                                    "at %d, position %d\n",
376                                    (int)request_thistime,
377                                    (int)(state->start_offset
378                                          + state->requested),
379                                    state->top_req));
380
381                         new_req = cli_read_andx_send(
382                                 state->reqs, state->ev, state->cli,
383                                 state->fnum,
384                                 state->start_offset + state->requested,
385                                 request_thistime);
386
387                         if (async_req_nomem(new_req, state->req)) {
388                                 return;
389                         }
390
391                         new_req->async.fn = cli_pull_read_done;
392                         new_req->async.priv = pull_req;
393
394                         state->reqs[state->top_req] = new_req;
395                         state->requested += request_thistime;
396                 }
397
398                 state->top_req = (state->top_req+1) % state->num_reqs;
399         }
400
401         async_req_done(pull_req);
402 }
403
404 NTSTATUS cli_pull_recv(struct async_req *req, SMB_OFF_T *received)
405 {
406         struct cli_pull_state *state = talloc_get_type_abort(
407                 req->private_data, struct cli_pull_state);
408
409         SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
410         if (req->state == ASYNC_REQ_ERROR) {
411                 return req->status;
412         }
413         *received = state->pushed;
414         return NT_STATUS_OK;
415 }
416
417 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
418                   off_t start_offset, SMB_OFF_T size, size_t window_size,
419                   NTSTATUS (*sink)(char *buf, size_t n, void *priv),
420                   void *priv, SMB_OFF_T *received)
421 {
422         TALLOC_CTX *frame = talloc_stackframe();
423         struct event_context *ev;
424         struct async_req *req;
425         NTSTATUS result = NT_STATUS_NO_MEMORY;
426
427         if (cli->fd_event != NULL) {
428                 /*
429                  * Can't use sync call while an async call is in flight
430                  */
431                 return NT_STATUS_INVALID_PARAMETER;
432         }
433
434         ev = event_context_init(frame);
435         if (ev == NULL) {
436                 goto nomem;
437         }
438
439         req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
440                             window_size, sink, priv);
441         if (req == NULL) {
442                 goto nomem;
443         }
444
445         while (req->state < ASYNC_REQ_DONE) {
446                 event_loop_once(ev);
447         }
448
449         result = cli_pull_recv(req, received);
450  nomem:
451         TALLOC_FREE(frame);
452         return result;
453 }
454
455 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
456 {
457         char **pbuf = (char **)priv;
458         memcpy(*pbuf, buf, n);
459         *pbuf += n;
460         return NT_STATUS_OK;
461 }
462
463 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
464                  off_t offset, size_t size)
465 {
466         NTSTATUS status;
467         SMB_OFF_T ret;
468
469         status = cli_pull(cli, fnum, offset, size, size,
470                           cli_read_sink, &buf, &ret);
471         if (!NT_STATUS_IS_OK(status)) {
472                 cli_set_error(cli, status);
473                 return -1;
474         }
475         return ret;
476 }
477
478 /****************************************************************************
479  Issue a single SMBwrite and don't wait for a reply.
480 ****************************************************************************/
481
482 static bool cli_issue_write(struct cli_state *cli,
483                                 int fnum,
484                                 off_t offset,
485                                 uint16 mode,
486                                 const char *buf,
487                                 size_t size,
488                                 int i)
489 {
490         char *p;
491         bool large_writex = false;
492         /* We can only do direct writes if not signing and not encrypting. */
493         bool direct_writes = !client_is_signing_on(cli) && !cli_encryption_on(cli);
494
495         if (!direct_writes && size + 1 > cli->bufsize) {
496                 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
497                 if (!cli->outbuf) {
498                         return False;
499                 }
500                 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
501                 if (cli->inbuf == NULL) {
502                         SAFE_FREE(cli->outbuf);
503                         return False;
504                 }
505                 cli->bufsize = size + 1024;
506         }
507
508         memset(cli->outbuf,'\0',smb_size);
509         memset(cli->inbuf,'\0',smb_size);
510
511         if (cli->capabilities & CAP_LARGE_FILES) {
512                 large_writex = True;
513         }
514
515         if (large_writex) {
516                 cli_set_message(cli->outbuf,14,0,True);
517         } else {
518                 cli_set_message(cli->outbuf,12,0,True);
519         }
520
521         SCVAL(cli->outbuf,smb_com,SMBwriteX);
522         SSVAL(cli->outbuf,smb_tid,cli->cnum);
523         cli_setup_packet(cli);
524
525         SCVAL(cli->outbuf,smb_vwv0,0xFF);
526         SSVAL(cli->outbuf,smb_vwv2,fnum);
527
528         SIVAL(cli->outbuf,smb_vwv3,offset);
529         SIVAL(cli->outbuf,smb_vwv5,0);
530         SSVAL(cli->outbuf,smb_vwv7,mode);
531
532         SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
533         /*
534          * According to CIFS-TR-1p00, this following field should only
535          * be set if CAP_LARGE_WRITEX is set. We should check this
536          * locally. However, this check might already have been
537          * done by our callers.
538          */
539         SSVAL(cli->outbuf,smb_vwv9,(size>>16));
540         SSVAL(cli->outbuf,smb_vwv10,size);
541         /* +1 is pad byte. */
542         SSVAL(cli->outbuf,smb_vwv11,
543               smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
544
545         if (large_writex) {
546                 SIVAL(cli->outbuf,smb_vwv12,(((uint64_t)offset)>>32) & 0xffffffff);
547         }
548
549         p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
550         *p++ = '\0'; /* pad byte. */
551         if (!direct_writes) {
552                 memcpy(p, buf, size);
553         }
554         if (size > 0x1FFFF) {
555                 /* This is a POSIX 14 word large write. */
556                 set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
557                 _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
558         } else {
559                 cli_setup_bcc(cli, p+size);
560         }
561
562         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
563
564         show_msg(cli->outbuf);
565         if (direct_writes) {
566                 /* For direct writes we now need to write the data
567                  * directly out of buf. */
568                 return cli_send_smb_direct_writeX(cli, buf, size);
569         } else {
570                 return cli_send_smb(cli);
571         }
572 }
573
574 /****************************************************************************
575   write to a file
576   write_mode: 0x0001 disallow write cacheing
577               0x0002 return bytes remaining
578               0x0004 use raw named pipe protocol
579               0x0008 start of message mode named pipe protocol
580 ****************************************************************************/
581
582 ssize_t cli_write(struct cli_state *cli,
583                  int fnum, uint16 write_mode,
584                  const char *buf, off_t offset, size_t size)
585 {
586         ssize_t bwritten = 0;
587         unsigned int issued = 0;
588         unsigned int received = 0;
589         int mpx = 1;
590         size_t writesize;
591         int blocks;
592
593         if(cli->max_mux > 1) {
594                 mpx = cli->max_mux-1;
595         } else {
596                 mpx = 1;
597         }
598
599         /* Default (small) writesize. */
600         writesize = (cli->max_xmit - (smb_size+32)) & ~1023;
601
602         if (write_mode == 0 &&
603                         !client_is_signing_on(cli) &&
604                         !cli_encryption_on(cli) &&
605                         (cli->posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
606                         (cli->capabilities & CAP_LARGE_FILES)) {
607                 /* Only do massive writes if we can do them direct
608                  * with no signing or encrypting - not on a pipe. */
609                 writesize = CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
610         } else if ((cli->capabilities & CAP_LARGE_WRITEX) &&
611                         (strcmp(cli->dev, "LPT1:") != 0)) {
612
613                 /* Printer devices are restricted to max_xmit
614                  * writesize in Vista and XPSP3. */
615
616                 if (cli->is_samba) {
617                         writesize = CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
618                 } else if (!client_is_signing_on(cli)) {
619                         /* Windows restricts signed writes to max_xmit.
620                          * Found by Volker. */
621                         writesize = CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
622                 }
623         }
624
625         blocks = (size + (writesize-1)) / writesize;
626
627         while (received < blocks) {
628
629                 while ((issued - received < mpx) && (issued < blocks)) {
630                         ssize_t bsent = issued * writesize;
631                         ssize_t size1 = MIN(writesize, size - bsent);
632
633                         if (!cli_issue_write(cli, fnum, offset + bsent,
634                                         write_mode,
635                                         buf + bsent,
636                                         size1, issued))
637                                 return -1;
638                         issued++;
639                 }
640
641                 if (!cli_receive_smb(cli)) {
642                         return bwritten;
643                 }
644
645                 received++;
646
647                 if (cli_is_error(cli))
648                         break;
649
650                 bwritten += SVAL(cli->inbuf, smb_vwv2);
651                 if (writesize > 0xFFFF) {
652                         bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
653                 }
654         }
655
656         while (received < issued && cli_receive_smb(cli)) {
657                 received++;
658         }
659
660         return bwritten;
661 }
662
663 /****************************************************************************
664   write to a file using a SMBwrite and not bypassing 0 byte writes
665 ****************************************************************************/
666
667 ssize_t cli_smbwrite(struct cli_state *cli,
668                      int fnum, char *buf, off_t offset, size_t size1)
669 {
670         char *p;
671         ssize_t total = 0;
672
673         do {
674                 size_t size = MIN(size1, cli->max_xmit - 48);
675
676                 memset(cli->outbuf,'\0',smb_size);
677                 memset(cli->inbuf,'\0',smb_size);
678
679                 cli_set_message(cli->outbuf,5, 0,True);
680
681                 SCVAL(cli->outbuf,smb_com,SMBwrite);
682                 SSVAL(cli->outbuf,smb_tid,cli->cnum);
683                 cli_setup_packet(cli);
684
685                 SSVAL(cli->outbuf,smb_vwv0,fnum);
686                 SSVAL(cli->outbuf,smb_vwv1,size);
687                 SIVAL(cli->outbuf,smb_vwv2,offset);
688                 SSVAL(cli->outbuf,smb_vwv4,0);
689
690                 p = smb_buf(cli->outbuf);
691                 *p++ = 1;
692                 SSVAL(p, 0, size); p += 2;
693                 memcpy(p, buf + total, size); p += size;
694
695                 cli_setup_bcc(cli, p);
696
697                 if (!cli_send_smb(cli))
698                         return -1;
699
700                 if (!cli_receive_smb(cli))
701                         return -1;
702
703                 if (cli_is_error(cli))
704                         return -1;
705
706                 size = SVAL(cli->inbuf,smb_vwv0);
707                 if (size == 0)
708                         break;
709
710                 size1 -= size;
711                 total += size;
712                 offset += size;
713
714         } while (size1);
715
716         return total;
717 }