s3:vfs: add SMB_VFS_READDIR_ATTR()
[Samba.git] / source3 / smbd / smb2_create.c
blobe0eef43e5cffa4b2cc9391a69354c8dfaab663a0
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
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/>.
22 #include "includes.h"
23 #include "printing.h"
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"
30 #include "messages.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:
36 return NO_OPLOCK;
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:
42 return BATCH_OPLOCK;
43 case SMB2_OPLOCK_LEVEL_LEASE:
44 return LEASE_OPLOCK;
45 default:
46 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
47 "unknown level %u\n",
48 (unsigned int)in_oplock_level));
49 return NO_OPLOCK;
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;
63 } else {
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,
78 const char *in_name,
79 struct smb2_create_blobs in_context_blobs);
80 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
81 TALLOC_CTX *mem_ctx,
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;
120 uint32_t dyn_offset;
121 NTSTATUS status;
122 bool ok;
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
148 * overlap
151 dyn_offset = SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(smb2req);
153 if (in_name_offset == 0 && in_name_length == 0) {
154 /* This is ok */
155 name_offset = 0;
156 } else if (in_name_offset < dyn_offset) {
157 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
158 } else {
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) {
178 /* This is ok */
179 context_offset = 0;
180 } else if (in_context_offset < dyn_offset) {
181 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
182 } else {
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 +
197 context_offset;
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,
205 in_name_buffer.data,
206 in_name_buffer.length,
207 &in_name_string,
208 &in_name_string_size);
209 if (!ok) {
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,
229 smb2req,
230 in_oplock_level,
231 in_impersonation_level,
232 in_desired_access,
233 in_file_attributes,
234 in_share_access,
235 in_create_disposition,
236 in_create_options,
237 in_name_string,
238 in_context_blobs);
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);
258 DATA_BLOB outbody;
259 DATA_BLOB outdyn;
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;
275 NTSTATUS status;
276 NTSTATUS error; /* transport error */
278 status = smbd_smb2_create_recv(tsubreq,
279 smb2req,
280 &out_oplock_level,
281 &out_create_action,
282 &out_creation_ts,
283 &out_last_access_ts,
284 &out_last_write_ts,
285 &out_change_ts,
286 &out_allocation_size,
287 &out_end_of_file,
288 &out_file_attributes,
289 &out_file_id_persistent,
290 &out_file_id_volatile,
291 &out_context_blobs);
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,
296 nt_errstr(error));
297 return;
299 return;
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,
307 nt_errstr(error));
308 return;
310 return;
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,
322 nt_errstr(error));
323 return;
325 return;
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,
367 nt_errstr(error));
368 return;
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(
378 const char *requested_filename, const struct files_struct *fsp,
379 const struct smb2_lease *lease_ptr)
381 struct smb_filename *smb_fname = NULL;
382 NTSTATUS status;
384 if (lease_ptr == NULL) {
385 if (fsp->oplock_type != LEASE_OPLOCK) {
386 return NT_STATUS_OK;
388 DEBUG(10, ("Reopened file has lease, but no lease "
389 "requested\n"));
390 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
393 if (fsp->oplock_type != LEASE_OPLOCK) {
394 DEBUG(10, ("Lease requested, but reopened file has no "
395 "lease\n"));
396 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
399 if (!smb2_lease_key_equal(&lease_ptr->lease_key,
400 &fsp->lease->lease.lease_key)) {
401 DEBUG(10, ("Different lease key requested than found "
402 "in reopened file\n"));
403 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
406 status = filename_convert(talloc_tos(), fsp->conn, false,
407 requested_filename, UCF_PREP_CREATEFILE,
408 NULL, &smb_fname);
409 if (!NT_STATUS_IS_OK(status)) {
410 DEBUG(10, ("filename_convert returned %s\n",
411 nt_errstr(status)));
412 return status;
415 if (!strequal(fsp->fsp_name->base_name, smb_fname->base_name)) {
416 DEBUG(10, ("Lease requested for file %s, reopened file "
417 "is named %s\n", smb_fname->base_name,
418 fsp->fsp_name->base_name));
419 TALLOC_FREE(smb_fname);
420 return NT_STATUS_INVALID_PARAMETER;
423 TALLOC_FREE(smb_fname);
425 return NT_STATUS_OK;
428 struct smbd_smb2_create_state {
429 struct smbd_smb2_request *smb2req;
430 struct smb_request *smb1req;
431 bool open_was_deferred;
432 struct tevent_timer *te;
433 struct tevent_immediate *im;
434 struct timeval request_time;
435 struct file_id id;
436 struct deferred_open_record *open_rec;
437 uint8_t out_oplock_level;
438 uint32_t out_create_action;
439 struct timespec out_creation_ts;
440 struct timespec out_last_access_ts;
441 struct timespec out_last_write_ts;
442 struct timespec out_change_ts;
443 uint64_t out_allocation_size;
444 uint64_t out_end_of_file;
445 uint32_t out_file_attributes;
446 uint64_t out_file_id_persistent;
447 uint64_t out_file_id_volatile;
448 struct smb2_create_blobs out_context_blobs;
451 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
452 struct tevent_context *ev,
453 struct smbd_smb2_request *smb2req,
454 uint8_t in_oplock_level,
455 uint32_t in_impersonation_level,
456 uint32_t in_desired_access,
457 uint32_t in_file_attributes,
458 uint32_t in_share_access,
459 uint32_t in_create_disposition,
460 uint32_t in_create_options,
461 const char *in_name,
462 struct smb2_create_blobs in_context_blobs)
464 struct tevent_req *req = NULL;
465 struct smbd_smb2_create_state *state = NULL;
466 NTSTATUS status;
467 struct smb_request *smb1req = NULL;
468 files_struct *result = NULL;
469 int info;
470 struct smb2_create_blobs out_context_blobs;
471 int requested_oplock_level;
472 struct smb2_create_blob *dhnc = NULL;
473 struct smb2_create_blob *dh2c = NULL;
474 struct smb2_create_blob *dhnq = NULL;
475 struct smb2_create_blob *dh2q = NULL;
476 struct smb2_create_blob *rqls = NULL;
477 struct smbXsrv_open *op = NULL;
479 ZERO_STRUCT(out_context_blobs);
481 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
482 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
483 } else {
484 requested_oplock_level = in_oplock_level;
488 if (smb2req->subreq == NULL) {
489 /* New create call. */
490 req = tevent_req_create(mem_ctx, &state,
491 struct smbd_smb2_create_state);
492 if (req == NULL) {
493 return NULL;
495 state->smb2req = smb2req;
497 smb1req = smbd_smb2_fake_smb_request(smb2req);
498 if (tevent_req_nomem(smb1req, req)) {
499 return tevent_req_post(req, ev);
501 state->smb1req = smb1req;
502 smb2req->subreq = req;
503 DEBUG(10,("smbd_smb2_create: name[%s]\n",
504 in_name));
505 } else {
506 /* Re-entrant create call. */
507 req = smb2req->subreq;
508 state = tevent_req_data(req,
509 struct smbd_smb2_create_state);
510 smb1req = state->smb1req;
511 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
512 in_name ));
515 dhnq = smb2_create_blob_find(&in_context_blobs,
516 SMB2_CREATE_TAG_DHNQ);
517 dhnc = smb2_create_blob_find(&in_context_blobs,
518 SMB2_CREATE_TAG_DHNC);
519 dh2q = smb2_create_blob_find(&in_context_blobs,
520 SMB2_CREATE_TAG_DH2Q);
521 dh2c = smb2_create_blob_find(&in_context_blobs,
522 SMB2_CREATE_TAG_DH2C);
523 if (smb2req->xconn->smb2.server.capabilities & SMB2_CAP_LEASING) {
524 rqls = smb2_create_blob_find(&in_context_blobs,
525 SMB2_CREATE_TAG_RQLS);
528 if ((dhnc && dh2c) || (dhnc && dh2q) || (dh2c && dhnq) ||
529 (dh2q && dh2c))
531 /* not both are allowed at the same time */
532 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
533 return tevent_req_post(req, ev);
536 if (dhnc) {
537 uint32_t num_blobs_allowed;
539 if (dhnc->data.length != 16) {
540 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
541 return tevent_req_post(req, ev);
545 * According to MS-SMB2: 3.3.5.9.7, "Handling the
546 * SMB2_CREATE_DURABLE_HANDLE_RECONNECT Create Context",
547 * we should ignore an additional dhnq blob, but fail
548 * the request (with status OBJECT_NAME_NOT_FOUND) if
549 * any other extra create blob has been provided.
551 * (Note that the cases of an additional dh2q or dh2c blob
552 * which require a different error code, have been treated
553 * above.)
555 * TODO:
556 * This is only true for the oplock case:
557 * For leases, lease request is required additionally.
560 if (dhnq) {
561 num_blobs_allowed = 2;
562 } else {
563 num_blobs_allowed = 1;
566 if (rqls != NULL) {
567 num_blobs_allowed += 1;
570 if (in_context_blobs.num_blobs != num_blobs_allowed) {
571 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
572 return tevent_req_post(req, ev);
576 if (dh2c) {
577 uint32_t num_blobs_allowed;
579 if (dh2c->data.length != 36) {
580 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
581 return tevent_req_post(req, ev);
585 * According to MS-SMB2: 3.3.5.9.12, "Handling the
586 * SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 Create Context",
587 * we should fail the request with status
588 * OBJECT_NAME_NOT_FOUND if any other create blob has been
589 * provided.
591 * (Note that the cases of an additional dhnq, dhnc or dh2q
592 * blob which require a different error code, have been
593 * treated above.)
595 * TODO:
596 * This is only true for the oplock case:
597 * For leases, lease request is required additionally!
600 num_blobs_allowed = 1;
602 if (rqls != NULL) {
603 num_blobs_allowed += 1;
606 if (in_context_blobs.num_blobs != num_blobs_allowed) {
607 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
608 return tevent_req_post(req, ev);
612 if (IS_IPC(smb1req->conn)) {
613 const char *pipe_name = in_name;
615 if (dhnc || dh2c) {
616 /* durable handles are not supported on IPC$ */
617 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
618 return tevent_req_post(req, ev);
621 if (!lp_nt_pipe_support()) {
622 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
623 return tevent_req_post(req, ev);
626 status = open_np_file(smb1req, pipe_name, &result);
627 if (!NT_STATUS_IS_OK(status)) {
628 tevent_req_nterror(req, status);
629 return tevent_req_post(req, ev);
631 info = FILE_WAS_OPENED;
632 } else if (CAN_PRINT(smb1req->conn)) {
633 if (dhnc || dh2c) {
634 /* durable handles are not supported on printers */
635 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
636 return tevent_req_post(req, ev);
639 status = file_new(smb1req, smb1req->conn, &result);
640 if(!NT_STATUS_IS_OK(status)) {
641 tevent_req_nterror(req, status);
642 return tevent_req_post(req, ev);
645 status = print_spool_open(result, in_name,
646 smb1req->vuid);
647 if (!NT_STATUS_IS_OK(status)) {
648 file_free(smb1req, result);
649 tevent_req_nterror(req, status);
650 return tevent_req_post(req, ev);
652 info = FILE_WAS_CREATED;
653 } else {
654 char *fname;
655 struct smb2_create_blob *exta = NULL;
656 struct ea_list *ea_list = NULL;
657 struct smb2_create_blob *mxac = NULL;
658 NTTIME max_access_time = 0;
659 struct smb2_create_blob *secd = NULL;
660 struct security_descriptor *sec_desc = NULL;
661 struct smb2_create_blob *alsi = NULL;
662 uint64_t allocation_size = 0;
663 struct smb2_create_blob *twrp = NULL;
664 struct smb2_create_blob *qfid = NULL;
665 struct GUID _create_guid = GUID_zero();
666 struct GUID *create_guid = NULL;
667 bool update_open = false;
668 bool durable_requested = false;
669 uint32_t durable_timeout_msec = 0;
670 bool do_durable_reconnect = false;
671 uint64_t persistent_id = 0;
672 struct smb2_lease lease;
673 struct smb2_lease *lease_ptr = NULL;
674 ssize_t lease_len = -1;
676 exta = smb2_create_blob_find(&in_context_blobs,
677 SMB2_CREATE_TAG_EXTA);
678 mxac = smb2_create_blob_find(&in_context_blobs,
679 SMB2_CREATE_TAG_MXAC);
680 secd = smb2_create_blob_find(&in_context_blobs,
681 SMB2_CREATE_TAG_SECD);
682 alsi = smb2_create_blob_find(&in_context_blobs,
683 SMB2_CREATE_TAG_ALSI);
684 twrp = smb2_create_blob_find(&in_context_blobs,
685 SMB2_CREATE_TAG_TWRP);
686 qfid = smb2_create_blob_find(&in_context_blobs,
687 SMB2_CREATE_TAG_QFID);
689 fname = talloc_strdup(state, in_name);
690 if (tevent_req_nomem(fname, req)) {
691 return tevent_req_post(req, ev);
694 if (exta) {
695 ea_list = read_nttrans_ea_list(mem_ctx,
696 (const char *)exta->data.data, exta->data.length);
697 if (!ea_list) {
698 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
699 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
700 return tevent_req_post(req, ev);
703 if (ea_list_has_invalid_name(ea_list)) {
704 tevent_req_nterror(req, STATUS_INVALID_EA_NAME);
705 return tevent_req_post(req, ev);
709 if (mxac) {
710 if (mxac->data.length == 0) {
711 max_access_time = 0;
712 } else if (mxac->data.length == 8) {
713 max_access_time = BVAL(mxac->data.data, 0);
714 } else {
715 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
716 return tevent_req_post(req, ev);
720 if (secd) {
721 enum ndr_err_code ndr_err;
723 sec_desc = talloc_zero(state, struct security_descriptor);
724 if (tevent_req_nomem(sec_desc, req)) {
725 return tevent_req_post(req, ev);
728 ndr_err = ndr_pull_struct_blob(&secd->data,
729 sec_desc, sec_desc,
730 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
731 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
732 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
733 ndr_errstr(ndr_err)));
734 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
735 return tevent_req_post(req, ev);
739 if (dhnq) {
740 if (dhnq->data.length != 16) {
741 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
742 return tevent_req_post(req, ev);
745 if (dh2q) {
746 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
747 return tevent_req_post(req, ev);
751 * durable handle request is processed below.
753 durable_requested = true;
755 * Set the timeout to 16 mins.
757 * TODO: test this against Windows 2012
758 * as the default for durable v2 is 1 min.
760 durable_timeout_msec = (16*60*1000);
763 if (dh2q) {
764 const uint8_t *p = dh2q->data.data;
765 uint32_t durable_v2_timeout = 0;
766 DATA_BLOB create_guid_blob;
768 if (dh2q->data.length != 32) {
769 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
770 return tevent_req_post(req, ev);
773 if (dhnq) {
774 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
775 return tevent_req_post(req, ev);
778 durable_v2_timeout = IVAL(p, 0);
779 create_guid_blob = data_blob_const(p + 16, 16);
781 status = GUID_from_ndr_blob(&create_guid_blob,
782 &_create_guid);
783 if (tevent_req_nterror(req, status)) {
784 return tevent_req_post(req, ev);
786 create_guid = &_create_guid;
788 * we need to store the create_guid later
790 update_open = true;
793 * durable handle v2 request processed below
795 durable_requested = true;
796 durable_timeout_msec = durable_v2_timeout;
797 if (durable_timeout_msec == 0) {
799 * Set the timeout to 1 min as default.
801 * This matches Windows 2012.
803 durable_timeout_msec = (60*1000);
807 if (dhnc) {
808 persistent_id = BVAL(dhnc->data.data, 0);
810 do_durable_reconnect = true;
813 if (dh2c) {
814 const uint8_t *p = dh2c->data.data;
815 DATA_BLOB create_guid_blob;
817 persistent_id = BVAL(p, 0);
818 create_guid_blob = data_blob_const(p + 16, 16);
820 status = GUID_from_ndr_blob(&create_guid_blob,
821 &_create_guid);
822 if (tevent_req_nterror(req, status)) {
823 return tevent_req_post(req, ev);
825 create_guid = &_create_guid;
827 do_durable_reconnect = true;
830 if (alsi) {
831 if (alsi->data.length != 8) {
832 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
833 return tevent_req_post(req, ev);
835 allocation_size = BVAL(alsi->data.data, 0);
838 if (twrp) {
839 NTTIME nttime;
840 time_t t;
841 struct tm *tm;
843 if (twrp->data.length != 8) {
844 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
845 return tevent_req_post(req, ev);
848 nttime = BVAL(twrp->data.data, 0);
849 t = nt_time_to_unix(nttime);
850 tm = gmtime(&t);
852 TALLOC_FREE(fname);
853 fname = talloc_asprintf(state,
854 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
855 tm->tm_year + 1900,
856 tm->tm_mon + 1,
857 tm->tm_mday,
858 tm->tm_hour,
859 tm->tm_min,
860 tm->tm_sec,
861 in_name);
862 if (tevent_req_nomem(fname, req)) {
863 return tevent_req_post(req, ev);
867 if (qfid) {
868 if (qfid->data.length != 0) {
869 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
870 return tevent_req_post(req, ev);
874 if (rqls) {
875 lease_len = smb2_lease_pull(
876 rqls->data.data, rqls->data.length, &lease);
877 if (lease_len == -1) {
878 tevent_req_nterror(
879 req, NT_STATUS_INVALID_PARAMETER);
880 return tevent_req_post(req, ev);
882 lease_ptr = &lease;
884 if (DEBUGLEVEL >= 10) {
885 DEBUG(10, ("Got lease request size %d\n",
886 (int)lease_len));
887 NDR_PRINT_DEBUG(smb2_lease, lease_ptr);
890 if (!smb2_lease_key_valid(&lease.lease_key)) {
891 lease_ptr = NULL;
892 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
895 if ((smb2req->xconn->protocol < PROTOCOL_SMB3_00) &&
896 (lease.lease_version != 1)) {
897 DEBUG(10, ("v2 lease key only for SMB3\n"));
898 lease_ptr = NULL;
902 /* these are ignored for SMB2 */
903 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
904 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
906 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
908 DEBUG(10, ("smbd_smb2_create_send: open execution phase\n"));
911 * For the backend file open procedure, there are
912 * two possible modes: durable_reconnect or not.
914 if (do_durable_reconnect) {
915 DATA_BLOB new_cookie = data_blob_null;
916 NTTIME now = timeval_to_nttime(&smb2req->request_time);
918 status = smb2srv_open_recreate(smb2req->xconn,
919 smb1req->conn->session_info,
920 persistent_id, create_guid,
921 now, &op);
922 if (!NT_STATUS_IS_OK(status)) {
923 DEBUG(3, ("smbd_smb2_create_send: "
924 "smb2srv_open_recreate failed: %s\n",
925 nt_errstr(status)));
926 tevent_req_nterror(req, status);
927 return tevent_req_post(req, ev);
930 DEBUG(10, ("smb2_create_send: %s to recreate the "
931 "smb2srv_open struct for a durable handle.\n",
932 op->global->durable ? "succeded" : "failed"));
934 if (!op->global->durable) {
935 talloc_free(op);
936 tevent_req_nterror(req,
937 NT_STATUS_OBJECT_NAME_NOT_FOUND);
938 return tevent_req_post(req, ev);
941 status = SMB_VFS_DURABLE_RECONNECT(smb1req->conn,
942 smb1req,
943 op, /* smbXsrv_open input */
944 op->global->backend_cookie,
945 op, /* TALLOC_CTX */
946 &result, &new_cookie);
947 if (!NT_STATUS_IS_OK(status)) {
948 NTSTATUS return_status;
950 return_status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
952 DEBUG(3, ("smbd_smb2_create_send: "
953 "durable_reconnect failed: %s => %s\n",
954 nt_errstr(status),
955 nt_errstr(return_status)));
957 tevent_req_nterror(req, return_status);
958 return tevent_req_post(req, ev);
961 DEBUG(10, ("result->oplock_type=%u, lease_ptr==%p\n",
962 (unsigned)result->oplock_type, lease_ptr));
964 status = smbd_smb2_create_durable_lease_check(
965 fname, result, lease_ptr);
966 if (!NT_STATUS_IS_OK(status)) {
967 close_file(smb1req, result, SHUTDOWN_CLOSE);
968 tevent_req_nterror(req, status);
969 return tevent_req_post(req, ev);
972 data_blob_free(&op->global->backend_cookie);
973 op->global->backend_cookie = new_cookie;
975 op->status = NT_STATUS_OK;
976 op->global->disconnect_time = 0;
978 /* save the timout for later update */
979 durable_timeout_msec = op->global->durable_timeout_msec;
981 update_open = true;
983 info = FILE_WAS_OPENED;
984 } else {
985 struct smb_filename *smb_fname = NULL;
987 if (requested_oplock_level == SMB2_OPLOCK_LEVEL_LEASE) {
988 if (lease_ptr == NULL) {
989 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
991 } else {
992 lease_ptr = NULL;
996 * For a DFS path the function parse_dfs_path()
997 * will do the path processing.
1000 if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
1001 /* convert '\\' into '/' */
1002 status = check_path_syntax(fname);
1003 if (!NT_STATUS_IS_OK(status)) {
1004 tevent_req_nterror(req, status);
1005 return tevent_req_post(req, ev);
1009 status = filename_convert(req,
1010 smb1req->conn,
1011 smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
1012 fname,
1013 UCF_PREP_CREATEFILE,
1014 NULL, /* ppath_contains_wcards */
1015 &smb_fname);
1016 if (!NT_STATUS_IS_OK(status)) {
1017 tevent_req_nterror(req, status);
1018 return tevent_req_post(req, ev);
1022 * MS-SMB2: 2.2.13 SMB2 CREATE Request
1023 * ImpersonationLevel ... MUST contain one of the
1024 * following values. The server MUST validate this
1025 * field, but otherwise ignore it.
1027 * NB. The source4/torture/smb2/durable_open.c test
1028 * shows this check is only done on real opens, not
1029 * on durable handle-reopens.
1032 if (in_impersonation_level >
1033 SMB2_IMPERSONATION_DELEGATE) {
1034 tevent_req_nterror(req,
1035 NT_STATUS_BAD_IMPERSONATION_LEVEL);
1036 return tevent_req_post(req, ev);
1040 * We know we're going to do a local open, so now
1041 * we must be protocol strict. JRA.
1043 * MS-SMB2: 3.3.5.9 - Receiving an SMB2 CREATE Request
1044 * If the file name length is greater than zero and the
1045 * first character is a path separator character, the
1046 * server MUST fail the request with
1047 * STATUS_INVALID_PARAMETER.
1049 if (in_name[0] == '\\' || in_name[0] == '/') {
1050 tevent_req_nterror(req,
1051 NT_STATUS_INVALID_PARAMETER);
1052 return tevent_req_post(req, ev);
1055 status = SMB_VFS_CREATE_FILE(smb1req->conn,
1056 smb1req,
1057 0, /* root_dir_fid */
1058 smb_fname,
1059 in_desired_access,
1060 in_share_access,
1061 in_create_disposition,
1062 in_create_options,
1063 in_file_attributes,
1064 map_smb2_oplock_levels_to_samba(requested_oplock_level),
1065 lease_ptr,
1066 allocation_size,
1067 0, /* private_flags */
1068 sec_desc,
1069 ea_list,
1070 &result,
1071 &info);
1072 if (!NT_STATUS_IS_OK(status)) {
1073 if (open_was_deferred(smb1req->xconn, smb1req->mid)) {
1074 SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(smb2req->profile);
1075 return req;
1077 tevent_req_nterror(req, status);
1078 return tevent_req_post(req, ev);
1080 op = result->op;
1084 * here we have op == result->op
1087 DEBUG(10, ("smbd_smb2_create_send: "
1088 "response construction phase\n"));
1090 if (mxac) {
1091 NTTIME last_write_time;
1093 last_write_time = unix_timespec_to_nt_time(
1094 result->fsp_name->st.st_ex_mtime);
1095 if (last_write_time != max_access_time) {
1096 uint8_t p[8];
1097 uint32_t max_access_granted;
1098 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1100 status = smbd_calculate_access_mask(smb1req->conn,
1101 result->fsp_name,
1102 false,
1103 SEC_FLAG_MAXIMUM_ALLOWED,
1104 &max_access_granted);
1106 SIVAL(p, 0, NT_STATUS_V(status));
1107 SIVAL(p, 4, max_access_granted);
1109 status = smb2_create_blob_add(state,
1110 &out_context_blobs,
1111 SMB2_CREATE_TAG_MXAC,
1112 blob);
1113 if (!NT_STATUS_IS_OK(status)) {
1114 tevent_req_nterror(req, status);
1115 return tevent_req_post(req, ev);
1120 if (durable_requested &&
1121 (fsp_lease_type(result) & SMB2_LEASE_HANDLE))
1123 status = SMB_VFS_DURABLE_COOKIE(result,
1125 &op->global->backend_cookie);
1126 if (!NT_STATUS_IS_OK(status)) {
1127 op->global->backend_cookie = data_blob_null;
1130 if (op->global->backend_cookie.length > 0) {
1131 update_open = true;
1133 op->global->durable = true;
1134 op->global->durable_timeout_msec = durable_timeout_msec;
1137 if (update_open) {
1138 op->global->create_guid = _create_guid;
1140 status = smbXsrv_open_update(op);
1141 DEBUG(10, ("smb2_create_send: smbXsrv_open_update "
1142 "returned %s\n",
1143 nt_errstr(status)));
1144 if (!NT_STATUS_IS_OK(status)) {
1145 tevent_req_nterror(req, status);
1146 return tevent_req_post(req, ev);
1150 if (dhnq && op->global->durable) {
1151 uint8_t p[8] = { 0, };
1152 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1154 status = smb2_create_blob_add(state,
1155 &out_context_blobs,
1156 SMB2_CREATE_TAG_DHNQ,
1157 blob);
1158 if (!NT_STATUS_IS_OK(status)) {
1159 tevent_req_nterror(req, status);
1160 return tevent_req_post(req, ev);
1164 if (dh2q && op->global->durable) {
1165 uint8_t p[8] = { 0, };
1166 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1167 uint32_t durable_v2_response_flags = 0;
1169 SIVAL(p, 0, op->global->durable_timeout_msec);
1170 SIVAL(p, 4, durable_v2_response_flags);
1172 status = smb2_create_blob_add(state, &out_context_blobs,
1173 SMB2_CREATE_TAG_DH2Q,
1174 blob);
1175 if (!NT_STATUS_IS_OK(status)) {
1176 tevent_req_nterror(req, status);
1177 return tevent_req_post(req, ev);
1181 if (qfid) {
1182 uint8_t p[32];
1183 uint64_t file_index = get_FileIndex(result->conn,
1184 &result->fsp_name->st);
1185 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1187 ZERO_STRUCT(p);
1189 /* From conversations with Microsoft engineers at
1190 the MS plugfest. The first 8 bytes are the "volume index"
1191 == inode, the second 8 bytes are the "volume id",
1192 == dev. This will be updated in the SMB2 doc. */
1193 SBVAL(p, 0, file_index);
1194 SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
1196 status = smb2_create_blob_add(state, &out_context_blobs,
1197 SMB2_CREATE_TAG_QFID,
1198 blob);
1199 if (!NT_STATUS_IS_OK(status)) {
1200 tevent_req_nterror(req, status);
1201 return tevent_req_post(req, ev);
1205 if ((rqls != NULL) && (result->oplock_type == LEASE_OPLOCK)) {
1206 uint8_t buf[52];
1208 lease = result->lease->lease;
1210 lease_len = sizeof(buf);
1211 if (lease.lease_version == 1) {
1212 lease_len = 32;
1215 if (!smb2_lease_push(&lease, buf, lease_len)) {
1216 tevent_req_nterror(
1217 req, NT_STATUS_INTERNAL_ERROR);
1218 return tevent_req_post(req, ev);
1221 status = smb2_create_blob_add(
1222 state, &out_context_blobs,
1223 SMB2_CREATE_TAG_RQLS,
1224 data_blob_const(buf, lease_len));
1225 if (!NT_STATUS_IS_OK(status)) {
1226 tevent_req_nterror(req, status);
1227 return tevent_req_post(req, ev);
1232 smb2req->compat_chain_fsp = smb1req->chain_fsp;
1234 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
1235 state->out_oplock_level = in_oplock_level;
1236 } else {
1237 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
1240 if ((in_create_disposition == FILE_SUPERSEDE)
1241 && (info == FILE_WAS_OVERWRITTEN)) {
1242 state->out_create_action = FILE_WAS_SUPERSEDED;
1243 } else {
1244 state->out_create_action = info;
1246 state->out_file_attributes = dos_mode(result->conn,
1247 result->fsp_name);
1249 state->out_creation_ts = get_create_timespec(smb1req->conn,
1250 result, result->fsp_name);
1251 state->out_last_access_ts = result->fsp_name->st.st_ex_atime;
1252 state->out_last_write_ts = result->fsp_name->st.st_ex_mtime;
1253 state->out_change_ts = get_change_timespec(smb1req->conn,
1254 result, result->fsp_name);
1255 state->out_allocation_size =
1256 SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
1257 &(result->fsp_name->st));
1258 state->out_end_of_file = result->fsp_name->st.st_ex_size;
1259 if (state->out_file_attributes == 0) {
1260 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
1262 state->out_file_id_persistent = result->op->global->open_persistent_id;
1263 state->out_file_id_volatile = result->op->global->open_volatile_id;
1264 state->out_context_blobs = out_context_blobs;
1266 DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
1267 fsp_str_dbg(result), fsp_fnum_dbg(result)));
1269 tevent_req_done(req);
1270 return tevent_req_post(req, ev);
1273 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
1274 TALLOC_CTX *mem_ctx,
1275 uint8_t *out_oplock_level,
1276 uint32_t *out_create_action,
1277 struct timespec *out_creation_ts,
1278 struct timespec *out_last_access_ts,
1279 struct timespec *out_last_write_ts,
1280 struct timespec *out_change_ts,
1281 uint64_t *out_allocation_size,
1282 uint64_t *out_end_of_file,
1283 uint32_t *out_file_attributes,
1284 uint64_t *out_file_id_persistent,
1285 uint64_t *out_file_id_volatile,
1286 struct smb2_create_blobs *out_context_blobs)
1288 NTSTATUS status;
1289 struct smbd_smb2_create_state *state = tevent_req_data(req,
1290 struct smbd_smb2_create_state);
1292 if (tevent_req_is_nterror(req, &status)) {
1293 tevent_req_received(req);
1294 return status;
1297 *out_oplock_level = state->out_oplock_level;
1298 *out_create_action = state->out_create_action;
1299 *out_creation_ts = state->out_creation_ts;
1300 *out_last_access_ts = state->out_last_access_ts;
1301 *out_last_write_ts = state->out_last_write_ts;
1302 *out_change_ts = state->out_change_ts;
1303 *out_allocation_size = state->out_allocation_size;
1304 *out_end_of_file = state->out_end_of_file;
1305 *out_file_attributes = state->out_file_attributes;
1306 *out_file_id_persistent = state->out_file_id_persistent;
1307 *out_file_id_volatile = state->out_file_id_volatile;
1308 *out_context_blobs = state->out_context_blobs;
1310 talloc_steal(mem_ctx, state->out_context_blobs.blobs);
1312 tevent_req_received(req);
1313 return NT_STATUS_OK;
1316 /*********************************************************
1317 Code for dealing with deferred opens.
1318 *********************************************************/
1320 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
1321 struct timeval *p_request_time,
1322 struct deferred_open_record **open_rec)
1324 struct smbd_smb2_create_state *state = NULL;
1325 struct tevent_req *req = NULL;
1327 if (!smb2req) {
1328 return false;
1330 req = smb2req->subreq;
1331 if (!req) {
1332 return false;
1334 state = tevent_req_data(req, struct smbd_smb2_create_state);
1335 if (!state) {
1336 return false;
1338 if (!state->open_was_deferred) {
1339 return false;
1341 if (p_request_time) {
1342 *p_request_time = state->request_time;
1344 if (open_rec != NULL) {
1345 *open_rec = state->open_rec;
1347 return true;
1350 /*********************************************************
1351 Re-process this call early - requested by message or
1352 close.
1353 *********************************************************/
1355 static struct smbd_smb2_request *find_open_smb2req(
1356 struct smbXsrv_connection *xconn, uint64_t mid)
1358 struct smbd_smb2_request *smb2req;
1360 for (smb2req = xconn->smb2.requests; smb2req; smb2req = smb2req->next) {
1361 uint64_t message_id;
1362 if (smb2req->subreq == NULL) {
1363 /* This message has been processed. */
1364 continue;
1366 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1367 /* This message has been processed. */
1368 continue;
1370 message_id = get_mid_from_smb2req(smb2req);
1371 if (message_id == mid) {
1372 return smb2req;
1375 return NULL;
1378 bool open_was_deferred_smb2(struct smbXsrv_connection *xconn, uint64_t mid)
1380 struct smbd_smb2_create_state *state = NULL;
1381 struct smbd_smb2_request *smb2req;
1383 smb2req = find_open_smb2req(xconn, mid);
1385 if (!smb2req) {
1386 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
1387 (unsigned long long)mid));
1388 return false;
1390 if (!smb2req->subreq) {
1391 return false;
1393 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1394 return false;
1396 state = tevent_req_data(smb2req->subreq,
1397 struct smbd_smb2_create_state);
1398 if (!state) {
1399 return false;
1401 /* It's not in progress if there's no timeout event. */
1402 if (!state->open_was_deferred) {
1403 return false;
1406 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
1407 (unsigned long long)mid));
1409 return true;
1412 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
1413 uint64_t mid)
1415 struct smbd_smb2_create_state *state = NULL;
1417 if (!smb2req->subreq) {
1418 return;
1420 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1421 return;
1423 state = tevent_req_data(smb2req->subreq,
1424 struct smbd_smb2_create_state);
1425 if (!state) {
1426 return;
1429 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
1430 "mid %llu\n",
1431 (unsigned long long)mid ));
1433 state->open_was_deferred = false;
1434 /* Ensure we don't have any outstanding timer event. */
1435 TALLOC_FREE(state->te);
1436 /* Ensure we don't have any outstanding immediate event. */
1437 TALLOC_FREE(state->im);
1440 void remove_deferred_open_message_smb2(
1441 struct smbXsrv_connection *xconn, uint64_t mid)
1443 struct smbd_smb2_request *smb2req;
1445 smb2req = find_open_smb2req(xconn, mid);
1447 if (!smb2req) {
1448 DEBUG(10,("remove_deferred_open_message_smb2: "
1449 "can't find mid %llu\n",
1450 (unsigned long long)mid ));
1451 return;
1453 remove_deferred_open_message_smb2_internal(smb2req, mid);
1456 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1457 struct tevent_immediate *im,
1458 void *private_data)
1460 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1461 struct smbd_smb2_request);
1462 uint64_t mid = get_mid_from_smb2req(smb2req);
1463 NTSTATUS status;
1465 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1466 "re-dispatching mid %llu\n",
1467 (unsigned long long)mid ));
1469 status = smbd_smb2_request_dispatch(smb2req);
1470 if (!NT_STATUS_IS_OK(status)) {
1471 smbd_server_connection_terminate(smb2req->xconn,
1472 nt_errstr(status));
1473 return;
1477 bool schedule_deferred_open_message_smb2(
1478 struct smbXsrv_connection *xconn, uint64_t mid)
1480 struct smbd_smb2_create_state *state = NULL;
1481 struct smbd_smb2_request *smb2req;
1483 smb2req = find_open_smb2req(xconn, mid);
1485 if (!smb2req) {
1486 DEBUG(10,("schedule_deferred_open_message_smb2: "
1487 "can't find mid %llu\n",
1488 (unsigned long long)mid ));
1489 return false;
1491 if (!smb2req->subreq) {
1492 return false;
1494 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1495 return false;
1497 state = tevent_req_data(smb2req->subreq,
1498 struct smbd_smb2_create_state);
1499 if (!state) {
1500 return false;
1503 /* Ensure we don't have any outstanding timer event. */
1504 TALLOC_FREE(state->te);
1505 /* Ensure we don't have any outstanding immediate event. */
1506 TALLOC_FREE(state->im);
1509 * This is subtle. We must null out the callback
1510 * before rescheduling, else the first call to
1511 * tevent_req_nterror() causes the _receive()
1512 * function to be called, this causing tevent_req_post()
1513 * to crash.
1515 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1517 state->im = tevent_create_immediate(smb2req);
1518 if (!state->im) {
1519 smbd_server_connection_terminate(smb2req->xconn,
1520 nt_errstr(NT_STATUS_NO_MEMORY));
1521 return false;
1524 DEBUG(10,("schedule_deferred_open_message_smb2: "
1525 "re-processing mid %llu\n",
1526 (unsigned long long)mid ));
1528 tevent_schedule_immediate(state->im,
1529 smb2req->sconn->ev_ctx,
1530 smbd_smb2_create_request_dispatch_immediate,
1531 smb2req);
1533 return true;
1536 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1538 struct smbd_smb2_request *smb2req = NULL;
1539 struct smbd_smb2_create_state *state = tevent_req_data(req,
1540 struct smbd_smb2_create_state);
1541 uint64_t mid;
1543 if (!state) {
1544 return false;
1547 if (!state->smb2req) {
1548 return false;
1551 smb2req = state->smb2req;
1552 mid = get_mid_from_smb2req(smb2req);
1554 if (is_deferred_open_async(state->open_rec)) {
1555 /* Can't cancel an async create. */
1556 return false;
1559 remove_deferred_open_message_smb2_internal(smb2req, mid);
1561 tevent_req_defer_callback(req, smb2req->sconn->ev_ctx);
1562 tevent_req_nterror(req, NT_STATUS_CANCELLED);
1563 return true;
1566 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1567 struct timeval request_time,
1568 struct timeval timeout,
1569 struct file_id id,
1570 struct deferred_open_record *open_rec)
1572 struct tevent_req *req = NULL;
1573 struct smbd_smb2_create_state *state = NULL;
1574 struct timeval end_time;
1576 if (!smb2req) {
1577 return false;
1579 req = smb2req->subreq;
1580 if (!req) {
1581 return false;
1583 state = tevent_req_data(req, struct smbd_smb2_create_state);
1584 if (!state) {
1585 return false;
1587 state->id = id;
1588 state->request_time = request_time;
1589 state->open_rec = talloc_move(state, &open_rec);
1591 /* Re-schedule us to retry on timer expiry. */
1592 end_time = timeval_sum(&request_time, &timeout);
1594 DEBUG(10,("push_deferred_open_message_smb2: "
1595 "timeout at %s\n",
1596 timeval_string(talloc_tos(),
1597 &end_time,
1598 true) ));
1600 state->open_was_deferred = true;
1602 /* allow this request to be canceled */
1603 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1605 return true;