hack skip smbd_smb2_request_verify* on read
[metze/samba/wip.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 "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "libcli/security/security.h"
27 #include "../lib/util/tevent_ntstatus.h"
28 #include "rpc_server/srv_pipe_hnd.h"
29
30 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
31                                               struct tevent_context *ev,
32                                               struct smbd_smb2_request *smb2req,
33                                               struct files_struct *in_fsp,
34                                               uint32_t in_length,
35                                               uint64_t in_offset,
36                                               uint32_t in_minimum,
37                                               uint32_t in_remaining);
38 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
39                                     TALLOC_CTX *mem_ctx,
40                                     DATA_BLOB *out_data,
41                                     uint32_t *out_remaining);
42
43 static void smbd_smb2_request_read_done(struct tevent_req *subreq);
44 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req)
45 {
46         NTSTATUS status;
47         const uint8_t *inbody;
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         struct files_struct *in_fsp;
53         uint32_t in_minimum_count;
54         uint32_t in_remaining_bytes;
55         struct tevent_req *subreq;
56
57         status = NT_STATUS_OK;//smbd_smb2_request_verify_sizes(req, 0x31);
58         if (!NT_STATUS_IS_OK(status)) {
59                 return smbd_smb2_request_error(req, status);
60         }
61         inbody = SMBD_SMB2_IN_BODY_PTR(req);
62
63         in_length               = IVAL(inbody, 0x04);
64         in_offset               = BVAL(inbody, 0x08);
65         in_file_id_persistent   = BVAL(inbody, 0x10);
66         in_file_id_volatile     = BVAL(inbody, 0x18);
67         in_minimum_count        = IVAL(inbody, 0x20);
68         in_remaining_bytes      = IVAL(inbody, 0x28);
69
70         /* check the max read size */
71         if (in_length > req->sconn->smb2.max_read) {
72                 DEBUG(2,("smbd_smb2_request_process_read: "
73                          "client ignored max read: %s: 0x%08X: 0x%08X\n",
74                         __location__, in_length, req->sconn->smb2.max_read));
75                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
76         }
77
78         status = NT_STATUS_OK;//smbd_smb2_request_verify_creditcharge(req, in_length);
79         if (!NT_STATUS_IS_OK(status)) {
80                 return smbd_smb2_request_error(req, status);
81         }
82
83         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
84         if (in_fsp == NULL) {
85                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
86         }
87
88         subreq = smbd_smb2_read_send(req, req->sconn->ev_ctx,
89                                      req, in_fsp,
90                                      in_length,
91                                      in_offset,
92                                      in_minimum_count,
93                                      in_remaining_bytes);
94         if (subreq == NULL) {
95                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
96         }
97         //tevent_req_set_callback(subreq, smbd_smb2_request_read_done, req);
98
99         smbd_smb2_request_read_done(req);
100         return NT_STATUS_OK;
101         return smbd_smb2_request_pending_queue(req, subreq, 500);
102 }
103
104 static void smbd_smb2_request_read_done(struct tevent_req *subreq)
105 {
106         struct smbd_smb2_request *req = subreq;//tevent_req_callback_data(subreq,
107                                         //struct smbd_smb2_request);
108                                         subreq = NULL;
109         DATA_BLOB outbody;
110         DATA_BLOB outdyn;
111         uint8_t out_data_offset;
112         DATA_BLOB out_data_buffer = data_blob_null;
113         uint32_t out_data_remaining = 0;
114         NTSTATUS status;
115         NTSTATUS error; /* transport error */
116
117         status = smbd_smb2_read_recv(subreq,
118                                      req,
119                                      &out_data_buffer,
120                                      &out_data_remaining);
121         TALLOC_FREE(subreq);
122         if (!NT_STATUS_IS_OK(status)) {
123                 error = smbd_smb2_request_error(req, status);
124                 if (!NT_STATUS_IS_OK(error)) {
125                         smbd_server_connection_terminate(req->sconn,
126                                                          nt_errstr(error));
127                         return;
128                 }
129                 return;
130         }
131
132         out_data_offset = SMB2_HDR_BODY + 0x10;
133
134         outbody = smbd_smb2_generate_outbody(req, 0x10);
135         if (outbody.data == NULL) {
136                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
137                 if (!NT_STATUS_IS_OK(error)) {
138                         smbd_server_connection_terminate(req->sconn,
139                                                          nt_errstr(error));
140                         return;
141                 }
142                 return;
143         }
144
145         SSVAL(outbody.data, 0x00, 0x10 + 1);    /* struct size */
146         SCVAL(outbody.data, 0x02,
147               out_data_offset);                 /* data offset */
148         SCVAL(outbody.data, 0x03, 0);           /* reserved */
149         SIVAL(outbody.data, 0x04,
150               out_data_buffer.length);          /* data length */
151         SIVAL(outbody.data, 0x08,
152               out_data_remaining);              /* data remaining */
153         SIVAL(outbody.data, 0x0C, 0);           /* reserved */
154
155         outdyn = out_data_buffer;
156
157         error = smbd_smb2_request_done(req, outbody, &outdyn);
158         if (!NT_STATUS_IS_OK(error)) {
159                 smbd_server_connection_terminate(req->sconn,
160                                                  nt_errstr(error));
161                 return;
162         }
163 }
164
165 struct smbd_smb2_read_state {
166         struct smbd_smb2_request *smb2req;
167         struct smb_request *smbreq;
168         files_struct *fsp;
169         uint32_t in_length;
170         uint64_t in_offset;
171         uint32_t in_minimum;
172         DATA_BLOB out_headers;
173         uint8_t _out_hdr_buf[NBT_HDR_SIZE + SMB2_HDR_BODY + 0x10];
174         DATA_BLOB out_data;
175         uint32_t out_remaining;
176 };
177
178 static struct smbd_smb2_read_state _state;
179
180 static int smb2_smb2_read_state_deny_destructor(struct smbd_smb2_read_state *state)
181 {
182         return -1;
183 }
184
185 /* struct smbd_smb2_read_state destructor. Send the SMB2_READ data. */
186 static int smb2_sendfile_send_data(struct smbd_smb2_read_state *state)
187 {
188         struct lock_struct lock;
189         uint32_t in_length = state->in_length;
190         uint64_t in_offset = state->in_offset;
191         files_struct *fsp = state->fsp;
192         ssize_t nread;
193
194         nread = SMB_VFS_SENDFILE(fsp->conn->sconn->sock,
195                                  fsp,
196                                  state->smb2req->queue_entry.sendfile_header,
197                                  in_offset,
198                                  in_length);
199         DEBUG(10,("smb2_sendfile_send_data: SMB_VFS_SENDFILE returned %d on file %s\n",
200                 (int)nread,
201                 fsp_str_dbg(fsp) ));
202
203         if (nread == -1) {
204                 if (errno == ENOSYS || errno == EINTR) {
205                         /*
206                          * Special hack for broken systems with no working
207                          * sendfile. Fake this up by doing read/write calls.
208                         */
209                         set_use_sendfile(SNUM(fsp->conn), false);
210                         nread = fake_sendfile(fsp, in_offset, in_length);
211                         if (nread == -1) {
212                                 DEBUG(0,("smb2_sendfile_send_data: "
213                                         "fake_sendfile failed for "
214                                         "file %s (%s).\n",
215                                         fsp_str_dbg(fsp),
216                                         strerror(errno)));
217                                 exit_server_cleanly("smb2_sendfile_send_data: "
218                                         "fake_sendfile failed");
219                         }
220                         goto out;
221                 }
222
223                 DEBUG(0,("smb2_sendfile_send_data: sendfile failed for file "
224                         "%s (%s). Terminating\n",
225                         fsp_str_dbg(fsp),
226                         strerror(errno)));
227                 exit_server_cleanly("smb2_sendfile_send_data: sendfile failed");
228         } else if (nread == 0) {
229                 /*
230                  * Some sendfile implementations return 0 to indicate
231                  * that there was a short read, but nothing was
232                  * actually written to the socket.  In this case,
233                  * fallback to the normal read path so the header gets
234                  * the correct byte count.
235                  */
236                 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
237                         "falling back to the normal read: %s\n",
238                         fsp_str_dbg(fsp)));
239
240                 nread = fake_sendfile(fsp, in_offset, in_length);
241                 if (nread == -1) {
242                         DEBUG(0,("smb2_sendfile_send_data: "
243                                 "fake_sendfile failed for file "
244                                 "%s (%s). Terminating\n",
245                                 fsp_str_dbg(fsp),
246                                 strerror(errno)));
247                         exit_server_cleanly("smb2_sendfile_send_data: "
248                                 "fake_sendfile failed");
249                 }
250         }
251
252   out:
253
254         if (nread < in_length) {
255                 sendfile_short_send(fsp, nread, 0, in_length);
256         }
257
258         init_strict_lock_struct(fsp,
259                                 fsp->op->global->open_persistent_id,
260                                 in_offset,
261                                 in_length,
262                                 READ_LOCK,
263                                 &lock);
264
265         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lock);
266         return 0;
267 }
268
269 static void smb2_sendfile_fn(struct smbd_smb2_read_state *state)
270 {
271         smb2_sendfile_send_data(state);
272         ZERO_STRUCTP(state);
273 }
274
275 static NTSTATUS schedule_smb2_sendfile_read(struct smbd_smb2_request *smb2req,
276                                         struct smbd_smb2_read_state *state)
277 {
278         files_struct *fsp = state->fsp;
279
280         /*
281          * We cannot use sendfile if...
282          * We were not configured to do so OR
283          * Signing is active OR
284          * This is a compound SMB2 operation OR
285          * fsp is a STREAM file OR
286          * We're using a write cache OR
287          * It's not a regular file OR
288          * Requested offset is greater than file size OR
289          * there's not enough data in the file.
290          * Phew :-). Luckily this means most
291          * reads on most normal files. JRA.
292         */
293
294         if (!lp__use_sendfile(SNUM(fsp->conn)) ||
295             smb2req->do_signing ||
296             smb2req->do_encryption ||
297             smb2req->in.vector_count >= (2*SMBD_SMB2_NUM_IOV_PER_REQ) ||
298             (fsp->base_fsp != NULL) ||
299             (fsp->wcp != NULL) ||
300             (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) ||
301             (state->in_offset >= fsp->fsp_name->st.st_ex_size) ||
302             (fsp->fsp_name->st.st_ex_size < state->in_offset + state->in_length))
303         {
304                 return NT_STATUS_RETRY;
305         }
306
307         /* We've already checked there's this amount of data
308            to read. */
309         state->out_data.length = state->in_length;
310         state->out_remaining = 0;
311
312         state->out_headers = data_blob_const(state->_out_hdr_buf,
313                                              sizeof(state->_out_hdr_buf));
314         return NT_STATUS_OK;
315 }
316
317 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq);
318
319 /*******************************************************************
320  Common read complete processing function for both synchronous and
321  asynchronous reads.
322 *******************************************************************/
323
324 NTSTATUS smb2_read_complete(struct tevent_req *req, ssize_t nread, int err)
325 {
326         struct smbd_smb2_read_state *state = &_state;//tevent_req_data(req,
327                                         //struct smbd_smb2_read_state);
328         files_struct *fsp = state->fsp;
329
330         if (nread < 0) {
331                 NTSTATUS status = map_nt_error_from_unix(err);
332
333                 DEBUG( 3,( "smb2_read_complete: file %s nread = %d. "
334                         "Error = %s (NTSTATUS %s)\n",
335                         fsp_str_dbg(fsp),
336                         (int)nread,
337                         strerror(err),
338                         nt_errstr(status)));
339
340                 return status;
341         }
342         if (nread == 0 && state->in_length != 0) {
343                 DEBUG(5,("smb2_read_complete: read_file[%s] end of file\n",
344                         fsp_str_dbg(fsp)));
345                 return NT_STATUS_END_OF_FILE;
346         }
347
348         if (nread < state->in_minimum) {
349                 DEBUG(5,("smb2_read_complete: read_file[%s] read less %d than "
350                         "minimum requested %u. Returning end of file\n",
351                         fsp_str_dbg(fsp),
352                         (int)nread,
353                         (unsigned int)state->in_minimum));
354                 return NT_STATUS_END_OF_FILE;
355         }
356
357         DEBUG(3,("smbd_smb2_read: %s, file %s, length=%lu offset=%lu read=%lu\n",
358                 fsp_fnum_dbg(fsp),
359                 fsp_str_dbg(fsp),
360                 (unsigned long)state->in_length,
361                 (unsigned long)state->in_offset,
362                 (unsigned long)nread));
363
364         state->out_data.length = nread;
365         state->out_remaining = 0;
366
367         return NT_STATUS_OK;
368 }
369
370 static bool smbd_smb2_read_cancel(struct tevent_req *req)
371 {
372         struct smbd_smb2_read_state *state = &_state;
373                 //tevent_req_data(req,
374                 //struct smbd_smb2_read_state);
375
376         return cancel_smb2_aio(state->smbreq);
377 }
378
379 static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
380                                               struct tevent_context *ev,
381                                               struct smbd_smb2_request *smb2req,
382                                               struct files_struct *fsp,
383                                               uint32_t in_length,
384                                               uint64_t in_offset,
385                                               uint32_t in_minimum,
386                                               uint32_t in_remaining)
387 {
388         NTSTATUS status;
389         //struct tevent_req *req = NULL;
390         struct smbd_smb2_read_state *state = NULL;
391         struct smb_request *smbreq = NULL;
392         connection_struct *conn = smb2req->tcon->compat;
393         ssize_t nread = -1;
394         struct lock_struct lock;
395         int saved_errno;
396
397         //req = tevent_req_create(mem_ctx, &state,
398         //                      struct smbd_smb2_read_state);
399         //if (req == NULL) {
400         //      return NULL;
401         //}
402         state = &_state;
403         ZERO_STRUCTP(state);
404         state->smb2req = smb2req;
405         state->in_length = in_length;
406         state->in_offset = in_offset;
407         state->in_minimum = in_minimum;
408         state->out_data = data_blob_null;
409         state->out_remaining = 0;
410
411         DEBUG(10,("smbd_smb2_read: %s - %s\n",
412                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
413
414         smbreq = smbd_smb2_fake_smb_request(smb2req);
415         //if (tevent_req_nomem(smbreq, req)) {
416         //      return tevent_req_post(req, ev);
417         //}
418         state->smbreq = smbreq;
419
420         //if (fsp->is_directory) {
421         //      tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
422         //      return tevent_req_post(req, ev);
423         //}
424
425         state->fsp = fsp;
426
427         //if (IS_IPC(smbreq->conn)) {
428         //      struct tevent_req *subreq = NULL;
429
430         //      state->out_data = data_blob_talloc(state, NULL, in_length);
431         //      if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
432         //              return tevent_req_post(req, ev);
433         //      }
434
435         //      if (!fsp_is_np(fsp)) {
436         //              tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
437         //              return tevent_req_post(req, ev);
438         //      }
439
440         //      subreq = np_read_send(state, ev,
441         //                            fsp->fake_file_handle,
442         //                            state->out_data.data,
443         //                            state->out_data.length);
444         //      if (tevent_req_nomem(subreq, req)) {
445         //              return tevent_req_post(req, ev);
446         //      }
447         //      tevent_req_set_callback(subreq,
448         //                              smbd_smb2_read_pipe_done,
449         //                              req);
450         //      return req;
451         //}
452
453         //if (!CHECK_READ(fsp, smbreq)) {
454         //      tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
455         //      return tevent_req_post(req, ev);
456         //}
457
458         status = schedule_smb2_aio_read(fsp->conn,
459                                 smbreq,
460                                 fsp,
461                                 state,
462                                 &state->out_data,
463                                 (off_t)in_offset,
464                                 (size_t)in_length);
465
466         if (NT_STATUS_IS_OK(status)) {
467                 /*
468                  * Doing an async read, allow this
469                  * request to be canceled
470                  */
471                 //tevent_req_set_cancel_fn(req, smbd_smb2_read_cancel);
472                 return state;
473                 //return req;
474         }
475
476         //if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
477         //      /* Real error in setting up aio. Fail. */
478         //      tevent_req_nterror(req, status);
479         //      return tevent_req_post(req, ev);
480         //}
481
482         /* Fallback to synchronous. */
483
484         init_strict_lock_struct(fsp,
485                                 fsp->op->global->open_persistent_id,
486                                 in_offset,
487                                 in_length,
488                                 READ_LOCK,
489                                 &lock);
490
491         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
492         //      tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
493         //      return tevent_req_post(req, ev);
494         }
495
496         /* Try sendfile in preference. */
497         status = schedule_smb2_sendfile_read(smb2req, state);
498         if (NT_STATUS_IS_OK(status)) {
499                 //tevent_req_done(req);
500                 return state;//req;//tevent_req_post(req, ev);
501         } else {
502 //              if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
503 //                      SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
504 //                      tevent_req_nterror(req, status);
505 //                      return tevent_req_post(req, ev);
506 //              }
507         }
508
509         /* Ok, read into memory. Allocate the out buffer. */
510         state->out_data = data_blob_talloc(state, NULL, in_length);
511 //      if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) {
512 //              SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
513 //              return tevent_req_post(req, ev);
514 //      }
515
516         nread = read_file(fsp,
517                           (char *)state->out_data.data,
518                           in_offset,
519                           in_length);
520
521         saved_errno = errno;
522
523         SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
524
525         DEBUG(10,("smbd_smb2_read: file %s, %s, offset=%llu "
526                 "len=%llu returned %lld\n",
527                 fsp_str_dbg(fsp),
528                 fsp_fnum_dbg(fsp),
529                 (unsigned long long)in_offset,
530                 (unsigned long long)in_length,
531                 (long long)nread));
532
533         status = smb2_read_complete(NULL, nread, saved_errno);
534 //      if (!NT_STATUS_IS_OK(status)) {
535 //              tevent_req_nterror(req, status);
536 //      } else {
537 //              /* Success. */
538 //              tevent_req_done(req);
539 //      }
540         return state;//req;//tevent_req_post(req, ev);
541 }
542
543 static void smbd_smb2_read_pipe_done(struct tevent_req *subreq)
544 {
545         struct tevent_req *req = tevent_req_callback_data(subreq,
546                                  struct tevent_req);
547         struct smbd_smb2_read_state *state = &_state;//tevent_req_data(req,
548                                              //struct smbd_smb2_read_state);
549         NTSTATUS status;
550         ssize_t nread = -1;
551         bool is_data_outstanding;
552
553         status = np_read_recv(subreq, &nread, &is_data_outstanding);
554         TALLOC_FREE(subreq);
555         if (!NT_STATUS_IS_OK(status)) {
556                 NTSTATUS old = status;
557                 status = nt_status_np_pipe(old);
558                 tevent_req_nterror(req, status);
559                 return;
560         }
561
562         if (nread == 0 && state->out_data.length != 0) {
563                 tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
564                 return;
565         }
566
567         state->out_data.length = nread;
568         state->out_remaining = 0;
569
570         /*
571          * TODO: add STATUS_BUFFER_OVERFLOW handling, once we also
572          * handle it in SMB1 pipe_read_andx_done().
573          */
574
575         tevent_req_done(req);
576 }
577
578 static NTSTATUS smbd_smb2_read_recv(struct tevent_req *req,
579                                     TALLOC_CTX *mem_ctx,
580                                     DATA_BLOB *out_data,
581                                     uint32_t *out_remaining)
582 {
583         NTSTATUS status;
584         struct smbd_smb2_read_state *state = &_state;//tevent_req_data(req,
585                                              //struct smbd_smb2_read_state);
586
587         //if (tevent_req_is_nterror(req, &status)) {
588         //      tevent_req_received(req);
589         //      return status;
590         //}
591
592         *out_data = state->out_data;
593         talloc_steal(mem_ctx, out_data->data);
594         *out_remaining = state->out_remaining;
595
596         if (state->out_headers.length > 0) {
597                 //talloc_steal(mem_ctx, state);
598                 //talloc_set_destructor(state, smb2_smb2_read_state_deny_destructor);
599                 //tevent_req_received(req);
600                 state->smb2req->queue_entry.sendfile_header = &state->out_headers;
601                 state->smb2req->queue_entry.sendfile_private = state;
602                 state->smb2req->queue_entry.sendfile_fn = smb2_sendfile_fn;
603                 //talloc_set_destructor(state, smb2_sendfile_send_data);
604         } else {
605                 //tevent_req_received(req);
606         }
607
608         return NT_STATUS_OK;
609 }