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 (LEVEL_II_OPLOCK_TYPE(oplock_type)) {
56 return SMB2_OPLOCK_LEVEL_II;
58 return SMB2_OPLOCK_LEVEL_NONE;
62 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
63 struct tevent_context *ev,
64 struct smbd_smb2_request *smb2req,
65 uint8_t in_oplock_level,
66 uint32_t in_impersonation_level,
67 uint32_t in_desired_access,
68 uint32_t in_file_attributes,
69 uint32_t in_share_access,
70 uint32_t in_create_disposition,
71 uint32_t in_create_options,
73 struct smb2_create_blobs in_context_blobs);
74 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
76 uint8_t *out_oplock_level,
77 uint32_t *out_create_action,
78 NTTIME *out_creation_time,
79 NTTIME *out_last_access_time,
80 NTTIME *out_last_write_time,
81 NTTIME *out_change_time,
82 uint64_t *out_allocation_size,
83 uint64_t *out_end_of_file,
84 uint32_t *out_file_attributes,
85 uint64_t *out_file_id_volatile,
86 struct smb2_create_blobs *out_context_blobs);
88 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
89 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
91 const uint8_t *inbody;
92 int i = smb2req->current_idx;
93 size_t expected_body_size = 0x39;
95 uint8_t in_oplock_level;
96 uint32_t in_impersonation_level;
97 uint32_t in_desired_access;
98 uint32_t in_file_attributes;
99 uint32_t in_share_access;
100 uint32_t in_create_disposition;
101 uint32_t in_create_options;
102 uint16_t in_name_offset;
103 uint16_t in_name_length;
104 DATA_BLOB in_name_buffer;
105 char *in_name_string;
106 size_t in_name_string_size;
107 uint32_t name_offset = 0;
108 uint32_t name_available_length = 0;
109 uint32_t in_context_offset;
110 uint32_t in_context_length;
111 DATA_BLOB in_context_buffer;
112 struct smb2_create_blobs in_context_blobs;
113 uint32_t context_offset = 0;
114 uint32_t context_available_length = 0;
118 struct tevent_req *tsubreq;
120 if (smb2req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
121 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
124 inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
126 body_size = SVAL(inbody, 0x00);
127 if (body_size != expected_body_size) {
128 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
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 + (body_size & 0xFFFFFFFE);
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 if (name_offset > smb2req->in.vector[i+2].iov_len) {
163 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
166 name_available_length = smb2req->in.vector[i+2].iov_len - name_offset;
168 if (in_name_length > name_available_length) {
169 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
172 in_name_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
174 in_name_buffer.length = in_name_length;
176 if (in_context_offset == 0 && in_context_length == 0) {
179 } else if (in_context_offset < dyn_offset) {
180 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
182 context_offset = in_context_offset - dyn_offset;
185 if (context_offset > smb2req->in.vector[i+2].iov_len) {
186 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
189 context_available_length = smb2req->in.vector[i+2].iov_len - context_offset;
191 if (in_context_length > context_available_length) {
192 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
195 in_context_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
197 in_context_buffer.length = in_context_length;
200 * Now interpret the name and context buffers
203 ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
205 in_name_buffer.length,
207 &in_name_string_size, false);
209 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
212 ZERO_STRUCT(in_context_blobs);
213 status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
214 if (!NT_STATUS_IS_OK(status)) {
215 return smbd_smb2_request_error(smb2req, status);
218 tsubreq = smbd_smb2_create_send(smb2req,
219 smb2req->sconn->smb2.event_ctx,
222 in_impersonation_level,
226 in_create_disposition,
230 if (tsubreq == NULL) {
231 smb2req->subreq = NULL;
232 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
234 tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
236 return smbd_smb2_request_pending_queue(smb2req, tsubreq);
239 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
241 uint8_t *reqhdr = (uint8_t *)smb2req->out.vector[smb2req->current_idx].iov_base;
242 return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
245 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
247 struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
248 struct smbd_smb2_request);
249 int i = smb2req->current_idx;
253 uint8_t out_oplock_level = 0;
254 uint32_t out_create_action = 0;
255 NTTIME out_creation_time = 0;
256 NTTIME out_last_access_time = 0;
257 NTTIME out_last_write_time = 0;
258 NTTIME out_change_time = 0;
259 uint64_t out_allocation_size = 0;
260 uint64_t out_end_of_file = 0;
261 uint32_t out_file_attributes = 0;
262 uint64_t out_file_id_volatile = 0;
263 struct smb2_create_blobs out_context_blobs;
264 DATA_BLOB out_context_buffer;
265 uint16_t out_context_buffer_offset = 0;
267 NTSTATUS error; /* transport error */
269 if (smb2req->cancelled) {
270 uint64_t mid = get_mid_from_smb2req(smb2req);
271 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
272 (unsigned long long)mid ));
273 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
274 if (!NT_STATUS_IS_OK(error)) {
275 smbd_server_connection_terminate(smb2req->sconn,
282 status = smbd_smb2_create_recv(tsubreq,
287 &out_last_access_time,
288 &out_last_write_time,
290 &out_allocation_size,
292 &out_file_attributes,
293 &out_file_id_volatile,
295 if (!NT_STATUS_IS_OK(status)) {
296 error = smbd_smb2_request_error(smb2req, status);
297 if (!NT_STATUS_IS_OK(error)) {
298 smbd_server_connection_terminate(smb2req->sconn,
305 status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
306 if (!NT_STATUS_IS_OK(status)) {
307 error = smbd_smb2_request_error(smb2req, status);
308 if (!NT_STATUS_IS_OK(error)) {
309 smbd_server_connection_terminate(smb2req->sconn,
316 if (out_context_buffer.length > 0) {
317 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
320 outhdr = (uint8_t *)smb2req->out.vector[i].iov_base;
322 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
323 if (outbody.data == NULL) {
324 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
325 if (!NT_STATUS_IS_OK(error)) {
326 smbd_server_connection_terminate(smb2req->sconn,
333 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
334 SCVAL(outbody.data, 0x02,
335 out_oplock_level); /* oplock level */
336 SCVAL(outbody.data, 0x03, 0); /* reserved */
337 SIVAL(outbody.data, 0x04,
338 out_create_action); /* create action */
339 SBVAL(outbody.data, 0x08,
340 out_creation_time); /* creation time */
341 SBVAL(outbody.data, 0x10,
342 out_last_access_time); /* last access time */
343 SBVAL(outbody.data, 0x18,
344 out_last_write_time); /* last write time */
345 SBVAL(outbody.data, 0x20,
346 out_change_time); /* change time */
347 SBVAL(outbody.data, 0x28,
348 out_allocation_size); /* allocation size */
349 SBVAL(outbody.data, 0x30,
350 out_end_of_file); /* end of file */
351 SIVAL(outbody.data, 0x38,
352 out_file_attributes); /* file attributes */
353 SIVAL(outbody.data, 0x3C, 0); /* reserved */
354 SBVAL(outbody.data, 0x40, 0); /* 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->sconn,
372 struct smbd_smb2_create_state {
373 struct smbd_smb2_request *smb2req;
374 struct smb_request *smb1req;
375 struct timed_event *te;
376 struct tevent_immediate *im;
377 struct timeval request_time;
379 DATA_BLOB private_data;
380 uint8_t out_oplock_level;
381 uint32_t out_create_action;
382 NTTIME out_creation_time;
383 NTTIME out_last_access_time;
384 NTTIME out_last_write_time;
385 NTTIME out_change_time;
386 uint64_t out_allocation_size;
387 uint64_t out_end_of_file;
388 uint32_t out_file_attributes;
389 uint64_t out_file_id_volatile;
390 struct smb2_create_blobs out_context_blobs;
393 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
394 struct tevent_context *ev,
395 struct smbd_smb2_request *smb2req,
396 uint8_t in_oplock_level,
397 uint32_t in_impersonation_level,
398 uint32_t in_desired_access,
399 uint32_t in_file_attributes,
400 uint32_t in_share_access,
401 uint32_t in_create_disposition,
402 uint32_t in_create_options,
404 struct smb2_create_blobs in_context_blobs)
406 struct tevent_req *req = NULL;
407 struct smbd_smb2_create_state *state = NULL;
409 struct smb_request *smb1req = NULL;
410 files_struct *result = NULL;
412 struct timespec write_time_ts;
413 struct smb2_create_blobs out_context_blobs;
415 ZERO_STRUCT(out_context_blobs);
417 if (!smb2req->async) {
418 /* New create call. */
419 req = tevent_req_create(mem_ctx, &state,
420 struct smbd_smb2_create_state);
424 state->smb2req = smb2req;
425 smb2req->subreq = req; /* So we can find this when going async. */
427 smb1req = smbd_smb2_fake_smb_request(smb2req);
428 if (tevent_req_nomem(smb1req, req)) {
429 return tevent_req_post(req, ev);
431 state->smb1req = smb1req;
432 DEBUG(10,("smbd_smb2_create: name[%s]\n",
435 /* Re-entrant create call. */
436 req = smb2req->subreq;
437 state = tevent_req_data(req,
438 struct smbd_smb2_create_state);
439 smb1req = state->smb1req;
440 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
444 if (IS_IPC(smb1req->conn)) {
445 const char *pipe_name = in_name;
447 if (!lp_nt_pipe_support()) {
448 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
449 return tevent_req_post(req, ev);
452 /* Strip \\ off the name. */
453 if (pipe_name[0] == '\\') {
457 status = open_np_file(smb1req, pipe_name, &result);
458 if (!NT_STATUS_IS_OK(status)) {
459 tevent_req_nterror(req, status);
460 return tevent_req_post(req, ev);
462 info = FILE_WAS_OPENED;
463 } else if (CAN_PRINT(smb1req->conn)) {
464 status = file_new(smb1req, smb1req->conn, &result);
465 if(!NT_STATUS_IS_OK(status)) {
466 tevent_req_nterror(req, status);
467 return tevent_req_post(req, ev);
470 status = print_fsp_open(smb1req,
475 if (!NT_STATUS_IS_OK(status)) {
476 file_free(smb1req, result);
477 tevent_req_nterror(req, status);
478 return tevent_req_post(req, ev);
480 info = FILE_WAS_CREATED;
483 struct smb_filename *smb_fname = NULL;
484 struct smb2_create_blob *exta = NULL;
485 struct ea_list *ea_list = NULL;
486 struct smb2_create_blob *mxac = NULL;
487 NTTIME max_access_time = 0;
488 struct smb2_create_blob *secd = NULL;
489 struct security_descriptor *sec_desc = NULL;
490 struct smb2_create_blob *dhnq = NULL;
491 struct smb2_create_blob *dhnc = NULL;
492 struct smb2_create_blob *alsi = NULL;
493 uint64_t allocation_size = 0;
494 struct smb2_create_blob *twrp = NULL;
495 struct smb2_create_blob *qfid = NULL;
497 exta = smb2_create_blob_find(&in_context_blobs,
498 SMB2_CREATE_TAG_EXTA);
499 mxac = smb2_create_blob_find(&in_context_blobs,
500 SMB2_CREATE_TAG_MXAC);
501 secd = smb2_create_blob_find(&in_context_blobs,
502 SMB2_CREATE_TAG_SECD);
503 dhnq = smb2_create_blob_find(&in_context_blobs,
504 SMB2_CREATE_TAG_DHNQ);
505 dhnc = smb2_create_blob_find(&in_context_blobs,
506 SMB2_CREATE_TAG_DHNC);
507 alsi = smb2_create_blob_find(&in_context_blobs,
508 SMB2_CREATE_TAG_ALSI);
509 twrp = smb2_create_blob_find(&in_context_blobs,
510 SMB2_CREATE_TAG_TWRP);
511 qfid = smb2_create_blob_find(&in_context_blobs,
512 SMB2_CREATE_TAG_QFID);
514 fname = talloc_strdup(state, in_name);
515 if (tevent_req_nomem(fname, req)) {
516 return tevent_req_post(req, ev);
521 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
522 return tevent_req_post(req, ev);
525 ea_list = read_nttrans_ea_list(mem_ctx,
526 (const char *)exta->data.data, exta->data.length);
528 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
529 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
530 return tevent_req_post(req, ev);
536 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
537 return tevent_req_post(req, ev);
540 if (mxac->data.length == 0) {
542 } else if (mxac->data.length == 8) {
543 max_access_time = BVAL(mxac->data.data, 0);
545 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
546 return tevent_req_post(req, ev);
551 enum ndr_err_code ndr_err;
554 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
555 return tevent_req_post(req, ev);
558 sec_desc = talloc_zero(state, struct security_descriptor);
559 if (tevent_req_nomem(sec_desc, req)) {
560 return tevent_req_post(req, ev);
563 ndr_err = ndr_pull_struct_blob(&secd->data,
564 sec_desc, NULL, sec_desc,
565 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
566 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
567 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
568 ndr_errstr(ndr_err)));
569 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
570 return tevent_req_post(req, ev);
576 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
577 return tevent_req_post(req, ev);
580 if (dhnq->data.length != 16) {
581 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
582 return tevent_req_post(req, ev);
585 * we don't support durable handles yet
586 * and have to ignore this
591 if (dhnc->data.length != 16) {
592 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
593 return tevent_req_post(req, ev);
595 /* we don't support durable handles yet */
596 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
597 return tevent_req_post(req, ev);
602 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
603 return tevent_req_post(req, ev);
606 if (alsi->data.length != 8) {
607 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
608 return tevent_req_post(req, ev);
610 allocation_size = BVAL(alsi->data.data, 0);
619 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
620 return tevent_req_post(req, ev);
623 if (twrp->data.length != 8) {
624 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
625 return tevent_req_post(req, ev);
628 nttime = BVAL(twrp->data.data, 0);
629 t = nt_time_to_unix(nttime);
633 fname = talloc_asprintf(state,
634 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
642 if (tevent_req_nomem(fname, req)) {
643 return tevent_req_post(req, ev);
648 if (qfid->data.length != 0) {
649 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
650 return tevent_req_post(req, ev);
654 /* these are ignored for SMB2 */
655 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
656 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
658 /* convert '\\' into '/' */
659 status = check_path_syntax(fname);
660 if (!NT_STATUS_IS_OK(status)) {
661 tevent_req_nterror(req, status);
662 return tevent_req_post(req, ev);
665 status = filename_convert(req,
667 smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
672 if (!NT_STATUS_IS_OK(status)) {
673 tevent_req_nterror(req, status);
674 return tevent_req_post(req, ev);
677 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
679 status = SMB_VFS_CREATE_FILE(smb1req->conn,
681 0, /* root_dir_fid */
685 in_create_disposition,
688 map_smb2_oplock_levels_to_samba(in_oplock_level),
690 0, /* private_flags */
695 if (!NT_STATUS_IS_OK(status)) {
696 if (open_was_deferred(smb1req->mid)) {
699 tevent_req_nterror(req, status);
700 return tevent_req_post(req, ev);
704 NTTIME last_write_time;
706 unix_timespec_to_nt_time(&last_write_time,
707 result->fsp_name->st.st_ex_mtime);
708 if (last_write_time != max_access_time) {
710 uint32_t max_access_granted;
711 DATA_BLOB blob = data_blob_const(p, sizeof(p));
713 status = smbd_check_open_rights(smb1req->conn,
715 SEC_FLAG_MAXIMUM_ALLOWED,
716 &max_access_granted);
718 SIVAL(p, 0, NT_STATUS_V(status));
719 SIVAL(p, 4, max_access_granted);
721 status = smb2_create_blob_add(state,
723 SMB2_CREATE_TAG_MXAC,
725 if (!NT_STATUS_IS_OK(status)) {
726 tevent_req_nterror(req, status);
727 return tevent_req_post(req, ev);
734 DATA_BLOB blob = data_blob_const(p, sizeof(p));
738 /* TODO: maybe use result->file_id */
739 SIVAL(p, 0, result->fsp_name->st.st_ex_ino);/* FileIndexLow */
740 SIVAL(p, 4, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
742 status = smb2_create_blob_add(state, &out_context_blobs,
743 SMB2_CREATE_TAG_QFID,
745 if (!NT_STATUS_IS_OK(status)) {
746 tevent_req_nterror(req, status);
747 return tevent_req_post(req, ev);
752 smb2req->compat_chain_fsp = smb1req->chain_fsp;
754 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
756 if ((in_create_disposition == FILE_SUPERSEDE)
757 && (info == FILE_WAS_OVERWRITTEN)) {
758 state->out_create_action = FILE_WAS_SUPERSEDED;
760 state->out_create_action = info;
762 state->out_file_attributes = dos_mode(result->conn,
764 /* Deal with other possible opens having a modified
766 ZERO_STRUCT(write_time_ts);
767 get_file_infos(result->file_id, NULL, &write_time_ts);
768 if (!null_timespec(write_time_ts)) {
769 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
772 unix_timespec_to_nt_time(&state->out_creation_time,
773 get_create_timespec(smb1req->conn, result,
775 unix_timespec_to_nt_time(&state->out_last_access_time,
776 result->fsp_name->st.st_ex_atime);
777 unix_timespec_to_nt_time(&state->out_last_write_time,
778 result->fsp_name->st.st_ex_mtime);
779 unix_timespec_to_nt_time(&state->out_change_time,
780 get_change_timespec(smb1req->conn, result,
782 state->out_allocation_size =
783 result->fsp_name->st.st_ex_blksize *
784 result->fsp_name->st.st_ex_blocks;
785 state->out_end_of_file = result->fsp_name->st.st_ex_size;
786 if (state->out_file_attributes == 0) {
787 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
789 state->out_file_id_volatile = result->fnum;
790 state->out_context_blobs = out_context_blobs;
792 tevent_req_done(req);
793 return tevent_req_post(req, ev);
796 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
798 uint8_t *out_oplock_level,
799 uint32_t *out_create_action,
800 NTTIME *out_creation_time,
801 NTTIME *out_last_access_time,
802 NTTIME *out_last_write_time,
803 NTTIME *out_change_time,
804 uint64_t *out_allocation_size,
805 uint64_t *out_end_of_file,
806 uint32_t *out_file_attributes,
807 uint64_t *out_file_id_volatile,
808 struct smb2_create_blobs *out_context_blobs)
811 struct smbd_smb2_create_state *state = tevent_req_data(req,
812 struct smbd_smb2_create_state);
814 if (tevent_req_is_nterror(req, &status)) {
815 tevent_req_received(req);
819 *out_oplock_level = state->out_oplock_level;
820 *out_create_action = state->out_create_action;
821 *out_creation_time = state->out_creation_time;
822 *out_last_access_time = state->out_last_access_time;
823 *out_last_write_time = state->out_last_write_time;
824 *out_change_time = state->out_change_time;
825 *out_allocation_size = state->out_allocation_size;
826 *out_end_of_file = state->out_end_of_file;
827 *out_file_attributes = state->out_file_attributes;
828 *out_file_id_volatile = state->out_file_id_volatile;
829 *out_context_blobs = state->out_context_blobs;
831 talloc_steal(mem_ctx, state->out_context_blobs.blobs);
833 tevent_req_received(req);
837 /*********************************************************
838 Code for dealing with deferred opens.
839 *********************************************************/
841 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
842 struct timeval *p_request_time,
845 struct smbd_smb2_create_state *state = NULL;
846 struct tevent_req *req = NULL;
851 if (!smb2req->async) {
854 req = smb2req->subreq;
858 state = tevent_req_data(req, struct smbd_smb2_create_state);
862 if (p_request_time) {
863 *p_request_time = state->request_time;
866 *pp_state = (void *)state->private_data.data;
871 /*********************************************************
872 Re-process this call early - requested by message or
874 *********************************************************/
876 static struct smbd_smb2_request *find_open_smb2req(uint64_t mid)
878 struct smbd_server_connection *sconn = smbd_server_conn;
879 struct smbd_smb2_request *smb2req;
881 for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
883 if (smb2req->subreq == NULL) {
884 /* This message has been processed. */
887 if (!tevent_req_is_in_progress(smb2req->subreq)) {
888 /* This message has been processed. */
891 message_id = get_mid_from_smb2req(smb2req);
892 if (message_id == mid) {
899 bool open_was_deferred_smb2(uint64_t mid)
901 struct smbd_smb2_create_state *state = NULL;
902 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
905 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
906 (unsigned long long)mid));
909 if (!smb2req->subreq) {
912 if (!tevent_req_is_in_progress(smb2req->subreq)) {
915 state = tevent_req_data(smb2req->subreq,
916 struct smbd_smb2_create_state);
920 /* It's not in progress if there's no timeout event. */
925 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
926 (unsigned long long)mid));
931 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
934 struct smbd_smb2_create_state *state = NULL;
936 if (!smb2req->subreq) {
939 if (!tevent_req_is_in_progress(smb2req->subreq)) {
942 state = tevent_req_data(smb2req->subreq,
943 struct smbd_smb2_create_state);
948 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
950 (unsigned long long)mid ));
952 /* Ensure we don't have any outstanding timer event. */
953 TALLOC_FREE(state->te);
954 /* Ensure we don't have any outstanding immediate event. */
955 TALLOC_FREE(state->im);
958 void remove_deferred_open_message_smb2(uint64_t mid)
960 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
963 DEBUG(10,("remove_deferred_open_message_smb2: "
964 "can't find mid %llu\n",
965 (unsigned long long)mid ));
968 remove_deferred_open_message_smb2_internal(smb2req, mid);
971 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
972 struct tevent_immediate *im,
975 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
976 struct smbd_smb2_request);
977 struct smbd_server_connection *sconn = smb2req->sconn;
978 uint64_t mid = get_mid_from_smb2req(smb2req);
981 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
982 "re-dispatching mid %llu\n",
983 (unsigned long long)mid ));
985 status = smbd_smb2_request_dispatch(smb2req);
986 if (!NT_STATUS_IS_OK(status)) {
987 smbd_server_connection_terminate(sconn, nt_errstr(status));
992 void schedule_deferred_open_message_smb2(uint64_t mid)
994 struct smbd_smb2_create_state *state = NULL;
995 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
998 DEBUG(10,("schedule_deferred_open_message_smb2: "
999 "can't find mid %llu\n",
1000 (unsigned long long)mid ));
1003 if (!smb2req->subreq) {
1006 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1009 state = tevent_req_data(smb2req->subreq,
1010 struct smbd_smb2_create_state);
1015 /* Ensure we don't have any outstanding timer event. */
1016 TALLOC_FREE(state->te);
1017 /* Ensure we don't have any outstanding immediate event. */
1018 TALLOC_FREE(state->im);
1021 * This is subtle. We must null out the callback
1022 * before resheduling, else the first call to
1023 * tevent_req_nterror() causes the _receive()
1024 * function to be called, this causing tevent_req_post()
1027 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1029 state->im = tevent_create_immediate(smb2req);
1031 smbd_server_connection_terminate(smb2req->sconn,
1032 nt_errstr(NT_STATUS_NO_MEMORY));
1035 DEBUG(10,("schedule_deferred_open_message_smb2: "
1036 "re-processing mid %llu\n",
1037 (unsigned long long)mid ));
1039 tevent_schedule_immediate(state->im,
1040 smb2req->sconn->smb2.event_ctx,
1041 smbd_smb2_create_request_dispatch_immediate,
1045 /*********************************************************
1046 Re-process this call.
1047 *********************************************************/
1049 static void smb2_deferred_open_timer(struct event_context *ev,
1050 struct timed_event *te,
1051 struct timeval _tval,
1055 struct smbd_smb2_create_state *state = NULL;
1056 struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
1057 struct smbd_smb2_request);
1059 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1060 smb2req->current_idx,
1061 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
1063 state = tevent_req_data(smb2req->subreq,
1064 struct smbd_smb2_create_state);
1069 * Null this out, don't talloc_free. It will
1070 * be talloc_free'd by the tevent library when
1074 /* Ensure we don't have any outstanding immediate event. */
1075 TALLOC_FREE(state->im);
1078 * This is subtle. We must null out the callback
1079 * before resheduling, else the first call to
1080 * tevent_req_nterror() causes the _receive()
1081 * function to be called, this causing tevent_req_post()
1084 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1086 status = smbd_smb2_request_dispatch(smb2req);
1088 if (!NT_STATUS_IS_OK(status)) {
1089 smbd_server_connection_terminate(smb2req->sconn,
1094 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1096 struct smbd_smb2_request *smb2req = NULL;
1097 struct smbd_smb2_create_state *state = tevent_req_data(req,
1098 struct smbd_smb2_create_state);
1105 if (!state->smb2req) {
1109 smb2req = state->smb2req;
1110 mid = get_mid_from_smb2req(smb2req);
1112 remove_deferred_open_entry(state->id, mid);
1113 remove_deferred_open_message_smb2_internal(smb2req, mid);
1114 smb2req->cancelled = true;
1116 tevent_req_done(req);
1120 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1121 struct timeval request_time,
1122 struct timeval timeout,
1127 struct tevent_req *req = NULL;
1128 struct smbd_smb2_create_state *state = NULL;
1129 struct timeval end_time;
1134 req = smb2req->subreq;
1138 state = tevent_req_data(req, struct smbd_smb2_create_state);
1143 state->request_time = request_time;
1144 state->private_data = data_blob_talloc(state, private_data,
1146 if (!state->private_data.data) {
1150 /* Boo - turns out this isn't what W2K8R2
1151 does. It actually sends the STATUS_PENDING
1152 message followed by the STATUS_SHARING_VIOLATION
1153 message. Surely this means that all open
1154 calls (even on directories) will potentially
1155 fail in a chain.... ? And I've seen directory
1156 opens as the start of a chain. JRA.
1159 * More subtlety. To match W2K8R2 don't
1160 * send a "gone async" message if it's simply
1161 * a STATUS_SHARING_VIOLATION (short) wait, not
1162 * an oplock break wait. We do this by prematurely
1163 * setting smb2req->async flag.
1165 if (timeout.tv_sec < 2) {
1166 DEBUG(10,("push_deferred_open_message_smb2: "
1167 "short timer wait (usec = %u). "
1168 "Don't send async message.\n",
1169 (unsigned int)timeout.tv_usec ));
1170 smb2req->async = true;
1174 /* Re-schedule us to retry on timer expiry. */
1175 end_time = timeval_sum(&request_time, &timeout);
1177 DEBUG(10,("push_deferred_open_message_smb2: "
1179 timeval_string(talloc_tos(),
1183 state->te = event_add_timed(smb2req->sconn->smb2.event_ctx,
1186 smb2_deferred_open_timer,
1192 /* allow this request to be canceled */
1193 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);