If using fake oplocks, use the correct SMB2 type code for "no oplock".
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_create.c
blob77fd60795aa31d8a892e10f0d4a8260910beedad
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 "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
26 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
28 switch(in_oplock_level) {
29 case SMB2_OPLOCK_LEVEL_NONE:
30 return NO_OPLOCK;
31 case SMB2_OPLOCK_LEVEL_II:
32 return LEVEL_II_OPLOCK;
33 case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
34 return EXCLUSIVE_OPLOCK;
35 case SMB2_OPLOCK_LEVEL_BATCH:
36 return BATCH_OPLOCK;
37 case SMB2_OPLOCK_LEVEL_LEASE:
38 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
39 "LEASE_OPLOCK_REQUESTED\n"));
40 return NO_OPLOCK;
41 default:
42 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
43 "unknown level %u\n",
44 (unsigned int)in_oplock_level));
45 return NO_OPLOCK;
49 static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
51 if (BATCH_OPLOCK_TYPE(oplock_type)) {
52 return SMB2_OPLOCK_LEVEL_BATCH;
53 } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type)) {
54 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
55 } else if (oplock_type == LEVEL_II_OPLOCK) {
57 * Don't use LEVEL_II_OPLOCK_TYPE here as
58 * this also includes FAKE_LEVEL_II_OPLOCKs
59 * which are internal only.
61 return SMB2_OPLOCK_LEVEL_II;
62 } else {
63 return SMB2_OPLOCK_LEVEL_NONE;
67 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
68 struct tevent_context *ev,
69 struct smbd_smb2_request *smb2req,
70 uint8_t in_oplock_level,
71 uint32_t in_impersonation_level,
72 uint32_t in_desired_access,
73 uint32_t in_file_attributes,
74 uint32_t in_share_access,
75 uint32_t in_create_disposition,
76 uint32_t in_create_options,
77 const char *in_name,
78 struct smb2_create_blobs in_context_blobs);
79 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
80 TALLOC_CTX *mem_ctx,
81 uint8_t *out_oplock_level,
82 uint32_t *out_create_action,
83 NTTIME *out_creation_time,
84 NTTIME *out_last_access_time,
85 NTTIME *out_last_write_time,
86 NTTIME *out_change_time,
87 uint64_t *out_allocation_size,
88 uint64_t *out_end_of_file,
89 uint32_t *out_file_attributes,
90 uint64_t *out_file_id_persistent,
91 uint64_t *out_file_id_volatile,
92 struct smb2_create_blobs *out_context_blobs);
94 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
95 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
97 const uint8_t *inbody;
98 int i = smb2req->current_idx;
99 size_t expected_body_size = 0x39;
100 size_t body_size;
101 uint8_t in_oplock_level;
102 uint32_t in_impersonation_level;
103 uint32_t in_desired_access;
104 uint32_t in_file_attributes;
105 uint32_t in_share_access;
106 uint32_t in_create_disposition;
107 uint32_t in_create_options;
108 uint16_t in_name_offset;
109 uint16_t in_name_length;
110 DATA_BLOB in_name_buffer;
111 char *in_name_string;
112 size_t in_name_string_size;
113 uint32_t name_offset = 0;
114 uint32_t name_available_length = 0;
115 uint32_t in_context_offset;
116 uint32_t in_context_length;
117 DATA_BLOB in_context_buffer;
118 struct smb2_create_blobs in_context_blobs;
119 uint32_t context_offset = 0;
120 uint32_t context_available_length = 0;
121 uint32_t dyn_offset;
122 NTSTATUS status;
123 bool ok;
124 struct tevent_req *tsubreq;
126 if (smb2req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
127 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
130 inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
132 body_size = SVAL(inbody, 0x00);
133 if (body_size != expected_body_size) {
134 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
137 in_oplock_level = CVAL(inbody, 0x03);
138 in_impersonation_level = IVAL(inbody, 0x04);
139 in_desired_access = IVAL(inbody, 0x18);
140 in_file_attributes = IVAL(inbody, 0x1C);
141 in_share_access = IVAL(inbody, 0x20);
142 in_create_disposition = IVAL(inbody, 0x24);
143 in_create_options = IVAL(inbody, 0x28);
144 in_name_offset = SVAL(inbody, 0x2C);
145 in_name_length = SVAL(inbody, 0x2E);
146 in_context_offset = IVAL(inbody, 0x30);
147 in_context_length = IVAL(inbody, 0x34);
150 * First check if the dynamic name and context buffers
151 * are correctly specified.
153 * Note: That we don't check if the name and context buffers
154 * overlap
157 dyn_offset = SMB2_HDR_BODY + (body_size & 0xFFFFFFFE);
159 if (in_name_offset == 0 && in_name_length == 0) {
160 /* This is ok */
161 name_offset = 0;
162 } else if (in_name_offset < dyn_offset) {
163 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
164 } else {
165 name_offset = in_name_offset - dyn_offset;
168 if (name_offset > smb2req->in.vector[i+2].iov_len) {
169 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
172 name_available_length = smb2req->in.vector[i+2].iov_len - name_offset;
174 if (in_name_length > name_available_length) {
175 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
178 in_name_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
179 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 > smb2req->in.vector[i+2].iov_len) {
192 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
195 context_available_length = smb2req->in.vector[i+2].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 *)smb2req->in.vector[i+2].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, false);
214 if (!ok) {
215 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
218 ZERO_STRUCT(in_context_blobs);
219 status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
220 if (!NT_STATUS_IS_OK(status)) {
221 return smbd_smb2_request_error(smb2req, status);
224 tsubreq = smbd_smb2_create_send(smb2req,
225 smb2req->sconn->smb2.event_ctx,
226 smb2req,
227 in_oplock_level,
228 in_impersonation_level,
229 in_desired_access,
230 in_file_attributes,
231 in_share_access,
232 in_create_disposition,
233 in_create_options,
234 in_name_string,
235 in_context_blobs);
236 if (tsubreq == NULL) {
237 smb2req->subreq = NULL;
238 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
240 tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
242 return smbd_smb2_request_pending_queue(smb2req, tsubreq);
245 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
247 uint8_t *reqhdr = (uint8_t *)smb2req->out.vector[smb2req->current_idx].iov_base;
248 return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
251 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
253 struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
254 struct smbd_smb2_request);
255 int i = smb2req->current_idx;
256 uint8_t *outhdr;
257 DATA_BLOB outbody;
258 DATA_BLOB outdyn;
259 uint8_t out_oplock_level = 0;
260 uint32_t out_create_action = 0;
261 NTTIME out_creation_time = 0;
262 NTTIME out_last_access_time = 0;
263 NTTIME out_last_write_time = 0;
264 NTTIME out_change_time = 0;
265 uint64_t out_allocation_size = 0;
266 uint64_t out_end_of_file = 0;
267 uint32_t out_file_attributes = 0;
268 uint64_t out_file_id_persistent = 0;
269 uint64_t out_file_id_volatile = 0;
270 struct smb2_create_blobs out_context_blobs;
271 DATA_BLOB out_context_buffer;
272 uint16_t out_context_buffer_offset = 0;
273 NTSTATUS status;
274 NTSTATUS error; /* transport error */
276 if (smb2req->cancelled) {
277 uint64_t mid = get_mid_from_smb2req(smb2req);
278 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
279 (unsigned long long)mid ));
280 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
281 if (!NT_STATUS_IS_OK(error)) {
282 smbd_server_connection_terminate(smb2req->sconn,
283 nt_errstr(error));
284 return;
286 return;
289 status = smbd_smb2_create_recv(tsubreq,
290 smb2req,
291 &out_oplock_level,
292 &out_create_action,
293 &out_creation_time,
294 &out_last_access_time,
295 &out_last_write_time,
296 &out_change_time,
297 &out_allocation_size,
298 &out_end_of_file,
299 &out_file_attributes,
300 &out_file_id_persistent,
301 &out_file_id_volatile,
302 &out_context_blobs);
303 if (!NT_STATUS_IS_OK(status)) {
304 error = smbd_smb2_request_error(smb2req, status);
305 if (!NT_STATUS_IS_OK(error)) {
306 smbd_server_connection_terminate(smb2req->sconn,
307 nt_errstr(error));
308 return;
310 return;
313 status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
314 if (!NT_STATUS_IS_OK(status)) {
315 error = smbd_smb2_request_error(smb2req, status);
316 if (!NT_STATUS_IS_OK(error)) {
317 smbd_server_connection_terminate(smb2req->sconn,
318 nt_errstr(error));
319 return;
321 return;
324 if (out_context_buffer.length > 0) {
325 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
328 outhdr = (uint8_t *)smb2req->out.vector[i].iov_base;
330 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
331 if (outbody.data == NULL) {
332 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
333 if (!NT_STATUS_IS_OK(error)) {
334 smbd_server_connection_terminate(smb2req->sconn,
335 nt_errstr(error));
336 return;
338 return;
341 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
342 SCVAL(outbody.data, 0x02,
343 out_oplock_level); /* oplock level */
344 SCVAL(outbody.data, 0x03, 0); /* reserved */
345 SIVAL(outbody.data, 0x04,
346 out_create_action); /* create action */
347 SBVAL(outbody.data, 0x08,
348 out_creation_time); /* creation time */
349 SBVAL(outbody.data, 0x10,
350 out_last_access_time); /* last access time */
351 SBVAL(outbody.data, 0x18,
352 out_last_write_time); /* last write time */
353 SBVAL(outbody.data, 0x20,
354 out_change_time); /* change time */
355 SBVAL(outbody.data, 0x28,
356 out_allocation_size); /* allocation size */
357 SBVAL(outbody.data, 0x30,
358 out_end_of_file); /* end of file */
359 SIVAL(outbody.data, 0x38,
360 out_file_attributes); /* file attributes */
361 SIVAL(outbody.data, 0x3C, 0); /* reserved */
362 SBVAL(outbody.data, 0x40,
363 out_file_id_persistent); /* file id (persistent) */
364 SBVAL(outbody.data, 0x48,
365 out_file_id_volatile); /* file id (volatile) */
366 SIVAL(outbody.data, 0x50,
367 out_context_buffer_offset); /* create contexts offset */
368 SIVAL(outbody.data, 0x54,
369 out_context_buffer.length); /* create contexts length */
371 outdyn = out_context_buffer;
373 error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
374 if (!NT_STATUS_IS_OK(error)) {
375 smbd_server_connection_terminate(smb2req->sconn,
376 nt_errstr(error));
377 return;
381 struct smbd_smb2_create_state {
382 struct smbd_smb2_request *smb2req;
383 struct smb_request *smb1req;
384 struct timed_event *te;
385 struct tevent_immediate *im;
386 struct timeval request_time;
387 struct file_id id;
388 DATA_BLOB private_data;
389 uint8_t out_oplock_level;
390 uint32_t out_create_action;
391 NTTIME out_creation_time;
392 NTTIME out_last_access_time;
393 NTTIME out_last_write_time;
394 NTTIME out_change_time;
395 uint64_t out_allocation_size;
396 uint64_t out_end_of_file;
397 uint32_t out_file_attributes;
398 uint64_t out_file_id_persistent;
399 uint64_t out_file_id_volatile;
400 struct smb2_create_blobs out_context_blobs;
403 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
404 struct tevent_context *ev,
405 struct smbd_smb2_request *smb2req,
406 uint8_t in_oplock_level,
407 uint32_t in_impersonation_level,
408 uint32_t in_desired_access,
409 uint32_t in_file_attributes,
410 uint32_t in_share_access,
411 uint32_t in_create_disposition,
412 uint32_t in_create_options,
413 const char *in_name,
414 struct smb2_create_blobs in_context_blobs)
416 struct tevent_req *req = NULL;
417 struct smbd_smb2_create_state *state = NULL;
418 NTSTATUS status;
419 struct smb_request *smb1req = NULL;
420 files_struct *result = NULL;
421 int info;
422 struct timespec write_time_ts;
423 struct smb2_create_blobs out_context_blobs;
424 int requested_oplock_level;
426 ZERO_STRUCT(out_context_blobs);
428 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat_conn))) {
429 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
430 } else {
431 requested_oplock_level = in_oplock_level;
435 if (!smb2req->async) {
436 /* New create call. */
437 req = tevent_req_create(mem_ctx, &state,
438 struct smbd_smb2_create_state);
439 if (req == NULL) {
440 return NULL;
442 state->smb2req = smb2req;
443 smb2req->subreq = req; /* So we can find this when going async. */
445 smb1req = smbd_smb2_fake_smb_request(smb2req);
446 if (tevent_req_nomem(smb1req, req)) {
447 return tevent_req_post(req, ev);
449 state->smb1req = smb1req;
450 DEBUG(10,("smbd_smb2_create: name[%s]\n",
451 in_name));
452 } else {
453 /* Re-entrant create call. */
454 req = smb2req->subreq;
455 state = tevent_req_data(req,
456 struct smbd_smb2_create_state);
457 smb1req = state->smb1req;
458 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
459 in_name ));
462 if (IS_IPC(smb1req->conn)) {
463 const char *pipe_name = in_name;
465 if (!lp_nt_pipe_support()) {
466 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
467 return tevent_req_post(req, ev);
470 /* Strip \\ off the name. */
471 if (pipe_name[0] == '\\') {
472 pipe_name++;
475 status = open_np_file(smb1req, pipe_name, &result);
476 if (!NT_STATUS_IS_OK(status)) {
477 tevent_req_nterror(req, status);
478 return tevent_req_post(req, ev);
480 info = FILE_WAS_OPENED;
481 } else if (CAN_PRINT(smb1req->conn)) {
482 status = file_new(smb1req, smb1req->conn, &result);
483 if(!NT_STATUS_IS_OK(status)) {
484 tevent_req_nterror(req, status);
485 return tevent_req_post(req, ev);
488 status = print_fsp_open(smb1req,
489 smb1req->conn,
490 in_name,
491 smb1req->vuid,
492 result);
493 if (!NT_STATUS_IS_OK(status)) {
494 file_free(smb1req, result);
495 tevent_req_nterror(req, status);
496 return tevent_req_post(req, ev);
498 info = FILE_WAS_CREATED;
499 } else {
500 char *fname;
501 struct smb_filename *smb_fname = NULL;
502 struct smb2_create_blob *exta = NULL;
503 struct ea_list *ea_list = NULL;
504 struct smb2_create_blob *mxac = NULL;
505 NTTIME max_access_time = 0;
506 struct smb2_create_blob *secd = NULL;
507 struct security_descriptor *sec_desc = NULL;
508 struct smb2_create_blob *dhnq = NULL;
509 struct smb2_create_blob *dhnc = NULL;
510 struct smb2_create_blob *alsi = NULL;
511 uint64_t allocation_size = 0;
512 struct smb2_create_blob *twrp = NULL;
513 struct smb2_create_blob *qfid = NULL;
515 exta = smb2_create_blob_find(&in_context_blobs,
516 SMB2_CREATE_TAG_EXTA);
517 mxac = smb2_create_blob_find(&in_context_blobs,
518 SMB2_CREATE_TAG_MXAC);
519 secd = smb2_create_blob_find(&in_context_blobs,
520 SMB2_CREATE_TAG_SECD);
521 dhnq = smb2_create_blob_find(&in_context_blobs,
522 SMB2_CREATE_TAG_DHNQ);
523 dhnc = smb2_create_blob_find(&in_context_blobs,
524 SMB2_CREATE_TAG_DHNC);
525 alsi = smb2_create_blob_find(&in_context_blobs,
526 SMB2_CREATE_TAG_ALSI);
527 twrp = smb2_create_blob_find(&in_context_blobs,
528 SMB2_CREATE_TAG_TWRP);
529 qfid = smb2_create_blob_find(&in_context_blobs,
530 SMB2_CREATE_TAG_QFID);
532 fname = talloc_strdup(state, in_name);
533 if (tevent_req_nomem(fname, req)) {
534 return tevent_req_post(req, ev);
537 if (exta) {
538 if (dhnc) {
539 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
540 return tevent_req_post(req, ev);
543 ea_list = read_nttrans_ea_list(mem_ctx,
544 (const char *)exta->data.data, exta->data.length);
545 if (!ea_list) {
546 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
547 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
548 return tevent_req_post(req, ev);
552 if (mxac) {
553 if (dhnc) {
554 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
555 return tevent_req_post(req, ev);
558 if (mxac->data.length == 0) {
559 max_access_time = 0;
560 } else if (mxac->data.length == 8) {
561 max_access_time = BVAL(mxac->data.data, 0);
562 } else {
563 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
564 return tevent_req_post(req, ev);
568 if (secd) {
569 enum ndr_err_code ndr_err;
571 if (dhnc) {
572 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
573 return tevent_req_post(req, ev);
576 sec_desc = talloc_zero(state, struct security_descriptor);
577 if (tevent_req_nomem(sec_desc, req)) {
578 return tevent_req_post(req, ev);
581 ndr_err = ndr_pull_struct_blob(&secd->data,
582 sec_desc, sec_desc,
583 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
584 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
585 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
586 ndr_errstr(ndr_err)));
587 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
588 return tevent_req_post(req, ev);
592 if (dhnq) {
593 if (dhnc) {
594 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
595 return tevent_req_post(req, ev);
598 if (dhnq->data.length != 16) {
599 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
600 return tevent_req_post(req, ev);
603 * we don't support durable handles yet
604 * and have to ignore this
608 if (dhnc) {
609 if (dhnc->data.length != 16) {
610 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
611 return tevent_req_post(req, ev);
613 /* we don't support durable handles yet */
614 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
615 return tevent_req_post(req, ev);
618 if (alsi) {
619 if (dhnc) {
620 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
621 return tevent_req_post(req, ev);
624 if (alsi->data.length != 8) {
625 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
626 return tevent_req_post(req, ev);
628 allocation_size = BVAL(alsi->data.data, 0);
631 if (twrp) {
632 NTTIME nttime;
633 time_t t;
634 struct tm *tm;
636 if (dhnc) {
637 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
638 return tevent_req_post(req, ev);
641 if (twrp->data.length != 8) {
642 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
643 return tevent_req_post(req, ev);
646 nttime = BVAL(twrp->data.data, 0);
647 t = nt_time_to_unix(nttime);
648 tm = gmtime(&t);
650 TALLOC_FREE(fname);
651 fname = talloc_asprintf(state,
652 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
653 tm->tm_year + 1900,
654 tm->tm_mon + 1,
655 tm->tm_mday,
656 tm->tm_hour,
657 tm->tm_min,
658 tm->tm_sec,
659 in_name);
660 if (tevent_req_nomem(fname, req)) {
661 return tevent_req_post(req, ev);
665 if (qfid) {
666 if (qfid->data.length != 0) {
667 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
668 return tevent_req_post(req, ev);
672 /* these are ignored for SMB2 */
673 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
674 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
677 * For a DFS path the function parse_dfs_path()
678 * will do the path processing.
681 if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
682 /* convert '\\' into '/' */
683 status = check_path_syntax(fname);
684 if (!NT_STATUS_IS_OK(status)) {
685 tevent_req_nterror(req, status);
686 return tevent_req_post(req, ev);
690 status = filename_convert(req,
691 smb1req->conn,
692 smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
693 fname,
695 NULL,
696 &smb_fname);
697 if (!NT_STATUS_IS_OK(status)) {
698 tevent_req_nterror(req, status);
699 return tevent_req_post(req, ev);
702 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
704 status = SMB_VFS_CREATE_FILE(smb1req->conn,
705 smb1req,
706 0, /* root_dir_fid */
707 smb_fname,
708 in_desired_access,
709 in_share_access,
710 in_create_disposition,
711 in_create_options,
712 in_file_attributes,
713 map_smb2_oplock_levels_to_samba(requested_oplock_level),
714 allocation_size,
715 0, /* private_flags */
716 sec_desc,
717 ea_list,
718 &result,
719 &info);
720 if (!NT_STATUS_IS_OK(status)) {
721 if (open_was_deferred(smb1req->mid)) {
722 return req;
724 tevent_req_nterror(req, status);
725 return tevent_req_post(req, ev);
728 if (mxac) {
729 NTTIME last_write_time;
731 unix_timespec_to_nt_time(&last_write_time,
732 result->fsp_name->st.st_ex_mtime);
733 if (last_write_time != max_access_time) {
734 uint8_t p[8];
735 uint32_t max_access_granted;
736 DATA_BLOB blob = data_blob_const(p, sizeof(p));
738 status = smbd_check_open_rights(smb1req->conn,
739 result->fsp_name,
740 SEC_FLAG_MAXIMUM_ALLOWED,
741 &max_access_granted);
743 SIVAL(p, 0, NT_STATUS_V(status));
744 SIVAL(p, 4, max_access_granted);
746 status = smb2_create_blob_add(state,
747 &out_context_blobs,
748 SMB2_CREATE_TAG_MXAC,
749 blob);
750 if (!NT_STATUS_IS_OK(status)) {
751 tevent_req_nterror(req, status);
752 return tevent_req_post(req, ev);
757 if (qfid) {
758 uint8_t p[32];
759 uint64_t file_index = get_FileIndex(result->conn,
760 &result->fsp_name->st);
761 DATA_BLOB blob = data_blob_const(p, sizeof(p));
763 ZERO_STRUCT(p);
765 /* From conversations with Microsoft engineers at
766 the MS plugfest. The first 8 bytes are the "volume index"
767 == inode, the second 8 bytes are the "volume id",
768 == dev. This will be updated in the SMB2 doc. */
769 SBVAL(p, 0, file_index);
770 SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
772 status = smb2_create_blob_add(state, &out_context_blobs,
773 SMB2_CREATE_TAG_QFID,
774 blob);
775 if (!NT_STATUS_IS_OK(status)) {
776 tevent_req_nterror(req, status);
777 return tevent_req_post(req, ev);
782 smb2req->compat_chain_fsp = smb1req->chain_fsp;
784 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat_conn))) {
785 state->out_oplock_level = in_oplock_level;
786 } else {
787 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
790 if ((in_create_disposition == FILE_SUPERSEDE)
791 && (info == FILE_WAS_OVERWRITTEN)) {
792 state->out_create_action = FILE_WAS_SUPERSEDED;
793 } else {
794 state->out_create_action = info;
796 state->out_file_attributes = dos_mode(result->conn,
797 result->fsp_name);
798 /* Deal with other possible opens having a modified
799 write time. JRA. */
800 ZERO_STRUCT(write_time_ts);
801 get_file_infos(result->file_id, NULL, &write_time_ts);
802 if (!null_timespec(write_time_ts)) {
803 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
806 unix_timespec_to_nt_time(&state->out_creation_time,
807 get_create_timespec(smb1req->conn, result,
808 result->fsp_name));
809 unix_timespec_to_nt_time(&state->out_last_access_time,
810 result->fsp_name->st.st_ex_atime);
811 unix_timespec_to_nt_time(&state->out_last_write_time,
812 result->fsp_name->st.st_ex_mtime);
813 unix_timespec_to_nt_time(&state->out_change_time,
814 get_change_timespec(smb1req->conn, result,
815 result->fsp_name));
816 state->out_allocation_size =
817 result->fsp_name->st.st_ex_blksize *
818 result->fsp_name->st.st_ex_blocks;
819 state->out_end_of_file = result->fsp_name->st.st_ex_size;
820 if (state->out_file_attributes == 0) {
821 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
823 state->out_file_id_persistent = result->fnum;
824 state->out_file_id_volatile = result->fnum;
825 state->out_context_blobs = out_context_blobs;
827 tevent_req_done(req);
828 return tevent_req_post(req, ev);
831 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
832 TALLOC_CTX *mem_ctx,
833 uint8_t *out_oplock_level,
834 uint32_t *out_create_action,
835 NTTIME *out_creation_time,
836 NTTIME *out_last_access_time,
837 NTTIME *out_last_write_time,
838 NTTIME *out_change_time,
839 uint64_t *out_allocation_size,
840 uint64_t *out_end_of_file,
841 uint32_t *out_file_attributes,
842 uint64_t *out_file_id_persistent,
843 uint64_t *out_file_id_volatile,
844 struct smb2_create_blobs *out_context_blobs)
846 NTSTATUS status;
847 struct smbd_smb2_create_state *state = tevent_req_data(req,
848 struct smbd_smb2_create_state);
850 if (tevent_req_is_nterror(req, &status)) {
851 tevent_req_received(req);
852 return status;
855 *out_oplock_level = state->out_oplock_level;
856 *out_create_action = state->out_create_action;
857 *out_creation_time = state->out_creation_time;
858 *out_last_access_time = state->out_last_access_time;
859 *out_last_write_time = state->out_last_write_time;
860 *out_change_time = state->out_change_time;
861 *out_allocation_size = state->out_allocation_size;
862 *out_end_of_file = state->out_end_of_file;
863 *out_file_attributes = state->out_file_attributes;
864 *out_file_id_persistent = state->out_file_id_persistent;
865 *out_file_id_volatile = state->out_file_id_volatile;
866 *out_context_blobs = state->out_context_blobs;
868 talloc_steal(mem_ctx, state->out_context_blobs.blobs);
870 tevent_req_received(req);
871 return NT_STATUS_OK;
874 /*********************************************************
875 Code for dealing with deferred opens.
876 *********************************************************/
878 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
879 struct timeval *p_request_time,
880 void **pp_state)
882 struct smbd_smb2_create_state *state = NULL;
883 struct tevent_req *req = NULL;
885 if (!smb2req) {
886 return false;
888 if (!smb2req->async) {
889 return false;
891 req = smb2req->subreq;
892 if (!req) {
893 return false;
895 state = tevent_req_data(req, struct smbd_smb2_create_state);
896 if (!state) {
897 return false;
899 if (p_request_time) {
900 *p_request_time = state->request_time;
902 if (pp_state) {
903 *pp_state = (void *)state->private_data.data;
905 return true;
908 /*********************************************************
909 Re-process this call early - requested by message or
910 close.
911 *********************************************************/
913 static struct smbd_smb2_request *find_open_smb2req(uint64_t mid)
915 struct smbd_server_connection *sconn = smbd_server_conn;
916 struct smbd_smb2_request *smb2req;
918 for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
919 uint64_t message_id;
920 if (smb2req->subreq == NULL) {
921 /* This message has been processed. */
922 continue;
924 if (!tevent_req_is_in_progress(smb2req->subreq)) {
925 /* This message has been processed. */
926 continue;
928 message_id = get_mid_from_smb2req(smb2req);
929 if (message_id == mid) {
930 return smb2req;
933 return NULL;
936 bool open_was_deferred_smb2(uint64_t mid)
938 struct smbd_smb2_create_state *state = NULL;
939 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
941 if (!smb2req) {
942 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
943 (unsigned long long)mid));
944 return false;
946 if (!smb2req->subreq) {
947 return false;
949 if (!tevent_req_is_in_progress(smb2req->subreq)) {
950 return false;
952 state = tevent_req_data(smb2req->subreq,
953 struct smbd_smb2_create_state);
954 if (!state) {
955 return false;
957 /* It's not in progress if there's no timeout event. */
958 if (!state->te) {
959 return false;
962 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
963 (unsigned long long)mid));
965 return true;
968 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
969 uint64_t mid)
971 struct smbd_smb2_create_state *state = NULL;
973 if (!smb2req->subreq) {
974 return;
976 if (!tevent_req_is_in_progress(smb2req->subreq)) {
977 return;
979 state = tevent_req_data(smb2req->subreq,
980 struct smbd_smb2_create_state);
981 if (!state) {
982 return;
985 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
986 "mid %llu\n",
987 (unsigned long long)mid ));
989 /* Ensure we don't have any outstanding timer event. */
990 TALLOC_FREE(state->te);
991 /* Ensure we don't have any outstanding immediate event. */
992 TALLOC_FREE(state->im);
995 void remove_deferred_open_message_smb2(uint64_t mid)
997 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
999 if (!smb2req) {
1000 DEBUG(10,("remove_deferred_open_message_smb2: "
1001 "can't find mid %llu\n",
1002 (unsigned long long)mid ));
1003 return;
1005 remove_deferred_open_message_smb2_internal(smb2req, mid);
1008 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1009 struct tevent_immediate *im,
1010 void *private_data)
1012 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1013 struct smbd_smb2_request);
1014 struct smbd_server_connection *sconn = smb2req->sconn;
1015 uint64_t mid = get_mid_from_smb2req(smb2req);
1016 NTSTATUS status;
1018 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1019 "re-dispatching mid %llu\n",
1020 (unsigned long long)mid ));
1022 status = smbd_smb2_request_dispatch(smb2req);
1023 if (!NT_STATUS_IS_OK(status)) {
1024 smbd_server_connection_terminate(sconn, nt_errstr(status));
1025 return;
1029 void schedule_deferred_open_message_smb2(uint64_t mid)
1031 struct smbd_smb2_create_state *state = NULL;
1032 struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
1034 if (!smb2req) {
1035 DEBUG(10,("schedule_deferred_open_message_smb2: "
1036 "can't find mid %llu\n",
1037 (unsigned long long)mid ));
1038 return;
1040 if (!smb2req->subreq) {
1041 return;
1043 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1044 return;
1046 state = tevent_req_data(smb2req->subreq,
1047 struct smbd_smb2_create_state);
1048 if (!state) {
1049 return;
1052 /* Ensure we don't have any outstanding timer event. */
1053 TALLOC_FREE(state->te);
1054 /* Ensure we don't have any outstanding immediate event. */
1055 TALLOC_FREE(state->im);
1058 * This is subtle. We must null out the callback
1059 * before resheduling, else the first call to
1060 * tevent_req_nterror() causes the _receive()
1061 * function to be called, this causing tevent_req_post()
1062 * to crash.
1064 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1066 state->im = tevent_create_immediate(smb2req);
1067 if (!state->im) {
1068 smbd_server_connection_terminate(smb2req->sconn,
1069 nt_errstr(NT_STATUS_NO_MEMORY));
1072 DEBUG(10,("schedule_deferred_open_message_smb2: "
1073 "re-processing mid %llu\n",
1074 (unsigned long long)mid ));
1076 tevent_schedule_immediate(state->im,
1077 smb2req->sconn->smb2.event_ctx,
1078 smbd_smb2_create_request_dispatch_immediate,
1079 smb2req);
1082 /*********************************************************
1083 Re-process this call.
1084 *********************************************************/
1086 static void smb2_deferred_open_timer(struct event_context *ev,
1087 struct timed_event *te,
1088 struct timeval _tval,
1089 void *private_data)
1091 NTSTATUS status;
1092 struct smbd_smb2_create_state *state = NULL;
1093 struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
1094 struct smbd_smb2_request);
1096 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1097 smb2req->current_idx,
1098 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
1100 state = tevent_req_data(smb2req->subreq,
1101 struct smbd_smb2_create_state);
1102 if (!state) {
1103 return;
1106 * Null this out, don't talloc_free. It will
1107 * be talloc_free'd by the tevent library when
1108 * this returns.
1110 state->te = NULL;
1111 /* Ensure we don't have any outstanding immediate event. */
1112 TALLOC_FREE(state->im);
1115 * This is subtle. We must null out the callback
1116 * before resheduling, else the first call to
1117 * tevent_req_nterror() causes the _receive()
1118 * function to be called, this causing tevent_req_post()
1119 * to crash.
1121 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1123 status = smbd_smb2_request_dispatch(smb2req);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 smbd_server_connection_terminate(smb2req->sconn,
1127 nt_errstr(status));
1131 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1133 struct smbd_smb2_request *smb2req = NULL;
1134 struct smbd_smb2_create_state *state = tevent_req_data(req,
1135 struct smbd_smb2_create_state);
1136 uint64_t mid;
1138 if (!state) {
1139 return false;
1142 if (!state->smb2req) {
1143 return false;
1146 smb2req = state->smb2req;
1147 mid = get_mid_from_smb2req(smb2req);
1149 remove_deferred_open_entry(state->id, mid);
1150 remove_deferred_open_message_smb2_internal(smb2req, mid);
1151 smb2req->cancelled = true;
1153 tevent_req_done(req);
1154 return true;
1157 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1158 struct timeval request_time,
1159 struct timeval timeout,
1160 struct file_id id,
1161 char *private_data,
1162 size_t priv_len)
1164 struct tevent_req *req = NULL;
1165 struct smbd_smb2_create_state *state = NULL;
1166 struct timeval end_time;
1168 if (!smb2req) {
1169 return false;
1171 req = smb2req->subreq;
1172 if (!req) {
1173 return false;
1175 state = tevent_req_data(req, struct smbd_smb2_create_state);
1176 if (!state) {
1177 return false;
1179 state->id = id;
1180 state->request_time = request_time;
1181 state->private_data = data_blob_talloc(state, private_data,
1182 priv_len);
1183 if (!state->private_data.data) {
1184 return false;
1187 #if 1
1188 /* Boo - turns out this isn't what W2K8R2
1189 does. It actually sends the STATUS_PENDING
1190 message followed by the STATUS_SHARING_VIOLATION
1191 message. Surely this means that all open
1192 calls (even on directories) will potentially
1193 fail in a chain.... ? And I've seen directory
1194 opens as the start of a chain. JRA.
1196 Update: 19th May 2010. Talking with Microsoft
1197 engineers at the plugfest this is a bug in
1198 Windows. Re-enable this code.
1201 * More subtlety. To match W2K8R2 don't
1202 * send a "gone async" message if it's simply
1203 * a STATUS_SHARING_VIOLATION (short) wait, not
1204 * an oplock break wait. We do this by prematurely
1205 * setting smb2req->async flag.
1207 if (timeout.tv_sec < 2) {
1208 DEBUG(10,("push_deferred_open_message_smb2: "
1209 "short timer wait (usec = %u). "
1210 "Don't send async message.\n",
1211 (unsigned int)timeout.tv_usec ));
1212 smb2req->async = true;
1214 #endif
1216 /* Re-schedule us to retry on timer expiry. */
1217 end_time = timeval_sum(&request_time, &timeout);
1219 DEBUG(10,("push_deferred_open_message_smb2: "
1220 "timeout at %s\n",
1221 timeval_string(talloc_tos(),
1222 &end_time,
1223 true) ));
1225 state->te = event_add_timed(smb2req->sconn->smb2.event_ctx,
1226 state,
1227 end_time,
1228 smb2_deferred_open_timer,
1229 smb2req);
1230 if (!state->te) {
1231 return false;
1234 /* allow this request to be canceled */
1235 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1237 return true;