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