~vcs-imports/samba/main

« back to all changes in this revision

Viewing changes to source/libsmb/clireadwrite.c

  • Committer: jerry
  • Date: 2006-07-14 21:48:39 UTC
  • Revision ID: vcs-imports@canonical.com-20060714214839-586d8c489a8fcead
gutting trunk to move to svn:externals

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
   Unix SMB/CIFS implementation.
3
 
   client file read/write routines
4
 
   Copyright (C) Andrew Tridgell 1994-1998
5
 
   
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.
10
 
   
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.
15
 
   
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.
19
 
*/
20
 
 
21
 
#include "includes.h"
22
 
 
23
 
/****************************************************************************
24
 
Issue a single SMBread and don't wait for a reply.
25
 
****************************************************************************/
26
 
 
27
 
static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset, 
28
 
                           size_t size, int i)
29
 
{
30
 
        BOOL bigoffset = False;
31
 
 
32
 
        memset(cli->outbuf,'\0',smb_size);
33
 
        memset(cli->inbuf,'\0',smb_size);
34
 
 
35
 
        if ((SMB_BIG_UINT)offset >> 32) 
36
 
                bigoffset = True;
37
 
 
38
 
        set_message(cli->outbuf,bigoffset ? 12 : 10,0,True);
39
 
                
40
 
        SCVAL(cli->outbuf,smb_com,SMBreadX);
41
 
        SSVAL(cli->outbuf,smb_tid,cli->cnum);
42
 
        cli_setup_packet(cli);
43
 
 
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);
51
 
 
52
 
        if (bigoffset) {
53
 
                SIVAL(cli->outbuf,smb_vwv10,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
54
 
        }
55
 
 
56
 
        return cli_send_smb(cli);
57
 
}
58
 
 
59
 
/****************************************************************************
60
 
  Read size bytes at offset offset using SMBreadX.
61
 
****************************************************************************/
62
 
 
63
 
ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
64
 
{
65
 
        char *p;
66
 
        int size2;
67
 
        int readsize;
68
 
        ssize_t total = 0;
69
 
 
70
 
        if (size == 0) 
71
 
                return 0;
72
 
 
73
 
        /*
74
 
         * Set readsize to the maximum size we can handle in one readX,
75
 
         * rounded down to a multiple of 1024.
76
 
         */
77
 
 
78
 
        if (cli->capabilities & CAP_LARGE_READX) {
79
 
                if (cli->is_samba) {
80
 
                        readsize = CLI_SAMBA_MAX_LARGE_READX_SIZE;
81
 
                } else {
82
 
                        readsize = CLI_WINDOWS_MAX_LARGE_READX_SIZE;
83
 
                }
84
 
        } else {
85
 
                readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
86
 
        }
87
 
 
88
 
        while (total < size) {
89
 
                readsize = MIN(readsize, size-total);
90
 
 
91
 
                /* Issue a read and receive a reply */
92
 
 
93
 
                if (!cli_issue_read(cli, fnum, offset, readsize, 0))
94
 
                        return -1;
95
 
 
96
 
                if (!cli_receive_smb(cli))
97
 
                        return -1;
98
 
 
99
 
                /* Check for error.  Make sure to check for DOS and NT
100
 
                   errors. */
101
 
 
102
 
                if (cli_is_error(cli)) {
103
 
                        BOOL recoverable_error = False;
104
 
                        NTSTATUS status = NT_STATUS_OK;
105
 
                        uint8 eclass = 0;
106
 
                        uint32 ecode = 0;
107
 
 
108
 
                        if (cli_is_nt_error(cli))
109
 
                                status = cli_nt_error(cli);
110
 
                        else
111
 
                                cli_dos_error(cli, &eclass, &ecode);
112
 
 
113
 
                        /*
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.
117
 
                         */
118
 
 
119
 
                        if ((eclass == ERRDOS && ecode == ERRmoredata) ||
120
 
                            NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
121
 
                                recoverable_error = True;
122
 
 
123
 
                        if (!recoverable_error)
124
 
                                return -1;
125
 
                }
126
 
 
127
 
                size2 = SVAL(cli->inbuf, smb_vwv5);
128
 
                size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7) & 1)) << 16);
129
 
 
130
 
                if (size2 > readsize) {
131
 
                        DEBUG(5,("server returned more than we wanted!\n"));
132
 
                        return -1;
133
 
                } else if (size2 < 0) {
134
 
                        DEBUG(5,("read return < 0!\n"));
135
 
                        return -1;
136
 
                }
137
 
 
138
 
                /* Copy data into buffer */
139
 
 
140
 
                p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
141
 
                memcpy(buf + total, p, size2);
142
 
 
143
 
                total += size2;
