Don't allow asynchronous creates to be canceled in SMB2.
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_create.c
blob7b5a26269b4056d3b95af7eda7403e1c91fd519c
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 int i = smb2req->current_idx;
105 uint8_t in_oplock_level;
106 uint32_t in_impersonation_level;
107 uint32_t in_desired_access;
108 uint32_t in_file_attributes;
109 uint32_t in_share_access;
110 uint32_t in_create_disposition;
111 uint32_t in_create_options;
112 uint16_t in_name_offset;
113 uint16_t in_name_length;
114 DATA_BLOB in_name_buffer;
115 char *in_name_string;
116 size_t in_name_string_size;
117 uint32_t name_offset = 0;
118 uint32_t name_available_length = 0;
119 uint32_t in_context_offset;
120 uint32_t in_context_length;
121 DATA_BLOB in_context_buffer;
122 struct smb2_create_blobs in_context_blobs;
123 uint32_t context_offset = 0;
124 uint32_t context_available_length = 0;
125 uint32_t dyn_offset;
126 NTSTATUS status;
127 bool ok;
128 struct tevent_req *tsubreq;
130 status = smbd_smb2_request_verify_sizes(smb2req, 0x39);
131 if (!NT_STATUS_IS_OK(status)) {
132 return smbd_smb2_request_error(smb2req, status);
134 inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
136 in_oplock_level = CVAL(inbody, 0x03);
137 in_impersonation_level = IVAL(inbody, 0x04);
138 in_desired_access = IVAL(inbody, 0x18);
139 in_file_attributes = IVAL(inbody, 0x1C);
140 in_share_access = IVAL(inbody, 0x20);
141 in_create_disposition = IVAL(inbody, 0x24);
142 in_create_options = IVAL(inbody, 0x28);
143 in_name_offset = SVAL(inbody, 0x2C);
144 in_name_length = SVAL(inbody, 0x2E);
145 in_context_offset = IVAL(inbody, 0x30);
146 in_context_length = IVAL(inbody, 0x34);
149 * First check if the dynamic name and context buffers
150 * are correctly specified.
152 * Note: That we don't check if the name and context buffers
153 * overlap
156 dyn_offset = SMB2_HDR_BODY + smb2req->in.vector[i+1].iov_len;
158 if (in_name_offset == 0 && in_name_length == 0) {
159 /* This is ok */
160 name_offset = 0;
161 } else if (in_name_offset < dyn_offset) {
162 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
163 } else {
164 name_offset = in_name_offset - dyn_offset;
167 indyniov = &smb2req->in.vector[i+2];
169 if (name_offset > indyniov->iov_len) {
170 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
173 name_available_length = indyniov->iov_len - name_offset;
175 if (in_name_length > name_available_length) {
176 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
179 in_name_buffer.data = (uint8_t *)indyniov->iov_base + name_offset;
180 in_name_buffer.length = in_name_length;
182 if (in_context_offset == 0 && in_context_length == 0) {
183 /* This is ok */
184 context_offset = 0;
185 } else if (in_context_offset < dyn_offset) {
186 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
187 } else {
188 context_offset = in_context_offset - dyn_offset;
191 if (context_offset > indyniov->iov_len) {
192 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
195 context_available_length = indyniov->iov_len - context_offset;
197 if (in_context_length > context_available_length) {
198 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
201 in_context_buffer.data = (uint8_t *)indyniov->iov_base +
202 context_offset;
203 in_context_buffer.length = in_context_length;
206 * Now interpret the name and context buffers
209 ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
210 in_name_buffer.data,
211 in_name_buffer.length,
212 &in_name_string,
213 &in_name_string_size);
214 if (!ok) {
215 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
218 if (in_name_buffer.length == 0) {
219 in_name_string_size = 0;
222 if (strlen(in_name_string) != in_name_string_size) {
223 return smbd_smb2_request_error(smb2req, NT_STATUS_OBJECT_NAME_INVALID);
226 ZERO_STRUCT(in_context_blobs);
227 status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
228 if (!NT_STATUS_IS_OK(status)) {
229 return smbd_smb2_request_error(smb2req, status);
232 tsubreq = smbd_smb2_create_send(smb2req,
233 smb2req->sconn->ev_ctx,
234 smb2req,
235 in_oplock_level,
236 in_impersonation_level,
237 in_desired_access,
238 in_file_attributes,
239 in_share_access,
240 in_create_disposition,
241 in_create_options,
242 in_name_string,
243 in_context_blobs);
244 if (tsubreq == NULL) {
245 smb2req->subreq = NULL;
246 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
248 tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
251 * For now we keep the logic that we do not send STATUS_PENDING
252 * for sharing violations, so we just wait 2 seconds.
254 * TODO: we need more tests for this.
256 return smbd_smb2_request_pending_queue(smb2req, tsubreq, 2000000);
259 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
261 uint8_t *reqhdr = (uint8_t *)smb2req->out.vector[smb2req->current_idx].iov_base;
262 return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
265 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
267 struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
268 struct smbd_smb2_request);
269 DATA_BLOB outbody;
270 DATA_BLOB outdyn;
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;
285 NTSTATUS status;
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,
295 nt_errstr(error));
296 return;
298 return;
301 status = smbd_smb2_create_recv(tsubreq,
302 smb2req,
303 &out_oplock_level,
304 &out_create_action,
305 &out_creation_time,
306 &out_last_access_time,
307 &out_last_write_time,
308 &out_change_time,
309 &out_allocation_size,
310 &out_end_of_file,
311 &out_file_attributes,
312 &out_file_id_persistent,
313 &out_file_id_volatile,
314 &out_context_blobs);
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,
319 nt_errstr(error));
320 return;
322 return;
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,
330 nt_errstr(error));
331 return;
333 return;
336 if (out_context_buffer.length > 0) {
337 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
340 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
341 if (outbody.data == NULL) {
342 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
343 if (!NT_STATUS_IS_OK(error)) {
344 smbd_server_connection_terminate(smb2req->sconn,
345 nt_errstr(error));
346 return;
348 return;
351 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
352 SCVAL(outbody.data, 0x02,
353 out_oplock_level); /* oplock level */
354 SCVAL(outbody.data, 0x03, 0); /* reserved */
355 SIVAL(outbody.data, 0x04,
356 out_create_action); /* create action */
357 SBVAL(outbody.data, 0x08,
358 out_creation_time); /* creation time */
359 SBVAL(outbody.data, 0x10,
360 out_last_access_time); /* last access time */
361 SBVAL(outbody.data, 0x18,
362 out_last_write_time); /* last write time */
363 SBVAL(outbody.data, 0x20,
364 out_change_time); /* change time */
365 SBVAL(outbody.data, 0x28,
366 out_allocation_size); /* allocation size */
367 SBVAL(outbody.data, 0x30,
368 out_end_of_file); /* end of file */
369 SIVAL(outbody.data, 0x38,
370 out_file_attributes); /* file attributes */
371 SIVAL(outbody.data, 0x3C, 0); /* reserved */
372 SBVAL(outbody.data, 0x40,
373 out_file_id_persistent); /* file id (persistent) */
374 SBVAL(outbody.data, 0x48,
375 out_file_id_volatile); /* file id (volatile) */
376 SIVAL(outbody.data, 0x50,
377 out_context_buffer_offset); /* create contexts offset */
378 SIVAL(outbody.data, 0x54,
379 out_context_buffer.length); /* create contexts length */
381 outdyn = out_context_buffer;
383 error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
384 if (!NT_STATUS_IS_OK(error)) {
385 smbd_server_connection_terminate(smb2req->sconn,
386 nt_errstr(error));
387 return;
391 struct smbd_smb2_create_state {
392 struct smbd_smb2_request *smb2req;
393 struct smb_request *smb1req;
394 struct timed_event *te;
395 struct tevent_immediate *im;
396 struct timeval request_time;
397 struct file_id id;
398 DATA_BLOB private_data;
399 uint8_t out_oplock_level;
400 uint32_t out_create_action;
401 NTTIME out_creation_time;
402 NTTIME out_last_access_time;
403 NTTIME out_last_write_time;
404 NTTIME out_change_time;
405 uint64_t out_allocation_size;
406 uint64_t out_end_of_file;
407 uint32_t out_file_attributes;
408 uint64_t out_file_id_persistent;
409 uint64_t out_file_id_volatile;
410 struct smb2_create_blobs out_context_blobs;
413 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
414 struct tevent_context *ev,
415 struct smbd_smb2_request *smb2req,
416 uint8_t in_oplock_level,
417 uint32_t in_impersonation_level,
418 uint32_t in_desired_access,
419 uint32_t in_file_attributes,
420 uint32_t in_share_access,
421 uint32_t in_create_disposition,
422 uint32_t in_create_options,
423 const char *in_name,
424 struct smb2_create_blobs in_context_blobs)
426 struct tevent_req *req = NULL;
427 struct smbd_smb2_create_state *state = NULL;
428 NTSTATUS status;
429 struct smb_request *smb1req = NULL;
430 files_struct *result = NULL;
431 int info;
432 struct timespec write_time_ts;
433 struct smb2_create_blobs out_context_blobs;
434 int requested_oplock_level;
436 ZERO_STRUCT(out_context_blobs);
438 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
439 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
440 } else {
441 requested_oplock_level = in_oplock_level;
445 if (smb2req->subreq == NULL) {
446 /* New create call. */
447 req = tevent_req_create(mem_ctx, &state,
448 struct smbd_smb2_create_state);
449 if (req == NULL) {
450 return NULL;
452 state->smb2req = smb2req;
454 smb1req = smbd_smb2_fake_smb_request(smb2req);
455 if (tevent_req_nomem(smb1req, req)) {
456 return tevent_req_post(req, ev);
458 state->smb1req = smb1req;
459 smb2req->subreq = req;
460 DEBUG(10,("smbd_smb2_create: name[%s]\n",
461 in_name));
462 } else {
463 /* Re-entrant create call. */
464 req = smb2req->subreq;
465 state = tevent_req_data(req,
466 struct smbd_smb2_create_state);
467 smb1req = state->smb1req;
468 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
469 in_name ));
472 if (IS_IPC(smb1req->conn)) {
473 const char *pipe_name = in_name;
475 if (!lp_nt_pipe_support()) {
476 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
477 return tevent_req_post(req, ev);
480 status = open_np_file(smb1req, pipe_name, &result);
481 if (!NT_STATUS_IS_OK(status)) {
482 tevent_req_nterror(req, status);
483 return tevent_req_post(req, ev);
485 info = FILE_WAS_OPENED;
486 } else if (CAN_PRINT(smb1req->conn)) {
487 status = file_new(smb1req, smb1req->conn, &result);
488 if(!NT_STATUS_IS_OK(status)) {
489 tevent_req_nterror(req, status);
490 return tevent_req_post(req, ev);
493 status = print_spool_open(result, in_name,
494 smb1req->vuid);
495 if (!NT_STATUS_IS_OK(status)) {
496 file_free(smb1req, result);
497 tevent_req_nterror(req, status);
498 return tevent_req_post(req, ev);
500 info = FILE_WAS_CREATED;
501 } else {
502 char *fname;
503 struct smb_filename *smb_fname = NULL;
504 struct smb2_create_blob *exta = NULL;
505 struct ea_list *ea_list = NULL;
506 struct smb2_create_blob *mxac = NULL;
507 NTTIME max_access_time = 0;
508 struct smb2_create_blob *secd = NULL;
509 struct security_descriptor *sec_desc = NULL;
510 struct smb2_create_blob *dhnq = NULL;
511 struct smb2_create_blob *dhnc = NULL;
512 struct smb2_create_blob *alsi = NULL;
513 uint64_t allocation_size = 0;
514 struct smb2_create_blob *twrp = NULL;
515 struct smb2_create_blob *qfid = NULL;
517 exta = smb2_create_blob_find(&in_context_blobs,
518 SMB2_CREATE_TAG_EXTA);
519 mxac = smb2_create_blob_find(&in_context_blobs,
520 SMB2_CREATE_TAG_MXAC);
521 secd = smb2_create_blob_find(&in_context_blobs,
522 SMB2_CREATE_TAG_SECD);
523 dhnq = smb2_create_blob_find(&in_context_blobs,
524 SMB2_CREATE_TAG_DHNQ);
525 dhnc = smb2_create_blob_find(&in_context_blobs,
526 SMB2_CREATE_TAG_DHNC);
527 alsi = smb2_create_blob_find(&in_context_blobs,
528 SMB2_CREATE_TAG_ALSI);
529 twrp = smb2_create_blob_find(&in_context_blobs,
530 SMB2_CREATE_TAG_TWRP);
531 qfid = smb2_create_blob_find(&in_context_blobs,
532 SMB2_CREATE_TAG_QFID);
534 fname = talloc_strdup(state, in_name);
535 if (tevent_req_nomem(fname, req)) {
536 return tevent_req_post(req, ev);
539 if (exta) {
540 if (dhnc) {
541 tevent_req_nterror(req,NT_STATUS_OBJECT_NAME_NOT_FOUND);
542 return tevent_req_post(req, ev);
545 ea_list = read_nttrans_ea_list(mem_ctx,
546 (const char *)exta->data.data, exta->data.length);
547 if (!ea_list) {
548 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
549 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
550 return tevent_req_post(req, ev);
554 if (mxac) {
555 if (dhnc) {
556 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
557 return tevent_req_post(req, ev);
560 if (mxac->data.length == 0) {
561 max_access_time = 0;
562 } else if (mxac->data.length == 8) {
563 max_access_time = BVAL(mxac->data.data, 0);
564 } else {
565 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
566 return tevent_req_post(req, ev);
570 if (secd) {
571 enum ndr_err_code ndr_err;
573 if (dhnc) {
574 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
575 return tevent_req_post(req, ev);
578 sec_desc = talloc_zero(state, struct security_descriptor);
579 if (tevent_req_nomem(sec_desc, req)) {
580 return tevent_req_post(req, ev);
583 ndr_err = ndr_pull_struct_blob(&secd->data,
584 sec_desc, sec_desc,
585 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
586 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
587 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
588 ndr_errstr(ndr_err)));
589 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
590 return tevent_req_post(req, ev);
594 if (dhnq) {
595 if (dhnc) {
596 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
597 return tevent_req_post(req, ev);
600 if (dhnq->data.length != 16) {
601 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
602 return tevent_req_post(req, ev);
605 * we don't support durable handles yet
606 * and have to ignore this
610 if (dhnc) {
611 if (dhnc->data.length != 16) {
612 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
613 return tevent_req_post(req, ev);
615 /* we don't support durable handles yet */
616 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
617 return tevent_req_post(req, ev);
620 if (alsi) {
621 if (dhnc) {
622 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
623 return tevent_req_post(req, ev);
626 if (alsi->data.length != 8) {
627 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
628 return tevent_req_post(req, ev);
630 allocation_size = BVAL(alsi->data.data, 0);
633 if (twrp) {
634 NTTIME nttime;
635 time_t t;
636 struct tm *tm;
638 if (dhnc) {
639 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
640 return tevent_req_post(req, ev);
643 if (twrp->data.length != 8) {
644 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
645 return tevent_req_post(req, ev);
648 nttime = BVAL(twrp->data.data, 0);
649 t = nt_time_to_unix(nttime);
650 tm = gmtime(&t);
652 TALLOC_FREE(fname);
653 fname = talloc_asprintf(state,
654 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
655 tm->tm_year + 1900,
656 tm->tm_mon + 1,
657 tm->tm_mday,
658 tm->tm_hour,
659 tm->tm_min,
660 tm->tm_sec,
661 in_name);
662 if (tevent_req_nomem(fname, req)) {
663 return tevent_req_post(req, ev);
667 if (qfid) {
668 if (qfid->data.length != 0) {
669 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
670 return tevent_req_post(req, ev);
674 /* these are ignored for SMB2 */
675 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
676 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
679 * For a DFS path the function parse_dfs_path()
680 * will do the path processing.
683 if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
684 /* convert '\\' into '/' */
685 status = check_path_syntax(fname);
686 if (!NT_STATUS_IS_OK(status)) {
687 tevent_req_nterror(req, status);
688 return tevent_req_post(req, ev);
692 status = filename_convert(req,
693 smb1req->conn,
694 smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
695 fname,
696 0, /* unix_convert flags */
697 NULL, /* ppath_contains_wcards */
698 &smb_fname);
699 if (!NT_STATUS_IS_OK(status)) {
700 tevent_req_nterror(req, status);
701 return tevent_req_post(req, ev);
704 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
706 status = SMB_VFS_CREATE_FILE(smb1req->conn,
707 smb1req,
708 0, /* root_dir_fid */
709 smb_fname,
710 in_desired_access,
711 in_share_access,
712 in_create_disposition,
713 in_create_options,
714 in_file_attributes,
715 map_smb2_oplock_levels_to_samba(requested_oplock_level),
716 allocation_size,
717 0, /* private_flags */
718 sec_desc,
719 ea_list,
720 &result,
721 &info);
722 if (!NT_STATUS_IS_OK(status)) {
723 if (open_was_deferred(smb1req->sconn, smb1req->mid)) {
724 return req;
726 tevent_req_nterror(req, status);
727 return tevent_req_post(req, ev);
730 if (mxac) {
731 NTTIME last_write_time;
733 unix_timespec_to_nt_time(&last_write_time,
734 result->fsp_name->st.st_ex_mtime);
735 if (last_write_time != max_access_time) {
736 uint8_t p[8];
737 uint32_t max_access_granted;
738 DATA_BLOB blob = data_blob_const(p, sizeof(p));
740 status = smbd_calculate_access_mask(smb1req->conn,
741 result->fsp_name,
742 SEC_FLAG_MAXIMUM_ALLOWED,
743 &max_access_granted);
745 SIVAL(p, 0, NT_STATUS_V(status));
746 SIVAL(p, 4, max_access_granted);
748 status = smb2_create_blob_add(state,
749 &out_context_blobs,
750 SMB2_CREATE_TAG_MXAC,
751 blob);
752 if (!NT_STATUS_IS_OK(status)) {
753 tevent_req_nterror(req, status);
754 return tevent_req_post(req, ev);
759 if (qfid) {
760 uint8_t p[32];
761 uint64_t file_index = get_FileIndex(result->conn,
762 &result->fsp_name->st);
763 DATA_BLOB blob = data_blob_const(p, sizeof(p));
765 ZERO_STRUCT(p);
767 /* From conversations with Microsoft engineers at
768 the MS plugfest. The first 8 bytes are the "volume index"
769 == inode, the second 8 bytes are the "volume id",
770 == dev. This will be updated in the SMB2 doc. */
771 SBVAL(p, 0, file_index);
772 SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
774 status = smb2_create_blob_add(state, &out_context_blobs,
775 SMB2_CREATE_TAG_QFID,
776 blob);
777 if (!NT_STATUS_IS_OK(status)) {
778 tevent_req_nterror(req, status);
779 return tevent_req_post(req, ev);
784 smb2req->compat_chain_fsp = smb1req->chain_fsp;
786 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
787 state->out_oplock_level = in_oplock_level;
788 } else {
789 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
792 if ((in_create_disposition == FILE_SUPERSEDE)
793 && (info == FILE_WAS_OVERWRITTEN)) {
794 state->out_create_action = FILE_WAS_SUPERSEDED;
795 } else {
796 state->out_create_action = info;
798 state->out_file_attributes = dos_mode(result->conn,
799 result->fsp_name);
800 /* Deal with other possible opens having a modified
801 write time. JRA. */
802 ZERO_STRUCT(write_time_ts);
803 get_file_infos(result->file_id, 0, NULL, &write_time_ts);
804 if (!null_timespec(write_time_ts)) {
805 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
808 unix_timespec_to_nt_time(&state->out_creation_time,
809 get_create_timespec(smb1req->conn, result,
810 result->fsp_name));
811 unix_timespec_to_nt_time(&state->out_last_access_time,
812 result->fsp_name->st.st_ex_atime);
813 unix_timespec_to_nt_time(&state->out_last_write_time,
814 result->fsp_name->st.st_ex_mtime);
815 unix_timespec_to_nt_time(&state->out_change_time,
816 get_change_timespec(smb1req->conn, result,
817 result->fsp_name));
818 state->out_allocation_size =
819 SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
820 &(result->fsp_name->st));
821 state->out_end_of_file = result->fsp_name->st.st_ex_size;
822 if (state->out_file_attributes == 0) {
823 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
825 state->out_file_id_persistent = result->op->global->open_persistent_id;
826 state->out_file_id_volatile = result->op->global->open_volatile_id;
827 state->out_context_blobs = out_context_blobs;
829 DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
830 fsp_str_dbg(result), fsp_fnum_dbg(result)));
832 tevent_req_done(req);
833 return tevent_req_post(req, ev);
836 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
837 TALLOC_CTX *mem_ctx,
838 uint8_t *out_oplock_level,
839 uint32_t *out_create_action,
840 NTTIME *out_creation_time,
841 NTTIME *out_last_access_time,
842 NTTIME *out_last_write_time,
843 NTTIME *out_change_time,
844 uint64_t *out_allocation_size,
845 uint64_t *out_end_of_file,
846 uint32_t *out_file_attributes,
847 uint64_t *out_file_id_persistent,
848 uint64_t *out_file_id_volatile,
849 struct smb2_create_blobs *out_context_blobs)
851 NTSTATUS status;
852 struct smbd_smb2_create_state *state = tevent_req_data(req,
853 struct smbd_smb2_create_state);
855 if (tevent_req_is_nterror(req, &status)) {
856 tevent_req_received(req);
857 return status;
860 *out_oplock_level = state->out_oplock_level;
861 *out_create_action = state->out_create_action;
862 *out_creation_time = state->out_creation_time;
863 *out_last_access_time = state->out_last_access_time;
864 *out_last_write_time = state->out_last_write_time;
865 *out_change_time = state->out_change_time;
866 *out_allocation_size = state->out_allocation_size;
867 *out_end_of_file = state->out_end_of_file;
868 *out_file_attributes = state->out_file_attributes;
869 *out_file_id_persistent = state->out_file_id_persistent;
870 *out_file_id_volatile = state->out_file_id_volatile;
871 *out_context_blobs = state->out_context_blobs;
873 talloc_steal(mem_ctx, state->out_context_blobs.blobs);
875 tevent_req_received(req);
876 return NT_STATUS_OK;
879 /*********************************************************
880 Code for dealing with deferred opens.
881 *********************************************************/
883 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
884 struct timeval *p_request_time,
885 void **pp_state)
887 struct smbd_smb2_create_state *state = NULL;
888 struct tevent_req *req = NULL;
890 if (!smb2req) {
891 return false;
893 if (smb2req->async_te == NULL) {
894 return false;
896 req = smb2req->subreq;
897 if (!req) {
898 return false;
900 state = tevent_req_data(req, struct smbd_smb2_create_state);
901 if (!state) {
902 return false;
904 if (p_request_time) {
905 *p_request_time = state->request_time;
907 if (pp_state) {
908 *pp_state = (void *)state->private_data.data;
910 return true;
913 /*********************************************************
914 Re-process this call early - requested by message or
915 close.
916 *********************************************************/
918 static struct smbd_smb2_request *find_open_smb2req(
919 struct smbd_server_connection *sconn, uint64_t mid)
921 struct smbd_smb2_request *smb2req;
923 for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
924 uint64_t message_id;
925 if (smb2req->subreq == NULL) {
926 /* This message has been processed. */
927 continue;
929 if (!tevent_req_is_in_progress(smb2req->subreq)) {
930 /* This message has been processed. */
931 continue;
933 message_id = get_mid_from_smb2req(smb2req);
934 if (message_id == mid) {
935 return smb2req;
938 return NULL;
941 bool open_was_deferred_smb2(struct smbd_server_connection *sconn, uint64_t mid)
943 struct smbd_smb2_create_state *state = NULL;
944 struct smbd_smb2_request *smb2req;
946 smb2req = find_open_smb2req(sconn, mid);
948 if (!smb2req) {
949 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
950 (unsigned long long)mid));
951 return false;
953 if (!smb2req->subreq) {
954 return false;
956 if (!tevent_req_is_in_progress(smb2req->subreq)) {
957 return false;
959 state = tevent_req_data(smb2req->subreq,
960 struct smbd_smb2_create_state);
961 if (!state) {
962 return false;
964 /* It's not in progress if there's no timeout event. */
965 if (!state->te) {
966 return false;
969 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
970 (unsigned long long)mid));
972 return true;
975 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
976 uint64_t mid)
978 struct smbd_smb2_create_state *state = NULL;
980 if (!smb2req->subreq) {
981 return;
983 if (!tevent_req_is_in_progress(smb2req->subreq)) {
984 return;
986 state = tevent_req_data(smb2req->subreq,
987 struct smbd_smb2_create_state);
988 if (!state) {
989 return;
992 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
993 "mid %llu\n",
994 (unsigned long long)mid ));
996 /* Ensure we don't have any outstanding timer event. */
997 TALLOC_FREE(state->te);
998 /* Ensure we don't have any outstanding immediate event. */
999 TALLOC_FREE(state->im);
1002 void remove_deferred_open_message_smb2(
1003 struct smbd_server_connection *sconn, uint64_t mid)
1005 struct smbd_smb2_request *smb2req;
1007 smb2req = find_open_smb2req(sconn, mid);
1009 if (!smb2req) {
1010 DEBUG(10,("remove_deferred_open_message_smb2: "
1011 "can't find mid %llu\n",
1012 (unsigned long long)mid ));
1013 return;
1015 remove_deferred_open_message_smb2_internal(smb2req, mid);
1018 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1019 struct tevent_immediate *im,
1020 void *private_data)
1022 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1023 struct smbd_smb2_request);
1024 struct smbd_server_connection *sconn = smb2req->sconn;
1025 uint64_t mid = get_mid_from_smb2req(smb2req);
1026 NTSTATUS status;
1028 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1029 "re-dispatching mid %llu\n",
1030 (unsigned long long)mid ));
1032 status = smbd_smb2_request_dispatch(smb2req);
1033 if (!NT_STATUS_IS_OK(status)) {
1034 smbd_server_connection_terminate(sconn, nt_errstr(status));
1035 return;
1039 bool schedule_deferred_open_message_smb2(
1040 struct smbd_server_connection *sconn, uint64_t mid)
1042 struct smbd_smb2_create_state *state = NULL;
1043 struct smbd_smb2_request *smb2req;
1045 smb2req = find_open_smb2req(sconn, mid);
1047 if (!smb2req) {
1048 DEBUG(10,("schedule_deferred_open_message_smb2: "
1049 "can't find mid %llu\n",
1050 (unsigned long long)mid ));
1051 return false;
1053 if (!smb2req->subreq) {
1054 return false;
1056 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1057 return false;
1059 state = tevent_req_data(smb2req->subreq,
1060 struct smbd_smb2_create_state);
1061 if (!state) {
1062 return false;
1065 /* Ensure we don't have any outstanding timer event. */
1066 TALLOC_FREE(state->te);
1067 /* Ensure we don't have any outstanding immediate event. */
1068 TALLOC_FREE(state->im);
1071 * This is subtle. We must null out the callback
1072 * before rescheduling, else the first call to
1073 * tevent_req_nterror() causes the _receive()
1074 * function to be called, this causing tevent_req_post()
1075 * to crash.
1077 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1079 state->im = tevent_create_immediate(smb2req);
1080 if (!state->im) {
1081 smbd_server_connection_terminate(smb2req->sconn,
1082 nt_errstr(NT_STATUS_NO_MEMORY));
1083 return false;
1086 DEBUG(10,("schedule_deferred_open_message_smb2: "
1087 "re-processing mid %llu\n",
1088 (unsigned long long)mid ));
1090 tevent_schedule_immediate(state->im,
1091 smb2req->sconn->ev_ctx,
1092 smbd_smb2_create_request_dispatch_immediate,
1093 smb2req);
1095 return true;
1098 /*********************************************************
1099 Re-process this call.
1100 *********************************************************/
1102 static void smb2_deferred_open_timer(struct event_context *ev,
1103 struct timed_event *te,
1104 struct timeval _tval,
1105 void *private_data)
1107 NTSTATUS status;
1108 struct smbd_smb2_create_state *state = NULL;
1109 struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
1110 struct smbd_smb2_request);
1112 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1113 smb2req->current_idx,
1114 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
1116 state = tevent_req_data(smb2req->subreq,
1117 struct smbd_smb2_create_state);
1118 if (!state) {
1119 return;
1122 * Null this out, don't talloc_free. It will
1123 * be talloc_free'd by the tevent library when
1124 * this returns.
1126 state->te = NULL;
1127 /* Ensure we don't have any outstanding immediate event. */
1128 TALLOC_FREE(state->im);
1131 * This is subtle. We must null out the callback
1132 * before rescheduling, else the first call to
1133 * tevent_req_nterror() causes the _receive()
1134 * function to be called, this causing tevent_req_post()
1135 * to crash.
1137 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1139 status = smbd_smb2_request_dispatch(smb2req);
1141 if (!NT_STATUS_IS_OK(status)) {
1142 smbd_server_connection_terminate(smb2req->sconn,
1143 nt_errstr(status));
1147 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1149 struct smbd_smb2_request *smb2req = NULL;
1150 struct smbd_smb2_create_state *state = tevent_req_data(req,
1151 struct smbd_smb2_create_state);
1152 uint64_t mid;
1154 if (!state) {
1155 return false;
1158 if (!state->smb2req) {
1159 return false;
1162 smb2req = state->smb2req;
1163 mid = get_mid_from_smb2req(smb2req);
1165 if (is_deferred_open_async(state->private_data.data)) {
1166 /* Can't cancel an async create. */
1167 return false;
1170 remove_deferred_open_entry(state->id, mid,
1171 messaging_server_id(smb2req->sconn->msg_ctx));
1172 remove_deferred_open_message_smb2_internal(smb2req, mid);
1173 smb2req->cancelled = true;
1175 tevent_req_done(req);
1176 return true;
1179 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1180 struct timeval request_time,
1181 struct timeval timeout,
1182 struct file_id id,
1183 char *private_data,
1184 size_t priv_len)
1186 struct tevent_req *req = NULL;
1187 struct smbd_smb2_create_state *state = NULL;
1188 struct timeval end_time;
1190 if (!smb2req) {
1191 return false;
1193 req = smb2req->subreq;
1194 if (!req) {
1195 return false;
1197 state = tevent_req_data(req, struct smbd_smb2_create_state);
1198 if (!state) {
1199 return false;
1201 state->id = id;
1202 state->request_time = request_time;
1203 state->private_data = data_blob_talloc(state, private_data,
1204 priv_len);
1205 if (!state->private_data.data) {
1206 return false;
1209 /* Re-schedule us to retry on timer expiry. */
1210 end_time = timeval_sum(&request_time, &timeout);
1212 DEBUG(10,("push_deferred_open_message_smb2: "
1213 "timeout at %s\n",
1214 timeval_string(talloc_tos(),
1215 &end_time,
1216 true) ));
1218 state->te = tevent_add_timer(smb2req->sconn->ev_ctx,
1219 state,
1220 end_time,
1221 smb2_deferred_open_timer,
1222 smb2req);
1223 if (!state->te) {
1224 return false;
1227 /* allow this request to be canceled */
1228 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1230 return true;