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