Store printer guid in the dsspooler registry key so we don't have to
[Samba/gebeck_regimport.git] / source / libsmb / clireadwrite.c
blob38e8aac42a5a559c4f327dc7a61cf5c01d6a0576
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 #define NO_SYSLOG
23 #include "includes.h"
25 /****************************************************************************
26 Issue a single SMBread and don't wait for a reply.
27 ****************************************************************************/
29 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
30 size_t size, int i)
32 memset(cli->outbuf,'\0',smb_size);
33 memset(cli->inbuf,'\0',smb_size);
35 set_message(cli->outbuf,10,0,True);
37 SCVAL(cli->outbuf,smb_com,SMBreadX);
38 SSVAL(cli->outbuf,smb_tid,cli->cnum);
39 cli_setup_packet(cli);
41 SCVAL(cli->outbuf,smb_vwv0,0xFF);
42 SSVAL(cli->outbuf,smb_vwv2,fnum);
43 SIVAL(cli->outbuf,smb_vwv3,offset);
44 SSVAL(cli->outbuf,smb_vwv5,size);
45 SSVAL(cli->outbuf,smb_vwv6,size);
46 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
48 return cli_send_smb(cli);
51 /****************************************************************************
52 Read size bytes at offset offset using SMBreadX.
53 ****************************************************************************/
55 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
57 char *p;
58 int size2;
59 int readsize;
60 ssize_t total = 0;
62 if (size == 0)
63 return 0;
66 * Set readsize to the maximum size we can handle in one readX,
67 * rounded down to a multiple of 1024.
70 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
72 while (total < size) {
73 readsize = MIN(readsize, size-total);
75 /* Issue a read and receive a reply */
77 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
78 return -1;
80 if (!cli_receive_smb(cli))
81 return -1;
83 /* Check for error. Make sure to check for DOS and NT
84 errors. */
86 if (cli_is_error(cli)) {
87 BOOL recoverable_error = False;
88 NTSTATUS status = NT_STATUS_OK;
89 uint8 eclass = 0;
90 uint32 ecode = 0;
92 if (cli_is_nt_error(cli))
93 status = cli_nt_error(cli);
94 else
95 cli_dos_error(cli, &eclass, &ecode);
98 * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
99 * recoverable error, plus we have valid data in the
100 * packet so don't error out here.
103 if ((eclass == ERRDOS && ecode == ERRmoredata) ||
104 NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
105 recoverable_error = True;
107 if (!recoverable_error)
108 return -1;
111 size2 = SVAL(cli->inbuf, smb_vwv5);
113 if (size2 > readsize) {
114 DEBUG(5,("server returned more than we wanted!\n"));
115 return -1;
116 } else if (size2 < 0) {
117 DEBUG(5,("read return < 0!\n"));
118 return -1;
121 /* Copy data into buffer */
123 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
124 memcpy(buf + total, p, size2);
126 total += size2;
127 offset += size2;
130 * If the server returned less than we asked for we're at EOF.
133 if (size2 < readsize)
134 break;
137 return total;
140 #if 0 /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
142 /* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
143 you must fix ensure you don't attempt to sign the packets - data
144 *will* be currupted */
146 /****************************************************************************
147 Issue a single SMBreadraw and don't wait for a reply.
148 ****************************************************************************/
150 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
151 size_t size, int i)
154 if (!cli->sign_info.use_smb_signing) {
155 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
156 return False;
159 memset(cli->outbuf,'\0',smb_size);
160 memset(cli->inbuf,'\0',smb_size);
162 set_message(cli->outbuf,10,0,True);
164 SCVAL(cli->outbuf,smb_com,SMBreadbraw);
165 SSVAL(cli->outbuf,smb_tid,cli->cnum);
166 cli_setup_packet(cli);
168 SSVAL(cli->outbuf,smb_vwv0,fnum);
169 SIVAL(cli->outbuf,smb_vwv1,offset);
170 SSVAL(cli->outbuf,smb_vwv2,size);
171 SSVAL(cli->outbuf,smb_vwv3,size);
172 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
174 return cli_send_smb(cli);
177 /****************************************************************************
178 Tester for the readraw call.
179 ****************************************************************************/
181 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
183 char *p;
184 int size2;
185 size_t readsize;
186 ssize_t total = 0;
188 if (size == 0)
189 return 0;
192 * Set readsize to the maximum size we can handle in one readraw.
195 readsize = 0xFFFF;
197 while (total < size) {
198 readsize = MIN(readsize, size-total);
200 /* Issue a read and receive a reply */
202 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
203 return -1;
205 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
206 return -1;
208 size2 = smb_len(cli->inbuf);
210 if (size2 > readsize) {
211 DEBUG(5,("server returned more than we wanted!\n"));
212 return -1;
213 } else if (size2 < 0) {
214 DEBUG(5,("read return < 0!\n"));
215 return -1;
218 /* Copy data into buffer */
220 if (size2) {
221 p = cli->inbuf + 4;
222 memcpy(buf + total, p, size2);
225 total += size2;
226 offset += size2;
229 * If the server returned less than we asked for we're at EOF.
232 if (size2 < readsize)
233 break;
236 return total;
238 #endif
239 /****************************************************************************
240 issue a single SMBwrite and don't wait for a reply
241 ****************************************************************************/
243 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
244 size_t size, int i)
246 char *p;
248 if (size > cli->bufsize) {
249 cli->outbuf = realloc(cli->outbuf, size + 1024);
250 cli->inbuf = realloc(cli->inbuf, size + 1024);
251 if (cli->outbuf == NULL || cli->inbuf == NULL)
252 return False;
253 cli->bufsize = size + 1024;
256 memset(cli->outbuf,'\0',smb_size);
257 memset(cli->inbuf,'\0',smb_size);
259 if (size > 0xFFFF)
260 set_message(cli->outbuf,14,0,True);
261 else
262 set_message(cli->outbuf,12,0,True);
264 SCVAL(cli->outbuf,smb_com,SMBwriteX);
265 SSVAL(cli->outbuf,smb_tid,cli->cnum);
266 cli_setup_packet(cli);
268 SCVAL(cli->outbuf,smb_vwv0,0xFF);
269 SSVAL(cli->outbuf,smb_vwv2,fnum);
271 SIVAL(cli->outbuf,smb_vwv3,offset);
272 SIVAL(cli->outbuf,smb_vwv5,(mode & 0x0008) ? 0xFFFFFFFF : 0);
273 SSVAL(cli->outbuf,smb_vwv7,mode);
275 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
276 SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
277 SSVAL(cli->outbuf,smb_vwv10,size);
278 SSVAL(cli->outbuf,smb_vwv11,
279 smb_buf(cli->outbuf) - smb_base(cli->outbuf));
281 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
282 memcpy(p, buf, size);
283 cli_setup_bcc(cli, p+size);
285 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
287 show_msg(cli->outbuf);
288 return cli_send_smb(cli);
291 /****************************************************************************
292 write to a file
293 write_mode: 0x0001 disallow write cacheing
294 0x0002 return bytes remaining
295 0x0004 use raw named pipe protocol
296 0x0008 start of message mode named pipe protocol
297 ****************************************************************************/
299 ssize_t cli_write(struct cli_state *cli,
300 int fnum, uint16 write_mode,
301 char *buf, off_t offset, size_t size)
303 int bwritten = 0;
304 int issued = 0;
305 int received = 0;
306 int mpx = MAX(cli->max_mux-1, 1);
307 int block = (cli->max_xmit - (smb_size+32)) & ~1023;
308 int blocks = (size + (block-1)) / block;
310 while (received < blocks) {
312 while ((issued - received < mpx) && (issued < blocks)) {
313 int bsent = issued * block;
314 int size1 = MIN(block, size - bsent);
316 if (!cli_issue_write(cli, fnum, offset + bsent,
317 write_mode,
318 buf + bsent,
319 size1, issued))
320 return -1;
321 issued++;
324 if (!cli_receive_smb(cli))
325 return bwritten;
327 received++;
329 if (cli_is_error(cli))
330 break;
332 bwritten += SVAL(cli->inbuf, smb_vwv2);
333 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))>>16);
336 while (received < issued && cli_receive_smb(cli))
337 received++;
339 return bwritten;
342 /****************************************************************************
343 write to a file using a SMBwrite and not bypassing 0 byte writes
344 ****************************************************************************/
346 ssize_t cli_smbwrite(struct cli_state *cli,
347 int fnum, char *buf, off_t offset, size_t size1)
349 char *p;
350 ssize_t total = 0;
352 do {
353 size_t size = MIN(size1, cli->max_xmit - 48);
355 memset(cli->outbuf,'\0',smb_size);
356 memset(cli->inbuf,'\0',smb_size);
358 set_message(cli->outbuf,5, 0,True);
360 SCVAL(cli->outbuf,smb_com,SMBwrite);
361 SSVAL(cli->outbuf,smb_tid,cli->cnum);
362 cli_setup_packet(cli);
364 SSVAL(cli->outbuf,smb_vwv0,fnum);
365 SSVAL(cli->outbuf,smb_vwv1,size);
366 SIVAL(cli->outbuf,smb_vwv2,offset);
367 SSVAL(cli->outbuf,smb_vwv4,0);
369 p = smb_buf(cli->outbuf);
370 *p++ = 1;
371 SSVAL(p, 0, size); p += 2;
372 memcpy(p, buf, size); p += size;
374 cli_setup_bcc(cli, p);
376 if (!cli_send_smb(cli))
377 return -1;
379 if (!cli_receive_smb(cli))
380 return -1;
382 if (cli_is_error(cli))
383 return -1;
385 size = SVAL(cli->inbuf,smb_vwv0);
386 if (size == 0)
387 break;
389 size1 -= size;
390 total += size;
391 offset += size;
393 } while (size1);
395 return total;