s3:smbd: use fsp_persistent_id() as persistent_file_id part for SMB2 (bug #8995)
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_create.c
blob9881ed2d515b8e1b90d039ac82f01c704d9d36c9
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_conn))) {
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_conn))) {
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 = fsp_persistent_id(result);
826 state->out_file_id_volatile = result->fnum;
827 state->out_context_blobs = out_context_blobs;
829 tevent_req_done(req);
830 return tevent_req_post(req, ev);
833 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
834 TALLOC_CTX *mem_ctx,
835 uint8_t *out_oplock_level,
836 uint32_t *out_create_action,
837 NTTIME *out_creation_time,
838 NTTIME *out_last_access_time,
839 NTTIME *out_last_write_time,
840 NTTIME *out_change_time,
841 uint64_t *out_allocation_size,
842 uint64_t *out_end_of_file,
843 uint32_t *out_file_attributes,
844 uint64_t *out_file_id_persistent,
845 uint64_t *out_file_id_volatile,
846 struct smb2_create_blobs *out_context_blobs)
848 NTSTATUS status;
849 struct smbd_smb2_create_state *state = tevent_req_data(req,
850 struct smbd_smb2_create_state);
852 if (tevent_req_is_nterror(req, &status)) {
853 tevent_req_received(req);
854 return status;
857 *out_oplock_level = state->out_oplock_level;
858 *out_create_action = state->out_create_action;
859 *out_creation_time = state->out_creation_time;
860 *out_last_access_time = state->out_last_access_time;
861 *out_last_write_time = state->out_last_write_time;
862 *out_change_time = state->out_change_time;
863 *out_allocation_size = state->out_allocation_size;
864 *out_end_of_file = state->out_end_of_file;
865 *out_file_attributes = state->out_file_attributes;
866 *out_file_id_persistent = state->out_file_id_persistent;
867 *out_file_id_volatile = state->out_file_id_volatile;
868 *out_context_blobs = state->out_context_blobs;
870 talloc_steal(mem_ctx, state->out_context_blobs.blobs);
872 tevent_req_received(req);
873 return NT_STATUS_OK;
876 /*********************************************************
877 Code for dealing with deferred opens.
878 *********************************************************/
880 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
881 struct timeval *p_request_time,
882 void **pp_state)
884 struct smbd_smb2_create_state *state = NULL;
885 struct tevent_req *req = NULL;
887 if (!smb2req) {
888 return false;
890 if (smb2req->async_te == NULL) {
891 return false;
893 req = smb2req->subreq;
894 if (!req) {
895 return false;
897 state = tevent_req_data(req, struct smbd_smb2_create_state);
898 if (!state) {
899 return false;
901 if (p_request_time) {
902 *p_request_time = state->request_time;
904 if (pp_state) {
905 *pp_state = (void *)state->private_data.data;
907 return true;
910 /*********************************************************
911 Re-process this call early - requested by message or
912 close.
913 *********************************************************/
915 static struct smbd_smb2_request *find_open_smb2req(
916 struct smbd_server_connection *sconn, uint64_t mid)
918 struct smbd_smb2_request *smb2req;
920 for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
921 uint64_t message_id;
922 if (smb2req->subreq == NULL) {
923 /* This message has been processed. */
924 continue;
926 if (!tevent_req_is_in_progress(smb2req->subreq)) {
927 /* This message has been processed. */
928 continue;
930 message_id = get_mid_from_smb2req(smb2req);
931 if (message_id == mid) {
932 return smb2req;
935 return NULL;
938 bool open_was_deferred_smb2(struct smbd_server_connection *sconn, uint64_t mid)
940 struct smbd_smb2_create_state *state = NULL;
941 struct smbd_smb2_request *smb2req;
943 smb2req = find_open_smb2req(sconn, mid);
945 if (!smb2req) {
946 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
947 (unsigned long long)mid));
948 return false;
950 if (!smb2req->subreq) {
951 return false;
953 if (!tevent_req_is_in_progress(smb2req->subreq)) {
954 return false;
956 state = tevent_req_data(smb2req->subreq,
957 struct smbd_smb2_create_state);
958 if (!state) {
959 return false;
961 /* It's not in progress if there's no timeout event. */
962 if (!state->te) {
963 return false;
966 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
967 (unsigned long long)mid));
969 return true;
972 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
973 uint64_t mid)
975 struct smbd_smb2_create_state *state = NULL;
977 if (!smb2req->subreq) {
978 return;
980 if (!tevent_req_is_in_progress(smb2req->subreq)) {
981 return;
983 state = tevent_req_data(smb2req->subreq,
984 struct smbd_smb2_create_state);
985 if (!state) {
986 return;
989 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
990 "mid %llu\n",
991 (unsigned long long)mid ));
993 /* Ensure we don't have any outstanding timer event. */
994 TALLOC_FREE(state->te);
995 /* Ensure we don't have any outstanding immediate event. */
996 TALLOC_FREE(state->im);
999 void remove_deferred_open_message_smb2(
1000 struct smbd_server_connection *sconn, uint64_t mid)
1002 struct smbd_smb2_request *smb2req;
1004 smb2req = find_open_smb2req(sconn, mid);
1006 if (!smb2req) {
1007 DEBUG(10,("remove_deferred_open_message_smb2: "
1008 "can't find mid %llu\n",
1009 (unsigned long long)mid ));
1010 return;
1012 remove_deferred_open_message_smb2_internal(smb2req, mid);
1015 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1016 struct tevent_immediate *im,
1017 void *private_data)
1019 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1020 struct smbd_smb2_request);
1021 struct smbd_server_connection *sconn = smb2req->sconn;
1022 uint64_t mid = get_mid_from_smb2req(smb2req);
1023 NTSTATUS status;
1025 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1026 "re-dispatching mid %llu\n",
1027 (unsigned long long)mid ));
1029 status = smbd_smb2_request_dispatch(smb2req);
1030 if (!NT_STATUS_IS_OK(status)) {
1031 smbd_server_connection_terminate(sconn, nt_errstr(status));
1032 return;
1036 void schedule_deferred_open_message_smb2(
1037 struct smbd_server_connection *sconn, uint64_t mid)
1039 struct smbd_smb2_create_state *state = NULL;
1040 struct smbd_smb2_request *smb2req;
1042 smb2req = find_open_smb2req(sconn, mid);
1044 if (!smb2req) {
1045 DEBUG(10,("schedule_deferred_open_message_smb2: "
1046 "can't find mid %llu\n",
1047 (unsigned long long)mid ));
1048 return;
1050 if (!smb2req->subreq) {
1051 return;
1053 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1054 return;
1056 state = tevent_req_data(smb2req->subreq,
1057 struct smbd_smb2_create_state);
1058 if (!state) {
1059 return;
1062 /* Ensure we don't have any outstanding timer event. */
1063 TALLOC_FREE(state->te);
1064 /* Ensure we don't have any outstanding immediate event. */
1065 TALLOC_FREE(state->im);
1068 * This is subtle. We must null out the callback
1069 * before resheduling, else the first call to
1070 * tevent_req_nterror() causes the _receive()
1071 * function to be called, this causing tevent_req_post()
1072 * to crash.
1074 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1076 state->im = tevent_create_immediate(smb2req);
1077 if (!state->im) {
1078 smbd_server_connection_terminate(smb2req->sconn,
1079 nt_errstr(NT_STATUS_NO_MEMORY));
1080 return;
1083 DEBUG(10,("schedule_deferred_open_message_smb2: "
1084 "re-processing mid %llu\n",
1085 (unsigned long long)mid ));
1087 tevent_schedule_immediate(state->im,
1088 smb2req->sconn->ev_ctx,
1089 smbd_smb2_create_request_dispatch_immediate,
1090 smb2req);
1093 /*********************************************************
1094 Re-process this call.
1095 *********************************************************/
1097 static void smb2_deferred_open_timer(struct event_context *ev,
1098 struct timed_event *te,
1099 struct timeval _tval,
1100 void *private_data)
1102 NTSTATUS status;
1103 struct smbd_smb2_create_state *state = NULL;
1104 struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
1105 struct smbd_smb2_request);
1107 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1108 smb2req->current_idx,
1109 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
1111 state = tevent_req_data(smb2req->subreq,
1112 struct smbd_smb2_create_state);
1113 if (!state) {
1114 return;
1117 * Null this out, don't talloc_free. It will
1118 * be talloc_free'd by the tevent library when
1119 * this returns.
1121 state->te = NULL;
1122 /* Ensure we don't have any outstanding immediate event. */
1123 TALLOC_FREE(state->im);
1126 * This is subtle. We must null out the callback
1127 * before resheduling, else the first call to
1128 * tevent_req_nterror() causes the _receive()
1129 * function to be called, this causing tevent_req_post()
1130 * to crash.
1132 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1134 status = smbd_smb2_request_dispatch(smb2req);
1136 if (!NT_STATUS_IS_OK(status)) {
1137 smbd_server_connection_terminate(smb2req->sconn,
1138 nt_errstr(status));
1142 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1144 struct smbd_smb2_request *smb2req = NULL;
1145 struct smbd_smb2_create_state *state = tevent_req_data(req,
1146 struct smbd_smb2_create_state);
1147 uint64_t mid;
1149 if (!state) {
1150 return false;
1153 if (!state->smb2req) {
1154 return false;
1157 smb2req = state->smb2req;
1158 mid = get_mid_from_smb2req(smb2req);
1160 remove_deferred_open_entry(state->id, mid,
1161 messaging_server_id(smb2req->sconn->msg_ctx));
1162 remove_deferred_open_message_smb2_internal(smb2req, mid);
1163 smb2req->cancelled = true;
1165 tevent_req_done(req);
1166 return true;
1169 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1170 struct timeval request_time,
1171 struct timeval timeout,
1172 struct file_id id,
1173 char *private_data,
1174 size_t priv_len)
1176 struct tevent_req *req = NULL;
1177 struct smbd_smb2_create_state *state = NULL;
1178 struct timeval end_time;
1180 if (!smb2req) {
1181 return false;
1183 req = smb2req->subreq;
1184 if (!req) {
1185 return false;
1187 state = tevent_req_data(req, struct smbd_smb2_create_state);
1188 if (!state) {
1189 return false;
1191 state->id = id;
1192 state->request_time = request_time;
1193 state->private_data = data_blob_talloc(state, private_data,
1194 priv_len);
1195 if (!state->private_data.data) {
1196 return false;
1199 /* Re-schedule us to retry on timer expiry. */
1200 end_time = timeval_sum(&request_time, &timeout);
1202 DEBUG(10,("push_deferred_open_message_smb2: "
1203 "timeout at %s\n",
1204 timeval_string(talloc_tos(),
1205 &end_time,
1206 true) ));
1208 state->te = tevent_add_timer(smb2req->sconn->ev_ctx,
1209 state,
1210 end_time,
1211 smb2_deferred_open_timer,
1212 smb2req);
1213 if (!state->te) {
1214 return false;
1217 /* allow this request to be canceled */
1218 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1220 return true;