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 3 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.
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,
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)
38 set_message(NULL,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));
50 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
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)
69 /* We can only do direct reads if not signing. */
70 BOOL direct_reads = !client_is_signing_on(cli);
76 * Set readsize to the maximum size we can handle in one readX,
77 * rounded down to a multiple of 1024.
80 if (client_is_signing_on(cli) == False &&
81 cli_encryption_on(cli) == False &&
82 (cli->posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
83 readsize = CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
84 } else if (cli->capabilities & CAP_LARGE_READX) {
86 readsize = CLI_SAMBA_MAX_LARGE_READX_SIZE;
88 readsize = CLI_WINDOWS_MAX_LARGE_READX_SIZE;
91 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
94 while (total < size) {
95 readsize = MIN(readsize, size-total);
97 /* Issue a read and receive a reply */
99 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
103 if (!cli_receive_smb_readX_header(cli))
106 if (!cli_receive_smb(cli))
110 /* Check for error. Make sure to check for DOS and NT
113 if (cli_is_error(cli)) {
114 BOOL recoverable_error = False;
115 NTSTATUS status = NT_STATUS_OK;
119 if (cli_is_nt_error(cli))
120 status = cli_nt_error(cli);
122 cli_dos_error(cli, &eclass, &ecode);
125 * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
126 * recoverable error, plus we have valid data in the
127 * packet so don't error out here.
130 if ((eclass == ERRDOS && ecode == ERRmoredata) ||
131 NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
132 recoverable_error = True;
134 if (!recoverable_error)
138 size2 = SVAL(cli->inbuf, smb_vwv5);
139 size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7))) << 16);
141 if (size2 > readsize) {
142 DEBUG(5,("server returned more than we wanted!\n"));
144 } else if (size2 < 0) {
145 DEBUG(5,("read return < 0!\n"));
150 /* Copy data into buffer */
151 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
152 memcpy(buf + total, p, size2);
154 /* Ensure the remaining data matches the return size. */
155 ssize_t toread = smb_len_large(cli->inbuf) - SVAL(cli->inbuf,smb_vwv6);
157 /* Ensure the size is correct. */
158 if (toread != size2) {
159 DEBUG(5,("direct read logic fail toread (%d) != size2 (%u)\n",
160 (int)toread, (unsigned int)size2 ));
164 /* Read data directly into buffer */
165 toread = cli_receive_smb_data(cli,buf+total,size2);
166 if (toread != size2) {
167 DEBUG(5,("direct read read failure toread (%d) != size2 (%u)\n",
168 (int)toread, (unsigned int)size2 ));
177 * If the server returned less than we asked for we're at EOF.
180 if (size2 < readsize)
187 #if 0 /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
189 /* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
190 you must fix ensure you don't attempt to sign the packets - data
191 *will* be currupted */
193 /****************************************************************************
194 Issue a single SMBreadraw and don't wait for a reply.
195 ****************************************************************************/
197 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
201 if (!cli->sign_info.use_smb_signing) {
202 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
206 memset(cli->outbuf,'\0',smb_size);
207 memset(cli->inbuf,'\0',smb_size);
209 set_message(NULL,cli->outbuf,10,0,True);
211 SCVAL(cli->outbuf,smb_com,SMBreadbraw);
212 SSVAL(cli->outbuf,smb_tid,cli->cnum);
213 cli_setup_packet(cli);
215 SSVAL(cli->outbuf,smb_vwv0,fnum);
216 SIVAL(cli->outbuf,smb_vwv1,offset);
217 SSVAL(cli->outbuf,smb_vwv2,size);
218 SSVAL(cli->outbuf,smb_vwv3,size);
219 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
221 return cli_send_smb(cli);
224 /****************************************************************************
225 Tester for the readraw call.
226 ****************************************************************************/
228 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
239 * Set readsize to the maximum size we can handle in one readraw.
244 while (total < size) {
245 readsize = MIN(readsize, size-total);
247 /* Issue a read and receive a reply */
249 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
252 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
255 size2 = smb_len(cli->inbuf);
257 if (size2 > readsize) {
258 DEBUG(5,("server returned more than we wanted!\n"));
260 } else if (size2 < 0) {
261 DEBUG(5,("read return < 0!\n"));
265 /* Copy data into buffer */
269 memcpy(buf + total, p, size2);
276 * If the server returned less than we asked for we're at EOF.
279 if (size2 < readsize)
286 /****************************************************************************
287 issue a single SMBwrite and don't wait for a reply
288 ****************************************************************************/
290 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset,
291 uint16 mode, const char *buf,
295 BOOL large_writex = False;
297 if (size > cli->bufsize) {
298 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
302 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
303 if (cli->inbuf == NULL) {
304 SAFE_FREE(cli->outbuf);
307 cli->bufsize = size + 1024;
310 memset(cli->outbuf,'\0',smb_size);
311 memset(cli->inbuf,'\0',smb_size);
313 if (((SMB_BIG_UINT)offset >> 32) || (size > 0xFFFF)) {
318 set_message(NULL,cli->outbuf,14,0,True);
320 set_message(NULL,cli->outbuf,12,0,True);
322 SCVAL(cli->outbuf,smb_com,SMBwriteX);
323 SSVAL(cli->outbuf,smb_tid,cli->cnum);
324 cli_setup_packet(cli);
326 SCVAL(cli->outbuf,smb_vwv0,0xFF);
327 SSVAL(cli->outbuf,smb_vwv2,fnum);
329 SIVAL(cli->outbuf,smb_vwv3,offset);
330 SIVAL(cli->outbuf,smb_vwv5,0);
331 SSVAL(cli->outbuf,smb_vwv7,mode);
333 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
335 * According to CIFS-TR-1p00, this following field should only
336 * be set if CAP_LARGE_WRITEX is set. We should check this
337 * locally. However, this check might already have been
338 * done by our callers.
340 SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
341 SSVAL(cli->outbuf,smb_vwv10,size);
342 SSVAL(cli->outbuf,smb_vwv11,
343 smb_buf(cli->outbuf) - smb_base(cli->outbuf));
346 SIVAL(cli->outbuf,smb_vwv12,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
349 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
350 memcpy(p, buf, size);
351 cli_setup_bcc(cli, p+size);
353 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
355 show_msg(cli->outbuf);
356 return cli_send_smb(cli);
359 /****************************************************************************
361 write_mode: 0x0001 disallow write cacheing
362 0x0002 return bytes remaining
363 0x0004 use raw named pipe protocol
364 0x0008 start of message mode named pipe protocol
365 ****************************************************************************/
367 ssize_t cli_write(struct cli_state *cli,
368 int fnum, uint16 write_mode,
369 const char *buf, off_t offset, size_t size)
371 ssize_t bwritten = 0;
372 unsigned int issued = 0;
373 unsigned int received = 0;
375 int block = cli->max_xmit - (smb_size+32);
376 int blocks = (size + (block-1)) / block;
378 if(cli->max_mux > 1) {
379 mpx = cli->max_mux-1;
384 while (received < blocks) {
386 while ((issued - received < mpx) && (issued < blocks)) {
387 ssize_t bsent = issued * block;
388 ssize_t size1 = MIN(block, size - bsent);
390 if (!cli_issue_write(cli, fnum, offset + bsent,
398 if (!cli_receive_smb(cli))
403 if (cli_is_error(cli))
406 bwritten += SVAL(cli->inbuf, smb_vwv2);
407 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
410 while (received < issued && cli_receive_smb(cli))
416 /****************************************************************************
417 write to a file using a SMBwrite and not bypassing 0 byte writes
418 ****************************************************************************/
420 ssize_t cli_smbwrite(struct cli_state *cli,
421 int fnum, char *buf, off_t offset, size_t size1)
427 size_t size = MIN(size1, cli->max_xmit - 48);
429 memset(cli->outbuf,'\0',smb_size);
430 memset(cli->inbuf,'\0',smb_size);
432 set_message(NULL,cli->outbuf,5, 0,True);
434 SCVAL(cli->outbuf,smb_com,SMBwrite);
435 SSVAL(cli->outbuf,smb_tid,cli->cnum);
436 cli_setup_packet(cli);
438 SSVAL(cli->outbuf,smb_vwv0,fnum);
439 SSVAL(cli->outbuf,smb_vwv1,size);
440 SIVAL(cli->outbuf,smb_vwv2,offset);
441 SSVAL(cli->outbuf,smb_vwv4,0);
443 p = smb_buf(cli->outbuf);
445 SSVAL(p, 0, size); p += 2;
446 memcpy(p, buf + total, size); p += size;
448 cli_setup_bcc(cli, p);
450 if (!cli_send_smb(cli))
453 if (!cli_receive_smb(cli))
456 if (cli_is_error(cli))
459 size = SVAL(cli->inbuf,smb_vwv0);