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/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "../librpc/gen_ndr/ndr_security.h"
28 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level
)
30 switch(in_oplock_level
) {
31 case SMB2_OPLOCK_LEVEL_NONE
:
33 case SMB2_OPLOCK_LEVEL_II
:
34 return LEVEL_II_OPLOCK
;
35 case SMB2_OPLOCK_LEVEL_EXCLUSIVE
:
36 return EXCLUSIVE_OPLOCK
;
37 case SMB2_OPLOCK_LEVEL_BATCH
:
39 case SMB2_OPLOCK_LEVEL_LEASE
:
40 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
41 "LEASE_OPLOCK_REQUESTED\n"));
44 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
46 (unsigned int)in_oplock_level
));
51 static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type
)
53 if (BATCH_OPLOCK_TYPE(oplock_type
)) {
54 return SMB2_OPLOCK_LEVEL_BATCH
;
55 } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type
)) {
56 return SMB2_OPLOCK_LEVEL_EXCLUSIVE
;
57 } else if (oplock_type
== LEVEL_II_OPLOCK
) {
59 * Don't use LEVEL_II_OPLOCK_TYPE here as
60 * this also includes FAKE_LEVEL_II_OPLOCKs
61 * which are internal only.
63 return SMB2_OPLOCK_LEVEL_II
;
65 return SMB2_OPLOCK_LEVEL_NONE
;
69 static struct tevent_req
*smbd_smb2_create_send(TALLOC_CTX
*mem_ctx
,
70 struct tevent_context
*ev
,
71 struct smbd_smb2_request
*smb2req
,
72 uint8_t in_oplock_level
,
73 uint32_t in_impersonation_level
,
74 uint32_t in_desired_access
,
75 uint32_t in_file_attributes
,
76 uint32_t in_share_access
,
77 uint32_t in_create_disposition
,
78 uint32_t in_create_options
,
80 struct smb2_create_blobs in_context_blobs
);
81 static NTSTATUS
smbd_smb2_create_recv(struct tevent_req
*req
,
83 uint8_t *out_oplock_level
,
84 uint32_t *out_create_action
,
85 NTTIME
*out_creation_time
,
86 NTTIME
*out_last_access_time
,
87 NTTIME
*out_last_write_time
,
88 NTTIME
*out_change_time
,
89 uint64_t *out_allocation_size
,
90 uint64_t *out_end_of_file
,
91 uint32_t *out_file_attributes
,
92 uint64_t *out_file_id_persistent
,
93 uint64_t *out_file_id_volatile
,
94 struct smb2_create_blobs
*out_context_blobs
);
96 static void smbd_smb2_request_create_done(struct tevent_req
*tsubreq
);
97 NTSTATUS
smbd_smb2_request_process_create(struct smbd_smb2_request
*smb2req
)
99 const uint8_t *inbody
;
100 int i
= smb2req
->current_idx
;
101 size_t expected_body_size
= 0x39;
103 uint8_t in_oplock_level
;
104 uint32_t in_impersonation_level
;
105 uint32_t in_desired_access
;
106 uint32_t in_file_attributes
;
107 uint32_t in_share_access
;
108 uint32_t in_create_disposition
;
109 uint32_t in_create_options
;
110 uint16_t in_name_offset
;
111 uint16_t in_name_length
;
112 DATA_BLOB in_name_buffer
;
113 char *in_name_string
;
114 size_t in_name_string_size
;
115 uint32_t name_offset
= 0;
116 uint32_t name_available_length
= 0;
117 uint32_t in_context_offset
;
118 uint32_t in_context_length
;
119 DATA_BLOB in_context_buffer
;
120 struct smb2_create_blobs in_context_blobs
;
121 uint32_t context_offset
= 0;
122 uint32_t context_available_length
= 0;
126 struct tevent_req
*tsubreq
;
128 if (smb2req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
129 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
132 inbody
= (const uint8_t *)smb2req
->in
.vector
[i
+1].iov_base
;
134 body_size
= SVAL(inbody
, 0x00);
135 if (body_size
!= expected_body_size
) {
136 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
139 in_oplock_level
= CVAL(inbody
, 0x03);
140 in_impersonation_level
= IVAL(inbody
, 0x04);
141 in_desired_access
= IVAL(inbody
, 0x18);
142 in_file_attributes
= IVAL(inbody
, 0x1C);
143 in_share_access
= IVAL(inbody
, 0x20);
144 in_create_disposition
= IVAL(inbody
, 0x24);
145 in_create_options
= IVAL(inbody
, 0x28);
146 in_name_offset
= SVAL(inbody
, 0x2C);
147 in_name_length
= SVAL(inbody
, 0x2E);
148 in_context_offset
= IVAL(inbody
, 0x30);
149 in_context_length
= IVAL(inbody
, 0x34);
152 * First check if the dynamic name and context buffers
153 * are correctly specified.
155 * Note: That we don't check if the name and context buffers
159 dyn_offset
= SMB2_HDR_BODY
+ (body_size
& 0xFFFFFFFE);
161 if (in_name_offset
== 0 && in_name_length
== 0) {
164 } else if (in_name_offset
< dyn_offset
) {
165 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
167 name_offset
= in_name_offset
- dyn_offset
;
170 if (name_offset
> smb2req
->in
.vector
[i
+2].iov_len
) {
171 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
174 name_available_length
= smb2req
->in
.vector
[i
+2].iov_len
- name_offset
;
176 if (in_name_length
> name_available_length
) {
177 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
180 in_name_buffer
.data
= (uint8_t *)smb2req
->in
.vector
[i
+2].iov_base
+
182 in_name_buffer
.length
= in_name_length
;
184 if (in_context_offset
== 0 && in_context_length
== 0) {
187 } else if (in_context_offset
< dyn_offset
) {
188 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
190 context_offset
= in_context_offset
- dyn_offset
;
193 if (context_offset
> smb2req
->in
.vector
[i
+2].iov_len
) {
194 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
197 context_available_length
= smb2req
->in
.vector
[i
+2].iov_len
- context_offset
;
199 if (in_context_length
> context_available_length
) {
200 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
203 in_context_buffer
.data
= (uint8_t *)smb2req
->in
.vector
[i
+2].iov_base
+
205 in_context_buffer
.length
= in_context_length
;
208 * Now interpret the name and context buffers
211 ok
= convert_string_talloc(smb2req
, CH_UTF16
, CH_UNIX
,
213 in_name_buffer
.length
,
215 &in_name_string_size
, false);
217 return smbd_smb2_request_error(smb2req
, NT_STATUS_ILLEGAL_CHARACTER
);
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
->smb2
.event_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
);
244 return smbd_smb2_request_pending_queue(smb2req
, tsubreq
);
247 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request
*smb2req
)
249 uint8_t *reqhdr
= (uint8_t *)smb2req
->out
.vector
[smb2req
->current_idx
].iov_base
;
250 return BVAL(reqhdr
, SMB2_HDR_MESSAGE_ID
);
253 static void smbd_smb2_request_create_done(struct tevent_req
*tsubreq
)
255 struct smbd_smb2_request
*smb2req
= tevent_req_callback_data(tsubreq
,
256 struct smbd_smb2_request
);
257 int i
= smb2req
->current_idx
;
261 uint8_t out_oplock_level
= 0;
262 uint32_t out_create_action
= 0;
263 NTTIME out_creation_time
= 0;
264 NTTIME out_last_access_time
= 0;
265 NTTIME out_last_write_time
= 0;
266 NTTIME out_change_time
= 0;
267 uint64_t out_allocation_size
= 0;
268 uint64_t out_end_of_file
= 0;
269 uint32_t out_file_attributes
= 0;
270 uint64_t out_file_id_persistent
= 0;
271 uint64_t out_file_id_volatile
= 0;
272 struct smb2_create_blobs out_context_blobs
;
273 DATA_BLOB out_context_buffer
;
274 uint16_t out_context_buffer_offset
= 0;
276 NTSTATUS error
; /* transport error */
278 if (smb2req
->cancelled
) {
279 uint64_t mid
= get_mid_from_smb2req(smb2req
);
280 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
281 (unsigned long long)mid
));
282 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_CANCELLED
);
283 if (!NT_STATUS_IS_OK(error
)) {
284 smbd_server_connection_terminate(smb2req
->sconn
,
291 status
= smbd_smb2_create_recv(tsubreq
,
296 &out_last_access_time
,
297 &out_last_write_time
,
299 &out_allocation_size
,
301 &out_file_attributes
,
302 &out_file_id_persistent
,
303 &out_file_id_volatile
,
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 status
= smb2_create_blob_push(smb2req
, &out_context_buffer
, out_context_blobs
);
316 if (!NT_STATUS_IS_OK(status
)) {
317 error
= smbd_smb2_request_error(smb2req
, status
);
318 if (!NT_STATUS_IS_OK(error
)) {
319 smbd_server_connection_terminate(smb2req
->sconn
,
326 if (out_context_buffer
.length
> 0) {
327 out_context_buffer_offset
= SMB2_HDR_BODY
+ 0x58;
330 outhdr
= (uint8_t *)smb2req
->out
.vector
[i
].iov_base
;
332 outbody
= data_blob_talloc(smb2req
->out
.vector
, NULL
, 0x58);
333 if (outbody
.data
== NULL
) {
334 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
335 if (!NT_STATUS_IS_OK(error
)) {
336 smbd_server_connection_terminate(smb2req
->sconn
,
343 SSVAL(outbody
.data
, 0x00, 0x58 + 1); /* struct size */
344 SCVAL(outbody
.data
, 0x02,
345 out_oplock_level
); /* oplock level */
346 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
347 SIVAL(outbody
.data
, 0x04,
348 out_create_action
); /* create action */
349 SBVAL(outbody
.data
, 0x08,
350 out_creation_time
); /* creation time */
351 SBVAL(outbody
.data
, 0x10,
352 out_last_access_time
); /* last access time */
353 SBVAL(outbody
.data
, 0x18,
354 out_last_write_time
); /* last write time */
355 SBVAL(outbody
.data
, 0x20,
356 out_change_time
); /* change time */
357 SBVAL(outbody
.data
, 0x28,
358 out_allocation_size
); /* allocation size */
359 SBVAL(outbody
.data
, 0x30,
360 out_end_of_file
); /* end of file */
361 SIVAL(outbody
.data
, 0x38,
362 out_file_attributes
); /* file attributes */
363 SIVAL(outbody
.data
, 0x3C, 0); /* reserved */
364 SBVAL(outbody
.data
, 0x40,
365 out_file_id_persistent
); /* file id (persistent) */
366 SBVAL(outbody
.data
, 0x48,
367 out_file_id_volatile
); /* file id (volatile) */
368 SIVAL(outbody
.data
, 0x50,
369 out_context_buffer_offset
); /* create contexts offset */
370 SIVAL(outbody
.data
, 0x54,
371 out_context_buffer
.length
); /* create contexts length */
373 outdyn
= out_context_buffer
;
375 error
= smbd_smb2_request_done(smb2req
, outbody
, &outdyn
);
376 if (!NT_STATUS_IS_OK(error
)) {
377 smbd_server_connection_terminate(smb2req
->sconn
,
383 struct smbd_smb2_create_state
{
384 struct smbd_smb2_request
*smb2req
;
385 struct smb_request
*smb1req
;
386 struct timed_event
*te
;
387 struct tevent_immediate
*im
;
388 struct timeval request_time
;
390 DATA_BLOB private_data
;
391 uint8_t out_oplock_level
;
392 uint32_t out_create_action
;
393 NTTIME out_creation_time
;
394 NTTIME out_last_access_time
;
395 NTTIME out_last_write_time
;
396 NTTIME out_change_time
;
397 uint64_t out_allocation_size
;
398 uint64_t out_end_of_file
;
399 uint32_t out_file_attributes
;
400 uint64_t out_file_id_persistent
;
401 uint64_t out_file_id_volatile
;
402 struct smb2_create_blobs out_context_blobs
;
405 static struct tevent_req
*smbd_smb2_create_send(TALLOC_CTX
*mem_ctx
,
406 struct tevent_context
*ev
,
407 struct smbd_smb2_request
*smb2req
,
408 uint8_t in_oplock_level
,
409 uint32_t in_impersonation_level
,
410 uint32_t in_desired_access
,
411 uint32_t in_file_attributes
,
412 uint32_t in_share_access
,
413 uint32_t in_create_disposition
,
414 uint32_t in_create_options
,
416 struct smb2_create_blobs in_context_blobs
)
418 struct tevent_req
*req
= NULL
;
419 struct smbd_smb2_create_state
*state
= NULL
;
421 struct smb_request
*smb1req
= NULL
;
422 files_struct
*result
= NULL
;
424 struct timespec write_time_ts
;
425 struct smb2_create_blobs out_context_blobs
;
426 int requested_oplock_level
;
428 ZERO_STRUCT(out_context_blobs
);
430 if(lp_fake_oplocks(SNUM(smb2req
->tcon
->compat_conn
))) {
431 requested_oplock_level
= SMB2_OPLOCK_LEVEL_NONE
;
433 requested_oplock_level
= in_oplock_level
;
437 if (!smb2req
->async
) {
438 /* New create call. */
439 req
= tevent_req_create(mem_ctx
, &state
,
440 struct smbd_smb2_create_state
);
444 state
->smb2req
= smb2req
;
445 smb2req
->subreq
= req
; /* So we can find this when going async. */
447 smb1req
= smbd_smb2_fake_smb_request(smb2req
);
448 if (tevent_req_nomem(smb1req
, req
)) {
449 return tevent_req_post(req
, ev
);
451 state
->smb1req
= smb1req
;
452 DEBUG(10,("smbd_smb2_create: name[%s]\n",
455 /* Re-entrant create call. */
456 req
= smb2req
->subreq
;
457 state
= tevent_req_data(req
,
458 struct smbd_smb2_create_state
);
459 smb1req
= state
->smb1req
;
460 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
464 if (IS_IPC(smb1req
->conn
)) {
465 const char *pipe_name
= in_name
;
467 if (!lp_nt_pipe_support()) {
468 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
469 return tevent_req_post(req
, ev
);
472 /* Strip \\ off the name. */
473 if (pipe_name
[0] == '\\') {
477 status
= open_np_file(smb1req
, pipe_name
, &result
);
478 if (!NT_STATUS_IS_OK(status
)) {
479 tevent_req_nterror(req
, status
);
480 return tevent_req_post(req
, ev
);
482 info
= FILE_WAS_OPENED
;
483 } else if (CAN_PRINT(smb1req
->conn
)) {
484 status
= file_new(smb1req
, smb1req
->conn
, &result
);
485 if(!NT_STATUS_IS_OK(status
)) {
486 tevent_req_nterror(req
, status
);
487 return tevent_req_post(req
, ev
);
490 status
= print_spool_open(result
, in_name
,
492 if (!NT_STATUS_IS_OK(status
)) {
493 file_free(smb1req
, result
);
494 tevent_req_nterror(req
, status
);
495 return tevent_req_post(req
, ev
);
497 info
= FILE_WAS_CREATED
;
500 struct smb_filename
*smb_fname
= NULL
;
501 struct smb2_create_blob
*exta
= NULL
;
502 struct ea_list
*ea_list
= NULL
;
503 struct smb2_create_blob
*mxac
= NULL
;
504 NTTIME max_access_time
= 0;
505 struct smb2_create_blob
*secd
= NULL
;
506 struct security_descriptor
*sec_desc
= NULL
;
507 struct smb2_create_blob
*dhnq
= NULL
;
508 struct smb2_create_blob
*dhnc
= NULL
;
509 struct smb2_create_blob
*alsi
= NULL
;
510 uint64_t allocation_size
= 0;
511 struct smb2_create_blob
*twrp
= NULL
;
512 struct smb2_create_blob
*qfid
= NULL
;
514 exta
= smb2_create_blob_find(&in_context_blobs
,
515 SMB2_CREATE_TAG_EXTA
);
516 mxac
= smb2_create_blob_find(&in_context_blobs
,
517 SMB2_CREATE_TAG_MXAC
);
518 secd
= smb2_create_blob_find(&in_context_blobs
,
519 SMB2_CREATE_TAG_SECD
);
520 dhnq
= smb2_create_blob_find(&in_context_blobs
,
521 SMB2_CREATE_TAG_DHNQ
);
522 dhnc
= smb2_create_blob_find(&in_context_blobs
,
523 SMB2_CREATE_TAG_DHNC
);
524 alsi
= smb2_create_blob_find(&in_context_blobs
,
525 SMB2_CREATE_TAG_ALSI
);
526 twrp
= smb2_create_blob_find(&in_context_blobs
,
527 SMB2_CREATE_TAG_TWRP
);
528 qfid
= smb2_create_blob_find(&in_context_blobs
,
529 SMB2_CREATE_TAG_QFID
);
531 fname
= talloc_strdup(state
, in_name
);
532 if (tevent_req_nomem(fname
, req
)) {
533 return tevent_req_post(req
, ev
);
538 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
539 return tevent_req_post(req
, ev
);
542 ea_list
= read_nttrans_ea_list(mem_ctx
,
543 (const char *)exta
->data
.data
, exta
->data
.length
);
545 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
546 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
547 return tevent_req_post(req
, ev
);
553 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
554 return tevent_req_post(req
, ev
);
557 if (mxac
->data
.length
== 0) {
559 } else if (mxac
->data
.length
== 8) {
560 max_access_time
= BVAL(mxac
->data
.data
, 0);
562 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
563 return tevent_req_post(req
, ev
);
568 enum ndr_err_code ndr_err
;
571 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
572 return tevent_req_post(req
, ev
);
575 sec_desc
= talloc_zero(state
, struct security_descriptor
);
576 if (tevent_req_nomem(sec_desc
, req
)) {
577 return tevent_req_post(req
, ev
);
580 ndr_err
= ndr_pull_struct_blob(&secd
->data
,
582 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
583 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
584 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
585 ndr_errstr(ndr_err
)));
586 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
587 return tevent_req_post(req
, ev
);
593 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
594 return tevent_req_post(req
, ev
);
597 if (dhnq
->data
.length
!= 16) {
598 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
599 return tevent_req_post(req
, ev
);
602 * we don't support durable handles yet
603 * and have to ignore this
608 if (dhnc
->data
.length
!= 16) {
609 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
610 return tevent_req_post(req
, ev
);
612 /* we don't support durable handles yet */
613 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
614 return tevent_req_post(req
, ev
);
619 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
620 return tevent_req_post(req
, ev
);
623 if (alsi
->data
.length
!= 8) {
624 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
625 return tevent_req_post(req
, ev
);
627 allocation_size
= BVAL(alsi
->data
.data
, 0);
636 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
637 return tevent_req_post(req
, ev
);
640 if (twrp
->data
.length
!= 8) {
641 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
642 return tevent_req_post(req
, ev
);
645 nttime
= BVAL(twrp
->data
.data
, 0);
646 t
= nt_time_to_unix(nttime
);
650 fname
= talloc_asprintf(state
,
651 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
659 if (tevent_req_nomem(fname
, req
)) {
660 return tevent_req_post(req
, ev
);
665 if (qfid
->data
.length
!= 0) {
666 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
667 return tevent_req_post(req
, ev
);
671 /* these are ignored for SMB2 */
672 in_create_options
&= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
673 in_create_options
&= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
676 * For a DFS path the function parse_dfs_path()
677 * will do the path processing.
680 if (!(smb1req
->flags2
& FLAGS2_DFS_PATHNAMES
)) {
681 /* convert '\\' into '/' */
682 status
= check_path_syntax(fname
);
683 if (!NT_STATUS_IS_OK(status
)) {
684 tevent_req_nterror(req
, status
);
685 return tevent_req_post(req
, ev
);
689 status
= filename_convert(req
,
691 smb1req
->flags2
& FLAGS2_DFS_PATHNAMES
,
696 if (!NT_STATUS_IS_OK(status
)) {
697 tevent_req_nterror(req
, status
);
698 return tevent_req_post(req
, ev
);
701 in_file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
703 status
= SMB_VFS_CREATE_FILE(smb1req
->conn
,
705 0, /* root_dir_fid */
709 in_create_disposition
,
712 map_smb2_oplock_levels_to_samba(requested_oplock_level
),
714 0, /* private_flags */
719 if (!NT_STATUS_IS_OK(status
)) {
720 if (open_was_deferred(smb1req
->mid
)) {
723 tevent_req_nterror(req
, status
);
724 return tevent_req_post(req
, ev
);
728 NTTIME last_write_time
;
730 unix_timespec_to_nt_time(&last_write_time
,
731 result
->fsp_name
->st
.st_ex_mtime
);
732 if (last_write_time
!= max_access_time
) {
734 uint32_t max_access_granted
;
735 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
737 status
= smbd_check_open_rights(smb1req
->conn
,
739 SEC_FLAG_MAXIMUM_ALLOWED
,
740 &max_access_granted
);
742 SIVAL(p
, 0, NT_STATUS_V(status
));
743 SIVAL(p
, 4, max_access_granted
);
745 status
= smb2_create_blob_add(state
,
747 SMB2_CREATE_TAG_MXAC
,
749 if (!NT_STATUS_IS_OK(status
)) {
750 tevent_req_nterror(req
, status
);
751 return tevent_req_post(req
, ev
);
758 uint64_t file_index
= get_FileIndex(result
->conn
,
759 &result
->fsp_name
->st
);
760 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
764 /* From conversations with Microsoft engineers at
765 the MS plugfest. The first 8 bytes are the "volume index"
766 == inode, the second 8 bytes are the "volume id",
767 == dev. This will be updated in the SMB2 doc. */
768 SBVAL(p
, 0, file_index
);
769 SIVAL(p
, 8, result
->fsp_name
->st
.st_ex_dev
);/* FileIndexHigh */
771 status
= smb2_create_blob_add(state
, &out_context_blobs
,
772 SMB2_CREATE_TAG_QFID
,
774 if (!NT_STATUS_IS_OK(status
)) {
775 tevent_req_nterror(req
, status
);
776 return tevent_req_post(req
, ev
);
781 smb2req
->compat_chain_fsp
= smb1req
->chain_fsp
;
783 if(lp_fake_oplocks(SNUM(smb2req
->tcon
->compat_conn
))) {
784 state
->out_oplock_level
= in_oplock_level
;
786 state
->out_oplock_level
= map_samba_oplock_levels_to_smb2(result
->oplock_type
);
789 if ((in_create_disposition
== FILE_SUPERSEDE
)
790 && (info
== FILE_WAS_OVERWRITTEN
)) {
791 state
->out_create_action
= FILE_WAS_SUPERSEDED
;
793 state
->out_create_action
= info
;
795 state
->out_file_attributes
= dos_mode(result
->conn
,
797 /* Deal with other possible opens having a modified
799 ZERO_STRUCT(write_time_ts
);
800 get_file_infos(result
->file_id
, 0, NULL
, &write_time_ts
);
801 if (!null_timespec(write_time_ts
)) {
802 update_stat_ex_mtime(&result
->fsp_name
->st
, write_time_ts
);
805 unix_timespec_to_nt_time(&state
->out_creation_time
,
806 get_create_timespec(smb1req
->conn
, result
,
808 unix_timespec_to_nt_time(&state
->out_last_access_time
,
809 result
->fsp_name
->st
.st_ex_atime
);
810 unix_timespec_to_nt_time(&state
->out_last_write_time
,
811 result
->fsp_name
->st
.st_ex_mtime
);
812 unix_timespec_to_nt_time(&state
->out_change_time
,
813 get_change_timespec(smb1req
->conn
, result
,
815 state
->out_allocation_size
=
816 result
->fsp_name
->st
.st_ex_blksize
*
817 result
->fsp_name
->st
.st_ex_blocks
;
818 state
->out_end_of_file
= result
->fsp_name
->st
.st_ex_size
;
819 if (state
->out_file_attributes
== 0) {
820 state
->out_file_attributes
= FILE_ATTRIBUTE_NORMAL
;
822 state
->out_file_id_persistent
= result
->fnum
;
823 state
->out_file_id_volatile
= result
->fnum
;
824 state
->out_context_blobs
= out_context_blobs
;
826 tevent_req_done(req
);
827 return tevent_req_post(req
, ev
);
830 static NTSTATUS
smbd_smb2_create_recv(struct tevent_req
*req
,
832 uint8_t *out_oplock_level
,
833 uint32_t *out_create_action
,
834 NTTIME
*out_creation_time
,
835 NTTIME
*out_last_access_time
,
836 NTTIME
*out_last_write_time
,
837 NTTIME
*out_change_time
,
838 uint64_t *out_allocation_size
,
839 uint64_t *out_end_of_file
,
840 uint32_t *out_file_attributes
,
841 uint64_t *out_file_id_persistent
,
842 uint64_t *out_file_id_volatile
,
843 struct smb2_create_blobs
*out_context_blobs
)
846 struct smbd_smb2_create_state
*state
= tevent_req_data(req
,
847 struct smbd_smb2_create_state
);
849 if (tevent_req_is_nterror(req
, &status
)) {
850 tevent_req_received(req
);
854 *out_oplock_level
= state
->out_oplock_level
;
855 *out_create_action
= state
->out_create_action
;
856 *out_creation_time
= state
->out_creation_time
;
857 *out_last_access_time
= state
->out_last_access_time
;
858 *out_last_write_time
= state
->out_last_write_time
;
859 *out_change_time
= state
->out_change_time
;
860 *out_allocation_size
= state
->out_allocation_size
;
861 *out_end_of_file
= state
->out_end_of_file
;
862 *out_file_attributes
= state
->out_file_attributes
;
863 *out_file_id_persistent
= state
->out_file_id_persistent
;
864 *out_file_id_volatile
= state
->out_file_id_volatile
;
865 *out_context_blobs
= state
->out_context_blobs
;
867 talloc_steal(mem_ctx
, state
->out_context_blobs
.blobs
);
869 tevent_req_received(req
);
873 /*********************************************************
874 Code for dealing with deferred opens.
875 *********************************************************/
877 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request
*smb2req
,
878 struct timeval
*p_request_time
,
881 struct smbd_smb2_create_state
*state
= NULL
;
882 struct tevent_req
*req
= NULL
;
887 if (!smb2req
->async
) {
890 req
= smb2req
->subreq
;
894 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
898 if (p_request_time
) {
899 *p_request_time
= state
->request_time
;
902 *pp_state
= (void *)state
->private_data
.data
;
907 /*********************************************************
908 Re-process this call early - requested by message or
910 *********************************************************/
912 static struct smbd_smb2_request
*find_open_smb2req(
913 struct smbd_server_connection
*sconn
, uint64_t mid
)
915 struct smbd_smb2_request
*smb2req
;
917 for (smb2req
= sconn
->smb2
.requests
; smb2req
; smb2req
= smb2req
->next
) {
919 if (smb2req
->subreq
== NULL
) {
920 /* This message has been processed. */
923 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
924 /* This message has been processed. */
927 message_id
= get_mid_from_smb2req(smb2req
);
928 if (message_id
== mid
) {
935 bool open_was_deferred_smb2(struct smbd_server_connection
*sconn
, uint64_t mid
)
937 struct smbd_smb2_create_state
*state
= NULL
;
938 struct smbd_smb2_request
*smb2req
;
940 smb2req
= find_open_smb2req(sconn
, mid
);
943 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
944 (unsigned long long)mid
));
947 if (!smb2req
->subreq
) {
950 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
953 state
= tevent_req_data(smb2req
->subreq
,
954 struct smbd_smb2_create_state
);
958 /* It's not in progress if there's no timeout event. */
963 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
964 (unsigned long long)mid
));
969 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request
*smb2req
,
972 struct smbd_smb2_create_state
*state
= NULL
;
974 if (!smb2req
->subreq
) {
977 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
980 state
= tevent_req_data(smb2req
->subreq
,
981 struct smbd_smb2_create_state
);
986 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
988 (unsigned long long)mid
));
990 /* Ensure we don't have any outstanding timer event. */
991 TALLOC_FREE(state
->te
);
992 /* Ensure we don't have any outstanding immediate event. */
993 TALLOC_FREE(state
->im
);
996 void remove_deferred_open_message_smb2(
997 struct smbd_server_connection
*sconn
, uint64_t mid
)
999 struct smbd_smb2_request
*smb2req
;
1001 smb2req
= find_open_smb2req(sconn
, mid
);
1004 DEBUG(10,("remove_deferred_open_message_smb2: "
1005 "can't find mid %llu\n",
1006 (unsigned long long)mid
));
1009 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
1012 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context
*ctx
,
1013 struct tevent_immediate
*im
,
1016 struct smbd_smb2_request
*smb2req
= talloc_get_type_abort(private_data
,
1017 struct smbd_smb2_request
);
1018 struct smbd_server_connection
*sconn
= smb2req
->sconn
;
1019 uint64_t mid
= get_mid_from_smb2req(smb2req
);
1022 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1023 "re-dispatching mid %llu\n",
1024 (unsigned long long)mid
));
1026 status
= smbd_smb2_request_dispatch(smb2req
);
1027 if (!NT_STATUS_IS_OK(status
)) {
1028 smbd_server_connection_terminate(sconn
, nt_errstr(status
));
1033 void schedule_deferred_open_message_smb2(
1034 struct smbd_server_connection
*sconn
, uint64_t mid
)
1036 struct smbd_smb2_create_state
*state
= NULL
;
1037 struct smbd_smb2_request
*smb2req
;
1039 smb2req
= find_open_smb2req(sconn
, mid
);
1042 DEBUG(10,("schedule_deferred_open_message_smb2: "
1043 "can't find mid %llu\n",
1044 (unsigned long long)mid
));
1047 if (!smb2req
->subreq
) {
1050 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1053 state
= tevent_req_data(smb2req
->subreq
,
1054 struct smbd_smb2_create_state
);
1059 /* Ensure we don't have any outstanding timer event. */
1060 TALLOC_FREE(state
->te
);
1061 /* Ensure we don't have any outstanding immediate event. */
1062 TALLOC_FREE(state
->im
);
1065 * This is subtle. We must null out the callback
1066 * before resheduling, else the first call to
1067 * tevent_req_nterror() causes the _receive()
1068 * function to be called, this causing tevent_req_post()
1071 tevent_req_set_callback(smb2req
->subreq
, NULL
, NULL
);
1073 state
->im
= tevent_create_immediate(smb2req
);
1075 smbd_server_connection_terminate(smb2req
->sconn
,
1076 nt_errstr(NT_STATUS_NO_MEMORY
));
1079 DEBUG(10,("schedule_deferred_open_message_smb2: "
1080 "re-processing mid %llu\n",
1081 (unsigned long long)mid
));
1083 tevent_schedule_immediate(state
->im
,
1084 smb2req
->sconn
->smb2
.event_ctx
,
1085 smbd_smb2_create_request_dispatch_immediate
,
1089 /*********************************************************
1090 Re-process this call.
1091 *********************************************************/
1093 static void smb2_deferred_open_timer(struct event_context
*ev
,
1094 struct timed_event
*te
,
1095 struct timeval _tval
,
1099 struct smbd_smb2_create_state
*state
= NULL
;
1100 struct smbd_smb2_request
*smb2req
= talloc_get_type(private_data
,
1101 struct smbd_smb2_request
);
1103 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1104 smb2req
->current_idx
,
1105 tevent_req_default_print(smb2req
->subreq
, talloc_tos()) ));
1107 state
= tevent_req_data(smb2req
->subreq
,
1108 struct smbd_smb2_create_state
);
1113 * Null this out, don't talloc_free. It will
1114 * be talloc_free'd by the tevent library when
1118 /* Ensure we don't have any outstanding immediate event. */
1119 TALLOC_FREE(state
->im
);
1122 * This is subtle. We must null out the callback
1123 * before resheduling, else the first call to
1124 * tevent_req_nterror() causes the _receive()
1125 * function to be called, this causing tevent_req_post()
1128 tevent_req_set_callback(smb2req
->subreq
, NULL
, NULL
);
1130 status
= smbd_smb2_request_dispatch(smb2req
);
1132 if (!NT_STATUS_IS_OK(status
)) {
1133 smbd_server_connection_terminate(smb2req
->sconn
,
1138 static bool smbd_smb2_create_cancel(struct tevent_req
*req
)
1140 struct smbd_smb2_request
*smb2req
= NULL
;
1141 struct smbd_smb2_create_state
*state
= tevent_req_data(req
,
1142 struct smbd_smb2_create_state
);
1149 if (!state
->smb2req
) {
1153 smb2req
= state
->smb2req
;
1154 mid
= get_mid_from_smb2req(smb2req
);
1156 remove_deferred_open_entry(state
->id
, mid
,
1157 sconn_server_id(smb2req
->sconn
));
1158 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
1159 smb2req
->cancelled
= true;
1161 tevent_req_done(req
);
1165 bool push_deferred_open_message_smb2(struct smbd_smb2_request
*smb2req
,
1166 struct timeval request_time
,
1167 struct timeval timeout
,
1172 struct tevent_req
*req
= NULL
;
1173 struct smbd_smb2_create_state
*state
= NULL
;
1174 struct timeval end_time
;
1179 req
= smb2req
->subreq
;
1183 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
1188 state
->request_time
= request_time
;
1189 state
->private_data
= data_blob_talloc(state
, private_data
,
1191 if (!state
->private_data
.data
) {
1196 /* Boo - turns out this isn't what W2K8R2
1197 does. It actually sends the STATUS_PENDING
1198 message followed by the STATUS_SHARING_VIOLATION
1199 message. Surely this means that all open
1200 calls (even on directories) will potentially
1201 fail in a chain.... ? And I've seen directory
1202 opens as the start of a chain. JRA.
1204 Update: 19th May 2010. Talking with Microsoft
1205 engineers at the plugfest this is a bug in
1206 Windows. Re-enable this code.
1209 * More subtlety. To match W2K8R2 don't
1210 * send a "gone async" message if it's simply
1211 * a STATUS_SHARING_VIOLATION (short) wait, not
1212 * an oplock break wait. We do this by prematurely
1213 * setting smb2req->async flag.
1215 if (timeout
.tv_sec
< 2) {
1216 DEBUG(10,("push_deferred_open_message_smb2: "
1217 "short timer wait (usec = %u). "
1218 "Don't send async message.\n",
1219 (unsigned int)timeout
.tv_usec
));
1220 smb2req
->async
= true;
1224 /* Re-schedule us to retry on timer expiry. */
1225 end_time
= timeval_sum(&request_time
, &timeout
);
1227 DEBUG(10,("push_deferred_open_message_smb2: "
1229 timeval_string(talloc_tos(),
1233 state
->te
= event_add_timed(smb2req
->sconn
->smb2
.event_ctx
,
1236 smb2_deferred_open_timer
,
1242 /* allow this request to be canceled */
1243 tevent_req_set_cancel_fn(req
, smbd_smb2_create_cancel
);