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