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