s3: Fix Coverity ID 986, BUFFER_SIZE_WARNING
[Samba.git] / source3 / smbd / smb2_create.c
blobdabe638145303737eed03710a5105bd28d4fd696
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"
29 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
31 switch(in_oplock_level) {
32 case SMB2_OPLOCK_LEVEL_NONE:
33 return NO_OPLOCK;
34 case SMB2_OPLOCK_LEVEL_II:
35 return LEVEL_II_OPLOCK;
36 case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
37 return EXCLUSIVE_OPLOCK;
38 case SMB2_OPLOCK_LEVEL_BATCH:
39 return BATCH_OPLOCK;
40 case SMB2_OPLOCK_LEVEL_LEASE:
41 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
42 "LEASE_OPLOCK_REQUESTED\n"));
43 return NO_OPLOCK;
44 default:
45 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
46 "unknown level %u\n",
47 (unsigned int)in_oplock_level));
48 return NO_OPLOCK;
52 static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
54 if (BATCH_OPLOCK_TYPE(oplock_type)) {
55 return SMB2_OPLOCK_LEVEL_BATCH;
56 } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type)) {
57 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
58 } else if (oplock_type == LEVEL_II_OPLOCK) {
60 * Don't use LEVEL_II_OPLOCK_TYPE here as
61 * this also includes FAKE_LEVEL_II_OPLOCKs
62 * which are internal only.
64 return SMB2_OPLOCK_LEVEL_II;
65 } else {
66 return SMB2_OPLOCK_LEVEL_NONE;
70 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
71 struct tevent_context *ev,
72 struct smbd_smb2_request *smb2req,
73 uint8_t in_oplock_level,
74 uint32_t in_impersonation_level,
75 uint32_t in_desired_access,
76 uint32_t in_file_attributes,
77 uint32_t in_share_access,
78 uint32_t in_create_disposition,
79 uint32_t in_create_options,
80 const char *in_name,
81 struct smb2_create_blobs in_context_blobs);
82 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
83 TALLOC_CTX *mem_ctx,
84 uint8_t *out_oplock_level,
85 uint32_t *out_create_action,
86 NTTIME *out_creation_time,
87 NTTIME *out_last_access_time,
88 NTTIME *out_last_write_time,
89 NTTIME *out_change_time,
90 uint64_t *out_allocation_size,
91 uint64_t *out_end_of_file,
92 uint32_t *out_file_attributes,
93 uint64_t *out_file_id_persistent,
94 uint64_t *out_file_id_volatile,
95 struct smb2_create_blobs *out_context_blobs);
97 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
98 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
100 const uint8_t *inbody;
101 int i = smb2req->current_idx;
102 size_t expected_body_size = 0x39;
103 size_t body_size;
104 uint8_t in_oplock_level;
105 uint32_t in_impersonation_level;
106 uint32_t in_desired_access;
107 uint32_t in_file_attributes;
108 uint32_t in_share_access;
109 uint32_t in_create_disposition;
110 uint32_t in_create_options;
111 uint16_t in_name_offset;
112 uint16_t in_name_length;
113 DATA_BLOB in_name_buffer;
114 char *in_name_string;
115 size_t in_name_string_size;
116 uint32_t name_offset = 0;
117 uint32_t name_available_length = 0;
118 uint32_t in_context_offset;
119 uint32_t in_context_length;
120 DATA_BLOB in_context_buffer;
121 struct smb2_create_blobs in_context_blobs;
122 uint32_t context_offset = 0;
123 uint32_t context_available_length = 0;
124 uint32_t dyn_offset;
125 NTSTATUS status;
126 bool ok;
127 struct tevent_req *tsubreq;
129 if (smb2req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
130 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
133 inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
135 body_size = SVAL(inbody, 0x00);
136 if (body_size != expected_body_size) {
137 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
140 in_oplock_level = CVAL(inbody, 0x03);
141 in_impersonation_level = IVAL(inbody, 0x04);
142 in_desired_access = IVAL(inbody, 0x18);
143 in_file_attributes = IVAL(inbody, 0x1C);
144 in_share_access = IVAL(inbody, 0x20);
145 in_create_disposition = IVAL(inbody, 0x24);
146 in_create_options = IVAL(inbody, 0x28);
147 in_name_offset = SVAL(inbody, 0x2C);
148 in_name_length = SVAL(inbody, 0x2E);
149 in_context_offset = IVAL(inbody, 0x30);
150 in_context_length = IVAL(inbody, 0x34);
153 * First check if the dynamic name and context buffers
154 * are correctly specified.
156 * Note: That we don't check if the name and context buffers
157 * overlap
160 dyn_offset = SMB2_HDR_BODY + (body_size & 0xFFFFFFFE);
162 if (in_name_offset == 0 && in_name_length == 0) {
163 /* This is ok */
164 name_offset = 0;
165 } else if (in_name_offset < dyn_offset) {
166 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
167 } else {
168 name_offset = in_name_offset - dyn_offset;
171 if (name_offset > smb2req->in.vector[i+2].iov_len) {
172 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
175 name_available_length = smb2req->in.vector[i+2].iov_len - name_offset;
177 if (in_name_length > name_available_length) {
178 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
181 in_name_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
182 name_offset;
183 in_name_buffer.length = in_name_length;
185 if (in_context_offset == 0 && in_context_length == 0) {
186 /* This is ok */
187 context_offset = 0;
188 } else if (in_context_offset < dyn_offset) {
189 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
190 } else {
191 context_offset = in_context_offset - dyn_offset;
194 if (context_offset > smb2req->in.vector[i+2].iov_len) {
195 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
198 context_available_length = smb2req->in.vector[i+2].iov_len - context_offset;
200 if (in_context_length > context_available_length) {
201 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
204 in_context_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
205 context_offset;
206 in_context_buffer.length = in_context_length;
209 * Now interpret the name and context buffers
212 ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
213 in_name_buffer.data,
214 in_name_buffer.length,
215 &in_name_string,
216 &in_name_string_size, false);
217 if (!ok) {
218 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
221 ZERO_STRUCT(in_context_blobs);
222 status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
223 if (!NT_STATUS_IS_OK(status)) {
224 return smbd_smb2_request_error(smb2req, status);
227 tsubreq = smbd_smb2_create_send(smb2req,
228 smb2req->sconn->smb2.event_ctx,
229 smb2req,
230 in_oplock_level,
231 in_impersonation_level,
232 in_desired_access,
233 in_file_attributes,
234 in_share_access,
235 in_create_disposition,
236 in_create_options,
237 in_name_string,
238 in_context_blobs);
239 if (tsubreq == NULL) {
240 smb2req->subreq = NULL;
241 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
243 tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
245 return smbd_smb2_request_pending_queue(smb2req, tsubreq);
248 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
250 uint8_t *reqhdr = (uint8_t *)smb2req->out.vector[smb2req->current_idx].iov_base;
251 return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
254 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
256 struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
257 struct smbd_smb2_request);
258 int i = smb2req->current_idx;
259 uint8_t *outhdr;
260 DATA_BLOB outbody;
261 DATA_BLOB outdyn;
262 uint8_t out_oplock_level = 0;
263 uint32_t out_create_action = 0;
264 NTTIME out_creation_time = 0;
265 NTTIME out_last_access_time = 0;
266 NTTIME out_last_write_time = 0;
267 NTTIME out_change_time = 0;
268 uint64_t out_allocation_size = 0;
269 uint64_t out_end_of_file = 0;
270 uint32_t out_file_attributes = 0;
271 uint64_t out_file_id_persistent = 0;
272 uint64_t out_file_id_volatile = 0;
273 struct smb2_create_blobs out_context_blobs;
274 DATA_BLOB out_context_buffer;
275 uint16_t out_context_buffer_offset = 0;
276 NTSTATUS status;
277 NTSTATUS error; /* transport error */
279 if (smb2req->cancelled) {
280 uint64_t mid = get_mid_from_smb2req(smb2req);
281 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
282 (unsigned long long)mid ));
283 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
284 if (!NT_STATUS_IS_OK(error)) {
285 smbd_server_connection_terminate(smb2req->sconn,
286 nt_errstr(error));
287 return;
289 return;
292 status = smbd_smb2_create_recv(tsubreq,
293 smb2req,
294 &out_oplock_level,
295 &out_create_action,
296 &out_creation_time,
297 &out_last_access_time,
298 &out_last_write_time,
299 &out_change_time,
300 &out_allocation_size,
301 &out_end_of_file,
302 &out_file_attributes,
303 &out_file_id_persistent,
304 &out_file_id_volatile,
305 &out_context_blobs);
306 if (!NT_STATUS_IS_OK(status)) {
307 error = smbd_smb2_request_error(smb2req, status);
308 if (!NT_STATUS_IS_OK(error)) {
309 smbd_server_connection_terminate(smb2req->sconn,
310 nt_errstr(error));
311 return;
313 return;
316 status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
317 if (!NT_STATUS_IS_OK(status)) {
318 error = smbd_smb2_request_error(smb2req, status);
319 if (!NT_STATUS_IS_OK(error)) {
320 smbd_server_connection_terminate(smb2req->sconn,
321 nt_errstr(error));
322 return;
324 return;
327 if (out_context_buffer.length > 0) {
328 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
331 outhdr = (uint8_t *)smb2req->out.vector[i].iov_base;
333 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
334 if (outbody.data == NULL) {
335 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
336 if (!NT_STATUS_IS_OK(error)) {
337 smbd_server_connection_terminate(smb2req->sconn,
338 nt_errstr(error));
339 return;
341 return;
344 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
345 SCVAL(outbody.data, 0x02,
346 out_oplock_level); /* oplock level */
347 SCVAL(outbody.data, 0x03, 0); /* reserved */
348 SIVAL(outbody.data, 0x04,
349 out_create_action); /* create action */
350 SBVAL(outbody.data, 0x08,
351 out_creation_time); /* creation time */
352 SBVAL(outbody.data, 0x10,
353 out_last_access_time); /* last access time */
354 SBVAL(outbody.data, 0x18,
355 out_last_write_time); /* last write time */
356 SBVAL(outbody.data, 0x20,
357 out_change_time); /* change time */
358 SBVAL(outbody.data, 0x28,
359 out_allocation_size); /* allocation size */
360 SBVAL(outbody.data, 0x30,
361 out_end_of_file); /* end of file */
362 SIVAL(outbody.data, 0x38,
363 out_file_attributes); /* file attributes */
364 SIVAL(outbody.data, 0x3C, 0); /* reserved */
365 SBVAL(outbody.data, 0x40,
366 out_file_id_persistent); /* file id (persistent) */
367 SBVAL(outbody.data, 0x48,
368 out_file_id_volatile); /* file id (volatile) */
369 SIVAL(outbody.data, 0x50,
370 out_context_buffer_offset); /* create contexts offset */
371 SIVAL(outbody.data, 0x54,
372 out_context_buffer.length); /* create contexts length */
374 outdyn = out_context_buffer;
376 error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
377 if (!NT_STATUS_IS_OK(error)) {
378 smbd_server_connection_terminate(smb2req->sconn,
379 nt_errstr(error));
380 return;
384 struct smbd_smb2_create_state {
385 struct smbd_smb2_request *smb2req;
386 struct smb_request *smb1req;
387 struct timed_event *te;
388 struct tevent_immediate *im;
389 struct timeval request_time;
390 struct file_id id;
391 DATA_BLOB private_data;
392 uint8_t out_oplock_level;
393 uint32_t out_create_action;
394 NTTIME out_creation_time;
395 NTTIME out_last_access_time;
396 NTTIME out_last_write_time;
397 NTTIME out_change_time;
398 uint64_t out_allocation_size;
399 uint64_t out_end_of_file;
400 uint32_t out_file_attributes;
401 uint64_t out_file_id_persistent;
402 uint64_t out_file_id_volatile;
403 struct smb2_create_blobs out_context_blobs;
406 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
407 struct tevent_context *ev,
408 struct smbd_smb2_request *smb2req,
409 uint8_t in_oplock_level,
410 uint32_t in_impersonation_level,
411 uint32_t in_desired_access,
412 uint32_t in_file_attributes,
413 uint32_t in_share_access,
414 uint32_t in_create_disposition,
415 uint32_t in_create_options,
416 const char *in_name,
417 struct smb2_create_blobs in_context_blobs)
419 struct tevent_req *req = NULL;
420 struct smbd_smb2_create_state *state = NULL;
421 NTSTATUS status;
422 struct smb_request *smb1req = NULL;
423 files_struct *result = NULL;
424 int info;
425 struct timespec write_time_ts;
426 struct smb2_create_blobs out_context_blobs;
427 int requested_oplock_level;
429 ZERO_STRUCT(out_context_blobs);
431 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat_conn))) {
432 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
433 } else {
434 requested_oplock_level = in_oplock_level;
438 if (!smb2req->async) {
439 /* New create call. */
440 req = tevent_req_create(mem_ctx, &state,
441 struct smbd_smb2_create_state);
442 if (req == NULL) {
443 return NULL;
445 state->smb2req = smb2req;
446 smb2req->subreq = req; /* So we can find this when going async. */
448 smb1req = smbd_smb2_fake_smb_request(smb2req);
449 if (tevent_req_nomem(smb1req, req)) {
450 return tevent_req_post(req, ev);
452 state->smb1req = smb1req;
453 DEBUG(10,("smbd_smb2_create: name[%s]\n",
454 in_name));
455 } else {
456 /* Re-entrant create call. */
457 req = smb2req->subreq;
458 state = tevent_req_data(req,
459 struct smbd_smb2_create_state);
460 smb1req = state->smb1req;
461 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
462 in_name ));
465 if (IS_IPC(smb1req->conn)) {
466 const char *pipe_name = in_name;
468 if (!lp_nt_pipe_support()) {
469 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
470 return tevent_req_post(req, ev);
473 /* Strip \\ off the name. */
474 if (pipe_name[0] == '\\') {
475 pipe_name++;
478 status = open_np_file(smb1req, pipe_name, &result);
479 if (!NT_STATUS_IS_OK(status)) {
480 tevent_req_nterror(req, status);
481 return tevent_req_post(req, ev);
483 info = FILE_WAS_OPENED;
484 } else if (CAN_PRINT(smb1req->conn)) {
485 status = file_new(smb1req, smb1req->conn, &result);
486 if(!NT_STATUS_IS_OK(status)) {
487 tevent_req_nterror(req, status);
488 return tevent_req_post(req, ev);
491 status = print_spool_open(result, in_name,
492 smb1req->vuid);
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, 0, 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(
914 struct smbd_server_connection *sconn, uint64_t mid)
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(struct smbd_server_connection *sconn, uint64_t mid)
938 struct smbd_smb2_create_state *state = NULL;
939 struct smbd_smb2_request *smb2req;
941 smb2req = find_open_smb2req(sconn, mid);
943 if (!smb2req) {
944 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
945 (unsigned long long)mid));
946 return false;
948 if (!smb2req->subreq) {
949 return false;
951 if (!tevent_req_is_in_progress(smb2req->subreq)) {
952 return false;
954 state = tevent_req_data(smb2req->subreq,
955 struct smbd_smb2_create_state);
956 if (!state) {
957 return false;
959 /* It's not in progress if there's no timeout event. */
960 if (!state->te) {
961 return false;
964 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
965 (unsigned long long)mid));
967 return true;
970 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
971 uint64_t mid)
973 struct smbd_smb2_create_state *state = NULL;
975 if (!smb2req->subreq) {
976 return;
978 if (!tevent_req_is_in_progress(smb2req->subreq)) {
979 return;
981 state = tevent_req_data(smb2req->subreq,
982 struct smbd_smb2_create_state);
983 if (!state) {
984 return;
987 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
988 "mid %llu\n",
989 (unsigned long long)mid ));
991 /* Ensure we don't have any outstanding timer event. */
992 TALLOC_FREE(state->te);
993 /* Ensure we don't have any outstanding immediate event. */
994 TALLOC_FREE(state->im);
997 void remove_deferred_open_message_smb2(
998 struct smbd_server_connection *sconn, uint64_t mid)
1000 struct smbd_smb2_request *smb2req;
1002 smb2req = find_open_smb2req(sconn, mid);
1004 if (!smb2req) {
1005 DEBUG(10,("remove_deferred_open_message_smb2: "
1006 "can't find mid %llu\n",
1007 (unsigned long long)mid ));
1008 return;
1010 remove_deferred_open_message_smb2_internal(smb2req, mid);
1013 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1014 struct tevent_immediate *im,
1015 void *private_data)
1017 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1018 struct smbd_smb2_request);
1019 struct smbd_server_connection *sconn = smb2req->sconn;
1020 uint64_t mid = get_mid_from_smb2req(smb2req);
1021 NTSTATUS status;
1023 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1024 "re-dispatching mid %llu\n",
1025 (unsigned long long)mid ));
1027 status = smbd_smb2_request_dispatch(smb2req);
1028 if (!NT_STATUS_IS_OK(status)) {
1029 smbd_server_connection_terminate(sconn, nt_errstr(status));
1030 return;
1034 void schedule_deferred_open_message_smb2(
1035 struct smbd_server_connection *sconn, uint64_t mid)
1037 struct smbd_smb2_create_state *state = NULL;
1038 struct smbd_smb2_request *smb2req;
1040 smb2req = find_open_smb2req(sconn, mid);
1042 if (!smb2req) {
1043 DEBUG(10,("schedule_deferred_open_message_smb2: "
1044 "can't find mid %llu\n",
1045 (unsigned long long)mid ));
1046 return;
1048 if (!smb2req->subreq) {
1049 return;
1051 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1052 return;
1054 state = tevent_req_data(smb2req->subreq,
1055 struct smbd_smb2_create_state);
1056 if (!state) {
1057 return;
1060 /* Ensure we don't have any outstanding timer event. */
1061 TALLOC_FREE(state->te);
1062 /* Ensure we don't have any outstanding immediate event. */
1063 TALLOC_FREE(state->im);
1066 * This is subtle. We must null out the callback
1067 * before resheduling, else the first call to
1068 * tevent_req_nterror() causes the _receive()
1069 * function to be called, this causing tevent_req_post()
1070 * to crash.
1072 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1074 state->im = tevent_create_immediate(smb2req);
1075 if (!state->im) {
1076 smbd_server_connection_terminate(smb2req->sconn,
1077 nt_errstr(NT_STATUS_NO_MEMORY));
1080 DEBUG(10,("schedule_deferred_open_message_smb2: "
1081 "re-processing mid %llu\n",
1082 (unsigned long long)mid ));
1084 tevent_schedule_immediate(state->im,
1085 smb2req->sconn->smb2.event_ctx,
1086 smbd_smb2_create_request_dispatch_immediate,
1087 smb2req);
1090 /*********************************************************
1091 Re-process this call.
1092 *********************************************************/
1094 static void smb2_deferred_open_timer(struct event_context *ev,
1095 struct timed_event *te,
1096 struct timeval _tval,
1097 void *private_data)
1099 NTSTATUS status;
1100 struct smbd_smb2_create_state *state = NULL;
1101 struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
1102 struct smbd_smb2_request);
1104 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1105 smb2req->current_idx,
1106 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
1108 state = tevent_req_data(smb2req->subreq,
1109 struct smbd_smb2_create_state);
1110 if (!state) {
1111 return;
1114 * Null this out, don't talloc_free. It will
1115 * be talloc_free'd by the tevent library when
1116 * this returns.
1118 state->te = NULL;
1119 /* Ensure we don't have any outstanding immediate event. */
1120 TALLOC_FREE(state->im);
1123 * This is subtle. We must null out the callback
1124 * before resheduling, else the first call to
1125 * tevent_req_nterror() causes the _receive()
1126 * function to be called, this causing tevent_req_post()
1127 * to crash.
1129 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1131 status = smbd_smb2_request_dispatch(smb2req);
1133 if (!NT_STATUS_IS_OK(status)) {
1134 smbd_server_connection_terminate(smb2req->sconn,
1135 nt_errstr(status));
1139 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1141 struct smbd_smb2_request *smb2req = NULL;
1142 struct smbd_smb2_create_state *state = tevent_req_data(req,
1143 struct smbd_smb2_create_state);
1144 uint64_t mid;
1146 if (!state) {
1147 return false;
1150 if (!state->smb2req) {
1151 return false;
1154 smb2req = state->smb2req;
1155 mid = get_mid_from_smb2req(smb2req);
1157 remove_deferred_open_entry(state->id, mid,
1158 sconn_server_id(smb2req->sconn));
1159 remove_deferred_open_message_smb2_internal(smb2req, mid);
1160 smb2req->cancelled = true;
1162 tevent_req_done(req);
1163 return true;
1166 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1167 struct timeval request_time,
1168 struct timeval timeout,
1169 struct file_id id,
1170 char *private_data,
1171 size_t priv_len)
1173 struct tevent_req *req = NULL;
1174 struct smbd_smb2_create_state *state = NULL;
1175 struct timeval end_time;
1177 if (!smb2req) {
1178 return false;
1180 req = smb2req->subreq;
1181 if (!req) {
1182 return false;
1184 state = tevent_req_data(req, struct smbd_smb2_create_state);
1185 if (!state) {
1186 return false;
1188 state->id = id;
1189 state->request_time = request_time;
1190 state->private_data = data_blob_talloc(state, private_data,
1191 priv_len);
1192 if (!state->private_data.data) {
1193 return false;
1196 #if 1
1197 /* Boo - turns out this isn't what W2K8R2
1198 does. It actually sends the STATUS_PENDING
1199 message followed by the STATUS_SHARING_VIOLATION
1200 message. Surely this means that all open
1201 calls (even on directories) will potentially
1202 fail in a chain.... ? And I've seen directory
1203 opens as the start of a chain. JRA.
1205 Update: 19th May 2010. Talking with Microsoft
1206 engineers at the plugfest this is a bug in
1207 Windows. Re-enable this code.
1210 * More subtlety. To match W2K8R2 don't
1211 * send a "gone async" message if it's simply
1212 * a STATUS_SHARING_VIOLATION (short) wait, not
1213 * an oplock break wait. We do this by prematurely
1214 * setting smb2req->async flag.
1216 if (timeout.tv_sec < 2) {
1217 DEBUG(10,("push_deferred_open_message_smb2: "
1218 "short timer wait (usec = %u). "
1219 "Don't send async message.\n",
1220 (unsigned int)timeout.tv_usec ));
1221 smb2req->async = true;
1223 #endif
1225 /* Re-schedule us to retry on timer expiry. */
1226 end_time = timeval_sum(&request_time, &timeout);
1228 DEBUG(10,("push_deferred_open_message_smb2: "
1229 "timeout at %s\n",
1230 timeval_string(talloc_tos(),
1231 &end_time,
1232 true) ));
1234 state->te = event_add_timed(smb2req->sconn->smb2.event_ctx,
1235 state,
1236 end_time,
1237 smb2_deferred_open_timer,
1238 smb2req);
1239 if (!state->te) {
1240 return false;
1243 /* allow this request to be canceled */
1244 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1246 return true;