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