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 status
= smbd_smb2_create_recv(tsubreq
,
292 &out_last_access_time
,
293 &out_last_write_time
,
295 &out_allocation_size
,
297 &out_file_attributes
,
298 &out_file_id_persistent
,
299 &out_file_id_volatile
,
301 if (!NT_STATUS_IS_OK(status
)) {
302 error
= smbd_smb2_request_error(smb2req
, status
);
303 if (!NT_STATUS_IS_OK(error
)) {
304 smbd_server_connection_terminate(smb2req
->sconn
,
311 status
= smb2_create_blob_push(smb2req
, &out_context_buffer
, out_context_blobs
);
312 if (!NT_STATUS_IS_OK(status
)) {
313 error
= smbd_smb2_request_error(smb2req
, status
);
314 if (!NT_STATUS_IS_OK(error
)) {
315 smbd_server_connection_terminate(smb2req
->sconn
,
322 if (out_context_buffer
.length
> 0) {
323 out_context_buffer_offset
= SMB2_HDR_BODY
+ 0x58;
326 outbody
= data_blob_talloc(smb2req
->out
.vector
, NULL
, 0x58);
327 if (outbody
.data
== NULL
) {
328 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
329 if (!NT_STATUS_IS_OK(error
)) {
330 smbd_server_connection_terminate(smb2req
->sconn
,
337 SSVAL(outbody
.data
, 0x00, 0x58 + 1); /* struct size */
338 SCVAL(outbody
.data
, 0x02,
339 out_oplock_level
); /* oplock level */
340 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
341 SIVAL(outbody
.data
, 0x04,
342 out_create_action
); /* create action */
343 SBVAL(outbody
.data
, 0x08,
344 out_creation_time
); /* creation time */
345 SBVAL(outbody
.data
, 0x10,
346 out_last_access_time
); /* last access time */
347 SBVAL(outbody
.data
, 0x18,
348 out_last_write_time
); /* last write time */
349 SBVAL(outbody
.data
, 0x20,
350 out_change_time
); /* 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 timed_event
*te
;
382 struct tevent_immediate
*im
;
383 struct timeval request_time
;
385 DATA_BLOB private_data
;
386 uint8_t out_oplock_level
;
387 uint32_t out_create_action
;
388 NTTIME out_creation_time
;
389 NTTIME out_last_access_time
;
390 NTTIME out_last_write_time
;
391 NTTIME out_change_time
;
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 timespec write_time_ts
;
420 struct smb2_create_blobs out_context_blobs
;
421 int requested_oplock_level
;
422 struct smb2_create_blob
*dhnc
= NULL
;
423 struct smb2_create_blob
*dh2c
= NULL
;
424 struct smbXsrv_open
*op
= NULL
;
426 ZERO_STRUCT(out_context_blobs
);
428 if(lp_fake_oplocks(SNUM(smb2req
->tcon
->compat
))) {
429 requested_oplock_level
= SMB2_OPLOCK_LEVEL_NONE
;
431 requested_oplock_level
= in_oplock_level
;
435 if (smb2req
->subreq
== NULL
) {
436 /* New create call. */
437 req
= tevent_req_create(mem_ctx
, &state
,
438 struct smbd_smb2_create_state
);
442 state
->smb2req
= smb2req
;
444 smb1req
= smbd_smb2_fake_smb_request(smb2req
);
445 if (tevent_req_nomem(smb1req
, req
)) {
446 return tevent_req_post(req
, ev
);
448 state
->smb1req
= smb1req
;
449 smb2req
->subreq
= req
;
450 DEBUG(10,("smbd_smb2_create: name[%s]\n",
453 /* Re-entrant create call. */
454 req
= smb2req
->subreq
;
455 state
= tevent_req_data(req
,
456 struct smbd_smb2_create_state
);
457 smb1req
= state
->smb1req
;
458 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
462 dhnc
= smb2_create_blob_find(&in_context_blobs
,
463 SMB2_CREATE_TAG_DHNC
);
466 if (dhnc
->data
.length
!= 16) {
467 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
468 return tevent_req_post(req
, ev
);
470 if (in_context_blobs
.num_blobs
!= 1) {
472 * DHNC should be the only one.
474 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
475 return tevent_req_post(req
, ev
);
479 dh2c
= smb2_create_blob_find(&in_context_blobs
,
480 SMB2_CREATE_TAG_DH2C
);
482 if (dh2c
->data
.length
!= 36) {
483 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
484 return tevent_req_post(req
, ev
);
486 if (in_context_blobs
.num_blobs
!= 1) {
488 * DH2C should be the only one.
490 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
491 return tevent_req_post(req
, ev
);
495 if (IS_IPC(smb1req
->conn
)) {
496 const char *pipe_name
= in_name
;
499 /* durable handles are not supported on IPC$ */
500 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
501 return tevent_req_post(req
, ev
);
504 if (!lp_nt_pipe_support()) {
505 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
506 return tevent_req_post(req
, ev
);
509 status
= open_np_file(smb1req
, pipe_name
, &result
);
510 if (!NT_STATUS_IS_OK(status
)) {
511 tevent_req_nterror(req
, status
);
512 return tevent_req_post(req
, ev
);
514 info
= FILE_WAS_OPENED
;
515 } else if (CAN_PRINT(smb1req
->conn
)) {
517 /* durable handles are not supported on printers */
518 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
519 return tevent_req_post(req
, ev
);
522 status
= file_new(smb1req
, smb1req
->conn
, &result
);
523 if(!NT_STATUS_IS_OK(status
)) {
524 tevent_req_nterror(req
, status
);
525 return tevent_req_post(req
, ev
);
528 status
= print_spool_open(result
, in_name
,
530 if (!NT_STATUS_IS_OK(status
)) {
531 file_free(smb1req
, result
);
532 tevent_req_nterror(req
, status
);
533 return tevent_req_post(req
, ev
);
535 info
= FILE_WAS_CREATED
;
538 struct smb2_create_blob
*exta
= NULL
;
539 struct ea_list
*ea_list
= NULL
;
540 struct smb2_create_blob
*mxac
= NULL
;
541 NTTIME max_access_time
= 0;
542 struct smb2_create_blob
*secd
= NULL
;
543 struct security_descriptor
*sec_desc
= NULL
;
544 struct smb2_create_blob
*dhnq
= NULL
;
545 struct smb2_create_blob
*alsi
= NULL
;
546 uint64_t allocation_size
= 0;
547 struct smb2_create_blob
*twrp
= NULL
;
548 struct smb2_create_blob
*qfid
= NULL
;
549 struct GUID create_guid
= GUID_zero();
550 bool update_open
= false;
551 bool durable_requested
= false;
552 uint32_t durable_timeout_msec
= 0;
553 bool do_durable_reconnect
= false;
554 struct smb2_create_blob
*dh2q
= NULL
;
556 exta
= smb2_create_blob_find(&in_context_blobs
,
557 SMB2_CREATE_TAG_EXTA
);
558 mxac
= smb2_create_blob_find(&in_context_blobs
,
559 SMB2_CREATE_TAG_MXAC
);
560 secd
= smb2_create_blob_find(&in_context_blobs
,
561 SMB2_CREATE_TAG_SECD
);
562 dhnq
= smb2_create_blob_find(&in_context_blobs
,
563 SMB2_CREATE_TAG_DHNQ
);
564 alsi
= smb2_create_blob_find(&in_context_blobs
,
565 SMB2_CREATE_TAG_ALSI
);
566 twrp
= smb2_create_blob_find(&in_context_blobs
,
567 SMB2_CREATE_TAG_TWRP
);
568 qfid
= smb2_create_blob_find(&in_context_blobs
,
569 SMB2_CREATE_TAG_QFID
);
570 dh2q
= smb2_create_blob_find(&in_context_blobs
,
571 SMB2_CREATE_TAG_DH2Q
);
573 fname
= talloc_strdup(state
, in_name
);
574 if (tevent_req_nomem(fname
, req
)) {
575 return tevent_req_post(req
, ev
);
579 ea_list
= read_nttrans_ea_list(mem_ctx
,
580 (const char *)exta
->data
.data
, exta
->data
.length
);
582 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
583 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
584 return tevent_req_post(req
, ev
);
587 if (ea_list_has_invalid_name(ea_list
)) {
588 tevent_req_nterror(req
, STATUS_INVALID_EA_NAME
);
589 return tevent_req_post(req
, ev
);
594 if (mxac
->data
.length
== 0) {
596 } else if (mxac
->data
.length
== 8) {
597 max_access_time
= BVAL(mxac
->data
.data
, 0);
599 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
600 return tevent_req_post(req
, ev
);
605 enum ndr_err_code ndr_err
;
607 sec_desc
= talloc_zero(state
, struct security_descriptor
);
608 if (tevent_req_nomem(sec_desc
, req
)) {
609 return tevent_req_post(req
, ev
);
612 ndr_err
= ndr_pull_struct_blob(&secd
->data
,
614 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
615 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
616 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
617 ndr_errstr(ndr_err
)));
618 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
619 return tevent_req_post(req
, ev
);
624 if (dhnq
->data
.length
!= 16) {
625 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
626 return tevent_req_post(req
, ev
);
630 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
631 return tevent_req_post(req
, ev
);
635 * durable handle request is processed below.
637 durable_requested
= true;
639 * Set the timeout to 16 mins.
641 * TODO: test this against Windows 2012
642 * as the default for durable v2 is 1 min.
644 durable_timeout_msec
= (16*60*1000);
648 const uint8_t *p
= dh2q
->data
.data
;
649 uint32_t durable_v2_timeout
= 0;
650 DATA_BLOB create_guid_blob
;
652 if (dh2q
->data
.length
!= 32) {
653 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
654 return tevent_req_post(req
, ev
);
658 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
659 return tevent_req_post(req
, ev
);
662 durable_v2_timeout
= IVAL(p
, 0);
663 create_guid_blob
= data_blob_const(p
+ 16, 16);
665 status
= GUID_from_ndr_blob(&create_guid_blob
,
667 if (tevent_req_nterror(req
, status
)) {
668 return tevent_req_post(req
, ev
);
671 * we need to store the create_guid later
676 * durable handle v2 request processed below
678 durable_requested
= true;
679 durable_timeout_msec
= durable_v2_timeout
;
680 if (durable_timeout_msec
== 0) {
682 * Set the timeout to 1 min as default.
684 * This matches Windows 2012.
686 durable_timeout_msec
= (60*1000);
691 NTTIME now
= timeval_to_nttime(&smb2req
->request_time
);
692 uint64_t persistent_id
;
694 persistent_id
= BVAL(dhnc
->data
.data
, 0);
696 status
= smb2srv_open_recreate(smb2req
->sconn
->conn
,
697 smb1req
->conn
->session_info
,
698 persistent_id
, create_guid
,
700 if (!NT_STATUS_IS_OK(status
)) {
701 DEBUG(3, ("smbd_smb2_create_send: "
702 "smb2srv_open_recreate v1 failed: %s\n",
704 tevent_req_nterror(req
, status
);
705 return tevent_req_post(req
, ev
);
708 DEBUG(10, ("smb2_create_send: DHNC: %s recreate the "
709 "smb2srv_open struct for a durable handle.\n",
710 op
->global
->durable
? "did" : "could not"));
712 if (!op
->global
->durable
) {
714 tevent_req_nterror(req
,
715 NT_STATUS_OBJECT_NAME_NOT_FOUND
);
716 return tevent_req_post(req
, ev
);
719 do_durable_reconnect
= true;
723 const uint8_t *p
= dh2c
->data
.data
;
724 NTTIME now
= timeval_to_nttime(&smb2req
->request_time
);
725 uint64_t persistent_id
;
726 DATA_BLOB create_guid_blob
;
728 persistent_id
= BVAL(p
, 0);
729 create_guid_blob
= data_blob_const(p
+ 16, 16);
731 status
= GUID_from_ndr_blob(&create_guid_blob
,
733 if (tevent_req_nterror(req
, status
)) {
734 return tevent_req_post(req
, ev
);
737 status
= smb2srv_open_recreate(smb2req
->sconn
->conn
,
738 smb1req
->conn
->session_info
,
739 persistent_id
, create_guid
,
741 if (!NT_STATUS_IS_OK(status
)) {
742 DEBUG(3, ("smbd_smb2_create_send: "
743 "smb2srv_open_recreate v2 failed: %s\n",
745 tevent_req_nterror(req
, status
);
746 return tevent_req_post(req
, ev
);
749 DEBUG(10, ("smb2_create_send: DH2C: %s recreate the "
750 "smb2srv_open struct for a durable handle.\n",
751 op
->global
->durable
? "did" : "could not"));
753 if (!op
->global
->durable
) {
755 tevent_req_nterror(req
,
756 NT_STATUS_OBJECT_NAME_NOT_FOUND
);
757 return tevent_req_post(req
, ev
);
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
;
822 status
= SMB_VFS_DURABLE_RECONNECT(smb1req
->conn
,
825 op
->global
->backend_cookie
,
826 op
, &result
, &new_cookie
);
827 if (!NT_STATUS_IS_OK(status
)) {
828 NTSTATUS return_status
;
830 return_status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
832 DEBUG(3, ("smbd_smb2_create_send: "
833 "durable_reconnect failed: %s => %s\n",
835 nt_errstr(return_status
)));
837 tevent_req_nterror(req
, return_status
);
838 return tevent_req_post(req
, ev
);
841 data_blob_free(&op
->global
->backend_cookie
);
842 op
->global
->backend_cookie
= new_cookie
;
844 op
->status
= NT_STATUS_OK
;
845 op
->global
->disconnect_time
= 0;
847 /* save the timout for later update */
848 durable_timeout_msec
= op
->global
->durable_timeout_msec
;
852 info
= FILE_WAS_OPENED
;
854 struct smb_filename
*smb_fname
= NULL
;
857 * For a DFS path the function parse_dfs_path()
858 * will do the path processing.
861 if (!(smb1req
->flags2
& FLAGS2_DFS_PATHNAMES
)) {
862 /* convert '\\' into '/' */
863 status
= check_path_syntax(fname
);
864 if (!NT_STATUS_IS_OK(status
)) {
865 tevent_req_nterror(req
, status
);
866 return tevent_req_post(req
, ev
);
870 status
= filename_convert(req
,
872 smb1req
->flags2
& FLAGS2_DFS_PATHNAMES
,
875 NULL
, /* ppath_contains_wcards */
877 if (!NT_STATUS_IS_OK(status
)) {
878 tevent_req_nterror(req
, status
);
879 return tevent_req_post(req
, ev
);
882 status
= SMB_VFS_CREATE_FILE(smb1req
->conn
,
884 0, /* root_dir_fid */
888 in_create_disposition
,
891 map_smb2_oplock_levels_to_samba(requested_oplock_level
),
893 0, /* private_flags */
898 if (!NT_STATUS_IS_OK(status
)) {
899 if (open_was_deferred(smb1req
->sconn
, smb1req
->mid
)) {
902 tevent_req_nterror(req
, status
);
903 return tevent_req_post(req
, ev
);
909 * here we have op == result->op
912 DEBUG(10, ("smbd_smb2_create_send: "
913 "response construction phase\n"));
916 NTTIME last_write_time
;
918 unix_timespec_to_nt_time(&last_write_time
,
919 result
->fsp_name
->st
.st_ex_mtime
);
920 if (last_write_time
!= max_access_time
) {
922 uint32_t max_access_granted
;
923 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
925 status
= smbd_calculate_access_mask(smb1req
->conn
,
927 SEC_FLAG_MAXIMUM_ALLOWED
,
928 &max_access_granted
);
930 SIVAL(p
, 0, NT_STATUS_V(status
));
931 SIVAL(p
, 4, max_access_granted
);
933 status
= smb2_create_blob_add(state
,
935 SMB2_CREATE_TAG_MXAC
,
937 if (!NT_STATUS_IS_OK(status
)) {
938 tevent_req_nterror(req
, status
);
939 return tevent_req_post(req
, ev
);
944 if (durable_requested
&&
945 BATCH_OPLOCK_TYPE(result
->oplock_type
))
947 status
= SMB_VFS_DURABLE_COOKIE(result
,
949 &op
->global
->backend_cookie
);
950 if (!NT_STATUS_IS_OK(status
)) {
951 op
->global
->backend_cookie
= data_blob_null
;
954 if (op
->global
->backend_cookie
.length
> 0) {
957 op
->global
->durable
= true;
958 op
->global
->durable_timeout_msec
= durable_timeout_msec
;
962 op
->global
->create_guid
= create_guid
;
964 status
= smbXsrv_open_update(op
);
965 DEBUG(10, ("smb2_create_send: smbXsrv_open_update "
968 if (!NT_STATUS_IS_OK(status
)) {
969 tevent_req_nterror(req
, status
);
970 return tevent_req_post(req
, ev
);
974 if (dhnq
&& op
->global
->durable
) {
975 uint8_t p
[8] = { 0, };
976 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
978 status
= smb2_create_blob_add(state
,
980 SMB2_CREATE_TAG_DHNQ
,
982 if (!NT_STATUS_IS_OK(status
)) {
983 tevent_req_nterror(req
, status
);
984 return tevent_req_post(req
, ev
);
988 if (dh2q
&& op
->global
->durable
) {
989 uint8_t p
[8] = { 0, };
990 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
991 uint32_t durable_v2_response_flags
= 0;
993 SIVAL(p
, 0, op
->global
->durable_timeout_msec
);
994 SIVAL(p
, 4, durable_v2_response_flags
);
996 status
= smb2_create_blob_add(state
, &out_context_blobs
,
997 SMB2_CREATE_TAG_DH2Q
,
999 if (!NT_STATUS_IS_OK(status
)) {
1000 tevent_req_nterror(req
, status
);
1001 return tevent_req_post(req
, ev
);
1007 uint64_t file_index
= get_FileIndex(result
->conn
,
1008 &result
->fsp_name
->st
);
1009 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
1013 /* From conversations with Microsoft engineers at
1014 the MS plugfest. The first 8 bytes are the "volume index"
1015 == inode, the second 8 bytes are the "volume id",
1016 == dev. This will be updated in the SMB2 doc. */
1017 SBVAL(p
, 0, file_index
);
1018 SIVAL(p
, 8, result
->fsp_name
->st
.st_ex_dev
);/* FileIndexHigh */
1020 status
= smb2_create_blob_add(state
, &out_context_blobs
,
1021 SMB2_CREATE_TAG_QFID
,
1023 if (!NT_STATUS_IS_OK(status
)) {
1024 tevent_req_nterror(req
, status
);
1025 return tevent_req_post(req
, ev
);
1030 smb2req
->compat_chain_fsp
= smb1req
->chain_fsp
;
1032 if(lp_fake_oplocks(SNUM(smb2req
->tcon
->compat
))) {
1033 state
->out_oplock_level
= in_oplock_level
;
1035 state
->out_oplock_level
= map_samba_oplock_levels_to_smb2(result
->oplock_type
);
1038 if ((in_create_disposition
== FILE_SUPERSEDE
)
1039 && (info
== FILE_WAS_OVERWRITTEN
)) {
1040 state
->out_create_action
= FILE_WAS_SUPERSEDED
;
1042 state
->out_create_action
= info
;
1044 state
->out_file_attributes
= dos_mode(result
->conn
,
1046 /* Deal with other possible opens having a modified
1048 ZERO_STRUCT(write_time_ts
);
1049 get_file_infos(result
->file_id
, 0, NULL
, &write_time_ts
);
1050 if (!null_timespec(write_time_ts
)) {
1051 update_stat_ex_mtime(&result
->fsp_name
->st
, write_time_ts
);
1054 unix_timespec_to_nt_time(&state
->out_creation_time
,
1055 get_create_timespec(smb1req
->conn
, result
,
1057 unix_timespec_to_nt_time(&state
->out_last_access_time
,
1058 result
->fsp_name
->st
.st_ex_atime
);
1059 unix_timespec_to_nt_time(&state
->out_last_write_time
,
1060 result
->fsp_name
->st
.st_ex_mtime
);
1061 unix_timespec_to_nt_time(&state
->out_change_time
,
1062 get_change_timespec(smb1req
->conn
, result
,
1064 state
->out_allocation_size
=
1065 SMB_VFS_GET_ALLOC_SIZE(smb1req
->conn
, result
,
1066 &(result
->fsp_name
->st
));
1067 state
->out_end_of_file
= result
->fsp_name
->st
.st_ex_size
;
1068 if (state
->out_file_attributes
== 0) {
1069 state
->out_file_attributes
= FILE_ATTRIBUTE_NORMAL
;
1071 state
->out_file_id_persistent
= result
->op
->global
->open_persistent_id
;
1072 state
->out_file_id_volatile
= result
->op
->global
->open_volatile_id
;
1073 state
->out_context_blobs
= out_context_blobs
;
1075 DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
1076 fsp_str_dbg(result
), fsp_fnum_dbg(result
)));
1078 tevent_req_done(req
);
1079 return tevent_req_post(req
, ev
);
1082 static NTSTATUS
smbd_smb2_create_recv(struct tevent_req
*req
,
1083 TALLOC_CTX
*mem_ctx
,
1084 uint8_t *out_oplock_level
,
1085 uint32_t *out_create_action
,
1086 NTTIME
*out_creation_time
,
1087 NTTIME
*out_last_access_time
,
1088 NTTIME
*out_last_write_time
,
1089 NTTIME
*out_change_time
,
1090 uint64_t *out_allocation_size
,
1091 uint64_t *out_end_of_file
,
1092 uint32_t *out_file_attributes
,
1093 uint64_t *out_file_id_persistent
,
1094 uint64_t *out_file_id_volatile
,
1095 struct smb2_create_blobs
*out_context_blobs
)
1098 struct smbd_smb2_create_state
*state
= tevent_req_data(req
,
1099 struct smbd_smb2_create_state
);
1101 if (tevent_req_is_nterror(req
, &status
)) {
1102 tevent_req_received(req
);
1106 *out_oplock_level
= state
->out_oplock_level
;
1107 *out_create_action
= state
->out_create_action
;
1108 *out_creation_time
= state
->out_creation_time
;
1109 *out_last_access_time
= state
->out_last_access_time
;
1110 *out_last_write_time
= state
->out_last_write_time
;
1111 *out_change_time
= state
->out_change_time
;
1112 *out_allocation_size
= state
->out_allocation_size
;
1113 *out_end_of_file
= state
->out_end_of_file
;
1114 *out_file_attributes
= state
->out_file_attributes
;
1115 *out_file_id_persistent
= state
->out_file_id_persistent
;
1116 *out_file_id_volatile
= state
->out_file_id_volatile
;
1117 *out_context_blobs
= state
->out_context_blobs
;
1119 talloc_steal(mem_ctx
, state
->out_context_blobs
.blobs
);
1121 tevent_req_received(req
);
1122 return NT_STATUS_OK
;
1125 /*********************************************************
1126 Code for dealing with deferred opens.
1127 *********************************************************/
1129 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request
*smb2req
,
1130 struct timeval
*p_request_time
,
1133 struct smbd_smb2_create_state
*state
= NULL
;
1134 struct tevent_req
*req
= NULL
;
1139 req
= smb2req
->subreq
;
1143 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
1147 if (!state
->open_was_deferred
) {
1150 if (p_request_time
) {
1151 *p_request_time
= state
->request_time
;
1154 *pp_state
= (void *)state
->private_data
.data
;
1159 /*********************************************************
1160 Re-process this call early - requested by message or
1162 *********************************************************/
1164 static struct smbd_smb2_request
*find_open_smb2req(
1165 struct smbd_server_connection
*sconn
, uint64_t mid
)
1167 struct smbd_smb2_request
*smb2req
;
1169 for (smb2req
= sconn
->smb2
.requests
; smb2req
; smb2req
= smb2req
->next
) {
1170 uint64_t message_id
;
1171 if (smb2req
->subreq
== NULL
) {
1172 /* This message has been processed. */
1175 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1176 /* This message has been processed. */
1179 message_id
= get_mid_from_smb2req(smb2req
);
1180 if (message_id
== mid
) {
1187 bool open_was_deferred_smb2(struct smbd_server_connection
*sconn
, uint64_t mid
)
1189 struct smbd_smb2_create_state
*state
= NULL
;
1190 struct smbd_smb2_request
*smb2req
;
1192 smb2req
= find_open_smb2req(sconn
, mid
);
1195 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
1196 (unsigned long long)mid
));
1199 if (!smb2req
->subreq
) {
1202 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1205 state
= tevent_req_data(smb2req
->subreq
,
1206 struct smbd_smb2_create_state
);
1210 /* It's not in progress if there's no timeout event. */
1211 if (!state
->open_was_deferred
) {
1215 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
1216 (unsigned long long)mid
));
1221 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request
*smb2req
,
1224 struct smbd_smb2_create_state
*state
= NULL
;
1226 if (!smb2req
->subreq
) {
1229 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1232 state
= tevent_req_data(smb2req
->subreq
,
1233 struct smbd_smb2_create_state
);
1238 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
1240 (unsigned long long)mid
));
1242 state
->open_was_deferred
= false;
1243 /* Ensure we don't have any outstanding timer event. */
1244 TALLOC_FREE(state
->te
);
1245 /* Ensure we don't have any outstanding immediate event. */
1246 TALLOC_FREE(state
->im
);
1249 void remove_deferred_open_message_smb2(
1250 struct smbd_server_connection
*sconn
, uint64_t mid
)
1252 struct smbd_smb2_request
*smb2req
;
1254 smb2req
= find_open_smb2req(sconn
, mid
);
1257 DEBUG(10,("remove_deferred_open_message_smb2: "
1258 "can't find mid %llu\n",
1259 (unsigned long long)mid
));
1262 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
1265 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context
*ctx
,
1266 struct tevent_immediate
*im
,
1269 struct smbd_smb2_request
*smb2req
= talloc_get_type_abort(private_data
,
1270 struct smbd_smb2_request
);
1271 struct smbd_server_connection
*sconn
= smb2req
->sconn
;
1272 uint64_t mid
= get_mid_from_smb2req(smb2req
);
1275 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1276 "re-dispatching mid %llu\n",
1277 (unsigned long long)mid
));
1279 status
= smbd_smb2_request_dispatch(smb2req
);
1280 if (!NT_STATUS_IS_OK(status
)) {
1281 smbd_server_connection_terminate(sconn
, nt_errstr(status
));
1286 bool schedule_deferred_open_message_smb2(
1287 struct smbd_server_connection
*sconn
, uint64_t mid
)
1289 struct smbd_smb2_create_state
*state
= NULL
;
1290 struct smbd_smb2_request
*smb2req
;
1292 smb2req
= find_open_smb2req(sconn
, mid
);
1295 DEBUG(10,("schedule_deferred_open_message_smb2: "
1296 "can't find mid %llu\n",
1297 (unsigned long long)mid
));
1300 if (!smb2req
->subreq
) {
1303 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1306 state
= tevent_req_data(smb2req
->subreq
,
1307 struct smbd_smb2_create_state
);
1312 /* Ensure we don't have any outstanding timer event. */
1313 TALLOC_FREE(state
->te
);
1314 /* Ensure we don't have any outstanding immediate event. */
1315 TALLOC_FREE(state
->im
);
1318 * This is subtle. We must null out the callback
1319 * before rescheduling, else the first call to
1320 * tevent_req_nterror() causes the _receive()
1321 * function to be called, this causing tevent_req_post()
1324 tevent_req_set_callback(smb2req
->subreq
, NULL
, NULL
);
1326 state
->im
= tevent_create_immediate(smb2req
);
1328 smbd_server_connection_terminate(smb2req
->sconn
,
1329 nt_errstr(NT_STATUS_NO_MEMORY
));
1333 DEBUG(10,("schedule_deferred_open_message_smb2: "
1334 "re-processing mid %llu\n",
1335 (unsigned long long)mid
));
1337 tevent_schedule_immediate(state
->im
,
1338 smb2req
->sconn
->ev_ctx
,
1339 smbd_smb2_create_request_dispatch_immediate
,
1345 /*********************************************************
1346 Re-process this call.
1347 *********************************************************/
1349 static void smb2_deferred_open_timer(struct event_context
*ev
,
1350 struct timed_event
*te
,
1351 struct timeval _tval
,
1355 struct smbd_smb2_create_state
*state
= NULL
;
1356 struct smbd_smb2_request
*smb2req
= talloc_get_type(private_data
,
1357 struct smbd_smb2_request
);
1359 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1360 smb2req
->current_idx
,
1361 tevent_req_default_print(smb2req
->subreq
, talloc_tos()) ));
1363 state
= tevent_req_data(smb2req
->subreq
,
1364 struct smbd_smb2_create_state
);
1369 * Null this out, don't talloc_free. It will
1370 * be talloc_free'd by the tevent library when
1374 /* Ensure we don't have any outstanding immediate event. */
1375 TALLOC_FREE(state
->im
);
1378 * This is subtle. We must null out the callback
1379 * before rescheduling, else the first call to
1380 * tevent_req_nterror() causes the _receive()
1381 * function to be called, this causing tevent_req_post()
1384 tevent_req_set_callback(smb2req
->subreq
, NULL
, NULL
);
1386 status
= smbd_smb2_request_dispatch(smb2req
);
1388 if (!NT_STATUS_IS_OK(status
)) {
1389 smbd_server_connection_terminate(smb2req
->sconn
,
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
->private_data
.data
)) {
1413 /* Can't cancel an async create. */
1417 remove_deferred_open_entry(state
->id
, mid
,
1418 messaging_server_id(smb2req
->sconn
->msg_ctx
));
1419 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
1421 tevent_req_defer_callback(req
, smb2req
->sconn
->ev_ctx
);
1422 tevent_req_nterror(req
, NT_STATUS_CANCELLED
);
1426 bool push_deferred_open_message_smb2(struct smbd_smb2_request
*smb2req
,
1427 struct timeval request_time
,
1428 struct timeval timeout
,
1433 struct tevent_req
*req
= NULL
;
1434 struct smbd_smb2_create_state
*state
= NULL
;
1435 struct timeval end_time
;
1440 req
= smb2req
->subreq
;
1444 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
1449 state
->request_time
= request_time
;
1450 state
->private_data
= data_blob_talloc(state
, private_data
,
1452 if (!state
->private_data
.data
) {
1456 /* Re-schedule us to retry on timer expiry. */
1457 end_time
= timeval_sum(&request_time
, &timeout
);
1459 DEBUG(10,("push_deferred_open_message_smb2: "
1461 timeval_string(talloc_tos(),
1465 state
->open_was_deferred
= true;
1466 state
->te
= tevent_add_timer(smb2req
->sconn
->ev_ctx
,
1469 smb2_deferred_open_timer
,
1475 /* allow this request to be canceled */
1476 tevent_req_set_cancel_fn(req
, smbd_smb2_create_cancel
);