2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "../libcli/smb/smb_common.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "../librpc/gen_ndr/ndr_smb2_lease_struct.h"
29 #include "../lib/util/tevent_ntstatus.h"
32 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
34 switch(in_oplock_level) {
35 case SMB2_OPLOCK_LEVEL_NONE:
37 case SMB2_OPLOCK_LEVEL_II:
38 return LEVEL_II_OPLOCK;
39 case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
40 return EXCLUSIVE_OPLOCK;
41 case SMB2_OPLOCK_LEVEL_BATCH:
43 case SMB2_OPLOCK_LEVEL_LEASE:
46 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
48 (unsigned int)in_oplock_level));
53 static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
55 if (BATCH_OPLOCK_TYPE(oplock_type)) {
56 return SMB2_OPLOCK_LEVEL_BATCH;
57 } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type)) {
58 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
59 } else if (oplock_type == LEVEL_II_OPLOCK) {
60 return SMB2_OPLOCK_LEVEL_II;
61 } else if (oplock_type == LEASE_OPLOCK) {
62 return SMB2_OPLOCK_LEVEL_LEASE;
64 return SMB2_OPLOCK_LEVEL_NONE;
68 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
69 struct tevent_context *ev,
70 struct smbd_smb2_request *smb2req,
71 uint8_t in_oplock_level,
72 uint32_t in_impersonation_level,
73 uint32_t in_desired_access,
74 uint32_t in_file_attributes,
75 uint32_t in_share_access,
76 uint32_t in_create_disposition,
77 uint32_t in_create_options,
79 struct smb2_create_blobs in_context_blobs);
80 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
82 uint8_t *out_oplock_level,
83 uint32_t *out_create_action,
84 struct timespec *out_creation_ts,
85 struct timespec *out_last_access_ts,
86 struct timespec *out_last_write_ts,
87 struct timespec *out_change_ts,
88 uint64_t *out_allocation_size,
89 uint64_t *out_end_of_file,
90 uint32_t *out_file_attributes,
91 uint64_t *out_file_id_persistent,
92 uint64_t *out_file_id_volatile,
93 struct smb2_create_blobs *out_context_blobs);
95 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
96 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
98 const uint8_t *inbody;
99 const struct iovec *indyniov;
100 uint8_t in_oplock_level;
101 uint32_t in_impersonation_level;
102 uint32_t in_desired_access;
103 uint32_t in_file_attributes;
104 uint32_t in_share_access;
105 uint32_t in_create_disposition;
106 uint32_t in_create_options;
107 uint16_t in_name_offset;
108 uint16_t in_name_length;
109 DATA_BLOB in_name_buffer;
110 char *in_name_string;
111 size_t in_name_string_size;
112 uint32_t name_offset = 0;
113 uint32_t name_available_length = 0;
114 uint32_t in_context_offset;
115 uint32_t in_context_length;
116 DATA_BLOB in_context_buffer;
117 struct smb2_create_blobs in_context_blobs;
118 uint32_t context_offset = 0;
119 uint32_t context_available_length = 0;
123 struct tevent_req *tsubreq;
125 status = smbd_smb2_request_verify_sizes(smb2req, 0x39);
126 if (!NT_STATUS_IS_OK(status)) {
127 return smbd_smb2_request_error(smb2req, status);
129 inbody = SMBD_SMB2_IN_BODY_PTR(smb2req);
131 in_oplock_level = CVAL(inbody, 0x03);
132 in_impersonation_level = IVAL(inbody, 0x04);
133 in_desired_access = IVAL(inbody, 0x18);
134 in_file_attributes = IVAL(inbody, 0x1C);
135 in_share_access = IVAL(inbody, 0x20);
136 in_create_disposition = IVAL(inbody, 0x24);
137 in_create_options = IVAL(inbody, 0x28);
138 in_name_offset = SVAL(inbody, 0x2C);
139 in_name_length = SVAL(inbody, 0x2E);
140 in_context_offset = IVAL(inbody, 0x30);
141 in_context_length = IVAL(inbody, 0x34);
144 * First check if the dynamic name and context buffers
145 * are correctly specified.
147 * Note: That we don't check if the name and context buffers
151 dyn_offset = SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(smb2req);
153 if (in_name_offset == 0 && in_name_length == 0) {
156 } else if (in_name_offset < dyn_offset) {
157 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
159 name_offset = in_name_offset - dyn_offset;
162 indyniov = SMBD_SMB2_IN_DYN_IOV(smb2req);
164 if (name_offset > indyniov->iov_len) {
165 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
168 name_available_length = indyniov->iov_len - name_offset;
170 if (in_name_length > name_available_length) {
171 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
174 in_name_buffer.data = (uint8_t *)indyniov->iov_base + name_offset;
175 in_name_buffer.length = in_name_length;
177 if (in_context_offset == 0 && in_context_length == 0) {
180 } else if (in_context_offset < dyn_offset) {
181 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
183 context_offset = in_context_offset - dyn_offset;
186 if (context_offset > indyniov->iov_len) {
187 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
190 context_available_length = indyniov->iov_len - context_offset;
192 if (in_context_length > context_available_length) {
193 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
196 in_context_buffer.data = (uint8_t *)indyniov->iov_base +
198 in_context_buffer.length = in_context_length;
201 * Now interpret the name and context buffers
204 ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
206 in_name_buffer.length,
208 &in_name_string_size);
210 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
213 if (in_name_buffer.length == 0) {
214 in_name_string_size = 0;
217 if (strlen(in_name_string) != in_name_string_size) {
218 return smbd_smb2_request_error(smb2req, NT_STATUS_OBJECT_NAME_INVALID);
221 ZERO_STRUCT(in_context_blobs);
222 status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
223 if (!NT_STATUS_IS_OK(status)) {
224 return smbd_smb2_request_error(smb2req, status);
227 tsubreq = smbd_smb2_create_send(smb2req,
228 smb2req->sconn->ev_ctx,
231 in_impersonation_level,
235 in_create_disposition,
239 if (tsubreq == NULL) {
240 smb2req->subreq = NULL;
241 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
243 tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
245 return smbd_smb2_request_pending_queue(smb2req, tsubreq, 500);
248 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
250 uint8_t *reqhdr = SMBD_SMB2_OUT_HDR_PTR(smb2req);
251 return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
254 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
256 struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
257 struct smbd_smb2_request);
260 uint8_t out_oplock_level = 0;
261 uint32_t out_create_action = 0;
262 connection_struct *conn = smb2req->tcon->compat;
263 struct timespec out_creation_ts = { 0, };
264 struct timespec out_last_access_ts = { 0, };
265 struct timespec out_last_write_ts = { 0, };
266 struct timespec out_change_ts = { 0, };
267 uint64_t out_allocation_size = 0;
268 uint64_t out_end_of_file = 0;
269 uint32_t out_file_attributes = 0;
270 uint64_t out_file_id_persistent = 0;
271 uint64_t out_file_id_volatile = 0;
272 struct smb2_create_blobs out_context_blobs;
273 DATA_BLOB out_context_buffer;
274 uint16_t out_context_buffer_offset = 0;
276 NTSTATUS error; /* transport error */
278 status = smbd_smb2_create_recv(tsubreq,
286 &out_allocation_size,
288 &out_file_attributes,
289 &out_file_id_persistent,
290 &out_file_id_volatile,
292 if (!NT_STATUS_IS_OK(status)) {
293 error = smbd_smb2_request_error(smb2req, status);
294 if (!NT_STATUS_IS_OK(error)) {
295 smbd_server_connection_terminate(smb2req->xconn,
302 status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
303 if (!NT_STATUS_IS_OK(status)) {
304 error = smbd_smb2_request_error(smb2req, status);
305 if (!NT_STATUS_IS_OK(error)) {
306 smbd_server_connection_terminate(smb2req->xconn,
313 if (out_context_buffer.length > 0) {
314 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
317 outbody = smbd_smb2_generate_outbody(smb2req, 0x58);
318 if (outbody.data == NULL) {
319 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
320 if (!NT_STATUS_IS_OK(error)) {
321 smbd_server_connection_terminate(smb2req->xconn,
328 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
329 SCVAL(outbody.data, 0x02,
330 out_oplock_level); /* oplock level */
331 SCVAL(outbody.data, 0x03, 0); /* reserved */
332 SIVAL(outbody.data, 0x04,
333 out_create_action); /* create action */
334 put_long_date_timespec(conn->ts_res,
335 (char *)outbody.data + 0x08,
336 out_creation_ts); /* creation time */
337 put_long_date_timespec(conn->ts_res,
338 (char *)outbody.data + 0x10,
339 out_last_access_ts); /* last access time */
340 put_long_date_timespec(conn->ts_res,
341 (char *)outbody.data + 0x18,
342 out_last_write_ts); /* last write time */
343 put_long_date_timespec(conn->ts_res,
344 (char *)outbody.data + 0x20,
345 out_change_ts); /* change time */
346 SBVAL(outbody.data, 0x28,
347 out_allocation_size); /* allocation size */
348 SBVAL(outbody.data, 0x30,
349 out_end_of_file); /* end of file */
350 SIVAL(outbody.data, 0x38,
351 out_file_attributes); /* file attributes */
352 SIVAL(outbody.data, 0x3C, 0); /* reserved */
353 SBVAL(outbody.data, 0x40,
354 out_file_id_persistent); /* file id (persistent) */
355 SBVAL(outbody.data, 0x48,
356 out_file_id_volatile); /* file id (volatile) */
357 SIVAL(outbody.data, 0x50,
358 out_context_buffer_offset); /* create contexts offset */
359 SIVAL(outbody.data, 0x54,
360 out_context_buffer.length); /* create contexts length */
362 outdyn = out_context_buffer;
364 error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
365 if (!NT_STATUS_IS_OK(error)) {
366 smbd_server_connection_terminate(smb2req->xconn,
372 static bool smb2_lease_key_valid(const struct smb2_lease_key *key)
374 return ((key->data[0] != 0) || (key->data[1] != 0));
377 static NTSTATUS smbd_smb2_create_durable_lease_check(struct smb_request *smb1req,
378 const char *requested_filename, const struct files_struct *fsp,
379 const struct smb2_lease *lease_ptr)
381 struct smb_filename *smb_fname = NULL;
385 if (lease_ptr == NULL) {
386 if (fsp->oplock_type != LEASE_OPLOCK) {
389 DEBUG(10, ("Reopened file has lease, but no lease "
391 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
394 if (fsp->oplock_type != LEASE_OPLOCK) {
395 DEBUG(10, ("Lease requested, but reopened file has no "
397 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
400 if (!smb2_lease_key_equal(&lease_ptr->lease_key,
401 &fsp->lease->lease.lease_key)) {
402 DEBUG(10, ("Different lease key requested than found "
403 "in reopened file\n"));
404 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
407 ucf_flags = filename_create_ucf_flags(smb1req, FILE_OPEN);
408 status = filename_convert(talloc_tos(), fsp->conn,
409 requested_filename, ucf_flags,
411 if (!NT_STATUS_IS_OK(status)) {
412 DEBUG(10, ("filename_convert returned %s\n",
417 if (!strequal(fsp->fsp_name->base_name, smb_fname->base_name)) {
418 DEBUG(10, ("Lease requested for file %s, reopened file "
419 "is named %s\n", smb_fname->base_name,
420 fsp->fsp_name->base_name));
421 TALLOC_FREE(smb_fname);
422 return NT_STATUS_INVALID_PARAMETER;
425 TALLOC_FREE(smb_fname);
430 struct smbd_smb2_create_state {
431 struct tevent_context *ev;
432 struct smbd_smb2_request *smb2req;
433 struct smb_request *smb1req;
434 bool open_was_deferred;
435 struct tevent_immediate *im;
436 struct timeval request_time;
438 struct deferred_open_record *open_rec;
439 files_struct *result;
440 bool replay_operation;
441 uint8_t in_oplock_level;
442 uint32_t in_create_disposition;
443 int requested_oplock_level;
446 struct ea_list *ea_list;
447 NTTIME max_access_time;
448 struct security_descriptor *sec_desc;
449 uint64_t allocation_size;
450 struct GUID _create_guid;
451 struct GUID *create_guid;
453 bool durable_requested;
454 uint32_t durable_timeout_msec;
455 bool do_durable_reconnect;
456 uint64_t persistent_id;
457 struct smb2_lease lease;
458 struct smb2_lease *lease_ptr;
460 bool need_replay_cache;
461 struct smbXsrv_open *op;
463 struct smb2_create_blob *dhnc;
464 struct smb2_create_blob *dh2c;
465 struct smb2_create_blob *dhnq;
466 struct smb2_create_blob *dh2q;
467 struct smb2_create_blob *rqls;
468 struct smb2_create_blob *exta;
469 struct smb2_create_blob *mxac;
470 struct smb2_create_blob *secd;
471 struct smb2_create_blob *alsi;
472 struct smb2_create_blob *twrp;
473 struct smb2_create_blob *qfid;
474 struct smb2_create_blob *svhdx;
476 uint8_t out_oplock_level;
477 uint32_t out_create_action;
478 struct timespec out_creation_ts;
479 struct timespec out_last_access_ts;
480 struct timespec out_last_write_ts;
481 struct timespec out_change_ts;
482 uint64_t out_allocation_size;
483 uint64_t out_end_of_file;
484 uint32_t out_file_attributes;
485 uint64_t out_file_id_persistent;
486 uint64_t out_file_id_volatile;
487 struct smb2_create_blobs *out_context_blobs;
490 static NTSTATUS smbd_smb2_create_fetch_create_ctx(
491 struct tevent_req *req,
492 struct smb2_create_blobs *in_context_blobs)
494 struct smbd_smb2_create_state *state = tevent_req_data(
495 req, struct smbd_smb2_create_state);
497 state->dhnq = smb2_create_blob_find(in_context_blobs,
498 SMB2_CREATE_TAG_DHNQ);
499 state->dhnc = smb2_create_blob_find(in_context_blobs,
500 SMB2_CREATE_TAG_DHNC);
501 state->dh2q = smb2_create_blob_find(in_context_blobs,
502 SMB2_CREATE_TAG_DH2Q);
503 state->dh2c = smb2_create_blob_find(in_context_blobs,
504 SMB2_CREATE_TAG_DH2C);
505 if (state->smb2req->xconn->smb2.server.capabilities & SMB2_CAP_LEASING) {
506 state->rqls = smb2_create_blob_find(in_context_blobs,
507 SMB2_CREATE_TAG_RQLS);
510 if (((state->dhnc != NULL) && (state->dh2c != NULL)) ||
511 ((state->dhnc != NULL) && (state->dh2q != NULL)) ||
512 ((state->dh2c != NULL) && (state->dhnq != NULL)) ||
513 ((state->dh2q != NULL) && (state->dh2c != NULL)))
515 /* not both are allowed at the same time */
516 return NT_STATUS_INVALID_PARAMETER;
519 if (state->dhnc != NULL) {
520 uint32_t num_blobs_allowed;
522 if (state->dhnc->data.length != 16) {
523 return NT_STATUS_INVALID_PARAMETER;
527 * According to MS-SMB2: 3.3.5.9.7, "Handling the
528 * SMB2_CREATE_DURABLE_HANDLE_RECONNECT Create Context",
529 * we should ignore an additional dhnq blob, but fail
530 * the request (with status OBJECT_NAME_NOT_FOUND) if
531 * any other extra create blob has been provided.
533 * (Note that the cases of an additional dh2q or dh2c blob
534 * which require a different error code, have been treated
538 if (state->dhnq != NULL) {
539 num_blobs_allowed = 2;
541 num_blobs_allowed = 1;
544 if (state->rqls != NULL) {
545 num_blobs_allowed += 1;
548 if (in_context_blobs->num_blobs != num_blobs_allowed) {
549 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
553 if (state->dh2c!= NULL) {
554 uint32_t num_blobs_allowed;
556 if (state->dh2c->data.length != 36) {
557 return NT_STATUS_INVALID_PARAMETER;
561 * According to MS-SMB2: 3.3.5.9.12, "Handling the
562 * SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 Create Context",
563 * we should fail the request with status
564 * OBJECT_NAME_NOT_FOUND if any other create blob has been
567 * (Note that the cases of an additional dhnq, dhnc or dh2q
568 * blob which require a different error code, have been
572 num_blobs_allowed = 1;
574 if (state->rqls != NULL) {
575 num_blobs_allowed += 1;
578 if (in_context_blobs->num_blobs != num_blobs_allowed) {
579 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
583 state->exta = smb2_create_blob_find(in_context_blobs,
584 SMB2_CREATE_TAG_EXTA);
585 state->mxac = smb2_create_blob_find(in_context_blobs,
586 SMB2_CREATE_TAG_MXAC);
587 state->secd = smb2_create_blob_find(in_context_blobs,
588 SMB2_CREATE_TAG_SECD);
589 state->alsi = smb2_create_blob_find(in_context_blobs,
590 SMB2_CREATE_TAG_ALSI);
591 state->twrp = smb2_create_blob_find(in_context_blobs,
592 SMB2_CREATE_TAG_TWRP);
593 state->qfid = smb2_create_blob_find(in_context_blobs,
594 SMB2_CREATE_TAG_QFID);
595 if (state->smb2req->xconn->protocol >= PROTOCOL_SMB3_02) {
597 * This was introduced with SMB3_02
599 state->svhdx = smb2_create_blob_find(
600 in_context_blobs, SVHDX_OPEN_DEVICE_CONTEXT);
606 static void smbd_smb2_create_before_exec(struct tevent_req *req);
607 static void smbd_smb2_create_after_exec(struct tevent_req *req);
608 static void smbd_smb2_create_finish(struct tevent_req *req);
610 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
611 struct tevent_context *ev,
612 struct smbd_smb2_request *smb2req,
613 uint8_t in_oplock_level,
614 uint32_t in_impersonation_level,
615 uint32_t in_desired_access,
616 uint32_t in_file_attributes,
617 uint32_t in_share_access,
618 uint32_t in_create_disposition,
619 uint32_t in_create_options,
621 struct smb2_create_blobs in_context_blobs)
623 struct tevent_req *req = NULL;
624 struct smbd_smb2_create_state *state = NULL;
626 struct smb_request *smb1req = NULL;
627 struct smb_filename *smb_fname = NULL;
630 req = tevent_req_create(mem_ctx, &state,
631 struct smbd_smb2_create_state);
635 *state = (struct smbd_smb2_create_state) {
638 .in_oplock_level = in_oplock_level,
639 .in_create_disposition = in_create_disposition,
642 smb1req = smbd_smb2_fake_smb_request(smb2req);
643 if (tevent_req_nomem(smb1req, req)) {
644 return tevent_req_post(req, state->ev);
646 state->smb1req = smb1req;
648 if (smb2req->subreq == NULL) {
649 DBG_DEBUG("name [%s]\n", in_name);
651 struct smbd_smb2_create_state *old_state = tevent_req_data(
652 smb2req->subreq, struct smbd_smb2_create_state);
654 DBG_DEBUG("reentrant for file %s\n", in_name);
656 state->id = old_state->id;
657 state->request_time = old_state->request_time;
658 state->open_rec = talloc_move(state, &old_state->open_rec);
659 state->open_was_deferred = old_state->open_was_deferred;
662 TALLOC_FREE(smb2req->subreq);
663 smb2req->subreq = req;
665 if (lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
666 state->requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
668 state->requested_oplock_level = state->in_oplock_level;
671 /* these are ignored for SMB2 */
672 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
673 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
675 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
677 state->fname = talloc_strdup(state, in_name);
678 if (tevent_req_nomem(state->fname, req)) {
679 return tevent_req_post(req, state->ev);
682 state->out_context_blobs = talloc_zero(state, struct smb2_create_blobs);
683 if (tevent_req_nomem(state->out_context_blobs, req)) {
684 return tevent_req_post(req, state->ev);
687 status = smbd_smb2_create_fetch_create_ctx(req, &in_context_blobs);
688 if (tevent_req_nterror(req, status)) {
689 return tevent_req_post(req, state->ev);
692 if (IS_IPC(smb1req->conn)) {
693 const char *pipe_name = in_name;
695 if (state->dhnc != NULL || state->dh2c != NULL) {
696 /* durable handles are not supported on IPC$ */
697 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
698 return tevent_req_post(req, state->ev);
701 if (!lp_nt_pipe_support()) {
702 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
703 return tevent_req_post(req, state->ev);
706 status = open_np_file(smb1req, pipe_name, &state->result);
707 if (!NT_STATUS_IS_OK(status)) {
708 tevent_req_nterror(req, status);
709 return tevent_req_post(req, state->ev);
711 state->info = FILE_WAS_OPENED;
713 smbd_smb2_create_finish(req);
716 } else if (CAN_PRINT(smb1req->conn)) {
717 if (state->dhnc != NULL || state->dh2c != NULL) {
718 /* durable handles are not supported on printers */
719 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
720 return tevent_req_post(req, state->ev);
723 status = file_new(smb1req, smb1req->conn, &state->result);
724 if(!NT_STATUS_IS_OK(status)) {
725 tevent_req_nterror(req, status);
726 return tevent_req_post(req, state->ev);
729 status = print_spool_open(state->result, in_name,
731 if (!NT_STATUS_IS_OK(status)) {
732 file_free(smb1req, state->result);
733 tevent_req_nterror(req, status);
734 return tevent_req_post(req, state->ev);
736 state->info = FILE_WAS_CREATED;
738 smbd_smb2_create_finish(req);
742 smbd_smb2_create_before_exec(req);
743 if (!tevent_req_is_in_progress(req)) {
744 return tevent_req_post(req, state->ev);
747 DBG_DEBUG("open execution phase\n");
750 * For the backend file open procedure, there are
751 * three possible modes: replay operation (in which case
752 * there is nothing else to do), durable_reconnect or
755 if (state->replay_operation) {
756 state->result = state->op->compat;
757 state->result->op = state->op;
758 state->update_open = false;
759 state->info = state->op->create_action;
761 smbd_smb2_create_after_exec(req);
762 if (!tevent_req_is_in_progress(req)) {
766 smbd_smb2_create_finish(req);
769 } else if (state->do_durable_reconnect) {
770 DATA_BLOB new_cookie = data_blob_null;
771 NTTIME now = timeval_to_nttime(&smb2req->request_time);
773 status = smb2srv_open_recreate(smb2req->xconn,
774 smb1req->conn->session_info,
775 state->persistent_id,
779 if (!NT_STATUS_IS_OK(status)) {
780 DBG_NOTICE("smb2srv_open_recreate failed: %s\n",
782 tevent_req_nterror(req, status);
783 return tevent_req_post(req, state->ev);
786 DBG_DEBUG("%s to recreate durable handle\n",
787 state->op->global->durable ? "succeeded" : "failed");
789 if (!state->op->global->durable) {
790 talloc_free(state->op);
791 tevent_req_nterror(req,
792 NT_STATUS_OBJECT_NAME_NOT_FOUND);
793 return tevent_req_post(req, state->ev);
796 status = SMB_VFS_DURABLE_RECONNECT(smb1req->conn,
798 state->op, /* smbXsrv_open input */
799 state->op->global->backend_cookie,
800 state->op, /* TALLOC_CTX */
803 if (!NT_STATUS_IS_OK(status)) {
804 NTSTATUS return_status;
806 return_status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
808 DBG_NOTICE("durable_reconnect failed: %s => %s\n",
810 nt_errstr(return_status));
812 tevent_req_nterror(req, return_status);
813 return tevent_req_post(req, state->ev);
816 DBG_DEBUG("oplock_type=%u, lease_ptr==%p\n",
817 (unsigned)state->result->oplock_type, state->lease_ptr);
819 status = smbd_smb2_create_durable_lease_check(
820 smb1req, state->fname, state->result, state->lease_ptr);
821 if (!NT_STATUS_IS_OK(status)) {
822 close_file(smb1req, state->result, SHUTDOWN_CLOSE);
823 tevent_req_nterror(req, status);
824 return tevent_req_post(req, state->ev);
827 data_blob_free(&state->op->global->backend_cookie);
828 state->op->global->backend_cookie = new_cookie;
830 state->op->status = NT_STATUS_OK;
831 state->op->global->disconnect_time = 0;
833 /* save the timout for later update */
834 state->durable_timeout_msec = state->op->global->durable_timeout_msec;
836 state->update_open = true;
838 state->info = FILE_WAS_OPENED;
840 smbd_smb2_create_after_exec(req);
841 if (!tevent_req_is_in_progress(req)) {
845 smbd_smb2_create_finish(req);
849 if (state->requested_oplock_level == SMB2_OPLOCK_LEVEL_LEASE) {
850 if (state->lease_ptr == NULL) {
851 state->requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
854 state->lease_ptr = NULL;
858 * For a DFS path the function parse_dfs_path()
859 * will do the path processing.
862 if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
863 /* convert '\\' into '/' */
864 status = check_path_syntax(state->fname);
865 if (!NT_STATUS_IS_OK(status)) {
866 tevent_req_nterror(req, status);
867 return tevent_req_post(req, state->ev);
871 ucf_flags = filename_create_ucf_flags(
872 smb1req, state->in_create_disposition);
873 status = filename_convert(req,
877 NULL, /* ppath_contains_wcards */
879 if (!NT_STATUS_IS_OK(status)) {
880 tevent_req_nterror(req, status);
881 return tevent_req_post(req, state->ev);
885 * MS-SMB2: 2.2.13 SMB2 CREATE Request
886 * ImpersonationLevel ... MUST contain one of the
887 * following values. The server MUST validate this
888 * field, but otherwise ignore it.
890 * NB. The source4/torture/smb2/durable_open.c test
891 * shows this check is only done on real opens, not
892 * on durable handle-reopens.
895 if (in_impersonation_level >
896 SMB2_IMPERSONATION_DELEGATE) {
897 tevent_req_nterror(req,
898 NT_STATUS_BAD_IMPERSONATION_LEVEL);
899 return tevent_req_post(req, state->ev);
903 * We know we're going to do a local open, so now
904 * we must be protocol strict. JRA.
906 * MS-SMB2: 3.3.5.9 - Receiving an SMB2 CREATE Request
907 * If the file name length is greater than zero and the
908 * first character is a path separator character, the
909 * server MUST fail the request with
910 * STATUS_INVALID_PARAMETER.
912 if (in_name[0] == '\\' || in_name[0] == '/') {
913 tevent_req_nterror(req,
914 NT_STATUS_INVALID_PARAMETER);
915 return tevent_req_post(req, state->ev);
918 status = SMB_VFS_CREATE_FILE(smb1req->conn,
920 0, /* root_dir_fid */
924 state->in_create_disposition,
927 map_smb2_oplock_levels_to_samba(
928 state->requested_oplock_level),
930 state->allocation_size,
931 0, /* private_flags */
937 state->out_context_blobs);
938 if (!NT_STATUS_IS_OK(status)) {
939 if (open_was_deferred(smb1req->xconn, smb1req->mid)) {
940 SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(smb2req->profile);
943 tevent_req_nterror(req, status);
944 return tevent_req_post(req, state->ev);
946 state->op = state->result->op;
948 smbd_smb2_create_after_exec(req);
949 if (!tevent_req_is_in_progress(req)) {
953 smbd_smb2_create_finish(req);
957 static void smbd_smb2_create_before_exec(struct tevent_req *req)
959 struct smbd_smb2_create_state *state = tevent_req_data(
960 req, struct smbd_smb2_create_state);
961 struct smb_request *smb1req = state->smb1req;
962 struct smbd_smb2_request *smb2req = state->smb2req;
965 if (state->exta != NULL) {
966 if (!lp_ea_support(SNUM(smb2req->tcon->compat))) {
967 tevent_req_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
971 state->ea_list = read_nttrans_ea_list(
973 (const char *)state->exta->data.data,
974 state->exta->data.length);
975 if (state->ea_list == NULL) {
976 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
977 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
982 * NB. When SMB2+ unix extensions are added,
983 * we need to relax this check in invalid
984 * names - we used to not do this if
985 * lp_posix_pathnames() was false.
987 if (ea_list_has_invalid_name(state->ea_list)) {
988 tevent_req_nterror(req, STATUS_INVALID_EA_NAME);
993 if (state->mxac != NULL) {
994 if (state->mxac->data.length == 0) {
995 state->max_access_time = 0;
996 } else if (state->mxac->data.length == 8) {
997 state->max_access_time = BVAL(state->mxac->data.data, 0);
999 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1004 if (state->secd != NULL) {
1005 enum ndr_err_code ndr_err;
1007 state->sec_desc = talloc_zero(state, struct security_descriptor);
1008 if (tevent_req_nomem(state->sec_desc, req)) {
1012 ndr_err = ndr_pull_struct_blob(&state->secd->data,
1013 state->sec_desc, state->sec_desc,
1014 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
1015 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1016 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
1017 ndr_errstr(ndr_err)));
1018 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1023 if (state->dhnq != NULL) {
1024 if (state->dhnq->data.length != 16) {
1025 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1029 if (state->dh2q != NULL) {
1030 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1035 * durable handle request is processed below.
1037 state->durable_requested = true;
1039 * Set the timeout to 16 mins.
1041 * TODO: test this against Windows 2012
1042 * as the default for durable v2 is 1 min.
1044 state->durable_timeout_msec = (16*60*1000);
1047 if (state->dh2q != NULL) {
1048 const uint8_t *p = state->dh2q->data.data;
1049 uint32_t durable_v2_timeout = 0;
1050 DATA_BLOB create_guid_blob;
1054 if (state->dh2q->data.length != 32) {
1055 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1059 if (state->dhnq != NULL) {
1060 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1064 durable_v2_timeout = IVAL(p, 0);
1065 create_guid_blob = data_blob_const(p + 16, 16);
1067 status = GUID_from_ndr_blob(&create_guid_blob,
1068 &state->_create_guid);
1069 if (tevent_req_nterror(req, status)) {
1072 state->create_guid = &state->_create_guid;
1075 * we need to store the create_guid later
1077 state->update_open = true;
1080 * And we need to create a cache for replaying the
1083 state->need_replay_cache = true;
1086 * durable handle v2 request processed below
1088 state->durable_requested = true;
1089 state->durable_timeout_msec = durable_v2_timeout;
1090 if (state->durable_timeout_msec == 0) {
1092 * Set the timeout to 1 min as default.
1094 * This matches Windows 2012.
1096 state->durable_timeout_msec = (60*1000);
1100 * Check for replay operation.
1101 * Only consider it when we have dh2q.
1102 * If we do not have a replay operation, verify that
1103 * the create_guid is not cached for replay.
1105 hdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
1106 flags = IVAL(hdr, SMB2_HDR_FLAGS);
1107 state->replay_operation =
1108 flags & SMB2_HDR_FLAG_REPLAY_OPERATION;
1110 status = smb2srv_open_lookup_replay_cache(smb2req->xconn,
1114 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1115 state->replay_operation = false;
1116 } else if (tevent_req_nterror(req, status)) {
1117 DBG_WARNING("smb2srv_open_lookup_replay_cache "
1118 "failed: %s\n", nt_errstr(status));
1120 } else if (!state->replay_operation) {
1122 * If a create without replay operation flag
1123 * is sent but with a create_guid that is
1124 * currently in the replay cache -- fail.
1126 status = NT_STATUS_DUPLICATE_OBJECTID;
1127 (void)tevent_req_nterror(req, status);
1132 if (state->dhnc != NULL) {
1133 state->persistent_id = BVAL(state->dhnc->data.data, 0);
1134 state->do_durable_reconnect = true;
1137 if (state->dh2c != NULL) {
1138 const uint8_t *p = state->dh2c->data.data;
1139 DATA_BLOB create_guid_blob;
1141 state->persistent_id = BVAL(p, 0);
1142 create_guid_blob = data_blob_const(p + 16, 16);
1144 status = GUID_from_ndr_blob(&create_guid_blob,
1145 &state->_create_guid);
1146 if (tevent_req_nterror(req, status)) {
1150 state->create_guid = &state->_create_guid;
1151 state->do_durable_reconnect = true;
1154 if (state->alsi != NULL) {
1155 if (state->alsi->data.length != 8) {
1156 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1159 state->allocation_size = BVAL(state->alsi->data.data, 0);
1162 if (state->twrp != NULL) {
1166 char *tmpname = state->fname;
1168 if (state->twrp->data.length != 8) {
1169 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1173 nttime = BVAL(state->twrp->data.data, 0);
1174 t = nt_time_to_unix(nttime);
1177 state->fname = talloc_asprintf(
1179 "%s\\@GMT-%04u.%02u.%02u-%02u.%02u.%02u",
1187 if (tevent_req_nomem(state->fname, req)) {
1190 TALLOC_FREE(tmpname);
1192 * Tell filename_create_ucf_flags() this
1195 smb1req->flags2 |= FLAGS2_REPARSE_PATH;
1198 if (state->qfid != NULL) {
1199 if (state->qfid->data.length != 0) {
1200 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1205 if (state->rqls != NULL) {
1206 ssize_t lease_len = -1;
1208 lease_len = smb2_lease_pull(state->rqls->data.data,
1209 state->rqls->data.length,
1211 if (lease_len == -1) {
1213 req, NT_STATUS_INVALID_PARAMETER);
1216 state->lease_ptr = &state->lease;
1218 if (DEBUGLEVEL >= 10) {
1219 DEBUG(10, ("Got lease request size %d\n",
1221 NDR_PRINT_DEBUG(smb2_lease, state->lease_ptr);
1224 if (!smb2_lease_key_valid(&state->lease.lease_key)) {
1225 state->lease_ptr = NULL;
1226 state->requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
1229 if ((smb2req->xconn->protocol < PROTOCOL_SMB3_00) &&
1230 (state->lease.lease_version != 1))
1232 DEBUG(10, ("v2 lease key only for SMB3\n"));
1233 state->lease_ptr = NULL;
1237 * Replay with a lease is only allowed if the
1238 * established open carries a lease with the
1241 if (state->replay_operation) {
1242 struct smb2_lease *op_ls =
1243 &state->op->compat->lease->lease;
1244 int op_oplock = state->op->compat->oplock_type;
1246 if (map_samba_oplock_levels_to_smb2(op_oplock)
1247 != SMB2_OPLOCK_LEVEL_LEASE)
1249 status = NT_STATUS_ACCESS_DENIED;
1250 (void)tevent_req_nterror(req, status);
1253 if (!smb2_lease_key_equal(&state->lease.lease_key,
1256 status = NT_STATUS_ACCESS_DENIED;
1257 (void)tevent_req_nterror(req, status);
1266 static void smbd_smb2_create_after_exec(struct tevent_req *req)
1268 struct smbd_smb2_create_state *state = tevent_req_data(
1269 req, struct smbd_smb2_create_state);
1270 struct smb_request *smb1req = state->smb1req;
1274 * here we have op == result->op
1277 DEBUG(10, ("smbd_smb2_create_send: "
1278 "response construction phase\n"));
1280 if (state->mxac != NULL) {
1281 NTTIME last_write_time;
1283 last_write_time = unix_timespec_to_nt_time(
1284 state->result->fsp_name->st.st_ex_mtime);
1285 if (last_write_time != state->max_access_time) {
1287 uint32_t max_access_granted;
1288 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1290 status = smbd_calculate_access_mask(smb1req->conn,
1291 state->result->fsp_name,
1293 SEC_FLAG_MAXIMUM_ALLOWED,
1294 &max_access_granted);
1296 SIVAL(p, 0, NT_STATUS_V(status));
1297 SIVAL(p, 4, max_access_granted);
1299 status = smb2_create_blob_add(
1300 state->out_context_blobs,
1301 state->out_context_blobs,
1302 SMB2_CREATE_TAG_MXAC,
1304 if (!NT_STATUS_IS_OK(status)) {
1305 tevent_req_nterror(req, status);
1306 tevent_req_post(req, state->ev);
1312 if (!state->replay_operation && state->durable_requested &&
1313 (fsp_lease_type(state->result) & SMB2_LEASE_HANDLE))
1315 status = SMB_VFS_DURABLE_COOKIE(
1318 &state->op->global->backend_cookie);
1319 if (!NT_STATUS_IS_OK(status)) {
1320 state->op->global->backend_cookie = data_blob_null;
1323 if (!state->replay_operation && state->op->global->backend_cookie.length > 0)
1325 state->update_open = true;
1327 state->op->global->durable = true;
1328 state->op->global->durable_timeout_msec = state->durable_timeout_msec;
1331 if (state->update_open) {
1332 state->op->global->create_guid = state->_create_guid;
1333 if (state->need_replay_cache) {
1334 state->op->flags |= SMBXSRV_OPEN_NEED_REPLAY_CACHE;
1337 status = smbXsrv_open_update(state->op);
1338 DEBUG(10, ("smb2_create_send: smbXsrv_open_update "
1340 nt_errstr(status)));
1341 if (!NT_STATUS_IS_OK(status)) {
1342 tevent_req_nterror(req, status);
1343 tevent_req_post(req, state->ev);
1348 if (state->dhnq != NULL && state->op->global->durable) {
1349 uint8_t p[8] = { 0, };
1350 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1352 status = smb2_create_blob_add(state->out_context_blobs,
1353 state->out_context_blobs,
1354 SMB2_CREATE_TAG_DHNQ,
1356 if (!NT_STATUS_IS_OK(status)) {
1357 tevent_req_nterror(req, status);
1358 tevent_req_post(req, state->ev);
1363 if (state->dh2q != NULL && state->op->global->durable &&
1365 * For replay operations, we return the dh2q blob
1366 * in the case of oplocks not based on the state of
1367 * the open, but on whether it could have been granted
1368 * for the request data. In the case of leases instead,
1369 * the state of the open is used...
1371 (!state->replay_operation ||
1372 state->in_oplock_level == SMB2_OPLOCK_LEVEL_BATCH ||
1373 state->in_oplock_level == SMB2_OPLOCK_LEVEL_LEASE))
1375 uint8_t p[8] = { 0, };
1376 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1377 uint32_t durable_v2_response_flags = 0;
1379 SIVAL(p, 0, state->op->global->durable_timeout_msec);
1380 SIVAL(p, 4, durable_v2_response_flags);
1382 status = smb2_create_blob_add(state->out_context_blobs,
1383 state->out_context_blobs,
1384 SMB2_CREATE_TAG_DH2Q,
1386 if (!NT_STATUS_IS_OK(status)) {
1387 tevent_req_nterror(req, status);
1388 tevent_req_post(req, state->ev);
1393 if (state->qfid != NULL) {
1395 uint64_t file_index = get_FileIndex(state->result->conn,
1396 &state->result->fsp_name->st);
1397 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1401 /* From conversations with Microsoft engineers at
1402 the MS plugfest. The first 8 bytes are the "volume index"
1403 == inode, the second 8 bytes are the "volume id",
1404 == dev. This will be updated in the SMB2 doc. */
1405 SBVAL(p, 0, file_index);
1406 SIVAL(p, 8, state->result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
1408 status = smb2_create_blob_add(state->out_context_blobs,
1409 state->out_context_blobs,
1410 SMB2_CREATE_TAG_QFID,
1412 if (!NT_STATUS_IS_OK(status)) {
1413 tevent_req_nterror(req, status);
1414 tevent_req_post(req, state->ev);
1419 if ((state->rqls != NULL) && (state->result->oplock_type == LEASE_OPLOCK)) {
1421 struct smb2_lease lease;
1424 lease = state->result->lease->lease;
1426 lease_len = sizeof(buf);
1427 if (lease.lease_version == 1) {
1431 if (!smb2_lease_push(&lease, buf, lease_len)) {
1433 req, NT_STATUS_INTERNAL_ERROR);
1434 tevent_req_post(req, state->ev);
1438 status = smb2_create_blob_add(
1439 state, state->out_context_blobs,
1440 SMB2_CREATE_TAG_RQLS,
1441 data_blob_const(buf, lease_len));
1442 if (!NT_STATUS_IS_OK(status)) {
1443 tevent_req_nterror(req, status);
1444 tevent_req_post(req, state->ev);
1452 static void smbd_smb2_create_finish(struct tevent_req *req)
1454 struct smbd_smb2_create_state *state = tevent_req_data(
1455 req, struct smbd_smb2_create_state);
1456 struct smbd_smb2_request *smb2req = state->smb2req;
1457 struct smb_request *smb1req = state->smb1req;
1458 files_struct *result = state->result;
1460 smb2req->compat_chain_fsp = smb1req->chain_fsp;
1462 if (state->replay_operation) {
1463 state->out_oplock_level = state->in_oplock_level;
1464 } else if (lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
1465 state->out_oplock_level = state->in_oplock_level;
1467 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
1470 if ((state->in_create_disposition == FILE_SUPERSEDE)
1471 && (state->info == FILE_WAS_OVERWRITTEN)) {
1472 state->out_create_action = FILE_WAS_SUPERSEDED;
1474 state->out_create_action = state->info;
1476 result->op->create_action = state->out_create_action;
1477 state->out_file_attributes = dos_mode(result->conn,
1480 state->out_creation_ts = get_create_timespec(smb1req->conn,
1481 result, result->fsp_name);
1482 state->out_last_access_ts = result->fsp_name->st.st_ex_atime;
1483 state->out_last_write_ts = result->fsp_name->st.st_ex_mtime;
1484 state->out_change_ts = get_change_timespec(smb1req->conn,
1485 result, result->fsp_name);
1487 if (lp_dos_filetime_resolution(SNUM(smb2req->tcon->compat))) {
1488 dos_filetime_timespec(&state->out_creation_ts);
1489 dos_filetime_timespec(&state->out_last_access_ts);
1490 dos_filetime_timespec(&state->out_last_write_ts);
1491 dos_filetime_timespec(&state->out_change_ts);
1494 state->out_allocation_size =
1495 SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
1496 &(result->fsp_name->st));
1497 state->out_end_of_file = result->fsp_name->st.st_ex_size;
1498 if (state->out_file_attributes == 0) {
1499 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
1501 state->out_file_id_persistent = result->op->global->open_persistent_id;
1502 state->out_file_id_volatile = result->op->global->open_volatile_id;
1504 DBG_DEBUG("%s - %s\n", fsp_str_dbg(result), fsp_fnum_dbg(result));
1506 tevent_req_done(req);
1507 tevent_req_post(req, state->ev);
1511 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
1512 TALLOC_CTX *mem_ctx,
1513 uint8_t *out_oplock_level,
1514 uint32_t *out_create_action,
1515 struct timespec *out_creation_ts,
1516 struct timespec *out_last_access_ts,
1517 struct timespec *out_last_write_ts,
1518 struct timespec *out_change_ts,
1519 uint64_t *out_allocation_size,
1520 uint64_t *out_end_of_file,
1521 uint32_t *out_file_attributes,
1522 uint64_t *out_file_id_persistent,
1523 uint64_t *out_file_id_volatile,
1524 struct smb2_create_blobs *out_context_blobs)
1527 struct smbd_smb2_create_state *state = tevent_req_data(req,
1528 struct smbd_smb2_create_state);
1530 if (tevent_req_is_nterror(req, &status)) {
1531 tevent_req_received(req);
1535 *out_oplock_level = state->out_oplock_level;
1536 *out_create_action = state->out_create_action;
1537 *out_creation_ts = state->out_creation_ts;
1538 *out_last_access_ts = state->out_last_access_ts;
1539 *out_last_write_ts = state->out_last_write_ts;
1540 *out_change_ts = state->out_change_ts;
1541 *out_allocation_size = state->out_allocation_size;
1542 *out_end_of_file = state->out_end_of_file;
1543 *out_file_attributes = state->out_file_attributes;
1544 *out_file_id_persistent = state->out_file_id_persistent;
1545 *out_file_id_volatile = state->out_file_id_volatile;
1546 *out_context_blobs = *(state->out_context_blobs);
1548 talloc_steal(mem_ctx, state->out_context_blobs->blobs);
1550 tevent_req_received(req);
1551 return NT_STATUS_OK;
1554 /*********************************************************
1555 Code for dealing with deferred opens.
1556 *********************************************************/
1558 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
1559 struct timeval *p_request_time,
1560 struct deferred_open_record **open_rec)
1562 struct smbd_smb2_create_state *state = NULL;
1563 struct tevent_req *req = NULL;
1568 req = smb2req->subreq;
1572 state = tevent_req_data(req, struct smbd_smb2_create_state);
1576 if (!state->open_was_deferred) {
1579 if (p_request_time) {
1580 *p_request_time = state->request_time;
1582 if (open_rec != NULL) {
1583 *open_rec = state->open_rec;
1588 /*********************************************************
1589 Re-process this call early - requested by message or
1591 *********************************************************/
1593 static struct smbd_smb2_request *find_open_smb2req(
1594 struct smbXsrv_connection *xconn, uint64_t mid)
1596 struct smbd_smb2_request *smb2req;
1598 for (smb2req = xconn->smb2.requests; smb2req; smb2req = smb2req->next) {
1599 uint64_t message_id;
1600 if (smb2req->subreq == NULL) {
1601 /* This message has been processed. */
1604 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1605 /* This message has been processed. */
1608 message_id = get_mid_from_smb2req(smb2req);
1609 if (message_id == mid) {
1616 bool open_was_deferred_smb2(struct smbXsrv_connection *xconn, uint64_t mid)
1618 struct smbd_smb2_create_state *state = NULL;
1619 struct smbd_smb2_request *smb2req;
1621 smb2req = find_open_smb2req(xconn, mid);
1624 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
1625 (unsigned long long)mid));
1628 if (!smb2req->subreq) {
1631 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1634 state = tevent_req_data(smb2req->subreq,
1635 struct smbd_smb2_create_state);
1639 /* It's not in progress if there's no timeout event. */
1640 if (!state->open_was_deferred) {
1644 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
1645 (unsigned long long)mid));
1650 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
1653 struct smbd_smb2_create_state *state = NULL;
1655 if (!smb2req->subreq) {
1658 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1661 state = tevent_req_data(smb2req->subreq,
1662 struct smbd_smb2_create_state);
1667 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
1669 (unsigned long long)mid ));
1671 state->open_was_deferred = false;
1672 /* Ensure we don't have any outstanding immediate event. */
1673 TALLOC_FREE(state->im);
1676 void remove_deferred_open_message_smb2(
1677 struct smbXsrv_connection *xconn, uint64_t mid)
1679 struct smbd_smb2_request *smb2req;
1681 smb2req = find_open_smb2req(xconn, mid);
1684 DEBUG(10,("remove_deferred_open_message_smb2: "
1685 "can't find mid %llu\n",
1686 (unsigned long long)mid ));
1689 remove_deferred_open_message_smb2_internal(smb2req, mid);
1692 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1693 struct tevent_immediate *im,
1696 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1697 struct smbd_smb2_request);
1698 uint64_t mid = get_mid_from_smb2req(smb2req);
1701 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1702 "re-dispatching mid %llu\n",
1703 (unsigned long long)mid ));
1705 status = smbd_smb2_request_dispatch(smb2req);
1706 if (!NT_STATUS_IS_OK(status)) {
1707 smbd_server_connection_terminate(smb2req->xconn,
1713 bool schedule_deferred_open_message_smb2(
1714 struct smbXsrv_connection *xconn, uint64_t mid)
1716 struct smbd_smb2_create_state *state = NULL;
1717 struct smbd_smb2_request *smb2req;
1719 smb2req = find_open_smb2req(xconn, mid);
1722 DEBUG(10,("schedule_deferred_open_message_smb2: "
1723 "can't find mid %llu\n",
1724 (unsigned long long)mid ));
1727 if (!smb2req->subreq) {
1730 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1733 state = tevent_req_data(smb2req->subreq,
1734 struct smbd_smb2_create_state);
1739 /* Ensure we don't have any outstanding immediate event. */
1740 TALLOC_FREE(state->im);
1743 * This is subtle. We must null out the callback
1744 * before rescheduling, else the first call to
1745 * tevent_req_nterror() causes the _receive()
1746 * function to be called, this causing tevent_req_post()
1749 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1751 state->im = tevent_create_immediate(smb2req);
1753 smbd_server_connection_terminate(smb2req->xconn,
1754 nt_errstr(NT_STATUS_NO_MEMORY));
1758 DEBUG(10,("schedule_deferred_open_message_smb2: "
1759 "re-processing mid %llu\n",
1760 (unsigned long long)mid ));
1762 tevent_schedule_immediate(state->im,
1763 smb2req->sconn->ev_ctx,
1764 smbd_smb2_create_request_dispatch_immediate,
1770 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1772 struct smbd_smb2_request *smb2req = NULL;
1773 struct smbd_smb2_create_state *state = tevent_req_data(req,
1774 struct smbd_smb2_create_state);
1781 if (!state->smb2req) {
1785 smb2req = state->smb2req;
1786 mid = get_mid_from_smb2req(smb2req);
1788 if (is_deferred_open_async(state->open_rec)) {
1789 /* Can't cancel an async create. */
1793 remove_deferred_open_message_smb2_internal(smb2req, mid);
1795 tevent_req_defer_callback(req, smb2req->sconn->ev_ctx);
1796 tevent_req_nterror(req, NT_STATUS_CANCELLED);
1800 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1801 struct timeval request_time,
1802 struct timeval timeout,
1804 struct deferred_open_record *open_rec)
1806 struct tevent_req *req = NULL;
1807 struct smbd_smb2_create_state *state = NULL;
1808 struct timeval end_time;
1813 req = smb2req->subreq;
1817 state = tevent_req_data(req, struct smbd_smb2_create_state);
1822 state->request_time = request_time;
1823 state->open_rec = talloc_move(state, &open_rec);
1825 /* Re-schedule us to retry on timer expiry. */
1826 end_time = timeval_sum(&request_time, &timeout);
1828 DEBUG(10,("push_deferred_open_message_smb2: "
1830 timeval_string(talloc_tos(),
1834 state->open_was_deferred = true;
1836 /* allow this request to be canceled */
1837 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);