s3:smbd: implement SMB2 Read
[ira/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 "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
24
25 static NTSTATUS smbd_smb2_read(struct smbd_smb2_request *req,
26                                uint32_t in_smbpid,
27                                uint64_t in_file_id_volatile,
28                                uint32_t in_length,
29                                uint64_t in_offset,
30                                uint32_t in_minimum,
31                                uint32_t in_remaining,
32                                DATA_BLOB *out_data_buffer,
33                                uint32_t *out_remaining);
34
35 NTSTATUS smbd_smb2_request_process_read(struct smbd_smb2_request *req)
36 {
37         const uint8_t *inhdr;
38         const uint8_t *inbody;
39         int i = req->current_idx;
40         uint8_t *outhdr;
41         DATA_BLOB outbody;
42         DATA_BLOB outdyn;
43         size_t expected_body_size = 0x31;
44         size_t body_size;
45         uint32_t in_smbpid;
46         uint32_t in_length;
47         uint64_t in_offset;
48         uint64_t in_file_id_persistent;
49         uint64_t in_file_id_volatile;
50         uint32_t in_minimum_count;
51         uint32_t in_remaining_bytes;
52         uint8_t out_data_offset;
53         DATA_BLOB out_data_buffer;
54         uint32_t out_data_remaining;
55         NTSTATUS status;
56
57         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
58         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
59                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
60         }
61
62         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
63
64         body_size = SVAL(inbody, 0x00);
65         if (body_size != expected_body_size) {
66                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
67         }
68
69         in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
70
71         in_length               = IVAL(inbody, 0x04);
72         in_offset               = BVAL(inbody, 0x08);
73         in_file_id_persistent   = BVAL(inbody, 0x10);
74         in_file_id_volatile     = BVAL(inbody, 0x18);
75         in_minimum_count        = IVAL(inbody, 0x20);
76         in_remaining_bytes      = IVAL(inbody, 0x28);
77
78         /* check the max read size */
79         if (in_length > 0x00010000) {
80                 DEBUG(0,("here:%s: 0x%08X: 0x%08X\n",
81                         __location__, in_length, 0x00010000));
82                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
83         }
84
85         if (in_file_id_persistent != 0) {
86                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
87         }
88
89         status = smbd_smb2_read(req,
90                                 in_smbpid,
91                                 in_file_id_volatile,
92                                 in_length,
93                                 in_offset,
94                                 in_minimum_count,
95                                 in_remaining_bytes,
96                                 &out_data_buffer,
97                                 &out_data_remaining);
98         if (!NT_STATUS_IS_OK(status)) {
99                 return smbd_smb2_request_error(req, status);
100         }
101
102         out_data_offset = SMB2_HDR_BODY + 0x10;
103
104         outhdr = (uint8_t *)req->out.vector[i].iov_base;
105
106         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
107         if (outbody.data == NULL) {
108                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
109         }
110
111         SSVAL(outbody.data, 0x00, 0x10 + 1);    /* struct size */
112         SCVAL(outbody.data, 0x02,
113               out_data_offset);                 /* data offset */
114         SCVAL(outbody.data, 0x03, 0);           /* reserved */
115         SIVAL(outbody.data, 0x04,
116               out_data_buffer.length);          /* data length */
117         SIVAL(outbody.data, 0x08,
118               out_data_remaining);              /* data remaining */
119         SIVAL(outbody.data, 0x0C, 0);           /* reserved */
120
121         outdyn = out_data_buffer;
122
123         return smbd_smb2_request_done(req, outbody, &outdyn);
124 }
125
126 static NTSTATUS smbd_smb2_read(struct smbd_smb2_request *req,
127                                uint32_t in_smbpid,
128                                uint64_t in_file_id_volatile,
129                                uint32_t in_length,
130                                uint64_t in_offset,
131                                uint32_t in_minimum,
132                                uint32_t in_remaining,
133                                DATA_BLOB *out_data_buffer,
134                                uint32_t *out_remaining)
135 {
136         struct smb_request *smbreq;
137         connection_struct *conn = req->tcon->compat_conn;
138         files_struct *fsp;
139         ssize_t nread = -1;
140         struct lock_struct lock;
141
142         DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n",
143                   (unsigned long long)in_file_id_volatile));
144
145         smbreq = smbd_smb2_fake_smb_request(req);
146         if (smbreq == NULL) {
147                 return NT_STATUS_NO_MEMORY;
148         }
149
150         /* If it's an IPC, pass off the pipe handler. */
151         if (IS_IPC(conn)) {
152                 return NT_STATUS_NOT_IMPLEMENTED;
153         }
154
155         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
156         if (fsp == NULL) {
157                 return NT_STATUS_FILE_CLOSED;
158         }
159         if (conn != fsp->conn) {
160                 return NT_STATUS_FILE_CLOSED;
161         }
162         if (req->session->vuid != fsp->vuid) {
163                 return NT_STATUS_FILE_CLOSED;
164         }
165
166         if (!CHECK_READ(fsp, smbreq)) {
167                 return NT_STATUS_ACCESS_DENIED;
168         }
169
170         *out_data_buffer = data_blob_talloc(req, NULL, in_length);
171         if (in_length > 0 && out_data_buffer->data == NULL) {
172                 return NT_STATUS_NO_MEMORY;
173         }
174
175         init_strict_lock_struct(fsp,
176                                 in_smbpid,
177                                 in_offset,
178                                 in_length,
179                                 READ_LOCK,
180                                 &lock);
181
182         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
183                 return NT_STATUS_FILE_LOCK_CONFLICT;
184         }
185
186         nread = read_file(fsp,
187                           (char *)out_data_buffer->data,
188                           in_offset,
189                           in_length);
190
191         SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
192
193         if (nread < 0) {
194                 DEBUG(5,("smbd_smb2_read: read_file[%s] nread[%lld]\n",
195                         fsp->fsp_name, (long long)nread));
196                 return NT_STATUS_ACCESS_DENIED;
197         }
198         if (nread == 0 && in_length != 0) {
199                 DEBUG(5,("smbd_smb2_read: read_file[%s] end of file\n",
200                         fsp->fsp_name));
201                 return NT_STATUS_END_OF_FILE;
202         }
203
204         out_data_buffer->length = nread;
205         *out_remaining = 0;
206         return NT_STATUS_OK;
207 }