r15740: add TODO, that we should check if the server supports
[tprouty/samba.git] / source4 / libcli / raw / rawreadwrite.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file read/write routines
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) James Myers 2003
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24
25 #define SETUP_REQUEST(cmd, wct, buflen) do { \
26         req = smbcli_request_setup(tree, cmd, wct, buflen); \
27         if (!req) return NULL; \
28 } while (0)
29
30 /****************************************************************************
31  low level read operation (async send)
32 ****************************************************************************/
33 struct smbcli_request *smb_raw_read_send(struct smbcli_tree *tree, union smb_read *parms)
34 {
35         BOOL bigoffset = False;
36         struct smbcli_request *req = NULL; 
37
38         switch (parms->generic.level) {
39         case RAW_READ_READBRAW:
40                 if (tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES) {
41                         bigoffset = True;
42                 }
43                 SETUP_REQUEST(SMBreadbraw, bigoffset? 10:8, 0);
44                 SSVAL(req->out.vwv, VWV(0), parms->readbraw.in.file.fnum);
45                 SIVAL(req->out.vwv, VWV(1), parms->readbraw.in.offset);
46                 SSVAL(req->out.vwv, VWV(3), parms->readbraw.in.maxcnt);
47                 SSVAL(req->out.vwv, VWV(4), parms->readbraw.in.mincnt);
48                 SIVAL(req->out.vwv, VWV(5), parms->readbraw.in.timeout);
49                 SSVAL(req->out.vwv, VWV(7), 0); /* reserved */
50                 if (bigoffset) {
51                         SIVAL(req->out.vwv, VWV(8),parms->readbraw.in.offset>>32);
52                 }
53                 break;
54
55         case RAW_READ_LOCKREAD:
56                 SETUP_REQUEST(SMBlockread, 5, 0);
57                 SSVAL(req->out.vwv, VWV(0), parms->lockread.in.file.fnum);
58                 SSVAL(req->out.vwv, VWV(1), parms->lockread.in.count);
59                 SIVAL(req->out.vwv, VWV(2), parms->lockread.in.offset);
60                 SSVAL(req->out.vwv, VWV(4), parms->lockread.in.remaining);
61                 break;
62
63         case RAW_READ_READ:
64                 SETUP_REQUEST(SMBread, 5, 0);
65                 SSVAL(req->out.vwv, VWV(0), parms->read.in.file.fnum);
66                 SSVAL(req->out.vwv, VWV(1), parms->read.in.count);
67                 SIVAL(req->out.vwv, VWV(2), parms->read.in.offset);
68                 SSVAL(req->out.vwv, VWV(4), parms->read.in.remaining);
69                 break;
70
71         case RAW_READ_READX:
72                 if (tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES) {
73                         bigoffset = True;
74                 }
75                 SETUP_REQUEST(SMBreadX, bigoffset ? 12 : 10, 0);
76                 SSVAL(req->out.vwv, VWV(0), SMB_CHAIN_NONE);
77                 SSVAL(req->out.vwv, VWV(1), 0);
78                 SSVAL(req->out.vwv, VWV(2), parms->readx.in.file.fnum);
79                 SIVAL(req->out.vwv, VWV(3), parms->readx.in.offset);
80                 SSVAL(req->out.vwv, VWV(5), parms->readx.in.maxcnt & 0xFFFF);
81                 SSVAL(req->out.vwv, VWV(6), parms->readx.in.mincnt);
82                 SIVAL(req->out.vwv, VWV(7), parms->readx.in.maxcnt >> 16);
83                 SSVAL(req->out.vwv, VWV(9), parms->readx.in.remaining);
84                 /*
85                  * TODO: give an error when the offset is 64 bit
86                  *       and the server doesn't support it
87                  */
88                 if (bigoffset) {
89                         SIVAL(req->out.vwv, VWV(10),parms->readx.in.offset>>32);
90                 }
91                 if (parms->readx.in.read_for_execute) {
92                         uint16_t flags2 = SVAL(req->out.hdr, HDR_FLG2);
93                         flags2 |= FLAGS2_READ_PERMIT_EXECUTE;
94                         SSVAL(req->out.hdr, HDR_FLG2, flags2);
95                 }
96                 break;
97         }
98
99         if (!smbcli_request_send(req)) {
100                 smbcli_request_destroy(req);
101                 return NULL;
102         }
103
104         /* the transport layer needs to know that a readbraw is pending
105            and handle receives a little differently */
106         if (parms->generic.level == RAW_READ_READBRAW) {
107                 tree->session->transport->readbraw_pending = 1;
108         }
109
110         return req;
111 }
112
113 /****************************************************************************
114  low level read operation (async recv)
115 ****************************************************************************/
116 NTSTATUS smb_raw_read_recv(struct smbcli_request *req, union smb_read *parms)
117 {
118         if (!smbcli_request_receive(req) ||
119             smbcli_request_is_error(req)) {
120                 goto failed;
121         }
122
123         switch (parms->generic.level) {
124         case RAW_READ_READBRAW:
125                 parms->readbraw.out.nread = req->in.size - NBT_HDR_SIZE;
126                 if (parms->readbraw.out.nread > 
127                     MAX(parms->readx.in.mincnt, parms->readx.in.maxcnt)) {
128                         req->status = NT_STATUS_BUFFER_TOO_SMALL;
129                         goto failed;
130                 }
131                 memcpy(parms->readbraw.out.data, req->in.buffer + NBT_HDR_SIZE, parms->readbraw.out.nread);
132                 break;
133                 
134         case RAW_READ_LOCKREAD:
135                 SMBCLI_CHECK_WCT(req, 5);
136                 parms->lockread.out.nread = SVAL(req->in.vwv, VWV(0));
137                 if (parms->lockread.out.nread > parms->lockread.in.count ||
138                     !smbcli_raw_pull_data(req, req->in.data+3, 
139                                        parms->lockread.out.nread, parms->lockread.out.data)) {
140                         req->status = NT_STATUS_BUFFER_TOO_SMALL;
141                 }
142                 break;
143
144         case RAW_READ_READ:
145                 /* there are 4 reserved words in the reply */
146                 SMBCLI_CHECK_WCT(req, 5);
147                 parms->read.out.nread = SVAL(req->in.vwv, VWV(0));
148                 if (parms->read.out.nread > parms->read.in.count ||
149                     !smbcli_raw_pull_data(req, req->in.data+3, 
150                                        parms->read.out.nread, parms->read.out.data)) {
151                         req->status = NT_STATUS_BUFFER_TOO_SMALL;
152                 }
153                 break;
154
155         case RAW_READ_READX:
156                 /* there are 5 reserved words in the reply */
157                 SMBCLI_CHECK_WCT(req, 12);
158                 parms->readx.out.remaining       = SVAL(req->in.vwv, VWV(2));
159                 parms->readx.out.compaction_mode = SVAL(req->in.vwv, VWV(3));
160                 parms->readx.out.nread = SVAL(req->in.vwv, VWV(5));
161                 if (parms->readx.out.nread > MAX(parms->readx.in.mincnt, parms->readx.in.maxcnt) ||
162                     !smbcli_raw_pull_data(req, req->in.hdr + SVAL(req->in.vwv, VWV(6)), 
163                                        parms->readx.out.nread, 
164                                        parms->readx.out.data)) {
165                         req->status = NT_STATUS_BUFFER_TOO_SMALL;
166                 }
167                 break;
168         }
169
170 failed:
171         return smbcli_request_destroy(req);
172 }
173
174 /****************************************************************************
175  low level read operation (sync interface)
176 ****************************************************************************/
177 NTSTATUS smb_raw_read(struct smbcli_tree *tree, union smb_read *parms)
178 {
179         struct smbcli_request *req = smb_raw_read_send(tree, parms);
180         return smb_raw_read_recv(req, parms);
181 }
182
183
184 /****************************************************************************
185  raw write interface (async send)
186 ****************************************************************************/
187 struct smbcli_request *smb_raw_write_send(struct smbcli_tree *tree, union smb_write *parms)
188 {
189         BOOL bigoffset = False;
190         struct smbcli_request *req = NULL; 
191
192         switch (parms->generic.level) {
193         case RAW_WRITE_WRITEUNLOCK:
194                 SETUP_REQUEST(SMBwriteunlock, 5, 3 + parms->writeunlock.in.count);
195                 SSVAL(req->out.vwv, VWV(0), parms->writeunlock.in.file.fnum);
196                 SSVAL(req->out.vwv, VWV(1), parms->writeunlock.in.count);
197                 SIVAL(req->out.vwv, VWV(2), parms->writeunlock.in.offset);
198                 SSVAL(req->out.vwv, VWV(4), parms->writeunlock.in.remaining);
199                 SCVAL(req->out.data, 0, SMB_DATA_BLOCK);
200                 SSVAL(req->out.data, 1, parms->writeunlock.in.count);
201                 if (parms->writeunlock.in.count > 0) {
202                         memcpy(req->out.data+3, parms->writeunlock.in.data, 
203                                parms->writeunlock.in.count);
204                 }
205                 break;
206
207         case RAW_WRITE_WRITE:
208                 SETUP_REQUEST(SMBwrite, 5,  3 + parms->write.in.count);
209                 SSVAL(req->out.vwv, VWV(0), parms->write.in.file.fnum);
210                 SSVAL(req->out.vwv, VWV(1), parms->write.in.count);
211                 SIVAL(req->out.vwv, VWV(2), parms->write.in.offset);
212                 SSVAL(req->out.vwv, VWV(4), parms->write.in.remaining);
213                 SCVAL(req->out.data, 0, SMB_DATA_BLOCK);
214                 SSVAL(req->out.data, 1, parms->write.in.count);
215                 if (parms->write.in.count > 0) {
216                         memcpy(req->out.data+3, parms->write.in.data, parms->write.in.count);
217                 }
218                 break;
219
220         case RAW_WRITE_WRITECLOSE:
221                 SETUP_REQUEST(SMBwriteclose, 6, 1 + parms->writeclose.in.count);
222                 SSVAL(req->out.vwv, VWV(0), parms->writeclose.in.file.fnum);
223                 SSVAL(req->out.vwv, VWV(1), parms->writeclose.in.count);
224                 SIVAL(req->out.vwv, VWV(2), parms->writeclose.in.offset);
225                 raw_push_dos_date3(tree->session->transport,
226                                   req->out.vwv, VWV(4), parms->writeclose.in.mtime);
227                 SCVAL(req->out.data, 0, 0);
228                 if (parms->writeclose.in.count > 0) {
229                         memcpy(req->out.data+1, parms->writeclose.in.data, 
230                                parms->writeclose.in.count);
231                 }
232                 break;
233
234         case RAW_WRITE_WRITEX:
235                 if (tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES) {
236                         bigoffset = True;
237                 }
238                 SETUP_REQUEST(SMBwriteX, bigoffset ? 14 : 12, parms->writex.in.count);
239                 SSVAL(req->out.vwv, VWV(0), SMB_CHAIN_NONE);
240                 SSVAL(req->out.vwv, VWV(1), 0);
241                 SSVAL(req->out.vwv, VWV(2), parms->writex.in.file.fnum);
242                 SIVAL(req->out.vwv, VWV(3), parms->writex.in.offset);
243                 SIVAL(req->out.vwv, VWV(5), 0); /* reserved */
244                 SSVAL(req->out.vwv, VWV(7), parms->writex.in.wmode);
245                 SSVAL(req->out.vwv, VWV(8), parms->writex.in.remaining);
246                 SSVAL(req->out.vwv, VWV(9), parms->writex.in.count>>16);
247                 SSVAL(req->out.vwv, VWV(10), parms->writex.in.count);
248                 SSVAL(req->out.vwv, VWV(11), PTR_DIFF(req->out.data, req->out.hdr));
249                 if (bigoffset) {
250                         SIVAL(req->out.vwv,VWV(12),parms->writex.in.offset>>32);
251                 }
252                 if (parms->writex.in.count > 0) {
253                         memcpy(req->out.data, parms->writex.in.data, parms->writex.in.count);
254                 }
255                 break;
256
257         case RAW_WRITE_SPLWRITE:
258                 SETUP_REQUEST(SMBsplwr, 1, parms->splwrite.in.count);
259                 SSVAL(req->out.vwv, VWV(0), parms->splwrite.in.file.fnum);
260                 if (parms->splwrite.in.count > 0) {
261                         memcpy(req->out.data, parms->splwrite.in.data, parms->splwrite.in.count);
262                 }
263                 break;
264         }
265
266         if (!smbcli_request_send(req)) {
267                 smbcli_request_destroy(req);
268                 return NULL;
269         }
270
271         return req;
272 }
273
274
275 /****************************************************************************
276  raw write interface (async recv)
277 ****************************************************************************/
278 NTSTATUS smb_raw_write_recv(struct smbcli_request *req, union smb_write *parms)
279 {
280         if (!smbcli_request_receive(req) ||
281             smbcli_request_is_error(req)) {
282                 goto failed;
283         }
284
285         switch (parms->generic.level) {
286         case RAW_WRITE_WRITEUNLOCK:
287                 SMBCLI_CHECK_WCT(req, 1);               
288                 parms->writeunlock.out.nwritten = SVAL(req->in.vwv, VWV(0));
289                 break;
290         case RAW_WRITE_WRITE:
291                 SMBCLI_CHECK_WCT(req, 1);
292                 parms->write.out.nwritten = SVAL(req->in.vwv, VWV(0));
293                 break;
294         case RAW_WRITE_WRITECLOSE:
295                 SMBCLI_CHECK_WCT(req, 1);
296                 parms->writeclose.out.nwritten = SVAL(req->in.vwv, VWV(0));
297                 break;
298         case RAW_WRITE_WRITEX:
299                 SMBCLI_CHECK_WCT(req, 6);
300                 parms->writex.out.nwritten  = SVAL(req->in.vwv, VWV(2));
301                 parms->writex.out.nwritten += (CVAL(req->in.vwv, VWV(4)) << 16);
302                 parms->writex.out.remaining = SVAL(req->in.vwv, VWV(3));
303                 break;
304         case RAW_WRITE_SPLWRITE:
305                 break;
306         }
307
308 failed:
309         return smbcli_request_destroy(req);
310 }
311
312 /****************************************************************************
313  raw write interface (sync interface)
314 ****************************************************************************/
315 NTSTATUS smb_raw_write(struct smbcli_tree *tree, union smb_write *parms)
316 {
317         struct smbcli_request *req = smb_raw_write_send(tree, parms);
318         return smb_raw_write_recv(req, parms);
319 }