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