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