2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "../libcli/smb/smb_common.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "../lib/util/tevent_ntstatus.h"
31 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
33 switch(in_oplock_level) {
34 case SMB2_OPLOCK_LEVEL_NONE:
36 case SMB2_OPLOCK_LEVEL_II:
37 return LEVEL_II_OPLOCK;
38 case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
39 return EXCLUSIVE_OPLOCK;
40 case SMB2_OPLOCK_LEVEL_BATCH:
42 case SMB2_OPLOCK_LEVEL_LEASE:
43 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
44 "LEASE_OPLOCK_REQUESTED\n"));
47 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
49 (unsigned int)in_oplock_level));
54 static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
56 if (BATCH_OPLOCK_TYPE(oplock_type)) {
57 return SMB2_OPLOCK_LEVEL_BATCH;
58 } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type)) {
59 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
60 } else if (oplock_type == LEVEL_II_OPLOCK) {
62 * Don't use LEVEL_II_OPLOCK_TYPE here as
63 * this also includes FAKE_LEVEL_II_OPLOCKs
64 * which are internal only.
66 return SMB2_OPLOCK_LEVEL_II;
68 return SMB2_OPLOCK_LEVEL_NONE;
72 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
73 struct tevent_context *ev,
74 struct smbd_smb2_request *smb2req,
75 uint8_t in_oplock_level,
76 uint32_t in_impersonation_level,
77 uint32_t in_desired_access,
78 uint32_t in_file_attributes,
79 uint32_t in_share_access,
80 uint32_t in_create_disposition,
81 uint32_t in_create_options,
83 struct smb2_create_blobs in_context_blobs);
84 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
86 uint8_t *out_oplock_level,
87 uint32_t *out_create_action,
88 NTTIME *out_creation_time,
89 NTTIME *out_last_access_time,
90 NTTIME *out_last_write_time,
91 NTTIME *out_change_time,
92 uint64_t *out_allocation_size,
93 uint64_t *out_end_of_file,
94 uint32_t *out_file_attributes,
95 uint64_t *out_file_id_persistent,
96 uint64_t *out_file_id_volatile,
97 struct smb2_create_blobs *out_context_blobs);
99 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
100 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
102 const uint8_t *inbody;
103 const struct iovec *indyniov;
104 uint8_t in_oplock_level;
105 uint32_t in_impersonation_level;
106 uint32_t in_desired_access;
107 uint32_t in_file_attributes;
108 uint32_t in_share_access;
109 uint32_t in_create_disposition;
110 uint32_t in_create_options;
111 uint16_t in_name_offset;
112 uint16_t in_name_length;
113 DATA_BLOB in_name_buffer;
114 char *in_name_string;
115 size_t in_name_string_size;
116 uint32_t name_offset = 0;
117 uint32_t name_available_length = 0;
118 uint32_t in_context_offset;
119 uint32_t in_context_length;
120 DATA_BLOB in_context_buffer;
121 struct smb2_create_blobs in_context_blobs;
122 uint32_t context_offset = 0;
123 uint32_t context_available_length = 0;
127 struct tevent_req *tsubreq;
129 status = smbd_smb2_request_verify_sizes(smb2req, 0x39);
130 if (!NT_STATUS_IS_OK(status)) {
131 return smbd_smb2_request_error(smb2req, status);
133 inbody = SMBD_SMB2_IN_BODY_PTR(smb2req);
135 in_oplock_level = CVAL(inbody, 0x03);
136 in_impersonation_level = IVAL(inbody, 0x04);
137 in_desired_access = IVAL(inbody, 0x18);
138 in_file_attributes = IVAL(inbody, 0x1C);
139 in_share_access = IVAL(inbody, 0x20);
140 in_create_disposition = IVAL(inbody, 0x24);
141 in_create_options = IVAL(inbody, 0x28);
142 in_name_offset = SVAL(inbody, 0x2C);
143 in_name_length = SVAL(inbody, 0x2E);
144 in_context_offset = IVAL(inbody, 0x30);
145 in_context_length = IVAL(inbody, 0x34);
148 * First check if the dynamic name and context buffers
149 * are correctly specified.
151 * Note: That we don't check if the name and context buffers
155 dyn_offset = SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(smb2req);
157 if (in_name_offset == 0 && in_name_length == 0) {
160 } else if (in_name_offset < dyn_offset) {
161 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
163 name_offset = in_name_offset - dyn_offset;
166 indyniov = SMBD_SMB2_IN_DYN_IOV(smb2req);
168 if (name_offset > indyniov->iov_len) {
169 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
172 name_available_length = indyniov->iov_len - name_offset;
174 if (in_name_length > name_available_length) {
175 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
178 in_name_buffer.data = (uint8_t *)indyniov->iov_base + name_offset;
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 > indyniov->iov_len) {
191 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
194 context_available_length = indyniov->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 *)indyniov->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);
214 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
217 if (in_name_buffer.length == 0) {
218 in_name_string_size = 0;
221 if (strlen(in_name_string) != in_name_string_size) {
222 return smbd_smb2_request_error(smb2req, NT_STATUS_OBJECT_NAME_INVALID);
225 ZERO_STRUCT(in_context_blobs);
226 status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
227 if (!NT_STATUS_IS_OK(status)) {
228 return smbd_smb2_request_error(smb2req, status);
231 tsubreq = smbd_smb2_create_send(smb2req,
232 smb2req->sconn->ev_ctx,
235 in_impersonation_level,
239 in_create_disposition,
243 if (tsubreq == NULL) {
244 smb2req->subreq = NULL;
245 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
247 tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
250 * For now we keep the logic that we do not send STATUS_PENDING
251 * for sharing violations, so we just wait 2 seconds.
253 * TODO: we need more tests for this.
255 return smbd_smb2_request_pending_queue(smb2req, tsubreq, 2000000);
258 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
260 uint8_t *reqhdr = SMBD_SMB2_OUT_HDR_PTR(smb2req);
261 return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
264 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
266 struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
267 struct smbd_smb2_request);
270 uint8_t out_oplock_level = 0;
271 uint32_t out_create_action = 0;
272 NTTIME out_creation_time = 0;
273 NTTIME out_last_access_time = 0;
274 NTTIME out_last_write_time = 0;
275 NTTIME out_change_time = 0;
276 uint64_t out_allocation_size = 0;
277 uint64_t out_end_of_file = 0;
278 uint32_t out_file_attributes = 0;
279 uint64_t out_file_id_persistent = 0;
280 uint64_t out_file_id_volatile = 0;
281 struct smb2_create_blobs out_context_blobs;
282 DATA_BLOB out_context_buffer;
283 uint16_t out_context_buffer_offset = 0;
285 NTSTATUS error; /* transport error */
287 if (smb2req->cancelled) {
288 uint64_t mid = get_mid_from_smb2req(smb2req);
289 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
290 (unsigned long long)mid ));
291 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
292 if (!NT_STATUS_IS_OK(error)) {
293 smbd_server_connection_terminate(smb2req->sconn,
300 status = smbd_smb2_create_recv(tsubreq,
305 &out_last_access_time,
306 &out_last_write_time,
308 &out_allocation_size,
310 &out_file_attributes,
311 &out_file_id_persistent,
312 &out_file_id_volatile,
314 if (!NT_STATUS_IS_OK(status)) {
315 error = smbd_smb2_request_error(smb2req, status);
316 if (!NT_STATUS_IS_OK(error)) {
317 smbd_server_connection_terminate(smb2req->sconn,
324 status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
325 if (!NT_STATUS_IS_OK(status)) {
326 error = smbd_smb2_request_error(smb2req, status);
327 if (!NT_STATUS_IS_OK(error)) {
328 smbd_server_connection_terminate(smb2req->sconn,
335 if (out_context_buffer.length > 0) {
336 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
339 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
340 if (outbody.data == NULL) {
341 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
342 if (!NT_STATUS_IS_OK(error)) {
343 smbd_server_connection_terminate(smb2req->sconn,
350 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
351 SCVAL(outbody.data, 0x02,
352 out_oplock_level); /* oplock level */
353 SCVAL(outbody.data, 0x03, 0); /* reserved */
354 SIVAL(outbody.data, 0x04,
355 out_create_action); /* create action */
356 SBVAL(outbody.data, 0x08,
357 out_creation_time); /* creation time */
358 SBVAL(outbody.data, 0x10,
359 out_last_access_time); /* last access time */
360 SBVAL(outbody.data, 0x18,
361 out_last_write_time); /* last write time */
362 SBVAL(outbody.data, 0x20,
363 out_change_time); /* change time */
364 SBVAL(outbody.data, 0x28,
365 out_allocation_size); /* allocation size */
366 SBVAL(outbody.data, 0x30,
367 out_end_of_file); /* end of file */
368 SIVAL(outbody.data, 0x38,
369 out_file_attributes); /* file attributes */
370 SIVAL(outbody.data, 0x3C, 0); /* reserved */
371 SBVAL(outbody.data, 0x40,
372 out_file_id_persistent); /* file id (persistent) */
373 SBVAL(outbody.data, 0x48,
374 out_file_id_volatile); /* file id (volatile) */
375 SIVAL(outbody.data, 0x50,
376 out_context_buffer_offset); /* create contexts offset */
377 SIVAL(outbody.data, 0x54,
378 out_context_buffer.length); /* create contexts length */
380 outdyn = out_context_buffer;
382 error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
383 if (!NT_STATUS_IS_OK(error)) {
384 smbd_server_connection_terminate(smb2req->sconn,
390 struct smbd_smb2_create_state {
391 struct smbd_smb2_request *smb2req;
392 struct smb_request *smb1req;
393 struct timed_event *te;
394 struct tevent_immediate *im;
395 struct timeval request_time;
397 DATA_BLOB private_data;
398 uint8_t out_oplock_level;
399 uint32_t out_create_action;
400 NTTIME out_creation_time;
401 NTTIME out_last_access_time;
402 NTTIME out_last_write_time;
403 NTTIME out_change_time;
404 uint64_t out_allocation_size;
405 uint64_t out_end_of_file;
406 uint32_t out_file_attributes;
407 uint64_t out_file_id_persistent;
408 uint64_t out_file_id_volatile;
409 struct smb2_create_blobs out_context_blobs;
412 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
413 struct tevent_context *ev,
414 struct smbd_smb2_request *smb2req,
415 uint8_t in_oplock_level,
416 uint32_t in_impersonation_level,
417 uint32_t in_desired_access,
418 uint32_t in_file_attributes,
419 uint32_t in_share_access,
420 uint32_t in_create_disposition,
421 uint32_t in_create_options,
423 struct smb2_create_blobs in_context_blobs)
425 struct tevent_req *req = NULL;
426 struct smbd_smb2_create_state *state = NULL;
428 struct smb_request *smb1req = NULL;
429 files_struct *result = NULL;
431 struct timespec write_time_ts;
432 struct smb2_create_blobs out_context_blobs;
433 int requested_oplock_level;
435 ZERO_STRUCT(out_context_blobs);
437 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
438 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
440 requested_oplock_level = in_oplock_level;
444 if (smb2req->subreq == NULL) {
445 /* New create call. */
446 req = tevent_req_create(mem_ctx, &state,
447 struct smbd_smb2_create_state);
451 state->smb2req = smb2req;
453 smb1req = smbd_smb2_fake_smb_request(smb2req);
454 if (tevent_req_nomem(smb1req, req)) {
455 return tevent_req_post(req, ev);
457 state->smb1req = smb1req;
458 smb2req->subreq = req;
459 DEBUG(10,("smbd_smb2_create: name[%s]\n",
462 /* Re-entrant create call. */
463 req = smb2req->subreq;
464 state = tevent_req_data(req,
465 struct smbd_smb2_create_state);
466 smb1req = state->smb1req;
467 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
471 if (IS_IPC(smb1req->conn)) {
472 const char *pipe_name = in_name;
474 if (!lp_nt_pipe_support()) {
475 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
476 return tevent_req_post(req, ev);
479 status = open_np_file(smb1req, pipe_name, &result);
480 if (!NT_STATUS_IS_OK(status)) {
481 tevent_req_nterror(req, status);
482 return tevent_req_post(req, ev);
484 info = FILE_WAS_OPENED;
485 } else if (CAN_PRINT(smb1req->conn)) {
486 status = file_new(smb1req, smb1req->conn, &result);
487 if(!NT_STATUS_IS_OK(status)) {
488 tevent_req_nterror(req, status);
489 return tevent_req_post(req, ev);
492 status = print_spool_open(result, in_name,
494 if (!NT_STATUS_IS_OK(status)) {
495 file_free(smb1req, result);
496 tevent_req_nterror(req, status);
497 return tevent_req_post(req, ev);
499 info = FILE_WAS_CREATED;
502 struct smb_filename *smb_fname = NULL;
503 struct smb2_create_blob *exta = NULL;
504 struct ea_list *ea_list = NULL;
505 struct smb2_create_blob *mxac = NULL;
506 NTTIME max_access_time = 0;
507 struct smb2_create_blob *secd = NULL;
508 struct security_descriptor *sec_desc = NULL;
509 struct smb2_create_blob *dhnq = NULL;
510 struct smb2_create_blob *dhnc = NULL;
511 struct smb2_create_blob *alsi = NULL;
512 uint64_t allocation_size = 0;
513 struct smb2_create_blob *twrp = NULL;
514 struct smb2_create_blob *qfid = NULL;
516 exta = smb2_create_blob_find(&in_context_blobs,
517 SMB2_CREATE_TAG_EXTA);
518 mxac = smb2_create_blob_find(&in_context_blobs,
519 SMB2_CREATE_TAG_MXAC);
520 secd = smb2_create_blob_find(&in_context_blobs,
521 SMB2_CREATE_TAG_SECD);
522 dhnq = smb2_create_blob_find(&in_context_blobs,
523 SMB2_CREATE_TAG_DHNQ);
524 dhnc = smb2_create_blob_find(&in_context_blobs,
525 SMB2_CREATE_TAG_DHNC);
526 alsi = smb2_create_blob_find(&in_context_blobs,
527 SMB2_CREATE_TAG_ALSI);
528 twrp = smb2_create_blob_find(&in_context_blobs,
529 SMB2_CREATE_TAG_TWRP);
530 qfid = smb2_create_blob_find(&in_context_blobs,
531 SMB2_CREATE_TAG_QFID);
533 fname = talloc_strdup(state, in_name);
534 if (tevent_req_nomem(fname, req)) {
535 return tevent_req_post(req, ev);
540 tevent_req_nterror(req,NT_STATUS_OBJECT_NAME_NOT_FOUND);
541 return tevent_req_post(req, ev);
544 ea_list = read_nttrans_ea_list(mem_ctx,
545 (const char *)exta->data.data, exta->data.length);
547 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
548 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
549 return tevent_req_post(req, ev);
555 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
556 return tevent_req_post(req, ev);
559 if (mxac->data.length == 0) {
561 } else if (mxac->data.length == 8) {
562 max_access_time = BVAL(mxac->data.data, 0);
564 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
565 return tevent_req_post(req, ev);
570 enum ndr_err_code ndr_err;
573 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
574 return tevent_req_post(req, ev);
577 sec_desc = talloc_zero(state, struct security_descriptor);
578 if (tevent_req_nomem(sec_desc, req)) {
579 return tevent_req_post(req, ev);
582 ndr_err = ndr_pull_struct_blob(&secd->data,
584 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
585 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
586 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
587 ndr_errstr(ndr_err)));
588 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
589 return tevent_req_post(req, ev);
595 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
596 return tevent_req_post(req, ev);
599 if (dhnq->data.length != 16) {
600 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
601 return tevent_req_post(req, ev);
604 * we don't support durable handles yet
605 * and have to ignore this
610 if (dhnc->data.length != 16) {
611 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
612 return tevent_req_post(req, ev);
614 /* we don't support durable handles yet */
615 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
616 return tevent_req_post(req, ev);
621 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
622 return tevent_req_post(req, ev);
625 if (alsi->data.length != 8) {
626 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
627 return tevent_req_post(req, ev);
629 allocation_size = BVAL(alsi->data.data, 0);
638 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
639 return tevent_req_post(req, ev);
642 if (twrp->data.length != 8) {
643 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
644 return tevent_req_post(req, ev);
647 nttime = BVAL(twrp->data.data, 0);
648 t = nt_time_to_unix(nttime);
652 fname = talloc_asprintf(state,
653 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
661 if (tevent_req_nomem(fname, req)) {
662 return tevent_req_post(req, ev);
667 if (qfid->data.length != 0) {
668 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
669 return tevent_req_post(req, ev);
673 /* these are ignored for SMB2 */
674 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
675 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
678 * For a DFS path the function parse_dfs_path()
679 * will do the path processing.
682 if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
683 /* convert '\\' into '/' */
684 status = check_path_syntax(fname);
685 if (!NT_STATUS_IS_OK(status)) {
686 tevent_req_nterror(req, status);
687 return tevent_req_post(req, ev);
691 status = filename_convert(req,
693 smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
695 0, /* unix_convert flags */
696 NULL, /* ppath_contains_wcards */
698 if (!NT_STATUS_IS_OK(status)) {
699 tevent_req_nterror(req, status);
700 return tevent_req_post(req, ev);
703 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
705 status = SMB_VFS_CREATE_FILE(smb1req->conn,
707 0, /* root_dir_fid */
711 in_create_disposition,
714 map_smb2_oplock_levels_to_samba(requested_oplock_level),
716 0, /* private_flags */
721 if (!NT_STATUS_IS_OK(status)) {
722 if (open_was_deferred(smb1req->sconn, smb1req->mid)) {
725 tevent_req_nterror(req, status);
726 return tevent_req_post(req, ev);
730 NTTIME last_write_time;
732 unix_timespec_to_nt_time(&last_write_time,
733 result->fsp_name->st.st_ex_mtime);
734 if (last_write_time != max_access_time) {
736 uint32_t max_access_granted;
737 DATA_BLOB blob = data_blob_const(p, sizeof(p));
739 status = smbd_calculate_access_mask(smb1req->conn,
741 SEC_FLAG_MAXIMUM_ALLOWED,
742 &max_access_granted);
744 SIVAL(p, 0, NT_STATUS_V(status));
745 SIVAL(p, 4, max_access_granted);
747 status = smb2_create_blob_add(state,
749 SMB2_CREATE_TAG_MXAC,
751 if (!NT_STATUS_IS_OK(status)) {
752 tevent_req_nterror(req, status);
753 return tevent_req_post(req, ev);
760 uint64_t file_index = get_FileIndex(result->conn,
761 &result->fsp_name->st);
762 DATA_BLOB blob = data_blob_const(p, sizeof(p));
766 /* From conversations with Microsoft engineers at
767 the MS plugfest. The first 8 bytes are the "volume index"
768 == inode, the second 8 bytes are the "volume id",
769 == dev. This will be updated in the SMB2 doc. */
770 SBVAL(p, 0, file_index);
771 SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
773 status = smb2_create_blob_add(state, &out_context_blobs,
774 SMB2_CREATE_TAG_QFID,
776 if (!NT_STATUS_IS_OK(status)) {
777 tevent_req_nterror(req, status);
778 return tevent_req_post(req, ev);
783 smb2req->compat_chain_fsp = smb1req->chain_fsp;
785 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
786 state->out_oplock_level = in_oplock_level;
788 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
791 if ((in_create_disposition == FILE_SUPERSEDE)
792 && (info == FILE_WAS_OVERWRITTEN)) {
793 state->out_create_action = FILE_WAS_SUPERSEDED;
795 state->out_create_action = info;
797 state->out_file_attributes = dos_mode(result->conn,
799 /* Deal with other possible opens having a modified
801 ZERO_STRUCT(write_time_ts);
802 get_file_infos(result->file_id, 0, NULL, &write_time_ts);
803 if (!null_timespec(write_time_ts)) {
804 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
807 unix_timespec_to_nt_time(&state->out_creation_time,
808 get_create_timespec(smb1req->conn, result,
810 unix_timespec_to_nt_time(&state->out_last_access_time,
811 result->fsp_name->st.st_ex_atime);
812 unix_timespec_to_nt_time(&state->out_last_write_time,
813 result->fsp_name->st.st_ex_mtime);
814 unix_timespec_to_nt_time(&state->out_change_time,
815 get_change_timespec(smb1req->conn, result,
817 state->out_allocation_size =
818 SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
819 &(result->fsp_name->st));
820 state->out_end_of_file = result->fsp_name->st.st_ex_size;
821 if (state->out_file_attributes == 0) {
822 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
824 state->out_file_id_persistent = result->op->global->open_persistent_id;
825 state->out_file_id_volatile = result->op->global->open_volatile_id;
826 state->out_context_blobs = out_context_blobs;
828 DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
829 fsp_str_dbg(result), fsp_fnum_dbg(result)));
831 tevent_req_done(req);
832 return tevent_req_post(req, ev);
835 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
837 uint8_t *out_oplock_level,
838 uint32_t *out_create_action,
839 NTTIME *out_creation_time,
840 NTTIME *out_last_access_time,
841 NTTIME *out_last_write_time,
842 NTTIME *out_change_time,
843 uint64_t *out_allocation_size,
844 uint64_t *out_end_of_file,
845 uint32_t *out_file_attributes,
846 uint64_t *out_file_id_persistent,
847 uint64_t *out_file_id_volatile,
848 struct smb2_create_blobs *out_context_blobs)
851 struct smbd_smb2_create_state *state = tevent_req_data(req,
852 struct smbd_smb2_create_state);
854 if (tevent_req_is_nterror(req, &status)) {
855 tevent_req_received(req);
859 *out_oplock_level = state->out_oplock_level;
860 *out_create_action = state->out_create_action;
861 *out_creation_time = state->out_creation_time;
862 *out_last_access_time = state->out_last_access_time;
863 *out_last_write_time = state->out_last_write_time;
864 *out_change_time = state->out_change_time;
865 *out_allocation_size = state->out_allocation_size;
866 *out_end_of_file = state->out_end_of_file;
867 *out_file_attributes = state->out_file_attributes;
868 *out_file_id_persistent = state->out_file_id_persistent;
869 *out_file_id_volatile = state->out_file_id_volatile;
870 *out_context_blobs = state->out_context_blobs;
872 talloc_steal(mem_ctx, state->out_context_blobs.blobs);
874 tevent_req_received(req);
878 /*********************************************************
879 Code for dealing with deferred opens.
880 *********************************************************/
882 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
883 struct timeval *p_request_time,
886 struct smbd_smb2_create_state *state = NULL;
887 struct tevent_req *req = NULL;
892 if (smb2req->async_te == NULL) {
895 req = smb2req->subreq;
899 state = tevent_req_data(req, struct smbd_smb2_create_state);
903 if (p_request_time) {
904 *p_request_time = state->request_time;
907 *pp_state = (void *)state->private_data.data;
912 /*********************************************************
913 Re-process this call early - requested by message or
915 *********************************************************/
917 static struct smbd_smb2_request *find_open_smb2req(
918 struct smbd_server_connection *sconn, uint64_t mid)
920 struct smbd_smb2_request *smb2req;
922 for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
924 if (smb2req->subreq == NULL) {
925 /* This message has been processed. */
928 if (!tevent_req_is_in_progress(smb2req->subreq)) {
929 /* This message has been processed. */
932 message_id = get_mid_from_smb2req(smb2req);
933 if (message_id == mid) {
940 bool open_was_deferred_smb2(struct smbd_server_connection *sconn, uint64_t mid)
942 struct smbd_smb2_create_state *state = NULL;
943 struct smbd_smb2_request *smb2req;
945 smb2req = find_open_smb2req(sconn, mid);
948 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
949 (unsigned long long)mid));
952 if (!smb2req->subreq) {
955 if (!tevent_req_is_in_progress(smb2req->subreq)) {
958 state = tevent_req_data(smb2req->subreq,
959 struct smbd_smb2_create_state);
963 /* It's not in progress if there's no timeout event. */
968 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
969 (unsigned long long)mid));
974 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
977 struct smbd_smb2_create_state *state = NULL;
979 if (!smb2req->subreq) {
982 if (!tevent_req_is_in_progress(smb2req->subreq)) {
985 state = tevent_req_data(smb2req->subreq,
986 struct smbd_smb2_create_state);
991 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
993 (unsigned long long)mid ));
995 /* Ensure we don't have any outstanding timer event. */
996 TALLOC_FREE(state->te);
997 /* Ensure we don't have any outstanding immediate event. */
998 TALLOC_FREE(state->im);
1001 void remove_deferred_open_message_smb2(
1002 struct smbd_server_connection *sconn, uint64_t mid)
1004 struct smbd_smb2_request *smb2req;
1006 smb2req = find_open_smb2req(sconn, mid);
1009 DEBUG(10,("remove_deferred_open_message_smb2: "
1010 "can't find mid %llu\n",
1011 (unsigned long long)mid ));
1014 remove_deferred_open_message_smb2_internal(smb2req, mid);
1017 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1018 struct tevent_immediate *im,
1021 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1022 struct smbd_smb2_request);
1023 struct smbd_server_connection *sconn = smb2req->sconn;
1024 uint64_t mid = get_mid_from_smb2req(smb2req);
1027 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1028 "re-dispatching mid %llu\n",
1029 (unsigned long long)mid ));
1031 status = smbd_smb2_request_dispatch(smb2req);
1032 if (!NT_STATUS_IS_OK(status)) {
1033 smbd_server_connection_terminate(sconn, nt_errstr(status));
1038 bool schedule_deferred_open_message_smb2(
1039 struct smbd_server_connection *sconn, uint64_t mid)
1041 struct smbd_smb2_create_state *state = NULL;
1042 struct smbd_smb2_request *smb2req;
1044 smb2req = find_open_smb2req(sconn, mid);
1047 DEBUG(10,("schedule_deferred_open_message_smb2: "
1048 "can't find mid %llu\n",
1049 (unsigned long long)mid ));
1052 if (!smb2req->subreq) {
1055 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1058 state = tevent_req_data(smb2req->subreq,
1059 struct smbd_smb2_create_state);
1064 /* Ensure we don't have any outstanding timer event. */
1065 TALLOC_FREE(state->te);
1066 /* Ensure we don't have any outstanding immediate event. */
1067 TALLOC_FREE(state->im);
1070 * This is subtle. We must null out the callback
1071 * before rescheduling, else the first call to
1072 * tevent_req_nterror() causes the _receive()
1073 * function to be called, this causing tevent_req_post()
1076 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1078 state->im = tevent_create_immediate(smb2req);
1080 smbd_server_connection_terminate(smb2req->sconn,
1081 nt_errstr(NT_STATUS_NO_MEMORY));
1085 DEBUG(10,("schedule_deferred_open_message_smb2: "
1086 "re-processing mid %llu\n",
1087 (unsigned long long)mid ));
1089 tevent_schedule_immediate(state->im,
1090 smb2req->sconn->ev_ctx,
1091 smbd_smb2_create_request_dispatch_immediate,
1097 /*********************************************************
1098 Re-process this call.
1099 *********************************************************/
1101 static void smb2_deferred_open_timer(struct event_context *ev,
1102 struct timed_event *te,
1103 struct timeval _tval,
1107 struct smbd_smb2_create_state *state = NULL;
1108 struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
1109 struct smbd_smb2_request);
1111 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1112 smb2req->current_idx,
1113 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
1115 state = tevent_req_data(smb2req->subreq,
1116 struct smbd_smb2_create_state);
1121 * Null this out, don't talloc_free. It will
1122 * be talloc_free'd by the tevent library when
1126 /* Ensure we don't have any outstanding immediate event. */
1127 TALLOC_FREE(state->im);
1130 * This is subtle. We must null out the callback
1131 * before rescheduling, else the first call to
1132 * tevent_req_nterror() causes the _receive()
1133 * function to be called, this causing tevent_req_post()
1136 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1138 status = smbd_smb2_request_dispatch(smb2req);
1140 if (!NT_STATUS_IS_OK(status)) {
1141 smbd_server_connection_terminate(smb2req->sconn,
1146 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1148 struct smbd_smb2_request *smb2req = NULL;
1149 struct smbd_smb2_create_state *state = tevent_req_data(req,
1150 struct smbd_smb2_create_state);
1157 if (!state->smb2req) {
1161 smb2req = state->smb2req;
1162 mid = get_mid_from_smb2req(smb2req);
1164 if (is_deferred_open_async(state->private_data.data)) {
1165 /* Can't cancel an async create. */
1169 remove_deferred_open_entry(state->id, mid,
1170 messaging_server_id(smb2req->sconn->msg_ctx));
1171 remove_deferred_open_message_smb2_internal(smb2req, mid);
1172 smb2req->cancelled = true;
1174 tevent_req_done(req);
1178 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1179 struct timeval request_time,
1180 struct timeval timeout,
1185 struct tevent_req *req = NULL;
1186 struct smbd_smb2_create_state *state = NULL;
1187 struct timeval end_time;
1192 req = smb2req->subreq;
1196 state = tevent_req_data(req, struct smbd_smb2_create_state);
1201 state->request_time = request_time;
1202 state->private_data = data_blob_talloc(state, private_data,
1204 if (!state->private_data.data) {
1208 /* Re-schedule us to retry on timer expiry. */
1209 end_time = timeval_sum(&request_time, &timeout);
1211 DEBUG(10,("push_deferred_open_message_smb2: "
1213 timeval_string(talloc_tos(),
1217 state->te = tevent_add_timer(smb2req->sconn->ev_ctx,
1220 smb2_deferred_open_timer,
1226 /* allow this request to be canceled */
1227 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);