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