s3:libsmb/cliconnect: make use of ntlmssp_is_anonymous()
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_create.c
blob3e5b81d5c8d8891799d00c623cf3d63711d22212
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "printing.h"
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "../libcli/smb/smb_common.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "../lib/util/tevent_ntstatus.h"
29 #include "messages.h"
31 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
33 switch(in_oplock_level) {
34 case SMB2_OPLOCK_LEVEL_NONE:
35 return NO_OPLOCK;
36 case SMB2_OPLOCK_LEVEL_II:
37 return LEVEL_II_OPLOCK;
38 case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
39 return EXCLUSIVE_OPLOCK;
40 case SMB2_OPLOCK_LEVEL_BATCH:
41 return BATCH_OPLOCK;
42 case SMB2_OPLOCK_LEVEL_LEASE:
43 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
44 "LEASE_OPLOCK_REQUESTED\n"));
45 return NO_OPLOCK;
46 default:
47 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
48 "unknown level %u\n",
49 (unsigned int)in_oplock_level));
50 return NO_OPLOCK;
54 static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
56 if (BATCH_OPLOCK_TYPE(oplock_type)) {
57 return SMB2_OPLOCK_LEVEL_BATCH;
58 } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type)) {
59 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
60 } else if (oplock_type == LEVEL_II_OPLOCK) {
62 * Don't use LEVEL_II_OPLOCK_TYPE here as
63 * this also includes FAKE_LEVEL_II_OPLOCKs
64 * which are internal only.
66 return SMB2_OPLOCK_LEVEL_II;
67 } else {
68 return SMB2_OPLOCK_LEVEL_NONE;
72 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
73 struct tevent_context *ev,
74 struct smbd_smb2_request *smb2req,
75 uint8_t in_oplock_level,
76 uint32_t in_impersonation_level,
77 uint32_t in_desired_access,
78 uint32_t in_file_attributes,
79 uint32_t in_share_access,
80 uint32_t in_create_disposition,
81 uint32_t in_create_options,
82 const char *in_name,
83 struct smb2_create_blobs in_context_blobs);
84 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
85 TALLOC_CTX *mem_ctx,
86 uint8_t *out_oplock_level,
87 uint32_t *out_create_action,
88 NTTIME *out_creation_time,
89 NTTIME *out_last_access_time,
90 NTTIME *out_last_write_time,
91 NTTIME *out_change_time,
92 uint64_t *out_allocation_size,
93 uint64_t *out_end_of_file,
94 uint32_t *out_file_attributes,
95 uint64_t *out_file_id_persistent,
96 uint64_t *out_file_id_volatile,
97 struct smb2_create_blobs *out_context_blobs);
99 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
100 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
102 const uint8_t *inbody;
103 int i = smb2req->current_idx;
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 status = smbd_smb2_request_verify_sizes(smb2req, 0x39);
130 if (!NT_STATUS_IS_OK(status)) {
131 return smbd_smb2_request_error(smb2req, status);
133 inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
135 in_oplock_level = CVAL(inbody, 0x03);
136 in_impersonation_level = IVAL(inbody, 0x04);
137 in_desired_access = IVAL(inbody, 0x18);
138 in_file_attributes = IVAL(inbody, 0x1C);
139 in_share_access = IVAL(inbody, 0x20);
140 in_create_disposition = IVAL(inbody, 0x24);
141 in_create_options = IVAL(inbody, 0x28);
142 in_name_offset = SVAL(inbody, 0x2C);
143 in_name_length = SVAL(inbody, 0x2E);
144 in_context_offset = IVAL(inbody, 0x30);
145 in_context_length = IVAL(inbody, 0x34);
148 * First check if the dynamic name and context buffers
149 * are correctly specified.
151 * Note: That we don't check if the name and context buffers
152 * overlap
155 dyn_offset = SMB2_HDR_BODY + smb2req->in.vector[i+1].iov_len;
157 if (in_name_offset == 0 && in_name_length == 0) {
158 /* This is ok */
159 name_offset = 0;
160 } else if (in_name_offset < dyn_offset) {
161 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
162 } else {
163 name_offset = in_name_offset - dyn_offset;
166 if (name_offset > smb2req->in.vector[i+2].iov_len) {
167 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
170 name_available_length = smb2req->in.vector[i+2].iov_len - name_offset;
172 if (in_name_length > name_available_length) {
173 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
176 in_name_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
177 name_offset;
178 in_name_buffer.length = in_name_length;
180 if (in_context_offset == 0 && in_context_length == 0) {
181 /* This is ok */
182 context_offset = 0;
183 } else if (in_context_offset < dyn_offset) {
184 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
185 } else {
186 context_offset = in_context_offset - dyn_offset;
189 if (context_offset > smb2req->in.vector[i+2].iov_len) {
190 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
193 context_available_length = smb2req->in.vector[i+2].iov_len - context_offset;
195 if (in_context_length > context_available_length) {
196 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
199 in_context_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
200 context_offset;
201 in_context_buffer.length = in_context_length;
204 * Now interpret the name and context buffers
207 ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
208 in_name_buffer.data,
209 in_name_buffer.length,
210 &in_name_string,
211 &in_name_string_size);
212 if (!ok) {
213 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
216 if (in_name_buffer.length == 0) {
217 in_name_string_size = 0;
220 if (strlen(in_name_string) != in_name_string_size) {
221 return smbd_smb2_request_error(smb2req, NT_STATUS_OBJECT_NAME_INVALID);
224 ZERO_STRUCT(in_context_blobs);
225 status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
226 if (!NT_STATUS_IS_OK(status)) {
227 return smbd_smb2_request_error(smb2req, status);
230 tsubreq = smbd_smb2_create_send(smb2req,
231 smb2req->sconn->ev_ctx,
232 smb2req,
233 in_oplock_level,
234 in_impersonation_level,
235 in_desired_access,
236 in_file_attributes,
237 in_share_access,
238 in_create_disposition,
239 in_create_options,
240 in_name_string,
241 in_context_blobs);
242 if (tsubreq == NULL) {
243 smb2req->subreq = NULL;
244 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
246 tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
249 * For now we keep the logic that we do not send STATUS_PENDING
250 * for sharing violations, so we just wait 2 seconds.
252 * TODO: we need more tests for this.
254 return smbd_smb2_request_pending_queue(smb2req, tsubreq, 2000000);
257 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
259 uint8_t *reqhdr = (uint8_t *)smb2req->out.vector[smb2req->current_idx].iov_base;
260 return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
263 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
265 struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
266 struct smbd_smb2_request);
267 DATA_BLOB outbody;
268 DATA_BLOB outdyn;
269 uint8_t out_oplock_level = 0;
270 uint32_t out_create_action = 0;
271 NTTIME out_creation_time = 0;
272 NTTIME out_last_access_time = 0;
273 NTTIME out_last_write_time = 0;
274 NTTIME out_change_time = 0;
275 uint64_t out_allocation_size = 0;
276 uint64_t out_end_of_file = 0;
277 uint32_t out_file_attributes = 0;
278 uint64_t out_file_id_persistent = 0;
279 uint64_t out_file_id_volatile = 0;
280 struct smb2_create_blobs out_context_blobs;
281 DATA_BLOB out_context_buffer;
282 uint16_t out_context_buffer_offset = 0;
283 NTSTATUS status;
284 NTSTATUS error; /* transport error */
286 if (smb2req->cancelled) {
287 uint64_t mid = get_mid_from_smb2req(smb2req);
288 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
289 (unsigned long long)mid ));
290 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
291 if (!NT_STATUS_IS_OK(error)) {
292 smbd_server_connection_terminate(smb2req->sconn,
293 nt_errstr(error));
294 return;
296 return;
299 status = smbd_smb2_create_recv(tsubreq,
300 smb2req,
301 &out_oplock_level,
302 &out_create_action,
303 &out_creation_time,
304 &out_last_access_time,
305 &out_last_write_time,
306 &out_change_time,
307 &out_allocation_size,
308 &out_end_of_file,
309 &out_file_attributes,
310 &out_file_id_persistent,
311 &out_file_id_volatile,
312 &out_context_blobs);
313 if (!NT_STATUS_IS_OK(status)) {
314 error = smbd_smb2_request_error(smb2req, status);
315 if (!NT_STATUS_IS_OK(error)) {
316 smbd_server_connection_terminate(smb2req->sconn,
317 nt_errstr(error));
318 return;
320 return;
323 status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
324 if (!NT_STATUS_IS_OK(status)) {
325 error = smbd_smb2_request_error(smb2req, status);
326 if (!NT_STATUS_IS_OK(error)) {
327 smbd_server_connection_terminate(smb2req->sconn,
328 nt_errstr(error));
329 return;
331 return;
334 if (out_context_buffer.length > 0) {
335 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
338 outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
339 if (outbody.data == NULL) {
340 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
341 if (!NT_STATUS_IS_OK(error)) {
342 smbd_server_connection_terminate(smb2req->sconn,
343 nt_errstr(error));
344 return;
346 return;
349 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
350 SCVAL(outbody.data, 0x02,
351 out_oplock_level); /* oplock level */
352 SCVAL(outbody.data, 0x03, 0); /* reserved */
353 SIVAL(outbody.data, 0x04,
354 out_create_action); /* create action */
355 SBVAL(outbody.data, 0x08,
356 out_creation_time); /* creation time */
357 SBVAL(outbody.data, 0x10,
358 out_last_access_time); /* last access time */
359 SBVAL(outbody.data, 0x18,
360 out_last_write_time); /* last write time */
361 SBVAL(outbody.data, 0x20,
362 out_change_time); /* change time */
363 SBVAL(outbody.data, 0x28,
364 out_allocation_size); /* allocation size */
365 SBVAL(outbody.data, 0x30,
366 out_end_of_file); /* end of file */
367 SIVAL(outbody.data, 0x38,
368 out_file_attributes); /* file attributes */
369 SIVAL(outbody.data, 0x3C, 0); /* reserved */
370 SBVAL(outbody.data, 0x40,
371 out_file_id_persistent); /* file id (persistent) */
372 SBVAL(outbody.data, 0x48,
373 out_file_id_volatile); /* file id (volatile) */
374 SIVAL(outbody.data, 0x50,
375 out_context_buffer_offset); /* create contexts offset */
376 SIVAL(outbody.data, 0x54,
377 out_context_buffer.length); /* create contexts length */
379 outdyn = out_context_buffer;
381 error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
382 if (!NT_STATUS_IS_OK(error)) {
383 smbd_server_connection_terminate(smb2req->sconn,
384 nt_errstr(error));
385 return;
389 struct smbd_smb2_create_state {
390 struct smbd_smb2_request *smb2req;
391 struct smb_request *smb1req;
392 struct timed_event *te;
393 struct tevent_immediate *im;
394 struct timeval request_time;
395 struct file_id id;
396 DATA_BLOB private_data;
397 uint8_t out_oplock_level;
398 uint32_t out_create_action;
399 NTTIME out_creation_time;
400 NTTIME out_last_access_time;
401 NTTIME out_last_write_time;
402 NTTIME out_change_time;
403 uint64_t out_allocation_size;
404 uint64_t out_end_of_file;
405 uint32_t out_file_attributes;
406 uint64_t out_file_id_persistent;
407 uint64_t out_file_id_volatile;
408 struct smb2_create_blobs out_context_blobs;
411 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
412 struct tevent_context *ev,
413 struct smbd_smb2_request *smb2req,
414 uint8_t in_oplock_level,
415 uint32_t in_impersonation_level,
416 uint32_t in_desired_access,
417 uint32_t in_file_attributes,
418 uint32_t in_share_access,
419 uint32_t in_create_disposition,
420 uint32_t in_create_options,
421 const char *in_name,
422 struct smb2_create_blobs in_context_blobs)
424 struct tevent_req *req = NULL;
425 struct smbd_smb2_create_state *state = NULL;
426 NTSTATUS status;
427 struct smb_request *smb1req = NULL;
428 files_struct *result = NULL;
429 int info;
430 struct timespec write_time_ts;
431 struct smb2_create_blobs out_context_blobs;
432 int requested_oplock_level;
434 ZERO_STRUCT(out_context_blobs);
436 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat_conn))) {
437 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
438 } else {
439 requested_oplock_level = in_oplock_level;
443 if (smb2req->subreq == NULL) {
444 /* New create call. */
445 req = tevent_req_create(mem_ctx, &state,
446 struct smbd_smb2_create_state);
447 if (req == NULL) {
448 return NULL;
450 state->smb2req = smb2req;
452 smb1req = smbd_smb2_fake_smb_request(smb2req);
453 if (tevent_req_nomem(smb1req, req)) {
454 return tevent_req_post(req, ev);
456 state->smb1req = smb1req;
457 smb2req->subreq = req;
458 DEBUG(10,("smbd_smb2_create: name[%s]\n",
459 in_name));
460 } else {
461 /* Re-entrant create call. */
462 req = smb2req->subreq;
463 state = tevent_req_data(req,
464 struct smbd_smb2_create_state);
465 smb1req = state->smb1req;
466 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
467 in_name ));
470 if (IS_IPC(smb1req->conn)) {
471 const char *pipe_name = in_name;
473 if (!lp_nt_pipe_support()) {
474 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
475 return tevent_req_post(req, ev);
478 /* Strip \\ off the name. */
479 if (pipe_name[0] == '\\') {
480 pipe_name++;
483 status = open_np_file(smb1req, pipe_name, &result);
484 if (!NT_STATUS_IS_OK(status)) {
485 tevent_req_nterror(req, status);
486 return tevent_req_post(req, ev);
488 info = FILE_WAS_OPENED;
489 } else if (CAN_PRINT(smb1req->conn)) {
490 status = file_new(smb1req, smb1req->conn, &result);
491 if(!NT_STATUS_IS_OK(status)) {
492 tevent_req_nterror(req, status);
493 return tevent_req_post(req, ev);
496 status = print_spool_open(result, in_name,
497 smb1req->vuid);
498 if (!NT_STATUS_IS_OK(status)) {
499 file_free(smb1req, result);
500 tevent_req_nterror(req, status);
501 return tevent_req_post(req, ev);
503 info = FILE_WAS_CREATED;
504 } else {
505 char *fname;
506 struct smb_filename *smb_fname = NULL;
507 struct smb2_create_blob *exta = NULL;
508 struct ea_list *ea_list = NULL;
509 struct smb2_create_blob *mxac = NULL;
510 NTTIME max_access_time = 0;
511 struct smb2_create_blob *secd = NULL;
512 struct security_descriptor *sec_desc = NULL;
513 struct smb2_create_blob *dhnq = NULL;
514 struct smb2_create_blob *dhnc = NULL;
515 struct smb2_create_blob *alsi = NULL;
516 uint64_t allocation_size = 0;
517 struct smb2_create_blob *twrp = NULL;
518 struct smb2_create_blob *qfid = NULL;
520 exta = smb2_create_blob_find(&in_context_blobs,
521 SMB2_CREATE_TAG_EXTA);
522 mxac = smb2_create_blob_find(&in_context_blobs,
523 SMB2_CREATE_TAG_MXAC);
524 secd = smb2_create_blob_find(&in_context_blobs,
525 SMB2_CREATE_TAG_SECD);
526 dhnq = smb2_create_blob_find(&in_context_blobs,
527 SMB2_CREATE_TAG_DHNQ);
528 dhnc = smb2_create_blob_find(&in_context_blobs,
529 SMB2_CREATE_TAG_DHNC);
530 alsi = smb2_create_blob_find(&in_context_blobs,
531 SMB2_CREATE_TAG_ALSI);
532 twrp = smb2_create_blob_find(&in_context_blobs,
533 SMB2_CREATE_TAG_TWRP);
534 qfid = smb2_create_blob_find(&in_context_blobs,
535 SMB2_CREATE_TAG_QFID);
537 fname = talloc_strdup(state, in_name);
538 if (tevent_req_nomem(fname, req)) {
539 return tevent_req_post(req, ev);
542 if (exta) {
543 if (dhnc) {
544 tevent_req_nterror(req,NT_STATUS_OBJECT_NAME_NOT_FOUND);
545 return tevent_req_post(req, ev);
548 ea_list = read_nttrans_ea_list(mem_ctx,
549 (const char *)exta->data.data, exta->data.length);
550 if (!ea_list) {
551 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
552 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
553 return tevent_req_post(req, ev);
557 if (mxac) {
558 if (dhnc) {
559 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
560 return tevent_req_post(req, ev);
563 if (mxac->data.length == 0) {
564 max_access_time = 0;
565 } else if (mxac->data.length == 8) {
566 max_access_time = BVAL(mxac->data.data, 0);
567 } else {
568 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
569 return tevent_req_post(req, ev);
573 if (secd) {
574 enum ndr_err_code ndr_err;
576 if (dhnc) {
577 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
578 return tevent_req_post(req, ev);
581 sec_desc = talloc_zero(state, struct security_descriptor);
582 if (tevent_req_nomem(sec_desc, req)) {
583 return tevent_req_post(req, ev);
586 ndr_err = ndr_pull_struct_blob(&secd->data,
587 sec_desc, sec_desc,
588 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
589 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
590 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
591 ndr_errstr(ndr_err)));
592 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
593 return tevent_req_post(req, ev);
597 if (dhnq) {
598 if (dhnc) {
599 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
600 return tevent_req_post(req, ev);
603 if (dhnq->data.length != 16) {
604 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
605 return tevent_req_post(req, ev);
608 * we don't support durable handles yet
609 * and have to ignore this
613 if (dhnc) {
614 if (dhnc->data.length != 16) {
615 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
616 return tevent_req_post(req, ev);
618 /* we don't support durable handles yet */
619 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
620 return tevent_req_post(req, ev);
623 if (alsi) {
624 if (dhnc) {
625 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
626 return tevent_req_post(req, ev);
629 if (alsi->data.length != 8) {
630 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
631 return tevent_req_post(req, ev);
633 allocation_size = BVAL(alsi->data.data, 0);
636 if (twrp) {
637 NTTIME nttime;
638 time_t t;
639 struct tm *tm;
641 if (dhnc) {
642 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
643 return tevent_req_post(req, ev);
646 if (twrp->data.length != 8) {
647 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
648 return tevent_req_post(req, ev);
651 nttime = BVAL(twrp->data.data, 0);
652 t = nt_time_to_unix(nttime);
653 tm = gmtime(&t);
655 TALLOC_FREE(fname);
656 fname = talloc_asprintf(state,
657 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
658 tm->tm_year + 1900,
659 tm->tm_mon + 1,
660 tm->tm_mday,
661 tm->tm_hour,
662 tm->tm_min,
663 tm->tm_sec,
664 in_name);
665 if (tevent_req_nomem(fname, req)) {
666 return tevent_req_post(req, ev);
670 if (qfid) {
671 if (qfid->data.length != 0) {
672 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
673 return tevent_req_post(req, ev);
677 /* these are ignored for SMB2 */
678 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
679 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
682 * For a DFS path the function parse_dfs_path()
683 * will do the path processing.
686 if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
687 /* convert '\\' into '/' */
688 status = check_path_syntax(fname);
689 if (!NT_STATUS_IS_OK(status)) {
690 tevent_req_nterror(req, status);
691 return tevent_req_post(req, ev);
695 status = filename_convert(req,
696 smb1req->conn,
697 smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
698 fname,
700 NULL,
701 &smb_fname);
702 if (!NT_STATUS_IS_OK(status)) {
703 tevent_req_nterror(req, status);
704 return tevent_req_post(req, ev);
707 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
709 status = SMB_VFS_CREATE_FILE(smb1req->conn,
710 smb1req,
711 0, /* root_dir_fid */
712 smb_fname,
713 in_desired_access,
714 in_share_access,
715 in_create_disposition,
716 in_create_options,
717 in_file_attributes,
718 map_smb2_oplock_levels_to_samba(requested_oplock_level),
719 allocation_size,
720 0, /* private_flags */
721 sec_desc,
722 ea_list,
723 &result,
724 &info);
725 if (!NT_STATUS_IS_OK(status)) {
726 if (open_was_deferred(smb1req->sconn, smb1req->mid)) {
727 return req;
729 tevent_req_nterror(req, status);
730 return tevent_req_post(req, ev);
733 if (mxac) {
734 NTTIME last_write_time;
736 unix_timespec_to_nt_time(&last_write_time,
737 result->fsp_name->st.st_ex_mtime);
738 if (last_write_time != max_access_time) {
739 uint8_t p[8];
740 uint32_t max_access_granted;
741 DATA_BLOB blob = data_blob_const(p, sizeof(p));
743 status = smbd_calculate_access_mask(smb1req->conn,
744 result->fsp_name,
745 SEC_FLAG_MAXIMUM_ALLOWED,
746 &max_access_granted);
748 SIVAL(p, 0, NT_STATUS_V(status));
749 SIVAL(p, 4, max_access_granted);
751 status = smb2_create_blob_add(state,
752 &out_context_blobs,
753 SMB2_CREATE_TAG_MXAC,
754 blob);
755 if (!NT_STATUS_IS_OK(status)) {
756 tevent_req_nterror(req, status);
757 return tevent_req_post(req, ev);
762 if (qfid) {
763 uint8_t p[32];
764 uint64_t file_index = get_FileIndex(result->conn,
765 &result->fsp_name->st);
766 DATA_BLOB blob = data_blob_const(p, sizeof(p));
768 ZERO_STRUCT(p);
770 /* From conversations with Microsoft engineers at
771 the MS plugfest. The first 8 bytes are the "volume index"
772 == inode, the second 8 bytes are the "volume id",
773 == dev. This will be updated in the SMB2 doc. */
774 SBVAL(p, 0, file_index);
775 SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
777 status = smb2_create_blob_add(state, &out_context_blobs,
778 SMB2_CREATE_TAG_QFID,
779 blob);
780 if (!NT_STATUS_IS_OK(status)) {
781 tevent_req_nterror(req, status);
782 return tevent_req_post(req, ev);
787 smb2req->compat_chain_fsp = smb1req->chain_fsp;
789 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat_conn))) {
790 state->out_oplock_level = in_oplock_level;
791 } else {
792 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
795 if ((in_create_disposition == FILE_SUPERSEDE)
796 && (info == FILE_WAS_OVERWRITTEN)) {
797 state->out_create_action = FILE_WAS_SUPERSEDED;
798 } else {
799 state->out_create_action = info;
801 state->out_file_attributes = dos_mode(result->conn,
802 result->fsp_name);
803 /* Deal with other possible opens having a modified
804 write time. JRA. */
805 ZERO_STRUCT(write_time_ts);
806 get_file_infos(result->file_id, 0, NULL, &write_time_ts);
807 if (!null_timespec(write_time_ts)) {
808 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
811 unix_timespec_to_nt_time(&state->out_creation_time,
812 get_create_timespec(smb1req->conn, result,
813 result->fsp_name));
814 unix_timespec_to_nt_time(&state->out_last_access_time,
815 result->fsp_name->st.st_ex_atime);
816 unix_timespec_to_nt_time(&state->out_last_write_time,
817 result->fsp_name->st.st_ex_mtime);
818 unix_timespec_to_nt_time(&state->out_change_time,
819 get_change_timespec(smb1req->conn, result,
820 result->fsp_name));
821 state->out_allocation_size =
822 SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
823 &(result->fsp_name->st));
824 state->out_end_of_file = result->fsp_name->st.st_ex_size;
825 if (state->out_file_attributes == 0) {
826 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
828 state->out_file_id_persistent = result->fnum;
829 state->out_file_id_volatile = result->fnum;
830 state->out_context_blobs = out_context_blobs;
832 tevent_req_done(req);
833 return tevent_req_post(req, ev);
836 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
837 TALLOC_CTX *mem_ctx,
838 uint8_t *out_oplock_level,
839 uint32_t *out_create_action,
840 NTTIME *out_creation_time,
841 NTTIME *out_last_access_time,
842 NTTIME *out_last_write_time,
843 NTTIME *out_change_time,
844 uint64_t *out_allocation_size,
845 uint64_t *out_end_of_file,
846 uint32_t *out_file_attributes,
847 uint64_t *out_file_id_persistent,
848 uint64_t *out_file_id_volatile,
849 struct smb2_create_blobs *out_context_blobs)
851 NTSTATUS status;
852 struct smbd_smb2_create_state *state = tevent_req_data(req,
853 struct smbd_smb2_create_state);
855 if (tevent_req_is_nterror(req, &status)) {
856 tevent_req_received(req);
857 return status;
860 *out_oplock_level = state->out_oplock_level;
861 *out_create_action = state->out_create_action;
862 *out_creation_time = state->out_creation_time;
863 *out_last_access_time = state->out_last_access_time;
864 *out_last_write_time = state->out_last_write_time;
865 *out_change_time = state->out_change_time;
866 *out_allocation_size = state->out_allocation_size;
867 *out_end_of_file = state->out_end_of_file;
868 *out_file_attributes = state->out_file_attributes;
869 *out_file_id_persistent = state->out_file_id_persistent;
870 *out_file_id_volatile = state->out_file_id_volatile;
871 *out_context_blobs = state->out_context_blobs;
873 talloc_steal(mem_ctx, state->out_context_blobs.blobs);
875 tevent_req_received(req);
876 return NT_STATUS_OK;
879 /*********************************************************
880 Code for dealing with deferred opens.
881 *********************************************************/
883 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
884 struct timeval *p_request_time,
885 void **pp_state)
887 struct smbd_smb2_create_state *state = NULL;
888 struct tevent_req *req = NULL;
890 if (!smb2req) {
891 return false;
893 if (smb2req->async_te == NULL) {
894 return false;
896 req = smb2req->subreq;
897 if (!req) {
898 return false;
900 state = tevent_req_data(req, struct smbd_smb2_create_state);
901 if (!state) {
902 return false;
904 if (p_request_time) {
905 *p_request_time = state->request_time;
907 if (pp_state) {
908 *pp_state = (void *)state->private_data.data;
910 return true;
913 /*********************************************************
914 Re-process this call early - requested by message or
915 close.
916 *********************************************************/
918 static struct smbd_smb2_request *find_open_smb2req(
919 struct smbd_server_connection *sconn, uint64_t mid)
921 struct smbd_smb2_request *smb2req;
923 for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
924 uint64_t message_id;
925 if (smb2req->subreq == NULL) {
926 /* This message has been processed. */
927 continue;
929 if (!tevent_req_is_in_progress(smb2req->subreq)) {
930 /* This message has been processed. */
931 continue;
933 message_id = get_mid_from_smb2req(smb2req);
934 if (message_id == mid) {
935 return smb2req;
938 return NULL;
941 bool open_was_deferred_smb2(struct smbd_server_connection *sconn, uint64_t mid)
943 struct smbd_smb2_create_state *state = NULL;
944 struct smbd_smb2_request *smb2req;
946 smb2req = find_open_smb2req(sconn, mid);
948 if (!smb2req) {
949 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
950 (unsigned long long)mid));
951 return false;
953 if (!smb2req->subreq) {
954 return false;
956 if (!tevent_req_is_in_progress(smb2req->subreq)) {
957 return false;
959 state = tevent_req_data(smb2req->subreq,
960 struct smbd_smb2_create_state);
961 if (!state) {
962 return false;
964 /* It's not in progress if there's no timeout event. */
965 if (!state->te) {
966 return false;
969 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
970 (unsigned long long)mid));
972 return true;
975 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
976 uint64_t mid)
978 struct smbd_smb2_create_state *state = NULL;
980 if (!smb2req->subreq) {
981 return;
983 if (!tevent_req_is_in_progress(smb2req->subreq)) {
984 return;
986 state = tevent_req_data(smb2req->subreq,
987 struct smbd_smb2_create_state);
988 if (!state) {
989 return;
992 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
993 "mid %llu\n",
994 (unsigned long long)mid ));
996 /* Ensure we don't have any outstanding timer event. */
997 TALLOC_FREE(state->te);
998 /* Ensure we don't have any outstanding immediate event. */
999 TALLOC_FREE(state->im);
1002 void remove_deferred_open_message_smb2(
1003 struct smbd_server_connection *sconn, uint64_t mid)
1005 struct smbd_smb2_request *smb2req;
1007 smb2req = find_open_smb2req(sconn, mid);
1009 if (!smb2req) {
1010 DEBUG(10,("remove_deferred_open_message_smb2: "
1011 "can't find mid %llu\n",
1012 (unsigned long long)mid ));
1013 return;
1015 remove_deferred_open_message_smb2_internal(smb2req, mid);
1018 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1019 struct tevent_immediate *im,
1020 void *private_data)
1022 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1023 struct smbd_smb2_request);
1024 struct smbd_server_connection *sconn = smb2req->sconn;
1025 uint64_t mid = get_mid_from_smb2req(smb2req);
1026 NTSTATUS status;
1028 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1029 "re-dispatching mid %llu\n",
1030 (unsigned long long)mid ));
1032 status = smbd_smb2_request_dispatch(smb2req);
1033 if (!NT_STATUS_IS_OK(status)) {
1034 smbd_server_connection_terminate(sconn, nt_errstr(status));
1035 return;
1039 void schedule_deferred_open_message_smb2(
1040 struct smbd_server_connection *sconn, uint64_t mid)
1042 struct smbd_smb2_create_state *state = NULL;
1043 struct smbd_smb2_request *smb2req;
1045 smb2req = find_open_smb2req(sconn, mid);
1047 if (!smb2req) {
1048 DEBUG(10,("schedule_deferred_open_message_smb2: "
1049 "can't find mid %llu\n",
1050 (unsigned long long)mid ));
1051 return;
1053 if (!smb2req->subreq) {
1054 return;
1056 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1057 return;
1059 state = tevent_req_data(smb2req->subreq,
1060 struct smbd_smb2_create_state);
1061 if (!state) {
1062 return;
1065 /* Ensure we don't have any outstanding timer event. */
1066 TALLOC_FREE(state->te);
1067 /* Ensure we don't have any outstanding immediate event. */
1068 TALLOC_FREE(state->im);
1071 * This is subtle. We must null out the callback
1072 * before resheduling, else the first call to
1073 * tevent_req_nterror() causes the _receive()
1074 * function to be called, this causing tevent_req_post()
1075 * to crash.
1077 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1079 state->im = tevent_create_immediate(smb2req);
1080 if (!state->im) {
1081 smbd_server_connection_terminate(smb2req->sconn,
1082 nt_errstr(NT_STATUS_NO_MEMORY));
1083 return;
1086 DEBUG(10,("schedule_deferred_open_message_smb2: "
1087 "re-processing mid %llu\n",
1088 (unsigned long long)mid ));
1090 tevent_schedule_immediate(state->im,
1091 smb2req->sconn->ev_ctx,
1092 smbd_smb2_create_request_dispatch_immediate,
1093 smb2req);
1096 /*********************************************************
1097 Re-process this call.
1098 *********************************************************/
1100 static void smb2_deferred_open_timer(struct event_context *ev,
1101 struct timed_event *te,
1102 struct timeval _tval,
1103 void *private_data)
1105 NTSTATUS status;
1106 struct smbd_smb2_create_state *state = NULL;
1107 struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
1108 struct smbd_smb2_request);
1110 DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1111 smb2req->current_idx,
1112 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
1114 state = tevent_req_data(smb2req->subreq,
1115 struct smbd_smb2_create_state);
1116 if (!state) {
1117 return;
1120 * Null this out, don't talloc_free. It will
1121 * be talloc_free'd by the tevent library when
1122 * this returns.
1124 state->te = NULL;
1125 /* Ensure we don't have any outstanding immediate event. */
1126 TALLOC_FREE(state->im);
1129 * This is subtle. We must null out the callback
1130 * before resheduling, else the first call to
1131 * tevent_req_nterror() causes the _receive()
1132 * function to be called, this causing tevent_req_post()
1133 * to crash.
1135 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1137 status = smbd_smb2_request_dispatch(smb2req);
1139 if (!NT_STATUS_IS_OK(status)) {
1140 smbd_server_connection_terminate(smb2req->sconn,
1141 nt_errstr(status));
1145 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1147 struct smbd_smb2_request *smb2req = NULL;
1148 struct smbd_smb2_create_state *state = tevent_req_data(req,
1149 struct smbd_smb2_create_state);
1150 uint64_t mid;
1152 if (!state) {
1153 return false;
1156 if (!state->smb2req) {
1157 return false;
1160 smb2req = state->smb2req;
1161 mid = get_mid_from_smb2req(smb2req);
1163 remove_deferred_open_entry(state->id, mid,
1164 messaging_server_id(smb2req->sconn->msg_ctx));
1165 remove_deferred_open_message_smb2_internal(smb2req, mid);
1166 smb2req->cancelled = true;
1168 tevent_req_done(req);
1169 return true;
1172 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1173 struct timeval request_time,
1174 struct timeval timeout,
1175 struct file_id id,
1176 char *private_data,
1177 size_t priv_len)
1179 struct tevent_req *req = NULL;
1180 struct smbd_smb2_create_state *state = NULL;
1181 struct timeval end_time;
1183 if (!smb2req) {
1184 return false;
1186 req = smb2req->subreq;
1187 if (!req) {
1188 return false;
1190 state = tevent_req_data(req, struct smbd_smb2_create_state);
1191 if (!state) {
1192 return false;
1194 state->id = id;
1195 state->request_time = request_time;
1196 state->private_data = data_blob_talloc(state, private_data,
1197 priv_len);
1198 if (!state->private_data.data) {
1199 return false;
1202 /* Re-schedule us to retry on timer expiry. */
1203 end_time = timeval_sum(&request_time, &timeout);
1205 DEBUG(10,("push_deferred_open_message_smb2: "
1206 "timeout at %s\n",
1207 timeval_string(talloc_tos(),
1208 &end_time,
1209 true) ));
1211 state->te = tevent_add_timer(smb2req->sconn->ev_ctx,
1212 state,
1213 end_time,
1214 smb2_deferred_open_timer,
1215 smb2req);
1216 if (!state->te) {
1217 return false;
1220 /* allow this request to be canceled */
1221 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1223 return true;