r13119: Fix for #1779 from William Jojo <jojowil@hvcc.edu>
[Samba/nascimento.git] / source / libsmb / clireadwrite.c
bloba080bd3c64d7208e7b3d78ce9f24f7bf5cdb3489
1 /*
2 Unix SMB/CIFS implementation.
3 client file read/write routines
4 Copyright (C) Andrew Tridgell 1994-1998
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.
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.
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.
21 #include "includes.h"
23 /****************************************************************************
24 Issue a single SMBread and don't wait for a reply.
25 ****************************************************************************/
27 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
28 size_t size, int i)
30 BOOL bigoffset = False;
32 memset(cli->outbuf,'\0',smb_size);
33 memset(cli->inbuf,'\0',smb_size);
35 if ((SMB_BIG_UINT)offset >> 32)
36 bigoffset = True;
38 set_message(cli->outbuf,bigoffset ? 12 : 10,0,True);
40 SCVAL(cli->outbuf,smb_com,SMBreadX);
41 SSVAL(cli->outbuf,smb_tid,cli->cnum);
42 cli_setup_packet(cli);
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);
52 if (bigoffset) {
53 SIVAL(cli->outbuf,smb_vwv10,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
56 return cli_send_smb(cli);
59 /****************************************************************************
60 Read size bytes at offset offset using SMBreadX.
61 ****************************************************************************/
63 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
65 char *p;
66 int size2;
67 int readsize;
68 ssize_t total = 0;
70 if (size == 0)
71 return 0;
74 * Set readsize to the maximum size we can handle in one readX,
75 * rounded down to a multiple of 1024.
78 if (cli->capabilities & CAP_LARGE_READX) {
79 readsize = CLI_MAX_LARGE_READX_SIZE;
80 } else {
81 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
84 while (total < size) {
85 readsize = MIN(readsize, size-total);
87 /* Issue a read and receive a reply */
89 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
90 return -1;
92 if (!cli_receive_smb(cli))
93 return -1;
95 /* Check for error. Make sure to check for DOS and NT
96 errors. */
98 if (cli_is_error(cli)) {
99 BOOL recoverable_error = False;
100 NTSTATUS status = NT_STATUS_OK;
101 uint8 eclass = 0;
102 uint32 ecode = 0;
104 if (cli_is_nt_error(cli))
105 status = cli_nt_error(cli);
106 else
107 cli_dos_error(cli, &eclass, &ecode);
110 * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
111 * recoverable error, plus we have valid data in the
112 * packet so don't error out here.
115 if ((eclass == ERRDOS && ecode == ERRmoredata) ||
116 NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
117 recoverable_error = True;
119 if (!recoverable_error)
120 return -1;
123 size2 = SVAL(cli->inbuf, smb_vwv5);
124 size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7) & 1)) << 16);
126 if (size2 > readsize) {
127 DEBUG(5,("server returned more than we wanted!\n"));
128 return -1;
129 } else if (size2 < 0) {
130 DEBUG(5,("read return < 0!\n"));
131 return -1;
134 /* Copy data into buffer */
136 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
137 memcpy(buf + total, p, size2);
139 total += size2;
140 offset += size2;
143 * If the server returned less than we asked for we're at EOF.
146 if (size2 < readsize)
147 break;
150 return total;
153 #if 0 /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
155 /* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
156 you must fix ensure you don't attempt to sign the packets - data
157 *will* be currupted */
159 /****************************************************************************
160 Issue a single SMBreadraw and don't wait for a reply.
161 ****************************************************************************/
163 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
164 size_t size, int i)
167 if (!cli->sign_info.use_smb_signing) {
168 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
169 return False;
172 memset(cli->outbuf,'\0',smb_size);
173 memset(cli->inbuf,'\0',smb_size);
175 set_message(cli->outbuf,10,0,True);
177 SCVAL(cli->outbuf,smb_com,SMBreadbraw);
178 SSVAL(cli->outbuf,smb_tid,cli->cnum);
179 cli_setup_packet(cli);
181 SSVAL(cli->outbuf,smb_vwv0,fnum);
182 SIVAL(cli->outbuf,smb_vwv1,offset);
183 SSVAL(cli->outbuf,smb_vwv2,size);
184 SSVAL(cli->outbuf,smb_vwv3,size);
185 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
187 return cli_send_smb(cli);
190 /****************************************************************************
191 Tester for the readraw call.
192 ****************************************************************************/
194 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
196 char *p;
197 int size2;
198 size_t readsize;
199 ssize_t total = 0;
201 if (size == 0)
202 return 0;
205 * Set readsize to the maximum size we can handle in one readraw.
208 readsize = 0xFFFF;
210 while (total < size) {
211 readsize = MIN(readsize, size-total);
213 /* Issue a read and receive a reply */
215 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
216 return -1;
218 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
219 return -1;
221 size2 = smb_len(cli->inbuf);
223 if (size2 > readsize) {
224 DEBUG(5,("server returned more than we wanted!\n"));
225 return -1;
226 } else if (size2 < 0) {
227 DEBUG(5,("read return < 0!\n"));
228 return -1;
231 /* Copy data into buffer */
233 if (size2) {
234 p = cli->inbuf + 4;
235 memcpy(buf + total, p, size2);
238 total += size2;
239 offset += size2;
242 * If the server returned less than we asked for we're at EOF.
245 if (size2 < readsize)
246 break;
249 return total;
251 #endif
252 /****************************************************************************
253 issue a single SMBwrite and don't wait for a reply
254 ****************************************************************************/
256 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset,
257 uint16 mode, const char *buf,
258 size_t size, int i)
260 char *p;
261 BOOL large_writex = False;
263 if (size > cli->bufsize) {
264 cli->outbuf = SMB_REALLOC(cli->outbuf, size + 1024);
265 cli->inbuf = SMB_REALLOC(cli->inbuf, size + 1024);
266 if (cli->outbuf == NULL || cli->inbuf == NULL)
267 return False;
268 cli->bufsize = size + 1024;
271 memset(cli->outbuf,'\0',smb_size);
272 memset(cli->inbuf,'\0',smb_size);
274 if (((SMB_BIG_UINT)offset >> 32) || (size > 0xFFFF)) {
275 large_writex = True;
278 if (large_writex)
279 set_message(cli->outbuf,14,0,True);
280 else
281 set_message(cli->outbuf,12,0,True);
283 SCVAL(cli->outbuf,smb_com,SMBwriteX);
284 SSVAL(cli->outbuf,smb_tid,cli->cnum);
285 cli_setup_packet(cli);
287 SCVAL(cli->outbuf,smb_vwv0,0xFF);
288 SSVAL(cli->outbuf,smb_vwv2,fnum);
290 SIVAL(cli->outbuf,smb_vwv3,offset);
291 SIVAL(cli->outbuf,smb_vwv5,0);
292 SSVAL(cli->outbuf,smb_vwv7,mode);
294 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
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.
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));
306 if (large_writex) {
307 SIVAL(cli->outbuf,smb_vwv12,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
310 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
311 memcpy(p, buf, size);
312 cli_setup_bcc(cli, p+size);
314 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
316 show_msg(cli->outbuf);
317 return cli_send_smb(cli);
320 /****************************************************************************
321 write to a file
322 write_mode: 0x0001 disallow write cacheing
323 0x0002 return bytes remaining
324 0x0004 use raw named pipe protocol
325 0x0008 start of message mode named pipe protocol
326 ****************************************************************************/
328 ssize_t cli_write(struct cli_state *cli,
329 int fnum, uint16 write_mode,
330 const char *buf, off_t offset, size_t size)
332 ssize_t bwritten = 0;
333 unsigned int issued = 0;
334 unsigned int received = 0;
335 int mpx = 1;
336 int block = cli->max_xmit - (smb_size+32);
337 int blocks = (size + (block-1)) / block;
339 if(cli->max_mux > 1) {
340 mpx = cli->max_mux-1;
341 } else {
342 mpx = 1;
345 while (received < blocks) {
347 while ((issued - received < mpx) && (issued < blocks)) {
348 ssize_t bsent = issued * block;
349 ssize_t size1 = MIN(block, size - bsent);
351 if (!cli_issue_write(cli, fnum, offset + bsent,
352 write_mode,
353 buf + bsent,
354 size1, issued))
355 return -1;
356 issued++;
359 if (!cli_receive_smb(cli))
360 return bwritten;
362 received++;
364 if (cli_is_error(cli))
365 break;
367 bwritten += SVAL(cli->inbuf, smb_vwv2);
368 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
371 while (received < issued && cli_receive_smb(cli))
372 received++;
374 return bwritten;
377 /****************************************************************************
378 write to a file using a SMBwrite and not bypassing 0 byte writes
379 ****************************************************************************/
381 ssize_t cli_smbwrite(struct cli_state *cli,
382 int fnum, char *buf, off_t offset, size_t size1)
384 char *p;
385 ssize_t total = 0;
387 do {
388 size_t size = MIN(size1, cli->max_xmit - 48);
390 memset(cli->outbuf,'\0',smb_size);
391 memset(cli->inbuf,'\0',smb_size);
393 set_message(cli->outbuf,5, 0,True);
395 SCVAL(cli->outbuf,smb_com,SMBwrite);
396 SSVAL(cli->outbuf,smb_tid,cli->cnum);
397 cli_setup_packet(cli);
399 SSVAL(cli->outbuf,smb_vwv0,fnum);
400 SSVAL(cli->outbuf,smb_vwv1,size);
401 SIVAL(cli->outbuf,smb_vwv2,offset);
402 SSVAL(cli->outbuf,smb_vwv4,0);
404 p = smb_buf(cli->outbuf);
405 *p++ = 1;
406 SSVAL(p, 0, size); p += 2;
407 memcpy(p, buf, size); p += size;
409 cli_setup_bcc(cli, p);
411 if (!cli_send_smb(cli))
412 return -1;
414 if (!cli_receive_smb(cli))
415 return -1;
417 if (cli_is_error(cli))
418 return -1;
420 size = SVAL(cli->inbuf,smb_vwv0);
421 if (size == 0)
422 break;
424 size1 -= size;
425 total += size;
426 offset += size;
428 } while (size1);
430 return total;