2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
25 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level
)
27 switch(in_oplock_level
) {
28 case SMB2_OPLOCK_LEVEL_NONE
:
30 case SMB2_OPLOCK_LEVEL_II
:
31 return LEVEL_II_OPLOCK
;
32 case SMB2_OPLOCK_LEVEL_EXCLUSIVE
:
33 return EXCLUSIVE_OPLOCK
;
34 case SMB2_OPLOCK_LEVEL_BATCH
:
36 case SMB2_OPLOCK_LEVEL_LEASE
:
37 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
38 "LEASE_OPLOCK_REQUESTED\n"));
41 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
43 (unsigned int)in_oplock_level
));
48 uint8_t map_samba_oplock_levels_to_smb2(int oplock_type
)
50 if (BATCH_OPLOCK_TYPE(oplock_type
)) {
51 return SMB2_OPLOCK_LEVEL_BATCH
;
52 } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type
)) {
53 return SMB2_OPLOCK_LEVEL_EXCLUSIVE
;
54 } else if (LEVEL_II_OPLOCK_TYPE(oplock_type
)) {
55 return SMB2_OPLOCK_LEVEL_II
;
57 return SMB2_OPLOCK_LEVEL_NONE
;
61 static struct tevent_req
*smbd_smb2_create_send(TALLOC_CTX
*mem_ctx
,
62 struct tevent_context
*ev
,
63 struct smbd_smb2_request
*smb2req
,
64 uint8_t in_oplock_level
,
65 uint32_t in_impersonation_level
,
66 uint32_t in_desired_access
,
67 uint32_t in_file_attributes
,
68 uint32_t in_share_access
,
69 uint32_t in_create_disposition
,
70 uint32_t in_create_options
,
72 struct smb2_create_blobs in_context_blobs
);
73 static NTSTATUS
smbd_smb2_create_recv(struct tevent_req
*req
,
75 uint8_t *out_oplock_level
,
76 uint32_t *out_create_action
,
77 NTTIME
*out_creation_time
,
78 NTTIME
*out_last_access_time
,
79 NTTIME
*out_last_write_time
,
80 NTTIME
*out_change_time
,
81 uint64_t *out_allocation_size
,
82 uint64_t *out_end_of_file
,
83 uint32_t *out_file_attributes
,
84 uint64_t *out_file_id_volatile
,
85 struct smb2_create_blobs
*out_context_blobs
);
87 static void smbd_smb2_request_create_done(struct tevent_req
*tsubreq
);
88 NTSTATUS
smbd_smb2_request_process_create(struct smbd_smb2_request
*smb2req
)
90 const uint8_t *inbody
;
91 int i
= smb2req
->current_idx
;
92 size_t expected_body_size
= 0x39;
94 uint8_t in_oplock_level
;
95 uint32_t in_impersonation_level
;
96 uint32_t in_desired_access
;
97 uint32_t in_file_attributes
;
98 uint32_t in_share_access
;
99 uint32_t in_create_disposition
;
100 uint32_t in_create_options
;
101 uint16_t in_name_offset
;
102 uint16_t in_name_length
;
103 DATA_BLOB in_name_buffer
;
104 char *in_name_string
;
105 size_t in_name_string_size
;
106 uint32_t name_offset
= 0;
107 uint32_t name_available_length
= 0;
108 uint32_t in_context_offset
;
109 uint32_t in_context_length
;
110 DATA_BLOB in_context_buffer
;
111 struct smb2_create_blobs in_context_blobs
;
112 uint32_t context_offset
= 0;
113 uint32_t context_available_length
= 0;
117 struct tevent_req
*tsubreq
;
119 if (smb2req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
120 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
123 inbody
= (const uint8_t *)smb2req
->in
.vector
[i
+1].iov_base
;
125 body_size
= SVAL(inbody
, 0x00);
126 if (body_size
!= expected_body_size
) {
127 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
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
+ (body_size
& 0xFFFFFFFE);
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 if (name_offset
> smb2req
->in
.vector
[i
+2].iov_len
) {
162 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
165 name_available_length
= smb2req
->in
.vector
[i
+2].iov_len
- name_offset
;
167 if (in_name_length
> name_available_length
) {
168 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
171 in_name_buffer
.data
= (uint8_t *)smb2req
->in
.vector
[i
+2].iov_base
+
173 in_name_buffer
.length
= in_name_length
;
175 if (in_context_offset
== 0 && in_context_length
== 0) {
178 } else if (in_context_offset
< dyn_offset
) {
179 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
181 context_offset
= in_context_offset
- dyn_offset
;
184 if (context_offset
> smb2req
->in
.vector
[i
+2].iov_len
) {
185 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
188 context_available_length
= smb2req
->in
.vector
[i
+2].iov_len
- context_offset
;
190 if (in_context_length
> context_available_length
) {
191 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
194 in_context_buffer
.data
= (uint8_t *)smb2req
->in
.vector
[i
+2].iov_base
+
196 in_context_buffer
.length
= in_context_length
;
199 * Now interpret the name and context buffers
202 ok
= convert_string_talloc(smb2req
, CH_UTF16
, CH_UNIX
,
204 in_name_buffer
.length
,
206 &in_name_string_size
, false);
208 return smbd_smb2_request_error(smb2req
, NT_STATUS_ILLEGAL_CHARACTER
);
211 ZERO_STRUCT(in_context_blobs
);
212 status
= smb2_create_blob_parse(smb2req
, in_context_buffer
, &in_context_blobs
);
213 if (!NT_STATUS_IS_OK(status
)) {
214 return smbd_smb2_request_error(smb2req
, status
);
217 tsubreq
= smbd_smb2_create_send(smb2req
,
218 smb2req
->sconn
->smb2
.event_ctx
,
221 in_impersonation_level
,
225 in_create_disposition
,
229 if (tsubreq
== NULL
) {
230 smb2req
->subreq
= NULL
;
231 return smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
233 tevent_req_set_callback(tsubreq
, smbd_smb2_request_create_done
, smb2req
);
235 return smbd_smb2_request_pending_queue(smb2req
, tsubreq
);
238 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request
*smb2req
)
240 uint8_t *reqhdr
= (uint8_t *)smb2req
->out
.vector
[smb2req
->current_idx
].iov_base
;
241 return BVAL(reqhdr
, SMB2_HDR_MESSAGE_ID
);
244 static void smbd_smb2_request_create_done(struct tevent_req
*tsubreq
)
246 struct smbd_smb2_request
*smb2req
= tevent_req_callback_data(tsubreq
,
247 struct smbd_smb2_request
);
248 int i
= smb2req
->current_idx
;
252 uint8_t out_oplock_level
= 0;
253 uint32_t out_create_action
= 0;
254 NTTIME out_creation_time
= 0;
255 NTTIME out_last_access_time
= 0;
256 NTTIME out_last_write_time
= 0;
257 NTTIME out_change_time
= 0;
258 uint64_t out_allocation_size
= 0;
259 uint64_t out_end_of_file
= 0;
260 uint32_t out_file_attributes
= 0;
261 uint64_t out_file_id_volatile
= 0;
262 struct smb2_create_blobs out_context_blobs
;
263 DATA_BLOB out_context_buffer
;
264 uint16_t out_context_buffer_offset
= 0;
266 NTSTATUS error
; /* transport error */
268 if (smb2req
->cancelled
) {
269 uint64_t mid
= get_mid_from_smb2req(smb2req
);
270 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
271 (unsigned long long)mid
));
272 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_CANCELLED
);
273 if (!NT_STATUS_IS_OK(error
)) {
274 smbd_server_connection_terminate(smb2req
->sconn
,
281 status
= smbd_smb2_create_recv(tsubreq
,
286 &out_last_access_time
,
287 &out_last_write_time
,
289 &out_allocation_size
,
291 &out_file_attributes
,
292 &out_file_id_volatile
,
294 if (!NT_STATUS_IS_OK(status
)) {
295 error
= smbd_smb2_request_error(smb2req
, status
);
296 if (!NT_STATUS_IS_OK(error
)) {
297 smbd_server_connection_terminate(smb2req
->sconn
,
304 status
= smb2_create_blob_push(smb2req
, &out_context_buffer
, out_context_blobs
);
305 if (!NT_STATUS_IS_OK(status
)) {
306 error
= smbd_smb2_request_error(smb2req
, status
);
307 if (!NT_STATUS_IS_OK(error
)) {
308 smbd_server_connection_terminate(smb2req
->sconn
,
315 if (out_context_buffer
.length
> 0) {
316 out_context_buffer_offset
= SMB2_HDR_BODY
+ 0x58;
319 outhdr
= (uint8_t *)smb2req
->out
.vector
[i
].iov_base
;
321 outbody
= data_blob_talloc(smb2req
->out
.vector
, NULL
, 0x58);
322 if (outbody
.data
== NULL
) {
323 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
324 if (!NT_STATUS_IS_OK(error
)) {
325 smbd_server_connection_terminate(smb2req
->sconn
,
332 SSVAL(outbody
.data
, 0x00, 0x58 + 1); /* struct size */
333 SCVAL(outbody
.data
, 0x02,
334 out_oplock_level
); /* oplock level */
335 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
336 SIVAL(outbody
.data
, 0x04,
337 out_create_action
); /* create action */
338 SBVAL(outbody
.data
, 0x08,
339 out_creation_time
); /* creation time */
340 SBVAL(outbody
.data
, 0x10,
341 out_last_access_time
); /* last access time */
342 SBVAL(outbody
.data
, 0x18,
343 out_last_write_time
); /* last write time */
344 SBVAL(outbody
.data
, 0x20,
345 out_change_time
); /* change time */
346 SBVAL(outbody
.data
, 0x28,
347 out_allocation_size
); /* allocation size */
348 SBVAL(outbody
.data
, 0x30,
349 out_end_of_file
); /* end of file */
350 SIVAL(outbody
.data
, 0x38,
351 out_file_attributes
); /* file attributes */
352 SIVAL(outbody
.data
, 0x3C, 0); /* reserved */
353 SBVAL(outbody
.data
, 0x40, 0); /* file id (persistent) */
354 SBVAL(outbody
.data
, 0x48,
355 out_file_id_volatile
); /* file id (volatile) */
356 SIVAL(outbody
.data
, 0x50,
357 out_context_buffer_offset
); /* create contexts offset */
358 SIVAL(outbody
.data
, 0x54,
359 out_context_buffer
.length
); /* create contexts length */
361 outdyn
= out_context_buffer
;
363 error
= smbd_smb2_request_done(smb2req
, outbody
, &outdyn
);
364 if (!NT_STATUS_IS_OK(error
)) {
365 smbd_server_connection_terminate(smb2req
->sconn
,
371 struct smbd_smb2_create_state
{
372 struct smbd_smb2_request
*smb2req
;
373 struct smb_request
*smb1req
;
374 struct timed_event
*te
;
375 struct timeval request_time
;
377 DATA_BLOB private_data
;
378 uint8_t out_oplock_level
;
379 uint32_t out_create_action
;
380 NTTIME out_creation_time
;
381 NTTIME out_last_access_time
;
382 NTTIME out_last_write_time
;
383 NTTIME out_change_time
;
384 uint64_t out_allocation_size
;
385 uint64_t out_end_of_file
;
386 uint32_t out_file_attributes
;
387 uint64_t out_file_id_volatile
;
388 struct smb2_create_blobs out_context_blobs
;
391 static struct tevent_req
*smbd_smb2_create_send(TALLOC_CTX
*mem_ctx
,
392 struct tevent_context
*ev
,
393 struct smbd_smb2_request
*smb2req
,
394 uint8_t in_oplock_level
,
395 uint32_t in_impersonation_level
,
396 uint32_t in_desired_access
,
397 uint32_t in_file_attributes
,
398 uint32_t in_share_access
,
399 uint32_t in_create_disposition
,
400 uint32_t in_create_options
,
402 struct smb2_create_blobs in_context_blobs
)
404 struct tevent_req
*req
= NULL
;
405 struct smbd_smb2_create_state
*state
= NULL
;
407 struct smb_request
*smb1req
= NULL
;
408 files_struct
*result
= NULL
;
410 struct timespec write_time_ts
;
411 struct smb2_create_blobs out_context_blobs
;
413 ZERO_STRUCT(out_context_blobs
);
415 if (!smb2req
->async
) {
416 /* New create call. */
417 req
= tevent_req_create(mem_ctx
, &state
,
418 struct smbd_smb2_create_state
);
422 state
->smb2req
= smb2req
;
423 smb2req
->subreq
= req
; /* So we can find this when going async. */
425 smb1req
= smbd_smb2_fake_smb_request(smb2req
);
426 if (tevent_req_nomem(smb1req
, req
)) {
427 return tevent_req_post(req
, ev
);
429 state
->smb1req
= smb1req
;
430 DEBUG(10,("smbd_smb2_create: name[%s]\n",
433 /* Re-entrant create call. */
434 req
= smb2req
->subreq
;
435 state
= tevent_req_data(req
,
436 struct smbd_smb2_create_state
);
437 smb1req
= state
->smb1req
;
438 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
442 if (IS_IPC(smb1req
->conn
)) {
443 const char *pipe_name
= in_name
;
445 if (!lp_nt_pipe_support()) {
446 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
447 return tevent_req_post(req
, ev
);
450 /* Strip \\ off the name. */
451 if (pipe_name
[0] == '\\') {
455 status
= open_np_file(smb1req
, pipe_name
, &result
);
456 if (!NT_STATUS_IS_OK(status
)) {
457 tevent_req_nterror(req
, status
);
458 return tevent_req_post(req
, ev
);
460 info
= FILE_WAS_OPENED
;
461 } else if (CAN_PRINT(smb1req
->conn
)) {
462 status
= file_new(smb1req
, smb1req
->conn
, &result
);
463 if(!NT_STATUS_IS_OK(status
)) {
464 tevent_req_nterror(req
, status
);
465 return tevent_req_post(req
, ev
);
468 status
= print_fsp_open(smb1req
,
473 if (!NT_STATUS_IS_OK(status
)) {
474 file_free(smb1req
, result
);
475 tevent_req_nterror(req
, status
);
476 return tevent_req_post(req
, ev
);
478 info
= FILE_WAS_CREATED
;
481 struct smb_filename
*smb_fname
= NULL
;
482 struct smb2_create_blob
*exta
= NULL
;
483 struct ea_list
*ea_list
= NULL
;
484 struct smb2_create_blob
*mxac
= NULL
;
485 NTTIME max_access_time
= 0;
486 struct smb2_create_blob
*secd
= NULL
;
487 struct security_descriptor
*sec_desc
= NULL
;
488 struct smb2_create_blob
*dhnq
= NULL
;
489 struct smb2_create_blob
*dhnc
= NULL
;
490 struct smb2_create_blob
*alsi
= NULL
;
491 uint64_t allocation_size
= 0;
492 struct smb2_create_blob
*twrp
= NULL
;
493 struct smb2_create_blob
*qfid
= NULL
;
495 exta
= smb2_create_blob_find(&in_context_blobs
,
496 SMB2_CREATE_TAG_EXTA
);
497 mxac
= smb2_create_blob_find(&in_context_blobs
,
498 SMB2_CREATE_TAG_MXAC
);
499 secd
= smb2_create_blob_find(&in_context_blobs
,
500 SMB2_CREATE_TAG_SECD
);
501 dhnq
= smb2_create_blob_find(&in_context_blobs
,
502 SMB2_CREATE_TAG_DHNQ
);
503 dhnc
= smb2_create_blob_find(&in_context_blobs
,
504 SMB2_CREATE_TAG_DHNC
);
505 alsi
= smb2_create_blob_find(&in_context_blobs
,
506 SMB2_CREATE_TAG_ALSI
);
507 twrp
= smb2_create_blob_find(&in_context_blobs
,
508 SMB2_CREATE_TAG_TWRP
);
509 qfid
= smb2_create_blob_find(&in_context_blobs
,
510 SMB2_CREATE_TAG_QFID
);
512 fname
= talloc_strdup(state
, in_name
);
513 if (tevent_req_nomem(fname
, req
)) {
514 return tevent_req_post(req
, ev
);
519 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
520 return tevent_req_post(req
, ev
);
523 ea_list
= read_nttrans_ea_list(mem_ctx
,
524 (const char *)exta
->data
.data
, exta
->data
.length
);
526 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
527 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
528 return tevent_req_post(req
, ev
);
534 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
535 return tevent_req_post(req
, ev
);
538 if (mxac
->data
.length
== 0) {
540 } else if (mxac
->data
.length
== 8) {
541 max_access_time
= BVAL(mxac
->data
.data
, 0);
543 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
544 return tevent_req_post(req
, ev
);
549 enum ndr_err_code ndr_err
;
552 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
553 return tevent_req_post(req
, ev
);
556 sec_desc
= talloc_zero(state
, struct security_descriptor
);
557 if (tevent_req_nomem(sec_desc
, req
)) {
558 return tevent_req_post(req
, ev
);
561 ndr_err
= ndr_pull_struct_blob(&secd
->data
,
562 sec_desc
, NULL
, sec_desc
,
563 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
564 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
565 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
566 ndr_errstr(ndr_err
)));
567 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
568 return tevent_req_post(req
, ev
);
574 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
575 return tevent_req_post(req
, ev
);
578 if (dhnq
->data
.length
!= 16) {
579 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
580 return tevent_req_post(req
, ev
);
583 * we don't support durable handles yet
584 * and have to ignore this
589 if (dhnc
->data
.length
!= 16) {
590 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
591 return tevent_req_post(req
, ev
);
593 /* we don't support durable handles yet */
594 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
595 return tevent_req_post(req
, ev
);
600 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
601 return tevent_req_post(req
, ev
);
604 if (alsi
->data
.length
!= 8) {
605 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
606 return tevent_req_post(req
, ev
);
608 allocation_size
= BVAL(alsi
->data
.data
, 0);
617 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
618 return tevent_req_post(req
, ev
);
621 if (twrp
->data
.length
!= 8) {
622 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
623 return tevent_req_post(req
, ev
);
626 nttime
= BVAL(twrp
->data
.data
, 0);
627 t
= nt_time_to_unix(nttime
);
631 fname
= talloc_asprintf(state
,
632 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
640 if (tevent_req_nomem(fname
, req
)) {
641 return tevent_req_post(req
, ev
);
646 if (qfid
->data
.length
!= 0) {
647 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
648 return tevent_req_post(req
, ev
);
652 /* these are ignored for SMB2 */
653 in_create_options
&= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
654 in_create_options
&= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
656 /* convert '\\' into '/' */
657 status
= check_path_syntax(fname
);
658 if (!NT_STATUS_IS_OK(status
)) {
659 tevent_req_nterror(req
, status
);
660 return tevent_req_post(req
, ev
);
663 status
= filename_convert(req
,
665 smb1req
->flags2
& FLAGS2_DFS_PATHNAMES
,
670 if (!NT_STATUS_IS_OK(status
)) {
671 tevent_req_nterror(req
, status
);
672 return tevent_req_post(req
, ev
);
675 in_file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
677 status
= SMB_VFS_CREATE_FILE(smb1req
->conn
,
679 0, /* root_dir_fid */
683 in_create_disposition
,
686 map_smb2_oplock_levels_to_samba(in_oplock_level
),
688 0, /* private_flags */
693 if (!NT_STATUS_IS_OK(status
)) {
694 if (open_was_deferred(smb1req
->mid
)) {
697 tevent_req_nterror(req
, status
);
698 return tevent_req_post(req
, ev
);
702 NTTIME last_write_time
;
704 unix_timespec_to_nt_time(&last_write_time
,
705 result
->fsp_name
->st
.st_ex_mtime
);
706 if (last_write_time
!= max_access_time
) {
708 uint32_t max_access_granted
;
709 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
711 status
= smbd_check_open_rights(smb1req
->conn
,
713 SEC_FLAG_MAXIMUM_ALLOWED
,
714 &max_access_granted
);
716 SIVAL(p
, 0, NT_STATUS_V(status
));
717 SIVAL(p
, 4, max_access_granted
);
719 status
= smb2_create_blob_add(state
,
721 SMB2_CREATE_TAG_MXAC
,
723 if (!NT_STATUS_IS_OK(status
)) {
724 tevent_req_nterror(req
, status
);
725 return tevent_req_post(req
, ev
);
732 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
736 /* TODO: maybe use result->file_id */
737 SIVAL(p
, 0, result
->fsp_name
->st
.st_ex_ino
);/* FileIndexLow */
738 SIVAL(p
, 4, result
->fsp_name
->st
.st_ex_dev
);/* FileIndexHigh */
740 status
= smb2_create_blob_add(state
, &out_context_blobs
,
741 SMB2_CREATE_TAG_QFID
,
743 if (!NT_STATUS_IS_OK(status
)) {
744 tevent_req_nterror(req
, status
);
745 return tevent_req_post(req
, ev
);
750 smb2req
->compat_chain_fsp
= smb1req
->chain_fsp
;
752 state
->out_oplock_level
= map_samba_oplock_levels_to_smb2(result
->oplock_type
);
754 if ((in_create_disposition
== FILE_SUPERSEDE
)
755 && (info
== FILE_WAS_OVERWRITTEN
)) {
756 state
->out_create_action
= FILE_WAS_SUPERSEDED
;
758 state
->out_create_action
= info
;
760 state
->out_file_attributes
= dos_mode(result
->conn
,
762 /* Deal with other possible opens having a modified
764 ZERO_STRUCT(write_time_ts
);
765 get_file_infos(result
->file_id
, NULL
, &write_time_ts
);
766 if (!null_timespec(write_time_ts
)) {
767 update_stat_ex_mtime(&result
->fsp_name
->st
, write_time_ts
);
770 unix_timespec_to_nt_time(&state
->out_creation_time
,
771 get_create_timespec(smb1req
->conn
, result
,
773 unix_timespec_to_nt_time(&state
->out_last_access_time
,
774 result
->fsp_name
->st
.st_ex_atime
);
775 unix_timespec_to_nt_time(&state
->out_last_write_time
,
776 result
->fsp_name
->st
.st_ex_mtime
);
777 unix_timespec_to_nt_time(&state
->out_change_time
,
778 get_change_timespec(smb1req
->conn
, result
,
780 state
->out_allocation_size
=
781 result
->fsp_name
->st
.st_ex_blksize
*
782 result
->fsp_name
->st
.st_ex_blocks
;
783 state
->out_end_of_file
= result
->fsp_name
->st
.st_ex_size
;
784 if (state
->out_file_attributes
== 0) {
785 state
->out_file_attributes
= FILE_ATTRIBUTE_NORMAL
;
787 state
->out_file_id_volatile
= result
->fnum
;
788 state
->out_context_blobs
= out_context_blobs
;
790 tevent_req_done(req
);
791 return tevent_req_post(req
, ev
);
794 static NTSTATUS
smbd_smb2_create_recv(struct tevent_req
*req
,
796 uint8_t *out_oplock_level
,
797 uint32_t *out_create_action
,
798 NTTIME
*out_creation_time
,
799 NTTIME
*out_last_access_time
,
800 NTTIME
*out_last_write_time
,
801 NTTIME
*out_change_time
,
802 uint64_t *out_allocation_size
,
803 uint64_t *out_end_of_file
,
804 uint32_t *out_file_attributes
,
805 uint64_t *out_file_id_volatile
,
806 struct smb2_create_blobs
*out_context_blobs
)
809 struct smbd_smb2_create_state
*state
= tevent_req_data(req
,
810 struct smbd_smb2_create_state
);
812 if (tevent_req_is_nterror(req
, &status
)) {
813 tevent_req_received(req
);
817 *out_oplock_level
= state
->out_oplock_level
;
818 *out_create_action
= state
->out_create_action
;
819 *out_creation_time
= state
->out_creation_time
;
820 *out_last_access_time
= state
->out_last_access_time
;
821 *out_last_write_time
= state
->out_last_write_time
;
822 *out_change_time
= state
->out_change_time
;
823 *out_allocation_size
= state
->out_allocation_size
;
824 *out_end_of_file
= state
->out_end_of_file
;
825 *out_file_attributes
= state
->out_file_attributes
;
826 *out_file_id_volatile
= state
->out_file_id_volatile
;
827 *out_context_blobs
= state
->out_context_blobs
;
829 talloc_steal(mem_ctx
, state
->out_context_blobs
.blobs
);
831 tevent_req_received(req
);
835 /*********************************************************
836 Code for dealing with deferred opens.
837 *********************************************************/
839 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request
*smb2req
,
840 struct timeval
*p_request_time
,
843 struct smbd_smb2_create_state
*state
= NULL
;
844 struct tevent_req
*req
= NULL
;
849 if (!smb2req
->async
) {
852 req
= smb2req
->subreq
;
856 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
860 if (p_request_time
) {
861 *p_request_time
= state
->request_time
;
864 *pp_state
= (void *)state
->private_data
.data
;
869 /*********************************************************
870 Re-process this call early - requested by message or
872 *********************************************************/
874 static struct smbd_smb2_request
*find_open_smb2req(uint64_t mid
)
876 struct smbd_server_connection
*sconn
= smbd_server_conn
;
877 struct smbd_smb2_request
*smb2req
;
879 for (smb2req
= sconn
->smb2
.requests
; smb2req
; smb2req
= smb2req
->next
) {
881 if (smb2req
->subreq
== NULL
) {
882 /* This message has been processed. */
885 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
886 /* This message has been processed. */
889 message_id
= get_mid_from_smb2req(smb2req
);
890 if (message_id
== mid
) {
897 bool open_was_deferred_smb2(uint64_t mid
)
899 struct smbd_smb2_create_state
*state
= NULL
;
900 struct smbd_smb2_request
*smb2req
= find_open_smb2req(mid
);
903 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
904 (unsigned long long)mid
));
907 if (!smb2req
->subreq
) {
910 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
913 state
= tevent_req_data(smb2req
->subreq
,
914 struct smbd_smb2_create_state
);
918 /* It's not in progress if there's no timeout event. */
923 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
924 (unsigned long long)mid
));
929 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request
*smb2req
,
932 struct smbd_smb2_create_state
*state
= NULL
;
934 if (!smb2req
->subreq
) {
937 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
940 state
= tevent_req_data(smb2req
->subreq
,
941 struct smbd_smb2_create_state
);
946 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
948 (unsigned long long)mid
));
950 /* Ensure we don't have any outstanding timer event. */
951 TALLOC_FREE(state
->te
);
954 void remove_deferred_open_message_smb2(uint64_t mid
)
956 struct smbd_smb2_request
*smb2req
= find_open_smb2req(mid
);
959 DEBUG(10,("remove_deferred_open_message_smb2: "
960 "can't find mid %llu\n",
961 (unsigned long long)mid
));
964 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
967 void schedule_deferred_open_message_smb2(uint64_t mid
)
969 struct tevent_immediate
*im
= NULL
;
970 struct smbd_smb2_create_state
*state
= NULL
;
971 struct smbd_smb2_request
*smb2req
= find_open_smb2req(mid
);
974 DEBUG(10,("schedule_deferred_open_message_smb2: "
975 "can't find mid %llu\n",
976 (unsigned long long)mid
));
979 if (!smb2req
->subreq
) {
982 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
985 state
= tevent_req_data(smb2req
->subreq
,
986 struct smbd_smb2_create_state
);
990 /* Ensure we don't have any outstanding timer event. */
991 TALLOC_FREE(state
->te
);
994 * This is subtle. We must null out the callback
995 * before resheduling, else the first call to
996 * tevent_req_nterror() causes the _receive()
997 * function to be called, this causing tevent_req_post()
1000 tevent_req_set_callback(smb2req
->subreq
, NULL
, NULL
);
1002 im
= tevent_create_immediate(smb2req
);
1004 smbd_server_connection_terminate(smb2req
->sconn
,
1005 nt_errstr(NT_STATUS_NO_MEMORY
));
1008 DEBUG(10,("schedule_deferred_open_message_smb2: "
1009 "re-processing mid %llu\n",
1010 (unsigned long long)mid
));
1012 tevent_schedule_immediate(im
,
1013 smb2req
->sconn
->smb2
.event_ctx
,
1014 smbd_smb2_request_dispatch_immediate
,
1018 /*********************************************************
1019 Re-process this call.
1020 *********************************************************/
1022 static void smb2_deferred_open_timer(struct event_context
*ev
,
1023 struct timed_event
*te
,
1024 struct timeval _tval
,
1028 struct smbd_smb2_create_state
*state
= NULL
;
1029 struct smbd_smb2_request
*smb2req
= talloc_get_type(private_data
,
1030 struct smbd_smb2_request
);
1032 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1033 smb2req
->current_idx
,
1034 tevent_req_default_print(smb2req
->subreq
, talloc_tos()) ));
1036 state
= tevent_req_data(smb2req
->subreq
,
1037 struct smbd_smb2_create_state
);
1042 * Null this out, don't talloc_free. It will
1043 * be talloc_free'd by the tevent library when
1049 * This is subtle. We must null out the callback
1050 * before resheduling, else the first call to
1051 * tevent_req_nterror() causes the _receive()
1052 * function to be called, this causing tevent_req_post()
1055 tevent_req_set_callback(smb2req
->subreq
, NULL
, NULL
);
1057 status
= smbd_smb2_request_dispatch(smb2req
);
1059 if (!NT_STATUS_IS_OK(status
)) {
1060 smbd_server_connection_terminate(smb2req
->sconn
,
1065 static bool smbd_smb2_create_cancel(struct tevent_req
*req
)
1067 struct smbd_smb2_request
*smb2req
= NULL
;
1068 struct smbd_smb2_create_state
*state
= tevent_req_data(req
,
1069 struct smbd_smb2_create_state
);
1076 if (!state
->smb2req
) {
1080 smb2req
= state
->smb2req
;
1081 mid
= get_mid_from_smb2req(smb2req
);
1083 remove_deferred_open_entry(state
->id
, mid
);
1084 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
1085 smb2req
->cancelled
= true;
1087 tevent_req_done(req
);
1091 bool push_deferred_open_message_smb2(struct smbd_smb2_request
*smb2req
,
1092 struct timeval request_time
,
1093 struct timeval timeout
,
1098 struct tevent_req
*req
= NULL
;
1099 struct smbd_smb2_create_state
*state
= NULL
;
1100 struct timeval end_time
;
1105 req
= smb2req
->subreq
;
1109 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
1114 state
->request_time
= request_time
;
1115 state
->private_data
= data_blob_talloc(state
, private_data
,
1117 if (!state
->private_data
.data
) {
1121 /* Boo - turns out this isn't what W2K8R2
1122 does. It actually sends the STATUS_PENDING
1123 message followed by the STATUS_SHARING_VIOLATION
1124 message. Surely this means that all open
1125 calls (even on directories) will potentially
1126 fail in a chain.... ? And I've seen directory
1127 opens as the start of a chain. JRA.
1130 * More subtlety. To match W2K8R2 don't
1131 * send a "gone async" message if it's simply
1132 * a STATUS_SHARING_VIOLATION (short) wait, not
1133 * an oplock break wait. We do this by prematurely
1134 * setting smb2req->async flag.
1136 if (timeout
.tv_sec
< 2) {
1137 DEBUG(10,("push_deferred_open_message_smb2: "
1138 "short timer wait (usec = %u). "
1139 "Don't send async message.\n",
1140 (unsigned int)timeout
.tv_usec
));
1141 smb2req
->async
= true;
1145 /* Re-schedule us to retry on timer expiry. */
1146 end_time
= timeval_sum(&request_time
, &timeout
);
1148 DEBUG(10,("push_deferred_open_message_smb2: "
1150 timeval_string(talloc_tos(),
1154 state
->te
= event_add_timed(smb2req
->sconn
->smb2
.event_ctx
,
1157 smb2_deferred_open_timer
,
1163 /* allow this request to be canceled */
1164 tevent_req_set_cancel_fn(req
, smbd_smb2_create_cancel
);