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/>.
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
26 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
28 switch(in_oplock_level) {
29 case SMB2_OPLOCK_LEVEL_NONE:
31 case SMB2_OPLOCK_LEVEL_II:
32 return LEVEL_II_OPLOCK;
33 case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
34 return EXCLUSIVE_OPLOCK;
35 case SMB2_OPLOCK_LEVEL_BATCH:
37 case SMB2_OPLOCK_LEVEL_LEASE:
38 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
39 "LEASE_OPLOCK_REQUESTED\n"));
42 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
44 (unsigned int)in_oplock_level));
49 static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
51 if (BATCH_OPLOCK_TYPE(oplock_type)) {
52 return SMB2_OPLOCK_LEVEL_BATCH;
53 } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type)) {
54 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
55 } else if (oplock_type == LEVEL_II_OPLOCK) {
57 * Don't use LEVEL_II_OPLOCK_TYPE here as
58 * this also includes FAKE_LEVEL_II_OPLOCKs
59 * which are internal only.
61 return SMB2_OPLOCK_LEVEL_II;
63 return SMB2_OPLOCK_LEVEL_NONE;
67 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
68 struct tevent_context *ev,
69 struct smbd_smb2_request *smb2req,
70 uint8_t in_oplock_level,
71 uint32_t in_impersonation_level,
72 uint32_t in_desired_access,
73 uint32_t in_file_attributes,
74 uint32_t in_share_access,
75 uint32_t in_create_disposition,
76 uint32_t in_create_options,
78 struct smb2_create_blobs in_context_blobs);
79 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
81 uint8_t *out_oplock_level,
82 uint32_t *out_create_action,
83 NTTIME *out_creation_time,
84 NTTIME *out_last_access_time,
85 NTTIME *out_last_write_time,
86 NTTIME *out_change_time,
87 uint64_t *out_allocation_size,
88 uint64_t *out_end_of_file,
89 uint32_t *out_file_attributes,
90 uint64_t *out_file_id_volatile,
91 struct smb2_create_blobs *out_context_blobs);
93 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
94 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
96 const uint8_t *inbody;
97 int i = smb2req->current_idx;
98 size_t expected_body_size = 0x39;
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 if (smb2req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
126 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
129 inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
131 body_size = SVAL(inbody, 0x00);
132 if (body_size != expected_body_size) {
133 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
136 in_oplock_level = CVAL(inbody, 0x03);
137 in_impersonation_level = IVAL(inbody, 0x04);
138 in_desired_access = IVAL(inbody, 0x18);
139 in_file_attributes = IVAL(inbody, 0x1C);
140 in_share_access = IVAL(inbody, 0x20);
141 in_create_disposition = IVAL(inbody, 0x24);
142 in_create_options = IVAL(inbody, 0x28);
143 in_name_offset = SVAL(inbody, 0x2C);
144 in_name_length = SVAL(inbody, 0x2E);
145 in_context_offset = IVAL(inbody, 0x30);
146 in_context_length = IVAL(inbody, 0x34);
149 * First check if the dynamic name and context buffers
150 * are correctly specified.
152 * Note: That we don't check if the name and context buffers
156 dyn_offset = SMB2_HDR_BODY + (body_size & 0xFFFFFFFE);
158 if (in_name_offset == 0 && in_name_length == 0) {
161 } else if (in_name_offset < dyn_offset) {
162 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
164 name_offset = in_name_offset - dyn_offset;
167 if (name_offset > smb2req->in.vector[i+2].iov_len) {
168 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
171 name_available_length = smb2req->in.vector[i+2].iov_len - name_offset;
173 if (in_name_length > name_available_length) {
174 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
177 in_name_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
179 in_name_buffer.length = in_name_length;
181 if (in_context_offset == 0 && in_context_length == 0) {
184 } else if (in_context_offset < dyn_offset) {
185 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
187 context_offset = in_context_offset - dyn_offset;
190 if (context_offset > smb2req->in.vector[i+2].iov_len) {
191 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
194 context_available_length = smb2req->in.vector[i+2].iov_len - context_offset;
196 if (in_context_length > context_available_length) {
197 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
200 in_context_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
202 in_context_buffer.length = in_context_length;
205 * Now interpret the name and context buffers
208 ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
210 in_name_buffer.length,
212 &in_name_string_size, false);
214 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
217 ZERO_STRUCT(in_context_blobs);
218 status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
219 if (!NT_STATUS_IS_OK(status)) {
220 return smbd_smb2_request_error(smb2req, status);
223 tsubreq = smbd_smb2_create_send(smb2req,
224 smb2req->sconn->smb2.event_ctx,
227 in_impersonation_level,
231 in_create_disposition,
235 if (tsubreq == NULL) {
236 smb2req->subreq = NULL;
237 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
239 tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
241 return smbd_smb2_request_pending_queue(smb2req, tsubreq);
244 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
246 uint8_t *reqhdr = (uint8_t *)smb2req->out.vector[smb2req->current_idx].iov_base;
247 return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
250 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
252 struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
253 struct smbd_smb2_request);
254 int i = smb2req->current_idx;
258 uint8_t out_oplock_level = 0;
259 uint32_t out_create_action = 0;
260 NTTIME out_creation_time = 0;
261 NTTIME out_last_access_time = 0;
262 NTTIME out_last_write_time = 0;
263 NTTIME out_change_time = 0;
264 uint64_t out_allocation_size = 0;
265 uint64_t out_end_of_file = 0;
266 uint32_t out_file_attributes = 0;
267 uint64_t out_file_id_volatile = 0;
268 struct smb2_create_blobs out_context_blobs;
269 DATA_BLOB out_context_buffer;
270 uint16_t out_context_buffer_offset = 0;
272 NTSTATUS error; /* transport error */
274 if (smb2req->cancelled) {
275 uint64_t mid = get_mid_from_smb2req(smb2req);
276 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
277 (unsigned long long)mid ));
278 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
279 if (!NT_STATUS_IS_OK(error)) {
280 smbd_server_connection_terminate(smb2req->sconn,
287 status = smbd_smb2_create_recv(tsubreq,
292 &out_last_access_time,
293 &out_last_write_time,
295 &out_allocation_size,
297 &out_file_attributes,
298 &out_file_id_volatile,
300 if (!NT_STATUS_IS_OK(status)) {
301 error = smbd_smb2_request_error(smb2req, status);
302 if (!NT_STATUS_IS_OK(error)) {
303 smbd_server_connection_terminate(smb2req->sconn,
310 status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
311 if (!NT_STATUS_IS_OK(status)) {
312 error = smbd_smb2_request_error(smb2req, status);
313 if (!NT_STATUS_IS_OK(error)) {
314 smbd_server_connection_terminate(smb2req->sconn,
321 if (out_context_buffer.length > 0) {
322 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
325 outhdr = (uint8_t *)smb2req->out.vector[i].iov_base;
327 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
328 if (outbody.data == NULL) {
329 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
330 if (!NT_STATUS_IS_OK(error)) {
331 smbd_server_connection_terminate(smb2req->sconn,
338 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
339 SCVAL(outbody.data, 0x02,
340 out_oplock_level); /* oplock level */
341 SCVAL(outbody.data, 0x03, 0); /* reserved */
342 SIVAL(outbody.data, 0x04,
343 out_create_action); /* create action */
344 SBVAL(outbody.data, 0x08,
345 out_creation_time); /* creation time */
346 SBVAL(outbody.data, 0x10,
347 out_last_access_time); /* last access time */
348 SBVAL(outbody.data, 0x18,
349 out_last_write_time); /* last write time */
350 SBVAL(outbody.data, 0x20,
351 out_change_time); /* change time */
352 SBVAL(outbody.data, 0x28,
353 out_allocation_size); /* allocation size */
354 SBVAL(outbody.data, 0x30,
355 out_end_of_file); /* end of file */
356 SIVAL(outbody.data, 0x38,
357 out_file_attributes); /* file attributes */
358 SIVAL(outbody.data, 0x3C, 0); /* reserved */
359 SBVAL(outbody.data, 0x40, 0); /* file id (persistent) */
360 SBVAL(outbody.data, 0x48,
361 out_file_id_volatile); /* file id (volatile) */
362 SIVAL(outbody.data, 0x50,
363 out_context_buffer_offset); /* create contexts offset */
364 SIVAL(outbody.data, 0x54,
365 out_context_buffer.length); /* create contexts length */
367 outdyn = out_context_buffer;
369 error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
370 if (!NT_STATUS_IS_OK(error)) {
371 smbd_server_connection_terminate(smb2req->sconn,
377 struct smbd_smb2_create_state {
378 struct smbd_smb2_request *smb2req;
379 struct smb_request *smb1req;
380 struct timed_event *te;
381 struct tevent_immediate *im;
382 struct timeval request_time;
384 DATA_BLOB private_data;
385 uint8_t out_oplock_level;
386 uint32_t out_create_action;
387 NTTIME out_creation_time;
388 NTTIME out_last_access_time;
389 NTTIME out_last_write_time;
390 NTTIME out_change_time;
391 uint64_t out_allocation_size;
392 uint64_t out_end_of_file;
393 uint32_t out_file_attributes;
394 uint64_t out_file_id_volatile;
395 struct smb2_create_blobs out_context_blobs;
398 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
399 struct tevent_context *ev,
400 struct smbd_smb2_request *smb2req,
401 uint8_t in_oplock_level,
402 uint32_t in_impersonation_level,
403 uint32_t in_desired_access,
404 uint32_t in_file_attributes,
405 uint32_t in_share_access,
406 uint32_t in_create_disposition,
407 uint32_t in_create_options,
409 struct smb2_create_blobs in_context_blobs)
411 struct tevent_req *req = NULL;
412 struct smbd_smb2_create_state *state = NULL;
414 struct smb_request *smb1req = NULL;
415 files_struct *result = NULL;
417 struct timespec write_time_ts;
418 struct smb2_create_blobs out_context_blobs;
420 ZERO_STRUCT(out_context_blobs);
422 if (!smb2req->async) {
423 /* New create call. */
424 req = tevent_req_create(mem_ctx, &state,
425 struct smbd_smb2_create_state);
429 state->smb2req = smb2req;
430 smb2req->subreq = req; /* So we can find this when going async. */
432 smb1req = smbd_smb2_fake_smb_request(smb2req);
433 if (tevent_req_nomem(smb1req, req)) {
434 return tevent_req_post(req, ev);
436 state->smb1req = smb1req;
437 DEBUG(10,("smbd_smb2_create: name[%s]\n",
440 /* Re-entrant create call. */
441 req = smb2req->subreq;
442 state = tevent_req_data(req,
443 struct smbd_smb2_create_state);
444 smb1req = state->smb1req;
445 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
449 if (IS_IPC(smb1req->conn)) {
450 const char *pipe_name = in_name;
452 if (!lp_nt_pipe_support()) {
453 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
454 return tevent_req_post(req, ev);
457 /* Strip \\ off the name. */
458 if (pipe_name[0] == '\\') {
462 status = open_np_file(smb1req, pipe_name, &result);
463 if (!NT_STATUS_IS_OK(status)) {
464 tevent_req_nterror(req, status);
465 return tevent_req_post(req, ev);
467 info = FILE_WAS_OPENED;
468 } else if (CAN_PRINT(smb1req->conn)) {
469 status = file_new(smb1req, smb1req->conn, &result);
470 if(!NT_STATUS_IS_OK(status)) {
471 tevent_req_nterror(req, status);
472 return tevent_req_post(req, ev);
475 status = print_fsp_open(smb1req,
480 if (!NT_STATUS_IS_OK(status)) {
481 file_free(smb1req, result);
482 tevent_req_nterror(req, status);
483 return tevent_req_post(req, ev);
485 info = FILE_WAS_CREATED;
488 struct smb_filename *smb_fname = NULL;
489 struct smb2_create_blob *exta = NULL;
490 struct ea_list *ea_list = NULL;
491 struct smb2_create_blob *mxac = NULL;
492 NTTIME max_access_time = 0;
493 struct smb2_create_blob *secd = NULL;
494 struct security_descriptor *sec_desc = NULL;
495 struct smb2_create_blob *dhnq = NULL;
496 struct smb2_create_blob *dhnc = NULL;
497 struct smb2_create_blob *alsi = NULL;
498 uint64_t allocation_size = 0;
499 struct smb2_create_blob *twrp = NULL;
500 struct smb2_create_blob *qfid = NULL;
502 exta = smb2_create_blob_find(&in_context_blobs,
503 SMB2_CREATE_TAG_EXTA);
504 mxac = smb2_create_blob_find(&in_context_blobs,
505 SMB2_CREATE_TAG_MXAC);
506 secd = smb2_create_blob_find(&in_context_blobs,
507 SMB2_CREATE_TAG_SECD);
508 dhnq = smb2_create_blob_find(&in_context_blobs,
509 SMB2_CREATE_TAG_DHNQ);
510 dhnc = smb2_create_blob_find(&in_context_blobs,
511 SMB2_CREATE_TAG_DHNC);
512 alsi = smb2_create_blob_find(&in_context_blobs,
513 SMB2_CREATE_TAG_ALSI);
514 twrp = smb2_create_blob_find(&in_context_blobs,
515 SMB2_CREATE_TAG_TWRP);
516 qfid = smb2_create_blob_find(&in_context_blobs,
517 SMB2_CREATE_TAG_QFID);
519 fname = talloc_strdup(state, in_name);
520 if (tevent_req_nomem(fname, req)) {
521 return tevent_req_post(req, ev);
526 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
527 return tevent_req_post(req, ev);
530 ea_list = read_nttrans_ea_list(mem_ctx,
531 (const char *)exta->data.data, exta->data.length);
533 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
534 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
535 return tevent_req_post(req, ev);
541 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
542 return tevent_req_post(req, ev);
545 if (mxac->data.length == 0) {
547 } else if (mxac->data.length == 8) {
548 max_access_time = BVAL(mxac->data.data, 0);
550 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
551 return tevent_req_post(req, ev);
556 enum ndr_err_code ndr_err;
559 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
560 return tevent_req_post(req, ev);
563 sec_desc = talloc_zero(state, struct security_descriptor);
564 if (tevent_req_nomem(sec_desc, req)) {
565 return tevent_req_post(req, ev);
568 ndr_err = ndr_pull_struct_blob(&secd->data,
570 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
571 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
572 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
573 ndr_errstr(ndr_err)));
574 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
575 return tevent_req_post(req, ev);
581 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
582 return tevent_req_post(req, ev);
585 if (dhnq->data.length != 16) {
586 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
587 return tevent_req_post(req, ev);
590 * we don't support durable handles yet
591 * and have to ignore this
596 if (dhnc->data.length != 16) {
597 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
598 return tevent_req_post(req, ev);
600 /* we don't support durable handles yet */
601 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
602 return tevent_req_post(req, ev);
607 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
608 return tevent_req_post(req, ev);
611 if (alsi->data.length != 8) {
612 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
613 return tevent_req_post(req, ev);
615 allocation_size = BVAL(alsi->data.data, 0);
624 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
625 return tevent_req_post(req, ev);
628 if (twrp->data.length != 8) {
629 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
630 return tevent_req_post(req, ev);
633 nttime = BVAL(twrp->data.data, 0);
634 t = nt_time_to_unix(nttime);
638 fname = talloc_asprintf(state,
639 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
647 if (tevent_req_nomem(fname, req)) {
648 return tevent_req_post(req, ev);
653 if (qfid->data.length != 0) {
654 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
655 return tevent_req_post(req, ev);
659 /* these are ignored for SMB2 */
660 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
661 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
663 /* convert '\\' into '/' */
664 status = check_path_syntax(fname);
665 if (!NT_STATUS_IS_OK(status)) {
666 tevent_req_nterror(req, status);
667 return tevent_req_post(req, ev);
670 status = filename_convert(req,
672 smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
677 if (!NT_STATUS_IS_OK(status)) {
678 tevent_req_nterror(req, status);
679 return tevent_req_post(req, ev);
682 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
684 status = SMB_VFS_CREATE_FILE(smb1req->conn,
686 0, /* root_dir_fid */
690 in_create_disposition,
693 map_smb2_oplock_levels_to_samba(in_oplock_level),
695 0, /* private_flags */
700 if (!NT_STATUS_IS_OK(status)) {
701 if (open_was_deferred(smb1req->mid)) {
704 tevent_req_nterror(req, status);
705 return tevent_req_post(req, ev);
709 NTTIME last_write_time;
711 unix_timespec_to_nt_time(&last_write_time,
712 result->fsp_name->st.st_ex_mtime);
713 if (last_write_time != max_access_time) {
715 uint32_t max_access_granted;
716 DATA_BLOB blob = data_blob_const(p, sizeof(p));
718 status = smbd_check_open_rights(smb1req->conn,
720 SEC_FLAG_MAXIMUM_ALLOWED,
721 &max_access_granted);
723 SIVAL(p, 0, NT_STATUS_V(status));
724 SIVAL(p, 4, max_access_granted);
726 status = smb2_create_blob_add(state,
728 SMB2_CREATE_TAG_MXAC,
730 if (!NT_STATUS_IS_OK(status)) {
731 tevent_req_nterror(req, status);
732 return tevent_req_post(req, ev);
739 DATA_BLOB blob = data_blob_const(p, sizeof(p));
743 /* TODO: maybe use result->file_id */
744 SIVAL(p, 0, result->fsp_name->st.st_ex_ino);/* FileIndexLow */
745 SIVAL(p, 4, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
747 status = smb2_create_blob_add(state, &out_context_blobs,
748 SMB2_CREATE_TAG_QFID,
750 if (!NT_STATUS_IS_OK(status)) {
751 tevent_req_nterror(req, status);
752 return tevent_req_post(req, ev);
757 smb2req->compat_chain_fsp = smb1req->chain_fsp;
759 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
761 if ((in_create_disposition == FILE_SUPERSEDE)
762 && (info == FILE_WAS_OVERWRITTEN)) {
763 state->out_create_action = FILE_WAS_SUPERSEDED;
765 state->out_create_action = info;
767 state->out_file_attributes = dos_mode(result->conn,
769 /* Deal with other possible opens having a modified
771 ZERO_STRUCT(write_time_ts);
772 get_file_infos(result->file_id, NULL, &write_time_ts);
773 if (!null_timespec(write_time_ts)) {
774 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
777 unix_timespec_to_nt_time(&state->out_creation_time,
778 get_create_timespec(smb1req->conn, result,
780 unix_timespec_to_nt_time(&state->out_last_access_time,
781 result->fsp_name->st.st_ex_atime);
782 unix_timespec_to_nt_time(&state->out_last_write_time,
783 result->fsp_name->st.st_ex_mtime);
784 unix_timespec_to_nt_time(&state->out_change_time,
785 get_change_timespec(smb1req->conn, result,
787 state->out_allocation_size =
788 result->fsp_name->st.st_ex_blksize *
789 result->fsp_name->st.st_ex_blocks;
790 state->out_end_of_file = result->fsp_name->st.st_ex_size;
791 if (state->out_file_attributes == 0) {
792 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
794 state->out_file_id_volatile = result->fnum;
795 state->out_context_blobs = out_context_blobs;
797 tevent_req_done(req);
798 return tevent_req_post(req, ev);
801 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
803 uint8_t *out_oplock_level,
804 uint32_t *out_create_action,
805 NTTIME *out_creation_time,
806 NTTIME *out_last_access_time,
807 NTTIME *out_last_write_time,
808 NTTIME *out_change_time,
809 uint64_t *out_allocation_size,
810 uint64_t *out_end_of_file,
811 uint32_t *out_file_attributes,
812 uint64_t *out_file_id_volatile,
813 struct smb2_create_blobs *out_context_blobs)
816 struct smbd_smb2_create_state *state = tevent_req_data(req,
817 struct smbd_smb2_create_state);
819 if (tevent_req_is_nterror(req, &status)) {
820 tevent_req_received(req);
824 *out_oplock_level = state->out_oplock_level;
825 *out_create_action = state->out_create_action;
826 *out_creation_time = state->out_creation_time;
827 *out_last_access_time = state->out_last_access_time;
828 *out_last_write_time = state->out_last_write_time;
829 *out_change_time = state->out_change_time;
830 *out_allocation_size = state->out_allocation_size;
831 *out_end_of_file = state->out_end_of_file;
832 *out_file_attributes = state->out_file_attributes;
833 *out_file_id_volatile = state->out_file_id_volatile;
834 *out_context_blobs = state->out_context_blobs;
836 talloc_steal(mem_ctx, state->out_context_blobs.blobs);
838 tevent_req_received(req);
842 /*********************************************************
843 Code for dealing with deferred opens.
844 *********************************************************/
846 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
847 struct timeval *p_request_time,
850 struct smbd_smb2_create_state *state = NULL;
851 struct tevent_req *req = NULL;
856 if (!smb2req->async) {
859 req = smb2req->subreq;
863 state = tevent_req_data(req, struct smbd_smb2_create_state);
867 if (p_request_time) {
868 *p_request_time = state->request_time;
871 *pp_state = (void *)state->private_data.data;
876 /*********************************************************
877 Re-process this call early - requested by message or
879 *********************************************************/
881 static struct smbd_smb2_request *find_open_smb2req(uint64_t mid)
883 struct smbd_server_connection *sconn = smbd_server_conn;
884 struct smbd_smb2_request *smb2req;
886 for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
888 if (smb2req->subreq == NULL) {
889 /* This message has been processed. */
892 if (!tevent_req_is_in_progress(smb2req->subreq)) {
893 /* This message has been processed. */
896 message_id = get_mid_from_smb2req(smb2req);
897 if (message_id == mid) {
904 bool open_was_deferred_smb2(uint64_t mid)
906 struct smbd_smb2_create_state *state = NULL;
907 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
910 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
911 (unsigned long long)mid));
914 if (!smb2req->subreq) {
917 if (!tevent_req_is_in_progress(smb2req->subreq)) {
920 state = tevent_req_data(smb2req->subreq,
921 struct smbd_smb2_create_state);
925 /* It's not in progress if there's no timeout event. */
930 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
931 (unsigned long long)mid));
936 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
939 struct smbd_smb2_create_state *state = NULL;
941 if (!smb2req->subreq) {
944 if (!tevent_req_is_in_progress(smb2req->subreq)) {
947 state = tevent_req_data(smb2req->subreq,
948 struct smbd_smb2_create_state);
953 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
955 (unsigned long long)mid ));
957 /* Ensure we don't have any outstanding timer event. */
958 TALLOC_FREE(state->te);
959 /* Ensure we don't have any outstanding immediate event. */
960 TALLOC_FREE(state->im);
963 void remove_deferred_open_message_smb2(uint64_t mid)
965 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
968 DEBUG(10,("remove_deferred_open_message_smb2: "
969 "can't find mid %llu\n",
970 (unsigned long long)mid ));
973 remove_deferred_open_message_smb2_internal(smb2req, mid);
976 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
977 struct tevent_immediate *im,
980 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
981 struct smbd_smb2_request);
982 struct smbd_server_connection *sconn = smb2req->sconn;
983 uint64_t mid = get_mid_from_smb2req(smb2req);
986 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
987 "re-dispatching mid %llu\n",
988 (unsigned long long)mid ));
990 status = smbd_smb2_request_dispatch(smb2req);
991 if (!NT_STATUS_IS_OK(status)) {
992 smbd_server_connection_terminate(sconn, nt_errstr(status));
997 void schedule_deferred_open_message_smb2(uint64_t mid)
999 struct smbd_smb2_create_state *state = NULL;
1000 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
1003 DEBUG(10,("schedule_deferred_open_message_smb2: "
1004 "can't find mid %llu\n",
1005 (unsigned long long)mid ));
1008 if (!smb2req->subreq) {
1011 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1014 state = tevent_req_data(smb2req->subreq,
1015 struct smbd_smb2_create_state);
1020 /* Ensure we don't have any outstanding timer event. */
1021 TALLOC_FREE(state->te);
1022 /* Ensure we don't have any outstanding immediate event. */
1023 TALLOC_FREE(state->im);
1026 * This is subtle. We must null out the callback
1027 * before resheduling, else the first call to
1028 * tevent_req_nterror() causes the _receive()
1029 * function to be called, this causing tevent_req_post()
1032 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1034 state->im = tevent_create_immediate(smb2req);
1036 smbd_server_connection_terminate(smb2req->sconn,
1037 nt_errstr(NT_STATUS_NO_MEMORY));
1040 DEBUG(10,("schedule_deferred_open_message_smb2: "
1041 "re-processing mid %llu\n",
1042 (unsigned long long)mid ));
1044 tevent_schedule_immediate(state->im,
1045 smb2req->sconn->smb2.event_ctx,
1046 smbd_smb2_create_request_dispatch_immediate,
1050 /*********************************************************
1051 Re-process this call.
1052 *********************************************************/
1054 static void smb2_deferred_open_timer(struct event_context *ev,
1055 struct timed_event *te,
1056 struct timeval _tval,
1060 struct smbd_smb2_create_state *state = NULL;
1061 struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
1062 struct smbd_smb2_request);
1064 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1065 smb2req->current_idx,
1066 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
1068 state = tevent_req_data(smb2req->subreq,
1069 struct smbd_smb2_create_state);
1074 * Null this out, don't talloc_free. It will
1075 * be talloc_free'd by the tevent library when
1079 /* Ensure we don't have any outstanding immediate event. */
1080 TALLOC_FREE(state->im);
1083 * This is subtle. We must null out the callback
1084 * before resheduling, else the first call to
1085 * tevent_req_nterror() causes the _receive()
1086 * function to be called, this causing tevent_req_post()
1089 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1091 status = smbd_smb2_request_dispatch(smb2req);
1093 if (!NT_STATUS_IS_OK(status)) {
1094 smbd_server_connection_terminate(smb2req->sconn,
1099 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1101 struct smbd_smb2_request *smb2req = NULL;
1102 struct smbd_smb2_create_state *state = tevent_req_data(req,
1103 struct smbd_smb2_create_state);
1110 if (!state->smb2req) {
1114 smb2req = state->smb2req;
1115 mid = get_mid_from_smb2req(smb2req);
1117 remove_deferred_open_entry(state->id, mid);
1118 remove_deferred_open_message_smb2_internal(smb2req, mid);
1119 smb2req->cancelled = true;
1121 tevent_req_done(req);
1125 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1126 struct timeval request_time,
1127 struct timeval timeout,
1132 struct tevent_req *req = NULL;
1133 struct smbd_smb2_create_state *state = NULL;
1134 struct timeval end_time;
1139 req = smb2req->subreq;
1143 state = tevent_req_data(req, struct smbd_smb2_create_state);
1148 state->request_time = request_time;
1149 state->private_data = data_blob_talloc(state, private_data,
1151 if (!state->private_data.data) {
1155 /* Boo - turns out this isn't what W2K8R2
1156 does. It actually sends the STATUS_PENDING
1157 message followed by the STATUS_SHARING_VIOLATION
1158 message. Surely this means that all open
1159 calls (even on directories) will potentially
1160 fail in a chain.... ? And I've seen directory
1161 opens as the start of a chain. JRA.
1164 * More subtlety. To match W2K8R2 don't
1165 * send a "gone async" message if it's simply
1166 * a STATUS_SHARING_VIOLATION (short) wait, not
1167 * an oplock break wait. We do this by prematurely
1168 * setting smb2req->async flag.
1170 if (timeout.tv_sec < 2) {
1171 DEBUG(10,("push_deferred_open_message_smb2: "
1172 "short timer wait (usec = %u). "
1173 "Don't send async message.\n",
1174 (unsigned int)timeout.tv_usec ));
1175 smb2req->async = true;
1179 /* Re-schedule us to retry on timer expiry. */
1180 end_time = timeval_sum(&request_time, &timeout);
1182 DEBUG(10,("push_deferred_open_message_smb2: "
1184 timeval_string(talloc_tos(),
1188 state->te = event_add_timed(smb2req->sconn->smb2.event_ctx,
1191 smb2_deferred_open_timer,
1197 /* allow this request to be canceled */
1198 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);