use cli_is_error() instead of looking in smb_rcls, otherwise NT status
[ira/wip.git] / source3 / libsmb / clireadwrite.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    client file read/write routines
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 /****************************************************************************
27 Issue a single SMBread and don't wait for a reply.
28 ****************************************************************************/
29
30 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset, 
31                            size_t size, int i)
32 {
33         memset(cli->outbuf,'\0',smb_size);
34         memset(cli->inbuf,'\0',smb_size);
35
36         set_message(cli->outbuf,10,0,True);
37                 
38         CVAL(cli->outbuf,smb_com) = SMBreadX;
39         SSVAL(cli->outbuf,smb_tid,cli->cnum);
40         cli_setup_packet(cli);
41
42         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
43         SSVAL(cli->outbuf,smb_vwv2,fnum);
44         SIVAL(cli->outbuf,smb_vwv3,offset);
45         SSVAL(cli->outbuf,smb_vwv5,size);
46         SSVAL(cli->outbuf,smb_vwv6,size);
47         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
48
49         return cli_send_smb(cli);
50 }
51
52 /****************************************************************************
53 Issue a single SMBreadraw and don't wait for a reply.
54 ****************************************************************************/
55
56 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset, 
57                            size_t size, int i)
58 {
59         memset(cli->outbuf,'\0',smb_size);
60         memset(cli->inbuf,'\0',smb_size);
61
62         set_message(cli->outbuf,10,0,True);
63                 
64         CVAL(cli->outbuf,smb_com) = SMBreadbraw;
65         SSVAL(cli->outbuf,smb_tid,cli->cnum);
66         cli_setup_packet(cli);
67
68         SSVAL(cli->outbuf,smb_vwv0,fnum);
69         SIVAL(cli->outbuf,smb_vwv1,offset);
70         SSVAL(cli->outbuf,smb_vwv2,size);
71         SSVAL(cli->outbuf,smb_vwv3,size);
72         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
73
74         return cli_send_smb(cli);
75 }
76
77 /****************************************************************************
78   Read size bytes at offset offset using SMBreadX.
79 ****************************************************************************/
80
81 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
82 {
83         char *p;
84         int size2;
85         int readsize;
86         ssize_t total = 0;
87
88         if (size == 0) 
89                 return 0;
90
91         /*
92          * Set readsize to the maximum size we can handle in one readX,
93          * rounded down to a multiple of 1024.
94          */
95
96         readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
97
98         while (total < size) {
99                 readsize = MIN(readsize, size-total);
100
101                 /* Issue a read and receive a reply */
102
103                 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
104                         return -1;
105
106                 if (!cli_receive_smb(cli))
107                         return -1;
108
109                 /* Check for error.  Make sure to check for DOS and NT
110                    errors. */
111
112                 if (cli_is_error(cli)) {
113                         NTSTATUS status = NT_STATUS_OK;
114                         uint8 eclass = 0;
115                         uint32 ecode = 0;
116
117                         if (cli_is_nt_error(cli))
118                                 status = cli_nt_error(cli);
119                         else
120                                 cli_dos_error(cli, &eclass, &ecode);
121
122                         if ((eclass == ERRDOS && ecode == ERRmoredata) ||
123                             NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
124                                 return -1;
125                 }
126
127                 size2 = SVAL(cli->inbuf, smb_vwv5);
128
129                 if (size2 > readsize) {
130                         DEBUG(5,("server returned more than we wanted!\n"));
131                         return -1;
132                 } else if (size2 < 0) {
133                         DEBUG(5,("read return < 0!\n"));
134                         return -1;
135                 }
136
137                 /* Copy data into buffer */
138
139                 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
140                 memcpy(buf + total, p, size2);
141
142                 total += size2;
143                 offset += size2;
144
145                 /*
146                  * If the server returned less than we asked for we're at EOF.
147                  */
148
149                 if (size2 < readsize)
150                         break;
151         }
152
153         return total;
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
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         memset(cli->outbuf,'\0',smb_size);
228         memset(cli->inbuf,'\0',smb_size);
229
230         if (size > 0xFFFF)
231                 set_message(cli->outbuf,14,0,True);
232         else
233                 set_message(cli->outbuf,12,0,True);
234         
235         CVAL(cli->outbuf,smb_com) = SMBwriteX;
236         SSVAL(cli->outbuf,smb_tid,cli->cnum);
237         cli_setup_packet(cli);
238         
239         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
240         SSVAL(cli->outbuf,smb_vwv2,fnum);
241
242         SIVAL(cli->outbuf,smb_vwv3,offset);
243         SIVAL(cli->outbuf,smb_vwv5,(mode & 0x0008) ? 0xFFFFFFFF : 0);
244         SSVAL(cli->outbuf,smb_vwv7,mode);
245
246         SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
247         SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
248         SSVAL(cli->outbuf,smb_vwv10,size);
249         SSVAL(cli->outbuf,smb_vwv11,
250               smb_buf(cli->outbuf) - smb_base(cli->outbuf));
251         
252         p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
253         memcpy(p, buf, size);
254         cli_setup_bcc(cli, p+size);
255
256         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
257         
258         show_msg(cli->outbuf);
259         return cli_send_smb(cli);
260 }
261
262 /****************************************************************************
263   write to a file
264   write_mode: 0x0001 disallow write cacheing
265               0x0002 return bytes remaining
266               0x0004 use raw named pipe protocol
267               0x0008 start of message mode named pipe protocol
268 ****************************************************************************/
269
270 ssize_t cli_write(struct cli_state *cli,
271                   int fnum, uint16 write_mode,
272                   char *buf, off_t offset, size_t size)
273 {
274         int bwritten = 0;
275         int issued = 0;
276         int received = 0;
277         int mpx = MAX(cli->max_mux-1, 1);
278         int block = (cli->max_xmit - (smb_size+32)) & ~1023;
279         int blocks = (size + (block-1)) / block;
280
281         while (received < blocks) {
282
283                 while ((issued - received < mpx) && (issued < blocks)) {
284                         int bsent = issued * block;
285                         int size1 = MIN(block, size - bsent);
286
287                         if (!cli_issue_write(cli, fnum, offset + bsent,
288                                         write_mode,
289                                         buf + bsent,
290                                         size1, issued))
291                                 return -1;
292                         issued++;
293                 }
294
295                 if (!cli_receive_smb(cli))
296                         return bwritten;
297
298                 received++;
299
300                 if (cli_is_error(cli))
301                         break;
302
303                 bwritten += SVAL(cli->inbuf, smb_vwv2);
304         }
305
306         while (received < issued && cli_receive_smb(cli))
307                 received++;
308         
309         return bwritten;
310 }
311
312 /****************************************************************************
313   write to a file using a SMBwrite and not bypassing 0 byte writes
314 ****************************************************************************/
315
316 ssize_t cli_smbwrite(struct cli_state *cli,
317                      int fnum, char *buf, off_t offset, size_t size1)
318 {
319         char *p;
320         ssize_t total = 0;
321
322         do {
323                 size_t size = MIN(size1, cli->max_xmit - 48);
324                 
325                 memset(cli->outbuf,'\0',smb_size);
326                 memset(cli->inbuf,'\0',smb_size);
327
328                 set_message(cli->outbuf,5, 0,True);
329
330                 CVAL(cli->outbuf,smb_com) = SMBwrite;
331                 SSVAL(cli->outbuf,smb_tid,cli->cnum);
332                 cli_setup_packet(cli);
333                 
334                 SSVAL(cli->outbuf,smb_vwv0,fnum);
335                 SSVAL(cli->outbuf,smb_vwv1,size);
336                 SIVAL(cli->outbuf,smb_vwv2,offset);
337                 SSVAL(cli->outbuf,smb_vwv4,0);
338                 
339                 p = smb_buf(cli->outbuf);
340                 *p++ = 1;
341                 SSVAL(p, 0, size); p += 2;
342                 memcpy(p, buf, size); p += size;
343
344                 cli_setup_bcc(cli, p);
345                 
346                 if (!cli_send_smb(cli))
347                         return -1;
348
349                 if (!cli_receive_smb(cli))
350                         return -1;
351                 
352                 if (cli_is_error(cli))
353                         return -1;
354
355                 size = SVAL(cli->inbuf,smb_vwv0);
356                 if (size == 0)
357                         break;
358
359                 size1 -= size;
360                 total += size;
361         } while (size1);
362
363         return total;
364 }