r4188: Ensure we add in the upper length in the right place !
[samba.git] / source / 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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #define NO_SYSLOG
22
23 #include "includes.h"
24
25 /****************************************************************************
26 Issue a single SMBread and don't wait for a reply.
27 ****************************************************************************/
28
29 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset, 
30                            size_t size, int i)
31 {
32         BOOL bigoffset = False;
33
34         memset(cli->outbuf,'\0',smb_size);
35         memset(cli->inbuf,'\0',smb_size);
36
37         if ((SMB_BIG_UINT)offset >> 32) 
38                 bigoffset = True;
39
40         set_message(cli->outbuf,bigoffset ? 12 : 10,0,True);
41                 
42         SCVAL(cli->outbuf,smb_com,SMBreadX);
43         SSVAL(cli->outbuf,smb_tid,cli->cnum);
44         cli_setup_packet(cli);
45
46         SCVAL(cli->outbuf,smb_vwv0,0xFF);
47         SSVAL(cli->outbuf,smb_vwv2,fnum);
48         SIVAL(cli->outbuf,smb_vwv3,offset);
49         SSVAL(cli->outbuf,smb_vwv5,size);
50         SSVAL(cli->outbuf,smb_vwv6,size);
51         SSVAL(cli->outbuf,smb_vwv7,((size >> 16) & 1));
52         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
53
54         if (bigoffset)
55                 SIVAL(cli->outbuf,smb_vwv10,(offset>>32) & 0xffffffff);
56
57         return cli_send_smb(cli);
58 }
59
60 /****************************************************************************
61   Read size bytes at offset offset using SMBreadX.
62 ****************************************************************************/
63
64 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
65 {
66         char *p;
67         int size2;
68         int readsize;
69         ssize_t total = 0;
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 (cli->capabilities & CAP_LARGE_READX) {
80                 readsize = CLI_MAX_LARGE_READX_SIZE;
81         } else {
82                 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
83         }
84
85         while (total < size) {
86                 readsize = MIN(readsize, size-total);
87
88                 /* Issue a read and receive a reply */
89
90                 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
91                         return -1;
92
93                 if (!cli_receive_smb(cli))
94                         return -1;
95
96                 /* Check for error.  Make sure to check for DOS and NT
97                    errors. */
98
99                 if (cli_is_error(cli)) {
100                         BOOL recoverable_error = False;
101                         NTSTATUS status = NT_STATUS_OK;
102                         uint8 eclass = 0;
103                         uint32 ecode = 0;
104
105                         if (cli_is_nt_error(cli))
106                                 status = cli_nt_error(cli);
107                         else
108                                 cli_dos_error(cli, &eclass, &ecode);
109
110                         /*
111                          * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
112                          * recoverable error, plus we have valid data in the
113                          * packet so don't error out here.
114                          */
115
116                         if ((eclass == ERRDOS && ecode == ERRmoredata) ||
117                             NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
118                                 recoverable_error = True;
119
120                         if (!recoverable_error)
121                                 return -1;
122                 }
123
124                 size2 = SVAL(cli->inbuf, smb_vwv5);
125                 size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7) & 1)) << 16);
126
127                 if (size2 > readsize) {
128                         DEBUG(5,("server returned more than we wanted!\n"));
129                         return -1;
130                 } else if (size2 < 0) {
131                         DEBUG(5,("read return < 0!\n"));
132                         return -1;
133                 }
134
135                 /* Copy data into buffer */
136
137                 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
138                 memcpy(buf + total, p, size2);
139
140                 total += size2;
141                 offset += size2;
142
143                 /*
144                  * If the server returned less than we asked for we're at EOF.
145                  */
146
147                 if (size2 < readsize)
148                         break;
149         }
150
151         return total;
152 }
153
154 #if 0  /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
155
156 /* This call is INCOMPATIBLE with SMB signing.  If you remove the #if 0
157    you must fix ensure you don't attempt to sign the packets - data
158    *will* be currupted */
159
160 /****************************************************************************
161 Issue a single SMBreadraw and don't wait for a reply.
162 ****************************************************************************/
163
164 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset, 
165                            size_t size, int i)
166 {
167
168         if (!cli->sign_info.use_smb_signing) {
169                 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
170                 return False;
171         }
172         
173         memset(cli->outbuf,'\0',smb_size);
174         memset(cli->inbuf,'\0',smb_size);
175
176         set_message(cli->outbuf,10,0,True);
177                 
178         SCVAL(cli->outbuf,smb_com,SMBreadbraw);
179         SSVAL(cli->outbuf,smb_tid,cli->cnum);
180         cli_setup_packet(cli);
181
182         SSVAL(cli->outbuf,smb_vwv0,fnum);
183         SIVAL(cli->outbuf,smb_vwv1,offset);
184         SSVAL(cli->outbuf,smb_vwv2,size);
185         SSVAL(cli->outbuf,smb_vwv3,size);
186         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
187
188         return cli_send_smb(cli);
189 }
190
191 /****************************************************************************
192  Tester for the readraw call.
193 ****************************************************************************/
194
195 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
196 {
197         char *p;
198         int size2;
199         size_t readsize;
200         ssize_t total = 0;
201
202         if (size == 0) 
203                 return 0;
204
205         /*
206          * Set readsize to the maximum size we can handle in one readraw.
207          */
208
209         readsize = 0xFFFF;
210
211         while (total < size) {
212                 readsize = MIN(readsize, size-total);
213
214                 /* Issue a read and receive a reply */
215
216                 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
217                         return -1;
218
219                 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
220                         return -1;
221
222                 size2 = smb_len(cli->inbuf);
223
224                 if (size2 > readsize) {
225                         DEBUG(5,("server returned more than we wanted!\n"));
226                         return -1;
227                 } else if (size2 < 0) {
228                         DEBUG(5,("read return < 0!\n"));
229                         return -1;
230                 }
231
232                 /* Copy data into buffer */
233
234                 if (size2) {
235                         p = cli->inbuf + 4;
236                         memcpy(buf + total, p, size2);
237                 }
238
239                 total += size2;
240                 offset += size2;
241
242                 /*
243                  * If the server returned less than we asked for we're at EOF.
244                  */
245
246                 if (size2 < readsize)
247                         break;
248         }
249
250         return total;
251 }
252 #endif
253 /****************************************************************************
254 issue a single SMBwrite and don't wait for a reply
255 ****************************************************************************/
256
257 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset, 
258                             uint16 mode, const char *buf,
259                             size_t size, int i)
260 {
261         char *p;
262         BOOL bigoffset = False;
263
264         if (size > cli->bufsize) {
265                 cli->outbuf = SMB_REALLOC(cli->outbuf, size + 1024);
266                 cli->inbuf = SMB_REALLOC(cli->inbuf, size + 1024);
267                 if (cli->outbuf == NULL || cli->inbuf == NULL)
268                         return False;
269                 cli->bufsize = size + 1024;
270         }
271
272         memset(cli->outbuf,'\0',smb_size);
273         memset(cli->inbuf,'\0',smb_size);
274
275         if ((SMB_BIG_UINT)offset >> 32) 
276                 bigoffset = True;
277
278         if (bigoffset)
279                 set_message(cli->outbuf,14,0,True);
280         else
281                 set_message(cli->outbuf,12,0,True);
282         
283         SCVAL(cli->outbuf,smb_com,SMBwriteX);
284         SSVAL(cli->outbuf,smb_tid,cli->cnum);
285         cli_setup_packet(cli);
286         
287         SCVAL(cli->outbuf,smb_vwv0,0xFF);
288         SSVAL(cli->outbuf,smb_vwv2,fnum);
289
290         SIVAL(cli->outbuf,smb_vwv3,offset);
291         SIVAL(cli->outbuf,smb_vwv5,0);
292         SSVAL(cli->outbuf,smb_vwv7,mode);
293
294         SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
295         /*
296          * According to CIFS-TR-1p00, this following field should only
297          * be set if CAP_LARGE_WRITEX is set. We should check this
298          * locally. However, this check might already have been
299          * done by our callers.
300          */
301         SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
302         SSVAL(cli->outbuf,smb_vwv10,size);
303         SSVAL(cli->outbuf,smb_vwv11,
304               smb_buf(cli->outbuf) - smb_base(cli->outbuf));
305
306         if (bigoffset)
307                 SIVAL(cli->outbuf,smb_vwv12,(offset>>32) & 0xffffffff);
308         
309         p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
310         memcpy(p, buf, size);
311         cli_setup_bcc(cli, p+size);
312
313         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
314         
315         show_msg(cli->outbuf);
316         return cli_send_smb(cli);
317 }
318
319 /****************************************************************************
320   write to a file
321   write_mode: 0x0001 disallow write cacheing
322               0x0002 return bytes remaining
323               0x0004 use raw named pipe protocol
324               0x0008 start of message mode named pipe protocol
325 ****************************************************************************/
326
327 size_t cli_write(struct cli_state *cli,
328                  int fnum, uint16 write_mode,
329                  const char *buf, off_t offset, size_t size)
330 {
331         int bwritten = 0;
332         int issued = 0;
333         int received = 0;
334         int mpx = 1;
335         int block = cli->max_xmit - (smb_size+32);
336         int blocks = (size + (block-1)) / block;
337
338         if(cli->max_mux > 1) {
339                 mpx = cli->max_mux-1;
340         } else {
341                 mpx = 1;
342         }
343
344         while (received < blocks) {
345
346                 while ((issued - received < mpx) && (issued < blocks)) {
347                         int bsent = issued * block;
348                         int size1 = MIN(block, size - bsent);
349
350                         if (!cli_issue_write(cli, fnum, offset + bsent,
351                                         write_mode,
352                                         buf + bsent,
353                                         size1, issued))
354                                 return -1;
355                         issued++;
356                 }
357
358                 if (!cli_receive_smb(cli))
359                         return bwritten;
360
361                 received++;
362
363                 if (cli_is_error(cli))
364                         break;
365
366                 bwritten += SVAL(cli->inbuf, smb_vwv2);
367                 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
368         }
369
370         while (received < issued && cli_receive_smb(cli))
371                 received++;
372         
373         return bwritten;
374 }
375
376 /****************************************************************************
377   write to a file using a SMBwrite and not bypassing 0 byte writes
378 ****************************************************************************/
379
380 ssize_t cli_smbwrite(struct cli_state *cli,
381                      int fnum, char *buf, off_t offset, size_t size1)
382 {
383         char *p;
384         ssize_t total = 0;
385
386         do {
387                 size_t size = MIN(size1, cli->max_xmit - 48);
388                 
389                 memset(cli->outbuf,'\0',smb_size);
390                 memset(cli->inbuf,'\0',smb_size);
391
392                 set_message(cli->outbuf,5, 0,True);
393
394                 SCVAL(cli->outbuf,smb_com,SMBwrite);
395                 SSVAL(cli->outbuf,smb_tid,cli->cnum);
396                 cli_setup_packet(cli);
397                 
398                 SSVAL(cli->outbuf,smb_vwv0,fnum);
399                 SSVAL(cli->outbuf,smb_vwv1,size);
400                 SIVAL(cli->outbuf,smb_vwv2,offset);
401                 SSVAL(cli->outbuf,smb_vwv4,0);
402                 
403                 p = smb_buf(cli->outbuf);
404                 *p++ = 1;
405                 SSVAL(p, 0, size); p += 2;
406                 memcpy(p, buf, size); p += size;
407
408                 cli_setup_bcc(cli, p);
409                 
410                 if (!cli_send_smb(cli))
411                         return -1;
412
413                 if (!cli_receive_smb(cli))
414                         return -1;
415                 
416                 if (cli_is_error(cli))
417                         return -1;
418
419                 size = SVAL(cli->inbuf,smb_vwv0);
420                 if (size == 0)
421                         break;
422
423                 size1 -= size;
424                 total += size;
425                 offset += size;
426
427         } while (size1);
428
429         return total;
430 }