source: branches/samba-3.5.x/source3/libsmb/clireadwrite.c@ 736

Last change on this file since 736 was 736, checked in by Silvan Scherrer, 13 years ago

Samba Server 3.5: update branche to 3.5.16

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