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
) {
61 return SMB2_OPLOCK_LEVEL_II
;
63 return SMB2_OPLOCK_LEVEL_NONE
;
67 static struct tevent_req
*smbd_smb2_create_send(TALLOC_CTX
*mem_ctx
,
68 struct tevent_context
*ev
,
69 struct smbd_smb2_request
*smb2req
,
70 uint8_t in_oplock_level
,
71 uint32_t in_impersonation_level
,
72 uint32_t in_desired_access
,
73 uint32_t in_file_attributes
,
74 uint32_t in_share_access
,
75 uint32_t in_create_disposition
,
76 uint32_t in_create_options
,
78 struct smb2_create_blobs in_context_blobs
);
79 static NTSTATUS
smbd_smb2_create_recv(struct tevent_req
*req
,
81 uint8_t *out_oplock_level
,
82 uint32_t *out_create_action
,
83 struct timespec
*out_creation_ts
,
84 struct timespec
*out_last_access_ts
,
85 struct timespec
*out_last_write_ts
,
86 struct timespec
*out_change_ts
,
87 uint64_t *out_allocation_size
,
88 uint64_t *out_end_of_file
,
89 uint32_t *out_file_attributes
,
90 uint64_t *out_file_id_persistent
,
91 uint64_t *out_file_id_volatile
,
92 struct smb2_create_blobs
*out_context_blobs
);
94 static void smbd_smb2_request_create_done(struct tevent_req
*tsubreq
);
95 NTSTATUS
smbd_smb2_request_process_create(struct smbd_smb2_request
*smb2req
)
97 const uint8_t *inbody
;
98 const struct iovec
*indyniov
;
99 uint8_t in_oplock_level
;
100 uint32_t in_impersonation_level
;
101 uint32_t in_desired_access
;
102 uint32_t in_file_attributes
;
103 uint32_t in_share_access
;
104 uint32_t in_create_disposition
;
105 uint32_t in_create_options
;
106 uint16_t in_name_offset
;
107 uint16_t in_name_length
;
108 DATA_BLOB in_name_buffer
;
109 char *in_name_string
;
110 size_t in_name_string_size
;
111 uint32_t name_offset
= 0;
112 uint32_t name_available_length
= 0;
113 uint32_t in_context_offset
;
114 uint32_t in_context_length
;
115 DATA_BLOB in_context_buffer
;
116 struct smb2_create_blobs in_context_blobs
;
117 uint32_t context_offset
= 0;
118 uint32_t context_available_length
= 0;
122 struct tevent_req
*tsubreq
;
124 status
= smbd_smb2_request_verify_sizes(smb2req
, 0x39);
125 if (!NT_STATUS_IS_OK(status
)) {
126 return smbd_smb2_request_error(smb2req
, status
);
128 inbody
= SMBD_SMB2_IN_BODY_PTR(smb2req
);
130 in_oplock_level
= CVAL(inbody
, 0x03);
131 in_impersonation_level
= IVAL(inbody
, 0x04);
132 in_desired_access
= IVAL(inbody
, 0x18);
133 in_file_attributes
= IVAL(inbody
, 0x1C);
134 in_share_access
= IVAL(inbody
, 0x20);
135 in_create_disposition
= IVAL(inbody
, 0x24);
136 in_create_options
= IVAL(inbody
, 0x28);
137 in_name_offset
= SVAL(inbody
, 0x2C);
138 in_name_length
= SVAL(inbody
, 0x2E);
139 in_context_offset
= IVAL(inbody
, 0x30);
140 in_context_length
= IVAL(inbody
, 0x34);
143 * First check if the dynamic name and context buffers
144 * are correctly specified.
146 * Note: That we don't check if the name and context buffers
150 dyn_offset
= SMB2_HDR_BODY
+ SMBD_SMB2_IN_BODY_LEN(smb2req
);
152 if (in_name_offset
== 0 && in_name_length
== 0) {
155 } else if (in_name_offset
< dyn_offset
) {
156 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
158 name_offset
= in_name_offset
- dyn_offset
;
161 indyniov
= SMBD_SMB2_IN_DYN_IOV(smb2req
);
163 if (name_offset
> indyniov
->iov_len
) {
164 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
167 name_available_length
= indyniov
->iov_len
- name_offset
;
169 if (in_name_length
> name_available_length
) {
170 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
173 in_name_buffer
.data
= (uint8_t *)indyniov
->iov_base
+ name_offset
;
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
> indyniov
->iov_len
) {
186 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
189 context_available_length
= indyniov
->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 *)indyniov
->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
);
209 return smbd_smb2_request_error(smb2req
, NT_STATUS_ILLEGAL_CHARACTER
);
212 if (in_name_buffer
.length
== 0) {
213 in_name_string_size
= 0;
216 if (strlen(in_name_string
) != in_name_string_size
) {
217 return smbd_smb2_request_error(smb2req
, NT_STATUS_OBJECT_NAME_INVALID
);
220 ZERO_STRUCT(in_context_blobs
);
221 status
= smb2_create_blob_parse(smb2req
, in_context_buffer
, &in_context_blobs
);
222 if (!NT_STATUS_IS_OK(status
)) {
223 return smbd_smb2_request_error(smb2req
, status
);
226 tsubreq
= smbd_smb2_create_send(smb2req
,
227 smb2req
->sconn
->ev_ctx
,
230 in_impersonation_level
,
234 in_create_disposition
,
238 if (tsubreq
== NULL
) {
239 smb2req
->subreq
= NULL
;
240 return smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
242 tevent_req_set_callback(tsubreq
, smbd_smb2_request_create_done
, smb2req
);
245 * For now we keep the logic that we do not send STATUS_PENDING
246 * for sharing violations, so we just wait 2 seconds.
248 * TODO: we need more tests for this.
250 return smbd_smb2_request_pending_queue(smb2req
, tsubreq
, 2000000);
253 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request
*smb2req
)
255 uint8_t *reqhdr
= SMBD_SMB2_OUT_HDR_PTR(smb2req
);
256 return BVAL(reqhdr
, SMB2_HDR_MESSAGE_ID
);
259 static void smbd_smb2_request_create_done(struct tevent_req
*tsubreq
)
261 struct smbd_smb2_request
*smb2req
= tevent_req_callback_data(tsubreq
,
262 struct smbd_smb2_request
);
265 uint8_t out_oplock_level
= 0;
266 uint32_t out_create_action
= 0;
267 connection_struct
*conn
= smb2req
->tcon
->compat
;
268 struct timespec out_creation_ts
= { 0, };
269 struct timespec out_last_access_ts
= { 0, };
270 struct timespec out_last_write_ts
= { 0, };
271 struct timespec out_change_ts
= { 0, };
272 uint64_t out_allocation_size
= 0;
273 uint64_t out_end_of_file
= 0;
274 uint32_t out_file_attributes
= 0;
275 uint64_t out_file_id_persistent
= 0;
276 uint64_t out_file_id_volatile
= 0;
277 struct smb2_create_blobs out_context_blobs
;
278 DATA_BLOB out_context_buffer
;
279 uint16_t out_context_buffer_offset
= 0;
281 NTSTATUS error
; /* transport error */
283 status
= smbd_smb2_create_recv(tsubreq
,
291 &out_allocation_size
,
293 &out_file_attributes
,
294 &out_file_id_persistent
,
295 &out_file_id_volatile
,
297 if (!NT_STATUS_IS_OK(status
)) {
298 error
= smbd_smb2_request_error(smb2req
, status
);
299 if (!NT_STATUS_IS_OK(error
)) {
300 smbd_server_connection_terminate(smb2req
->sconn
,
307 status
= smb2_create_blob_push(smb2req
, &out_context_buffer
, out_context_blobs
);
308 if (!NT_STATUS_IS_OK(status
)) {
309 error
= smbd_smb2_request_error(smb2req
, status
);
310 if (!NT_STATUS_IS_OK(error
)) {
311 smbd_server_connection_terminate(smb2req
->sconn
,
318 if (out_context_buffer
.length
> 0) {
319 out_context_buffer_offset
= SMB2_HDR_BODY
+ 0x58;
322 outbody
= smbd_smb2_generate_outbody(smb2req
, 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 put_long_date_timespec(conn
->ts_res
,
340 (char *)outbody
.data
+ 0x08,
341 out_creation_ts
); /* creation time */
342 put_long_date_timespec(conn
->ts_res
,
343 (char *)outbody
.data
+ 0x10,
344 out_last_access_ts
); /* last access time */
345 put_long_date_timespec(conn
->ts_res
,
346 (char *)outbody
.data
+ 0x18,
347 out_last_write_ts
); /* last write time */
348 put_long_date_timespec(conn
->ts_res
,
349 (char *)outbody
.data
+ 0x20,
350 out_change_ts
); /* change time */
351 SBVAL(outbody
.data
, 0x28,
352 out_allocation_size
); /* allocation size */
353 SBVAL(outbody
.data
, 0x30,
354 out_end_of_file
); /* end of file */
355 SIVAL(outbody
.data
, 0x38,
356 out_file_attributes
); /* file attributes */
357 SIVAL(outbody
.data
, 0x3C, 0); /* reserved */
358 SBVAL(outbody
.data
, 0x40,
359 out_file_id_persistent
); /* file id (persistent) */
360 SBVAL(outbody
.data
, 0x48,
361 out_file_id_volatile
); /* file id (volatile) */
362 SIVAL(outbody
.data
, 0x50,
363 out_context_buffer_offset
); /* create contexts offset */
364 SIVAL(outbody
.data
, 0x54,
365 out_context_buffer
.length
); /* create contexts length */
367 outdyn
= out_context_buffer
;
369 error
= smbd_smb2_request_done(smb2req
, outbody
, &outdyn
);
370 if (!NT_STATUS_IS_OK(error
)) {
371 smbd_server_connection_terminate(smb2req
->sconn
,
377 struct smbd_smb2_create_state
{
378 struct smbd_smb2_request
*smb2req
;
379 struct smb_request
*smb1req
;
380 bool open_was_deferred
;
381 struct tevent_timer
*te
;
382 struct tevent_immediate
*im
;
383 struct timeval request_time
;
385 struct deferred_open_record
*open_rec
;
386 uint8_t out_oplock_level
;
387 uint32_t out_create_action
;
388 struct timespec out_creation_ts
;
389 struct timespec out_last_access_ts
;
390 struct timespec out_last_write_ts
;
391 struct timespec out_change_ts
;
392 uint64_t out_allocation_size
;
393 uint64_t out_end_of_file
;
394 uint32_t out_file_attributes
;
395 uint64_t out_file_id_persistent
;
396 uint64_t out_file_id_volatile
;
397 struct smb2_create_blobs out_context_blobs
;
400 static struct tevent_req
*smbd_smb2_create_send(TALLOC_CTX
*mem_ctx
,
401 struct tevent_context
*ev
,
402 struct smbd_smb2_request
*smb2req
,
403 uint8_t in_oplock_level
,
404 uint32_t in_impersonation_level
,
405 uint32_t in_desired_access
,
406 uint32_t in_file_attributes
,
407 uint32_t in_share_access
,
408 uint32_t in_create_disposition
,
409 uint32_t in_create_options
,
411 struct smb2_create_blobs in_context_blobs
)
413 struct tevent_req
*req
= NULL
;
414 struct smbd_smb2_create_state
*state
= NULL
;
416 struct smb_request
*smb1req
= NULL
;
417 files_struct
*result
= NULL
;
419 struct smb2_create_blobs out_context_blobs
;
420 int requested_oplock_level
;
421 struct smb2_create_blob
*dhnc
= NULL
;
422 struct smb2_create_blob
*dh2c
= NULL
;
423 struct smb2_create_blob
*dhnq
= NULL
;
424 struct smb2_create_blob
*dh2q
= NULL
;
425 struct smbXsrv_open
*op
= NULL
;
427 ZERO_STRUCT(out_context_blobs
);
429 if(lp_fake_oplocks(SNUM(smb2req
->tcon
->compat
))) {
430 requested_oplock_level
= SMB2_OPLOCK_LEVEL_NONE
;
432 requested_oplock_level
= in_oplock_level
;
436 if (smb2req
->subreq
== NULL
) {
437 /* New create call. */
438 req
= tevent_req_create(mem_ctx
, &state
,
439 struct smbd_smb2_create_state
);
443 state
->smb2req
= smb2req
;
445 smb1req
= smbd_smb2_fake_smb_request(smb2req
);
446 if (tevent_req_nomem(smb1req
, req
)) {
447 return tevent_req_post(req
, ev
);
449 state
->smb1req
= smb1req
;
450 smb2req
->subreq
= req
;
451 DEBUG(10,("smbd_smb2_create: name[%s]\n",
454 /* Re-entrant create call. */
455 req
= smb2req
->subreq
;
456 state
= tevent_req_data(req
,
457 struct smbd_smb2_create_state
);
458 smb1req
= state
->smb1req
;
459 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
463 dhnq
= smb2_create_blob_find(&in_context_blobs
,
464 SMB2_CREATE_TAG_DHNQ
);
465 dhnc
= smb2_create_blob_find(&in_context_blobs
,
466 SMB2_CREATE_TAG_DHNC
);
467 dh2q
= smb2_create_blob_find(&in_context_blobs
,
468 SMB2_CREATE_TAG_DH2Q
);
469 dh2c
= smb2_create_blob_find(&in_context_blobs
,
470 SMB2_CREATE_TAG_DH2C
);
472 if ((dhnc
&& dh2c
) || (dhnc
&& dh2q
) || (dh2c
&& dhnq
) ||
475 /* not both are allowed at the same time */
476 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
477 return tevent_req_post(req
, ev
);
481 uint32_t num_blobs_allowed
;
483 if (dhnc
->data
.length
!= 16) {
484 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
485 return tevent_req_post(req
, ev
);
489 * According to MS-SMB2: 3.3.5.9.7, "Handling the
490 * SMB2_CREATE_DURABLE_HANDLE_RECONNECT Create Context",
491 * we should ignore an additional dhnq blob, but fail
492 * the request (with status OBJECT_NAME_NOT_FOUND) if
493 * any other extra create blob has been provided.
495 * (Note that the cases of an additional dh2q or dh2c blob
496 * which require a different error code, have been treated
500 * This is only true for the oplock case:
501 * For leases, lease request is required additionally.
505 num_blobs_allowed
= 2;
507 num_blobs_allowed
= 1;
510 if (in_context_blobs
.num_blobs
!= num_blobs_allowed
) {
511 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
512 return tevent_req_post(req
, ev
);
517 uint32_t num_blobs_allowed
;
519 if (dh2c
->data
.length
!= 36) {
520 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
521 return tevent_req_post(req
, ev
);
525 * According to MS-SMB2: 3.3.5.9.12, "Handling the
526 * SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 Create Context",
527 * we should fail the request with status
528 * OBJECT_NAME_NOT_FOUND if any other create blob has been
531 * (Note that the cases of an additional dhnq, dhnc or dh2q
532 * blob which require a different error code, have been
536 * This is only true for the oplock case:
537 * For leases, lease request is required additionally!
540 num_blobs_allowed
= 1;
542 if (in_context_blobs
.num_blobs
!= num_blobs_allowed
) {
543 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
544 return tevent_req_post(req
, ev
);
548 if (IS_IPC(smb1req
->conn
)) {
549 const char *pipe_name
= in_name
;
552 /* durable handles are not supported on IPC$ */
553 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
554 return tevent_req_post(req
, ev
);
557 if (!lp_nt_pipe_support()) {
558 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
559 return tevent_req_post(req
, ev
);
562 status
= open_np_file(smb1req
, pipe_name
, &result
);
563 if (!NT_STATUS_IS_OK(status
)) {
564 tevent_req_nterror(req
, status
);
565 return tevent_req_post(req
, ev
);
567 info
= FILE_WAS_OPENED
;
568 } else if (CAN_PRINT(smb1req
->conn
)) {
570 /* durable handles are not supported on printers */
571 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
572 return tevent_req_post(req
, ev
);
575 status
= file_new(smb1req
, smb1req
->conn
, &result
);
576 if(!NT_STATUS_IS_OK(status
)) {
577 tevent_req_nterror(req
, status
);
578 return tevent_req_post(req
, ev
);
581 status
= print_spool_open(result
, in_name
,
583 if (!NT_STATUS_IS_OK(status
)) {
584 file_free(smb1req
, result
);
585 tevent_req_nterror(req
, status
);
586 return tevent_req_post(req
, ev
);
588 info
= FILE_WAS_CREATED
;
591 struct smb2_create_blob
*exta
= NULL
;
592 struct ea_list
*ea_list
= NULL
;
593 struct smb2_create_blob
*mxac
= NULL
;
594 NTTIME max_access_time
= 0;
595 struct smb2_create_blob
*secd
= NULL
;
596 struct security_descriptor
*sec_desc
= NULL
;
597 struct smb2_create_blob
*alsi
= NULL
;
598 uint64_t allocation_size
= 0;
599 struct smb2_create_blob
*twrp
= NULL
;
600 struct smb2_create_blob
*qfid
= NULL
;
601 struct GUID _create_guid
= GUID_zero();
602 struct GUID
*create_guid
= NULL
;
603 bool update_open
= false;
604 bool durable_requested
= false;
605 uint32_t durable_timeout_msec
= 0;
606 bool do_durable_reconnect
= false;
607 uint64_t persistent_id
= 0;
609 exta
= smb2_create_blob_find(&in_context_blobs
,
610 SMB2_CREATE_TAG_EXTA
);
611 mxac
= smb2_create_blob_find(&in_context_blobs
,
612 SMB2_CREATE_TAG_MXAC
);
613 secd
= smb2_create_blob_find(&in_context_blobs
,
614 SMB2_CREATE_TAG_SECD
);
615 alsi
= smb2_create_blob_find(&in_context_blobs
,
616 SMB2_CREATE_TAG_ALSI
);
617 twrp
= smb2_create_blob_find(&in_context_blobs
,
618 SMB2_CREATE_TAG_TWRP
);
619 qfid
= smb2_create_blob_find(&in_context_blobs
,
620 SMB2_CREATE_TAG_QFID
);
622 fname
= talloc_strdup(state
, in_name
);
623 if (tevent_req_nomem(fname
, req
)) {
624 return tevent_req_post(req
, ev
);
628 ea_list
= read_nttrans_ea_list(mem_ctx
,
629 (const char *)exta
->data
.data
, exta
->data
.length
);
631 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
632 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
633 return tevent_req_post(req
, ev
);
636 if (ea_list_has_invalid_name(ea_list
)) {
637 tevent_req_nterror(req
, STATUS_INVALID_EA_NAME
);
638 return tevent_req_post(req
, ev
);
643 if (mxac
->data
.length
== 0) {
645 } else if (mxac
->data
.length
== 8) {
646 max_access_time
= BVAL(mxac
->data
.data
, 0);
648 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
649 return tevent_req_post(req
, ev
);
654 enum ndr_err_code ndr_err
;
656 sec_desc
= talloc_zero(state
, struct security_descriptor
);
657 if (tevent_req_nomem(sec_desc
, req
)) {
658 return tevent_req_post(req
, ev
);
661 ndr_err
= ndr_pull_struct_blob(&secd
->data
,
663 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
664 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
665 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
666 ndr_errstr(ndr_err
)));
667 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
668 return tevent_req_post(req
, ev
);
673 if (dhnq
->data
.length
!= 16) {
674 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
675 return tevent_req_post(req
, ev
);
679 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
680 return tevent_req_post(req
, ev
);
684 * durable handle request is processed below.
686 durable_requested
= true;
688 * Set the timeout to 16 mins.
690 * TODO: test this against Windows 2012
691 * as the default for durable v2 is 1 min.
693 durable_timeout_msec
= (16*60*1000);
697 const uint8_t *p
= dh2q
->data
.data
;
698 uint32_t durable_v2_timeout
= 0;
699 DATA_BLOB create_guid_blob
;
701 if (dh2q
->data
.length
!= 32) {
702 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
703 return tevent_req_post(req
, ev
);
707 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
708 return tevent_req_post(req
, ev
);
711 durable_v2_timeout
= IVAL(p
, 0);
712 create_guid_blob
= data_blob_const(p
+ 16, 16);
714 status
= GUID_from_ndr_blob(&create_guid_blob
,
716 if (tevent_req_nterror(req
, status
)) {
717 return tevent_req_post(req
, ev
);
719 create_guid
= &_create_guid
;
721 * we need to store the create_guid later
726 * durable handle v2 request processed below
728 durable_requested
= true;
729 durable_timeout_msec
= durable_v2_timeout
;
730 if (durable_timeout_msec
== 0) {
732 * Set the timeout to 1 min as default.
734 * This matches Windows 2012.
736 durable_timeout_msec
= (60*1000);
741 persistent_id
= BVAL(dhnc
->data
.data
, 0);
743 do_durable_reconnect
= true;
747 const uint8_t *p
= dh2c
->data
.data
;
748 DATA_BLOB create_guid_blob
;
750 persistent_id
= BVAL(p
, 0);
751 create_guid_blob
= data_blob_const(p
+ 16, 16);
753 status
= GUID_from_ndr_blob(&create_guid_blob
,
755 if (tevent_req_nterror(req
, status
)) {
756 return tevent_req_post(req
, ev
);
758 create_guid
= &_create_guid
;
760 do_durable_reconnect
= true;
764 if (alsi
->data
.length
!= 8) {
765 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
766 return tevent_req_post(req
, ev
);
768 allocation_size
= BVAL(alsi
->data
.data
, 0);
776 if (twrp
->data
.length
!= 8) {
777 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
778 return tevent_req_post(req
, ev
);
781 nttime
= BVAL(twrp
->data
.data
, 0);
782 t
= nt_time_to_unix(nttime
);
786 fname
= talloc_asprintf(state
,
787 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
795 if (tevent_req_nomem(fname
, req
)) {
796 return tevent_req_post(req
, ev
);
801 if (qfid
->data
.length
!= 0) {
802 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
803 return tevent_req_post(req
, ev
);
807 /* these are ignored for SMB2 */
808 in_create_options
&= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
809 in_create_options
&= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
811 in_file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
813 DEBUG(10, ("smbd_smb2_create_send: open execution phase\n"));
816 * For the backend file open procedure, there are
817 * two possible modes: durable_reconnect or not.
819 if (do_durable_reconnect
) {
820 DATA_BLOB new_cookie
= data_blob_null
;
821 NTTIME now
= timeval_to_nttime(&smb2req
->request_time
);
823 status
= smb2srv_open_recreate(smb2req
->sconn
->conn
,
824 smb1req
->conn
->session_info
,
825 persistent_id
, create_guid
,
827 if (!NT_STATUS_IS_OK(status
)) {
828 DEBUG(3, ("smbd_smb2_create_send: "
829 "smb2srv_open_recreate failed: %s\n",
831 tevent_req_nterror(req
, status
);
832 return tevent_req_post(req
, ev
);
835 DEBUG(10, ("smb2_create_send: %s to recreate the "
836 "smb2srv_open struct for a durable handle.\n",
837 op
->global
->durable
? "succeded" : "failed"));
839 if (!op
->global
->durable
) {
841 tevent_req_nterror(req
,
842 NT_STATUS_OBJECT_NAME_NOT_FOUND
);
843 return tevent_req_post(req
, ev
);
846 status
= SMB_VFS_DURABLE_RECONNECT(smb1req
->conn
,
848 op
, /* smbXsrv_open input */
849 op
->global
->backend_cookie
,
851 &result
, &new_cookie
);
852 if (!NT_STATUS_IS_OK(status
)) {
853 NTSTATUS return_status
;
855 return_status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
857 DEBUG(3, ("smbd_smb2_create_send: "
858 "durable_reconnect failed: %s => %s\n",
860 nt_errstr(return_status
)));
862 tevent_req_nterror(req
, return_status
);
863 return tevent_req_post(req
, ev
);
866 data_blob_free(&op
->global
->backend_cookie
);
867 op
->global
->backend_cookie
= new_cookie
;
869 op
->status
= NT_STATUS_OK
;
870 op
->global
->disconnect_time
= 0;
872 /* save the timout for later update */
873 durable_timeout_msec
= op
->global
->durable_timeout_msec
;
877 info
= FILE_WAS_OPENED
;
879 struct smb_filename
*smb_fname
= NULL
;
882 * For a DFS path the function parse_dfs_path()
883 * will do the path processing.
886 if (!(smb1req
->flags2
& FLAGS2_DFS_PATHNAMES
)) {
887 /* convert '\\' into '/' */
888 status
= check_path_syntax(fname
);
889 if (!NT_STATUS_IS_OK(status
)) {
890 tevent_req_nterror(req
, status
);
891 return tevent_req_post(req
, ev
);
895 status
= filename_convert(req
,
897 smb1req
->flags2
& FLAGS2_DFS_PATHNAMES
,
900 NULL
, /* ppath_contains_wcards */
902 if (!NT_STATUS_IS_OK(status
)) {
903 tevent_req_nterror(req
, status
);
904 return tevent_req_post(req
, ev
);
908 * MS-SMB2: 2.2.13 SMB2 CREATE Request
909 * ImpersonationLevel ... MUST contain one of the
910 * following values. The server MUST validate this
911 * field, but otherwise ignore it.
913 * NB. The source4/torture/smb2/durable_open.c test
914 * shows this check is only done on real opens, not
915 * on durable handle-reopens.
918 if (in_impersonation_level
>
919 SMB2_IMPERSONATION_DELEGATE
) {
920 tevent_req_nterror(req
,
921 NT_STATUS_BAD_IMPERSONATION_LEVEL
);
922 return tevent_req_post(req
, ev
);
926 * We know we're going to do a local open, so now
927 * we must be protocol strict. JRA.
929 * MS-SMB2: 3.3.5.9 - Receiving an SMB2 CREATE Request
930 * If the file name length is greater than zero and the
931 * first character is a path separator character, the
932 * server MUST fail the request with
933 * STATUS_INVALID_PARAMETER.
935 if (in_name
[0] == '\\' || in_name
[0] == '/') {
936 tevent_req_nterror(req
,
937 NT_STATUS_INVALID_PARAMETER
);
938 return tevent_req_post(req
, ev
);
941 status
= SMB_VFS_CREATE_FILE(smb1req
->conn
,
943 0, /* root_dir_fid */
947 in_create_disposition
,
950 map_smb2_oplock_levels_to_samba(requested_oplock_level
),
952 0, /* private_flags */
957 if (!NT_STATUS_IS_OK(status
)) {
958 if (open_was_deferred(smb1req
->sconn
, smb1req
->mid
)) {
961 tevent_req_nterror(req
, status
);
962 return tevent_req_post(req
, ev
);
968 * here we have op == result->op
971 DEBUG(10, ("smbd_smb2_create_send: "
972 "response construction phase\n"));
975 NTTIME last_write_time
;
977 last_write_time
= unix_timespec_to_nt_time(
978 result
->fsp_name
->st
.st_ex_mtime
);
979 if (last_write_time
!= max_access_time
) {
981 uint32_t max_access_granted
;
982 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
984 status
= smbd_calculate_access_mask(smb1req
->conn
,
987 SEC_FLAG_MAXIMUM_ALLOWED
,
988 &max_access_granted
);
990 SIVAL(p
, 0, NT_STATUS_V(status
));
991 SIVAL(p
, 4, max_access_granted
);
993 status
= smb2_create_blob_add(state
,
995 SMB2_CREATE_TAG_MXAC
,
997 if (!NT_STATUS_IS_OK(status
)) {
998 tevent_req_nterror(req
, status
);
999 return tevent_req_post(req
, ev
);
1004 if (durable_requested
&&
1005 BATCH_OPLOCK_TYPE(result
->oplock_type
))
1007 status
= SMB_VFS_DURABLE_COOKIE(result
,
1009 &op
->global
->backend_cookie
);
1010 if (!NT_STATUS_IS_OK(status
)) {
1011 op
->global
->backend_cookie
= data_blob_null
;
1014 if (op
->global
->backend_cookie
.length
> 0) {
1017 op
->global
->durable
= true;
1018 op
->global
->durable_timeout_msec
= durable_timeout_msec
;
1022 op
->global
->create_guid
= _create_guid
;
1024 status
= smbXsrv_open_update(op
);
1025 DEBUG(10, ("smb2_create_send: smbXsrv_open_update "
1027 nt_errstr(status
)));
1028 if (!NT_STATUS_IS_OK(status
)) {
1029 tevent_req_nterror(req
, status
);
1030 return tevent_req_post(req
, ev
);
1034 if (dhnq
&& op
->global
->durable
) {
1035 uint8_t p
[8] = { 0, };
1036 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
1038 status
= smb2_create_blob_add(state
,
1040 SMB2_CREATE_TAG_DHNQ
,
1042 if (!NT_STATUS_IS_OK(status
)) {
1043 tevent_req_nterror(req
, status
);
1044 return tevent_req_post(req
, ev
);
1048 if (dh2q
&& op
->global
->durable
) {
1049 uint8_t p
[8] = { 0, };
1050 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
1051 uint32_t durable_v2_response_flags
= 0;
1053 SIVAL(p
, 0, op
->global
->durable_timeout_msec
);
1054 SIVAL(p
, 4, durable_v2_response_flags
);
1056 status
= smb2_create_blob_add(state
, &out_context_blobs
,
1057 SMB2_CREATE_TAG_DH2Q
,
1059 if (!NT_STATUS_IS_OK(status
)) {
1060 tevent_req_nterror(req
, status
);
1061 return tevent_req_post(req
, ev
);
1067 uint64_t file_index
= get_FileIndex(result
->conn
,
1068 &result
->fsp_name
->st
);
1069 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
1073 /* From conversations with Microsoft engineers at
1074 the MS plugfest. The first 8 bytes are the "volume index"
1075 == inode, the second 8 bytes are the "volume id",
1076 == dev. This will be updated in the SMB2 doc. */
1077 SBVAL(p
, 0, file_index
);
1078 SIVAL(p
, 8, result
->fsp_name
->st
.st_ex_dev
);/* FileIndexHigh */
1080 status
= smb2_create_blob_add(state
, &out_context_blobs
,
1081 SMB2_CREATE_TAG_QFID
,
1083 if (!NT_STATUS_IS_OK(status
)) {
1084 tevent_req_nterror(req
, status
);
1085 return tevent_req_post(req
, ev
);
1090 smb2req
->compat_chain_fsp
= smb1req
->chain_fsp
;
1092 if(lp_fake_oplocks(SNUM(smb2req
->tcon
->compat
))) {
1093 state
->out_oplock_level
= in_oplock_level
;
1095 state
->out_oplock_level
= map_samba_oplock_levels_to_smb2(result
->oplock_type
);
1098 if ((in_create_disposition
== FILE_SUPERSEDE
)
1099 && (info
== FILE_WAS_OVERWRITTEN
)) {
1100 state
->out_create_action
= FILE_WAS_SUPERSEDED
;
1102 state
->out_create_action
= info
;
1104 state
->out_file_attributes
= dos_mode(result
->conn
,
1107 state
->out_creation_ts
= get_create_timespec(smb1req
->conn
,
1108 result
, result
->fsp_name
);
1109 state
->out_last_access_ts
= result
->fsp_name
->st
.st_ex_atime
;
1110 state
->out_last_write_ts
= result
->fsp_name
->st
.st_ex_mtime
;
1111 state
->out_change_ts
= get_change_timespec(smb1req
->conn
,
1112 result
, result
->fsp_name
);
1113 state
->out_allocation_size
=
1114 SMB_VFS_GET_ALLOC_SIZE(smb1req
->conn
, result
,
1115 &(result
->fsp_name
->st
));
1116 state
->out_end_of_file
= result
->fsp_name
->st
.st_ex_size
;
1117 if (state
->out_file_attributes
== 0) {
1118 state
->out_file_attributes
= FILE_ATTRIBUTE_NORMAL
;
1120 state
->out_file_id_persistent
= result
->op
->global
->open_persistent_id
;
1121 state
->out_file_id_volatile
= result
->op
->global
->open_volatile_id
;
1122 state
->out_context_blobs
= out_context_blobs
;
1124 DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
1125 fsp_str_dbg(result
), fsp_fnum_dbg(result
)));
1127 tevent_req_done(req
);
1128 return tevent_req_post(req
, ev
);
1131 static NTSTATUS
smbd_smb2_create_recv(struct tevent_req
*req
,
1132 TALLOC_CTX
*mem_ctx
,
1133 uint8_t *out_oplock_level
,
1134 uint32_t *out_create_action
,
1135 struct timespec
*out_creation_ts
,
1136 struct timespec
*out_last_access_ts
,
1137 struct timespec
*out_last_write_ts
,
1138 struct timespec
*out_change_ts
,
1139 uint64_t *out_allocation_size
,
1140 uint64_t *out_end_of_file
,
1141 uint32_t *out_file_attributes
,
1142 uint64_t *out_file_id_persistent
,
1143 uint64_t *out_file_id_volatile
,
1144 struct smb2_create_blobs
*out_context_blobs
)
1147 struct smbd_smb2_create_state
*state
= tevent_req_data(req
,
1148 struct smbd_smb2_create_state
);
1150 if (tevent_req_is_nterror(req
, &status
)) {
1151 tevent_req_received(req
);
1155 *out_oplock_level
= state
->out_oplock_level
;
1156 *out_create_action
= state
->out_create_action
;
1157 *out_creation_ts
= state
->out_creation_ts
;
1158 *out_last_access_ts
= state
->out_last_access_ts
;
1159 *out_last_write_ts
= state
->out_last_write_ts
;
1160 *out_change_ts
= state
->out_change_ts
;
1161 *out_allocation_size
= state
->out_allocation_size
;
1162 *out_end_of_file
= state
->out_end_of_file
;
1163 *out_file_attributes
= state
->out_file_attributes
;
1164 *out_file_id_persistent
= state
->out_file_id_persistent
;
1165 *out_file_id_volatile
= state
->out_file_id_volatile
;
1166 *out_context_blobs
= state
->out_context_blobs
;
1168 talloc_steal(mem_ctx
, state
->out_context_blobs
.blobs
);
1170 tevent_req_received(req
);
1171 return NT_STATUS_OK
;
1174 /*********************************************************
1175 Code for dealing with deferred opens.
1176 *********************************************************/
1178 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request
*smb2req
,
1179 struct timeval
*p_request_time
,
1180 struct deferred_open_record
**open_rec
)
1182 struct smbd_smb2_create_state
*state
= NULL
;
1183 struct tevent_req
*req
= NULL
;
1188 req
= smb2req
->subreq
;
1192 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
1196 if (!state
->open_was_deferred
) {
1199 if (p_request_time
) {
1200 *p_request_time
= state
->request_time
;
1202 if (open_rec
!= NULL
) {
1203 *open_rec
= state
->open_rec
;
1208 /*********************************************************
1209 Re-process this call early - requested by message or
1211 *********************************************************/
1213 static struct smbd_smb2_request
*find_open_smb2req(
1214 struct smbd_server_connection
*sconn
, uint64_t mid
)
1216 struct smbd_smb2_request
*smb2req
;
1218 for (smb2req
= sconn
->smb2
.requests
; smb2req
; smb2req
= smb2req
->next
) {
1219 uint64_t message_id
;
1220 if (smb2req
->subreq
== NULL
) {
1221 /* This message has been processed. */
1224 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1225 /* This message has been processed. */
1228 message_id
= get_mid_from_smb2req(smb2req
);
1229 if (message_id
== mid
) {
1236 bool open_was_deferred_smb2(struct smbd_server_connection
*sconn
, uint64_t mid
)
1238 struct smbd_smb2_create_state
*state
= NULL
;
1239 struct smbd_smb2_request
*smb2req
;
1241 smb2req
= find_open_smb2req(sconn
, mid
);
1244 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
1245 (unsigned long long)mid
));
1248 if (!smb2req
->subreq
) {
1251 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1254 state
= tevent_req_data(smb2req
->subreq
,
1255 struct smbd_smb2_create_state
);
1259 /* It's not in progress if there's no timeout event. */
1260 if (!state
->open_was_deferred
) {
1264 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
1265 (unsigned long long)mid
));
1270 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request
*smb2req
,
1273 struct smbd_smb2_create_state
*state
= NULL
;
1275 if (!smb2req
->subreq
) {
1278 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1281 state
= tevent_req_data(smb2req
->subreq
,
1282 struct smbd_smb2_create_state
);
1287 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
1289 (unsigned long long)mid
));
1291 state
->open_was_deferred
= false;
1292 /* Ensure we don't have any outstanding timer event. */
1293 TALLOC_FREE(state
->te
);
1294 /* Ensure we don't have any outstanding immediate event. */
1295 TALLOC_FREE(state
->im
);
1298 void remove_deferred_open_message_smb2(
1299 struct smbd_server_connection
*sconn
, uint64_t mid
)
1301 struct smbd_smb2_request
*smb2req
;
1303 smb2req
= find_open_smb2req(sconn
, mid
);
1306 DEBUG(10,("remove_deferred_open_message_smb2: "
1307 "can't find mid %llu\n",
1308 (unsigned long long)mid
));
1311 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
1314 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context
*ctx
,
1315 struct tevent_immediate
*im
,
1318 struct smbd_smb2_request
*smb2req
= talloc_get_type_abort(private_data
,
1319 struct smbd_smb2_request
);
1320 struct smbd_server_connection
*sconn
= smb2req
->sconn
;
1321 uint64_t mid
= get_mid_from_smb2req(smb2req
);
1324 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1325 "re-dispatching mid %llu\n",
1326 (unsigned long long)mid
));
1328 status
= smbd_smb2_request_dispatch(smb2req
);
1329 if (!NT_STATUS_IS_OK(status
)) {
1330 smbd_server_connection_terminate(sconn
, nt_errstr(status
));
1335 bool schedule_deferred_open_message_smb2(
1336 struct smbd_server_connection
*sconn
, uint64_t mid
)
1338 struct smbd_smb2_create_state
*state
= NULL
;
1339 struct smbd_smb2_request
*smb2req
;
1341 smb2req
= find_open_smb2req(sconn
, mid
);
1344 DEBUG(10,("schedule_deferred_open_message_smb2: "
1345 "can't find mid %llu\n",
1346 (unsigned long long)mid
));
1349 if (!smb2req
->subreq
) {
1352 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1355 state
= tevent_req_data(smb2req
->subreq
,
1356 struct smbd_smb2_create_state
);
1361 /* Ensure we don't have any outstanding timer event. */
1362 TALLOC_FREE(state
->te
);
1363 /* Ensure we don't have any outstanding immediate event. */
1364 TALLOC_FREE(state
->im
);
1367 * This is subtle. We must null out the callback
1368 * before rescheduling, else the first call to
1369 * tevent_req_nterror() causes the _receive()
1370 * function to be called, this causing tevent_req_post()
1373 tevent_req_set_callback(smb2req
->subreq
, NULL
, NULL
);
1375 state
->im
= tevent_create_immediate(smb2req
);
1377 smbd_server_connection_terminate(smb2req
->sconn
,
1378 nt_errstr(NT_STATUS_NO_MEMORY
));
1382 DEBUG(10,("schedule_deferred_open_message_smb2: "
1383 "re-processing mid %llu\n",
1384 (unsigned long long)mid
));
1386 tevent_schedule_immediate(state
->im
,
1387 smb2req
->sconn
->ev_ctx
,
1388 smbd_smb2_create_request_dispatch_immediate
,
1394 static bool smbd_smb2_create_cancel(struct tevent_req
*req
)
1396 struct smbd_smb2_request
*smb2req
= NULL
;
1397 struct smbd_smb2_create_state
*state
= tevent_req_data(req
,
1398 struct smbd_smb2_create_state
);
1405 if (!state
->smb2req
) {
1409 smb2req
= state
->smb2req
;
1410 mid
= get_mid_from_smb2req(smb2req
);
1412 if (is_deferred_open_async(state
->open_rec
)) {
1413 /* Can't cancel an async create. */
1417 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
1419 tevent_req_defer_callback(req
, smb2req
->sconn
->ev_ctx
);
1420 tevent_req_nterror(req
, NT_STATUS_CANCELLED
);
1424 bool push_deferred_open_message_smb2(struct smbd_smb2_request
*smb2req
,
1425 struct timeval request_time
,
1426 struct timeval timeout
,
1428 struct deferred_open_record
*open_rec
)
1430 struct tevent_req
*req
= NULL
;
1431 struct smbd_smb2_create_state
*state
= NULL
;
1432 struct timeval end_time
;
1437 req
= smb2req
->subreq
;
1441 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
1446 state
->request_time
= request_time
;
1447 state
->open_rec
= talloc_move(state
, &open_rec
);
1449 /* Re-schedule us to retry on timer expiry. */
1450 end_time
= timeval_sum(&request_time
, &timeout
);
1452 DEBUG(10,("push_deferred_open_message_smb2: "
1454 timeval_string(talloc_tos(),
1458 state
->open_was_deferred
= true;
1460 /* allow this request to be canceled */
1461 tevent_req_set_cancel_fn(req
, smbd_smb2_create_cancel
);