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