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