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 int i
= smb2req
->current_idx
;
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
= (const uint8_t *)smb2req
->in
.vector
[i
+1].iov_base
;
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
+ smb2req
->in
.vector
[i
+1].iov_len
;
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 if (name_offset
> smb2req
->in
.vector
[i
+2].iov_len
) {
167 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
170 name_available_length
= smb2req
->in
.vector
[i
+2].iov_len
- name_offset
;
172 if (in_name_length
> name_available_length
) {
173 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
176 in_name_buffer
.data
= (uint8_t *)smb2req
->in
.vector
[i
+2].iov_base
+
178 in_name_buffer
.length
= in_name_length
;
180 if (in_context_offset
== 0 && in_context_length
== 0) {
183 } else if (in_context_offset
< dyn_offset
) {
184 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
186 context_offset
= in_context_offset
- dyn_offset
;
189 if (context_offset
> smb2req
->in
.vector
[i
+2].iov_len
) {
190 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
193 context_available_length
= smb2req
->in
.vector
[i
+2].iov_len
- context_offset
;
195 if (in_context_length
> context_available_length
) {
196 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
199 in_context_buffer
.data
= (uint8_t *)smb2req
->in
.vector
[i
+2].iov_base
+
201 in_context_buffer
.length
= in_context_length
;
204 * Now interpret the name and context buffers
207 ok
= convert_string_talloc(smb2req
, CH_UTF16
, CH_UNIX
,
209 in_name_buffer
.length
,
211 &in_name_string_size
);
213 return smbd_smb2_request_error(smb2req
, NT_STATUS_ILLEGAL_CHARACTER
);
216 if (in_name_buffer
.length
== 0) {
217 in_name_string_size
= 0;
220 if (strlen(in_name_string
) != in_name_string_size
) {
221 return smbd_smb2_request_error(smb2req
, NT_STATUS_OBJECT_NAME_INVALID
);
224 ZERO_STRUCT(in_context_blobs
);
225 status
= smb2_create_blob_parse(smb2req
, in_context_buffer
, &in_context_blobs
);
226 if (!NT_STATUS_IS_OK(status
)) {
227 return smbd_smb2_request_error(smb2req
, status
);
230 tsubreq
= smbd_smb2_create_send(smb2req
,
231 smb2req
->sconn
->ev_ctx
,
234 in_impersonation_level
,
238 in_create_disposition
,
242 if (tsubreq
== NULL
) {
243 smb2req
->subreq
= NULL
;
244 return smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
246 tevent_req_set_callback(tsubreq
, smbd_smb2_request_create_done
, smb2req
);
249 * For now we keep the logic that we do not send STATUS_PENDING
250 * for sharing violations, so we just wait 2 seconds.
252 * TODO: we need more tests for this.
254 return smbd_smb2_request_pending_queue(smb2req
, tsubreq
, 2000000);
257 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request
*smb2req
)
259 uint8_t *reqhdr
= (uint8_t *)smb2req
->out
.vector
[smb2req
->current_idx
].iov_base
;
260 return BVAL(reqhdr
, SMB2_HDR_MESSAGE_ID
);
263 static void smbd_smb2_request_create_done(struct tevent_req
*tsubreq
)
265 struct smbd_smb2_request
*smb2req
= tevent_req_callback_data(tsubreq
,
266 struct smbd_smb2_request
);
267 int i
= smb2req
->current_idx
;
271 uint8_t out_oplock_level
= 0;
272 uint32_t out_create_action
= 0;
273 NTTIME out_creation_time
= 0;
274 NTTIME out_last_access_time
= 0;
275 NTTIME out_last_write_time
= 0;
276 NTTIME out_change_time
= 0;
277 uint64_t out_allocation_size
= 0;
278 uint64_t out_end_of_file
= 0;
279 uint32_t out_file_attributes
= 0;
280 uint64_t out_file_id_persistent
= 0;
281 uint64_t out_file_id_volatile
= 0;
282 struct smb2_create_blobs out_context_blobs
;
283 DATA_BLOB out_context_buffer
;
284 uint16_t out_context_buffer_offset
= 0;
286 NTSTATUS error
; /* transport error */
288 if (smb2req
->cancelled
) {
289 uint64_t mid
= get_mid_from_smb2req(smb2req
);
290 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
291 (unsigned long long)mid
));
292 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_CANCELLED
);
293 if (!NT_STATUS_IS_OK(error
)) {
294 smbd_server_connection_terminate(smb2req
->sconn
,
301 status
= smbd_smb2_create_recv(tsubreq
,
306 &out_last_access_time
,
307 &out_last_write_time
,
309 &out_allocation_size
,
311 &out_file_attributes
,
312 &out_file_id_persistent
,
313 &out_file_id_volatile
,
315 if (!NT_STATUS_IS_OK(status
)) {
316 error
= smbd_smb2_request_error(smb2req
, status
);
317 if (!NT_STATUS_IS_OK(error
)) {
318 smbd_server_connection_terminate(smb2req
->sconn
,
325 status
= smb2_create_blob_push(smb2req
, &out_context_buffer
, out_context_blobs
);
326 if (!NT_STATUS_IS_OK(status
)) {
327 error
= smbd_smb2_request_error(smb2req
, status
);
328 if (!NT_STATUS_IS_OK(error
)) {
329 smbd_server_connection_terminate(smb2req
->sconn
,
336 if (out_context_buffer
.length
> 0) {
337 out_context_buffer_offset
= SMB2_HDR_BODY
+ 0x58;
340 outhdr
= (uint8_t *)smb2req
->out
.vector
[i
].iov_base
;
342 outbody
= data_blob_talloc(smb2req
->out
.vector
, NULL
, 0x58);
343 if (outbody
.data
== NULL
) {
344 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
345 if (!NT_STATUS_IS_OK(error
)) {
346 smbd_server_connection_terminate(smb2req
->sconn
,
353 SSVAL(outbody
.data
, 0x00, 0x58 + 1); /* struct size */
354 SCVAL(outbody
.data
, 0x02,
355 out_oplock_level
); /* oplock level */
356 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
357 SIVAL(outbody
.data
, 0x04,
358 out_create_action
); /* create action */
359 SBVAL(outbody
.data
, 0x08,
360 out_creation_time
); /* creation time */
361 SBVAL(outbody
.data
, 0x10,
362 out_last_access_time
); /* last access time */
363 SBVAL(outbody
.data
, 0x18,
364 out_last_write_time
); /* last write time */
365 SBVAL(outbody
.data
, 0x20,
366 out_change_time
); /* change time */
367 SBVAL(outbody
.data
, 0x28,
368 out_allocation_size
); /* allocation size */
369 SBVAL(outbody
.data
, 0x30,
370 out_end_of_file
); /* end of file */
371 SIVAL(outbody
.data
, 0x38,
372 out_file_attributes
); /* file attributes */
373 SIVAL(outbody
.data
, 0x3C, 0); /* reserved */
374 SBVAL(outbody
.data
, 0x40,
375 out_file_id_persistent
); /* file id (persistent) */
376 SBVAL(outbody
.data
, 0x48,
377 out_file_id_volatile
); /* file id (volatile) */
378 SIVAL(outbody
.data
, 0x50,
379 out_context_buffer_offset
); /* create contexts offset */
380 SIVAL(outbody
.data
, 0x54,
381 out_context_buffer
.length
); /* create contexts length */
383 outdyn
= out_context_buffer
;
385 error
= smbd_smb2_request_done(smb2req
, outbody
, &outdyn
);
386 if (!NT_STATUS_IS_OK(error
)) {
387 smbd_server_connection_terminate(smb2req
->sconn
,
393 struct smbd_smb2_create_state
{
394 struct smbd_smb2_request
*smb2req
;
395 struct smb_request
*smb1req
;
396 struct timed_event
*te
;
397 struct tevent_immediate
*im
;
398 struct timeval request_time
;
400 DATA_BLOB private_data
;
401 uint8_t out_oplock_level
;
402 uint32_t out_create_action
;
403 NTTIME out_creation_time
;
404 NTTIME out_last_access_time
;
405 NTTIME out_last_write_time
;
406 NTTIME out_change_time
;
407 uint64_t out_allocation_size
;
408 uint64_t out_end_of_file
;
409 uint32_t out_file_attributes
;
410 uint64_t out_file_id_persistent
;
411 uint64_t out_file_id_volatile
;
412 struct smb2_create_blobs out_context_blobs
;
415 static struct tevent_req
*smbd_smb2_create_send(TALLOC_CTX
*mem_ctx
,
416 struct tevent_context
*ev
,
417 struct smbd_smb2_request
*smb2req
,
418 uint8_t in_oplock_level
,
419 uint32_t in_impersonation_level
,
420 uint32_t in_desired_access
,
421 uint32_t in_file_attributes
,
422 uint32_t in_share_access
,
423 uint32_t in_create_disposition
,
424 uint32_t in_create_options
,
426 struct smb2_create_blobs in_context_blobs
)
428 struct tevent_req
*req
= NULL
;
429 struct smbd_smb2_create_state
*state
= NULL
;
431 struct smb_request
*smb1req
= NULL
;
432 files_struct
*result
= NULL
;
434 struct timespec write_time_ts
;
435 struct smb2_create_blobs out_context_blobs
;
436 int requested_oplock_level
;
438 ZERO_STRUCT(out_context_blobs
);
440 if(lp_fake_oplocks(SNUM(smb2req
->tcon
->compat_conn
))) {
441 requested_oplock_level
= SMB2_OPLOCK_LEVEL_NONE
;
443 requested_oplock_level
= in_oplock_level
;
447 if (smb2req
->subreq
== NULL
) {
448 /* New create call. */
449 req
= tevent_req_create(mem_ctx
, &state
,
450 struct smbd_smb2_create_state
);
454 state
->smb2req
= smb2req
;
456 smb1req
= smbd_smb2_fake_smb_request(smb2req
);
457 if (tevent_req_nomem(smb1req
, req
)) {
458 return tevent_req_post(req
, ev
);
460 state
->smb1req
= smb1req
;
461 DEBUG(10,("smbd_smb2_create: name[%s]\n",
464 /* Re-entrant create call. */
465 req
= smb2req
->subreq
;
466 state
= tevent_req_data(req
,
467 struct smbd_smb2_create_state
);
468 smb1req
= state
->smb1req
;
469 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
473 if (IS_IPC(smb1req
->conn
)) {
474 const char *pipe_name
= in_name
;
476 if (!lp_nt_pipe_support()) {
477 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
478 return tevent_req_post(req
, ev
);
481 /* Strip \\ off the name. */
482 if (pipe_name
[0] == '\\') {
486 status
= open_np_file(smb1req
, pipe_name
, &result
);
487 if (!NT_STATUS_IS_OK(status
)) {
488 tevent_req_nterror(req
, status
);
489 return tevent_req_post(req
, ev
);
491 info
= FILE_WAS_OPENED
;
492 } else if (CAN_PRINT(smb1req
->conn
)) {
493 status
= file_new(smb1req
, smb1req
->conn
, &result
);
494 if(!NT_STATUS_IS_OK(status
)) {
495 tevent_req_nterror(req
, status
);
496 return tevent_req_post(req
, ev
);
499 status
= print_spool_open(result
, in_name
,
501 if (!NT_STATUS_IS_OK(status
)) {
502 file_free(smb1req
, result
);
503 tevent_req_nterror(req
, status
);
504 return tevent_req_post(req
, ev
);
506 info
= FILE_WAS_CREATED
;
509 struct smb_filename
*smb_fname
= NULL
;
510 struct smb2_create_blob
*exta
= NULL
;
511 struct ea_list
*ea_list
= NULL
;
512 struct smb2_create_blob
*mxac
= NULL
;
513 NTTIME max_access_time
= 0;
514 struct smb2_create_blob
*secd
= NULL
;
515 struct security_descriptor
*sec_desc
= NULL
;
516 struct smb2_create_blob
*dhnq
= NULL
;
517 struct smb2_create_blob
*dhnc
= NULL
;
518 struct smb2_create_blob
*alsi
= NULL
;
519 uint64_t allocation_size
= 0;
520 struct smb2_create_blob
*twrp
= NULL
;
521 struct smb2_create_blob
*qfid
= NULL
;
523 exta
= smb2_create_blob_find(&in_context_blobs
,
524 SMB2_CREATE_TAG_EXTA
);
525 mxac
= smb2_create_blob_find(&in_context_blobs
,
526 SMB2_CREATE_TAG_MXAC
);
527 secd
= smb2_create_blob_find(&in_context_blobs
,
528 SMB2_CREATE_TAG_SECD
);
529 dhnq
= smb2_create_blob_find(&in_context_blobs
,
530 SMB2_CREATE_TAG_DHNQ
);
531 dhnc
= smb2_create_blob_find(&in_context_blobs
,
532 SMB2_CREATE_TAG_DHNC
);
533 alsi
= smb2_create_blob_find(&in_context_blobs
,
534 SMB2_CREATE_TAG_ALSI
);
535 twrp
= smb2_create_blob_find(&in_context_blobs
,
536 SMB2_CREATE_TAG_TWRP
);
537 qfid
= smb2_create_blob_find(&in_context_blobs
,
538 SMB2_CREATE_TAG_QFID
);
540 fname
= talloc_strdup(state
, in_name
);
541 if (tevent_req_nomem(fname
, req
)) {
542 return tevent_req_post(req
, ev
);
547 tevent_req_nterror(req
,NT_STATUS_OBJECT_NAME_NOT_FOUND
);
548 return tevent_req_post(req
, ev
);
551 ea_list
= read_nttrans_ea_list(mem_ctx
,
552 (const char *)exta
->data
.data
, exta
->data
.length
);
554 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
555 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
556 return tevent_req_post(req
, ev
);
562 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
563 return tevent_req_post(req
, ev
);
566 if (mxac
->data
.length
== 0) {
568 } else if (mxac
->data
.length
== 8) {
569 max_access_time
= BVAL(mxac
->data
.data
, 0);
571 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
572 return tevent_req_post(req
, ev
);
577 enum ndr_err_code ndr_err
;
580 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
581 return tevent_req_post(req
, ev
);
584 sec_desc
= talloc_zero(state
, struct security_descriptor
);
585 if (tevent_req_nomem(sec_desc
, req
)) {
586 return tevent_req_post(req
, ev
);
589 ndr_err
= ndr_pull_struct_blob(&secd
->data
,
591 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
592 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
593 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
594 ndr_errstr(ndr_err
)));
595 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
596 return tevent_req_post(req
, ev
);
602 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
603 return tevent_req_post(req
, ev
);
606 if (dhnq
->data
.length
!= 16) {
607 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
608 return tevent_req_post(req
, ev
);
611 * we don't support durable handles yet
612 * and have to ignore this
617 if (dhnc
->data
.length
!= 16) {
618 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
619 return tevent_req_post(req
, ev
);
621 /* we don't support durable handles yet */
622 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
623 return tevent_req_post(req
, ev
);
628 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
629 return tevent_req_post(req
, ev
);
632 if (alsi
->data
.length
!= 8) {
633 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
634 return tevent_req_post(req
, ev
);
636 allocation_size
= BVAL(alsi
->data
.data
, 0);
645 tevent_req_nterror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
646 return tevent_req_post(req
, ev
);
649 if (twrp
->data
.length
!= 8) {
650 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
651 return tevent_req_post(req
, ev
);
654 nttime
= BVAL(twrp
->data
.data
, 0);
655 t
= nt_time_to_unix(nttime
);
659 fname
= talloc_asprintf(state
,
660 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
668 if (tevent_req_nomem(fname
, req
)) {
669 return tevent_req_post(req
, ev
);
674 if (qfid
->data
.length
!= 0) {
675 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
676 return tevent_req_post(req
, ev
);
680 /* these are ignored for SMB2 */
681 in_create_options
&= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
682 in_create_options
&= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
685 * For a DFS path the function parse_dfs_path()
686 * will do the path processing.
689 if (!(smb1req
->flags2
& FLAGS2_DFS_PATHNAMES
)) {
690 /* convert '\\' into '/' */
691 status
= check_path_syntax(fname
);
692 if (!NT_STATUS_IS_OK(status
)) {
693 tevent_req_nterror(req
, status
);
694 return tevent_req_post(req
, ev
);
698 status
= filename_convert(req
,
700 smb1req
->flags2
& FLAGS2_DFS_PATHNAMES
,
705 if (!NT_STATUS_IS_OK(status
)) {
706 tevent_req_nterror(req
, status
);
707 return tevent_req_post(req
, ev
);
710 in_file_attributes
&= ~FILE_FLAG_POSIX_SEMANTICS
;
712 status
= SMB_VFS_CREATE_FILE(smb1req
->conn
,
714 0, /* root_dir_fid */
718 in_create_disposition
,
721 map_smb2_oplock_levels_to_samba(requested_oplock_level
),
723 0, /* private_flags */
728 if (!NT_STATUS_IS_OK(status
)) {
729 if (open_was_deferred(smb1req
->sconn
, smb1req
->mid
)) {
732 tevent_req_nterror(req
, status
);
733 return tevent_req_post(req
, ev
);
737 NTTIME last_write_time
;
739 unix_timespec_to_nt_time(&last_write_time
,
740 result
->fsp_name
->st
.st_ex_mtime
);
741 if (last_write_time
!= max_access_time
) {
743 uint32_t max_access_granted
;
744 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
746 status
= smbd_calculate_access_mask(smb1req
->conn
,
753 SEC_FLAG_MAXIMUM_ALLOWED
,
754 &max_access_granted
);
756 SIVAL(p
, 0, NT_STATUS_V(status
));
757 SIVAL(p
, 4, max_access_granted
);
759 status
= smb2_create_blob_add(state
,
761 SMB2_CREATE_TAG_MXAC
,
763 if (!NT_STATUS_IS_OK(status
)) {
764 tevent_req_nterror(req
, status
);
765 return tevent_req_post(req
, ev
);
772 uint64_t file_index
= get_FileIndex(result
->conn
,
773 &result
->fsp_name
->st
);
774 DATA_BLOB blob
= data_blob_const(p
, sizeof(p
));
778 /* From conversations with Microsoft engineers at
779 the MS plugfest. The first 8 bytes are the "volume index"
780 == inode, the second 8 bytes are the "volume id",
781 == dev. This will be updated in the SMB2 doc. */
782 SBVAL(p
, 0, file_index
);
783 SIVAL(p
, 8, result
->fsp_name
->st
.st_ex_dev
);/* FileIndexHigh */
785 status
= smb2_create_blob_add(state
, &out_context_blobs
,
786 SMB2_CREATE_TAG_QFID
,
788 if (!NT_STATUS_IS_OK(status
)) {
789 tevent_req_nterror(req
, status
);
790 return tevent_req_post(req
, ev
);
795 smb2req
->compat_chain_fsp
= smb1req
->chain_fsp
;
797 if(lp_fake_oplocks(SNUM(smb2req
->tcon
->compat_conn
))) {
798 state
->out_oplock_level
= in_oplock_level
;
800 state
->out_oplock_level
= map_samba_oplock_levels_to_smb2(result
->oplock_type
);
803 if ((in_create_disposition
== FILE_SUPERSEDE
)
804 && (info
== FILE_WAS_OVERWRITTEN
)) {
805 state
->out_create_action
= FILE_WAS_SUPERSEDED
;
807 state
->out_create_action
= info
;
809 state
->out_file_attributes
= dos_mode(result
->conn
,
811 /* Deal with other possible opens having a modified
813 ZERO_STRUCT(write_time_ts
);
814 get_file_infos(result
->file_id
, 0, NULL
, &write_time_ts
);
815 if (!null_timespec(write_time_ts
)) {
816 update_stat_ex_mtime(&result
->fsp_name
->st
, write_time_ts
);
819 unix_timespec_to_nt_time(&state
->out_creation_time
,
820 get_create_timespec(smb1req
->conn
, result
,
822 unix_timespec_to_nt_time(&state
->out_last_access_time
,
823 result
->fsp_name
->st
.st_ex_atime
);
824 unix_timespec_to_nt_time(&state
->out_last_write_time
,
825 result
->fsp_name
->st
.st_ex_mtime
);
826 unix_timespec_to_nt_time(&state
->out_change_time
,
827 get_change_timespec(smb1req
->conn
, result
,
829 state
->out_allocation_size
=
830 SMB_VFS_GET_ALLOC_SIZE(smb1req
->conn
, result
,
831 &(result
->fsp_name
->st
));
832 state
->out_end_of_file
= result
->fsp_name
->st
.st_ex_size
;
833 if (state
->out_file_attributes
== 0) {
834 state
->out_file_attributes
= FILE_ATTRIBUTE_NORMAL
;
836 state
->out_file_id_persistent
= result
->fnum
;
837 state
->out_file_id_volatile
= result
->fnum
;
838 state
->out_context_blobs
= out_context_blobs
;
840 tevent_req_done(req
);
841 return tevent_req_post(req
, ev
);
844 static NTSTATUS
smbd_smb2_create_recv(struct tevent_req
*req
,
846 uint8_t *out_oplock_level
,
847 uint32_t *out_create_action
,
848 NTTIME
*out_creation_time
,
849 NTTIME
*out_last_access_time
,
850 NTTIME
*out_last_write_time
,
851 NTTIME
*out_change_time
,
852 uint64_t *out_allocation_size
,
853 uint64_t *out_end_of_file
,
854 uint32_t *out_file_attributes
,
855 uint64_t *out_file_id_persistent
,
856 uint64_t *out_file_id_volatile
,
857 struct smb2_create_blobs
*out_context_blobs
)
860 struct smbd_smb2_create_state
*state
= tevent_req_data(req
,
861 struct smbd_smb2_create_state
);
863 if (tevent_req_is_nterror(req
, &status
)) {
864 tevent_req_received(req
);
868 *out_oplock_level
= state
->out_oplock_level
;
869 *out_create_action
= state
->out_create_action
;
870 *out_creation_time
= state
->out_creation_time
;
871 *out_last_access_time
= state
->out_last_access_time
;
872 *out_last_write_time
= state
->out_last_write_time
;
873 *out_change_time
= state
->out_change_time
;
874 *out_allocation_size
= state
->out_allocation_size
;
875 *out_end_of_file
= state
->out_end_of_file
;
876 *out_file_attributes
= state
->out_file_attributes
;
877 *out_file_id_persistent
= state
->out_file_id_persistent
;
878 *out_file_id_volatile
= state
->out_file_id_volatile
;
879 *out_context_blobs
= state
->out_context_blobs
;
881 talloc_steal(mem_ctx
, state
->out_context_blobs
.blobs
);
883 tevent_req_received(req
);
887 /*********************************************************
888 Code for dealing with deferred opens.
889 *********************************************************/
891 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request
*smb2req
,
892 struct timeval
*p_request_time
,
895 struct smbd_smb2_create_state
*state
= NULL
;
896 struct tevent_req
*req
= NULL
;
901 if (smb2req
->subreq
== NULL
) {
904 req
= smb2req
->subreq
;
908 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
912 if (p_request_time
) {
913 *p_request_time
= state
->request_time
;
916 *pp_state
= (void *)state
->private_data
.data
;
921 /*********************************************************
922 Re-process this call early - requested by message or
924 *********************************************************/
926 static struct smbd_smb2_request
*find_open_smb2req(
927 struct smbd_server_connection
*sconn
, uint64_t mid
)
929 struct smbd_smb2_request
*smb2req
;
931 for (smb2req
= sconn
->smb2
.requests
; smb2req
; smb2req
= smb2req
->next
) {
933 if (smb2req
->subreq
== NULL
) {
934 /* This message has been processed. */
937 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
938 /* This message has been processed. */
941 message_id
= get_mid_from_smb2req(smb2req
);
942 if (message_id
== mid
) {
949 bool open_was_deferred_smb2(struct smbd_server_connection
*sconn
, uint64_t mid
)
951 struct smbd_smb2_create_state
*state
= NULL
;
952 struct smbd_smb2_request
*smb2req
;
954 smb2req
= find_open_smb2req(sconn
, mid
);
957 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
958 (unsigned long long)mid
));
961 if (!smb2req
->subreq
) {
964 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
967 state
= tevent_req_data(smb2req
->subreq
,
968 struct smbd_smb2_create_state
);
972 /* It's not in progress if there's no timeout event. */
977 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
978 (unsigned long long)mid
));
983 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request
*smb2req
,
986 struct smbd_smb2_create_state
*state
= NULL
;
988 if (!smb2req
->subreq
) {
991 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
994 state
= tevent_req_data(smb2req
->subreq
,
995 struct smbd_smb2_create_state
);
1000 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
1002 (unsigned long long)mid
));
1004 /* Ensure we don't have any outstanding timer event. */
1005 TALLOC_FREE(state
->te
);
1006 /* Ensure we don't have any outstanding immediate event. */
1007 TALLOC_FREE(state
->im
);
1010 void remove_deferred_open_message_smb2(
1011 struct smbd_server_connection
*sconn
, uint64_t mid
)
1013 struct smbd_smb2_request
*smb2req
;
1015 smb2req
= find_open_smb2req(sconn
, mid
);
1018 DEBUG(10,("remove_deferred_open_message_smb2: "
1019 "can't find mid %llu\n",
1020 (unsigned long long)mid
));
1023 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
1026 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context
*ctx
,
1027 struct tevent_immediate
*im
,
1030 struct smbd_smb2_request
*smb2req
= talloc_get_type_abort(private_data
,
1031 struct smbd_smb2_request
);
1032 struct smbd_server_connection
*sconn
= smb2req
->sconn
;
1033 uint64_t mid
= get_mid_from_smb2req(smb2req
);
1036 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1037 "re-dispatching mid %llu\n",
1038 (unsigned long long)mid
));
1040 status
= smbd_smb2_request_dispatch(smb2req
);
1041 if (!NT_STATUS_IS_OK(status
)) {
1042 smbd_server_connection_terminate(sconn
, nt_errstr(status
));
1047 void schedule_deferred_open_message_smb2(
1048 struct smbd_server_connection
*sconn
, uint64_t mid
)
1050 struct smbd_smb2_create_state
*state
= NULL
;
1051 struct smbd_smb2_request
*smb2req
;
1053 smb2req
= find_open_smb2req(sconn
, mid
);
1056 DEBUG(10,("schedule_deferred_open_message_smb2: "
1057 "can't find mid %llu\n",
1058 (unsigned long long)mid
));
1061 if (!smb2req
->subreq
) {
1064 if (!tevent_req_is_in_progress(smb2req
->subreq
)) {
1067 state
= tevent_req_data(smb2req
->subreq
,
1068 struct smbd_smb2_create_state
);
1073 /* Ensure we don't have any outstanding timer event. */
1074 TALLOC_FREE(state
->te
);
1075 /* Ensure we don't have any outstanding immediate event. */
1076 TALLOC_FREE(state
->im
);
1079 * This is subtle. We must null out the callback
1080 * before resheduling, else the first call to
1081 * tevent_req_nterror() causes the _receive()
1082 * function to be called, this causing tevent_req_post()
1085 tevent_req_set_callback(smb2req
->subreq
, NULL
, NULL
);
1087 state
->im
= tevent_create_immediate(smb2req
);
1089 smbd_server_connection_terminate(smb2req
->sconn
,
1090 nt_errstr(NT_STATUS_NO_MEMORY
));
1094 DEBUG(10,("schedule_deferred_open_message_smb2: "
1095 "re-processing mid %llu\n",
1096 (unsigned long long)mid
));
1098 tevent_schedule_immediate(state
->im
,
1099 smb2req
->sconn
->ev_ctx
,
1100 smbd_smb2_create_request_dispatch_immediate
,
1104 /*********************************************************
1105 Re-process this call.
1106 *********************************************************/
1108 static void smb2_deferred_open_timer(struct event_context
*ev
,
1109 struct timed_event
*te
,
1110 struct timeval _tval
,
1114 struct smbd_smb2_create_state
*state
= NULL
;
1115 struct smbd_smb2_request
*smb2req
= talloc_get_type(private_data
,
1116 struct smbd_smb2_request
);
1118 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1119 smb2req
->current_idx
,
1120 tevent_req_default_print(smb2req
->subreq
, talloc_tos()) ));
1122 state
= tevent_req_data(smb2req
->subreq
,
1123 struct smbd_smb2_create_state
);
1128 * Null this out, don't talloc_free. It will
1129 * be talloc_free'd by the tevent library when
1133 /* Ensure we don't have any outstanding immediate event. */
1134 TALLOC_FREE(state
->im
);
1137 * This is subtle. We must null out the callback
1138 * before resheduling, else the first call to
1139 * tevent_req_nterror() causes the _receive()
1140 * function to be called, this causing tevent_req_post()
1143 tevent_req_set_callback(smb2req
->subreq
, NULL
, NULL
);
1145 status
= smbd_smb2_request_dispatch(smb2req
);
1147 if (!NT_STATUS_IS_OK(status
)) {
1148 smbd_server_connection_terminate(smb2req
->sconn
,
1153 static bool smbd_smb2_create_cancel(struct tevent_req
*req
)
1155 struct smbd_smb2_request
*smb2req
= NULL
;
1156 struct smbd_smb2_create_state
*state
= tevent_req_data(req
,
1157 struct smbd_smb2_create_state
);
1164 if (!state
->smb2req
) {
1168 smb2req
= state
->smb2req
;
1169 mid
= get_mid_from_smb2req(smb2req
);
1171 remove_deferred_open_entry(state
->id
, mid
,
1172 messaging_server_id(smb2req
->sconn
->msg_ctx
));
1173 remove_deferred_open_message_smb2_internal(smb2req
, mid
);
1174 smb2req
->cancelled
= true;
1176 tevent_req_done(req
);
1180 bool push_deferred_open_message_smb2(struct smbd_smb2_request
*smb2req
,
1181 struct timeval request_time
,
1182 struct timeval timeout
,
1187 struct tevent_req
*req
= NULL
;
1188 struct smbd_smb2_create_state
*state
= NULL
;
1189 struct timeval end_time
;
1194 req
= smb2req
->subreq
;
1198 state
= tevent_req_data(req
, struct smbd_smb2_create_state
);
1203 state
->request_time
= request_time
;
1204 state
->private_data
= data_blob_talloc(state
, private_data
,
1206 if (!state
->private_data
.data
) {
1210 /* Re-schedule us to retry on timer expiry. */
1211 end_time
= timeval_sum(&request_time
, &timeout
);
1213 DEBUG(10,("push_deferred_open_message_smb2: "
1215 timeval_string(talloc_tos(),
1219 state
->te
= tevent_add_timer(smb2req
->sconn
->ev_ctx
,
1222 smb2_deferred_open_timer
,
1228 /* allow this request to be canceled */
1229 tevent_req_set_cancel_fn(req
, smbd_smb2_create_cancel
);