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