Fix bug 5334
[Samba/bjacke.git] / source / libsmb / clireadwrite.c
blob13c024a26427bedf600c22227c904088a0260744
1 /*
2 Unix SMB/CIFS implementation.
3 client file read/write routines
4 Copyright (C) Andrew Tridgell 1994-1998
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.
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.
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/>.
20 #include "includes.h"
22 /****************************************************************************
23 Calculate the recommended read buffer size
24 ****************************************************************************/
25 static size_t cli_read_max_bufsize(struct cli_state *cli)
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;
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;
36 return (cli->max_xmit - (smb_size+32)) & ~1023;
40 * Send a read&x request
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)
47 struct async_req *result;
48 struct cli_request *req;
49 bool bigoffset = False;
50 char *enc_buf;
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;
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;
65 req = cli_request_get(result);
67 req->data.read.ofs = offset;
68 req->data.read.size = size;
69 req->data.read.received = 0;
70 req->data.read.rcvbuf = NULL;
72 if ((SMB_BIG_UINT)offset >> 32)
73 bigoffset = True;
75 cli_set_message(req->outbuf, bigoffset ? 12 : 10, 0, False);
77 SCVAL(req->outbuf,smb_com,SMBreadX);
78 SSVAL(req->outbuf,smb_tid,cli->cnum);
79 cli_setup_packet_buf(cli, req->outbuf);
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);
93 if (bigoffset) {
94 SIVAL(req->outbuf, smb_vwv10,
95 (((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
98 cli_calculate_sign_mac(cli, req->outbuf);
100 event_fd_set_writeable(cli->fd_event);
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;
111 req->outbuf = enc_buf;
112 req->enc_state = cli->trans_enc_state;
115 return result;
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!
125 NTSTATUS cli_read_andx_recv(struct async_req *req, ssize_t *received,
126 uint8_t **rcvbuf)
128 struct cli_request *cli_req = cli_request_get(req);
129 NTSTATUS status;
130 size_t size;
132 SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
133 if (req->state == ASYNC_REQ_ERROR) {
134 return req->status;
137 status = cli_pull_error(cli_req->inbuf);
139 if (!NT_STATUS_IS_OK(status)) {
140 return status;
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);
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;
153 if (size < 0) {
154 DEBUG(5,("read return < 0!\n"));
155 return NT_STATUS_UNEXPECTED_IO_ERROR;
158 *rcvbuf = (uint8_t *)
159 (smb_base(cli_req->inbuf) + SVAL(cli_req->inbuf, smb_vwv6));
160 *received = size;
161 return NT_STATUS_OK;
165 * Parallel read support.
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.
172 struct cli_pull_state {
173 struct async_req *req;
175 struct cli_state *cli;
176 uint16_t fnum;
177 off_t start_offset;
178 size_t size;
180 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
181 void *priv;
183 size_t chunk_size;
186 * Outstanding requests
188 int num_reqs;
189 struct async_req **reqs;
192 * For how many bytes did we send requests already?
194 off_t requested;
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".
202 int top_req;
205 * How many bytes did we push into "sink"?
208 off_t pushed;
211 static char *cli_pull_print(TALLOC_CTX *mem_ctx, struct async_req *req)
213 struct cli_pull_state *state = talloc_get_type_abort(
214 req->private_data, struct cli_pull_state);
215 char *result;
217 result = async_req_print(mem_ctx, req);
218 if (result == NULL) {
219 return NULL;
222 return talloc_asprintf_append_buffer(
223 result, "num_reqs=%d, top_req=%d",
224 state->num_reqs, state->top_req);
227 static void cli_pull_read_done(struct async_req *read_req);
230 * Prepare an async pull request
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)
240 struct async_req *result;
241 struct cli_pull_state *state;
242 int i;
244 result = async_req_new(mem_ctx, cli->event_ctx);
245 if (result == NULL) {
246 goto failed;
248 state = talloc(result, struct cli_pull_state);
249 if (state == NULL) {
250 goto failed;
252 result->private_data = state;
253 result->print = cli_pull_print;
254 state->req = result;
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;
263 state->pushed = 0;
264 state->top_req = 0;
266 if (size == 0) {
267 if (!async_post_status(result, NT_STATUS_OK)) {
268 goto failed;
270 return result;
273 state->chunk_size = cli_read_max_bufsize(cli);
275 state->num_reqs = MAX(window_size/state->chunk_size, 1);
276 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
278 state->reqs = TALLOC_ZERO_ARRAY(state, struct async_req *,
279 state->num_reqs);
280 if (state->reqs == NULL) {
281 goto failed;
284 state->requested = 0;
286 for (i=0; i<state->num_reqs; i++) {
287 size_t size_left, request_thistime;
289 if (state->requested >= size) {
290 state->num_reqs = i;
291 break;
294 size_left = size - state->requested;
295 request_thistime = MIN(size_left, state->chunk_size);
297 state->reqs[i] = cli_read_andx_send(
298 state->reqs, cli, fnum,
299 state->start_offset + state->requested,
300 request_thistime);
302 if (state->reqs[i] == NULL) {
303 goto failed;
306 state->reqs[i]->async.fn = cli_pull_read_done;
307 state->reqs[i]->async.priv = result;
309 state->requested += request_thistime;
311 return result;
313 failed:
314 TALLOC_FREE(result);
315 return NULL;
319 * Handle incoming read replies, push the data into sink and send out new
320 * requests if necessary.
323 static void cli_pull_read_done(struct async_req *read_req)
325 struct async_req *pull_req = talloc_get_type_abort(
326 read_req->async.priv, struct async_req);
327 struct cli_pull_state *state = talloc_get_type_abort(
328 pull_req->private_data, struct cli_pull_state);
329 struct cli_request *read_state = cli_request_get(read_req);
330 NTSTATUS status;
332 status = cli_read_andx_recv(read_req, &read_state->data.read.received,
333 &read_state->data.read.rcvbuf);
334 if (!NT_STATUS_IS_OK(status)) {
335 async_req_error(state->req, status);
336 return;
340 * This loop is the one to take care of out-of-order replies. All
341 * pending requests are in state->reqs, state->reqs[top_req] is the
342 * one that is to be pushed next. If however a request later than
343 * top_req is replied to, then we can't push yet. If top_req is
344 * replied to at a later point then, we need to push all the finished
345 * requests.
348 while (state->reqs[state->top_req] != NULL) {
349 struct cli_request *top_read;
351 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
352 state->top_req));
354 if (state->reqs[state->top_req]->state < ASYNC_REQ_DONE) {
355 DEBUG(11, ("cli_pull_read_done: top request not yet "
356 "done\n"));
357 return;
360 top_read = cli_request_get(state->reqs[state->top_req]);
362 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
363 "pushed\n", (int)top_read->data.read.received,
364 (int)state->pushed));
366 status = state->sink((char *)top_read->data.read.rcvbuf,
367 top_read->data.read.received,
368 state->priv);
369 if (!NT_STATUS_IS_OK(status)) {
370 async_req_error(state->req, status);
371 return;
373 state->pushed += top_read->data.read.received;
375 TALLOC_FREE(state->reqs[state->top_req]);
377 if (state->requested < state->size) {
378 struct async_req *new_req;
379 size_t size_left, request_thistime;
381 size_left = state->size - state->requested;
382 request_thistime = MIN(size_left, state->chunk_size);
384 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
385 "at %d, position %d\n",
386 (int)request_thistime,
387 (int)(state->start_offset
388 + state->requested),
389 state->top_req));
391 new_req = cli_read_andx_send(
392 state->reqs, state->cli, state->fnum,
393 state->start_offset + state->requested,
394 request_thistime);
396 if (async_req_nomem(new_req, state->req)) {
397 return;
400 new_req->async.fn = cli_pull_read_done;
401 new_req->async.priv = pull_req;
403 state->reqs[state->top_req] = new_req;
404 state->requested += request_thistime;
407 state->top_req = (state->top_req+1) % state->num_reqs;
410 async_req_done(pull_req);
413 NTSTATUS cli_pull_recv(struct async_req *req, ssize_t *received)
415 struct cli_pull_state *state = talloc_get_type_abort(
416 req->private_data, struct cli_pull_state);
418 SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
419 if (req->state == ASYNC_REQ_ERROR) {
420 return req->status;
422 *received = state->pushed;
423 return NT_STATUS_OK;
426 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
427 off_t start_offset, size_t size, size_t window_size,
428 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
429 void *priv, ssize_t *received)
431 TALLOC_CTX *frame = talloc_stackframe();
432 struct async_req *req;
433 NTSTATUS result = NT_STATUS_NO_MEMORY;
435 if (cli_tmp_event_ctx(frame, cli) == NULL) {
436 goto nomem;
439 req = cli_pull_send(frame, cli, fnum, start_offset, size, window_size,
440 sink, priv);
441 if (req == NULL) {
442 goto nomem;
445 while (req->state < ASYNC_REQ_DONE) {
446 event_loop_once(cli->event_ctx);
449 result = cli_pull_recv(req, received);
450 nomem:
451 TALLOC_FREE(frame);
452 return result;
455 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
457 char **pbuf = (char **)priv;
458 memcpy(*pbuf, buf, n);
459 *pbuf += n;
460 return NT_STATUS_OK;
463 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
464 off_t offset, size_t size)
466 NTSTATUS status;
467 ssize_t ret;
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;
475 return ret;
478 #if 0 /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
480 /* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
481 you must fix ensure you don't attempt to sign the packets - data
482 *will* be currupted */
484 /****************************************************************************
485 Issue a single SMBreadraw and don't wait for a reply.
486 ****************************************************************************/
488 static bool cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
489 size_t size, int i)
492 if (!cli->sign_info.use_smb_signing) {
493 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
494 return False;
497 memset(cli->outbuf,'\0',smb_size);
498 memset(cli->inbuf,'\0',smb_size);
500 cli_set_message(cli->outbuf,10,0,True);
502 SCVAL(cli->outbuf,smb_com,SMBreadbraw);
503 SSVAL(cli->outbuf,smb_tid,cli->cnum);
504 cli_setup_packet(cli);
506 SSVAL(cli->outbuf,smb_vwv0,fnum);
507 SIVAL(cli->outbuf,smb_vwv1,offset);
508 SSVAL(cli->outbuf,smb_vwv2,size);
509 SSVAL(cli->outbuf,smb_vwv3,size);
510 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
512 return cli_send_smb(cli);
515 /****************************************************************************
516 Tester for the readraw call.
517 ****************************************************************************/
519 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
521 char *p;
522 int size2;
523 size_t readsize;
524 ssize_t total = 0;
526 if (size == 0)
527 return 0;
530 * Set readsize to the maximum size we can handle in one readraw.
533 readsize = 0xFFFF;
535 while (total < size) {
536 readsize = MIN(readsize, size-total);
538 /* Issue a read and receive a reply */
540 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
541 return -1;
543 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
544 return -1;
546 size2 = smb_len(cli->inbuf);
548 if (size2 > readsize) {
549 DEBUG(5,("server returned more than we wanted!\n"));
550 return -1;
551 } else if (size2 < 0) {
552 DEBUG(5,("read return < 0!\n"));
553 return -1;
556 /* Copy data into buffer */
558 if (size2) {
559 p = cli->inbuf + 4;
560 memcpy(buf + total, p, size2);
563 total += size2;
564 offset += size2;
567 * If the server returned less than we asked for we're at EOF.
570 if (size2 < readsize)
571 break;
574 return total;
576 #endif
578 /****************************************************************************
579 Issue a single SMBwrite and don't wait for a reply.
580 ****************************************************************************/
582 static bool cli_issue_write(struct cli_state *cli,
583 int fnum,
584 off_t offset,
585 uint16 mode,
586 const char *buf,
587 size_t size,
588 int i)
590 char *p;
591 bool large_writex = false;
592 /* We can only do direct writes if not signing and not encrypting. */
593 bool direct_writes = !client_is_signing_on(cli) && !cli_encryption_on(cli);
595 if (!direct_writes && size + 1 > cli->bufsize) {
596 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
597 if (!cli->outbuf) {
598 return False;
600 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
601 if (cli->inbuf == NULL) {
602 SAFE_FREE(cli->outbuf);
603 return False;
605 cli->bufsize = size + 1024;
608 memset(cli->outbuf,'\0',smb_size);
609 memset(cli->inbuf,'\0',smb_size);
611 if (cli->capabilities & CAP_LARGE_FILES) {
612 large_writex = True;
615 if (large_writex) {
616 cli_set_message(cli->outbuf,14,0,True);
617 } else {
618 cli_set_message(cli->outbuf,12,0,True);
621 SCVAL(cli->outbuf,smb_com,SMBwriteX);
622 SSVAL(cli->outbuf,smb_tid,cli->cnum);
623 cli_setup_packet(cli);
625 SCVAL(cli->outbuf,smb_vwv0,0xFF);
626 SSVAL(cli->outbuf,smb_vwv2,fnum);
628 SIVAL(cli->outbuf,smb_vwv3,offset);
629 SIVAL(cli->outbuf,smb_vwv5,0);
630 SSVAL(cli->outbuf,smb_vwv7,mode);
632 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
634 * According to CIFS-TR-1p00, this following field should only
635 * be set if CAP_LARGE_WRITEX is set. We should check this
636 * locally. However, this check might already have been
637 * done by our callers.
639 SSVAL(cli->outbuf,smb_vwv9,(size>>16));
640 SSVAL(cli->outbuf,smb_vwv10,size);
641 /* +1 is pad byte. */
642 SSVAL(cli->outbuf,smb_vwv11,
643 smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
645 if (large_writex) {
646 SIVAL(cli->outbuf,smb_vwv12,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
649 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
650 *p++ = '\0'; /* pad byte. */
651 if (!direct_writes) {
652 memcpy(p, buf, size);
654 if (size > 0x1FFFF) {
655 /* This is a POSIX 14 word large write. */
656 set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
657 _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
658 } else {
659 cli_setup_bcc(cli, p+size);
662 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
664 show_msg(cli->outbuf);
665 if (direct_writes) {
666 /* For direct writes we now need to write the data
667 * directly out of buf. */
668 return cli_send_smb_direct_writeX(cli, buf, size);
669 } else {
670 return cli_send_smb(cli);
674 /****************************************************************************
675 write to a file
676 write_mode: 0x0001 disallow write cacheing
677 0x0002 return bytes remaining
678 0x0004 use raw named pipe protocol
679 0x0008 start of message mode named pipe protocol
680 ****************************************************************************/
682 ssize_t cli_write(struct cli_state *cli,
683 int fnum, uint16 write_mode,
684 const char *buf, off_t offset, size_t size)
686 ssize_t bwritten = 0;
687 unsigned int issued = 0;
688 unsigned int received = 0;
689 int mpx = 1;
690 size_t writesize;
691 int blocks;
693 if(cli->max_mux > 1) {
694 mpx = cli->max_mux-1;
695 } else {
696 mpx = 1;
699 /* Default (small) writesize. */
700 writesize = (cli->max_xmit - (smb_size+32)) & ~1023;
702 if (write_mode == 0 &&
703 !client_is_signing_on(cli) &&
704 !cli_encryption_on(cli) &&
705 (cli->posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
706 (cli->capabilities & CAP_LARGE_FILES)) {
707 /* Only do massive writes if we can do them direct
708 * with no signing or encrypting - not on a pipe. */
709 writesize = CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
710 } else if (cli->capabilities & CAP_LARGE_WRITEX) {
711 if (cli->is_samba) {
712 writesize = CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
713 } else if (!client_is_signing_on(cli)) {
714 /* Windows restricts signed writes to max_xmit.
715 * Found by Volker. */
716 writesize = CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
720 blocks = (size + (writesize-1)) / writesize;
722 while (received < blocks) {
724 while ((issued - received < mpx) && (issued < blocks)) {
725 ssize_t bsent = issued * writesize;
726 ssize_t size1 = MIN(writesize, size - bsent);
728 if (!cli_issue_write(cli, fnum, offset + bsent,
729 write_mode,
730 buf + bsent,
731 size1, issued))
732 return -1;
733 issued++;
736 if (!cli_receive_smb(cli)) {
737 return bwritten;
740 received++;
742 if (cli_is_error(cli))
743 break;
745 bwritten += SVAL(cli->inbuf, smb_vwv2);
746 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
749 while (received < issued && cli_receive_smb(cli)) {
750 received++;
753 return bwritten;
756 /****************************************************************************
757 write to a file using a SMBwrite and not bypassing 0 byte writes
758 ****************************************************************************/
760 ssize_t cli_smbwrite(struct cli_state *cli,
761 int fnum, char *buf, off_t offset, size_t size1)
763 char *p;
764 ssize_t total = 0;
766 do {
767 size_t size = MIN(size1, cli->max_xmit - 48);
769 memset(cli->outbuf,'\0',smb_size);
770 memset(cli->inbuf,'\0',smb_size);
772 cli_set_message(cli->outbuf,5, 0,True);
774 SCVAL(cli->outbuf,smb_com,SMBwrite);
775 SSVAL(cli->outbuf,smb_tid,cli->cnum);
776 cli_setup_packet(cli);
778 SSVAL(cli->outbuf,smb_vwv0,fnum);
779 SSVAL(cli->outbuf,smb_vwv1,size);
780 SIVAL(cli->outbuf,smb_vwv2,offset);
781 SSVAL(cli->outbuf,smb_vwv4,0);
783 p = smb_buf(cli->outbuf);
784 *p++ = 1;
785 SSVAL(p, 0, size); p += 2;
786 memcpy(p, buf + total, size); p += size;
788 cli_setup_bcc(cli, p);
790 if (!cli_send_smb(cli))
791 return -1;
793 if (!cli_receive_smb(cli))
794 return -1;
796 if (cli_is_error(cli))
797 return -1;
799 size = SVAL(cli->inbuf,smb_vwv0);
800 if (size == 0)
801 break;
803 size1 -= size;
804 total += size;
805 offset += size;
807 } while (size1);
809 return total;