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