source: vendor/current/source3/smbd/smb2_create.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 47.3 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
4
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
7
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.
12
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.
17
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/>.
20*/
21
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 "../librpc/gen_ndr/ndr_smb2_lease_struct.h"
29#include "../lib/util/tevent_ntstatus.h"
30#include "messages.h"
31
32int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
33{
34 switch(in_oplock_level) {
35 case SMB2_OPLOCK_LEVEL_NONE:
36 return NO_OPLOCK;
37 case SMB2_OPLOCK_LEVEL_II:
38 return LEVEL_II_OPLOCK;
39 case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
40 return EXCLUSIVE_OPLOCK;
41 case SMB2_OPLOCK_LEVEL_BATCH:
42 return BATCH_OPLOCK;
43 case SMB2_OPLOCK_LEVEL_LEASE:
44 return LEASE_OPLOCK;
45 default:
46 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
47 "unknown level %u\n",
48 (unsigned int)in_oplock_level));
49 return NO_OPLOCK;
50 }
51}
52
53static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
54{
55 if (BATCH_OPLOCK_TYPE(oplock_type)) {
56 return SMB2_OPLOCK_LEVEL_BATCH;
57 } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type)) {
58 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
59 } else if (oplock_type == LEVEL_II_OPLOCK) {
60 return SMB2_OPLOCK_LEVEL_II;
61 } else if (oplock_type == LEASE_OPLOCK) {
62 return SMB2_OPLOCK_LEVEL_LEASE;
63 } else {
64 return SMB2_OPLOCK_LEVEL_NONE;
65 }
66}
67
68static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
69 struct tevent_context *ev,
70 struct smbd_smb2_request *smb2req,
71 uint8_t in_oplock_level,
72 uint32_t in_impersonation_level,
73 uint32_t in_desired_access,
74 uint32_t in_file_attributes,
75 uint32_t in_share_access,
76 uint32_t in_create_disposition,
77 uint32_t in_create_options,
78 const char *in_name,
79 struct smb2_create_blobs in_context_blobs);
80static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
81 TALLOC_CTX *mem_ctx,
82 uint8_t *out_oplock_level,
83 uint32_t *out_create_action,
84 struct timespec *out_creation_ts,
85 struct timespec *out_last_access_ts,
86 struct timespec *out_last_write_ts,
87 struct timespec *out_change_ts,
88 uint64_t *out_allocation_size,
89 uint64_t *out_end_of_file,
90 uint32_t *out_file_attributes,
91 uint64_t *out_file_id_persistent,
92 uint64_t *out_file_id_volatile,
93 struct smb2_create_blobs *out_context_blobs);
94
95static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
96NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
97{
98 const uint8_t *inbody;
99 const struct iovec *indyniov;
100 uint8_t in_oplock_level;
101 uint32_t in_impersonation_level;
102 uint32_t in_desired_access;
103 uint32_t in_file_attributes;
104 uint32_t in_share_access;
105 uint32_t in_create_disposition;
106 uint32_t in_create_options;
107 uint16_t in_name_offset;
108 uint16_t in_name_length;
109 DATA_BLOB in_name_buffer;
110 char *in_name_string;
111 size_t in_name_string_size;
112 uint32_t name_offset = 0;
113 uint32_t name_available_length = 0;
114 uint32_t in_context_offset;
115 uint32_t in_context_length;
116 DATA_BLOB in_context_buffer;
117 struct smb2_create_blobs in_context_blobs;
118 uint32_t context_offset = 0;
119 uint32_t context_available_length = 0;
120 uint32_t dyn_offset;
121 NTSTATUS status;
122 bool ok;
123 struct tevent_req *tsubreq;
124
125 status = smbd_smb2_request_verify_sizes(smb2req, 0x39);
126 if (!NT_STATUS_IS_OK(status)) {
127 return smbd_smb2_request_error(smb2req, status);
128 }
129 inbody = SMBD_SMB2_IN_BODY_PTR(smb2req);
130
131 in_oplock_level = CVAL(inbody, 0x03);
132 in_impersonation_level = IVAL(inbody, 0x04);
133 in_desired_access = IVAL(inbody, 0x18);
134 in_file_attributes = IVAL(inbody, 0x1C);
135 in_share_access = IVAL(inbody, 0x20);
136 in_create_disposition = IVAL(inbody, 0x24);
137 in_create_options = IVAL(inbody, 0x28);
138 in_name_offset = SVAL(inbody, 0x2C);
139 in_name_length = SVAL(inbody, 0x2E);
140 in_context_offset = IVAL(inbody, 0x30);
141 in_context_length = IVAL(inbody, 0x34);
142
143 /*
144 * First check if the dynamic name and context buffers
145 * are correctly specified.
146 *
147 * Note: That we don't check if the name and context buffers
148 * overlap
149 */
150
151 dyn_offset = SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(smb2req);
152
153 if (in_name_offset == 0 && in_name_length == 0) {
154 /* This is ok */
155 name_offset = 0;
156 } else if (in_name_offset < dyn_offset) {
157 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
158 } else {
159 name_offset = in_name_offset - dyn_offset;
160 }
161
162 indyniov = SMBD_SMB2_IN_DYN_IOV(smb2req);
163
164 if (name_offset > indyniov->iov_len) {
165 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
166 }
167
168 name_available_length = indyniov->iov_len - name_offset;
169
170 if (in_name_length > name_available_length) {
171 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
172 }
173
174 in_name_buffer.data = (uint8_t *)indyniov->iov_base + name_offset;
175 in_name_buffer.length = in_name_length;
176
177 if (in_context_offset == 0 && in_context_length == 0) {
178 /* This is ok */
179 context_offset = 0;
180 } else if (in_context_offset < dyn_offset) {
181 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
182 } else {
183 context_offset = in_context_offset - dyn_offset;
184 }
185
186 if (context_offset > indyniov->iov_len) {
187 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
188 }
189
190 context_available_length = indyniov->iov_len - context_offset;
191
192 if (in_context_length > context_available_length) {
193 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
194 }
195
196 in_context_buffer.data = (uint8_t *)indyniov->iov_base +
197 context_offset;
198 in_context_buffer.length = in_context_length;
199
200 /*
201 * Now interpret the name and context buffers
202 */
203
204 ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
205 in_name_buffer.data,
206 in_name_buffer.length,
207 &in_name_string,
208 &in_name_string_size);
209 if (!ok) {
210 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
211 }
212
213 if (in_name_buffer.length == 0) {
214 in_name_string_size = 0;
215 }
216
217 if (strlen(in_name_string) != in_name_string_size) {
218 return smbd_smb2_request_error(smb2req, NT_STATUS_OBJECT_NAME_INVALID);
219 }
220
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);
225 }
226
227 tsubreq = smbd_smb2_create_send(smb2req,
228 smb2req->sconn->ev_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);
242 }
243 tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
244
245 return smbd_smb2_request_pending_queue(smb2req, tsubreq, 500);
246}
247
248static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
249{
250 uint8_t *reqhdr = SMBD_SMB2_OUT_HDR_PTR(smb2req);
251 return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
252}
253
254static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
255{
256 struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
257 struct smbd_smb2_request);
258 DATA_BLOB outbody;
259 DATA_BLOB outdyn;
260 uint8_t out_oplock_level = 0;
261 uint32_t out_create_action = 0;
262 connection_struct *conn = smb2req->tcon->compat;
263 struct timespec out_creation_ts = { 0, };
264 struct timespec out_last_access_ts = { 0, };
265 struct timespec out_last_write_ts = { 0, };
266 struct timespec out_change_ts = { 0, };
267 uint64_t out_allocation_size = 0;
268 uint64_t out_end_of_file = 0;
269 uint32_t out_file_attributes = 0;
270 uint64_t out_file_id_persistent = 0;
271 uint64_t out_file_id_volatile = 0;
272 struct smb2_create_blobs out_context_blobs;
273 DATA_BLOB out_context_buffer;
274 uint16_t out_context_buffer_offset = 0;
275 NTSTATUS status;
276 NTSTATUS error; /* transport error */
277
278 status = smbd_smb2_create_recv(tsubreq,
279 smb2req,
280 &out_oplock_level,
281 &out_create_action,
282 &out_creation_ts,
283 &out_last_access_ts,
284 &out_last_write_ts,
285 &out_change_ts,
286 &out_allocation_size,
287 &out_end_of_file,
288 &out_file_attributes,
289 &out_file_id_persistent,
290 &out_file_id_volatile,
291 &out_context_blobs);
292 if (!NT_STATUS_IS_OK(status)) {
293 error = smbd_smb2_request_error(smb2req, status);
294 if (!NT_STATUS_IS_OK(error)) {
295 smbd_server_connection_terminate(smb2req->xconn,
296 nt_errstr(error));
297 return;
298 }
299 return;
300 }
301
302 status = smb2_create_blob_push(smb2req, &out_context_buffer, 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->xconn,
307 nt_errstr(error));
308 return;
309 }
310 return;
311 }
312
313 if (out_context_buffer.length > 0) {
314 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
315 }
316
317 outbody = smbd_smb2_generate_outbody(smb2req, 0x58);
318 if (outbody.data == NULL) {
319 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
320 if (!NT_STATUS_IS_OK(error)) {
321 smbd_server_connection_terminate(smb2req->xconn,
322 nt_errstr(error));
323 return;
324 }
325 return;
326 }
327
328 SSVAL(outbody.data, 0x00, 0x58 + 1); /* struct size */
329 SCVAL(outbody.data, 0x02,
330 out_oplock_level); /* oplock level */
331 SCVAL(outbody.data, 0x03, 0); /* reserved */
332 SIVAL(outbody.data, 0x04,
333 out_create_action); /* create action */
334 put_long_date_timespec(conn->ts_res,
335 (char *)outbody.data + 0x08,
336 out_creation_ts); /* creation time */
337 put_long_date_timespec(conn->ts_res,
338 (char *)outbody.data + 0x10,
339 out_last_access_ts); /* last access time */
340 put_long_date_timespec(conn->ts_res,
341 (char *)outbody.data + 0x18,
342 out_last_write_ts); /* last write time */
343 put_long_date_timespec(conn->ts_res,
344 (char *)outbody.data + 0x20,
345 out_change_ts); /* change time */
346 SBVAL(outbody.data, 0x28,
347 out_allocation_size); /* allocation size */
348 SBVAL(outbody.data, 0x30,
349 out_end_of_file); /* end of file */
350 SIVAL(outbody.data, 0x38,
351 out_file_attributes); /* file attributes */
352 SIVAL(outbody.data, 0x3C, 0); /* reserved */
353 SBVAL(outbody.data, 0x40,
354 out_file_id_persistent); /* file id (persistent) */
355 SBVAL(outbody.data, 0x48,
356 out_file_id_volatile); /* file id (volatile) */
357 SIVAL(outbody.data, 0x50,
358 out_context_buffer_offset); /* create contexts offset */
359 SIVAL(outbody.data, 0x54,
360 out_context_buffer.length); /* create contexts length */
361
362 outdyn = out_context_buffer;
363
364 error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
365 if (!NT_STATUS_IS_OK(error)) {
366 smbd_server_connection_terminate(smb2req->xconn,
367 nt_errstr(error));
368 return;
369 }
370}
371
372static bool smb2_lease_key_valid(const struct smb2_lease_key *key)
373{
374 return ((key->data[0] != 0) || (key->data[1] != 0));
375}
376
377static NTSTATUS smbd_smb2_create_durable_lease_check(
378 const char *requested_filename, const struct files_struct *fsp,
379 const struct smb2_lease *lease_ptr)
380{
381 struct smb_filename *smb_fname = NULL;
382 uint32_t ucf_flags = UCF_PREP_CREATEFILE;
383 NTSTATUS status;
384
385 if (lease_ptr == NULL) {
386 if (fsp->oplock_type != LEASE_OPLOCK) {
387 return NT_STATUS_OK;
388 }
389 DEBUG(10, ("Reopened file has lease, but no lease "
390 "requested\n"));
391 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
392 }
393
394 if (fsp->oplock_type != LEASE_OPLOCK) {
395 DEBUG(10, ("Lease requested, but reopened file has no "
396 "lease\n"));
397 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
398 }
399
400 if (!smb2_lease_key_equal(&lease_ptr->lease_key,
401 &fsp->lease->lease.lease_key)) {
402 DEBUG(10, ("Different lease key requested than found "
403 "in reopened file\n"));
404 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
405 }
406
407 status = filename_convert(talloc_tos(), fsp->conn, false,
408 requested_filename, ucf_flags,
409 NULL, &smb_fname);
410 if (!NT_STATUS_IS_OK(status)) {
411 DEBUG(10, ("filename_convert returned %s\n",
412 nt_errstr(status)));
413 return status;
414 }
415
416 if (!strequal(fsp->fsp_name->base_name, smb_fname->base_name)) {
417 DEBUG(10, ("Lease requested for file %s, reopened file "
418 "is named %s\n", smb_fname->base_name,
419 fsp->fsp_name->base_name));
420 TALLOC_FREE(smb_fname);
421 return NT_STATUS_INVALID_PARAMETER;
422 }
423
424 TALLOC_FREE(smb_fname);
425
426 return NT_STATUS_OK;
427}
428
429struct smbd_smb2_create_state {
430 struct smbd_smb2_request *smb2req;
431 struct smb_request *smb1req;
432 bool open_was_deferred;
433 struct tevent_timer *te;
434 struct tevent_immediate *im;
435 struct timeval request_time;
436 struct file_id id;
437 struct deferred_open_record *open_rec;
438 uint8_t out_oplock_level;
439 uint32_t out_create_action;
440 struct timespec out_creation_ts;
441 struct timespec out_last_access_ts;
442 struct timespec out_last_write_ts;
443 struct timespec out_change_ts;
444 uint64_t out_allocation_size;
445 uint64_t out_end_of_file;
446 uint32_t out_file_attributes;
447 uint64_t out_file_id_persistent;
448 uint64_t out_file_id_volatile;
449 struct smb2_create_blobs *out_context_blobs;
450};
451
452static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
453 struct tevent_context *ev,
454 struct smbd_smb2_request *smb2req,
455 uint8_t in_oplock_level,
456 uint32_t in_impersonation_level,
457 uint32_t in_desired_access,
458 uint32_t in_file_attributes,
459 uint32_t in_share_access,
460 uint32_t in_create_disposition,
461 uint32_t in_create_options,
462 const char *in_name,
463 struct smb2_create_blobs in_context_blobs)
464{
465 struct tevent_req *req = NULL;
466 struct smbd_smb2_create_state *state = NULL;
467 NTSTATUS status;
468 struct smb_request *smb1req = NULL;
469 files_struct *result = NULL;
470 int info;
471 int requested_oplock_level;
472 struct smb2_create_blob *dhnc = NULL;
473 struct smb2_create_blob *dh2c = NULL;
474 struct smb2_create_blob *dhnq = NULL;
475 struct smb2_create_blob *dh2q = NULL;
476 struct smb2_create_blob *rqls = NULL;
477 bool replay_operation = false;
478
479 if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
480 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
481 } else {
482 requested_oplock_level = in_oplock_level;
483 }
484
485
486 if (smb2req->subreq == NULL) {
487 /* New create call. */
488 req = tevent_req_create(mem_ctx, &state,
489 struct smbd_smb2_create_state);
490 if (req == NULL) {
491 return NULL;
492 }
493 state->smb2req = smb2req;
494
495 smb1req = smbd_smb2_fake_smb_request(smb2req);
496 if (tevent_req_nomem(smb1req, req)) {
497 return tevent_req_post(req, ev);
498 }
499 state->smb1req = smb1req;
500 smb2req->subreq = req;
501 DEBUG(10,("smbd_smb2_create: name[%s]\n",
502 in_name));
503 } else {
504 /* Re-entrant create call. */
505 req = smb2req->subreq;
506 state = tevent_req_data(req,
507 struct smbd_smb2_create_state);
508 smb1req = state->smb1req;
509 TALLOC_FREE(state->out_context_blobs);
510 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
511 in_name ));
512 }
513
514 state->out_context_blobs = talloc_zero(state, struct smb2_create_blobs);
515 if (tevent_req_nomem(state->out_context_blobs, req)) {
516 return tevent_req_post(req, ev);
517 }
518
519 dhnq = smb2_create_blob_find(&in_context_blobs,
520 SMB2_CREATE_TAG_DHNQ);
521 dhnc = smb2_create_blob_find(&in_context_blobs,
522 SMB2_CREATE_TAG_DHNC);
523 dh2q = smb2_create_blob_find(&in_context_blobs,
524 SMB2_CREATE_TAG_DH2Q);
525 dh2c = smb2_create_blob_find(&in_context_blobs,
526 SMB2_CREATE_TAG_DH2C);
527 if (smb2req->xconn->smb2.server.capabilities & SMB2_CAP_LEASING) {
528 rqls = smb2_create_blob_find(&in_context_blobs,
529 SMB2_CREATE_TAG_RQLS);
530 }
531
532 if ((dhnc && dh2c) || (dhnc && dh2q) || (dh2c && dhnq) ||
533 (dh2q && dh2c))
534 {
535 /* not both are allowed at the same time */
536 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
537 return tevent_req_post(req, ev);
538 }
539
540 if (dhnc) {
541 uint32_t num_blobs_allowed;
542
543 if (dhnc->data.length != 16) {
544 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
545 return tevent_req_post(req, ev);
546 }
547
548 /*
549 * According to MS-SMB2: 3.3.5.9.7, "Handling the
550 * SMB2_CREATE_DURABLE_HANDLE_RECONNECT Create Context",
551 * we should ignore an additional dhnq blob, but fail
552 * the request (with status OBJECT_NAME_NOT_FOUND) if
553 * any other extra create blob has been provided.
554 *
555 * (Note that the cases of an additional dh2q or dh2c blob
556 * which require a different error code, have been treated
557 * above.)
558 */
559
560 if (dhnq) {
561 num_blobs_allowed = 2;
562 } else {
563 num_blobs_allowed = 1;
564 }
565
566 if (rqls != NULL) {
567 num_blobs_allowed += 1;
568 }
569
570 if (in_context_blobs.num_blobs != num_blobs_allowed) {
571 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
572 return tevent_req_post(req, ev);
573 }
574 }
575
576 if (dh2c) {
577 uint32_t num_blobs_allowed;
578
579 if (dh2c->data.length != 36) {
580 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
581 return tevent_req_post(req, ev);
582 }
583
584 /*
585 * According to MS-SMB2: 3.3.5.9.12, "Handling the
586 * SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 Create Context",
587 * we should fail the request with status
588 * OBJECT_NAME_NOT_FOUND if any other create blob has been
589 * provided.
590 *
591 * (Note that the cases of an additional dhnq, dhnc or dh2q
592 * blob which require a different error code, have been
593 * treated above.)
594 */
595
596 num_blobs_allowed = 1;
597
598 if (rqls != NULL) {
599 num_blobs_allowed += 1;
600 }
601
602 if (in_context_blobs.num_blobs != num_blobs_allowed) {
603 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
604 return tevent_req_post(req, ev);
605 }
606 }
607
608 if (IS_IPC(smb1req->conn)) {
609 const char *pipe_name = in_name;
610
611 if (dhnc || dh2c) {
612 /* durable handles are not supported on IPC$ */
613 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
614 return tevent_req_post(req, ev);
615 }
616
617 if (!lp_nt_pipe_support()) {
618 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
619 return tevent_req_post(req, ev);
620 }
621
622 status = open_np_file(smb1req, pipe_name, &result);
623 if (!NT_STATUS_IS_OK(status)) {
624 tevent_req_nterror(req, status);
625 return tevent_req_post(req, ev);
626 }
627 info = FILE_WAS_OPENED;
628 } else if (CAN_PRINT(smb1req->conn)) {
629 if (dhnc || dh2c) {
630 /* durable handles are not supported on printers */
631 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
632 return tevent_req_post(req, ev);
633 }
634
635 status = file_new(smb1req, smb1req->conn, &result);
636 if(!NT_STATUS_IS_OK(status)) {
637 tevent_req_nterror(req, status);
638 return tevent_req_post(req, ev);
639 }
640
641 status = print_spool_open(result, in_name,
642 smb1req->vuid);
643 if (!NT_STATUS_IS_OK(status)) {
644 file_free(smb1req, result);
645 tevent_req_nterror(req, status);
646 return tevent_req_post(req, ev);
647 }
648 info = FILE_WAS_CREATED;
649 } else {
650 char *fname;
651 struct smb2_create_blob *exta = NULL;
652 struct ea_list *ea_list = NULL;
653 struct smb2_create_blob *mxac = NULL;
654 NTTIME max_access_time = 0;
655 struct smb2_create_blob *secd = NULL;
656 struct security_descriptor *sec_desc = NULL;
657 struct smb2_create_blob *alsi = NULL;
658 uint64_t allocation_size = 0;
659 struct smb2_create_blob *twrp = NULL;
660 struct smb2_create_blob *qfid = NULL;
661 struct GUID _create_guid = GUID_zero();
662 struct GUID *create_guid = NULL;
663 bool update_open = false;
664 bool durable_requested = false;
665 uint32_t durable_timeout_msec = 0;
666 bool do_durable_reconnect = false;
667 uint64_t persistent_id = 0;
668 struct smb2_lease lease;
669 struct smb2_lease *lease_ptr = NULL;
670 ssize_t lease_len = -1;
671 bool need_replay_cache = false;
672 struct smbXsrv_open *op = NULL;
673#if 0
674 struct smb2_create_blob *svhdx = NULL;
675#endif
676
677 exta = smb2_create_blob_find(&in_context_blobs,
678 SMB2_CREATE_TAG_EXTA);
679 mxac = smb2_create_blob_find(&in_context_blobs,
680 SMB2_CREATE_TAG_MXAC);
681 secd = smb2_create_blob_find(&in_context_blobs,
682 SMB2_CREATE_TAG_SECD);
683 alsi = smb2_create_blob_find(&in_context_blobs,
684 SMB2_CREATE_TAG_ALSI);
685 twrp = smb2_create_blob_find(&in_context_blobs,
686 SMB2_CREATE_TAG_TWRP);
687 qfid = smb2_create_blob_find(&in_context_blobs,
688 SMB2_CREATE_TAG_QFID);
689#if 0
690 if (smb2req->xconn->protocol >= PROTOCOL_SMB3_02) {
691 /*
692 * This was introduced with SMB3_02
693 */
694 svhdx = smb2_create_blob_find(&in_context_blobs,
695 SVHDX_OPEN_DEVICE_CONTEXT);
696 }
697#endif
698
699 fname = talloc_strdup(state, in_name);
700 if (tevent_req_nomem(fname, req)) {
701 return tevent_req_post(req, ev);
702 }
703
704 if (exta) {
705 if (!lp_ea_support(SNUM(smb2req->tcon->compat))) {
706 tevent_req_nterror(req,
707 NT_STATUS_EAS_NOT_SUPPORTED);
708 return tevent_req_post(req, ev);
709 }
710
711 ea_list = read_nttrans_ea_list(mem_ctx,
712 (const char *)exta->data.data, exta->data.length);
713 if (!ea_list) {
714 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
715 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
716 return tevent_req_post(req, ev);
717 }
718
719 if (ea_list_has_invalid_name(ea_list)) {
720 tevent_req_nterror(req, STATUS_INVALID_EA_NAME);
721 return tevent_req_post(req, ev);
722 }
723 }
724
725 if (mxac) {
726 if (mxac->data.length == 0) {
727 max_access_time = 0;
728 } else if (mxac->data.length == 8) {
729 max_access_time = BVAL(mxac->data.data, 0);
730 } else {
731 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
732 return tevent_req_post(req, ev);
733 }
734 }
735
736 if (secd) {
737 enum ndr_err_code ndr_err;
738
739 sec_desc = talloc_zero(state, struct security_descriptor);
740 if (tevent_req_nomem(sec_desc, req)) {
741 return tevent_req_post(req, ev);
742 }
743
744 ndr_err = ndr_pull_struct_blob(&secd->data,
745 sec_desc, sec_desc,
746 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
747 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
748 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
749 ndr_errstr(ndr_err)));
750 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
751 return tevent_req_post(req, ev);
752 }
753 }
754
755 if (dhnq) {
756 if (dhnq->data.length != 16) {
757 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
758 return tevent_req_post(req, ev);
759 }
760
761 if (dh2q) {
762 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
763 return tevent_req_post(req, ev);
764 }
765
766 /*
767 * durable handle request is processed below.
768 */
769 durable_requested = true;
770 /*
771 * Set the timeout to 16 mins.
772 *
773 * TODO: test this against Windows 2012
774 * as the default for durable v2 is 1 min.
775 */
776 durable_timeout_msec = (16*60*1000);
777 }
778
779 if (dh2q) {
780 const uint8_t *p = dh2q->data.data;
781 uint32_t durable_v2_timeout = 0;
782 DATA_BLOB create_guid_blob;
783 const uint8_t *hdr;
784 uint32_t flags;
785
786 if (dh2q->data.length != 32) {
787 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
788 return tevent_req_post(req, ev);
789 }
790
791 if (dhnq) {
792 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
793 return tevent_req_post(req, ev);
794 }
795
796 durable_v2_timeout = IVAL(p, 0);
797 create_guid_blob = data_blob_const(p + 16, 16);
798
799 status = GUID_from_ndr_blob(&create_guid_blob,
800 &_create_guid);
801 if (tevent_req_nterror(req, status)) {
802 return tevent_req_post(req, ev);
803 }
804 create_guid = &_create_guid;
805 /*
806 * we need to store the create_guid later
807 */
808 update_open = true;
809
810 /*
811 * And we need to create a cache for replaying the
812 * create.
813 */
814 need_replay_cache = true;
815
816 /*
817 * durable handle v2 request processed below
818 */
819 durable_requested = true;
820 durable_timeout_msec = durable_v2_timeout;
821 if (durable_timeout_msec == 0) {
822 /*
823 * Set the timeout to 1 min as default.
824 *
825 * This matches Windows 2012.
826 */
827 durable_timeout_msec = (60*1000);
828 }
829
830 /*
831 * Check for replay operation.
832 * Only consider it when we have dh2q.
833 * If we do not have a replay operation, verify that
834 * the create_guid is not cached for replay.
835 */
836 hdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
837 flags = IVAL(hdr, SMB2_HDR_FLAGS);
838 replay_operation =
839 !!(flags & SMB2_HDR_FLAG_REPLAY_OPERATION);
840
841 status = smb2srv_open_lookup_replay_cache(
842 smb2req->xconn, create_guid,
843 0 /* now */, &op);
844
845 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
846 replay_operation = false;
847 } else if (tevent_req_nterror(req, status)) {
848 DBG_WARNING("smb2srv_open_lookup_replay_cache "
849 "failed: %s\n", nt_errstr(status));
850 return tevent_req_post(req, ev);
851 } else if (!replay_operation) {
852 /*
853 * If a create without replay operation flag
854 * is sent but with a create_guid that is
855 * currently in the replay cache -- fail.
856 */
857 status = NT_STATUS_DUPLICATE_OBJECTID;
858 (void)tevent_req_nterror(req, status);
859 return tevent_req_post(req, ev);
860 }
861 }
862
863 if (dhnc) {
864 persistent_id = BVAL(dhnc->data.data, 0);
865
866 do_durable_reconnect = true;
867 }
868
869 if (dh2c) {
870 const uint8_t *p = dh2c->data.data;
871 DATA_BLOB create_guid_blob;
872
873 persistent_id = BVAL(p, 0);
874 create_guid_blob = data_blob_const(p + 16, 16);
875
876 status = GUID_from_ndr_blob(&create_guid_blob,
877 &_create_guid);
878 if (tevent_req_nterror(req, status)) {
879 return tevent_req_post(req, ev);
880 }
881 create_guid = &_create_guid;
882
883 do_durable_reconnect = true;
884 }
885
886 if (alsi) {
887 if (alsi->data.length != 8) {
888 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
889 return tevent_req_post(req, ev);
890 }
891 allocation_size = BVAL(alsi->data.data, 0);
892 }
893
894 if (twrp) {
895 NTTIME nttime;
896 time_t t;
897 struct tm *tm;
898
899 if (twrp->data.length != 8) {
900 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
901 return tevent_req_post(req, ev);
902 }
903
904 nttime = BVAL(twrp->data.data, 0);
905 t = nt_time_to_unix(nttime);
906 tm = gmtime(&t);
907
908 TALLOC_FREE(fname);
909 fname = talloc_asprintf(state,
910 "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
911 tm->tm_year + 1900,
912 tm->tm_mon + 1,
913 tm->tm_mday,
914 tm->tm_hour,
915 tm->tm_min,
916 tm->tm_sec,
917 in_name);
918 if (tevent_req_nomem(fname, req)) {
919 return tevent_req_post(req, ev);
920 }
921 }
922
923 if (qfid) {
924 if (qfid->data.length != 0) {
925 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
926 return tevent_req_post(req, ev);
927 }
928 }
929
930 if (rqls) {
931 lease_len = smb2_lease_pull(
932 rqls->data.data, rqls->data.length, &lease);
933 if (lease_len == -1) {
934 tevent_req_nterror(
935 req, NT_STATUS_INVALID_PARAMETER);
936 return tevent_req_post(req, ev);
937 }
938 lease_ptr = &lease;
939
940 if (DEBUGLEVEL >= 10) {
941 DEBUG(10, ("Got lease request size %d\n",
942 (int)lease_len));
943 NDR_PRINT_DEBUG(smb2_lease, lease_ptr);
944 }
945
946 if (!smb2_lease_key_valid(&lease.lease_key)) {
947 lease_ptr = NULL;
948 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
949 }
950
951 if ((smb2req->xconn->protocol < PROTOCOL_SMB3_00) &&
952 (lease.lease_version != 1)) {
953 DEBUG(10, ("v2 lease key only for SMB3\n"));
954 lease_ptr = NULL;
955 }
956
957 /*
958 * Replay with a lease is only allowed if the
959 * established open carries a lease with the
960 * same lease key.
961 */
962 if (replay_operation) {
963 struct smb2_lease *op_ls =
964 &op->compat->lease->lease;
965 int op_oplock = op->compat->oplock_type;
966
967 if (map_samba_oplock_levels_to_smb2(op_oplock)
968 != SMB2_OPLOCK_LEVEL_LEASE)
969 {
970 status = NT_STATUS_ACCESS_DENIED;
971 (void)tevent_req_nterror(req, status);
972 return tevent_req_post(req, ev);
973 }
974 if (!smb2_lease_key_equal(&lease.lease_key,
975 &op_ls->lease_key))
976 {
977 status = NT_STATUS_ACCESS_DENIED;
978 (void)tevent_req_nterror(req, status);
979 return tevent_req_post(req, ev);
980 }
981 }
982 }
983
984 /* these are ignored for SMB2 */
985 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
986 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
987
988 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
989
990 DEBUG(10, ("smbd_smb2_create_send: open execution phase\n"));
991
992 /*
993 * For the backend file open procedure, there are
994 * three possible modes: replay operation (in which case
995 * there is nothing else to do), durable_reconnect or
996 * new open.
997 */
998 if (replay_operation) {
999 result = op->compat;
1000 result->op = op;
1001 update_open = false;
1002 info = op->create_action;
1003 } else if (do_durable_reconnect) {
1004 DATA_BLOB new_cookie = data_blob_null;
1005 NTTIME now = timeval_to_nttime(&smb2req->request_time);
1006
1007 status = smb2srv_open_recreate(smb2req->xconn,
1008 smb1req->conn->session_info,
1009 persistent_id, create_guid,
1010 now, &op);
1011 if (!NT_STATUS_IS_OK(status)) {
1012 DEBUG(3, ("smbd_smb2_create_send: "
1013 "smb2srv_open_recreate failed: %s\n",
1014 nt_errstr(status)));
1015 tevent_req_nterror(req, status);
1016 return tevent_req_post(req, ev);
1017 }
1018
1019 DEBUG(10, ("smb2_create_send: %s to recreate the "
1020 "smb2srv_open struct for a durable handle.\n",
1021 op->global->durable ? "succeded" : "failed"));
1022
1023 if (!op->global->durable) {
1024 talloc_free(op);
1025 tevent_req_nterror(req,
1026 NT_STATUS_OBJECT_NAME_NOT_FOUND);
1027 return tevent_req_post(req, ev);
1028 }
1029
1030 status = SMB_VFS_DURABLE_RECONNECT(smb1req->conn,
1031 smb1req,
1032 op, /* smbXsrv_open input */
1033 op->global->backend_cookie,
1034 op, /* TALLOC_CTX */
1035 &result, &new_cookie);
1036 if (!NT_STATUS_IS_OK(status)) {
1037 NTSTATUS return_status;
1038
1039 return_status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1040
1041 DEBUG(3, ("smbd_smb2_create_send: "
1042 "durable_reconnect failed: %s => %s\n",
1043 nt_errstr(status),
1044 nt_errstr(return_status)));
1045
1046 tevent_req_nterror(req, return_status);
1047 return tevent_req_post(req, ev);
1048 }
1049
1050 DEBUG(10, ("result->oplock_type=%u, lease_ptr==%p\n",
1051 (unsigned)result->oplock_type, lease_ptr));
1052
1053 status = smbd_smb2_create_durable_lease_check(
1054 fname, result, lease_ptr);
1055 if (!NT_STATUS_IS_OK(status)) {
1056 close_file(smb1req, result, SHUTDOWN_CLOSE);
1057 tevent_req_nterror(req, status);
1058 return tevent_req_post(req, ev);
1059 }
1060
1061 data_blob_free(&op->global->backend_cookie);
1062 op->global->backend_cookie = new_cookie;
1063
1064 op->status = NT_STATUS_OK;
1065 op->global->disconnect_time = 0;
1066
1067 /* save the timout for later update */
1068 durable_timeout_msec = op->global->durable_timeout_msec;
1069
1070 update_open = true;
1071
1072 info = FILE_WAS_OPENED;
1073 } else {
1074 struct smb_filename *smb_fname = NULL;
1075 uint32_t ucf_flags = UCF_PREP_CREATEFILE;
1076
1077 if (requested_oplock_level == SMB2_OPLOCK_LEVEL_LEASE) {
1078 if (lease_ptr == NULL) {
1079 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
1080 }
1081 } else {
1082 lease_ptr = NULL;
1083 }
1084
1085 /*
1086 * For a DFS path the function parse_dfs_path()
1087 * will do the path processing.
1088 */
1089
1090 if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
1091 /* convert '\\' into '/' */
1092 status = check_path_syntax(fname);
1093 if (!NT_STATUS_IS_OK(status)) {
1094 tevent_req_nterror(req, status);
1095 return tevent_req_post(req, ev);
1096 }
1097 }
1098
1099 status = filename_convert(req,
1100 smb1req->conn,
1101 smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
1102 fname,
1103 ucf_flags,
1104 NULL, /* ppath_contains_wcards */
1105 &smb_fname);
1106 if (!NT_STATUS_IS_OK(status)) {
1107 tevent_req_nterror(req, status);
1108 return tevent_req_post(req, ev);
1109 }
1110
1111 /*
1112 * MS-SMB2: 2.2.13 SMB2 CREATE Request
1113 * ImpersonationLevel ... MUST contain one of the
1114 * following values. The server MUST validate this
1115 * field, but otherwise ignore it.
1116 *
1117 * NB. The source4/torture/smb2/durable_open.c test
1118 * shows this check is only done on real opens, not
1119 * on durable handle-reopens.
1120 */
1121
1122 if (in_impersonation_level >
1123 SMB2_IMPERSONATION_DELEGATE) {
1124 tevent_req_nterror(req,
1125 NT_STATUS_BAD_IMPERSONATION_LEVEL);
1126 return tevent_req_post(req, ev);
1127 }
1128
1129 /*
1130 * We know we're going to do a local open, so now
1131 * we must be protocol strict. JRA.
1132 *
1133 * MS-SMB2: 3.3.5.9 - Receiving an SMB2 CREATE Request
1134 * If the file name length is greater than zero and the
1135 * first character is a path separator character, the
1136 * server MUST fail the request with
1137 * STATUS_INVALID_PARAMETER.
1138 */
1139 if (in_name[0] == '\\' || in_name[0] == '/') {
1140 tevent_req_nterror(req,
1141 NT_STATUS_INVALID_PARAMETER);
1142 return tevent_req_post(req, ev);
1143 }
1144
1145 status = SMB_VFS_CREATE_FILE(smb1req->conn,
1146 smb1req,
1147 0, /* root_dir_fid */
1148 smb_fname,
1149 in_desired_access,
1150 in_share_access,
1151 in_create_disposition,
1152 in_create_options,
1153 in_file_attributes,
1154 map_smb2_oplock_levels_to_samba(requested_oplock_level),
1155 lease_ptr,
1156 allocation_size,
1157 0, /* private_flags */
1158 sec_desc,
1159 ea_list,
1160 &result,
1161 &info,
1162 &in_context_blobs,
1163 state->out_context_blobs);
1164 if (!NT_STATUS_IS_OK(status)) {
1165 if (open_was_deferred(smb1req->xconn, smb1req->mid)) {
1166 SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(smb2req->profile);
1167 return req;
1168 }
1169 tevent_req_nterror(req, status);
1170 return tevent_req_post(req, ev);
1171 }
1172 op = result->op;
1173 }
1174
1175 /*
1176 * here we have op == result->op
1177 */
1178
1179 DEBUG(10, ("smbd_smb2_create_send: "
1180 "response construction phase\n"));
1181
1182 if (mxac) {
1183 NTTIME last_write_time;
1184
1185 last_write_time = unix_timespec_to_nt_time(
1186 result->fsp_name->st.st_ex_mtime);
1187 if (last_write_time != max_access_time) {
1188 uint8_t p[8];
1189 uint32_t max_access_granted;
1190 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1191
1192 status = smbd_calculate_access_mask(smb1req->conn,
1193 result->fsp_name,
1194 false,
1195 SEC_FLAG_MAXIMUM_ALLOWED,
1196 &max_access_granted);
1197
1198 SIVAL(p, 0, NT_STATUS_V(status));
1199 SIVAL(p, 4, max_access_granted);
1200
1201 status = smb2_create_blob_add(
1202 state->out_context_blobs,
1203 state->out_context_blobs,
1204 SMB2_CREATE_TAG_MXAC,
1205 blob);
1206 if (!NT_STATUS_IS_OK(status)) {
1207 tevent_req_nterror(req, status);
1208 return tevent_req_post(req, ev);
1209 }
1210 }
1211 }
1212
1213 if (!replay_operation && durable_requested &&
1214 (fsp_lease_type(result) & SMB2_LEASE_HANDLE))
1215 {
1216 status = SMB_VFS_DURABLE_COOKIE(result,
1217 op,
1218 &op->global->backend_cookie);
1219 if (!NT_STATUS_IS_OK(status)) {
1220 op->global->backend_cookie = data_blob_null;
1221 }
1222 }
1223 if (!replay_operation && op->global->backend_cookie.length > 0)
1224 {
1225 update_open = true;
1226
1227 op->global->durable = true;
1228 op->global->durable_timeout_msec = durable_timeout_msec;
1229 }
1230
1231 if (update_open) {
1232 op->global->create_guid = _create_guid;
1233 if (need_replay_cache) {
1234 op->flags |= SMBXSRV_OPEN_NEED_REPLAY_CACHE;
1235 }
1236
1237 status = smbXsrv_open_update(op);
1238 DEBUG(10, ("smb2_create_send: smbXsrv_open_update "
1239 "returned %s\n",
1240 nt_errstr(status)));
1241 if (!NT_STATUS_IS_OK(status)) {
1242 tevent_req_nterror(req, status);
1243 return tevent_req_post(req, ev);
1244 }
1245 }
1246
1247 if (dhnq && op->global->durable) {
1248 uint8_t p[8] = { 0, };
1249 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1250
1251 status = smb2_create_blob_add(state->out_context_blobs,
1252 state->out_context_blobs,
1253 SMB2_CREATE_TAG_DHNQ,
1254 blob);
1255 if (!NT_STATUS_IS_OK(status)) {
1256 tevent_req_nterror(req, status);
1257 return tevent_req_post(req, ev);
1258 }
1259 }
1260
1261 if (dh2q && op->global->durable &&
1262 /*
1263 * For replay operations, we return the dh2q blob
1264 * in the case of oplocks not based on the state of
1265 * the open, but on whether it could have been granted
1266 * for the request data. In the case of leases instead,
1267 * the state of the open is used...
1268 */
1269 (!replay_operation ||
1270 in_oplock_level == SMB2_OPLOCK_LEVEL_BATCH ||
1271 in_oplock_level == SMB2_OPLOCK_LEVEL_LEASE))
1272 {
1273 uint8_t p[8] = { 0, };
1274 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1275 uint32_t durable_v2_response_flags = 0;
1276
1277 SIVAL(p, 0, op->global->durable_timeout_msec);
1278 SIVAL(p, 4, durable_v2_response_flags);
1279
1280 status = smb2_create_blob_add(state->out_context_blobs,
1281 state->out_context_blobs,
1282 SMB2_CREATE_TAG_DH2Q,
1283 blob);
1284 if (!NT_STATUS_IS_OK(status)) {
1285 tevent_req_nterror(req, status);
1286 return tevent_req_post(req, ev);
1287 }
1288 }
1289
1290 if (qfid) {
1291 uint8_t p[32];
1292 uint64_t file_index = get_FileIndex(result->conn,
1293 &result->fsp_name->st);
1294 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1295
1296 ZERO_STRUCT(p);
1297
1298 /* From conversations with Microsoft engineers at
1299 the MS plugfest. The first 8 bytes are the "volume index"
1300 == inode, the second 8 bytes are the "volume id",
1301 == dev. This will be updated in the SMB2 doc. */
1302 SBVAL(p, 0, file_index);
1303 SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
1304
1305 status = smb2_create_blob_add(state->out_context_blobs,
1306 state->out_context_blobs,
1307 SMB2_CREATE_TAG_QFID,
1308 blob);
1309 if (!NT_STATUS_IS_OK(status)) {
1310 tevent_req_nterror(req, status);
1311 return tevent_req_post(req, ev);
1312 }
1313 }
1314
1315 if ((rqls != NULL) && (result->oplock_type == LEASE_OPLOCK)) {
1316 uint8_t buf[52];
1317
1318 lease = result->lease->lease;
1319
1320 lease_len = sizeof(buf);
1321 if (lease.lease_version == 1) {
1322 lease_len = 32;
1323 }
1324
1325 if (!smb2_lease_push(&lease, buf, lease_len)) {
1326 tevent_req_nterror(
1327 req, NT_STATUS_INTERNAL_ERROR);
1328 return tevent_req_post(req, ev);
1329 }
1330
1331 status = smb2_create_blob_add(
1332 state, state->out_context_blobs,
1333 SMB2_CREATE_TAG_RQLS,
1334 data_blob_const(buf, lease_len));
1335 if (!NT_STATUS_IS_OK(status)) {
1336 tevent_req_nterror(req, status);
1337 return tevent_req_post(req, ev);
1338 }
1339 }
1340 }
1341
1342 smb2req->compat_chain_fsp = smb1req->chain_fsp;
1343
1344 if (replay_operation) {
1345 state->out_oplock_level = in_oplock_level;
1346 } else if (lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
1347 state->out_oplock_level = in_oplock_level;
1348 } else {
1349 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
1350 }
1351
1352 if ((in_create_disposition == FILE_SUPERSEDE)
1353 && (info == FILE_WAS_OVERWRITTEN)) {
1354 state->out_create_action = FILE_WAS_SUPERSEDED;
1355 } else {
1356 state->out_create_action = info;
1357 }
1358 result->op->create_action = state->out_create_action;
1359 state->out_file_attributes = dos_mode(result->conn,
1360 result->fsp_name);
1361
1362 state->out_creation_ts = get_create_timespec(smb1req->conn,
1363 result, result->fsp_name);
1364 state->out_last_access_ts = result->fsp_name->st.st_ex_atime;
1365 state->out_last_write_ts = result->fsp_name->st.st_ex_mtime;
1366 state->out_change_ts = get_change_timespec(smb1req->conn,
1367 result, result->fsp_name);
1368
1369 if (lp_dos_filetime_resolution(SNUM(smb2req->tcon->compat))) {
1370 dos_filetime_timespec(&state->out_creation_ts);
1371 dos_filetime_timespec(&state->out_last_access_ts);
1372 dos_filetime_timespec(&state->out_last_write_ts);
1373 dos_filetime_timespec(&state->out_change_ts);
1374 }
1375
1376 state->out_allocation_size =
1377 SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
1378 &(result->fsp_name->st));
1379 state->out_end_of_file = result->fsp_name->st.st_ex_size;
1380 if (state->out_file_attributes == 0) {
1381 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
1382 }
1383 state->out_file_id_persistent = result->op->global->open_persistent_id;
1384 state->out_file_id_volatile = result->op->global->open_volatile_id;
1385
1386 DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
1387 fsp_str_dbg(result), fsp_fnum_dbg(result)));
1388
1389 tevent_req_done(req);
1390 return tevent_req_post(req, ev);
1391}
1392
1393static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
1394 TALLOC_CTX *mem_ctx,
1395 uint8_t *out_oplock_level,
1396 uint32_t *out_create_action,
1397 struct timespec *out_creation_ts,
1398 struct timespec *out_last_access_ts,
1399 struct timespec *out_last_write_ts,
1400 struct timespec *out_change_ts,
1401 uint64_t *out_allocation_size,
1402 uint64_t *out_end_of_file,
1403 uint32_t *out_file_attributes,
1404 uint64_t *out_file_id_persistent,
1405 uint64_t *out_file_id_volatile,
1406 struct smb2_create_blobs *out_context_blobs)
1407{
1408 NTSTATUS status;
1409 struct smbd_smb2_create_state *state = tevent_req_data(req,
1410 struct smbd_smb2_create_state);
1411
1412 if (tevent_req_is_nterror(req, &status)) {
1413 tevent_req_received(req);
1414 return status;
1415 }
1416
1417 *out_oplock_level = state->out_oplock_level;
1418 *out_create_action = state->out_create_action;
1419 *out_creation_ts = state->out_creation_ts;
1420 *out_last_access_ts = state->out_last_access_ts;
1421 *out_last_write_ts = state->out_last_write_ts;
1422 *out_change_ts = state->out_change_ts;
1423 *out_allocation_size = state->out_allocation_size;
1424 *out_end_of_file = state->out_end_of_file;
1425 *out_file_attributes = state->out_file_attributes;
1426 *out_file_id_persistent = state->out_file_id_persistent;
1427 *out_file_id_volatile = state->out_file_id_volatile;
1428 *out_context_blobs = *(state->out_context_blobs);
1429
1430 talloc_steal(mem_ctx, state->out_context_blobs->blobs);
1431
1432 tevent_req_received(req);
1433 return NT_STATUS_OK;
1434}
1435
1436/*********************************************************
1437 Code for dealing with deferred opens.
1438*********************************************************/
1439
1440bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
1441 struct timeval *p_request_time,
1442 struct deferred_open_record **open_rec)
1443{
1444 struct smbd_smb2_create_state *state = NULL;
1445 struct tevent_req *req = NULL;
1446
1447 if (!smb2req) {
1448 return false;
1449 }
1450 req = smb2req->subreq;
1451 if (!req) {
1452 return false;
1453 }
1454 state = tevent_req_data(req, struct smbd_smb2_create_state);
1455 if (!state) {
1456 return false;
1457 }
1458 if (!state->open_was_deferred) {
1459 return false;
1460 }
1461 if (p_request_time) {
1462 *p_request_time = state->request_time;
1463 }
1464 if (open_rec != NULL) {
1465 *open_rec = state->open_rec;
1466 }
1467 return true;
1468}
1469
1470/*********************************************************
1471 Re-process this call early - requested by message or
1472 close.
1473*********************************************************/
1474
1475static struct smbd_smb2_request *find_open_smb2req(
1476 struct smbXsrv_connection *xconn, uint64_t mid)
1477{
1478 struct smbd_smb2_request *smb2req;
1479
1480 for (smb2req = xconn->smb2.requests; smb2req; smb2req = smb2req->next) {
1481 uint64_t message_id;
1482 if (smb2req->subreq == NULL) {
1483 /* This message has been processed. */
1484 continue;
1485 }
1486 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1487 /* This message has been processed. */
1488 continue;
1489 }
1490 message_id = get_mid_from_smb2req(smb2req);
1491 if (message_id == mid) {
1492 return smb2req;
1493 }
1494 }
1495 return NULL;
1496}
1497
1498bool open_was_deferred_smb2(struct smbXsrv_connection *xconn, uint64_t mid)
1499{
1500 struct smbd_smb2_create_state *state = NULL;
1501 struct smbd_smb2_request *smb2req;
1502
1503 smb2req = find_open_smb2req(xconn, mid);
1504
1505 if (!smb2req) {
1506 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
1507 (unsigned long long)mid));
1508 return false;
1509 }
1510 if (!smb2req->subreq) {
1511 return false;
1512 }
1513 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1514 return false;
1515 }
1516 state = tevent_req_data(smb2req->subreq,
1517 struct smbd_smb2_create_state);
1518 if (!state) {
1519 return false;
1520 }
1521 /* It's not in progress if there's no timeout event. */
1522 if (!state->open_was_deferred) {
1523 return false;
1524 }
1525
1526 DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
1527 (unsigned long long)mid));
1528
1529 return true;
1530}
1531
1532static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
1533 uint64_t mid)
1534{
1535 struct smbd_smb2_create_state *state = NULL;
1536
1537 if (!smb2req->subreq) {
1538 return;
1539 }
1540 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1541 return;
1542 }
1543 state = tevent_req_data(smb2req->subreq,
1544 struct smbd_smb2_create_state);
1545 if (!state) {
1546 return;
1547 }
1548
1549 DEBUG(10,("remove_deferred_open_message_smb2_internal: "
1550 "mid %llu\n",
1551 (unsigned long long)mid ));
1552
1553 state->open_was_deferred = false;
1554 /* Ensure we don't have any outstanding timer event. */
1555 TALLOC_FREE(state->te);
1556 /* Ensure we don't have any outstanding immediate event. */
1557 TALLOC_FREE(state->im);
1558}
1559
1560void remove_deferred_open_message_smb2(
1561 struct smbXsrv_connection *xconn, uint64_t mid)
1562{
1563 struct smbd_smb2_request *smb2req;
1564
1565 smb2req = find_open_smb2req(xconn, mid);
1566
1567 if (!smb2req) {
1568 DEBUG(10,("remove_deferred_open_message_smb2: "
1569 "can't find mid %llu\n",
1570 (unsigned long long)mid ));
1571 return;
1572 }
1573 remove_deferred_open_message_smb2_internal(smb2req, mid);
1574}
1575
1576static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1577 struct tevent_immediate *im,
1578 void *private_data)
1579{
1580 struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1581 struct smbd_smb2_request);
1582 uint64_t mid = get_mid_from_smb2req(smb2req);
1583 NTSTATUS status;
1584
1585 DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1586 "re-dispatching mid %llu\n",
1587 (unsigned long long)mid ));
1588
1589 status = smbd_smb2_request_dispatch(smb2req);
1590 if (!NT_STATUS_IS_OK(status)) {
1591 smbd_server_connection_terminate(smb2req->xconn,
1592 nt_errstr(status));
1593 return;
1594 }
1595}
1596
1597bool schedule_deferred_open_message_smb2(
1598 struct smbXsrv_connection *xconn, uint64_t mid)
1599{
1600 struct smbd_smb2_create_state *state = NULL;
1601 struct smbd_smb2_request *smb2req;
1602
1603 smb2req = find_open_smb2req(xconn, mid);
1604
1605 if (!smb2req) {
1606 DEBUG(10,("schedule_deferred_open_message_smb2: "
1607 "can't find mid %llu\n",
1608 (unsigned long long)mid ));
1609 return false;
1610 }
1611 if (!smb2req->subreq) {
1612 return false;
1613 }
1614 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1615 return false;
1616 }
1617 state = tevent_req_data(smb2req->subreq,
1618 struct smbd_smb2_create_state);
1619 if (!state) {
1620 return false;
1621 }
1622
1623 /* Ensure we don't have any outstanding timer event. */
1624 TALLOC_FREE(state->te);
1625 /* Ensure we don't have any outstanding immediate event. */
1626 TALLOC_FREE(state->im);
1627
1628 /*
1629 * This is subtle. We must null out the callback
1630 * before rescheduling, else the first call to
1631 * tevent_req_nterror() causes the _receive()
1632 * function to be called, this causing tevent_req_post()
1633 * to crash.
1634 */
1635 tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1636
1637 state->im = tevent_create_immediate(smb2req);
1638 if (!state->im) {
1639 smbd_server_connection_terminate(smb2req->xconn,
1640 nt_errstr(NT_STATUS_NO_MEMORY));
1641 return false;
1642 }
1643
1644 DEBUG(10,("schedule_deferred_open_message_smb2: "
1645 "re-processing mid %llu\n",
1646 (unsigned long long)mid ));
1647
1648 tevent_schedule_immediate(state->im,
1649 smb2req->sconn->ev_ctx,
1650 smbd_smb2_create_request_dispatch_immediate,
1651 smb2req);
1652
1653 return true;
1654}
1655
1656static bool smbd_smb2_create_cancel(struct tevent_req *req)
1657{
1658 struct smbd_smb2_request *smb2req = NULL;
1659 struct smbd_smb2_create_state *state = tevent_req_data(req,
1660 struct smbd_smb2_create_state);
1661 uint64_t mid;
1662
1663 if (!state) {
1664 return false;
1665 }
1666
1667 if (!state->smb2req) {
1668 return false;
1669 }
1670
1671 smb2req = state->smb2req;
1672 mid = get_mid_from_smb2req(smb2req);
1673
1674 if (is_deferred_open_async(state->open_rec)) {
1675 /* Can't cancel an async create. */
1676 return false;
1677 }
1678
1679 remove_deferred_open_message_smb2_internal(smb2req, mid);
1680
1681 tevent_req_defer_callback(req, smb2req->sconn->ev_ctx);
1682 tevent_req_nterror(req, NT_STATUS_CANCELLED);
1683 return true;
1684}
1685
1686bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1687 struct timeval request_time,
1688 struct timeval timeout,
1689 struct file_id id,
1690 struct deferred_open_record *open_rec)
1691{
1692 struct tevent_req *req = NULL;
1693 struct smbd_smb2_create_state *state = NULL;
1694 struct timeval end_time;
1695
1696 if (!smb2req) {
1697 return false;
1698 }
1699 req = smb2req->subreq;
1700 if (!req) {
1701 return false;
1702 }
1703 state = tevent_req_data(req, struct smbd_smb2_create_state);
1704 if (!state) {
1705 return false;
1706 }
1707 state->id = id;
1708 state->request_time = request_time;
1709 state->open_rec = talloc_move(state, &open_rec);
1710
1711 /* Re-schedule us to retry on timer expiry. */
1712 end_time = timeval_sum(&request_time, &timeout);
1713
1714 DEBUG(10,("push_deferred_open_message_smb2: "
1715 "timeout at %s\n",
1716 timeval_string(talloc_tos(),
1717 &end_time,
1718 true) ));
1719
1720 state->open_was_deferred = true;
1721
1722 /* allow this request to be canceled */
1723 tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1724
1725 return true;
1726}
Note: See TracBrowser for help on using the repository browser.