When walking the SMB2 requests queue, ensure the request is still "in flight"
[Samba/ekacnet.git] / source3 / smbd / smb2_create.c
blob377db3214862b57a76025ad9f7571ad3f15f2ac1
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
25 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
27 switch(in_oplock_level) {
28 case SMB2_OPLOCK_LEVEL_NONE:
29 return NO_OPLOCK;
30 case SMB2_OPLOCK_LEVEL_II:
31 return LEVEL_II_OPLOCK;
32 case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
33 return EXCLUSIVE_OPLOCK;
34 case SMB2_OPLOCK_LEVEL_BATCH:
35 return BATCH_OPLOCK;
36 case SMB2_OPLOCK_LEVEL_LEASE:
37 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
38 "LEASE_OPLOCK_REQUESTED\n"));
39 return NO_OPLOCK;
40 default:
41 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
42 "unknown level %u\n",
43 (unsigned int)in_oplock_level));
44 return NO_OPLOCK;
48 uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
50 if (BATCH_OPLOCK_TYPE(oplock_type)) {
51 return SMB2_OPLOCK_LEVEL_BATCH;
52 } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type)) {
53 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
54 } else if (LEVEL_II_OPLOCK_TYPE(oplock_type)) {
55 return SMB2_OPLOCK_LEVEL_II;
56 } else {
57 return SMB2_OPLOCK_LEVEL_NONE;
61 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
62 struct tevent_context *ev,
63 struct smbd_smb2_request *smb2req,
64 uint8_t in_oplock_level,
65 uint32_t in_impersonation_level,
66 uint32_t in_desired_access,
67 uint32_t in_file_attributes,
68 uint32_t in_share_access,
69 uint32_t in_create_disposition,
70 uint32_t in_create_options,
71 const char *in_name,
72 struct smb2_create_blobs in_context_blobs);
73 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
74 TALLOC_CTX *mem_ctx,
75 uint8_t *out_oplock_level,
76 uint32_t *out_create_action,
77 NTTIME *out_creation_time,
78 NTTIME *out_last_access_time,
79 NTTIME *out_last_write_time,
80 NTTIME *out_change_time,
81 uint64_t *out_allocation_size,
82 uint64_t *out_end_of_file,
83 uint32_t *out_file_attributes,
84 uint64_t *out_file_id_volatile,
85 struct smb2_create_blobs *out_context_blobs);
87 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
88 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
90 const uint8_t *inbody;
91 int i = smb2req->current_idx;
92 size_t expected_body_size = 0x39;
93 size_t body_size;
94 uint8_t in_oplock_level;
95 uint32_t in_impersonation_level;
96 uint32_t in_desired_access;
97 uint32_t in_file_attributes;
98 uint32_t in_share_access;
99 uint32_t in_create_disposition;
100 uint32_t in_create_options;
101 uint16_t in_name_offset;
102 uint16_t in_name_length;
103 DATA_BLOB in_name_buffer;
104 char *in_name_string;
105 size_t in_name_string_size;
106 uint32_t name_offset = 0;
107 uint32_t name_available_length = 0;
108 uint32_t in_context_offset;
109 uint32_t in_context_length;
110 DATA_BLOB in_context_buffer;
111 struct smb2_create_blobs in_context_blobs;
112 uint32_t context_offset = 0;
113 uint32_t context_available_length = 0;
114 uint32_t dyn_offset;
115 NTSTATUS status;
116 bool ok;
117 struct tevent_req *tsubreq;
119 if (smb2req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
120 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
123 inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
125 body_size = SVAL(inbody, 0x00);
126 if (body_size != expected_body_size) {
127 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
130 in_oplock_level = CVAL(inbody, 0x03);
131 in_impersonation_level = IVAL(inbody, 0x04);
132 in_desired_access = IVAL(inbody, 0x18);
133 in_file_attributes = IVAL(inbody, 0x1C);
134 in_share_access = IVAL(inbody, 0x20);
135 in_create_disposition = IVAL(inbody, 0x24);
136 in_create_options = IVAL(inbody, 0x28);
137 in_name_offset = SVAL(inbody, 0x2C);
138 in_name_length = SVAL(inbody, 0x2E);
139 in_context_offset = IVAL(inbody, 0x30);
140 in_context_length = IVAL(inbody, 0x34);
143 * First check if the dynamic name and context buffers
144 * are correctly specified.
146 * Note: That we don't check if the name and context buffers
147 * overlap
150 dyn_offset = SMB2_HDR_BODY + (body_size & 0xFFFFFFFE);
152 if (in_name_offset == 0 && in_name_length == 0) {
153 /* This is ok */
154 name_offset = 0;
155 } else if (in_name_offset < dyn_offset) {
156 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
157 } else {
158 name_offset = in_name_offset - dyn_offset;
161 if (name_offset > smb2req->in.vector[i+2].iov_len) {
162 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
165 name_available_length = smb2req->in.vector[i+2].iov_len - name_offset;
167 if (in_name_length > name_available_length) {
168 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
171 in_name_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
172 name_offset;
173 in_name_buffer.length = in_name_length;
175 if (in_context_offset == 0 && in_context_length == 0) {
176 /* This is ok */
177 context_offset = 0;
178 } else if (in_context_offset < dyn_offset) {
179 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
180 } else {
181 context_offset = in_context_offset - dyn_offset;
184 if (context_offset > smb2req->in.vector[i+2].iov_len) {
185 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
188 context_available_length = smb2req->in.vector[i+2].iov_len - context_offset;
190 if (in_context_length > context_available_length) {
191 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
194 in_context_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
195 context_offset;
196 in_context_buffer.length = in_context_length;
199 * Now interpret the name and context buffers
202 ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
203 in_name_buffer.data,
204 in_name_buffer.length,
205 &in_name_string,
206 &in_name_string_size, false);
207 if (!ok) {
208 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
211 ZERO_STRUCT(in_context_blobs);
212 status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
213 if (!NT_STATUS_IS_OK(status)) {
214 return smbd_smb2_request_error(smb2req, status);
217 tsubreq = smbd_smb2_create_send(smb2req,
218 smb2req->sconn->smb2.event_ctx,
219 smb2req,
220 in_oplock_level,
221 in_impersonation_level,
222 in_desired_access,
223 in_file_attributes,
224 in_share_access,
225 in_create_disposition,
226 in_create_options,
227 in_name_string,
228 in_context_blobs);
229 if (tsubreq == NULL) {
230 smb2req->subreq = NULL;
231 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
233 tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
235 return smbd_smb2_request_pending_queue(smb2req, tsubreq);
238 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
240 uint8_t *reqhdr = (uint8_t *)smb2req->out.vector[smb2req->current_idx].iov_base;
241 return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
244 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
246 struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
247 struct smbd_smb2_request);
248 int i = smb2req->current_idx;
249 uint8_t *outhdr;
250 DATA_BLOB outbody;
251 DATA_BLOB outdyn;
252 uint8_t out_oplock_level = 0;
253 uint32_t out_create_action = 0;
254 NTTIME out_creation_time = 0;
255 NTTIME out_last_access_time = 0;
256 NTTIME out_last_write_time = 0;
257 NTTIME out_change_time = 0;
258 uint64_t out_allocation_size = 0;
259 uint64_t out_end_of_file = 0;
260 uint32_t out_file_attributes = 0;
261 uint64_t out_file_id_volatile = 0;
262 struct smb2_create_blobs out_context_blobs;
263 DATA_BLOB out_context_buffer;
264 uint16_t out_context_buffer_offset = 0;
265 NTSTATUS status;
266 NTSTATUS error; /* transport error */
268 if (smb2req->cancelled) {
269 uint64_t mid = get_mid_from_smb2req(smb2req);
270 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
271 (unsigned long long)mid ));
272 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
273 if (!NT_STATUS_IS_OK(error)) {
274 smbd_server_connection_terminate(smb2req->sconn,
275 nt_errstr(error));
276 return;
278 return;
281 status = smbd_smb2_create_recv(tsubreq,
282 smb2req,
283 &out_oplock_level,
284 &out_create_action,
285 &out_creation_time,
286 &out_last_access_time,
287 &out_last_write_time,
288 &out_change_time,
289 &out_allocation_size,
290 &out_end_of_file,
291 &out_file_attributes,
292 &out_file_id_volatile,
293 &out_context_blobs);
294 if (!NT_STATUS_IS_OK(status)) {
295 error = smbd_smb2_request_error(smb2req, status);
296 if (!NT_STATUS_IS_OK(error)) {
297 smbd_server_connection_terminate(smb2req->sconn,
298 nt_errstr(error));
299 return;
301 return;
304 status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
305 if (!NT_STATUS_IS_OK(status)) {
306 error = smbd_smb2_request_error(smb2req, status);
307 if (!NT_STATUS_IS_OK(error)) {
308 smbd_server_connection_terminate(smb2req->sconn,
309 nt_errstr(error));
310 return;
312 return;
315 if (out_context_buffer.length > 0) {
316 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
319 outhdr = (uint8_t *)smb2req->out.vector[i].iov_base;
321 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
322 if (outbody.data == NULL) {
323 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
324 if (!NT_STATUS_IS_OK(error)) {
325 smbd_server_connection_terminate(smb2req->sconn,
326 nt_errstr(error));
327 return;
329 return;
332 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
333 SCVAL(outbody.data, 0x02,
334 out_oplock_level); /* oplock level */
335 SCVAL(outbody.data, 0x03, 0); /* reserved */
336 SIVAL(outbody.data, 0x04,
337 out_create_action); /* create action */
338 SBVAL(outbody.data, 0x08,
339 out_creation_time); /* creation time */
340 SBVAL(outbody.data, 0x10,
341 out_last_access_time); /* last access time */
342 SBVAL(outbody.data, 0x18,
343 out_last_write_time); /* last write time */
344 SBVAL(outbody.data, 0x20,
345 out_change_time); /* change time */
346 SBVAL(outbody.data, 0x28,
347 out_allocation_size); /* allocation size */
348 SBVAL(outbody.data, 0x30,
349 out_end_of_file); /* end of file */
350 SIVAL(outbody.data, 0x38,
351 out_file_attributes); /* file attributes */
352 SIVAL(outbody.data, 0x3C, 0); /* reserved */
353 SBVAL(outbody.data, 0x40, 0); /* file id (persistent) */
354 SBVAL(outbody.data, 0x48,
355 out_file_id_volatile); /* file id (volatile) */
356 SIVAL(outbody.data, 0x50,
357 out_context_buffer_offset); /* create contexts offset */
358 SIVAL(outbody.data, 0x54,
359 out_context_buffer.length); /* create contexts length */
361 outdyn = out_context_buffer;
363 error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
364 if (!NT_STATUS_IS_OK(error)) {
365 smbd_server_connection_terminate(smb2req->sconn,
366 nt_errstr(error));
367 return;
371 struct smbd_smb2_create_state {
372 struct smbd_smb2_request *smb2req;
373 struct smb_request *smb1req;
374 struct timed_event *te;
375 struct timeval request_time;
376 struct file_id id;
377 DATA_BLOB private_data;
378 uint8_t out_oplock_level;
379 uint32_t out_create_action;
380 NTTIME out_creation_time;
381 NTTIME out_last_access_time;
382 NTTIME out_last_write_time;
383 NTTIME out_change_time;
384 uint64_t out_allocation_size;
385 uint64_t out_end_of_file;
386 uint32_t out_file_attributes;
387 uint64_t out_file_id_volatile;
388 struct smb2_create_blobs out_context_blobs;
391 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
392 struct tevent_context *ev,
393 struct smbd_smb2_request *smb2req,
394 uint8_t in_oplock_level,
395 uint32_t in_impersonation_level,
396 uint32_t in_desired_access,
397 uint32_t in_file_attributes,
398 uint32_t in_share_access,
399 uint32_t in_create_disposition,
400 uint32_t in_create_options,
401 const char *in_name,
402 struct smb2_create_blobs in_context_blobs)
404 struct tevent_req *req = NULL;
405 struct smbd_smb2_create_state *state = NULL;
406 NTSTATUS status;
407 struct smb_request *smb1req = NULL;
408 files_struct *result = NULL;
409 int info;
410 struct timespec write_time_ts;
411 struct smb2_create_blobs out_context_blobs;
413 ZERO_STRUCT(out_context_blobs);
415 if (!smb2req->async) {
416 /* New create call. */
417 req = tevent_req_create(mem_ctx, &state,
418 struct smbd_smb2_create_state);
419 if (req == NULL) {
420 return NULL;
422 state->smb2req = smb2req;
423 smb2req->subreq = req; /* So we can find this when going async. */
425 smb1req = smbd_smb2_fake_smb_request(smb2req);
426 if (tevent_req_nomem(smb1req, req)) {
427 return tevent_req_post(req, ev);
429 state->smb1req = smb1req;
430 DEBUG(10,("smbd_smb2_create: name[%s]\n",
431 in_name));
432 } else {
433 /* Re-entrant create call. */
434 req = smb2req->subreq;
435 state = tevent_req_data(req,
436 struct smbd_smb2_create_state);
437 smb1req = state->smb1req;
438 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
439 in_name ));
442 if (IS_IPC(smb1req->conn)) {
443 const char *pipe_name = in_name;
445 if (!lp_nt_pipe_support()) {
446 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
447 return tevent_req_post(req, ev);
450 /* Strip \\ off the name. */
451 if (pipe_name[0] == '\\') {
452 pipe_name++;
455 status = open_np_file(smb1req, pipe_name, &result);
456 if (!NT_STATUS_IS_OK(status)) {
457 tevent_req_nterror(req, status);
458 return tevent_req_post(req, ev);
460 info = FILE_WAS_OPENED;
461 } else if (CAN_PRINT(smb1req->conn)) {
462 status = file_new(smb1req, smb1req->conn, &result);
463 if(!NT_STATUS_IS_OK(status)) {
464 tevent_req_nterror(req, status);
465 return tevent_req_post(req, ev);
468 status = print_fsp_open(smb1req,
469 smb1req->conn,
470 in_name,
471 smb1req->vuid,
472 result);
473 if (!NT_STATUS_IS_OK(status)) {
474 file_free(smb1req, result);
475 tevent_req_nterror(req, status);
476 return tevent_req_post(req, ev);
478 info = FILE_WAS_CREATED;
479 } else {
480 char *fname;
481 struct smb_filename *smb_fname = NULL;
482 struct smb2_create_blob *exta = NULL;
483 struct ea_list *ea_list = NULL;
484 struct smb2_create_blob *mxac = NULL;
485 NTTIME max_access_time = 0;
486 struct smb2_create_blob *secd = NULL;
487 struct security_descriptor *sec_desc = NULL;
488 struct smb2_create_blob *dhnq = NULL;
489 struct smb2_create_blob *dhnc = NULL;
490 struct smb2_create_blob *alsi = NULL;
491 uint64_t allocation_size = 0;
492 struct smb2_create_blob *twrp = NULL;
493 struct smb2_create_blob *qfid = NULL;
495 exta = smb2_create_blob_find(&in_context_blobs,
496 SMB2_CREATE_TAG_EXTA);
497 mxac = smb2_create_blob_find(&in_context_blobs,
498 SMB2_CREATE_TAG_MXAC);
499 secd = smb2_create_blob_find(&in_context_blobs,
500 SMB2_CREATE_TAG_SECD);
501 dhnq = smb2_create_blob_find(&in_context_blobs,
502 SMB2_CREATE_TAG_DHNQ);
503 dhnc = smb2_create_blob_find(&in_context_blobs,
504 SMB2_CREATE_TAG_DHNC);
505 alsi = smb2_create_blob_find(&in_context_blobs,
506 SMB2_CREATE_TAG_ALSI);
507 twrp = smb2_create_blob_find(&in_context_blobs,
508 SMB2_CREATE_TAG_TWRP);
509 qfid = smb2_create_blob_find(&in_context_blobs,
510 SMB2_CREATE_TAG_QFID);
512 fname = talloc_strdup(state, in_name);
513 if (tevent_req_nomem(fname, req)) {
514 return tevent_req_post(req, ev);
517 if (exta) {
518 if (dhnc) {
519 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
520 return tevent_req_post(req, ev);
523 ea_list = read_nttrans_ea_list(mem_ctx,
524 (const char *)exta->data.data, exta->data.length);
525 if (!ea_list) {
526 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
527 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
528 return tevent_req_post(req, ev);
532 if (mxac) {
533 if (dhnc) {
534 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
535 return tevent_req_post(req, ev);
538 if (mxac->data.length == 0) {
539 max_access_time = 0;
540 } else if (mxac->data.length == 8) {
541 max_access_time = BVAL(mxac->data.data, 0);
542 } else {
543 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
544 return tevent_req_post(req, ev);
548 if (secd) {
549 enum ndr_err_code ndr_err;
551 if (dhnc) {
552 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
553 return tevent_req_post(req, ev);
556 sec_desc = talloc_zero(state, struct security_descriptor);
557 if (tevent_req_nomem(sec_desc, req)) {
558 return tevent_req_post(req, ev);
561 ndr_err = ndr_pull_struct_blob(&secd->data,
562 sec_desc, NULL, sec_desc,
563 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
564 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
565 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
566 ndr_errstr(ndr_err)));
567 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
568 return tevent_req_post(req, ev);
572 if (dhnq) {
573 if (dhnc) {
574 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
575 return tevent_req_post(req, ev);
578 if (dhnq->data.length != 16) {
579 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
580 return tevent_req_post(req, ev);
583 * we don't support durable handles yet
584 * and have to ignore this
588 if (dhnc) {
589 if (dhnc->data.length != 16) {
590 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
591 return tevent_req_post(req, ev);
593 /* we don't support durable handles yet */
594 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
595 return tevent_req_post(req, ev);
598 if (alsi) {
599 if (dhnc) {
600 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
601 return tevent_req_post(req, ev);
604 if (alsi->data.length != 8) {
605 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
606 return tevent_req_post(req, ev);
608 allocation_size = BVAL(alsi->data.data, 0);
611 if (twrp) {
612 NTTIME nttime;
613 time_t t;
614 struct tm *tm;
616 if (dhnc) {
617 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
618 return tevent_req_post(req, ev);
621 if (twrp->data.length != 8) {
622 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
623 return tevent_req_post(req, ev);
626 nttime = BVAL(twrp->data.data, 0);
627 t = nt_time_to_unix(nttime);
628 tm = gmtime(&t);
630 TALLOC_FREE(fname);
631 fname = talloc_asprintf(state,
632 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
633 tm->tm_year + 1900,
634 tm->tm_mon + 1,
635 tm->tm_mday,
636 tm->tm_hour,
637 tm->tm_min,
638 tm->tm_sec,
639 in_name);
640 if (tevent_req_nomem(fname, req)) {
641 return tevent_req_post(req, ev);
645 if (qfid) {
646 if (qfid->data.length != 0) {
647 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
648 return tevent_req_post(req, ev);
652 /* these are ignored for SMB2 */
653 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
654 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
656 /* convert '\\' into '/' */
657 status = check_path_syntax(fname);
658 if (!NT_STATUS_IS_OK(status)) {
659 tevent_req_nterror(req, status);
660 return tevent_req_post(req, ev);
663 status = filename_convert(req,
664 smb1req->conn,
665 smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
666 fname,
668 NULL,
669 &smb_fname);
670 if (!NT_STATUS_IS_OK(status)) {
671 tevent_req_nterror(req, status);
672 return tevent_req_post(req, ev);
675 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
677 status = SMB_VFS_CREATE_FILE(smb1req->conn,
678 smb1req,
679 0, /* root_dir_fid */
680 smb_fname,
681 in_desired_access,
682 in_share_access,
683 in_create_disposition,
684 in_create_options,
685 in_file_attributes,
686 map_smb2_oplock_levels_to_samba(in_oplock_level),
687 allocation_size,
688 0, /* private_flags */
689 sec_desc,
690 ea_list,
691 &result,
692 &info);
693 if (!NT_STATUS_IS_OK(status)) {
694 if (open_was_deferred(smb1req->mid)) {
695 return req;
697 tevent_req_nterror(req, status);
698 return tevent_req_post(req, ev);
701 if (mxac) {
702 NTTIME last_write_time;
704 unix_timespec_to_nt_time(&last_write_time,
705 result->fsp_name->st.st_ex_mtime);
706 if (last_write_time != max_access_time) {
707 uint8_t p[8];
708 uint32_t max_access_granted;
709 DATA_BLOB blob = data_blob_const(p, sizeof(p));
711 status = smbd_check_open_rights(smb1req->conn,
712 result->fsp_name,
713 SEC_FLAG_MAXIMUM_ALLOWED,
714 &max_access_granted);
716 SIVAL(p, 0, NT_STATUS_V(status));
717 SIVAL(p, 4, max_access_granted);
719 status = smb2_create_blob_add(state,
720 &out_context_blobs,
721 SMB2_CREATE_TAG_MXAC,
722 blob);
723 if (!NT_STATUS_IS_OK(status)) {
724 tevent_req_nterror(req, status);
725 return tevent_req_post(req, ev);
730 if (qfid) {
731 uint8_t p[32];
732 DATA_BLOB blob = data_blob_const(p, sizeof(p));
734 ZERO_STRUCT(p);
736 /* TODO: maybe use result->file_id */
737 SIVAL(p, 0, result->fsp_name->st.st_ex_ino);/* FileIndexLow */
738 SIVAL(p, 4, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
740 status = smb2_create_blob_add(state, &out_context_blobs,
741 SMB2_CREATE_TAG_QFID,
742 blob);
743 if (!NT_STATUS_IS_OK(status)) {
744 tevent_req_nterror(req, status);
745 return tevent_req_post(req, ev);
750 smb2req->compat_chain_fsp = smb1req->chain_fsp;
752 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
754 if ((in_create_disposition == FILE_SUPERSEDE)
755 && (info == FILE_WAS_OVERWRITTEN)) {
756 state->out_create_action = FILE_WAS_SUPERSEDED;
757 } else {
758 state->out_create_action = info;
760 state->out_file_attributes = dos_mode(result->conn,
761 result->fsp_name);
762 /* Deal with other possible opens having a modified
763 write time. JRA. */
764 ZERO_STRUCT(write_time_ts);
765 get_file_infos(result->file_id, NULL, &write_time_ts);
766 if (!null_timespec(write_time_ts)) {
767 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
770 unix_timespec_to_nt_time(&state->out_creation_time,
771 get_create_timespec(smb1req->conn, result,
772 result->fsp_name));
773 unix_timespec_to_nt_time(&state->out_last_access_time,
774 result->fsp_name->st.st_ex_atime);
775 unix_timespec_to_nt_time(&state->out_last_write_time,
776 result->fsp_name->st.st_ex_mtime);
777 unix_timespec_to_nt_time(&state->out_change_time,
778 get_change_timespec(smb1req->conn, result,
779 result->fsp_name));
780 state->out_allocation_size =
781 result->fsp_name->st.st_ex_blksize *
782 result->fsp_name->st.st_ex_blocks;
783 state->out_end_of_file = result->fsp_name->st.st_ex_size;
784 if (state->out_file_attributes == 0) {
785 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
787 state->out_file_id_volatile = result->fnum;
788 state->out_context_blobs = out_context_blobs;
790 tevent_req_done(req);
791 return tevent_req_post(req, ev);
794 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
795 TALLOC_CTX *mem_ctx,
796 uint8_t *out_oplock_level,
797 uint32_t *out_create_action,
798 NTTIME *out_creation_time,
799 NTTIME *out_last_access_time,
800 NTTIME *out_last_write_time,
801 NTTIME *out_change_time,
802 uint64_t *out_allocation_size,
803 uint64_t *out_end_of_file,
804 uint32_t *out_file_attributes,
805 uint64_t *out_file_id_volatile,
806 struct smb2_create_blobs *out_context_blobs)
808 NTSTATUS status;
809 struct smbd_smb2_create_state *state = tevent_req_data(req,
810 struct smbd_smb2_create_state);
812 if (tevent_req_is_nterror(req, &status)) {
813 tevent_req_received(req);
814 return status;
817 *out_oplock_level = state->out_oplock_level;
818 *out_create_action = state->out_create_action;
819 *out_creation_time = state->out_creation_time;
820 *out_last_access_time = state->out_last_access_time;
821 *out_last_write_time = state->out_last_write_time;
822 *out_change_time = state->out_change_time;
823 *out_allocation_size = state->out_allocation_size;
824 *out_end_of_file = state->out_end_of_file;
825 *out_file_attributes = state->out_file_attributes;
826 *out_file_id_volatile = state->out_file_id_volatile;
827 *out_context_blobs = state->out_context_blobs;
829 talloc_steal(mem_ctx, state->out_context_blobs.blobs);
831 tevent_req_received(req);
832 return NT_STATUS_OK;
835 /*********************************************************
836 Code for dealing with deferred opens.
837 *********************************************************/
839 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
840 struct timeval *p_request_time,
841 void **pp_state)
843 struct smbd_smb2_create_state *state = NULL;
844 struct tevent_req *req = NULL;
846 if (!smb2req) {
847 return false;
849 if (!smb2req->async) {
850 return false;
852 req = smb2req->subreq;
853 if (!req) {
854 return false;
856 state = tevent_req_data(req, struct smbd_smb2_create_state);
857 if (!state) {
858 return false;
860 if (p_request_time) {
861 *p_request_time = state->request_time;
863 if (pp_state) {
864 *pp_state = (void *)state->private_data.data;
866 return true;
869 /*********************************************************
870 Re-process this call early - requested by message or
871 close.
872 *********************************************************/
874 static struct smbd_smb2_request *find_open_smb2req(uint64_t mid)
876 struct smbd_server_connection *sconn = smbd_server_conn;
877 struct smbd_smb2_request *smb2req;
879 for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
880 uint64_t message_id;
881 if (smb2req->subreq == NULL) {
882 /* This message has been processed. */
883 continue;
885 if (!tevent_req_is_in_progress(smb2req->subreq)) {
886 /* This message has been processed. */
887 continue;
889 message_id = get_mid_from_smb2req(smb2req);
890 if (message_id == mid) {
891 return smb2req;
894 return NULL;
897 bool open_was_deferred_smb2(uint64_t mid)
899 struct smbd_smb2_create_state *state = NULL;
900 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
902 if (!smb2req) {
903 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
904 (unsigned long long)mid));
905 return false;
907 if (!smb2req->subreq) {
908 return false;
910 if (!tevent_req_is_in_progress(smb2req->subreq)) {
911 return false;
913 state = tevent_req_data(smb2req->subreq,
914 struct smbd_smb2_create_state);
915 if (!state) {
916 return false;
918 /* It's not in progress if there's no timeout event. */
919 if (!state->te) {
920 return false;
923 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
924 (unsigned long long)mid));
926 return true;
929 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
930 uint64_t mid)
932 struct smbd_smb2_create_state *state = NULL;
934 if (!smb2req->subreq) {
935 return;
937 if (!tevent_req_is_in_progress(smb2req->subreq)) {
938 return;
940 state = tevent_req_data(smb2req->subreq,
941 struct smbd_smb2_create_state);
942 if (!state) {
943 return;
946 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
947 "mid %llu\n",
948 (unsigned long long)mid ));
950 /* Ensure we don't have any outstanding timer event. */
951 TALLOC_FREE(state->te);
954 void remove_deferred_open_message_smb2(uint64_t mid)
956 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
958 if (!smb2req) {
959 DEBUG(10,("remove_deferred_open_message_smb2: "
960 "can't find mid %llu\n",
961 (unsigned long long)mid ));
962 return;
964 remove_deferred_open_message_smb2_internal(smb2req, mid);
967 void schedule_deferred_open_message_smb2(uint64_t mid)
969 struct tevent_immediate *im = NULL;
970 struct smbd_smb2_create_state *state = NULL;
971 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
973 if (!smb2req) {
974 DEBUG(10,("schedule_deferred_open_message_smb2: "
975 "can't find mid %llu\n",
976 (unsigned long long)mid ));
977 return;
979 if (!smb2req->subreq) {
980 return;
982 if (!tevent_req_is_in_progress(smb2req->subreq)) {
983 return;
985 state = tevent_req_data(smb2req->subreq,
986 struct smbd_smb2_create_state);
987 if (!state) {
988 return;
990 /* Ensure we don't have any outstanding timer event. */
991 TALLOC_FREE(state->te);
994 * This is subtle. We must null out the callback
995 * before resheduling, else the first call to
996 * tevent_req_nterror() causes the _receive()
997 * function to be called, this causing tevent_req_post()
998 * to crash.
1000 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1002 im = tevent_create_immediate(smb2req);
1003 if (!im) {
1004 smbd_server_connection_terminate(smb2req->sconn,
1005 nt_errstr(NT_STATUS_NO_MEMORY));
1008 DEBUG(10,("schedule_deferred_open_message_smb2: "
1009 "re-processing mid %llu\n",
1010 (unsigned long long)mid ));
1012 tevent_schedule_immediate(im,
1013 smb2req->sconn->smb2.event_ctx,
1014 smbd_smb2_request_dispatch_immediate,
1015 smb2req);
1018 /*********************************************************
1019 Re-process this call.
1020 *********************************************************/
1022 static void smb2_deferred_open_timer(struct event_context *ev,
1023 struct timed_event *te,
1024 struct timeval _tval,
1025 void *private_data)
1027 NTSTATUS status;
1028 struct smbd_smb2_create_state *state = NULL;
1029 struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
1030 struct smbd_smb2_request);
1032 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1033 smb2req->current_idx,
1034 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
1036 state = tevent_req_data(smb2req->subreq,
1037 struct smbd_smb2_create_state);
1038 if (!state) {
1039 return;
1042 * Null this out, don't talloc_free. It will
1043 * be talloc_free'd by the tevent library when
1044 * this returns.
1046 state->te = NULL;
1049 * This is subtle. We must null out the callback
1050 * before resheduling, else the first call to
1051 * tevent_req_nterror() causes the _receive()
1052 * function to be called, this causing tevent_req_post()
1053 * to crash.
1055 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1057 status = smbd_smb2_request_dispatch(smb2req);
1059 if (!NT_STATUS_IS_OK(status)) {
1060 smbd_server_connection_terminate(smb2req->sconn,
1061 nt_errstr(status));
1065 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1067 struct smbd_smb2_request *smb2req = NULL;
1068 struct smbd_smb2_create_state *state = tevent_req_data(req,
1069 struct smbd_smb2_create_state);
1070 uint64_t mid;
1072 if (!state) {
1073 return false;
1076 if (!state->smb2req) {
1077 return false;
1080 smb2req = state->smb2req;
1081 mid = get_mid_from_smb2req(smb2req);
1083 remove_deferred_open_entry(state->id, mid);
1084 remove_deferred_open_message_smb2_internal(smb2req, mid);
1085 smb2req->cancelled = true;
1087 tevent_req_done(req);
1088 return true;
1091 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1092 struct timeval request_time,
1093 struct timeval timeout,
1094 struct file_id id,
1095 char *private_data,
1096 size_t priv_len)
1098 struct tevent_req *req = NULL;
1099 struct smbd_smb2_create_state *state = NULL;
1100 struct timeval end_time;
1102 if (!smb2req) {
1103 return false;
1105 req = smb2req->subreq;
1106 if (!req) {
1107 return false;
1109 state = tevent_req_data(req, struct smbd_smb2_create_state);
1110 if (!state) {
1111 return false;
1113 state->id = id;
1114 state->request_time = request_time;
1115 state->private_data = data_blob_talloc(state, private_data,
1116 priv_len);
1117 if (!state->private_data.data) {
1118 return false;
1120 #if 0
1121 /* Boo - turns out this isn't what W2K8R2
1122 does. It actually sends the STATUS_PENDING
1123 message followed by the STATUS_SHARING_VIOLATION
1124 message. Surely this means that all open
1125 calls (even on directories) will potentially
1126 fail in a chain.... ? And I've seen directory
1127 opens as the start of a chain. JRA.
1130 * More subtlety. To match W2K8R2 don't
1131 * send a "gone async" message if it's simply
1132 * a STATUS_SHARING_VIOLATION (short) wait, not
1133 * an oplock break wait. We do this by prematurely
1134 * setting smb2req->async flag.
1136 if (timeout.tv_sec < 2) {
1137 DEBUG(10,("push_deferred_open_message_smb2: "
1138 "short timer wait (usec = %u). "
1139 "Don't send async message.\n",
1140 (unsigned int)timeout.tv_usec ));
1141 smb2req->async = true;
1143 #endif
1145 /* Re-schedule us to retry on timer expiry. */
1146 end_time = timeval_sum(&request_time, &timeout);
1148 DEBUG(10,("push_deferred_open_message_smb2: "
1149 "timeout at %s\n",
1150 timeval_string(talloc_tos(),
1151 &end_time,
1152 true) ));
1154 state->te = event_add_timed(smb2req->sconn->smb2.event_ctx,
1155 state,
1156 end_time,
1157 smb2_deferred_open_timer,
1158 smb2req);
1159 if (!state->te) {
1160 return false;
1163 /* allow this request to be canceled */
1164 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1166 return true;