144
 
                offset += size2;
145
 
 
146
 
                /*
147
 
                 * If the server returned less than we asked for we're at EOF.
148
 
                 */
149
 
 
150
 
                if (size2 < readsize)
151
 
                        break;
152
 
        }
153
 
 
154
 
        return total;
155
 
}
156
 
 
157
 
#if 0  /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
158
 
 
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 */
162
 
 
163
 
/****************************************************************************
164
 
Issue a single SMBreadraw and don't wait for a reply.
165
 
****************************************************************************/
166
 
 
167
 
static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset, 
168
 
                           size_t size, int i)
169
 
{
170
 
 
171
 
        if (!cli->sign_info.use_smb_signing) {
172
 
                DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
173
 
                return False;
174
 
        }
175
 
        
176
 
        memset(cli->outbuf,'\0',smb_size);
177
 
        memset(cli->inbuf,'\0',smb_size);
178
 
 
179
 
        set_message(cli->outbuf,10,0,True);
180
 
                
181
 
        SCVAL(cli->outbuf,smb_com,SMBreadbraw);
182
 
        SSVAL(cli->outbuf,smb_tid,cli->cnum);
183
 
        cli_setup_packet(cli);
184
 
 
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);
190
 
 
191
 
        return cli_send_smb(cli);
192
 
}
193
 
 
194
 
/****************************************************************************
195
 
 Tester for the readraw call.
196
 
****************************************************************************/
197
 
 
198
 
ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
199
 
{
200
 
        char *p;
201
 
        int size2;
202
 
        size_t readsize;
203
 
        ssize_t total = 0;
204
 
 
205
 
        if (size == 0) 
206
 
                return 0;
207
 
 
208
 
        /*
209
 
         * Set readsize to the maximum size we can handle in one readraw.
210
 
         */
211
 
 
212
 
        readsize = 0xFFFF;
213
 
 
214
 
        while (total < size) {
215
 
                readsize = MIN(readsize, size-total);
216
 
 
217
 
                /* Issue a read and receive a reply */
218
 
 
219
 
                if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
220
 
                        return -1;
221
 
 
222
 
                if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
223
 
                        return -1;
224
 
 
225
 
                size2 = smb_len(cli->inbuf);
226
 
 
227
 
                if (size2 > readsize) {
228
 
                        DEBUG(5,("server returned more than we wanted!\n"));
229
 
                        return -1;
230
 
                } else if (size2 < 0) {
231
 
                        DEBUG(5,("read return < 0!\n"));
232
 
                        return -1;
233
 
                }
234
 
 
235
 
                /* Copy data into buffer */
236
 
 
237
 
                if (size2) {
238
 
                        p = cli->inbuf + 4;
239
 
                        memcpy(buf + total, p, size2);
240
 
                }
241
 
 
242
 
                total += size2;
243
 
                offset += size2;
244
 
 
245
 
                /*
246
 
                 * If the server returned less than we asked for we're at EOF.
247
 
                 */
248
 
 
249
 
                if (size2 < readsize)
250
 
                        break;
251
 
        }
252
 
 
253
 
        return total;
254
 
}
255
 
#endif
256
 
/****************************************************************************
257
 
issue a single SMBwrite and don't wait for a reply
258
 
****************************************************************************/
259
 
 
260
 
static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset, 
261
 
                            uint16 mode, const char *buf,
262
 
                            size_t size, int i)
263
 
{
264
 
        char *p;
265
 
        BOOL large_writex = False;
266
 
 
267
 
        if (size > cli->bufsize) {
268
 
                cli->outbuf = SMB_REALLOC(cli->outbuf, size + 1024);
269
 
                if (!cli->outbuf) {
270
 
                        return False;
271
 
                }
272
 
                cli->inbuf = SMB_REALLOC(cli->inbuf, size + 1024);
273
 
                if (cli->inbuf == NULL) {
274
 
                        SAFE_FREE(cli->outbuf);
275
 
                        return False;
276
 
                }
277
 
                cli->bufsize = size + 1024;
278
 
        }
279
 
 
280
 
        memset(cli->outbuf,'\0',smb_size);
281
 
        memset(cli->inbuf,'\0',smb_size);
282
 
 
283
 
        if (((SMB_BIG_UINT)offset >> 32) || (size > 0xFFFF)) {
284
 
                large_writex = True;
285
 
        }
286
 
 
287
 
        if (large_writex)
288
 
                set_message(cli->outbuf,14,0,True);
289
 
        else
290
 
                set_message(cli->outbuf,12,0,True);
291
 
        
292
 
        SCVAL(cli->outbuf,smb_com,SMBwriteX);
293
 
        SSVAL(cli->outbuf,smb_tid,cli->cnum);
294
 
        cli_setup_packet(cli);
295
 
        
296
 
        SCVAL(cli->outbuf,smb_vwv0,0xFF);
297
 
        SSVAL(cli->outbuf,smb_vwv2,fnum);
298
 
 
299
 
        SIVAL(cli->outbuf,smb_vwv3,offset);
300
 
        SIVAL(cli->outbuf,smb_vwv5,0);
301
 
        SSVAL(cli->outbuf,smb_vwv7,mode);
302
 
 
303
 
        SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
304
 
        /*
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.
309
 
         */
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));
314
 
 
315
 
        if (large_writex) {
316
 
                SIVAL(cli->outbuf,smb_vwv12,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
317
 
        }
318
 
        
319
 
        p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
320
 
        memcpy(p, buf, size);
321
 
        cli_setup_bcc(cli, p+size);
322
 
 
323
 
        SSVAL(cli->outbuf,smb_mid,cli->mid + i);
324
 
        
325
 
        show_msg(cli->outbuf);
326
 
        return cli_send_smb(cli);
327
 
}
328
 
 
329
 
