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 struct timespec
*out_creation_ts
,
89 struct timespec
*out_last_access_ts
,
90 struct timespec
*out_last_write_ts
,
91 struct timespec
*out_change_ts
,
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 connection_struct
*conn
= smb2req
->tcon
->compat
;
273 struct timespec out_creation_ts
= { 0, };
274 struct timespec out_last_access_ts
= { 0, };
275 struct timespec out_last_write_ts
= { 0, };
276 struct timespec out_change_ts
= { 0, };
277 uint64_t out_allocation_size
= 0;
278 uint64_t out_end_of_file
= 0;
279 uint32_t out_file_attributes
= 0;
280 uint64_t out_file_id_persistent
= 0;
281 uint64_t out_file_id_volatile
= 0;
282 struct smb2_create_blobs out_context_blobs
;
283 DATA_BLOB out_context_buffer
;
284 uint16_t out_context_buffer_offset
= 0;
286 NTSTATUS error
; /* transport error */
288 status
= smbd_smb2_create_recv(tsubreq
,
296 &out_allocation_size
,
298 &out_file_attributes
,
299 &out_file_id_persistent
,
300 &out_file_id_volatile
,
302 if (!NT_STATUS_IS_OK(status
)) {
303 error
= smbd_smb2_request_error(smb2req
, status
);
304 if (!NT_STATUS_IS_OK(error
)) {
305 smbd_server_connection_terminate(smb2req
->sconn
,
312 status
= smb2_create_blob_push(smb2req
, &out_context_buffer
, out_context_blobs
);
313 if (!NT_STATUS_IS_OK(status
)) {
314 error
= smbd_smb2_request_error(smb2req
, status
);
315 if (!NT_STATUS_IS_OK(error
)) {
316 smbd_server_connection_terminate(smb2req
->sconn
,
323 if (out_context_buffer
.length
> 0) {
324 out_context_buffer_offset
= SMB2_HDR_BODY
+ 0x58;
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 put_long_date_timespec(conn
->ts_res
,
345 (char *)outbody
.data
+ 0x08,
346 out_creation_ts
); /* creation time */
347 put_long_date_timespec(conn
->ts_res
,
348 (char *)outbody
.data
+ 0x10,
349 out_last_access_ts
); /* last access time */
350 put_long_date_timespec(conn
->ts_res
,
351 (char *)outbody
.data
+ 0x18,
352 out_last_write_ts
); /* last write time */
353 put_long_date_timespec(conn
->ts_res
,
354 (char *)outbody
.data
+ 0x20,
355 out_change_ts
); /* change time */
356 SBVAL(outbody
.data
, 0x28,
357 out_allocation_size
); /* allocation size */
358 SBVAL(outbody
.data
, 0x30,
359 out_end_of_file
); /* end of file */
360 SIVAL(outbody
.data
, 0x38,
361 out_file_attributes
); /* file attributes */
362 SIVAL(outbody
.data
, 0x3C, 0); /* reserved */
363 SBVAL(outbody
.data
, 0x40,
364 out_file_id_persistent
); /* file id (persistent) */
365 SBVAL(outbody
.data
, 0x48,
366 out_file_id_volatile
); /* file id (volatile) */
367 SIVAL(outbody
.data
, 0x50,
368 out_context_buffer_offset
); /* create contexts offset */
369 SIVAL(outbody
.data
, 0x54,
370 out_context_buffer
.length
); /* create contexts length */
372 outdyn
= out_context_buffer
;
374 error
= smbd_smb2_request_done(smb2req
, outbody
, &outdyn
);
375 if (!NT_STATUS_IS_OK(error
)) {
376 smbd_server_connection_terminate(smb2req
->sconn
,
382 struct smbd_smb2_create_state
{
383 struct smbd_smb2_request
*smb2req
;
384 struct smb_request
*smb1req
;
385 bool open_was_deferred
;
386 struct timed_event
*te
;
387 struct tevent_immediate
*im
;
388 struct timeval request_time
;
390 DATA_BLOB private_data
;
391 uint8_t out_oplock_level
;
392 uint32_t out_create_action
;
393 struct timespec out_creation_ts
;
394 struct timespec out_last_access_ts
;
395 struct timespec out_last_write_ts
;
396 struct timespec out_change_ts
;
397 uint64_t out_allocation_size
;
398 uint64_t out_end_of_file
;
399 uint32_t out_file_attributes
;
400 uint64_t out_file_id_persistent
;
401 uint64_t out_file_id_volatile
;
402 struct smb2_create_blobs out_context_blobs
;
405 static struct tevent_req
*smbd_smb2_create_send(TALLOC_CTX
*mem_ctx
,
406 struct tevent_context
*ev
,
407 struct smbd_smb2_request
*smb2req
,
408 uint8_t in_oplock_level
,
409 uint32_t in_impersonation_level
,
410 uint32_t in_desired_access
,
411 uint32_t in_file_attributes
,
412 uint32_t in_share_access
,
413 uint32_t in_create_disposition
,
414 uint32_t in_create_options
,
416 struct smb2_create_blobs in_context_blobs
)
418 struct tevent_req
*req
= NULL
;
419 struct smbd_smb2_create_state
*state
= NULL
;
421 struct smb_request
*smb1req
= NULL
;
422 files_struct
*result
= NULL
;
424 struct timespec write_time_ts
;
425 struct smb2_create_blobs out_context_blobs
;
426 int requested_oplock_level
;
427 struct smb2_create_blob
*dhnc
= NULL
;
428 struct smb2_create_blob
*dh2c
= NULL
;
429 struct smbXsrv_open
*op
= NULL
;
431 ZERO_STRUCT(out_context_blobs
);
433 if(lp_fake_oplocks(SNUM(smb2req
->tcon
->compat
))) {
434 requested_oplock_level
= SMB2_OPLOCK_LEVEL_NONE
;
436 requested_oplock_level
= in_oplock_level
;
440 if (smb2req
->subreq
== NULL
) {
441 /* New create call. */
442 req
= tevent_req_create(mem_ctx
, &state
,
443 struct smbd_smb2_create_state
);
447 state
->smb2req
= smb2req
;
449 smb1req
= smbd_smb2_fake_smb_request(smb2req
);
450 if (tevent_req_nomem(smb1req
, req
)) {
451 return tevent_req_post(req
, ev
);
453 state
->smb1req
= smb1req
;
454 smb2req
->subreq
= req
;
455 DEBUG(10,("smbd_smb2_create: name[%s]\n",
458 /* Re-entrant create call. */
459 req
= smb2req
->subreq
;
460 state
= tevent_req_data(req
,
461 struct smbd_smb2_create_state
);
462 smb1req
= state
->smb1req
;
463 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
467 dhnc
= smb2_create_blob_find(&in_context_blobs
,
468 SMB2_CREATE_TAG_DHNC
);
471 if (dhnc
->data
.length
!= 16) {
472 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
473 return tevent_req_post(req
, ev
);
475 if (in_context_blobs
.num_blobs
!= 1) {
477 * DHNC should be the only one.
479 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
480 return tevent_req_post(req
, ev
);
484 dh2c
= smb2_create_blob_find(&in_context_blobs
,
485 SMB2_CREATE_TAG_DH2C
);
487 if (dh2c
->data
.length
!= 36) {
488 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
489 return tevent_req_post(req
, ev
);
491 if (in_context_blobs
.num_blobs
!= 1) {
493 * DH2C should be the only one.
495 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
496 return tevent_req_post(req
, ev
);
500 if (IS_IPC(smb1req
->conn
)) {
501 const char *pipe_name
= in_name
;
504 /* durable handles are not supported on IPC$ */
505 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
506 return tevent_req_post(req
, ev
);
509 if (!lp_nt_pipe_support()) {
510 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
511 return tevent_req_post(req
, ev
);
514 status
= open_np_file(smb1req
, pipe_name
, &result
);
515 if (!NT_STATUS_IS_OK(status
)) {
516 tevent_req_nterror(req
, status
);
517 return tevent_req_post(req
, ev
);
519 info
= FILE_WAS_OPENED
;
520 } else if (CAN_PRINT(smb1req
->conn
)) {
522 /* durable handles are not supported on printers */
523 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
524 return tevent_req_post(req
, ev
);
527 status
= file_new(smb1req
, smb1req
->conn
, &result
);
528 if(!NT_STATUS_IS_OK(status
)) {
529 tevent_req_nterror(req
, status
);
530 return tevent_req_post(req
, ev
);
533 status
= print_spool_open(result
, in_name
,
535 if (!NT_STATUS_IS_OK(status
)) {
536 file_free(smb1req
, result
);
537 tevent_req_nterror(req
, status
);
538 return tevent_req_post(req
, ev
);
540 info
= FILE_WAS_CREATED
;
543 struct smb2_create_blob
*exta
= NULL
;
544 struct ea_list
*ea_list
= NULL
;
545 struct smb2_create_blob
*mxac
= NULL
;
546 NTTIME max_access_time
= 0;
547 struct smb2_create_blob
*secd
= NULL
;
548 struct security_descriptor
*sec_desc
= NULL
;
549 struct smb2_create_blob
*dhnq
= NULL
;
550 struct smb2_create_blob
*alsi
= NULL
;
551 uint64_t allocation_size
= 0;
552 struct smb2_create_blob
*twrp
= NULL
;
553 struct smb2_create_blob
*qfid
= NULL
;
554 struct GUID create_guid
= GUID_zero();
555 bool update_open
= false;
556 bool durable_requested
= false;
557 uint32_t durable_timeout_msec
= 0;
558 bool do_durable_reconnect
= false;
559 struct smb2_create_blob
*dh2q
= NULL
;
561 exta
= smb2_create_blob_find(&in_context_blobs
,
562 SMB2_CREATE_TAG_EXTA
);
563 mxac
= smb2_create_blob_find(&in_context_blobs
,
564 SMB2_CREATE_TAG_MXAC
);
565 secd
= smb2_create_blob_find(&in_context_blobs
,
566 SMB2_CREATE_TAG_SECD
);
567 dhnq
= smb2_create_blob_find(&in_context_blobs
,
568 SMB2_CREATE_TAG_DHNQ
);
569 alsi
= smb2_create_blob_find(&in_context_blobs
,
570 SMB2_CREATE_TAG_ALSI
);
571 twrp
= smb2_create_blob_find(&in_context_blobs
,
572 SMB2_CREATE_TAG_TWRP
);
573 qfid
= smb2_create_blob_find(&in_context_blobs
,
574 SMB2_CREATE_TAG_QFID
);
575 dh2q
= smb2_create_blob_find(&in_context_blobs
,
576 SMB2_CREATE_TAG_DH2Q
);
578 fname
= talloc_strdup(state
, in_name
);
579 if (tevent_req_nomem(fname
, req
)) {
580 return tevent_req_post(req
, ev
);
584 ea_list
= read_nttrans_ea_list(mem_ctx
,
585 (const char *)exta
->data
.data
, exta
->data
.length
);
587 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
588 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
589 return tevent_req_post(req
, ev
);
592 if (ea_list_has_invalid_name(ea_list
)) {
593 tevent_req_nterror(req
, STATUS_INVALID_EA_NAME
);
594 return tevent_req_post(req
, ev
);
599 if (mxac
->data
.length
== 0) {
601 } else if (mxac
->data
.length
== 8) {
602 max_access_time
= BVAL(mxac
->data
.data
, 0);
604 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
605 return tevent_req_post(req
, ev
);
610 enum ndr_err_code ndr_err
;
612 sec_desc
= talloc_zero(state
, struct security_descriptor
);
613 if (tevent_req_nomem(sec_desc
, req
)) {
614 return tevent_req_post(req
, ev
);
617 ndr_err
= ndr_pull_struct_blob(&secd
->data
,
619 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
620 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
621 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
622 ndr_errstr(ndr_err
)));
623 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
624 return tevent_req_post(req
, ev
);
629 if (dhnq
->data
.length
!= 16) {
630 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
631 return tevent_req_post(req
, ev
);
635 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
636 return tevent_req_post(req
, ev
);
640 * durable handle request is processed below.
642 durable_requested
= true;
644 * Set the timeout to 16 mins.
646 * TODO: test this against Windows 2012
647 * as the default for durable v2 is 1 min.
649 durable_timeout_msec
= (16*60*1000);
653 const uint8_t *p
= dh2q
->data
.data
;
654 uint32_t durable_v2_timeout
= 0;
655 DATA_BLOB create_guid_blob
;
657 if (dh2q
->data
.length
!= 32) {
658 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
659 return tevent_req_post(req
, ev
);
663 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
664 return tevent_req_post(req
, ev
);
667 durable_v2_timeout
= IVAL(p
, 0);
668 create_guid_blob
= data_blob_const(p
+ 16, 16);
670 status
= GUID_from_ndr_blob(&create_guid_blob
,
672 if (tevent_req_nterror(req
, status
)) {
673 return tevent_req_post(req
, ev
);
676 * we need to store the create_guid later
681 * durable handle v2 request processed below
683 durable_requested
= true;
684 durable_timeout_msec
= durable_v2_timeout
;
685 if (durable_timeout_msec
== 0) {
687 * Set the timeout to 1 min as default.
689 * This matches Windows 2012.
691 durable_timeout_msec
= (60*1000);
696 NTTIME now
= timeval_to_nttime(&smb2req
->request_time
);
697 uint64_t persistent_id
;
699 persistent_id
= BVAL(dhnc
->data
.data
, 0);
701 status
= smb2srv_open_recreate(smb2req
->sconn
->conn
,
702 smb1req
->conn
->session_info
,
703 persistent_id
, create_guid
,
705 if (!NT_STATUS_IS_OK(status
)) {
706 DEBUG(3, ("smbd_smb2_create_send: "
707 "smb2srv_open_recreate v1 failed: %s\n",
709 tevent_req_nterror(req
, status
);
710 return tevent_req_post(req
, ev
);
713 DEBUG(10, ("smb2_create_send: DHNC: %s recreate the "
714 "smb2srv_open struct for a durable handle.\n",
715 op
->global
->durable
? "did" : "could not"));
717 if (!op
->global
->durable
) {
719 tevent_req_nterror(req
,
720 NT_STATUS_OBJECT_NAME_NOT_FOUND
);
721 return tevent_req_post(req
, ev
);
724 do_durable_reconnect
= true;
728 const uint8_t *p
= dh2c
->data
.data
;
729 NTTIME now
= timeval_to_nttime(&smb2req
->request_time
);
730 uint64_t persistent_id
;
731 DATA_BLOB create_guid_blob
;
733 persistent_id
= BVAL(p
, 0);
734 create_guid_blob
= data_blob_const(p
+ 16, 16);
736 status
= GUID_from_ndr_blob(&create_guid_blob
,
738 if (tevent_req_nterror(req
, status
)) {
739 return tevent_req_post(req
, ev
);
742 status
= smb2srv_open_recreate(smb2req
->sconn
->conn
,
743 smb1req
->conn
->session_info
,
744 persistent_id
, create_guid
,
746 if (!NT_STATUS_IS_OK(status
)) {
747 DEBUG(3, ("smbd_smb2_create_send: "
748 "smb2srv_open_recreate v2 failed: %s\n",
750 tevent_req_nterror(req
, status
);
751 return tevent_req_post(req
, ev
);
754 DEBUG(10, ("smb2_create_send: DH2C: %s recreate the "
755 "smb2srv_open struct for a durable handle.\n",
756 op
->global
->durable
? "did" : "could not"));
758 if (!op
->global
->durable
) {
760 tevent_req_nterror(req
,
761 NT_STATUS_OBJECT_NAME_NOT_FOUND
);
762 return tevent_req_post(req
, ev
);
765 do_durable_reconnect
= true;
769 if (alsi
->data
.length
!= 8) {
770 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
771 return tevent_req_post(req
, ev
);
773 allocation_size
= BVAL(alsi
->data
.data
, 0);
781 if (twrp
->data
.length
!= 8) {
782 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
783 return tevent_req_post(req
, ev
);
786 nttime
= BVAL(twrp
->data
.data
, 0);
787 t
= nt_time_to_unix(nttime
);
791 fname
= talloc_asprintf(state
,
792 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
800 if (tevent_req_nomem(fname
, req
)) {
801 return tevent_req_post(req
, ev
);
806 if (qfid
->data
.length
!= 0) {
807 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
808 return tevent_req_post(req
, ev
);
812 /* these are ignored for SMB2 */
813 in_create_options
&= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
814 in_create_options
&= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
816 in_file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
818 DEBUG(10, ("smbd_smb2_create_send: open execution phase\n"));
821 * For the backend file open procedure, there are
822 * two possible modes: durable_reconnect or not.
824 if (do_durable_reconnect
) {
825 DATA_BLOB new_cookie
= data_blob_null
;
827 status
= SMB_VFS_DURABLE_RECONNECT(smb1req
->conn
,
830 op
->global
->backend_cookie
,
831 op
, &result
, &new_cookie
);
832 if (!NT_STATUS_IS_OK(status
)) {
833 NTSTATUS return_status
;
835 return_status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
837 DEBUG(3, ("smbd_smb2_create_send: "
838 "durable_reconnect failed: %s => %s\n",
840 nt_errstr(return_status
)));
842 tevent_req_nterror(req
, return_status
);
843 return tevent_req_post(req
, ev
);
846 data_blob_free(&op
->global
->backend_cookie
);
847 op
->global
->backend_cookie
= new_cookie
;
849 op
->status
= NT_STATUS_OK
;
850 op
->global
->disconnect_time
= 0;
852 /* save the timout for later update */
853 durable_timeout_msec
= op
->global
->durable_timeout_msec
;
857 info
= FILE_WAS_OPENED
;
859 struct smb_filename
*smb_fname
= NULL
;
862 * For a DFS path the function parse_dfs_path()
863 * will do the path processing.
866 if (!(smb1req
->flags2
& FLAGS2_DFS_PATHNAMES
)) {
867 /* convert '\\' into '/' */
868 status
= check_path_syntax(fname
);
869 if (!NT_STATUS_IS_OK(status
)) {
870 tevent_req_nterror(req
, status
);
871 return tevent_req_post(req
, ev
);
875 status
= filename_convert(req
,
877 smb1req
->flags2
& FLAGS2_DFS_PATHNAMES
,
880 NULL
, /* ppath_contains_wcards */
882 if (!NT_STATUS_IS_OK(status
)) {
883 tevent_req_nterror(req
, status
);
884 return tevent_req_post(req
, ev
);
887 status
= SMB_VFS_CREATE_FILE(smb1req
->conn
,
889 0, /* root_dir_fid */
893 in_create_disposition
,
896 map_smb2_oplock_levels_to_samba(requested_oplock_level
),
898 0, /* private_flags */
903 if (!NT_STATUS_IS_OK(status
)) {
904 if (open_was_deferred(smb1req
->sconn
, smb1req
->mid
)) {
907 tevent_req_nterror(req
, status
);
908 return tevent_req_post(req
, ev
);
914 * here we have op == result->op
917 DEBUG(10, ("smbd_smb2_create_send: "
918 "response construction phase\n"));
921 NTTIME last_write_time
;
923 unix_timespec_to_nt_time(&last_write_time
,
924 result
->fsp_name
->st
.st_ex_mtime
);
925 if (last_write_time
!= max_access_time
) {
927 uint32_t max_access_granted
;
928 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
930 status
= smbd_calculate_access_mask(smb1req
->conn
,
932 SEC_FLAG_MAXIMUM_ALLOWED
,
933 &max_access_granted
);
935 SIVAL(p
, 0, NT_STATUS_V(status
));
936 SIVAL(p
, 4, max_access_granted
);
938 status
= smb2_create_blob_add(state
,
940 SMB2_CREATE_TAG_MXAC
,
942 if (!NT_STATUS_IS_OK(status
)) {
943 tevent_req_nterror(req
, status
);
944 return tevent_req_post(req
, ev
);
949 if (durable_requested
&&
950 BATCH_OPLOCK_TYPE(result
->oplock_type
))
952 status
= SMB_VFS_DURABLE_COOKIE(result
,
954 &op
->global
->backend_cookie
);
955 if (!NT_STATUS_IS_OK(status
)) {
956 op
->global
->backend_cookie
= data_blob_null
;
959 if (op
->global
->backend_cookie
.length
> 0) {
962 op
->global
->durable
= true;
963 op
->global
->durable_timeout_msec
= durable_timeout_msec
;
967 op
->global
->create_guid
= create_guid
;
969 status
= smbXsrv_open_update(op
);
970 DEBUG(10, ("smb2_create_send: smbXsrv_open_update "
973 if (!NT_STATUS_IS_OK(status
)) {
974 tevent_req_nterror(req
, status
);
975 return tevent_req_post(req
, ev
);
979 if (dhnq
&& op
->global
->durable
) {
980 uint8_t p
[8] = { 0, };
981 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
983 status
= smb2_create_blob_add(state
,
985 SMB2_CREATE_TAG_DHNQ
,
987 if (!NT_STATUS_IS_OK(status
)) {
988 tevent_req_nterror(req
, status
);
989 return tevent_req_post(req
, ev
);
993 if (dh2q
&& op
->global
->durable
) {
994 uint8_t p
[8] = { 0, };
995 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
996 uint32_t durable_v2_response_flags
= 0;
998 SIVAL(p
, 0, op
->global
->durable_timeout_msec
);
999 SIVAL(p
, 4, durable_v2_response_flags
);
1001 status
= smb2_create_blob_add(state
, &out_context_blobs
,
1002 SMB2_CREATE_TAG_DH2Q
,
1004 if (!NT_STATUS_IS_OK(status
)) {
1005 tevent_req_nterror(req
, status
);
1006 return tevent_req_post(req
, ev
);
1012 uint64_t file_index
= get_FileIndex(result
->conn
,
1013 &result
->fsp_name
->st
);
1014 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
1018 /* From conversations with Microsoft engineers at
1019 the MS plugfest. The first 8 bytes are the "volume index"
1020 == inode, the second 8 bytes are the "volume id",
1021 == dev. This will be updated in the SMB2 doc. */
1022 SBVAL(p
, 0, file_index
);
1023 SIVAL(p
, 8, result
->fsp_name
->st
.st_ex_dev
);/* FileIndexHigh */
1025 status
= smb2_create_blob_add(state
, &out_context_blobs
,
1026 SMB2_CREATE_TAG_QFID
,
1028 if (!NT_STATUS_IS_OK(status
)) {
1029 tevent_req_nterror(req
, status
);
1030 return tevent_req_post(req
, ev
);
1035 smb2req
->compat_chain_fsp
= smb1req
->chain_fsp
;
1037 if(lp_fake_oplocks(SNUM(smb2req
->tcon
->compat
))) {
1038 state
->out_oplock_level
= in_oplock_level
;
1040 state
->out_oplock_level
= map_samba_oplock_levels_to_smb2(result
->oplock_type
);
1043 if ((in_create_disposition
== FILE_SUPERSEDE
)
1044 && (info
== FILE_WAS_OVERWRITTEN
)) {
1045 state
->out_create_action
= FILE_WAS_SUPERSEDED
;
1047 state
->out_create_action
= info
;
1049 state
->out_file_attributes
= dos_mode(result
->conn
,
1051 /* Deal with other possible opens having a modified
1053 ZERO_STRUCT(write_time_ts
);
1054 get_file_infos(result
->file_id
, 0, NULL
, &write_time_ts
);
1055 if (!null_timespec(write_time_ts
)) {
1056 update_stat_ex_mtime(&result
->fsp_name
->st
, write_time_ts
);
1059 state
->out_creation_ts
= get_create_timespec(smb1req
->conn
,
1060 result
, result
->fsp_name
);
1061 state
->out_last_access_ts
= result
->fsp_name
->st
.st_ex_atime
;
1062 state
->out_last_write_ts
= result
->fsp_name
->st
.st_ex_mtime
;
1063 state
->out_change_ts
= get_change_timespec(smb1req
->conn
,
1064 result
, result
->fsp_name
);
1065 state
->out_allocation_size
=
1066 SMB_VFS_GET_ALLOC_SIZE(smb1req
->conn
, result
,
1067 &(result
->fsp_name
->st
));
1068 state
->out_end_of_file
= result
->fsp_name
->st
.st_ex_size
;
1069 if (state
->out_file_attributes
== 0) {
1070 state
->out_file_attributes
= FILE_ATTRIBUTE_NORMAL
;
1072 state
->out_file_id_persistent
= result
->op
->global
->open_persistent_id
;
1073 state
->out_file_id_volatile
= result
->op
->global
->open_volatile_id
;
1074 state
->out_context_blobs
= out_context_blobs
;
1076 DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
1077 fsp_str_dbg(result
), fsp_fnum_dbg(result
)));
1079 tevent_req_done(req
);
1080 return tevent_req_post(req
, ev
);
1083 static NTSTATUS
smbd_smb2_create_recv(struct tevent_req
*req
,
1084 TALLOC_CTX
*mem_ctx
,
1085 uint8_t *out_oplock_level
,
1086 uint32_t *out_create_action
,
1087 struct timespec
*out_creation_ts
,
1088 struct timespec
*out_last_access_ts
,
1089 struct timespec
*out_last_write_ts
,
1090 struct timespec
*out_change_ts
,
1091 uint64_t *out_allocation_size
,
1092 uint64_t *out_end_of_file
,
1093 uint32_t *out_file_attributes
,
1094 uint64_t *out_file_id_persistent
,
1095 uint64_t *out_file_id_volatile
,
1096 struct smb2_create_blobs
*out_context_blobs
)
1099 struct smbd_smb2_create_state
*state
= tevent_req_data(req
,
1100 struct smbd_smb2_create_state
);
1102 if (tevent_req_is_nterror(req
, &status
)) {
1103 tevent_req_received(req
);
1107 *out_oplock_level
= state
->out_oplock_level
;
1108 *out_create_action
= state
->out_create_action
;
1109 *out_creation_ts
= state
->out_creation_ts
;
1110 *out_last_access_ts
= state
->out_last_access_ts
;
1111 *out_last_write_ts
= state
->out_last_write_ts
;
1112 *out_change_ts
= state
->out_change_ts
;
1113 *out_allocation_size
= state
->out_allocation_size
;
1114 *out_end_of_file
= state
->out_end_of_file
;
1115 *out_file_attributes
= state
->out_file_attributes
;
1116 *out_file_id_persistent
= state
->out_file_id_persistent
;
1117 *out_file_id_volatile
= state
->out_file_id_volatile
;
1118 *out_context_blobs
= state
->out_context_blobs
;
1120 talloc_steal(mem_ctx
, state
->out_context_blobs
.blobs
);
1122 tevent_req_received(req
);
1123 return NT_STATUS_OK
;
1126 /*********************************************************
1127 Code for dealing with deferred opens.
1128 *********************************************************/
1130 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request
*smb2req
,
1131 struct timeval
*p_request_time
,
1134 struct smbd_smb2_create_state
*state
= NULL
;
1135 struct tevent_req
*req
= NULL
;
1140 req
= smb2req
->subreq
;
1144 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
1148 if (!state
->open_was_deferred
) {
1151 if (p_request_time
) {
1152 *p_request_time
= state
->request_time
;
1155 *pp_state
= (void *)state
->private_data
.data
;
1160 /*********************************************************
1161 Re-process this call early - requested by message or
1163 *********************************************************/
1165 static struct smbd_smb2_request
*find_open_smb2req(
1166 struct smbd_server_connection
*sconn
, uint64_t mid
)
1168 struct smbd_smb2_request
*smb2req
;
1170 for (smb2req
= sconn
->smb2
.requests
; smb2req
; smb2req
= smb2req
->next
) {
1171 uint64_t message_id
;
1172 if (smb2req
->subreq
== NULL
) {
1173 /* This message has been processed. */
1176 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1177 /* This message has been processed. */
1180 message_id
= get_mid_from_smb2req(smb2req
);
1181 if (message_id
== mid
) {
1188 bool open_was_deferred_smb2(struct smbd_server_connection
*sconn
, uint64_t mid
)
1190 struct smbd_smb2_create_state
*state
= NULL
;
1191 struct smbd_smb2_request
*smb2req
;
1193 smb2req
= find_open_smb2req(sconn
, mid
);
1196 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
1197 (unsigned long long)mid
));
1200 if (!smb2req
->subreq
) {
1203 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1206 state
= tevent_req_data(smb2req
->subreq
,
1207 struct smbd_smb2_create_state
);
1211 /* It's not in progress if there's no timeout event. */
1212 if (!state
->open_was_deferred
) {
1216 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
1217 (unsigned long long)mid
));
1222 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request
*smb2req
,
1225 struct smbd_smb2_create_state
*state
= NULL
;
1227 if (!smb2req
->subreq
) {
1230 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1233 state
= tevent_req_data(smb2req
->subreq
,
1234 struct smbd_smb2_create_state
);
1239 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
1241 (unsigned long long)mid
));
1243 state
->open_was_deferred
= false;
1244 /* Ensure we don't have any outstanding timer event. */
1245 TALLOC_FREE(state
->te
);
1246 /* Ensure we don't have any outstanding immediate event. */
1247 TALLOC_FREE(state
->im
);
1250 void remove_deferred_open_message_smb2(
1251 struct smbd_server_connection
*sconn
, uint64_t mid
)
1253 struct smbd_smb2_request
*smb2req
;
1255 smb2req
= find_open_smb2req(sconn
, mid
);
1258 DEBUG(10,("remove_deferred_open_message_smb2: "
1259 "can't find mid %llu\n",
1260 (unsigned long long)mid
));
1263 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
1266 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context
*ctx
,
1267 struct tevent_immediate
*im
,
1270 struct smbd_smb2_request
*smb2req
= talloc_get_type_abort(private_data
,
1271 struct smbd_smb2_request
);
1272 struct smbd_server_connection
*sconn
= smb2req
->sconn
;
1273 uint64_t mid
= get_mid_from_smb2req(smb2req
);
1276 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1277 "re-dispatching mid %llu\n",
1278 (unsigned long long)mid
));
1280 status
= smbd_smb2_request_dispatch(smb2req
);
1281 if (!NT_STATUS_IS_OK(status
)) {
1282 smbd_server_connection_terminate(sconn
, nt_errstr(status
));
1287 bool schedule_deferred_open_message_smb2(
1288 struct smbd_server_connection
*sconn
, uint64_t mid
)
1290 struct smbd_smb2_create_state
*state
= NULL
;
1291 struct smbd_smb2_request
*smb2req
;
1293 smb2req
= find_open_smb2req(sconn
, mid
);
1296 DEBUG(10,("schedule_deferred_open_message_smb2: "
1297 "can't find mid %llu\n",
1298 (unsigned long long)mid
));
1301 if (!smb2req
->subreq
) {
1304 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1307 state
= tevent_req_data(smb2req
->subreq
,
1308 struct smbd_smb2_create_state
);
1313 /* Ensure we don't have any outstanding timer event. */
1314 TALLOC_FREE(state
->te
);
1315 /* Ensure we don't have any outstanding immediate event. */
1316 TALLOC_FREE(state
->im
);
1319 * This is subtle. We must null out the callback
1320 * before rescheduling, else the first call to
1321 * tevent_req_nterror() causes the _receive()
1322 * function to be called, this causing tevent_req_post()
1325 tevent_req_set_callback(smb2req
->subreq
, NULL
, NULL
);
1327 state
->im
= tevent_create_immediate(smb2req
);
1329 smbd_server_connection_terminate(smb2req
->sconn
,
1330 nt_errstr(NT_STATUS_NO_MEMORY
));
1334 DEBUG(10,("schedule_deferred_open_message_smb2: "
1335 "re-processing mid %llu\n",
1336 (unsigned long long)mid
));
1338 tevent_schedule_immediate(state
->im
,
1339 smb2req
->sconn
->ev_ctx
,
1340 smbd_smb2_create_request_dispatch_immediate
,
1346 /*********************************************************
1347 Re-process this call.
1348 *********************************************************/
1350 static void smb2_deferred_open_timer(struct event_context
*ev
,
1351 struct timed_event
*te
,
1352 struct timeval _tval
,
1356 struct smbd_smb2_create_state
*state
= NULL
;
1357 struct smbd_smb2_request
*smb2req
= talloc_get_type(private_data
,
1358 struct smbd_smb2_request
);
1360 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1361 smb2req
->current_idx
,
1362 tevent_req_default_print(smb2req
->subreq
, talloc_tos()) ));
1364 state
= tevent_req_data(smb2req
->subreq
,
1365 struct smbd_smb2_create_state
);
1370 * Null this out, don't talloc_free. It will
1371 * be talloc_free'd by the tevent library when
1375 /* Ensure we don't have any outstanding immediate event. */
1376 TALLOC_FREE(state
->im
);
1379 * This is subtle. We must null out the callback
1380 * before rescheduling, else the first call to
1381 * tevent_req_nterror() causes the _receive()
1382 * function to be called, this causing tevent_req_post()
1385 tevent_req_set_callback(smb2req
->subreq
, NULL
, NULL
);
1387 status
= smbd_smb2_request_dispatch(smb2req
);
1389 if (!NT_STATUS_IS_OK(status
)) {
1390 smbd_server_connection_terminate(smb2req
->sconn
,
1395 static bool smbd_smb2_create_cancel(struct tevent_req
*req
)
1397 struct smbd_smb2_request
*smb2req
= NULL
;
1398 struct smbd_smb2_create_state
*state
= tevent_req_data(req
,
1399 struct smbd_smb2_create_state
);
1406 if (!state
->smb2req
) {
1410 smb2req
= state
->smb2req
;
1411 mid
= get_mid_from_smb2req(smb2req
);
1413 if (is_deferred_open_async(state
->private_data
.data
)) {
1414 /* Can't cancel an async create. */
1418 remove_deferred_open_entry(state
->id
, mid
,
1419 messaging_server_id(smb2req
->sconn
->msg_ctx
));
1420 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
1422 tevent_req_defer_callback(req
, smb2req
->sconn
->ev_ctx
);
1423 tevent_req_nterror(req
, NT_STATUS_CANCELLED
);
1427 bool push_deferred_open_message_smb2(struct smbd_smb2_request
*smb2req
,
1428 struct timeval request_time
,
1429 struct timeval timeout
,
1434 struct tevent_req
*req
= NULL
;
1435 struct smbd_smb2_create_state
*state
= NULL
;
1436 struct timeval end_time
;
1441 req
= smb2req
->subreq
;
1445 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
1450 state
->request_time
= request_time
;
1451 state
->private_data
= data_blob_talloc(state
, private_data
,
1453 if (!state
->private_data
.data
) {
1457 /* Re-schedule us to retry on timer expiry. */
1458 end_time
= timeval_sum(&request_time
, &timeout
);
1460 DEBUG(10,("push_deferred_open_message_smb2: "
1462 timeval_string(talloc_tos(),
1466 state
->open_was_deferred
= true;
1467 state
->te
= tevent_add_timer(smb2req
->sconn
->ev_ctx
,
1470 smb2_deferred_open_timer
,
1476 /* allow this request to be canceled */
1477 tevent_req_set_cancel_fn(req
, smbd_smb2_create_cancel
);