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.
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(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);
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)
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) {
80
readsize = CLI_SAMBA_MAX_LARGE_READX_SIZE;
82
readsize = CLI_WINDOWS_MAX_LARGE_READX_SIZE;
85
readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
88
while (total < size) {
89
readsize = MIN(readsize, size-total);
91
/* Issue a read and receive a reply */
93
if (!cli_issue_read(cli, fnum, offset, readsize, 0))
96
if (!cli_receive_smb(cli))
99
/* Check for error. Make sure to check for DOS and NT
102
if (cli_is_error(cli)) {
103
BOOL recoverable_error = False;
104
NTSTATUS status = NT_STATUS_OK;
108
if (cli_is_nt_error(cli))
109
status = cli_nt_error(cli);
111
cli_dos_error(cli, &eclass, &ecode);
114
* ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
115
* recoverable error, plus we have valid data in the
116
* packet so don't error out here.
119
if ((eclass == ERRDOS && ecode == ERRmoredata) ||
120
NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
121
recoverable_error = True;
123
if (!recoverable_error)
127
size2 = SVAL(cli->inbuf, smb_vwv5);
128
size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7) & 1)) << 16);
130
if (size2 > readsize) {
131
DEBUG(5,("server returned more than we wanted!\n"));
133
} else if (size2 < 0) {
134
DEBUG(5,("read return < 0!\n"));
138
/* Copy data into buffer */
140
p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
141
memcpy(buf + total, p, size2);
147
* If the server returned less than we asked for we're at EOF.
150
if (size2 < readsize)
157
#if 0 /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
159
/* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
160
you must fix ensure you don't attempt to sign the packets - data
161
*will* be currupted */
163
/****************************************************************************
164
Issue a single SMBreadraw and don't wait for a reply.
165
****************************************************************************/
167
static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
171
if (!cli->sign_info.use_smb_signing) {
172
DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
176
memset(cli->outbuf,'\0',smb_size);
177
memset(cli->inbuf,'\0',smb_size);
179
set_message(cli->outbuf,10,0,True);
181
SCVAL(cli->outbuf,smb_com,SMBreadbraw);
182
SSVAL(cli->outbuf,smb_tid,cli->cnum);
183
cli_setup_packet(cli);
185
SSVAL(cli->outbuf,smb_vwv0,fnum);
186
SIVAL(cli->outbuf,smb_vwv1,offset);
187
SSVAL(cli->outbuf,smb_vwv2,size);
188
SSVAL(cli->outbuf,smb_vwv3,size);
189
SSVAL(cli->outbuf,smb_mid,cli->mid + i);
191
return cli_send_smb(cli);
194
/****************************************************************************
195
Tester for the readraw call.
196
****************************************************************************/
198
ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
209
* Set readsize to the maximum size we can handle in one readraw.
214
while (total < size) {
215
readsize = MIN(readsize, size-total);
217
/* Issue a read and receive a reply */
219
if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
222
if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
225
size2 = smb_len(cli->inbuf);
227
if (size2 > readsize) {
228
DEBUG(5,("server returned more than we wanted!\n"));
230
} else if (size2 < 0) {
231
DEBUG(5,("read return < 0!\n"));
235
/* Copy data into buffer */
239
memcpy(buf + total, p, size2);
246
* If the server returned less than we asked for we're at EOF.
249
if (size2 < readsize)
256
/****************************************************************************
257
issue a single SMBwrite and don't wait for a reply
258
****************************************************************************/
260
static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset,
261
uint16 mode, const char *buf,
265
BOOL large_writex = False;
267
if (size > cli->bufsize) {
268
cli->outbuf = SMB_REALLOC(cli->outbuf, size + 1024);
272
cli->inbuf = SMB_REALLOC(cli->inbuf, size + 1024);
273
if (cli->inbuf == NULL) {
274
SAFE_FREE(cli->outbuf);
277
cli->bufsize = size + 1024;
280
memset(cli->outbuf,'\0',smb_size);
281
memset(cli->inbuf,'\0',smb_size);
283
if (((SMB_BIG_UINT)offset >> 32) || (size > 0xFFFF)) {
288
set_message(cli->outbuf,14,0,True);
290
set_message(cli->outbuf,12,0,True);
292
SCVAL(cli->outbuf,smb_com,SMBwriteX);
293
SSVAL(cli->outbuf,smb_tid,cli->cnum);
294
cli_setup_packet(cli);
296
SCVAL(cli->outbuf,smb_vwv0,0xFF);
297
SSVAL(cli->outbuf,smb_vwv2,fnum);
299
SIVAL(cli->outbuf,smb_vwv3,offset);
300
SIVAL(cli->outbuf,smb_vwv5,0);
301
SSVAL(cli->outbuf,smb_vwv7,mode);
303
SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
305
* According to CIFS-TR-1p00, this following field should only
306
* be set if CAP_LARGE_WRITEX is set. We should check this
307
* locally. However, this check might already have been
308
* done by our callers.
310
SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
311
SSVAL(cli->outbuf,smb_vwv10,size);
312
SSVAL(cli->outbuf,smb_vwv11,
313
smb_buf(cli->outbuf) - smb_base(cli->outbuf));
316
SIVAL(cli->outbuf,smb_vwv12,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
319
p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
320
memcpy(p, buf, size);
321
cli_setup_bcc(cli, p+size);
323
SSVAL(cli->outbuf,smb_mid,cli->mid + i);
325
show_msg(cli->outbuf);
326
return cli_send_smb(cli);
329
/****************************************************************************
331
write_mode: 0x0001 disallow write cacheing
332
0x0002 return bytes remaining
333
0x0004 use raw named pipe protocol
334
0x0008 start of message mode named pipe protocol
335
****************************************************************************/
337
ssize_t cli_write(struct cli_state *cli,
338
int fnum, uint16 write_mode,
339
const char *buf, off_t offset, size_t size)
341
ssize_t bwritten = 0;
342
unsigned int issued = 0;
343
unsigned int received = 0;
345
int block = cli->max_xmit - (smb_size+32);
346
int blocks = (size + (block-1)) / block;
348
if(cli->max_mux > 1) {
349
mpx = cli->max_mux-1;
354
while (received < blocks) {
356
while ((issued - received < mpx) && (issued < blocks)) {
357
ssize_t bsent = issued * block;
358
ssize_t size1 = MIN(block, size - bsent);
360
if (!cli_issue_write(cli, fnum, offset + bsent,
368
if (!cli_receive_smb(cli))
373
if (cli_is_error(cli))
376
bwritten += SVAL(cli->inbuf, smb_vwv2);
377
bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
380
while (received < issued && cli_receive_smb(cli))
386
/****************************************************************************
387
write to a file using a SMBwrite and not bypassing 0 byte writes
388
****************************************************************************/
390
ssize_t cli_smbwrite(struct cli_state *cli,
391
int fnum, char *buf, off_t offset, size_t size1)
397
size_t size = MIN(size1, cli->max_xmit - 48);
399
memset(cli->outbuf,'\0',smb_size);
400
memset(cli->inbuf,'\0',smb_size);
402
set_message(cli->outbuf,5, 0,True);
404
SCVAL(cli->outbuf,smb_com,SMBwrite);
405
SSVAL(cli->outbuf,smb_tid,cli->cnum);
406
cli_setup_packet(cli);
408
SSVAL(cli->outbuf,smb_vwv0,fnum);
409
SSVAL(cli->outbuf,smb_vwv1,size);
410
SIVAL(cli->outbuf,smb_vwv2,offset);
411
SSVAL(cli->outbuf,smb_vwv4,0);
413
p = smb_buf(cli->outbuf);
415
SSVAL(p, 0, size); p += 2;
416
memcpy(p, buf, size); p += size;
418
cli_setup_bcc(cli, p);
420
if (!cli_send_smb(cli))
423
if (!cli_receive_smb(cli))
426
if (cli_is_error(cli))
429
size = SVAL(cli->inbuf,smb_vwv0);