Split out the "finished read processing" code into a function so it can be called
[nivanova/samba-autobuild/.git] / source3 / smbd / smb2_read.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
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 "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
24
25 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
26                                               struct tevent_context *ev,
27                                               struct smbd_smb2_request *smb2req,
28                                               uint32_t in_smbpid,
29                                               uint64_t in_file_id_volatile,
30                                               uint32_t in_length,
31                                               uint64_t in_offset,
32                                               uint32_t in_minimum,
33                                               uint32_t in_remaining);
34 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
35                                     TALLOC_CTX *mem_ctx,
36                                     DATA_BLOB *out_data,
37                                     uint32_t *out_remaining);
38
39 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
40 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req)
41 {
42         const uint8_t *inhdr;
43         const uint8_t *inbody;
44         int i = req->current_idx;
45         size_t expected_body_size = 0x31;
46         size_t body_size;
47         uint32_t in_smbpid;
48         uint32_t in_length;
49         uint64_t in_offset;
50         uint64_t in_file_id_persistent;
51         uint64_t in_file_id_volatile;
52         uint32_t in_minimum_count;
53         uint32_t in_remaining_bytes;
54         struct tevent_req *subreq;
55
56         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
57         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
58                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
59         }
60
61         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
62
63         body_size = SVAL(inbody, 0x00);
64         if (body_size != expected_body_size) {
65                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
66         }
67
68         in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
69
70         in_length               = IVAL(inbody, 0x04);
71         in_offset               = BVAL(inbody, 0x08);
72         in_file_id_persistent   = BVAL(inbody, 0x10);
73         in_file_id_volatile     = BVAL(inbody, 0x18);
74         in_minimum_count        = IVAL(inbody, 0x20);
75         in_remaining_bytes      = IVAL(inbody, 0x28);
76
77         /* check the max read size */
78         if (in_length > lp_smb2_max_read()) {
79                 DEBUG(0,("here:%s: 0x%08X: 0x%08X\n",
80                         __location__, in_length, lp_smb2_max_read()));
81                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
82         }
83
84         if (req->compat_chain_fsp) {
85                 /* skip check */
86         } else if (in_file_id_persistent != in_file_id_volatile) {
87                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
88         }
89
90         subreq = smbd_smb2_read_send(req,
91                                      req->sconn->smb2.event_ctx,
92                                      req,
93                                      in_smbpid,
94                                      in_file_id_volatile,
95                                      in_length,
96                                      in_offset,
97                                      in_minimum_count,
98                                      in_remaining_bytes);
99         if (subreq == NULL) {
100                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
101         }
102         tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
103
104         return smbd_smb2_request_pending_queue(req, subreq);
105 }
106
107 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
108 {
109         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
110                                         struct smbd_smb2_request);
111         int i = req->current_idx;
112         uint8_t *outhdr;
113         DATA_BLOB outbody;
114         DATA_BLOB outdyn;
115         uint8_t out_data_offset;
116         DATA_BLOB out_data_buffer = data_blob_null;
117         uint32_t out_data_remaining = 0;
118         NTSTATUS status;
119         NTSTATUS error; /* transport error */
120
121         status = smbd_smb2_read_recv(subreq,
122                                      req,
123                                      &out_data_buffer,
124                                      &out_data_remaining);
125         TALLOC_FREE(subreq);
126         if (!NT_STATUS_IS_OK(status)) {
127                 error = smbd_smb2_request_error(req, status);
128                 if (!NT_STATUS_IS_OK(error)) {
129                         smbd_server_connection_terminate(req->sconn,
130                                                          nt_errstr(error));
131                         return;
132                 }
133                 return;
134         }
135
136         out_data_offset = SMB2_HDR_BODY + 0x10;
137
138         outhdr = (uint8_t *)req->out.vector[i].iov_base;
139
140         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
141         if (outbody.data == NULL) {
142                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
143                 if (!NT_STATUS_IS_OK(error)) {
144                         smbd_server_connection_terminate(req->sconn,
145                                                          nt_errstr(error));
146                         return;
147                 }
148                 return;
149         }
150
151         SSVAL(outbody.data, 0x00, 0x10 + 1);    /* struct size */
152         SCVAL(outbody.data, 0x02,
153               out_data_offset);                 /* data offset */
154         SCVAL(outbody.data, 0x03, 0);           /* reserved */
155         SIVAL(outbody.data, 0x04,
156               out_data_buffer.length);          /* data length */
157         SIVAL(outbody.data, 0x08,
158               out_data_remaining);              /* data remaining */
159         SIVAL(outbody.data, 0x0C, 0);           /* reserved */
160
161         outdyn = out_data_buffer;
162
163         error = smbd_smb2_request_done(req, outbody, &outdyn);
164         if (!NT_STATUS_IS_OK(error)) {
165                 smbd_server_connection_terminate(req->sconn,
166                                                  nt_errstr(error));
167                 return;
168         }
169 }
170
171 struct smbd_smb2_read_state {
172         struct smbd_smb2_request *smb2req;
173         files_struct *fsp;
174         uint32_t in_length;
175         uint64_t in_offset;
176         uint32_t in_minimum;
177         DATA_BLOB out_data;
178         uint32_t out_remaining;
179 };
180
181 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
182
183 /*******************************************************************
184  Common read complete processing function for both synchronous and
185  asynchronous reads.
186 *******************************************************************/
187
188 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
189 {
190         struct smbd_smb2_read_state *state = tevent_req_data(req,
191                                         struct smbd_smb2_read_state);
192         files_struct *fsp = state->fsp;
193
194         if (nread < 0) {
195                 NTSTATUS status = map_nt_error_from_unix(err);
196
197                 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
198                         "Error = %s (NTSTATUS %s)\n",
199                         fsp_str_dbg(fsp),
200                         (int)nread,
201                         strerror(err),
202                         nt_errstr(status)));
203
204                 return status;
205         }
206         if (nread == 0 && state->in_length != 0) {
207                 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
208                         fsp_str_dbg(fsp)));
209                 return NT_STATUS_END_OF_FILE;
210         }
211
212         if (nread < state->in_minimum) {
213                 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
214                         "minimum requested %u. Returning end of file\n",
215                         fsp_str_dbg(fsp),
216                         (int)nread,
217                         (unsigned int)state->in_minimum));
218                 return NT_STATUS_END_OF_FILE;
219         }
220
221         DEBUG(3,("smbd_smb2_read: fnum=[%d/%s] length=%lu offset=%lu read=%lu\n",
222                 fsp->fnum,
223                 fsp_str_dbg(fsp),
224                 (unsigned long)state->in_length,
225                 (unsigned long)state->in_offset,
226                 (unsigned long)nread));
227
228         state->out_data.length = nread;
229         state->out_remaining = 0;
230
231         return NT_STATUS_OK;
232 }
233
234 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
235                                               struct tevent_context *ev,
236                                               struct smbd_smb2_request *smb2req,
237                                               uint32_t in_smbpid,
238                                               uint64_t in_file_id_volatile,
239                                               uint32_t in_length,
240                                               uint64_t in_offset,
241                                               uint32_t in_minimum,
242                                               uint32_t in_remaining)
243 {
244         NTSTATUS status;
245         struct tevent_req *req = NULL;
246         struct smbd_smb2_read_state *state = NULL;
247         struct smb_request *smbreq = NULL;
248         connection_struct *conn = smb2req->tcon->compat_conn;
249         files_struct *fsp = NULL;
250         ssize_t nread = -1;
251         struct lock_struct lock;
252         int saved_errno;
253
254         req = tevent_req_create(mem_ctx, &state,
255                                 struct smbd_smb2_read_state);
256         if (req == NULL) {
257                 return NULL;
258         }
259         state->smb2req = smb2req;
260         state->in_length = in_length;
261         state->in_offset = in_offset;
262         state->in_minimum = in_minimum;
263         state->out_data = data_blob_null;
264         state->out_remaining = 0;
265
266         DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
267                   (unsigned long long)in_file_id_volatile));
268
269         smbreq = smbd_smb2_fake_smb_request(smb2req);
270         if (tevent_req_nomem(smbreq, req)) {
271                 return tevent_req_post(req, ev);
272         }
273
274         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
275         if (fsp == NULL) {
276                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
277                 return tevent_req_post(req, ev);
278         }
279         if (conn != fsp->conn) {
280                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
281                 return tevent_req_post(req, ev);
282         }
283         if (smb2req->session->vuid != fsp->vuid) {
284                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
285                 return tevent_req_post(req, ev);
286         }
287         if (fsp->is_directory) {
288                 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
289                 return tevent_req_post(req, ev);
290         }
291
292         state->out_data = data_blob_talloc(state, NULL, in_length);
293         if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
294                 return tevent_req_post(req, ev);
295         }
296
297         state->fsp = fsp;
298
299         if (IS_IPC(smbreq->conn)) {
300                 struct tevent_req *subreq = NULL;
301
302                 if (!fsp_is_np(fsp)) {
303                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
304                         return tevent_req_post(req, ev);
305                 }
306
307                 subreq = np_read_send(state, smbd_event_context(),
308                                       fsp->fake_file_handle,
309                                       state->out_data.data,
310                                       state->out_data.length);
311                 if (tevent_req_nomem(subreq, req)) {
312                         return tevent_req_post(req, ev);
313                 }
314                 tevent_req_set_callback(subreq,
315                                         smbd_smb2_read_pipe_done,
316                                         req);
317                 return req;
318         }
319
320         if (!CHECK_READ(fsp, smbreq)) {
321                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
322                 return tevent_req_post(req, ev);
323         }
324
325         init_strict_lock_struct(fsp,
326                                 in_file_id_volatile,
327                                 in_offset,
328                                 in_length,
329                                 READ_LOCK,
330                                 &lock);
331
332         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
333                 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
334                 return tevent_req_post(req, ev);
335         }
336
337         nread = read_file(fsp,
338                           (char *)state->out_data.data,
339                           in_offset,
340                           in_length);
341
342         saved_errno = errno;
343
344         SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
345
346         DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu "
347                 "len=%llu returned %lld\n",
348                 fsp_str_dbg(fsp),
349                 (unsigned long long)in_file_id_volatile,
350                 (unsigned long long)in_offset,
351                 (unsigned long long)in_length,
352                 (long long)nread));
353
354         status = smb2_read_complete(req, nread, saved_errno);
355         if (!NT_STATUS_IS_OK(status)) {
356                 tevent_req_nterror(req, status);
357         } else {
358                 /* Success. */
359                 tevent_req_done(req);
360         }
361         return tevent_req_post(req, ev);
362 }
363
364 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
365 {
366         struct tevent_req *req = tevent_req_callback_data(subreq,
367                                  struct tevent_req);
368         struct smbd_smb2_read_state *state = tevent_req_data(req,
369                                              struct smbd_smb2_read_state);
370         NTSTATUS status;
371         ssize_t nread = -1;
372         bool is_data_outstanding;
373
374         status = np_read_recv(subreq, &nread, &is_data_outstanding);
375         TALLOC_FREE(subreq);
376         if (!NT_STATUS_IS_OK(status)) {
377                 tevent_req_nterror(req, status);
378                 return;
379         }
380
381         if (nread == 0 && state->out_data.length != 0) {
382                 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
383                 return;
384         }
385
386         state->out_data.length = nread;
387         state->out_remaining = 0;
388
389         tevent_req_done(req);
390 }
391
392 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
393                                     TALLOC_CTX *mem_ctx,
394                                     DATA_BLOB *out_data,
395                                     uint32_t *out_remaining)
396 {
397         NTSTATUS status;
398         struct smbd_smb2_read_state *state = tevent_req_data(req,
399                                              struct smbd_smb2_read_state);
400
401         if (tevent_req_is_nterror(req, &status)) {
402                 tevent_req_received(req);
403                 return status;
404         }
405
406         *out_data = state->out_data;
407         talloc_steal(mem_ctx, out_data->data);
408         *out_remaining = state->out_remaining;
409
410         tevent_req_received(req);
411         return NT_STATUS_OK;
412 }