/****************************************************************************
330
 
  write to a file
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
 
****************************************************************************/
336
 
 
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)
340
 
{
341
 
        ssize_t bwritten = 0;
342
 
        unsigned int issued = 0;
343
 
        unsigned int received = 0;
344
 
        int mpx = 1;
345
 
        int block = cli->max_xmit - (smb_size+32);
346
 
        int blocks = (size + (block-1)) / block;
347
 
 
348
 
        if(cli->max_mux > 1) {
349
 
                mpx = cli->max_mux-1;
350
 
        } else {
351
 
                mpx = 1;
352
 
        }
353
 
 
354
 
        while (received < blocks) {
355
 
 
356
 
                while ((issued - received < mpx) && (issued < blocks)) {
357
 
                        ssize_t bsent = issued * block;
358
 
                        ssize_t size1 = MIN(block, size - bsent);
359
 
 
360
 
                        if (!cli_issue_write(cli, fnum, offset + bsent,
361
 
                                        write_mode,
362
 
                                        buf + bsent,
363
 
                                        size1, issued))
364
 
                                return -1;
365
 
                        issued++;
366
 
                }
367
 
 
368
 
                if (!cli_receive_smb(cli))
369
 
                        return bwritten;
370
 
 
371
 
                received++;
372
 
 
373
 
                if (cli_is_error(cli))
374
 
                        break;
375
 
 
376
 
                bwritten += SVAL(cli->inbuf, smb_vwv2);
377
 
                bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
378
 
        }
379
 
 
380
 
        while (received < issued && cli_receive_smb(cli))
381
 
                received++;
382
 
        
383
 
        return bwritten;
384
 
}
385
 
 
386
 
/****************************************************************************
387
 
  write to a file using a SMBwrite and not bypassing 0 byte writes
388
 
****************************************************************************/
389
 
 
390
 
ssize_t cli_smbwrite(struct cli_state *cli,
391
 
                     int fnum, char *buf, off_t offset, size_t size1)
392
 
{
393
 
        char *p;
394
 
        ssize_t total = 0;
395
 
 
396
 
        do {
397
 
                size_t size = MIN(size1, cli->max_xmit - 48);
398
 
                
399
 
                memset(cli->outbuf,'\0',smb_size);
400
 
                memset(cli->inbuf,'\0',smb_size);
401
 
 
402
 
                set_message(cli->outbuf,5, 0,True);
403
 
 
404
 
                SCVAL(cli->outbuf,smb_com,SMBwrite);
405
 
                SSVAL(cli->outbuf,smb_tid,cli->cnum);
406
 
                cli_setup_packet(cli);
407
 
                
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);
412
 
                
413
 
                p = smb_buf(cli->outbuf);
414
 
                *p++ = 1;
415
 
                SSVAL(p, 0, size); p += 2;
416
 
                memcpy(p, buf, size); p += size;
417
 
 
418
 
                cli_setup_bcc(cli, p);
419
 
                
420
 
                if (!cli_send_smb(cli))
421
 
                        return -1;
422
 
 
423
 
                if (!cli_receive_smb(cli))
424
 
                        return -1;
425
 
                
426
 
                if (cli_is_error(cli))
427
 
                        return -1;
428
 
 
429
 
                size = SVAL(cli->inbuf,smb_vwv0);
430
 
                if (size == 0)
431
 
                        break;
432
 
 
433
 
                size1 -= size;
434
 
                total += size;
435
 
                offset += size;
436
 
 
437
 
        } while (size1);
438
 
 
439
 
        return total;
440
 
}