Ensure we don't use massive writes in pipe mode.
[samba.git] / source3 / libsmb / clireadwrite.c
1 /*
2    Unix SMB/CIFS implementation.
3    client file read/write routines
4    Copyright (C) Andrew Tridgell 1994-1998
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 /****************************************************************************
23 Issue a single SMBread and don't wait for a reply.
24 ****************************************************************************/
25
26 static bool cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
27                            size_t size, int i)
28 {
29         bool bigoffset = False;
30
31         memset(cli->outbuf,'\0',smb_size);
32         memset(cli->inbuf,'\0',smb_size);
33
34         if ((SMB_BIG_UINT)offset >> 32)
35                 bigoffset = True;
36
37         set_message(cli->outbuf,bigoffset ? 12 : 10,0,True);
38
39         SCVAL(cli->outbuf,smb_com,SMBreadX);
40         SSVAL(cli->outbuf,smb_tid,cli->cnum);
41         cli_setup_packet(cli);
42
43         SCVAL(cli->outbuf,smb_vwv0,0xFF);
44         SSVAL(cli->outbuf,smb_vwv2,fnum);
45         SIVAL(cli->outbuf,smb_vwv3,offset);
46         SSVAL(cli->outbuf,smb_vwv5,size);
47         SSVAL(cli->outbuf,smb_vwv6,size);
48         SSVAL(cli->outbuf,smb_vwv7,(size >> 16));
49         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
50
51         if (bigoffset) {
52                 SIVAL(cli->outbuf,smb_vwv10,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
53         }
54
55         return cli_send_smb(cli);
56 }
57
58 /****************************************************************************
59   Read size bytes at offset offset using SMBreadX.
60 ****************************************************************************/
61
62 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
63 {
64         char *p;
65         size_t size2;
66         size_t readsize;
67         ssize_t total = 0;
68         /* We can only do direct reads if not signing. */
69         bool direct_reads = !client_is_signing_on(cli);
70
71         if (size == 0)
72                 return 0;
73
74         /*
75          * Set readsize to the maximum size we can handle in one readX,
76          * rounded down to a multiple of 1024.
77          */
78
79         if (client_is_signing_on(cli) == False && (cli->posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
80                 readsize = CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
81         } else if (cli->capabilities & CAP_LARGE_READX) {
82                 if (cli->is_samba) {
83                         readsize = CLI_SAMBA_MAX_LARGE_READX_SIZE;
84                 } else {
85                         readsize = CLI_WINDOWS_MAX_LARGE_READX_SIZE;
86                 }
87         } else {
88                 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
89         }
90
91         while (total < size) {
92                 readsize = MIN(readsize, size-total);
93
94                 /* Issue a read and receive a reply */
95
96                 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
97                         return -1;
98
99                 if (direct_reads) {
100                         if (!cli_receive_smb_readX_header(cli))
101                                 return -1;
102                 } else {
103                         if (!cli_receive_smb(cli))
104                                 return -1;
105                 }
106
107                 /* Check for error.  Make sure to check for DOS and NT
108                    errors. */
109
110                 if (cli_is_error(cli)) {
111                         bool recoverable_error = False;
112                         NTSTATUS status = NT_STATUS_OK;
113                         uint8 eclass = 0;
114                         uint32 ecode = 0;
115
116                         if (cli_is_nt_error(cli))
117                                 status = cli_nt_error(cli);
118                         else
119                                 cli_dos_error(cli, &eclass, &ecode);
120
121                         /*
122                          * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
123                          * recoverable error, plus we have valid data in the
124                          * packet so don't error out here.
125                          */
126
127                         if ((eclass == ERRDOS && ecode == ERRmoredata) ||
128                             NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
129                                 recoverable_error = True;
130
131                         if (!recoverable_error)
132                                 return -1;
133                 }
134
135                 size2 = SVAL(cli->inbuf, smb_vwv5);
136                 size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7))) << 16);
137
138                 if (size2 > readsize) {
139                         DEBUG(5,("server returned more than we wanted!\n"));
140                         return -1;
141                 } else if (size2 < 0) {
142                         DEBUG(5,("read return < 0!\n"));
143                         return -1;
144                 }
145
146                 if (!direct_reads) {
147                         /* Copy data into buffer */
148                         p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
149                         memcpy(buf + total, p, size2);
150                 } else {
151                         /* Ensure the remaining data matches the return size. */
152                         ssize_t toread = smb_len_large(cli->inbuf) - SVAL(cli->inbuf,smb_vwv6);
153
154                         /* Ensure the size is correct. */
155                         if (toread != size2) {
156                                 DEBUG(5,("direct read logic fail toread (%d) != size2 (%u)\n",
157                                         (int)toread, (unsigned int)size2 ));
158                                 return -1;
159                         }
160
161                         /* Read data directly into buffer */
162                         toread = cli_receive_smb_data(cli,buf+total,size2);
163                         if (toread != size2) {
164                                 DEBUG(5,("direct read read failure toread (%d) != size2 (%u)\n",
165                                         (int)toread, (unsigned int)size2 ));
166                                 return -1;
167                         }
168                 }
169
170                 total += size2;
171                 offset += size2;
172
173                 /*
174                  * If the server returned less than we asked for we're at EOF.
175                  */
176
177                 if (size2 < readsize)
178                         break;
179         }
180
181         return total;
182 }
183
184 #if 0  /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
185
186 /* This call is INCOMPATIBLE with SMB signing.  If you remove the #if 0
187    you must fix ensure you don't attempt to sign the packets - data
188    *will* be currupted */
189
190 /****************************************************************************
191 Issue a single SMBreadraw and don't wait for a reply.
192 ****************************************************************************/
193
194 static bool cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset, 
195                            size_t size, int i)
196 {
197
198         if (!cli->sign_info.use_smb_signing) {
199                 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
200                 return False;
201         }
202         
203         memset(cli->outbuf,'\0',smb_size);
204         memset(cli->inbuf,'\0',smb_size);
205
206         set_message(cli->outbuf,10,0,True);
207                 
208         SCVAL(cli->outbuf,smb_com,SMBreadbraw);
209         SSVAL(cli->outbuf,smb_tid,cli->cnum);
210         cli_setup_packet(cli);
211
212         SSVAL(cli->outbuf,smb_vwv0,fnum);
213         SIVAL(cli->outbuf,smb_vwv1,offset);
214         SSVAL(cli->outbuf,smb_vwv2,size);
215         SSVAL(cli->outbuf,smb_vwv3,size);
216         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
217
218         return cli_send_smb(cli);
219 }
220
221 /****************************************************************************
222  Tester for the readraw call.
223 ****************************************************************************/
224
225 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
226 {
227         char *p;
228         int size2;
229         size_t readsize;
230         ssize_t total = 0;
231
232         if (size == 0) 
233                 return 0;
234
235         /*
236          * Set readsize to the maximum size we can handle in one readraw.
237          */
238
239         readsize = 0xFFFF;
240
241         while (total < size) {
242                 readsize = MIN(readsize, size-total);
243
244                 /* Issue a read and receive a reply */
245
246                 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
247                         return -1;
248
249                 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
250                         return -1;
251
252                 size2 = smb_len(cli->inbuf);
253
254                 if (size2 > readsize) {
255                         DEBUG(5,("server returned more than we wanted!\n"));
256                         return -1;
257                 } else if (size2 < 0) {
258                         DEBUG(5,("read return < 0!\n"));
259                         return -1;
260                 }
261
262                 /* Copy data into buffer */
263
264                 if (size2) {
265                         p = cli->inbuf + 4;
266                         memcpy(buf + total, p, size2);
267                 }
268
269                 total += size2;
270                 offset += size2;
271
272                 /*
273                  * If the server returned less than we asked for we're at EOF.
274                  */
275
276                 if (size2 < readsize)
277                         break;
278         }
279
280         return total;
281 }
282 #endif
283
284 /****************************************************************************
285  Issue a single SMBwrite and don't wait for a reply.
286 ****************************************************************************/
287
288 static bool cli_issue_write(struct cli_state *cli,
289                                 int fnum,
290                                 off_t offset,
291                                 uint16 mode,
292                                 const char *buf,
293                                 size_t size,
294                                 int i)
295 {
296         char *p;
297         bool large_writex = false;
298         /* We can only do direct writes if not signing. */
299         bool direct_writes = !client_is_signing_on(cli);
300
301         if (!direct_writes && size + 1 > cli->bufsize) {
302                 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
303                 if (!cli->outbuf) {
304                         return False;
305                 }
306                 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
307                 if (cli->inbuf == NULL) {
308                         SAFE_FREE(cli->outbuf);
309                         return False;
310                 }
311                 cli->bufsize = size + 1024;
312         }
313
314         memset(cli->outbuf,'\0',smb_size);
315         memset(cli->inbuf,'\0',smb_size);
316
317         if (cli->capabilities & CAP_LARGE_FILES) {
318                 large_writex = True;
319         }
320
321         if (large_writex) {
322                 set_message(cli->outbuf,14,0,True);
323         } else {
324                 set_message(cli->outbuf,12,0,True);
325         }
326
327         SCVAL(cli->outbuf,smb_com,SMBwriteX);
328         SSVAL(cli->outbuf,smb_tid,cli->cnum);
329         cli_setup_packet(cli);
330
331         SCVAL(cli->outbuf,smb_vwv0,0xFF);
332         SSVAL(cli->outbuf,smb_vwv2,fnum);
333
334         SIVAL(cli->outbuf,smb_vwv3,offset);
335         SIVAL(cli->outbuf,smb_vwv5,0);
336         SSVAL(cli->outbuf,smb_vwv7,mode);
337
338         SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
339         /*
340          * According to CIFS-TR-1p00, this following field should only
341          * be set if CAP_LARGE_WRITEX is set. We should check this
342          * locally. However, this check might already have been
343          * done by our callers.
344          */
345         SSVAL(cli->outbuf,smb_vwv9,(size>>16));
346         SSVAL(cli->outbuf,smb_vwv10,size);
347         /* +1 is pad byte. */
348         SSVAL(cli->outbuf,smb_vwv11,
349               smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
350
351         if (large_writex) {
352                 SIVAL(cli->outbuf,smb_vwv12,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
353         }
354
355         p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
356         *p++ = '\0'; /* pad byte. */
357         if (!direct_writes) {
358                 memcpy(p, buf, size);
359         }
360         if (size > 0x1FFFF) {
361                 /* This is a POSIX 14 word large write. */
362                 set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
363                 _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
364         } else {
365                 cli_setup_bcc(cli, p+size);
366         }
367
368         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
369
370         show_msg(cli->outbuf);
371         if (direct_writes) {
372                 /* For direct writes we now need to write the data
373                  * directly out of buf. */
374                 return cli_send_smb_direct_writeX(cli, buf, size);
375         } else {
376                 return cli_send_smb(cli);
377         }
378 }
379
380 /****************************************************************************
381   write to a file
382   write_mode: 0x0001 disallow write cacheing
383               0x0002 return bytes remaining
384               0x0004 use raw named pipe protocol
385               0x0008 start of message mode named pipe protocol
386 ****************************************************************************/
387
388 ssize_t cli_write(struct cli_state *cli,
389                  int fnum, uint16 write_mode,
390                  const char *buf, off_t offset, size_t size)
391 {
392         ssize_t bwritten = 0;
393         unsigned int issued = 0;
394         unsigned int received = 0;
395         int mpx = 1;
396         size_t writesize;
397         int blocks;
398
399         if(cli->max_mux > 1) {
400                 mpx = cli->max_mux-1;
401         } else {
402                 mpx = 1;
403         }
404
405         if (write_mode == 0 &&
406                         !client_is_signing_on(cli) &&
407                         (cli->posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
408                         (cli->capabilities & CAP_LARGE_FILES)) {
409                 /* Only do massive writes if we can do them direct
410                  * with no signing - not on a pipe. */
411                 writesize = CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
412         } else if (cli->capabilities & CAP_LARGE_READX) {
413                 if (cli->is_samba) {
414                         writesize = CLI_SAMBA_MAX_LARGE_READX_SIZE;
415                 } else {
416                         writesize = CLI_WINDOWS_MAX_LARGE_READX_SIZE;
417                 }
418         } else {
419                 writesize = (cli->max_xmit - (smb_size+32)) & ~1023;
420         }
421
422         blocks = (size + (writesize-1)) / writesize;
423
424         while (received < blocks) {
425
426                 while ((issued - received < mpx) && (issued < blocks)) {
427                         ssize_t bsent = issued * writesize;
428                         ssize_t size1 = MIN(writesize, size - bsent);
429
430                         if (!cli_issue_write(cli, fnum, offset + bsent,
431                                         write_mode,
432                                         buf + bsent,
433                                         size1, issued))
434                                 return -1;
435                         issued++;
436                 }
437
438                 if (!cli_receive_smb(cli)) {
439                         return bwritten;
440                 }
441
442                 received++;
443
444                 if (cli_is_error(cli))
445                         break;
446
447                 bwritten += SVAL(cli->inbuf, smb_vwv2);
448                 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
449         }
450
451         while (received < issued && cli_receive_smb(cli)) {
452                 received++;
453         }
454
455         return bwritten;
456 }
457
458 /****************************************************************************
459   write to a file using a SMBwrite and not bypassing 0 byte writes
460 ****************************************************************************/
461
462 ssize_t cli_smbwrite(struct cli_state *cli,
463                      int fnum, char *buf, off_t offset, size_t size1)
464 {
465         char *p;
466         ssize_t total = 0;
467
468         do {
469                 size_t size = MIN(size1, cli->max_xmit - 48);
470
471                 memset(cli->outbuf,'\0',smb_size);
472                 memset(cli->inbuf,'\0',smb_size);
473
474                 set_message(cli->outbuf,5, 0,True);
475
476                 SCVAL(cli->outbuf,smb_com,SMBwrite);
477                 SSVAL(cli->outbuf,smb_tid,cli->cnum);
478                 cli_setup_packet(cli);
479
480                 SSVAL(cli->outbuf,smb_vwv0,fnum);
481                 SSVAL(cli->outbuf,smb_vwv1,size);
482                 SIVAL(cli->outbuf,smb_vwv2,offset);
483                 SSVAL(cli->outbuf,smb_vwv4,0);
484
485                 p = smb_buf(cli->outbuf);
486                 *p++ = 1;
487                 SSVAL(p, 0, size); p += 2;
488                 memcpy(p, buf + total, size); p += size;
489
490                 cli_setup_bcc(cli, p);
491
492                 if (!cli_send_smb(cli))
493                         return -1;
494
495                 if (!cli_receive_smb(cli))
496                         return -1;
497
498                 if (cli_is_error(cli))
499                         return -1;
500
501                 size = SVAL(cli->inbuf,smb_vwv0);
502                 if (size == 0)
503                         break;
504
505                 size1 -= size;
506                 total += size;
507                 offset += size;
508
509         } while (size1);
510
511         return total;
512 }