s3:libsmb: Add in the core of the libsmb client SMB2 functions.
[Samba.git] / source3 / smbd / smb2_create.c
blob4f2edfca2ce15e3fa528178b75ec9d977efc36c4
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
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/>.
22 #include "includes.h"
23 #include "printing.h"
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"
29 #include "messages.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:
35 return NO_OPLOCK;
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:
41 return BATCH_OPLOCK;
42 case SMB2_OPLOCK_LEVEL_LEASE:
43 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
44 "LEASE_OPLOCK_REQUESTED\n"));
45 return NO_OPLOCK;
46 default:
47 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
48 "unknown level %u\n",
49 (unsigned int)in_oplock_level));
50 return NO_OPLOCK;
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;
67 } else {
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,
82 const char *in_name,
83 struct smb2_create_blobs in_context_blobs);
84 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
85 TALLOC_CTX *mem_ctx,
86 uint8_t *out_oplock_level,
87 uint32_t *out_create_action,
88 NTTIME *out_creation_time,
89 NTTIME *out_last_access_time,
90 NTTIME *out_last_write_time,
91 NTTIME *out_change_time,
92 uint64_t *out_allocation_size,
93 uint64_t *out_end_of_file,
94 uint32_t *out_file_attributes,
95 uint64_t *out_file_id_persistent,
96 uint64_t *out_file_id_volatile,
97 struct smb2_create_blobs *out_context_blobs);
99 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
100 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
102 const uint8_t *inbody;
103 const struct iovec *indyniov;
104 uint8_t in_oplock_level;
105 uint32_t in_impersonation_level;
106 uint32_t in_desired_access;
107 uint32_t in_file_attributes;
108 uint32_t in_share_access;
109 uint32_t in_create_disposition;
110 uint32_t in_create_options;
111 uint16_t in_name_offset;
112 uint16_t in_name_length;
113 DATA_BLOB in_name_buffer;
114 char *in_name_string;
115 size_t in_name_string_size;
116 uint32_t name_offset = 0;
117 uint32_t name_available_length = 0;
118 uint32_t in_context_offset;
119 uint32_t in_context_length;
120 DATA_BLOB in_context_buffer;
121 struct smb2_create_blobs in_context_blobs;
122 uint32_t context_offset = 0;
123 uint32_t context_available_length = 0;
124 uint32_t dyn_offset;
125 NTSTATUS status;
126 bool ok;
127 struct tevent_req *tsubreq;
129 status = smbd_smb2_request_verify_sizes(smb2req, 0x39);
130 if (!NT_STATUS_IS_OK(status)) {
131 return smbd_smb2_request_error(smb2req, status);
133 inbody = SMBD_SMB2_IN_BODY_PTR(smb2req);
135 in_oplock_level = CVAL(inbody, 0x03);
136 in_impersonation_level = IVAL(inbody, 0x04);
137 in_desired_access = IVAL(inbody, 0x18);
138 in_file_attributes = IVAL(inbody, 0x1C);
139 in_share_access = IVAL(inbody, 0x20);
140 in_create_disposition = IVAL(inbody, 0x24);
141 in_create_options = IVAL(inbody, 0x28);
142 in_name_offset = SVAL(inbody, 0x2C);
143 in_name_length = SVAL(inbody, 0x2E);
144 in_context_offset = IVAL(inbody, 0x30);
145 in_context_length = IVAL(inbody, 0x34);
148 * First check if the dynamic name and context buffers
149 * are correctly specified.
151 * Note: That we don't check if the name and context buffers
152 * overlap
155 dyn_offset = SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(smb2req);
157 if (in_name_offset == 0 && in_name_length == 0) {
158 /* This is ok */
159 name_offset = 0;
160 } else if (in_name_offset < dyn_offset) {
161 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
162 } else {
163 name_offset = in_name_offset - dyn_offset;
166 indyniov = SMBD_SMB2_IN_DYN_IOV(smb2req);
168 if (name_offset > indyniov->iov_len) {
169 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
172 name_available_length = indyniov->iov_len - name_offset;
174 if (in_name_length > name_available_length) {
175 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
178 in_name_buffer.data = (uint8_t *)indyniov->iov_base + name_offset;
179 in_name_buffer.length = in_name_length;
181 if (in_context_offset == 0 && in_context_length == 0) {
182 /* This is ok */
183 context_offset = 0;
184 } else if (in_context_offset < dyn_offset) {
185 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
186 } else {
187 context_offset = in_context_offset - dyn_offset;
190 if (context_offset > indyniov->iov_len) {
191 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
194 context_available_length = indyniov->iov_len - context_offset;
196 if (in_context_length > context_available_length) {
197 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
200 in_context_buffer.data = (uint8_t *)indyniov->iov_base +
201 context_offset;
202 in_context_buffer.length = in_context_length;
205 * Now interpret the name and context buffers
208 ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
209 in_name_buffer.data,
210 in_name_buffer.length,
211 &in_name_string,
212 &in_name_string_size);
213 if (!ok) {
214 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
217 if (in_name_buffer.length == 0) {
218 in_name_string_size = 0;
221 if (strlen(in_name_string) != in_name_string_size) {
222 return smbd_smb2_request_error(smb2req, NT_STATUS_OBJECT_NAME_INVALID);
225 ZERO_STRUCT(in_context_blobs);
226 status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
227 if (!NT_STATUS_IS_OK(status)) {
228 return smbd_smb2_request_error(smb2req, status);
231 tsubreq = smbd_smb2_create_send(smb2req,
232 smb2req->sconn->ev_ctx,
233 smb2req,
234 in_oplock_level,
235 in_impersonation_level,
236 in_desired_access,
237 in_file_attributes,
238 in_share_access,
239 in_create_disposition,
240 in_create_options,
241 in_name_string,
242 in_context_blobs);
243 if (tsubreq == NULL) {
244 smb2req->subreq = NULL;
245 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
247 tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
250 * For now we keep the logic that we do not send STATUS_PENDING
251 * for sharing violations, so we just wait 2 seconds.
253 * TODO: we need more tests for this.
255 return smbd_smb2_request_pending_queue(smb2req, tsubreq, 2000000);
258 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
260 uint8_t *reqhdr = SMBD_SMB2_OUT_HDR_PTR(smb2req);
261 return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
264 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
266 struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
267 struct smbd_smb2_request);
268 DATA_BLOB outbody;
269 DATA_BLOB outdyn;
270 uint8_t out_oplock_level = 0;
271 uint32_t out_create_action = 0;
272 NTTIME out_creation_time = 0;
273 NTTIME out_last_access_time = 0;
274 NTTIME out_last_write_time = 0;
275 NTTIME out_change_time = 0;
276 uint64_t out_allocation_size = 0;
277 uint64_t out_end_of_file = 0;
278 uint32_t out_file_attributes = 0;
279 uint64_t out_file_id_persistent = 0;
280 uint64_t out_file_id_volatile = 0;
281 struct smb2_create_blobs out_context_blobs;
282 DATA_BLOB out_context_buffer;
283 uint16_t out_context_buffer_offset = 0;
284 NTSTATUS status;
285 NTSTATUS error; /* transport error */
287 status = smbd_smb2_create_recv(tsubreq,
288 smb2req,
289 &out_oplock_level,
290 &out_create_action,
291 &out_creation_time,
292 &out_last_access_time,
293 &out_last_write_time,
294 &out_change_time,
295 &out_allocation_size,
296 &out_end_of_file,
297 &out_file_attributes,
298 &out_file_id_persistent,
299 &out_file_id_volatile,
300 &out_context_blobs);
301 if (!NT_STATUS_IS_OK(status)) {
302 error = smbd_smb2_request_error(smb2req, status);
303 if (!NT_STATUS_IS_OK(error)) {
304 smbd_server_connection_terminate(smb2req->sconn,
305 nt_errstr(error));
306 return;
308 return;
311 status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
312 if (!NT_STATUS_IS_OK(status)) {
313 error = smbd_smb2_request_error(smb2req, status);
314 if (!NT_STATUS_IS_OK(error)) {
315 smbd_server_connection_terminate(smb2req->sconn,
316 nt_errstr(error));
317 return;
319 return;
322 if (out_context_buffer.length > 0) {
323 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
326 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
327 if (outbody.data == NULL) {
328 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
329 if (!NT_STATUS_IS_OK(error)) {
330 smbd_server_connection_terminate(smb2req->sconn,
331 nt_errstr(error));
332 return;
334 return;
337 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
338 SCVAL(outbody.data, 0x02,
339 out_oplock_level); /* oplock level */
340 SCVAL(outbody.data, 0x03, 0); /* reserved */
341 SIVAL(outbody.data, 0x04,
342 out_create_action); /* create action */
343 SBVAL(outbody.data, 0x08,
344 out_creation_time); /* creation time */
345 SBVAL(outbody.data, 0x10,
346 out_last_access_time); /* last access time */
347 SBVAL(outbody.data, 0x18,
348 out_last_write_time); /* last write time */
349 SBVAL(outbody.data, 0x20,
350 out_change_time); /* change time */
351 SBVAL(outbody.data, 0x28,
352 out_allocation_size); /* allocation size */
353 SBVAL(outbody.data, 0x30,
354 out_end_of_file); /* end of file */
355 SIVAL(outbody.data, 0x38,
356 out_file_attributes); /* file attributes */
357 SIVAL(outbody.data, 0x3C, 0); /* reserved */
358 SBVAL(outbody.data, 0x40,
359 out_file_id_persistent); /* file id (persistent) */
360 SBVAL(outbody.data, 0x48,
361 out_file_id_volatile); /* file id (volatile) */
362 SIVAL(outbody.data, 0x50,
363 out_context_buffer_offset); /* create contexts offset */
364 SIVAL(outbody.data, 0x54,
365 out_context_buffer.length); /* create contexts length */
367 outdyn = out_context_buffer;
369 error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
370 if (!NT_STATUS_IS_OK(error)) {
371 smbd_server_connection_terminate(smb2req->sconn,
372 nt_errstr(error));
373 return;
377 struct smbd_smb2_create_state {
378 struct smbd_smb2_request *smb2req;
379 struct smb_request *smb1req;
380 bool open_was_deferred;
381 struct tevent_timer *te;
382 struct tevent_immediate *im;
383 struct timeval request_time;
384 struct file_id id;
385 DATA_BLOB private_data;
386 uint8_t out_oplock_level;
387 uint32_t out_create_action;
388 NTTIME out_creation_time;
389 NTTIME out_last_access_time;
390 NTTIME out_last_write_time;
391 NTTIME out_change_time;
392 uint64_t out_allocation_size;
393 uint64_t out_end_of_file;
394 uint32_t out_file_attributes;
395 uint64_t out_file_id_persistent;
396 uint64_t out_file_id_volatile;
397 struct smb2_create_blobs out_context_blobs;
400 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
401 struct tevent_context *ev,
402 struct smbd_smb2_request *smb2req,
403 uint8_t in_oplock_level,
404 uint32_t in_impersonation_level,
405 uint32_t in_desired_access,
406 uint32_t in_file_attributes,
407 uint32_t in_share_access,
408 uint32_t in_create_disposition,
409 uint32_t in_create_options,
410 const char *in_name,
411 struct smb2_create_blobs in_context_blobs)
413 struct tevent_req *req = NULL;
414 struct smbd_smb2_create_state *state = NULL;
415 NTSTATUS status;
416 struct smb_request *smb1req = NULL;
417 files_struct *result = NULL;
418 int info;
419 struct timespec write_time_ts;
420 struct smb2_create_blobs out_context_blobs;
421 int requested_oplock_level;
422 struct smb2_create_blob *dhnc = NULL;
423 struct smb2_create_blob *dh2c = NULL;
424 struct smbXsrv_open *op = NULL;
426 ZERO_STRUCT(out_context_blobs);
428 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
429 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
430 } else {
431 requested_oplock_level = in_oplock_level;
435 if (smb2req->subreq == NULL) {
436 /* New create call. */
437 req = tevent_req_create(mem_ctx, &state,
438 struct smbd_smb2_create_state);
439 if (req == NULL) {
440 return NULL;
442 state->smb2req = smb2req;
444 smb1req = smbd_smb2_fake_smb_request(smb2req);
445 if (tevent_req_nomem(smb1req, req)) {
446 return tevent_req_post(req, ev);
448 state->smb1req = smb1req;
449 smb2req->subreq = req;
450 DEBUG(10,("smbd_smb2_create: name[%s]\n",
451 in_name));
452 } else {
453 /* Re-entrant create call. */
454 req = smb2req->subreq;
455 state = tevent_req_data(req,
456 struct smbd_smb2_create_state);
457 smb1req = state->smb1req;
458 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
459 in_name ));
462 dhnc = smb2_create_blob_find(&in_context_blobs,
463 SMB2_CREATE_TAG_DHNC);
465 if (dhnc) {
466 if (dhnc->data.length != 16) {
467 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
468 return tevent_req_post(req, ev);
470 if (in_context_blobs.num_blobs != 1) {
472 * DHNC should be the only one.
474 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
475 return tevent_req_post(req, ev);
479 dh2c = smb2_create_blob_find(&in_context_blobs,
480 SMB2_CREATE_TAG_DH2C);
481 if (dh2c) {
482 if (dh2c->data.length != 36) {
483 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
484 return tevent_req_post(req, ev);
486 if (in_context_blobs.num_blobs != 1) {
488 * DH2C should be the only one.
490 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
491 return tevent_req_post(req, ev);
495 if (IS_IPC(smb1req->conn)) {
496 const char *pipe_name = in_name;
498 if (dhnc || dh2c) {
499 /* durable handles are not supported on IPC$ */
500 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
501 return tevent_req_post(req, ev);
504 if (!lp_nt_pipe_support()) {
505 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
506 return tevent_req_post(req, ev);
509 status = open_np_file(smb1req, pipe_name, &result);
510 if (!NT_STATUS_IS_OK(status)) {
511 tevent_req_nterror(req, status);
512 return tevent_req_post(req, ev);
514 info = FILE_WAS_OPENED;
515 } else if (CAN_PRINT(smb1req->conn)) {
516 if (dhnc || dh2c) {
517 /* durable handles are not supported on printers */
518 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
519 return tevent_req_post(req, ev);
522 status = file_new(smb1req, smb1req->conn, &result);
523 if(!NT_STATUS_IS_OK(status)) {
524 tevent_req_nterror(req, status);
525 return tevent_req_post(req, ev);
528 status = print_spool_open(result, in_name,
529 smb1req->vuid);
530 if (!NT_STATUS_IS_OK(status)) {
531 file_free(smb1req, result);
532 tevent_req_nterror(req, status);
533 return tevent_req_post(req, ev);
535 info = FILE_WAS_CREATED;
536 } else {
537 char *fname;
538 struct smb2_create_blob *exta = NULL;
539 struct ea_list *ea_list = NULL;
540 struct smb2_create_blob *mxac = NULL;
541 NTTIME max_access_time = 0;
542 struct smb2_create_blob *secd = NULL;
543 struct security_descriptor *sec_desc = NULL;
544 struct smb2_create_blob *dhnq = NULL;
545 struct smb2_create_blob *alsi = NULL;
546 uint64_t allocation_size = 0;
547 struct smb2_create_blob *twrp = NULL;
548 struct smb2_create_blob *qfid = NULL;
549 struct GUID create_guid = GUID_zero();
550 bool update_open = false;
551 bool durable_requested = false;
552 uint32_t durable_timeout_msec = 0;
553 bool do_durable_reconnect = false;
554 struct smb2_create_blob *dh2q = NULL;
555 uint64_t persistent_id = 0;
557 exta = smb2_create_blob_find(&in_context_blobs,
558 SMB2_CREATE_TAG_EXTA);
559 mxac = smb2_create_blob_find(&in_context_blobs,
560 SMB2_CREATE_TAG_MXAC);
561 secd = smb2_create_blob_find(&in_context_blobs,
562 SMB2_CREATE_TAG_SECD);
563 dhnq = smb2_create_blob_find(&in_context_blobs,
564 SMB2_CREATE_TAG_DHNQ);
565 alsi = smb2_create_blob_find(&in_context_blobs,
566 SMB2_CREATE_TAG_ALSI);
567 twrp = smb2_create_blob_find(&in_context_blobs,
568 SMB2_CREATE_TAG_TWRP);
569 qfid = smb2_create_blob_find(&in_context_blobs,
570 SMB2_CREATE_TAG_QFID);
571 dh2q = smb2_create_blob_find(&in_context_blobs,
572 SMB2_CREATE_TAG_DH2Q);
574 fname = talloc_strdup(state, in_name);
575 if (tevent_req_nomem(fname, req)) {
576 return tevent_req_post(req, ev);
579 if (exta) {
580 ea_list = read_nttrans_ea_list(mem_ctx,
581 (const char *)exta->data.data, exta->data.length);
582 if (!ea_list) {
583 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
584 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
585 return tevent_req_post(req, ev);
588 if (ea_list_has_invalid_name(ea_list)) {
589 tevent_req_nterror(req, STATUS_INVALID_EA_NAME);
590 return tevent_req_post(req, ev);
594 if (mxac) {
595 if (mxac->data.length == 0) {
596 max_access_time = 0;
597 } else if (mxac->data.length == 8) {
598 max_access_time = BVAL(mxac->data.data, 0);
599 } else {
600 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
601 return tevent_req_post(req, ev);
605 if (secd) {
606 enum ndr_err_code ndr_err;
608 sec_desc = talloc_zero(state, struct security_descriptor);
609 if (tevent_req_nomem(sec_desc, req)) {
610 return tevent_req_post(req, ev);
613 ndr_err = ndr_pull_struct_blob(&secd->data,
614 sec_desc, sec_desc,
615 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
616 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
617 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
618 ndr_errstr(ndr_err)));
619 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
620 return tevent_req_post(req, ev);
624 if (dhnq) {
625 if (dhnq->data.length != 16) {
626 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
627 return tevent_req_post(req, ev);
630 if (dh2q) {
631 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
632 return tevent_req_post(req, ev);
636 * durable handle request is processed below.
638 durable_requested = true;
640 * Set the timeout to 16 mins.
642 * TODO: test this against Windows 2012
643 * as the default for durable v2 is 1 min.
645 durable_timeout_msec = (16*60*1000);
648 if (dh2q) {
649 const uint8_t *p = dh2q->data.data;
650 uint32_t durable_v2_timeout = 0;
651 DATA_BLOB create_guid_blob;
653 if (dh2q->data.length != 32) {
654 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
655 return tevent_req_post(req, ev);
658 if (dhnq) {
659 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
660 return tevent_req_post(req, ev);
663 durable_v2_timeout = IVAL(p, 0);
664 create_guid_blob = data_blob_const(p + 16, 16);
666 status = GUID_from_ndr_blob(&create_guid_blob,
667 &create_guid);
668 if (tevent_req_nterror(req, status)) {
669 return tevent_req_post(req, ev);
672 * we need to store the create_guid later
674 update_open = true;
677 * durable handle v2 request processed below
679 durable_requested = true;
680 durable_timeout_msec = durable_v2_timeout;
681 if (durable_timeout_msec == 0) {
683 * Set the timeout to 1 min as default.
685 * This matches Windows 2012.
687 durable_timeout_msec = (60*1000);
691 if (dhnc) {
692 persistent_id = BVAL(dhnc->data.data, 0);
694 do_durable_reconnect = true;
697 if (dh2c) {
698 const uint8_t *p = dh2c->data.data;
699 DATA_BLOB create_guid_blob;
701 persistent_id = BVAL(p, 0);
702 create_guid_blob = data_blob_const(p + 16, 16);
704 status = GUID_from_ndr_blob(&create_guid_blob,
705 &create_guid);
706 if (tevent_req_nterror(req, status)) {
707 return tevent_req_post(req, ev);
710 do_durable_reconnect = true;
713 if (alsi) {
714 if (alsi->data.length != 8) {
715 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
716 return tevent_req_post(req, ev);
718 allocation_size = BVAL(alsi->data.data, 0);
721 if (twrp) {
722 NTTIME nttime;
723 time_t t;
724 struct tm *tm;
726 if (twrp->data.length != 8) {
727 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
728 return tevent_req_post(req, ev);
731 nttime = BVAL(twrp->data.data, 0);
732 t = nt_time_to_unix(nttime);
733 tm = gmtime(&t);
735 TALLOC_FREE(fname);
736 fname = talloc_asprintf(state,
737 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
738 tm->tm_year + 1900,
739 tm->tm_mon + 1,
740 tm->tm_mday,
741 tm->tm_hour,
742 tm->tm_min,
743 tm->tm_sec,
744 in_name);
745 if (tevent_req_nomem(fname, req)) {
746 return tevent_req_post(req, ev);
750 if (qfid) {
751 if (qfid->data.length != 0) {
752 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
753 return tevent_req_post(req, ev);
757 /* these are ignored for SMB2 */
758 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
759 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
761 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
763 DEBUG(10, ("smbd_smb2_create_send: open execution phase\n"));
766 * For the backend file open procedure, there are
767 * two possible modes: durable_reconnect or not.
769 if (do_durable_reconnect) {
770 DATA_BLOB new_cookie = data_blob_null;
771 NTTIME now = timeval_to_nttime(&smb2req->request_time);
773 status = smb2srv_open_recreate(smb2req->sconn->conn,
774 smb1req->conn->session_info,
775 persistent_id, create_guid,
776 now, &op);
777 if (!NT_STATUS_IS_OK(status)) {
778 DEBUG(3, ("smbd_smb2_create_send: "
779 "smb2srv_open_recreate failed: %s\n",
780 nt_errstr(status)));
781 tevent_req_nterror(req, status);
782 return tevent_req_post(req, ev);
785 DEBUG(10, ("smb2_create_send: %s to recreate the "
786 "smb2srv_open struct for a durable handle.\n",
787 op->global->durable ? "succeded" : "failed"));
789 if (!op->global->durable) {
790 talloc_free(op);
791 tevent_req_nterror(req,
792 NT_STATUS_OBJECT_NAME_NOT_FOUND);
793 return tevent_req_post(req, ev);
796 status = SMB_VFS_DURABLE_RECONNECT(smb1req->conn,
797 smb1req,
798 op, /* smbXsrv_open input */
799 op->global->backend_cookie,
800 op, /* TALLOC_CTX */
801 &result, &new_cookie);
802 if (!NT_STATUS_IS_OK(status)) {
803 NTSTATUS return_status;
805 return_status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
807 DEBUG(3, ("smbd_smb2_create_send: "
808 "durable_reconnect failed: %s => %s\n",
809 nt_errstr(status),
810 nt_errstr(return_status)));
812 tevent_req_nterror(req, return_status);
813 return tevent_req_post(req, ev);
816 data_blob_free(&op->global->backend_cookie);
817 op->global->backend_cookie = new_cookie;
819 op->status = NT_STATUS_OK;
820 op->global->disconnect_time = 0;
822 /* save the timout for later update */
823 durable_timeout_msec = op->global->durable_timeout_msec;
825 update_open = true;
827 info = FILE_WAS_OPENED;
828 } else {
829 struct smb_filename *smb_fname = NULL;
832 * For a DFS path the function parse_dfs_path()
833 * will do the path processing.
836 if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
837 /* convert '\\' into '/' */
838 status = check_path_syntax(fname);
839 if (!NT_STATUS_IS_OK(status)) {
840 tevent_req_nterror(req, status);
841 return tevent_req_post(req, ev);
845 status = filename_convert(req,
846 smb1req->conn,
847 smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
848 fname,
849 (in_create_disposition == FILE_CREATE) ?
850 UCF_CREATING_FILE : 0,
851 NULL, /* ppath_contains_wcards */
852 &smb_fname);
853 if (!NT_STATUS_IS_OK(status)) {
854 tevent_req_nterror(req, status);
855 return tevent_req_post(req, ev);
858 status = SMB_VFS_CREATE_FILE(smb1req->conn,
859 smb1req,
860 0, /* root_dir_fid */
861 smb_fname,
862 in_desired_access,
863 in_share_access,
864 in_create_disposition,
865 in_create_options,
866 in_file_attributes,
867 map_smb2_oplock_levels_to_samba(requested_oplock_level),
868 allocation_size,
869 0, /* private_flags */
870 sec_desc,
871 ea_list,
872 &result,
873 &info);
874 if (!NT_STATUS_IS_OK(status)) {
875 if (open_was_deferred(smb1req->sconn, smb1req->mid)) {
876 return req;
878 tevent_req_nterror(req, status);
879 return tevent_req_post(req, ev);
881 op = result->op;
885 * here we have op == result->op
888 DEBUG(10, ("smbd_smb2_create_send: "
889 "response construction phase\n"));
891 if (mxac) {
892 NTTIME last_write_time;
894 unix_timespec_to_nt_time(&last_write_time,
895 result->fsp_name->st.st_ex_mtime);
896 if (last_write_time != max_access_time) {
897 uint8_t p[8];
898 uint32_t max_access_granted;
899 DATA_BLOB blob = data_blob_const(p, sizeof(p));
901 status = smbd_calculate_access_mask(smb1req->conn,
902 result->fsp_name,
903 false,
904 SEC_FLAG_MAXIMUM_ALLOWED,
905 &max_access_granted);
907 SIVAL(p, 0, NT_STATUS_V(status));
908 SIVAL(p, 4, max_access_granted);
910 status = smb2_create_blob_add(state,
911 &out_context_blobs,
912 SMB2_CREATE_TAG_MXAC,
913 blob);
914 if (!NT_STATUS_IS_OK(status)) {
915 tevent_req_nterror(req, status);
916 return tevent_req_post(req, ev);
921 if (durable_requested &&
922 BATCH_OPLOCK_TYPE(result->oplock_type))
924 status = SMB_VFS_DURABLE_COOKIE(result,
926 &op->global->backend_cookie);
927 if (!NT_STATUS_IS_OK(status)) {
928 op->global->backend_cookie = data_blob_null;
931 if (op->global->backend_cookie.length > 0) {
932 update_open = true;
934 op->global->durable = true;
935 op->global->durable_timeout_msec = durable_timeout_msec;
938 if (update_open) {
939 op->global->create_guid = create_guid;
941 status = smbXsrv_open_update(op);
942 DEBUG(10, ("smb2_create_send: smbXsrv_open_update "
943 "returned %s\n",
944 nt_errstr(status)));
945 if (!NT_STATUS_IS_OK(status)) {
946 tevent_req_nterror(req, status);
947 return tevent_req_post(req, ev);
951 if (dhnq && op->global->durable) {
952 uint8_t p[8] = { 0, };
953 DATA_BLOB blob = data_blob_const(p, sizeof(p));
955 status = smb2_create_blob_add(state,
956 &out_context_blobs,
957 SMB2_CREATE_TAG_DHNQ,
958 blob);
959 if (!NT_STATUS_IS_OK(status)) {
960 tevent_req_nterror(req, status);
961 return tevent_req_post(req, ev);
965 if (dh2q && op->global->durable) {
966 uint8_t p[8] = { 0, };
967 DATA_BLOB blob = data_blob_const(p, sizeof(p));
968 uint32_t durable_v2_response_flags = 0;
970 SIVAL(p, 0, op->global->durable_timeout_msec);
971 SIVAL(p, 4, durable_v2_response_flags);
973 status = smb2_create_blob_add(state, &out_context_blobs,
974 SMB2_CREATE_TAG_DH2Q,
975 blob);
976 if (!NT_STATUS_IS_OK(status)) {
977 tevent_req_nterror(req, status);
978 return tevent_req_post(req, ev);
982 if (qfid) {
983 uint8_t p[32];
984 uint64_t file_index = get_FileIndex(result->conn,
985 &result->fsp_name->st);
986 DATA_BLOB blob = data_blob_const(p, sizeof(p));
988 ZERO_STRUCT(p);
990 /* From conversations with Microsoft engineers at
991 the MS plugfest. The first 8 bytes are the "volume index"
992 == inode, the second 8 bytes are the "volume id",
993 == dev. This will be updated in the SMB2 doc. */
994 SBVAL(p, 0, file_index);
995 SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
997 status = smb2_create_blob_add(state, &out_context_blobs,
998 SMB2_CREATE_TAG_QFID,
999 blob);
1000 if (!NT_STATUS_IS_OK(status)) {
1001 tevent_req_nterror(req, status);
1002 return tevent_req_post(req, ev);
1007 smb2req->compat_chain_fsp = smb1req->chain_fsp;
1009 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
1010 state->out_oplock_level = in_oplock_level;
1011 } else {
1012 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
1015 if ((in_create_disposition == FILE_SUPERSEDE)
1016 && (info == FILE_WAS_OVERWRITTEN)) {
1017 state->out_create_action = FILE_WAS_SUPERSEDED;
1018 } else {
1019 state->out_create_action = info;
1021 state->out_file_attributes = dos_mode(result->conn,
1022 result->fsp_name);
1023 /* Deal with other possible opens having a modified
1024 write time. JRA. */
1025 ZERO_STRUCT(write_time_ts);
1026 get_file_infos(result->file_id, 0, NULL, &write_time_ts);
1027 if (!null_timespec(write_time_ts)) {
1028 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
1031 unix_timespec_to_nt_time(&state->out_creation_time,
1032 get_create_timespec(smb1req->conn, result,
1033 result->fsp_name));
1034 unix_timespec_to_nt_time(&state->out_last_access_time,
1035 result->fsp_name->st.st_ex_atime);
1036 unix_timespec_to_nt_time(&state->out_last_write_time,
1037 result->fsp_name->st.st_ex_mtime);
1038 unix_timespec_to_nt_time(&state->out_change_time,
1039 get_change_timespec(smb1req->conn, result,
1040 result->fsp_name));
1041 state->out_allocation_size =
1042 SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
1043 &(result->fsp_name->st));
1044 state->out_end_of_file = result->fsp_name->st.st_ex_size;
1045 if (state->out_file_attributes == 0) {
1046 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
1048 state->out_file_id_persistent = result->op->global->open_persistent_id;
1049 state->out_file_id_volatile = result->op->global->open_volatile_id;
1050 state->out_context_blobs = out_context_blobs;
1052 DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
1053 fsp_str_dbg(result), fsp_fnum_dbg(result)));
1055 tevent_req_done(req);
1056 return tevent_req_post(req, ev);
1059 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
1060 TALLOC_CTX *mem_ctx,
1061 uint8_t *out_oplock_level,
1062 uint32_t *out_create_action,
1063 NTTIME *out_creation_time,
1064 NTTIME *out_last_access_time,
1065 NTTIME *out_last_write_time,
1066 NTTIME *out_change_time,
1067 uint64_t *out_allocation_size,
1068 uint64_t *out_end_of_file,
1069 uint32_t *out_file_attributes,
1070 uint64_t *out_file_id_persistent,
1071 uint64_t *out_file_id_volatile,
1072 struct smb2_create_blobs *out_context_blobs)
1074 NTSTATUS status;
1075 struct smbd_smb2_create_state *state = tevent_req_data(req,
1076 struct smbd_smb2_create_state);
1078 if (tevent_req_is_nterror(req, &status)) {
1079 tevent_req_received(req);
1080 return status;
1083 *out_oplock_level = state->out_oplock_level;
1084 *out_create_action = state->out_create_action;
1085 *out_creation_time = state->out_creation_time;
1086 *out_last_access_time = state->out_last_access_time;
1087 *out_last_write_time = state->out_last_write_time;
1088 *out_change_time = state->out_change_time;
1089 *out_allocation_size = state->out_allocation_size;
1090 *out_end_of_file = state->out_end_of_file;
1091 *out_file_attributes = state->out_file_attributes;
1092 *out_file_id_persistent = state->out_file_id_persistent;
1093 *out_file_id_volatile = state->out_file_id_volatile;
1094 *out_context_blobs = state->out_context_blobs;
1096 talloc_steal(mem_ctx, state->out_context_blobs.blobs);
1098 tevent_req_received(req);
1099 return NT_STATUS_OK;
1102 /*********************************************************
1103 Code for dealing with deferred opens.
1104 *********************************************************/
1106 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
1107 struct timeval *p_request_time,
1108 void **pp_state)
1110 struct smbd_smb2_create_state *state = NULL;
1111 struct tevent_req *req = NULL;
1113 if (!smb2req) {
1114 return false;
1116 req = smb2req->subreq;
1117 if (!req) {
1118 return false;
1120 state = tevent_req_data(req, struct smbd_smb2_create_state);
1121 if (!state) {
1122 return false;
1124 if (!state->open_was_deferred) {
1125 return false;
1127 if (p_request_time) {
1128 *p_request_time = state->request_time;
1130 if (pp_state) {
1131 *pp_state = (void *)state->private_data.data;
1133 return true;
1136 /*********************************************************
1137 Re-process this call early - requested by message or
1138 close.
1139 *********************************************************/
1141 static struct smbd_smb2_request *find_open_smb2req(
1142 struct smbd_server_connection *sconn, uint64_t mid)
1144 struct smbd_smb2_request *smb2req;
1146 for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
1147 uint64_t message_id;
1148 if (smb2req->subreq == NULL) {
1149 /* This message has been processed. */
1150 continue;
1152 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1153 /* This message has been processed. */
1154 continue;
1156 message_id = get_mid_from_smb2req(smb2req);
1157 if (message_id == mid) {
1158 return smb2req;
1161 return NULL;
1164 bool open_was_deferred_smb2(struct smbd_server_connection *sconn, uint64_t mid)
1166 struct smbd_smb2_create_state *state = NULL;
1167 struct smbd_smb2_request *smb2req;
1169 smb2req = find_open_smb2req(sconn, mid);
1171 if (!smb2req) {
1172 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
1173 (unsigned long long)mid));
1174 return false;
1176 if (!smb2req->subreq) {
1177 return false;
1179 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1180 return false;
1182 state = tevent_req_data(smb2req->subreq,
1183 struct smbd_smb2_create_state);
1184 if (!state) {
1185 return false;
1187 /* It's not in progress if there's no timeout event. */
1188 if (!state->open_was_deferred) {
1189 return false;
1192 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
1193 (unsigned long long)mid));
1195 return true;
1198 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
1199 uint64_t mid)
1201 struct smbd_smb2_create_state *state = NULL;
1203 if (!smb2req->subreq) {
1204 return;
1206 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1207 return;
1209 state = tevent_req_data(smb2req->subreq,
1210 struct smbd_smb2_create_state);
1211 if (!state) {
1212 return;
1215 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
1216 "mid %llu\n",
1217 (unsigned long long)mid ));
1219 state->open_was_deferred = false;
1220 /* Ensure we don't have any outstanding timer event. */
1221 TALLOC_FREE(state->te);
1222 /* Ensure we don't have any outstanding immediate event. */
1223 TALLOC_FREE(state->im);
1226 void remove_deferred_open_message_smb2(
1227 struct smbd_server_connection *sconn, uint64_t mid)
1229 struct smbd_smb2_request *smb2req;
1231 smb2req = find_open_smb2req(sconn, mid);
1233 if (!smb2req) {
1234 DEBUG(10,("remove_deferred_open_message_smb2: "
1235 "can't find mid %llu\n",
1236 (unsigned long long)mid ));
1237 return;
1239 remove_deferred_open_message_smb2_internal(smb2req, mid);
1242 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1243 struct tevent_immediate *im,
1244 void *private_data)
1246 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1247 struct smbd_smb2_request);
1248 struct smbd_server_connection *sconn = smb2req->sconn;
1249 uint64_t mid = get_mid_from_smb2req(smb2req);
1250 NTSTATUS status;
1252 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1253 "re-dispatching mid %llu\n",
1254 (unsigned long long)mid ));
1256 status = smbd_smb2_request_dispatch(smb2req);
1257 if (!NT_STATUS_IS_OK(status)) {
1258 smbd_server_connection_terminate(sconn, nt_errstr(status));
1259 return;
1263 bool schedule_deferred_open_message_smb2(
1264 struct smbd_server_connection *sconn, uint64_t mid)
1266 struct smbd_smb2_create_state *state = NULL;
1267 struct smbd_smb2_request *smb2req;
1269 smb2req = find_open_smb2req(sconn, mid);
1271 if (!smb2req) {
1272 DEBUG(10,("schedule_deferred_open_message_smb2: "
1273 "can't find mid %llu\n",
1274 (unsigned long long)mid ));
1275 return false;
1277 if (!smb2req->subreq) {
1278 return false;
1280 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1281 return false;
1283 state = tevent_req_data(smb2req->subreq,
1284 struct smbd_smb2_create_state);
1285 if (!state) {
1286 return false;
1289 /* Ensure we don't have any outstanding timer event. */
1290 TALLOC_FREE(state->te);
1291 /* Ensure we don't have any outstanding immediate event. */
1292 TALLOC_FREE(state->im);
1295 * This is subtle. We must null out the callback
1296 * before rescheduling, else the first call to
1297 * tevent_req_nterror() causes the _receive()
1298 * function to be called, this causing tevent_req_post()
1299 * to crash.
1301 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1303 state->im = tevent_create_immediate(smb2req);
1304 if (!state->im) {
1305 smbd_server_connection_terminate(smb2req->sconn,
1306 nt_errstr(NT_STATUS_NO_MEMORY));
1307 return false;
1310 DEBUG(10,("schedule_deferred_open_message_smb2: "
1311 "re-processing mid %llu\n",
1312 (unsigned long long)mid ));
1314 tevent_schedule_immediate(state->im,
1315 smb2req->sconn->ev_ctx,
1316 smbd_smb2_create_request_dispatch_immediate,
1317 smb2req);
1319 return true;
1322 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1324 struct smbd_smb2_request *smb2req = NULL;
1325 struct smbd_smb2_create_state *state = tevent_req_data(req,
1326 struct smbd_smb2_create_state);
1327 uint64_t mid;
1329 if (!state) {
1330 return false;
1333 if (!state->smb2req) {
1334 return false;
1337 smb2req = state->smb2req;
1338 mid = get_mid_from_smb2req(smb2req);
1340 if (is_deferred_open_async(state->private_data.data)) {
1341 /* Can't cancel an async create. */
1342 return false;
1345 remove_deferred_open_message_smb2_internal(smb2req, mid);
1347 tevent_req_defer_callback(req, smb2req->sconn->ev_ctx);
1348 tevent_req_nterror(req, NT_STATUS_CANCELLED);
1349 return true;
1352 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1353 struct timeval request_time,
1354 struct timeval timeout,
1355 struct file_id id,
1356 char *private_data,
1357 size_t priv_len)
1359 struct tevent_req *req = NULL;
1360 struct smbd_smb2_create_state *state = NULL;
1361 struct timeval end_time;
1363 if (!smb2req) {
1364 return false;
1366 req = smb2req->subreq;
1367 if (!req) {
1368 return false;
1370 state = tevent_req_data(req, struct smbd_smb2_create_state);
1371 if (!state) {
1372 return false;
1374 state->id = id;
1375 state->request_time = request_time;
1376 state->private_data = data_blob_talloc(state, private_data,
1377 priv_len);
1378 if (!state->private_data.data) {
1379 return false;
1382 /* Re-schedule us to retry on timer expiry. */
1383 end_time = timeval_sum(&request_time, &timeout);
1385 DEBUG(10,("push_deferred_open_message_smb2: "
1386 "timeout at %s\n",
1387 timeval_string(talloc_tos(),
1388 &end_time,
1389 true) ));
1391 state->open_was_deferred = true;
1393 /* allow this request to be canceled */
1394 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1396 return true;