Line data Source code
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 3 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, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "includes.h"
22 : #include "libcli/raw/libcliraw.h"
23 : #include "libcli/raw/raw_proto.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 51654 : _PUBLIC_ struct smbcli_request *smb_raw_read_send(struct smbcli_tree *tree, union smb_read *parms)
34 : {
35 51654 : bool bigoffset = false;
36 51654 : struct smbcli_request *req = NULL;
37 :
38 51654 : switch (parms->generic.level) {
39 12 : case RAW_READ_READBRAW:
40 12 : if (tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES) {
41 12 : bigoffset = true;
42 : }
43 12 : SETUP_REQUEST(SMBreadbraw, bigoffset? 10:8, 0);
44 12 : SSVAL(req->out.vwv, VWV(0), parms->readbraw.in.file.fnum);
45 12 : SIVAL(req->out.vwv, VWV(1), parms->readbraw.in.offset);
46 12 : SSVAL(req->out.vwv, VWV(3), parms->readbraw.in.maxcnt);
47 12 : SSVAL(req->out.vwv, VWV(4), parms->readbraw.in.mincnt);
48 12 : SIVAL(req->out.vwv, VWV(5), parms->readbraw.in.timeout);
49 12 : SSVAL(req->out.vwv, VWV(7), 0); /* reserved */
50 12 : if (bigoffset) {
51 12 : SIVAL(req->out.vwv, VWV(8),parms->readbraw.in.offset>>32);
52 : }
53 12 : break;
54 :
55 13 : case RAW_READ_LOCKREAD:
56 13 : SETUP_REQUEST(SMBlockread, 5, 0);
57 13 : SSVAL(req->out.vwv, VWV(0), parms->lockread.in.file.fnum);
58 13 : SSVAL(req->out.vwv, VWV(1), parms->lockread.in.count);
59 13 : SIVAL(req->out.vwv, VWV(2), parms->lockread.in.offset);
60 13 : SSVAL(req->out.vwv, VWV(4), parms->lockread.in.remaining);
61 13 : break;
62 :
63 8 : case RAW_READ_READ:
64 8 : SETUP_REQUEST(SMBread, 5, 0);
65 8 : SSVAL(req->out.vwv, VWV(0), parms->read.in.file.fnum);
66 8 : SSVAL(req->out.vwv, VWV(1), parms->read.in.count);
67 8 : SIVAL(req->out.vwv, VWV(2), parms->read.in.offset);
68 8 : SSVAL(req->out.vwv, VWV(4), parms->read.in.remaining);
69 8 : break;
70 :
71 51621 : case RAW_READ_READX:
72 51621 : if (tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES) {
73 51621 : bigoffset = true;
74 : }
75 51621 : SETUP_REQUEST(SMBreadX, bigoffset ? 12 : 10, 0);
76 51621 : SSVAL(req->out.vwv, VWV(0), SMB_CHAIN_NONE);
77 51621 : SSVAL(req->out.vwv, VWV(1), 0);
78 51621 : SSVAL(req->out.vwv, VWV(2), parms->readx.in.file.fnum);
79 51621 : SIVAL(req->out.vwv, VWV(3), parms->readx.in.offset);
80 51621 : SSVAL(req->out.vwv, VWV(5), parms->readx.in.maxcnt & 0xFFFF);
81 51621 : SSVAL(req->out.vwv, VWV(6), parms->readx.in.mincnt);
82 51621 : SIVAL(req->out.vwv, VWV(7), parms->readx.in.maxcnt >> 16);
83 51621 : 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 51621 : if (bigoffset) {
89 51621 : SIVAL(req->out.vwv, VWV(10),parms->readx.in.offset>>32);
90 : }
91 51621 : if (parms->readx.in.read_for_execute) {
92 24993 : uint16_t flags2 = SVAL(req->out.hdr, HDR_FLG2);
93 24993 : flags2 |= FLAGS2_READ_PERMIT_EXECUTE;
94 24993 : SSVAL(req->out.hdr, HDR_FLG2, flags2);
95 : }
96 51621 : break;
97 :
98 0 : case RAW_READ_SMB2:
99 0 : return NULL;
100 : }
101 :
102 51654 : if (!smbcli_request_send(req)) {
103 0 : smbcli_request_destroy(req);
104 0 : return NULL;
105 : }
106 :
107 51654 : return req;
108 : }
109 :
110 : /****************************************************************************
111 : low level read operation (async recv)
112 : ****************************************************************************/
113 51653 : _PUBLIC_ NTSTATUS smb_raw_read_recv(struct smbcli_request *req, union smb_read *parms)
114 : {
115 103306 : if (!smbcli_request_receive(req) ||
116 51653 : smbcli_request_is_error(req)) {
117 0 : goto failed;
118 : }
119 :
120 32744 : switch (parms->generic.level) {
121 12 : case RAW_READ_READBRAW:
122 12 : parms->readbraw.out.nread = req->in.size - NBT_HDR_SIZE;
123 24 : if (parms->readbraw.out.nread >
124 12 : MAX(parms->readx.in.mincnt, parms->readx.in.maxcnt)) {
125 0 : req->status = NT_STATUS_BUFFER_TOO_SMALL;
126 0 : goto failed;
127 : }
128 12 : memcpy(parms->readbraw.out.data, req->in.buffer + NBT_HDR_SIZE, parms->readbraw.out.nread);
129 42 : break;
130 :
131 6 : case RAW_READ_LOCKREAD:
132 6 : SMBCLI_CHECK_WCT(req, 5);
133 6 : parms->lockread.out.nread = SVAL(req->in.vwv, VWV(0));
134 12 : if (parms->lockread.out.nread > parms->lockread.in.count ||
135 12 : !smbcli_raw_pull_data(&req->in.bufinfo, req->in.data+3,
136 6 : parms->lockread.out.nread, parms->lockread.out.data)) {
137 0 : req->status = NT_STATUS_BUFFER_TOO_SMALL;
138 : }
139 6 : break;
140 :
141 6 : case RAW_READ_READ:
142 : /* there are 4 reserved words in the reply */
143 6 : SMBCLI_CHECK_WCT(req, 5);
144 6 : parms->read.out.nread = SVAL(req->in.vwv, VWV(0));
145 12 : if (parms->read.out.nread > parms->read.in.count ||
146 12 : !smbcli_raw_pull_data(&req->in.bufinfo, req->in.data+3,
147 6 : parms->read.out.nread, parms->read.out.data)) {
148 0 : req->status = NT_STATUS_BUFFER_TOO_SMALL;
149 : }
150 6 : break;
151 :
152 32720 : case RAW_READ_READX:
153 : /* there are 5 reserved words in the reply */
154 32720 : SMBCLI_CHECK_WCT(req, 12);
155 32720 : parms->readx.out.remaining = SVAL(req->in.vwv, VWV(2));
156 32720 : parms->readx.out.compaction_mode = SVAL(req->in.vwv, VWV(3));
157 32720 : parms->readx.out.nread = SVAL(req->in.vwv, VWV(5));
158 32720 : parms->readx.out.flags2 = req->flags2;
159 32720 : parms->readx.out.data_offset = SVAL(req->in.vwv, VWV(6));
160 :
161 : /* handle oversize replies for non-chained readx replies with
162 : CAP_LARGE_READX. The snia spec has must to answer for. */
163 32720 : if ((req->tree->session->transport->negotiate.capabilities & CAP_LARGE_READX)
164 65410 : && CVAL(req->in.vwv, VWV(0)) == SMB_CHAIN_NONE &&
165 32720 : req->in.size >= 0x10000) {
166 1 : parms->readx.out.nread += (SVAL(req->in.vwv, VWV(7)) << 16);
167 3 : if (req->in.hdr + SVAL(req->in.vwv, VWV(6)) +
168 1 : parms->readx.out.nread <=
169 1 : req->in.buffer + req->in.size) {
170 1 : req->in.data_size += (SVAL(req->in.vwv, VWV(7)) << 16);
171 :
172 : /* update the bufinfo with the new size */
173 1 : smb_setup_bufinfo(req);
174 : }
175 : }
176 :
177 65410 : if (parms->readx.out.nread > MAX(parms->readx.in.mincnt, parms->readx.in.maxcnt) ||
178 65410 : !smbcli_raw_pull_data(&req->in.bufinfo, req->in.hdr + SVAL(req->in.vwv, VWV(6)),
179 32720 : parms->readx.out.nread,
180 : parms->readx.out.data)) {
181 0 : req->status = NT_STATUS_BUFFER_TOO_SMALL;
182 : }
183 32720 : break;
184 :
185 0 : case RAW_READ_SMB2:
186 0 : req->status = NT_STATUS_INTERNAL_ERROR;
187 0 : break;
188 : }
189 :
190 70562 : failed:
191 51653 : return smbcli_request_destroy(req);
192 : }
193 :
194 : /****************************************************************************
195 : low level read operation (sync interface)
196 : ****************************************************************************/
197 34574 : _PUBLIC_ NTSTATUS smb_raw_read(struct smbcli_tree *tree, union smb_read *parms)
198 : {
199 34574 : struct smbcli_request *req = smb_raw_read_send(tree, parms);
200 34574 : return smb_raw_read_recv(req, parms);
201 : }
202 :
203 :
204 : /****************************************************************************
205 : raw write interface (async send)
206 : ****************************************************************************/
207 26825 : _PUBLIC_ struct smbcli_request *smb_raw_write_send(struct smbcli_tree *tree, union smb_write *parms)
208 : {
209 26825 : bool bigoffset = false;
210 26825 : struct smbcli_request *req = NULL;
211 :
212 26825 : switch (parms->generic.level) {
213 7 : case RAW_WRITE_WRITEUNLOCK:
214 7 : SETUP_REQUEST(SMBwriteunlock, 5, 3 + parms->writeunlock.in.count);
215 7 : SSVAL(req->out.vwv, VWV(0), parms->writeunlock.in.file.fnum);
216 7 : SSVAL(req->out.vwv, VWV(1), parms->writeunlock.in.count);
217 7 : SIVAL(req->out.vwv, VWV(2), parms->writeunlock.in.offset);
218 7 : SSVAL(req->out.vwv, VWV(4), parms->writeunlock.in.remaining);
219 7 : SCVAL(req->out.data, 0, SMB_DATA_BLOCK);
220 7 : SSVAL(req->out.data, 1, parms->writeunlock.in.count);
221 7 : if (parms->writeunlock.in.count > 0) {
222 6 : memcpy(req->out.data+3, parms->writeunlock.in.data,
223 6 : parms->writeunlock.in.count);
224 : }
225 7 : break;
226 :
227 8 : case RAW_WRITE_WRITE:
228 8 : SETUP_REQUEST(SMBwrite, 5, 3 + parms->write.in.count);
229 8 : SSVAL(req->out.vwv, VWV(0), parms->write.in.file.fnum);
230 8 : SSVAL(req->out.vwv, VWV(1), parms->write.in.count);
231 8 : SIVAL(req->out.vwv, VWV(2), parms->write.in.offset);
232 8 : SSVAL(req->out.vwv, VWV(4), parms->write.in.remaining);
233 8 : SCVAL(req->out.data, 0, SMB_DATA_BLOCK);
234 8 : SSVAL(req->out.data, 1, parms->write.in.count);
235 8 : if (parms->write.in.count > 0) {
236 7 : memcpy(req->out.data+3, parms->write.in.data, parms->write.in.count);
237 : }
238 8 : break;
239 :
240 9 : case RAW_WRITE_WRITECLOSE:
241 9 : SETUP_REQUEST(SMBwriteclose, 6, 1 + parms->writeclose.in.count);
242 9 : SSVAL(req->out.vwv, VWV(0), parms->writeclose.in.file.fnum);
243 9 : SSVAL(req->out.vwv, VWV(1), parms->writeclose.in.count);
244 9 : SIVAL(req->out.vwv, VWV(2), parms->writeclose.in.offset);
245 9 : raw_push_dos_date3(tree->session->transport,
246 : req->out.vwv, VWV(4), parms->writeclose.in.mtime);
247 9 : SCVAL(req->out.data, 0, 0);
248 9 : if (parms->writeclose.in.count > 0) {
249 7 : memcpy(req->out.data+1, parms->writeclose.in.data,
250 7 : parms->writeclose.in.count);
251 : }
252 9 : break;
253 :
254 26801 : case RAW_WRITE_WRITEX:
255 26801 : if (tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES) {
256 26801 : bigoffset = true;
257 : }
258 26801 : SETUP_REQUEST(SMBwriteX, bigoffset ? 14 : 12, parms->writex.in.count);
259 26801 : SSVAL(req->out.vwv, VWV(0), SMB_CHAIN_NONE);
260 26801 : SSVAL(req->out.vwv, VWV(1), 0);
261 26801 : SSVAL(req->out.vwv, VWV(2), parms->writex.in.file.fnum);
262 26801 : SIVAL(req->out.vwv, VWV(3), parms->writex.in.offset);
263 26801 : SIVAL(req->out.vwv, VWV(5), 0); /* reserved */
264 26801 : SSVAL(req->out.vwv, VWV(7), parms->writex.in.wmode);
265 26801 : SSVAL(req->out.vwv, VWV(8), parms->writex.in.remaining);
266 26801 : SSVAL(req->out.vwv, VWV(9), parms->writex.in.count>>16);
267 26801 : SSVAL(req->out.vwv, VWV(10), parms->writex.in.count);
268 26801 : SSVAL(req->out.vwv, VWV(11), PTR_DIFF(req->out.data, req->out.hdr));
269 26801 : if (bigoffset) {
270 26801 : SIVAL(req->out.vwv,VWV(12),parms->writex.in.offset>>32);
271 : }
272 26801 : if (parms->writex.in.count > 0) {
273 26800 : memcpy(req->out.data, parms->writex.in.data, parms->writex.in.count);
274 : }
275 26801 : break;
276 :
277 0 : case RAW_WRITE_SPLWRITE:
278 0 : SETUP_REQUEST(SMBsplwr, 1, parms->splwrite.in.count);
279 0 : SSVAL(req->out.vwv, VWV(0), parms->splwrite.in.file.fnum);
280 0 : if (parms->splwrite.in.count > 0) {
281 0 : memcpy(req->out.data, parms->splwrite.in.data, parms->splwrite.in.count);
282 : }
283 0 : break;
284 :
285 0 : case RAW_WRITE_SMB2:
286 0 : return NULL;
287 : }
288 :
289 26825 : if (!smbcli_request_send(req)) {
290 0 : smbcli_request_destroy(req);
291 0 : return NULL;
292 : }
293 :
294 26825 : return req;
295 : }
296 :
297 :
298 : /****************************************************************************
299 : raw write interface (async recv)
300 : ****************************************************************************/
301 26825 : _PUBLIC_ NTSTATUS smb_raw_write_recv(struct smbcli_request *req, union smb_write *parms)
302 : {
303 53623 : if (!smbcli_request_receive(req) ||
304 26825 : !NT_STATUS_IS_OK(req->status)) {
305 0 : goto failed;
306 : }
307 :
308 14110 : switch (parms->generic.level) {
309 4 : case RAW_WRITE_WRITEUNLOCK:
310 4 : SMBCLI_CHECK_WCT(req, 1);
311 4 : parms->writeunlock.out.nwritten = SVAL(req->in.vwv, VWV(0));
312 31 : break;
313 7 : case RAW_WRITE_WRITE:
314 7 : SMBCLI_CHECK_WCT(req, 1);
315 7 : parms->write.out.nwritten = SVAL(req->in.vwv, VWV(0));
316 7 : break;
317 6 : case RAW_WRITE_WRITECLOSE:
318 6 : SMBCLI_CHECK_WCT(req, 1);
319 6 : parms->writeclose.out.nwritten = SVAL(req->in.vwv, VWV(0));
320 6 : break;
321 14093 : case RAW_WRITE_WRITEX:
322 14093 : SMBCLI_CHECK_WCT(req, 6);
323 14093 : parms->writex.out.nwritten = SVAL(req->in.vwv, VWV(2));
324 14093 : parms->writex.out.nwritten += (CVAL(req->in.vwv, VWV(4)) << 16);
325 14093 : parms->writex.out.remaining = SVAL(req->in.vwv, VWV(3));
326 14093 : break;
327 0 : case RAW_WRITE_SPLWRITE:
328 0 : break;
329 0 : case RAW_WRITE_SMB2:
330 0 : req->status = NT_STATUS_INTERNAL_ERROR;
331 0 : break;
332 : }
333 :
334 39540 : failed:
335 26825 : return smbcli_request_destroy(req);
336 : }
337 :
338 : /****************************************************************************
339 : raw write interface (sync interface)
340 : ****************************************************************************/
341 18153 : _PUBLIC_ NTSTATUS smb_raw_write(struct smbcli_tree *tree, union smb_write *parms)
342 : {
343 18153 : struct smbcli_request *req = smb_raw_write_send(tree, parms);
344 18153 : return smb_raw_write_recv(req, parms);
345 : }
|