Debian Codegraph @

source4/torture/smb2/durable_open.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3
4 test suite for SMB2 durable opens
5
6 Copyright (C) Stefan Metzmacher 2008
7 Copyright (C) Michael Adam 2011-2012
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "../libcli/smb/smbXcli_base.h"
27 #include "torture/torture.h"
28 #include "torture/smb2/proto.h"
29 #include "torture/util.h"
30 #include "../libcli/smb/smbXcli_base.h"
31 #include "lib/param/param.h"
32 #include "libcli/resolve/resolve.h"
33 #include "auth/credentials/credentials.h"
34
35 #define CHECK_VAL(v, correct) \
36 torture_assert_u64_equal_goto(tctx, v, correct, ret, done, __location__)
37
38 #define CHECK_NOT_VAL(v, incorrect) \
39 torture_assert_u64_not_equal_goto(tctx, v, incorrect, ret, done, __location__)
40
41 #define CHECK_NOT_NULL(p) \
42 torture_assert_not_null_goto(tctx, p, ret, done, __location__)
43
44 #define CHECK_STATUS(status, correct) \
45 torture_assert_ntstatus_equal_goto(tctx, status, correct, ret, done, __location__)
46
47 #define CHECK_CREATED(__io, __created, __attribute) \
48 do { \
49 CHECK_VAL((__io)->out.create_action, NTCREATEX_ACTION_ ## __created); \
50 CHECK_VAL((__io)->out.size, 0); \
51 CHECK_VAL((__io)->out.file_attr, (__attribute)); \
52 CHECK_VAL((__io)->out.reserved2, 0); \
53 } while(0)
54
55 #define CHECK_CREATED_SIZE(__io, __created, __attribute, __alloc_size, __size) \
56 do { \
57 CHECK_VAL((__io)->out.create_action, NTCREATEX_ACTION_ ## __created); \
58 if (__alloc_size != 0) { \
59 CHECK_VAL((__io)->out.alloc_size, (__alloc_size)); \
60 } \
61 CHECK_VAL((__io)->out.size, (__size)); \
62 CHECK_VAL((__io)->out.file_attr, (__attribute)); \
63 CHECK_VAL((__io)->out.reserved2, 0); \
64 } while(0)
65
66
67
68 /**
69 * basic durable_open test.
70 * durable state should only be granted when requested
71 * along with a batch oplock or a handle lease.
72 *
73 * This test tests durable open with all possible oplock types.
74 */
75
76 struct durable_open_vs_oplock {
77 const char *level;
78 const char *share_mode;
79 bool expected;
80 };
81
82 #define NUM_OPLOCK_TYPES 4
83 #define NUM_SHARE_MODES 8
84 #define NUM_OPLOCK_OPEN_TESTS ( NUM_OPLOCK_TYPES * NUM_SHARE_MODES )
85 static struct durable_open_vs_oplock durable_open_vs_oplock_table[NUM_OPLOCK_OPEN_TESTS] =
86 {
87 { "", "", false },
88 { "", "R", false },
89 { "", "W", false },
90 { "", "D", false },
91 { "", "RD", false },
92 { "", "RW", false },
93 { "", "WD", false },
94 { "", "RWD", false },
95
96 { "s", "", false },
97 { "s", "R", false },
98 { "s", "W", false },
99 { "s", "D", false },
100 { "s", "RD", false },
101 { "s", "RW", false },
102 { "s", "WD", false },
103 { "s", "RWD", false },
104
105 { "x", "", false },
106 { "x", "R", false },
107 { "x", "W", false },
108 { "x", "D", false },
109 { "x", "RD", false },
110 { "x", "RW", false },
111 { "x", "WD", false },
112 { "x", "RWD", false },
113
114 { "b", "", true },
115 { "b", "R", true },
116 { "b", "W", true },
117 { "b", "D", true },
118 { "b", "RD", true },
119 { "b", "RW", true },
120 { "b", "WD", true },
121 { "b", "RWD", true },
122 };
123
124 static bool test_one_durable_open_open_oplock(struct torture_context *tctx,
125 struct smb2_tree *tree,
126 const char *fname,
127 struct durable_open_vs_oplock test)
128 {
129 NTSTATUS status;
130 TALLOC_CTX *mem_ctx = talloc_new(tctx);
131 struct smb2_handle _h;
132 struct smb2_handle *h = NULL;
133 bool ret = true;
134 struct smb2_create io;
135
136 smb2_util_unlink(tree, fname);
137
138 smb2_oplock_create_share(&io, fname,
139 smb2_util_share_access(test.share_mode),
140 smb2_util_oplock_level(test.level));
141 io.in.durable_open = true;
142
143 status = smb2_create(tree, mem_ctx, &io);
144 CHECK_STATUS(status, NT_STATUS_OK);
145 _h = io.out.file.handle;
146 h = &_h;
147 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
148 CHECK_VAL(io.out.durable_open, test.expected);
149 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level(test.level));
150
151 done:
152 if (h != NULL) {
153 smb2_util_close(tree, *h);
154 }
155 smb2_util_unlink(tree, fname);
156 talloc_free(mem_ctx);
157
158 return ret;
159 }
160
161 static bool test_durable_open_open_oplock(struct torture_context *tctx,
162 struct smb2_tree *tree)
163 {
164 TALLOC_CTX *mem_ctx = talloc_new(tctx);
165 char fname[256];
166 bool ret = true;
167 int i;
168
169 /* Choose a random name in case the state is left a little funky. */
170 snprintf(fname, 256, "durable_open_open_oplock_%s.dat", generate_random_str(tctx, 8));
171
172 smb2_util_unlink(tree, fname);
173
174 /* test various oplock levels with durable open */
175
176 for (i = 0; i < NUM_OPLOCK_OPEN_TESTS; i++) {
177 ret = test_one_durable_open_open_oplock(tctx,
178 tree,
179 fname,
180 durable_open_vs_oplock_table[i]);
181 if (ret == false) {
182 goto done;
183 }
184 }
185
186 done:
187 smb2_util_unlink(tree, fname);
188 talloc_free(tree);
189 talloc_free(mem_ctx);
190
191 return ret;
192 }
193
194 /**
195 * basic durable_open test.
196 * durable state should only be granted when requested
197 * along with a batch oplock or a handle lease.
198 *
199 * This test tests durable open with all valid lease types.
200 */
201
202 struct durable_open_vs_lease {
203 const char *type;
204 const char *share_mode;
205 bool expected;
206 };
207
208 #define NUM_LEASE_TYPES 5
209 #define NUM_LEASE_OPEN_TESTS ( NUM_LEASE_TYPES * NUM_SHARE_MODES )
210 static struct durable_open_vs_lease durable_open_vs_lease_table[NUM_LEASE_OPEN_TESTS] =
211 {
212 { "", "", false },
213 { "", "R", false },
214 { "", "W", false },
215 { "", "D", false },
216 { "", "RW", false },
217 { "", "RD", false },
218 { "", "WD", false },
219 { "", "RWD", false },
220
221 { "R", "", false },
222 { "R", "R", false },
223 { "R", "W", false },
224 { "R", "D", false },
225 { "R", "RW", false },
226 { "R", "RD", false },
227 { "R", "DW", false },
228 { "R", "RWD", false },
229
230 { "RW", "", false },
231 { "RW", "R", false },
232 { "RW", "W", false },
233 { "RW", "D", false },
234 { "RW", "RW", false },
235 { "RW", "RD", false },
236 { "RW", "WD", false },
237 { "RW", "RWD", false },
238
239 { "RH", "", true },
240 { "RH", "R", true },
241 { "RH", "W", true },
242 { "RH", "D", true },
243 { "RH", "RW", true },
244 { "RH", "RD", true },
245 { "RH", "WD", true },
246 { "RH", "RWD", true },
247
248 { "RHW", "", true },
249 { "RHW", "R", true },
250 { "RHW", "W", true },
251 { "RHW", "D", true },
252 { "RHW", "RW", true },
253 { "RHW", "RD", true },
254 { "RHW", "WD", true },
255 { "RHW", "RWD", true },
256 };
257
258 static bool test_one_durable_open_open_lease(struct torture_context *tctx,
259 struct smb2_tree *tree,
260 const char *fname,
261 struct durable_open_vs_lease test)
262 {
263 NTSTATUS status;
264 TALLOC_CTX *mem_ctx = talloc_new(tctx);
265 struct smb2_handle _h;
266 struct smb2_handle *h = NULL;
267 bool ret = true;
268 struct smb2_create io;
269 struct smb2_lease ls;
270 uint64_t lease;
271 uint32_t caps;
272
273 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
274 if (!(caps & SMB2_CAP_LEASING)) {
275 torture_skip(tctx, "leases are not supported");
276 }
277
278 smb2_util_unlink(tree, fname);
279
280 lease = random();
281
282 smb2_lease_create_share(&io, &ls, false /* dir */, fname,
283 smb2_util_share_access(test.share_mode),
284 lease,
285 smb2_util_lease_state(test.type));
286 io.in.durable_open = true;
287
288 status = smb2_create(tree, mem_ctx, &io);
289 CHECK_STATUS(status, NT_STATUS_OK);
290 _h = io.out.file.handle;
291 h = &_h;
292 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
293 CHECK_VAL(io.out.durable_open, test.expected);
294 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
295 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
296 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
297 CHECK_VAL(io.out.lease_response.lease_state,
298 smb2_util_lease_state(test.type));
299 done:
300 if (h != NULL) {
301 smb2_util_close(tree, *h);
302 }
303 smb2_util_unlink(tree, fname);
304 talloc_free(mem_ctx);
305
306 return ret;
307 }
308
309 static bool test_durable_open_open_lease(struct torture_context *tctx,
310 struct smb2_tree *tree)
311 {
312 TALLOC_CTX *mem_ctx = talloc_new(tctx);
313 char fname[256];
314 bool ret = true;
315 int i;
316 uint32_t caps;
317
318 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
319 if (!(caps & SMB2_CAP_LEASING)) {
320 torture_skip(tctx, "leases are not supported");
321 }
322
323 /* Choose a random name in case the state is left a little funky. */
324 snprintf(fname, 256, "durable_open_open_lease_%s.dat", generate_random_str(tctx, 8));
325
326 smb2_util_unlink(tree, fname);
327
328
329 /* test various oplock levels with durable open */
330
331 for (i = 0; i < NUM_LEASE_OPEN_TESTS; i++) {
332 ret = test_one_durable_open_open_lease(tctx,
333 tree,
334 fname,
335 durable_open_vs_lease_table[i]);
336 if (ret == false) {
337 goto done;
338 }
339 }
340
341 done:
342 smb2_util_unlink(tree, fname);
343 talloc_free(tree);
344 talloc_free(mem_ctx);
345
346 return ret;
347 }
348
349 /**
350 * basic test for doing a durable open
351 * and do a durable reopen on the same connection
352 * while the first open is still active (fails)
353 */
354 static bool test_durable_open_reopen1(struct torture_context *tctx,
355 struct smb2_tree *tree)
356 {
357 NTSTATUS status;
358 TALLOC_CTX *mem_ctx = talloc_new(tctx);
359 char fname[256];
360 struct smb2_handle _h;
361 struct smb2_handle *h = NULL;
362 struct smb2_create io1, io2;
363 bool ret = true;
364
365 /* Choose a random name in case the state is left a little funky. */
366 snprintf(fname, 256, "durable_open_reopen1_%s.dat",
367 generate_random_str(tctx, 8));
368
369 smb2_util_unlink(tree, fname);
370
371 smb2_oplock_create_share(&io1, fname,
372 smb2_util_share_access(""),
373 smb2_util_oplock_level("b"));
374 io1.in.durable_open = true;
375
376 status = smb2_create(tree, mem_ctx, &io1);
377 CHECK_STATUS(status, NT_STATUS_OK);
378 _h = io1.out.file.handle;
379 h = &_h;
380 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
381 CHECK_VAL(io1.out.durable_open, true);
382 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
383
384 /* try a durable reconnect while the file is still open */
385 ZERO_STRUCT(io2);
386 io2.in.fname = fname;
387 io2.in.durable_handle = h;
388
389 status = smb2_create(tree, mem_ctx, &io2);
390 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
391
392 done:
393 if (h != NULL) {
394 smb2_util_close(tree, *h);
395 }
396
397 smb2_util_unlink(tree, fname);
398
399 talloc_free(tree);
400
401 talloc_free(mem_ctx);
402
403 return ret;
404 }
405
406 /**
407 * Basic test for doing a durable open
408 * and do a session reconnect while the first
409 * session is still active and the handle is
410 * still open in the client.
411 * This closes the original session and a
412 * durable reconnect on the new session succeeds.
413 */
414 static bool test_durable_open_reopen1a(struct torture_context *tctx,
415 struct smb2_tree *tree)
416 {
417 NTSTATUS status;
418 TALLOC_CTX *mem_ctx = talloc_new(tctx);
419 char fname[256];
420 struct smb2_handle _h;
421 struct smb2_handle *h = NULL;
422 struct smb2_create io;
423 bool ret = true;
424 struct smb2_tree *tree2 = NULL;
425 struct smb2_tree *tree3 = NULL;
426 uint64_t previous_session_id;
427 struct smbcli_options options;
428 struct GUID orig_client_guid;
429
430 options = tree->session->transport->options;
431 orig_client_guid = options.client_guid;
432
433 /* Choose a random name in case the state is left a little funky. */
434 snprintf(fname, 256, "durable_open_reopen1a_%s.dat",
435 generate_random_str(tctx, 8));
436
437 smb2_util_unlink(tree, fname);
438
439 smb2_oplock_create_share(&io, fname,
440 smb2_util_share_access(""),
441 smb2_util_oplock_level("b"));
442 io.in.durable_open = true;
443
444 status = smb2_create(tree, mem_ctx, &io);
445 CHECK_STATUS(status, NT_STATUS_OK);
446 _h = io.out.file.handle;
447 h = &_h;
448 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
449 CHECK_VAL(io.out.durable_open, true);
450 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
451
452 /*
453 * a session reconnect on a second tcp connection
454 */
455
456 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
457
458 /* for oplocks, the client guid can be different: */
459 options.client_guid = GUID_random();
460
461 ret = torture_smb2_connection_ext(tctx, previous_session_id,
462 &options, &tree2);
463 torture_assert_goto(tctx, ret, ret, done, "could not reconnect");
464
465 /*
466 * check that this has deleted the old session
467 */
468
469 ZERO_STRUCT(io);
470 io.in.fname = fname;
471 io.in.durable_handle = h;
472
473 status = smb2_create(tree, mem_ctx, &io);
474 CHECK_STATUS(status, NT_STATUS_USER_SESSION_DELETED);
475
476 TALLOC_FREE(tree);
477
478 /*
479 * but a durable reconnect on the new session succeeds:
480 */
481
482 ZERO_STRUCT(io);
483 io.in.fname = fname;
484 io.in.durable_handle = h;
485
486 status = smb2_create(tree2, mem_ctx, &io);
487 CHECK_STATUS(status, NT_STATUS_OK);
488 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
489 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
490 _h = io.out.file.handle;
491 h = &_h;
492
493 /*
494 * a session reconnect on a second tcp connection
495 */
496
497 previous_session_id = smb2cli_session_current_id(tree2->session->smbXcli);
498
499 /* the original client_guid works just the same */
500 options.client_guid = orig_client_guid;
501
502 ret = torture_smb2_connection_ext(tctx, previous_session_id,
503 &options, &tree3);
504 torture_assert_goto(tctx, ret, ret, done, "could not reconnect");
505
506 /*
507 * check that this has deleted the old session
508 */
509
510 ZERO_STRUCT(io);
511 io.in.fname = fname;
512 io.in.durable_handle = h;
513
514 status = smb2_create(tree2, mem_ctx, &io);
515 CHECK_STATUS(status, NT_STATUS_USER_SESSION_DELETED);
516
517 TALLOC_FREE(tree2);
518
519 /*
520 * but a durable reconnect on the new session succeeds:
521 */
522
523 ZERO_STRUCT(io);
524 io.in.fname = fname;
525 io.in.durable_handle = h;
526
527 status = smb2_create(tree3, mem_ctx, &io);
528 CHECK_STATUS(status, NT_STATUS_OK);
529 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
530 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
531 _h = io.out.file.handle;
532 h = &_h;
533
534 done:
535 if (tree == NULL) {
536 tree = tree2;
537 }
538
539 if (tree == NULL) {
540 tree = tree3;
541 }
542
543 if (tree != NULL) {
544 if (h != NULL) {
545 smb2_util_close(tree, *h);
546 h = NULL;
547 }
548 smb2_util_unlink(tree, fname);
549
550 talloc_free(tree);
551 }
552
553 talloc_free(mem_ctx);
554
555 return ret;
556 }
557
558 /**
559 * lease variant of reopen1a
560 *
561 * Basic test for doing a durable open and doing a session
562 * reconnect while the first session is still active and the
563 * handle is still open in the client.
564 * This closes the original session and a durable reconnect on
565 * the new session succeeds depending on the client guid:
566 *
567 * Durable reconnect on a session with a different client guid fails.
568 * Durable reconnect on a session with the original client guid succeeds.
569 */
570 bool test_durable_open_reopen1a_lease(struct torture_context *tctx,
571 struct smb2_tree *tree)
572 {
573 NTSTATUS status;
574 TALLOC_CTX *mem_ctx = talloc_new(tctx);
575 char fname[256];
576 struct smb2_handle _h;
577 struct smb2_handle *h = NULL;
578 struct smb2_create io;
579 struct smb2_lease ls;
580 uint64_t lease_key;
581 bool ret = true;
582 struct smb2_tree *tree2 = NULL;
583 struct smb2_tree *tree3 = NULL;
584 uint64_t previous_session_id;
585 struct smbcli_options options;
586 struct GUID orig_client_guid;
587
588 options = tree->session->transport->options;
589 orig_client_guid = options.client_guid;
590
591 /* Choose a random name in case the state is left a little funky. */
592 snprintf(fname, 256, "durable_v2_open_reopen1a_lease_%s.dat",
593 generate_random_str(tctx, 8));
594
595 smb2_util_unlink(tree, fname);
596
597 lease_key = random();
598 smb2_lease_create(&io, &ls, false /* dir */, fname,
599 lease_key, smb2_util_lease_state("RWH"));
600 io.in.durable_open = true;
601
602 status = smb2_create(tree, mem_ctx, &io);
603 CHECK_STATUS(status, NT_STATUS_OK);
604 _h = io.out.file.handle;
605 h = &_h;
606 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
607 CHECK_VAL(io.out.durable_open, true);
608 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
609 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease_key);
610 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease_key);
611 CHECK_VAL(io.out.lease_response.lease_state,
612 smb2_util_lease_state("RWH"));
613 CHECK_VAL(io.out.lease_response.lease_flags, 0);
614 CHECK_VAL(io.out.lease_response.lease_duration, 0);
615
616 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
617
618 /*
619 * a session reconnect on a second tcp connection
620 * with a different client_guid does not allow
621 * the durable reconnect.
622 */
623
624 options.client_guid = GUID_random();
625
626 ret = torture_smb2_connection_ext(tctx, previous_session_id,
627 &options, &tree2);
628 torture_assert_goto(tctx, ret, ret, done, "couldn't reconnect");
629
630 /*
631 * check that this has deleted the old session
632 */
633
634 ZERO_STRUCT(io);
635 io.in.fname = fname;
636 io.in.durable_handle = h;
637 io.in.lease_request = &ls;
638 status = smb2_create(tree, mem_ctx, &io);
639 CHECK_STATUS(status, NT_STATUS_USER_SESSION_DELETED);
640 TALLOC_FREE(tree);
641
642
643 /*
644 * but a durable reconnect on the new session with the wrong
645 * client guid fails
646 */
647
648 ZERO_STRUCT(io);
649 io.in.fname = fname;
650 io.in.durable_handle = h;
651 io.in.lease_request = &ls;
652 status = smb2_create(tree2, mem_ctx, &io);
653 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
654
655 /*
656 * now a session reconnect on a second tcp connection
657 * with original client_guid allows the durable reconnect.
658 */
659
660 options.client_guid = orig_client_guid;
661
662 ret = torture_smb2_connection_ext(tctx, previous_session_id,
663 &options, &tree3);
664 torture_assert_goto(tctx, ret, ret, done, "couldn't reconnect");
665
666 /*
667 * check that this has deleted the old session
668 * In this case, a durable reconnect attempt with the
669 * correct client_guid yields a different error code.
670 */
671
672 ZERO_STRUCT(io);
673 io.in.fname = fname;
674 io.in.durable_handle = h;
675 io.in.lease_request = &ls;
676 status = smb2_create(tree2, mem_ctx, &io);
677 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
678 TALLOC_FREE(tree2);
679
680 /*
681 * but a durable reconnect on the new session succeeds:
682 */
683
684 ZERO_STRUCT(io);
685 io.in.fname = fname;
686 io.in.durable_handle = h;
687 io.in.lease_request = &ls;
688 status = smb2_create(tree3, mem_ctx, &io);
689 CHECK_STATUS(status, NT_STATUS_OK);
690 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
691 CHECK_VAL(io.out.durable_open, false); /* no dh response context... */
692 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
693 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease_key);
694 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease_key);
695 CHECK_VAL(io.out.lease_response.lease_state,
696 smb2_util_lease_state("RWH"));
697 CHECK_VAL(io.out.lease_response.lease_flags, 0);
698 CHECK_VAL(io.out.lease_response.lease_duration, 0);
699 _h = io.out.file.handle;
700 h = &_h;
701
702 done:
703 if (tree == NULL) {
704 tree = tree2;
705 }
706
707 if (tree == NULL) {
708 tree = tree3;
709 }
710
711 if (tree != NULL) {
712 if (h != NULL) {
713 smb2_util_close(tree, *h);
714 }
715
716 smb2_util_unlink(tree, fname);
717
718 talloc_free(tree);
719 }
720
721 talloc_free(mem_ctx);
722
723 return ret;
724 }
725
726
727 /**
728 * basic test for doing a durable open
729 * tcp disconnect, reconnect, do a durable reopen (succeeds)
730 */
731 static bool test_durable_open_reopen2(struct torture_context *tctx,
732 struct smb2_tree *tree)
733 {
734 NTSTATUS status;
735 TALLOC_CTX *mem_ctx = talloc_new(tctx);
736 char fname[256];
737 struct smb2_handle _h;
738 struct smb2_handle *h = NULL;
739 struct smb2_create io;
740 bool ret = true;
741
742 /* Choose a random name in case the state is left a little funky. */
743 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
744 generate_random_str(tctx, 8));
745
746 smb2_util_unlink(tree, fname);
747
748 smb2_oplock_create_share(&io, fname,
749 smb2_util_share_access(""),
750 smb2_util_oplock_level("b"));
751 io.in.durable_open = true;
752
753 status = smb2_create(tree, mem_ctx, &io);
754 CHECK_STATUS(status, NT_STATUS_OK);
755 _h = io.out.file.handle;
756 h = &_h;
757 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
758 CHECK_VAL(io.out.durable_open, true);
759 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
760
761 /* disconnect, leaving the durable in place */
762 TALLOC_FREE(tree);
763
764 if (!torture_smb2_connection(tctx, &tree)) {
765 torture_warning(tctx, "couldn't reconnect, bailing\n");
766 ret = false;
767 goto done;
768 }
769
770 ZERO_STRUCT(io);
771 /* the path name is ignored by the server */
772 io.in.fname = fname;
773 io.in.durable_handle = h; /* durable v1 reconnect request */
774 h = NULL;
775
776 status = smb2_create(tree, mem_ctx, &io);
777 CHECK_STATUS(status, NT_STATUS_OK);
778 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
779 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
780 _h = io.out.file.handle;
781 h = &_h;
782
783 /* disconnect again, leaving the durable in place */
784 TALLOC_FREE(tree);
785
786 if (!torture_smb2_connection(tctx, &tree)) {
787 torture_warning(tctx, "couldn't reconnect, bailing\n");
788 ret = false;
789 goto done;
790 }
791
792 /*
793 * show that the filename and many other fields
794 * are ignored. only the reconnect request blob
795 * is important.
796 */
797 ZERO_STRUCT(io);
798 /* the path name is ignored by the server */
799 io.in.security_flags = 0x78;
800 io.in.oplock_level = 0x78;
801 io.in.impersonation_level = 0x12345678;
802 io.in.create_flags = 0x12345678;
803 io.in.reserved = 0x12345678;
804 io.in.desired_access = 0x12345678;
805 io.in.file_attributes = 0x12345678;
806 io.in.share_access = 0x12345678;
807 io.in.create_disposition = 0x12345678;
808 io.in.create_options = 0x12345678;
809 io.in.fname = "__non_existing_fname__";
810 io.in.durable_handle = h; /* durable v1 reconnect request */
811 h = NULL;
812
813 status = smb2_create(tree, mem_ctx, &io);
814 CHECK_STATUS(status, NT_STATUS_OK);
815 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
816 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
817 _h = io.out.file.handle;
818 h = &_h;
819
820 /* disconnect, leaving the durable in place */
821 TALLOC_FREE(tree);
822
823 if (!torture_smb2_connection(tctx, &tree)) {
824 torture_warning(tctx, "couldn't reconnect, bailing\n");
825 ret = false;
826 goto done;
827 }
828
829 /*
830 * show that an additionally specified durable v1 request
831 * is ignored by the server.
832 * See MS-SMB2, 3.3.5.9.7
833 * Handling the SMB2_CREATE_DURABLE_HANDLE_RECONNECT Create Context
834 */
835 ZERO_STRUCT(io);
836 /* the path name is ignored by the server */
837 io.in.fname = fname;
838 io.in.durable_handle = h; /* durable v1 reconnect request */
839 io.in.durable_open = true; /* durable v1 handle request */
840 h = NULL;
841
842 status = smb2_create(tree, mem_ctx, &io);
843 CHECK_STATUS(status, NT_STATUS_OK);
844 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
845 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
846 _h = io.out.file.handle;
847 h = &_h;
848
849 done:
850 if (tree != NULL) {
851 if (h != NULL) {
852 smb2_util_close(tree, *h);
853 }
854
855 smb2_util_unlink(tree, fname);
856
857 talloc_free(tree);
858 }
859
860 talloc_free(mem_ctx);
861
862 return ret;
863 }
864
865 /**
866 * lease variant of reopen2
867 * basic test for doing a durable open
868 * tcp disconnect, reconnect, do a durable reopen (succeeds)
869 */
870 static bool test_durable_open_reopen2_lease(struct torture_context *tctx,
871 struct smb2_tree *tree)
872 {
873 NTSTATUS status;
874 TALLOC_CTX *mem_ctx = talloc_new(tctx);
875 char fname[256];
876 struct smb2_handle _h;
877 struct smb2_handle *h = NULL;
878 struct smb2_create io;
879 struct smb2_lease ls;
880 uint64_t lease_key;
881 bool ret = true;
882 struct smbcli_options options;
883 uint32_t caps;
884
885 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
886 if (!(caps & SMB2_CAP_LEASING)) {
887 torture_skip(tctx, "leases are not supported");
888 }
889
890 options = tree->session->transport->options;
891
892 /* Choose a random name in case the state is left a little funky. */
893 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
894 generate_random_str(tctx, 8));
895
896 smb2_util_unlink(tree, fname);
897
898 lease_key = random();
899 smb2_lease_create(&io, &ls, false /* dir */, fname, lease_key,
900 smb2_util_lease_state("RWH"));
901 io.in.durable_open = true;
902
903 status = smb2_create(tree, mem_ctx, &io);
904 CHECK_STATUS(status, NT_STATUS_OK);
905 _h = io.out.file.handle;
906 h = &_h;
907 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
908
909 CHECK_VAL(io.out.durable_open, true);
910 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
911 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease_key);
912 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease_key);
913 CHECK_VAL(io.out.lease_response.lease_state,
914 smb2_util_lease_state("RWH"));
915 CHECK_VAL(io.out.lease_response.lease_flags, 0);
916 CHECK_VAL(io.out.lease_response.lease_duration, 0);
917
918 /* disconnect, reconnect and then do durable reopen */
919 TALLOC_FREE(tree);
920
921 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree)) {
922 torture_warning(tctx, "couldn't reconnect, bailing\n");
923 ret = false;
924 goto done;
925 }
926
927
928 /* a few failure tests: */
929
930 /*
931 * several attempts without lease attached:
932 * all fail with NT_STATUS_OBJECT_NAME_NOT_FOUND
933 * irrespective of file name provided
934 */
935
936 ZERO_STRUCT(io);
937 io.in.fname = "";
938 io.in.durable_handle = h;
939 status = smb2_create(tree, mem_ctx, &io);
940 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
941
942 ZERO_STRUCT(io);
943 io.in.fname = "__non_existing_fname__";
944 io.in.durable_handle = h;
945 status = smb2_create(tree, mem_ctx, &io);
946 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
947
948 ZERO_STRUCT(io);
949 io.in.fname = fname;
950 io.in.durable_handle = h;
951 status = smb2_create(tree, mem_ctx, &io);
952 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
953
954 /*
955 * attempt with lease provided, but
956 * with a changed lease key. => fails
957 */
958 ZERO_STRUCT(io);
959 io.in.fname = fname;
960 io.in.durable_open = false;
961 io.in.durable_handle = h;
962 io.in.lease_request = &ls;
963 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
964 /* a wrong lease key lets the request fail */
965 ls.lease_key.data[0]++;
966
967 status = smb2_create(tree, mem_ctx, &io);
968 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
969
970 /* restore the correct lease key */
971 ls.lease_key.data[0]--;
972
973 /*
974 * this last failing attempt is almost correct:
975 * only problem is: we use the wrong filename...
976 * Note that this gives INVALID_PARAMETER.
977 * This is different from oplocks!
978 */
979 ZERO_STRUCT(io);
980 io.in.fname = "__non_existing_fname__";
981 io.in.durable_open = false;
982 io.in.durable_handle = h;
983 io.in.lease_request = &ls;
984 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
985
986 status = smb2_create(tree, mem_ctx, &io);
987 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
988
989 /*
990 * Now for a succeeding reconnect:
991 */
992
993 ZERO_STRUCT(io);
994 io.in.fname = fname;
995 io.in.durable_open = false;
996 io.in.durable_handle = h;
997 io.in.lease_request = &ls;
998 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
999
1000 /* the requested lease state is irrelevant */
1001 ls.lease_state = smb2_util_lease_state("");
1002
1003 h = NULL;
1004
1005 status = smb2_create(tree, mem_ctx, &io);
1006 CHECK_STATUS(status, NT_STATUS_OK);
1007
1008 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1009 CHECK_VAL(io.out.durable_open, false);
1010 CHECK_VAL(io.out.durable_open_v2, false); /* no dh2q response blob */
1011 CHECK_VAL(io.out.persistent_open, false);
1012 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1013 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease_key);
1014 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease_key);
1015 CHECK_VAL(io.out.lease_response.lease_state,
1016 smb2_util_lease_state("RWH"));
1017 CHECK_VAL(io.out.lease_response.lease_flags, 0);
1018 CHECK_VAL(io.out.lease_response.lease_duration, 0);
1019 _h = io.out.file.handle;
1020 h = &_h;
1021
1022 /* disconnect one more time */
1023 TALLOC_FREE(tree);
1024
1025 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree)) {
1026 torture_warning(tctx, "couldn't reconnect, bailing\n");
1027 ret = false;
1028 goto done;
1029 }
1030
1031 /*
1032 * demonstrate that various parameters are ignored
1033 * in the reconnect
1034 */
1035
1036 ZERO_STRUCT(io);
1037 /*
1038 * These are completely ignored by the server
1039 */
1040 io.in.security_flags = 0x78;
1041 io.in.oplock_level = 0x78;
1042 io.in.impersonation_level = 0x12345678;
1043 io.in.create_flags = 0x12345678;
1044 io.in.reserved = 0x12345678;
1045 io.in.desired_access = 0x12345678;
1046 io.in.file_attributes = 0x12345678;
1047 io.in.share_access = 0x12345678;
1048 io.in.create_disposition = 0x12345678;
1049 io.in.create_options = 0x12345678;
1050
1051 /*
1052 * only these are checked:
1053 * - io.in.fname
1054 * - io.in.durable_handle,
1055 * - io.in.lease_request->lease_key
1056 */
1057
1058 io.in.fname = fname;
1059 io.in.durable_open_v2 = false;
1060 io.in.durable_handle_v2 = h;
1061 io.in.lease_request = &ls;
1062
1063 /* the requested lease state is irrelevant */
1064 ls.lease_state = smb2_util_lease_state("");
1065
1066 h = NULL;
1067
1068 status = smb2_create(tree, mem_ctx, &io);
1069 CHECK_STATUS(status, NT_STATUS_OK);
1070
1071 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1072 CHECK_VAL(io.out.durable_open, false);
1073 CHECK_VAL(io.out.durable_open_v2, false); /* no dh2q response blob */
1074 CHECK_VAL(io.out.persistent_open, false);
1075 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1076 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease_key);
1077 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease_key);
1078 CHECK_VAL(io.out.lease_response.lease_state,
1079 smb2_util_lease_state("RWH"));
1080 CHECK_VAL(io.out.lease_response.lease_flags, 0);
1081 CHECK_VAL(io.out.lease_response.lease_duration, 0);
1082
1083 _h = io.out.file.handle;
1084 h = &_h;
1085
1086 done:
1087 if (tree != NULL) {
1088 if (h != NULL) {
1089 smb2_util_close(tree, *h);
1090 }
1091
1092 smb2_util_unlink(tree, fname);
1093
1094 talloc_free(tree);
1095 }
1096
1097 talloc_free(mem_ctx);
1098
1099 return ret;
1100 }
1101
1102 /**
1103 * lease v2 variant of reopen2
1104 * basic test for doing a durable open
1105 * tcp disconnect, reconnect, do a durable reopen (succeeds)
1106 */
1107 static bool test_durable_open_reopen2_lease_v2(struct torture_context *tctx,
1108 struct smb2_tree *tree)
1109 {
1110 NTSTATUS status;
1111 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1112 char fname[256];
1113 struct smb2_handle _h;
1114 struct smb2_handle *h = NULL;
1115 struct smb2_create io;
1116 struct smb2_lease ls;
1117 uint64_t lease_key;
1118 bool ret = true;
1119 struct smbcli_options options;
1120 uint32_t caps;
1121
1122 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
1123 if (!(caps & SMB2_CAP_LEASING)) {
1124 torture_skip(tctx, "leases are not supported");
1125 }
1126
1127 options = tree->session->transport->options;
1128
1129 /* Choose a random name in case the state is left a little funky. */
1130 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
1131 generate_random_str(tctx, 8));
1132
1133 smb2_util_unlink(tree, fname);
1134
1135 lease_key = random();
1136 smb2_lease_v2_create(&io, &ls, false /* dir */, fname,
1137 lease_key, 0, /* parent lease key */
1138 smb2_util_lease_state("RWH"), 0 /* lease epoch */);
1139 io.in.durable_open = true;
1140
1141 status = smb2_create(tree, mem_ctx, &io);
1142 CHECK_STATUS(status, NT_STATUS_OK);
1143 _h = io.out.file.handle;
1144 h = &_h;
1145 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1146
1147 CHECK_VAL(io.out.durable_open, true);
1148 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1149 CHECK_VAL(io.out.lease_response_v2.lease_key.data[0], lease_key);
1150 CHECK_VAL(io.out.lease_response_v2.lease_key.data[1], ~lease_key);
1151 CHECK_VAL(io.out.lease_response_v2.lease_state,
1152 smb2_util_lease_state("RWH"));
1153 CHECK_VAL(io.out.lease_response_v2.lease_flags, 0);
1154 CHECK_VAL(io.out.lease_response_v2.lease_duration, 0);
1155
1156 /* disconnect, reconnect and then do durable reopen */
1157 TALLOC_FREE(tree);
1158
1159 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree)) {
1160 torture_warning(tctx, "couldn't reconnect, bailing\n");
1161 ret = false;
1162 goto done;
1163 }
1164
1165 /* a few failure tests: */
1166
1167 /*
1168 * several attempts without lease attached:
1169 * all fail with NT_STATUS_OBJECT_NAME_NOT_FOUND
1170 * irrespective of file name provided
1171 */
1172
1173 ZERO_STRUCT(io);
1174 io.in.fname = "";
1175 io.in.durable_handle = h;
1176 status = smb2_create(tree, mem_ctx, &io);
1177 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1178
1179 ZERO_STRUCT(io);
1180 io.in.fname = "__non_existing_fname__";
1181 io.in.durable_handle = h;
1182 status = smb2_create(tree, mem_ctx, &io);
1183 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1184
1185 ZERO_STRUCT(io);
1186 io.in.fname = fname;
1187 io.in.durable_handle = h;
1188 status = smb2_create(tree, mem_ctx, &io);
1189 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1190
1191 /*
1192 * attempt with lease provided, but
1193 * with a changed lease key. => fails
1194 */
1195 ZERO_STRUCT(io);
1196 io.in.fname = fname;
1197 io.in.durable_open = false;
1198 io.in.durable_handle = h;
1199 io.in.lease_request_v2 = &ls;
1200 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
1201 /* a wrong lease key lets the request fail */
1202 ls.lease_key.data[0]++;
1203
1204 status = smb2_create(tree, mem_ctx, &io);
1205 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1206
1207 /* restore the correct lease key */
1208 ls.lease_key.data[0]--;
1209
1210 /*
1211 * this last failing attempt is almost correct:
1212 * only problem is: we use the wrong filename...
1213 * Note that this gives INVALID_PARAMETER.
1214 * This is different from oplocks!
1215 */
1216 ZERO_STRUCT(io);
1217 io.in.fname = "__non_existing_fname__";
1218 io.in.durable_open = false;
1219 io.in.durable_handle = h;
1220 io.in.lease_request_v2 = &ls;
1221 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
1222
1223 status = smb2_create(tree, mem_ctx, &io);
1224 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
1225
1226 /*
1227 * Now for a succeeding reconnect:
1228 */
1229
1230 ZERO_STRUCT(io);
1231 io.in.fname = fname;
1232 io.in.durable_open = false;
1233 io.in.durable_handle = h;
1234 io.in.lease_request_v2 = &ls;
1235 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
1236
1237 /* the requested lease state is irrelevant */
1238 ls.lease_state = smb2_util_lease_state("");
1239
1240 h = NULL;
1241
1242 status = smb2_create(tree, mem_ctx, &io);
1243 CHECK_STATUS(status, NT_STATUS_OK);
1244
1245 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1246 CHECK_VAL(io.out.durable_open, false);
1247 CHECK_VAL(io.out.durable_open_v2, false); /* no dh2q response blob */
1248 CHECK_VAL(io.out.persistent_open, false);
1249 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1250 CHECK_VAL(io.out.lease_response_v2.lease_key.data[0], lease_key);
1251 CHECK_VAL(io.out.lease_response_v2.lease_key.data[1], ~lease_key);
1252 CHECK_VAL(io.out.lease_response_v2.lease_state,
1253 smb2_util_lease_state("RWH"));
1254 CHECK_VAL(io.out.lease_response_v2.lease_flags, 0);
1255 CHECK_VAL(io.out.lease_response_v2.lease_duration, 0);
1256 _h = io.out.file.handle;
1257 h = &_h;
1258
1259 /* disconnect one more time */
1260 TALLOC_FREE(tree);
1261
1262 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree)) {
1263 torture_warning(tctx, "couldn't reconnect, bailing\n");
1264 ret = false;
1265 goto done;
1266 }
1267
1268 /*
1269 * demonstrate that various parameters are ignored
1270 * in the reconnect
1271 */
1272
1273 ZERO_STRUCT(io);
1274 /*
1275 * These are completely ignored by the server
1276 */
1277 io.in.security_flags = 0x78;
1278 io.in.oplock_level = 0x78;
1279 io.in.impersonation_level = 0x12345678;
1280 io.in.create_flags = 0x12345678;
1281 io.in.reserved = 0x12345678;
1282 io.in.desired_access = 0x12345678;
1283 io.in.file_attributes = 0x12345678;
1284 io.in.share_access = 0x12345678;
1285 io.in.create_disposition = 0x12345678;
1286 io.in.create_options = 0x12345678;
1287
1288 /*
1289 * only these are checked:
1290 * - io.in.fname
1291 * - io.in.durable_handle,
1292 * - io.in.lease_request->lease_key
1293 */
1294
1295 io.in.fname = fname;
1296 io.in.durable_open_v2 = false;
1297 io.in.durable_handle_v2 = h;
1298 io.in.lease_request_v2 = &ls;
1299
1300 /* the requested lease state is irrelevant */
1301 ls.lease_state = smb2_util_lease_state("");
1302
1303 h = NULL;
1304
1305 status = smb2_create(tree, mem_ctx, &io);
1306 CHECK_STATUS(status, NT_STATUS_OK);
1307
1308 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1309 CHECK_VAL(io.out.durable_open, false);
1310 CHECK_VAL(io.out.durable_open_v2, false); /* no dh2q response blob */
1311 CHECK_VAL(io.out.persistent_open, false);
1312 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1313 CHECK_VAL(io.out.lease_response_v2.lease_key.data[0], lease_key);
1314 CHECK_VAL(io.out.lease_response_v2.lease_key.data[1], ~lease_key);
1315 CHECK_VAL(io.out.lease_response_v2.lease_state,
1316 smb2_util_lease_state("RWH"));
1317 CHECK_VAL(io.out.lease_response_v2.lease_flags, 0);
1318 CHECK_VAL(io.out.lease_response_v2.lease_duration, 0);
1319
1320 _h = io.out.file.handle;
1321 h = &_h;
1322
1323 done:
1324 if (tree != NULL) {
1325 if (h != NULL) {
1326 smb2_util_close(tree, *h);
1327 }
1328
1329 smb2_util_unlink(tree, fname);
1330
1331 talloc_free(tree);
1332 }
1333
1334 talloc_free(mem_ctx);
1335
1336 return ret;
1337 }
1338
1339 /**
1340 * basic test for doing a durable open
1341 * tcp disconnect, reconnect with a session reconnect and
1342 * do a durable reopen (succeeds)
1343 */
1344 static bool test_durable_open_reopen2a(struct torture_context *tctx,
1345 struct smb2_tree *tree)
1346 {
1347 NTSTATUS status;
1348 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1349 char fname[256];
1350 struct smb2_handle _h;
1351 struct smb2_handle *h = NULL;
1352 struct smb2_create io1, io2;
1353 uint64_t previous_session_id;
1354 bool ret = true;
1355 struct smbcli_options options;
1356
1357 options = tree->session->transport->options;
1358
1359 /* Choose a random name in case the state is left a little funky. */
1360 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
1361 generate_random_str(tctx, 8));
1362
1363 smb2_util_unlink(tree, fname);
1364
1365 smb2_oplock_create_share(&io1, fname,
1366 smb2_util_share_access(""),
1367 smb2_util_oplock_level("b"));
1368 io1.in.durable_open = true;
1369
1370 status = smb2_create(tree, mem_ctx, &io1);
1371 CHECK_STATUS(status, NT_STATUS_OK);
1372 _h = io1.out.file.handle;
1373 h = &_h;
1374 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1375 CHECK_VAL(io1.out.durable_open, true);
1376 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
1377
1378 /* disconnect, reconnect and then do durable reopen */
1379 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1380 talloc_free(tree);
1381 tree = NULL;
1382
1383 if (!torture_smb2_connection_ext(tctx, previous_session_id,
1384 &options, &tree))
1385 {
1386 torture_warning(tctx, "couldn't reconnect, bailing\n");
1387 ret = false;
1388 goto done;
1389 }
1390
1391 ZERO_STRUCT(io2);
1392 io2.in.fname = fname;
1393 io2.in.durable_handle = h;
1394 h = NULL;
1395
1396 status = smb2_create(tree, mem_ctx, &io2);
1397 CHECK_STATUS(status, NT_STATUS_OK);
1398 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1399 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
1400 _h = io2.out.file.handle;
1401 h = &_h;
1402
1403 done:
1404 if (tree != NULL) {
1405 if (h != NULL) {
1406 smb2_util_close(tree, *h);
1407 }
1408
1409 smb2_util_unlink(tree, fname);
1410
1411 talloc_free(tree);
1412 }
1413
1414 talloc_free(mem_ctx);
1415
1416 return ret;
1417 }
1418
1419
1420 /**
1421 * basic test for doing a durable open:
1422 * tdis, new tcon, try durable reopen (fails)
1423 */
1424 static bool test_durable_open_reopen3(struct torture_context *tctx,
1425 struct smb2_tree *tree)
1426 {
1427 NTSTATUS status;
1428 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1429 char fname[256];
1430 struct smb2_handle _h;
1431 struct smb2_handle *h = NULL;
1432 struct smb2_create io1, io2;
1433 bool ret = true;
1434 struct smb2_tree *tree2 = NULL;
1435
1436 /* Choose a random name in case the state is left a little funky. */
1437 snprintf(fname, 256, "durable_open_reopen3_%s.dat",
1438 generate_random_str(tctx, 8));
1439
1440 smb2_util_unlink(tree, fname);
1441
1442 smb2_oplock_create_share(&io1, fname,
1443 smb2_util_share_access(""),
1444 smb2_util_oplock_level("b"));
1445 io1.in.durable_open = true;
1446
1447 status = smb2_create(tree, mem_ctx, &io1);
1448 CHECK_STATUS(status, NT_STATUS_OK);
1449 _h = io1.out.file.handle;
1450 h = &_h;
1451 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1452 CHECK_VAL(io1.out.durable_open, true);
1453 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
1454
1455 /* disconnect, reconnect and then do durable reopen */
1456 status = smb2_tdis(tree);
1457 CHECK_STATUS(status, NT_STATUS_OK);
1458
1459 if (!torture_smb2_tree_connect(tctx, tree->session, mem_ctx, &tree2)) {
1460 torture_warning(tctx, "couldn't reconnect to share, bailing\n");
1461 ret = false;
1462 goto done;
1463 }
1464
1465
1466 ZERO_STRUCT(io2);
1467 io2.in.fname = fname;
1468 io2.in.durable_handle = h;
1469
1470 status = smb2_create(tree2, mem_ctx, &io2);
1471 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1472
1473 done:
1474 if (tree != NULL) {
1475 if (h != NULL) {
1476 smb2_util_close(tree, *h);
1477 }
1478
1479 smb2_util_unlink(tree2, fname);
1480
1481 talloc_free(tree);
1482 }
1483
1484 talloc_free(mem_ctx);
1485
1486 return ret;
1487 }
1488
1489 /**
1490 * basic test for doing a durable open:
1491 * logoff, create a new session, do a durable reopen (succeeds)
1492 */
1493 static bool test_durable_open_reopen4(struct torture_context *tctx,
1494 struct smb2_tree *tree)
1495 {
1496 NTSTATUS status;
1497 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1498 char fname[256];
1499 struct smb2_handle _h;
1500 struct smb2_handle *h = NULL;
1501 struct smb2_create io1, io2;
1502 bool ret = true;
1503 struct smb2_transport *transport = NULL;
1504 struct smb2_session *session2 = NULL;
1505 struct smb2_tree *tree2 = NULL;
1506
1507 /* Choose a random name in case the state is left a little funky. */
1508 snprintf(fname, 256, "durable_open_reopen4_%s.dat",
1509 generate_random_str(tctx, 8));
1510
1511 smb2_util_unlink(tree, fname);
1512
1513 smb2_oplock_create_share(&io1, fname,
1514 smb2_util_share_access(""),
1515 smb2_util_oplock_level("b"));
1516 io1.in.durable_open = true;
1517
1518 status = smb2_create(tree, mem_ctx, &io1);
1519 CHECK_STATUS(status, NT_STATUS_OK);
1520 _h = io1.out.file.handle;
1521 h = &_h;
1522 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1523 CHECK_VAL(io1.out.durable_open, true);
1524 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
1525
1526 /*
1527 * do a session logoff, establish a new session and tree
1528 * connect on the same transport, and try a durable reopen
1529 */
1530 transport = tree->session->transport;
1531 status = smb2_logoff(tree->session);
1532 CHECK_STATUS(status, NT_STATUS_OK);
1533
1534 if (!torture_smb2_session_setup(tctx, transport,
1535 0, /* previous_session_id */
1536 mem_ctx, &session2))
1537 {
1538 torture_warning(tctx, "session setup failed.\n");
1539 ret = false;
1540 goto done;
1541 }
1542
1543 /*
1544 * the session setup has talloc-stolen the transport,
1545 * so we can safely free the old tree+session for clarity
1546 */
1547 TALLOC_FREE(tree);
1548
1549 if (!torture_smb2_tree_connect(tctx, session2, mem_ctx, &tree2)) {
1550 torture_warning(tctx, "tree connect failed.\n");
1551 ret = false;
1552 goto done;
1553 }
1554
1555 ZERO_STRUCT(io2);
1556 io2.in.fname = fname;
1557 io2.in.durable_handle = h;
1558 h = NULL;
1559
1560 status = smb2_create(tree2, mem_ctx, &io2);
1561 CHECK_STATUS(status, NT_STATUS_OK);
1562
1563 _h = io2.out.file.handle;
1564 h = &_h;
1565 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1566 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
1567
1568 done:
1569 if (tree != NULL) {
1570 if (h != NULL) {
1571 smb2_util_close(tree2, *h);
1572 }
1573
1574 smb2_util_unlink(tree2, fname);
1575
1576 talloc_free(tree);
1577 }
1578
1579 talloc_free(mem_ctx);
1580
1581 return ret;
1582 }
1583
1584 /*
1585 * Test for failing RH reconnect not clobbering the open record state of a DH
1586 * handle of another process:
1587 * 1. Get a DH handle in tree and keep tree connected for now
1588 * 2. New connection tree2 that attempts a DH reconnect. This fails as the
1589 * handle is still connected in "tree".
1590 * 3. Disconnect "tree": this stores the DH state on disk, ready for reconnect.
1591 * 4. Disconnect "tree2": this overwrites the on-disk DH state
1592 * 5. Connect "tree3" and attempt a DH reconnect. This fails as the on-disk
1593 * DH state was clobbered in step 4.
1594 */
1595 static bool test_durable_open_reopen5(struct torture_context *tctx,
1596 struct smb2_tree *tree)
1597 {
1598 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1599 char fname[256];
1600 struct smb2_handle _h;
1601 struct smb2_handle *h = NULL;
1602 struct smb2_create io1, io2;
1603 struct smb2_tree *tree2 = NULL;
1604 struct smb2_tree *tree3 = NULL;
1605 struct smbcli_options options = tree->session->transport->options;
1606 struct GUID orig_client_guid = options.client_guid;
1607 bool ret = true;
1608 NTSTATUS status;
1609
1610 /* Choose a random name in case the state is left a little funky. */
1611 snprintf(fname, 256, "durable_open_reopen4_%s.dat",
1612 generate_random_str(tctx, 8));
1613
1614 smb2_util_unlink(tree, fname);
1615
1616 smb2_oplock_create_share(&io1, fname,
1617 smb2_util_share_access(""),
1618 smb2_util_oplock_level("b"));
1619 io1.in.durable_open = true;
1620
1621 status = smb2_create(tree, mem_ctx, &io1);
1622 CHECK_STATUS(status, NT_STATUS_OK);
1623 _h = io1.out.file.handle;
1624 h = &_h;
1625 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1626 CHECK_VAL(io1.out.durable_open, true);
1627 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
1628
1629 /*
1630 * A second connection: again issue a DH reconnect and see what happens,
1631 * should fail, as the handle is currently active in tree.
1632 */
1633
1634 options.client_guid = GUID_random();
1635
1636 ret = torture_smb2_connection_ext(tctx, 0, &options, &tree2);
1637 torture_assert_goto(tctx, ret, ret, done,
1638 "torture_smb2_connection_ext failed\n");
1639
1640 ZERO_STRUCT(io2);
1641 io2.in.fname = fname;
1642 io2.in.durable_handle = h;
1643
1644 status = smb2_create(tree2, mem_ctx, &io2);
1645 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1646
1647 /*
1648 * Now disconnect the first connection which currently has the handle
1649 * open (tree), this prepares the on-disk DH state for reconnect
1650 */
1651
1652 TALLOC_FREE(tree);
1653
1654 /*
1655 * Disconnect tree2 which had the failing DH reconnect above. As the
1656 * open succeeded and has an smbXsrv_open_destructor(), once we free
1657 * tree2 the destructor will overwrite the record with a cookie where
1658 * cookie.allow_reconnect is false, which causes the DH reconnect to
1659 * fail in the VFS.
1660 */
1661
1662 sleep(3);
1663 TALLOC_FREE(tree2);
1664
1665 options.client_guid = orig_client_guid;
1666
1667 ret = torture_smb2_connection_ext(tctx, 0, &options, &tree3);
1668 torture_assert_goto(tctx, ret, ret, done,
1669 "torture_smb2_connection_ext failed\n");
1670
1671 ZERO_STRUCT(io2);
1672 io2.in.fname = fname;
1673 io2.in.durable_handle = h;
1674 h = NULL;
1675
1676 status = smb2_create(tree3, mem_ctx, &io2);
1677 CHECK_STATUS(status, NT_STATUS_OK);
1678
1679 TALLOC_FREE(tree3);
1680
1681 done:
1682 if (tree != NULL) {
1683 if (h != NULL) {
1684 smb2_util_close(tree, *h);
1685 }
1686 smb2_util_unlink(tree, fname);
1687 }
1688 if (tree3 != NULL) {
1689 if (h != NULL) {
1690 smb2_util_close(tree, *h);
1691 }
1692 smb2_util_unlink(tree3, fname);
1693 TALLOC_FREE(tree3);
1694 }
1695
1696 talloc_free(mem_ctx);
1697
1698 return ret;
1699 }
1700
1701 /**
1702 * basic test for doing a durable open:
1703 * logoff, create a new session, do a durable reopen (fails with EACCESS)
1704 */
1705 static bool test_durable_open_reopen6(struct torture_context *tctx,
1706 struct smb2_tree *tree)
1707 {
1708 const char *host = torture_setting_string(tctx, "host", NULL);
1709 const char *share = torture_setting_string(tctx, "share", NULL);
1710 struct cli_credentials *creds2 = torture_user2_credentials(tctx, tctx);
1711 struct smbcli_options options = tree->session->transport->options;
1712 struct smb2_tree *tree2 = NULL;
1713 char fname[256];
1714 struct smb2_handle h, d;
1715 struct smb2_create c;
1716 NTSTATUS status;
1717 bool anon;
1718 bool ret = true;
1719
1720 torture_assert(tctx, (creds2 != NULL), "talloc error");
1721 anon = cli_credentials_is_anonymous(creds2);
1722 if (anon) {
1723 torture_skip(tctx, "valid user2 credentials are required");
1724 }
1725
1726 /* Choose a random name in case the state is left a little funky. */
1727 snprintf(fname, 256, "durable_open_reopen4_%s.dat",
1728 generate_random_str(tctx, 8));
1729
1730 smb2_util_unlink(tree, fname);
1731
1732 smb2_oplock_create_share(&c, fname,
1733 smb2_util_share_access(""),
1734 smb2_util_oplock_level("b"));
1735 c.in.durable_open = true;
1736
1737 status = smb2_create(tree, tree, &c);
1738 CHECK_STATUS(status, NT_STATUS_OK);
1739 h = d = c.out.file.handle;
1740
1741 CHECK_CREATED(&c, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1742 CHECK_VAL(c.out.durable_open, true);
1743 CHECK_VAL(c.out.oplock_level, smb2_util_oplock_level("b"));
1744
1745 /* Disconnect */
1746 TALLOC_FREE(tree);
1747 ZERO_STRUCT(h);
1748
1749 /* Reconnect with a different user */
1750 status = smb2_connect(tctx,
1751 host,
1752 share,
1753 tctx->lp_ctx,
1754 lpcfg_resolve_context(tctx->lp_ctx),
1755 creds2,
1756 &tree2,
1757 tctx->ev,
1758 &options,
1759 lpcfg_socket_options(tctx->lp_ctx),
1760 lpcfg_gensec_settings(tctx, tctx->lp_ctx)
1761 );
1762 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1763 "smb2_connect failed");
1764
1765 ZERO_STRUCT(c);
1766 c.in.fname = fname;
1767 c.in.durable_handle = &d;
1768
1769 status = smb2_create(tree2, tree2, &c);
1770 torture_assert_ntstatus_equal_goto(tctx, status, NT_STATUS_ACCESS_DENIED,
1771 ret, done, "failed\n");
1772
1773 ret = torture_smb2_connection_ext(tctx, 0, &options, &tree);
1774 torture_assert_goto(tctx, ret, ret, done,
1775 "torture_smb2_connection_ext failed\n");
1776
1777 status = smb2_create(tree, tree, &c);
1778 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1779 "smb2_create failed\n");
1780 h = c.out.file.handle;
1781
1782 done:
1783 if (tree != NULL) {
1784 if (!smb2_util_handle_empty(h)) {
1785 smb2_util_close(tree, h);
1786 }
1787 smb2_util_unlink(tree, fname);
1788 talloc_free(tree);
1789 }
1790
1791 talloc_free(tree2);
1792 return ret;
1793 }
1794
1795 static bool test_durable_open_delete_on_close1(struct torture_context *tctx,
1796 struct smb2_tree *tree)
1797 {
1798 NTSTATUS status;
1799 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1800 char fname[256];
1801 struct smb2_handle _h;
1802 struct smb2_handle *h = NULL;
1803 struct smb2_create io1, io2;
1804 bool ret = true;
1805 uint8_t b = 0;
1806
1807 /* Choose a random name in case the state is left a little funky. */
1808 snprintf(fname, 256, "durable_open_delete_on_close1_%s.dat",
1809 generate_random_str(tctx, 8));
1810
1811 smb2_util_unlink(tree, fname);
1812
1813 smb2_oplock_create_share(&io1, fname,
1814 smb2_util_share_access(""),
1815 smb2_util_oplock_level("b"));
1816 io1.in.durable_open = true;
1817 io1.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
1818
1819 status = smb2_create(tree, mem_ctx, &io1);
1820 CHECK_STATUS(status, NT_STATUS_OK);
1821 _h = io1.out.file.handle;
1822 h = &_h;
1823 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1824 CHECK_VAL(io1.out.durable_open, true);
1825 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
1826
1827 status = smb2_util_write(tree, *h, &b, 0, 1);
1828 CHECK_STATUS(status, NT_STATUS_OK);
1829
1830 /* disconnect, leaving the durable handle in place */
1831 TALLOC_FREE(tree);
1832
1833 if (!torture_smb2_connection(tctx, &tree)) {
1834 torture_warning(tctx, "could not reconnect, bailing\n");
1835 ret = false;
1836 goto done;
1837 }
1838
1839 /*
1840 * Open the file on the new connection again
1841 * and check that it has been newly created,
1842 * i.e. delete on close was effective on the disconnected handle.
1843 * Also check that the file is really empty,
1844 * the previously written byte gone.
1845 */
1846 smb2_oplock_create_share(&io2, fname,
1847 smb2_util_share_access(""),
1848 smb2_util_oplock_level("b"));
1849 io2.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
1850
1851 status = smb2_create(tree, mem_ctx, &io2);
1852 CHECK_STATUS(status, NT_STATUS_OK);
1853 _h = io2.out.file.handle;
1854 h = &_h;
1855 CHECK_CREATED_SIZE(&io2, CREATED, FILE_ATTRIBUTE_ARCHIVE, 0, 0);
1856 CHECK_VAL(io2.out.durable_open, false);
1857 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
1858
1859 done:
1860 if (tree != NULL) {
1861 if (h != NULL) {
1862 smb2_util_close(tree, *h);
1863 }
1864
1865 smb2_util_unlink(tree, fname);
1866
1867 talloc_free(tree);
1868 }
1869
1870 talloc_free(mem_ctx);
1871
1872 return ret;
1873 }
1874
1875
1876 static bool test_durable_open_delete_on_close2(struct torture_context *tctx,
1877 struct smb2_tree *tree)
1878 {
1879 NTSTATUS status;
1880 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1881 char fname[256];
1882 struct smb2_handle _h;
1883 struct smb2_handle *h = NULL;
1884 struct smb2_create io;
1885 bool ret = true;
1886 uint8_t b = 0;
1887 uint64_t previous_session_id;
1888 uint64_t alloc_size_step;
1889 struct smbcli_options options;
1890
1891 options = tree->session->transport->options;
1892
1893 /* Choose a random name in case the state is left a little funky. */
1894 snprintf(fname, 256, "durable_open_delete_on_close2_%s.dat",
1895 generate_random_str(tctx, 8));
1896
1897 smb2_util_unlink(tree, fname);
1898
1899 smb2_oplock_create_share(&io, fname,
1900 smb2_util_share_access(""),
1901 smb2_util_oplock_level("b"));
1902 io.in.durable_open = true;
1903 io.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
1904
1905 status = smb2_create(tree, mem_ctx, &io);
1906 CHECK_STATUS(status, NT_STATUS_OK);
1907 _h = io.out.file.handle;
1908 h = &_h;
1909 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1910 CHECK_VAL(io.out.durable_open, true);
1911 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1912
1913 status = smb2_util_write(tree, *h, &b, 0, 1);
1914 CHECK_STATUS(status, NT_STATUS_OK);
1915
1916 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1917
1918 /* disconnect, leaving the durable handle in place */
1919 TALLOC_FREE(tree);
1920
1921 if (!torture_smb2_connection_ext(tctx, previous_session_id,
1922 &options, &tree))
1923 {
1924 torture_warning(tctx, "could not reconnect, bailing\n");
1925 ret = false;
1926 goto done;
1927 }
1928
1929 ZERO_STRUCT(io);
1930 io.in.fname = fname;
1931 io.in.durable_handle = h;
1932
1933 status = smb2_create(tree, mem_ctx, &io);
1934 CHECK_STATUS(status, NT_STATUS_OK);
1935 _h = io.out.file.handle;
1936 h = &_h;
1937 alloc_size_step = io.out.alloc_size;
1938 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE, alloc_size_step, 1);
1939 CHECK_VAL(io.out.durable_open, false);
1940 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1941
1942 /* close the file, thereby deleting it */
1943 smb2_util_close(tree, *h);
1944 status = smb2_logoff(tree->session);
1945 TALLOC_FREE(tree);
1946
1947 if (!torture_smb2_connection(tctx, &tree)) {
1948 torture_warning(tctx, "could not reconnect, bailing\n");
1949 ret = false;
1950 goto done;
1951 }
1952
1953 /*
1954 * Open the file on the new connection again
1955 * and check that it has been newly created,
1956 * i.e. delete on close was effective on the reconnected handle.
1957 * Also check that the file is really empty,
1958 * the previously written byte gone.
1959 */
1960 smb2_oplock_create_share(&io, fname,
1961 smb2_util_share_access(""),
1962 smb2_util_oplock_level("b"));
1963 io.in.durable_open = true;
1964 io.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
1965
1966 status = smb2_create(tree, mem_ctx, &io);
1967 CHECK_STATUS(status, NT_STATUS_OK);
1968 _h = io.out.file.handle;
1969 h = &_h;
1970 CHECK_CREATED_SIZE(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE, 0, 0);
1971 CHECK_VAL(io.out.durable_open, true);
1972 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1973
1974 done:
1975 if (tree != NULL) {
1976 if (h != NULL) {
1977 smb2_util_close(tree, *h);
1978 }
1979
1980 smb2_util_unlink(tree, fname);
1981
1982 talloc_free(tree);
1983 }
1984
1985 talloc_free(mem_ctx);
1986
1987 return ret;
1988 }
1989
1990 /*
1991 basic testing of SMB2 durable opens
1992 regarding the position information on the handle
1993 */
1994 static bool test_durable_open_file_position(struct torture_context *tctx,
1995 struct smb2_tree *tree)
1996 {
1997 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1998 struct smb2_handle h;
1999 struct smb2_create io;
2000 NTSTATUS status;
2001 const char *fname = "durable_open_position.dat";
2002 union smb_fileinfo qfinfo;
2003 union smb_setfileinfo sfinfo;
2004 bool ret = true;
2005 uint64_t pos;
2006 uint64_t previous_session_id;
2007 struct smbcli_options options;
2008
2009 options = tree->session->transport->options;
2010
2011 smb2_util_unlink(tree, fname);
2012
2013 smb2_oplock_create(&io, fname, SMB2_OPLOCK_LEVEL_BATCH);
2014 io.in.durable_open = true;
2015
2016 status = smb2_create(tree, mem_ctx, &io);
2017 CHECK_STATUS(status, NT_STATUS_OK);
2018 h = io.out.file.handle;
2019 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2020 CHECK_VAL(io.out.durable_open, true);
2021 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
2022
2023 /* TODO: check extra blob content */
2024
2025 ZERO_STRUCT(qfinfo);
2026 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
2027 qfinfo.generic.in.file.handle = h;
2028 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
2029 CHECK_STATUS(status, NT_STATUS_OK);
2030 CHECK_VAL(qfinfo.position_information.out.position, 0);
2031 pos = qfinfo.position_information.out.position;
2032 torture_comment(tctx, "position: %llu\n",
2033 (unsigned long long)pos);
2034
2035 ZERO_STRUCT(sfinfo);
2036 sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
2037 sfinfo.generic.in.file.handle = h;
2038 sfinfo.position_information.in.position = 0x1000;
2039 status = smb2_setinfo_file(tree, &sfinfo);
2040 CHECK_STATUS(status, NT_STATUS_OK);
2041
2042 ZERO_STRUCT(qfinfo);
2043 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
2044 qfinfo.generic.in.file.handle = h;
2045 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
2046 CHECK_STATUS(status, NT_STATUS_OK);
2047 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
2048 pos = qfinfo.position_information.out.position;
2049 torture_comment(tctx, "position: %llu\n",
2050 (unsigned long long)pos);
2051
2052 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
2053
2054 /* tcp disconnect */
2055 talloc_free(tree);
2056 tree = NULL;
2057
2058 /* do a session reconnect */
2059 if (!torture_smb2_connection_ext(tctx, previous_session_id,
2060 &options, &tree))
2061 {
2062 torture_warning(tctx, "couldn't reconnect, bailing\n");
2063 ret = false;
2064 goto done;
2065 }
2066
2067 ZERO_STRUCT(qfinfo);
2068 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
2069 qfinfo.generic.in.file.handle = h;
2070 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
2071 CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
2072
2073 ZERO_STRUCT(io);
2074 io.in.fname = fname;
2075 io.in.durable_handle = &h;
2076
2077 status = smb2_create(tree, mem_ctx, &io);
2078 CHECK_STATUS(status, NT_STATUS_OK);
2079 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
2080 CHECK_VAL(io.out.reserved, 0x00);
2081 CHECK_VAL(io.out.create_action, NTCREATEX_ACTION_EXISTED);
2082 CHECK_VAL(io.out.size, 0);
2083 CHECK_VAL(io.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
2084 CHECK_VAL(io.out.reserved2, 0);
2085
2086 h = io.out.file.handle;
2087
2088 ZERO_STRUCT(qfinfo);
2089 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
2090 qfinfo.generic.in.file.handle = h;
2091 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
2092 CHECK_STATUS(status, NT_STATUS_OK);
2093 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
2094 pos = qfinfo.position_information.out.position;
2095 torture_comment(tctx, "position: %llu\n",
2096 (unsigned long long)pos);
2097
2098 smb2_util_close(tree, h);
2099
2100 talloc_free(mem_ctx);
2101
2102 smb2_util_unlink(tree, fname);
2103
2104 done:
2105 talloc_free(tree);
2106
2107 return ret;
2108 }
2109
2110 /*
2111 Open, disconnect, oplock break, reconnect.
2112 */
2113 static bool test_durable_open_oplock(struct torture_context *tctx,
2114 struct smb2_tree *tree1,
2115 struct smb2_tree *tree2)
2116 {
2117 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2118 struct smb2_create io1, io2;
2119 struct smb2_handle h1 = {{0}};
2120 struct smb2_handle h2 = {{0}};
2121 NTSTATUS status;
2122 char fname[256];
2123 bool ret = true;
2124
2125 /* Choose a random name in case the state is left a little funky. */
2126 snprintf(fname, 256, "durable_open_oplock_%s.dat", generate_random_str(tctx, 8));
2127
2128 /* Clean slate */
2129 smb2_util_unlink(tree1, fname);
2130
2131 /* Create with batch oplock */
2132 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
2133 io1.in.durable_open = true;
2134
2135 io2 = io1;
2136 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
2137
2138 status = smb2_create(tree1, mem_ctx, &io1);
2139 CHECK_STATUS(status, NT_STATUS_OK);
2140 h1 = io1.out.file.handle;
2141 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2142 CHECK_VAL(io1.out.durable_open, true);
2143 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
2144
2145 /* Disconnect after getting the batch */
2146 talloc_free(tree1);
2147 tree1 = NULL;
2148
2149 /*
2150 * Windows7 (build 7000) will break a batch oplock immediately if the
2151 * original client is gone. (ZML: This seems like a bug. It should give
2152 * some time for the client to reconnect!)
2153 */
2154 status = smb2_create(tree2, mem_ctx, &io2);
2155 CHECK_STATUS(status, NT_STATUS_OK);
2156 h2 = io2.out.file.handle;
2157 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
2158 CHECK_VAL(io2.out.durable_open, true);
2159 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
2160
2161 /* What if tree1 tries to come back and reclaim? */
2162 if (!torture_smb2_connection(tctx, &tree1)) {
2163 torture_warning(tctx, "couldn't reconnect, bailing\n");
2164 ret = false;
2165 goto done;
2166 }
2167
2168 ZERO_STRUCT(io1);
2169 io1.in.fname = fname;
2170 io1.in.durable_handle = &h1;
2171
2172 status = smb2_create(tree1, mem_ctx, &io1);
2173 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
2174
2175 done:
2176 smb2_util_close(tree2, h2);
2177 smb2_util_unlink(tree2, fname);
2178
2179 talloc_free(tree1);
2180 talloc_free(tree2);
2181
2182 return ret;
2183 }
2184
2185 /*
2186 Open, disconnect, lease break, reconnect.
2187 */
2188 static bool test_durable_open_lease(struct torture_context *tctx,
2189 struct smb2_tree *tree1,
2190 struct smb2_tree *tree2)
2191 {
2192 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2193 struct smb2_create io1, io2;
2194 struct smb2_lease ls1, ls2;
2195 struct smb2_handle h1 = {{0}};
2196 struct smb2_handle h2 = {{0}};
2197 NTSTATUS status;
2198 char fname[256];
2199 bool ret = true;
2200 uint64_t lease1, lease2;
2201 uint32_t caps;
2202
2203 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
2204 if (!(caps & SMB2_CAP_LEASING)) {
2205 torture_skip(tctx, "leases are not supported");
2206 }
2207
2208 /*
2209 * Choose a random name and random lease in case the state is left a
2210 * little funky.
2211 */
2212 lease1 = random();
2213 lease2 = random();
2214 snprintf(fname, 256, "durable_open_lease_%s.dat", generate_random_str(tctx, 8));
2215
2216 /* Clean slate */
2217 smb2_util_unlink(tree1, fname);
2218
2219 /* Create with lease */
2220 smb2_lease_create(&io1, &ls1, false /* dir */, fname,
2221 lease1, smb2_util_lease_state("RHW"));
2222 io1.in.durable_open = true;
2223
2224 smb2_lease_create(&io2, &ls2, false /* dir */, fname,
2225 lease2, smb2_util_lease_state("RHW"));
2226 io2.in.durable_open = true;
2227 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
2228
2229 status = smb2_create(tree1, mem_ctx, &io1);
2230 CHECK_STATUS(status, NT_STATUS_OK);
2231 h1 = io1.out.file.handle;
2232 CHECK_VAL(io1.out.durable_open, true);
2233 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2234
2235 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
2236 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease1);
2237 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease1);
2238 CHECK_VAL(io1.out.lease_response.lease_state,
2239 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
2240
2241 /* Disconnect after getting the lease */
2242 talloc_free(tree1);
2243 tree1 = NULL;
2244
2245 /*
2246 * Windows7 (build 7000) will grant an RH lease immediate (not an RHW?)
2247 * even if the original client is gone. (ZML: This seems like a bug. It
2248 * should give some time for the client to reconnect! And why RH?)
2249 *
2250 * obnox: Current windows 7 and w2k8r2 grant RHW instead of RH.
2251 * Test is adapted accordingly.
2252 */
2253 status = smb2_create(tree2, mem_ctx, &io2);
2254 CHECK_STATUS(status, NT_STATUS_OK);
2255 h2 = io2.out.file.handle;
2256 CHECK_VAL(io2.out.durable_open, true);
2257 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
2258
2259 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
2260 CHECK_VAL(io2.out.lease_response.lease_key.data[0], lease2);
2261 CHECK_VAL(io2.out.lease_response.lease_key.data[1], ~lease2);
2262 CHECK_VAL(io2.out.lease_response.lease_state,
2263 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
2264
2265 /* What if tree1 tries to come back and reclaim? */
2266 if (!torture_smb2_connection(tctx, &tree1)) {
2267 torture_warning(tctx, "couldn't reconnect, bailing\n");
2268 ret = false;
2269 goto done;
2270 }
2271
2272 ZERO_STRUCT(io1);
2273 io1.in.fname = fname;
2274 io1.in.durable_handle = &h1;
2275 io1.in.lease_request = &ls1;
2276
2277 status = smb2_create(tree1, mem_ctx, &io1);
2278 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
2279
2280 done:
2281 smb2_util_close(tree2, h2);
2282 smb2_util_unlink(tree2, fname);
2283
2284 talloc_free(tree1);
2285 talloc_free(tree2);
2286
2287 return ret;
2288 }
2289
2290 static bool test_durable_open_lock_oplock(struct torture_context *tctx,
2291 struct smb2_tree *tree)
2292 {
2293 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2294 struct smb2_create io;
2295 struct smb2_handle h = {{0}};
2296 struct smb2_lock lck;
2297 struct smb2_lock_element el[2];
2298 NTSTATUS status;
2299 char fname[256];
2300 bool ret = true;
2301
2302 /*
2303 */
2304 snprintf(fname, 256, "durable_open_oplock_lock_%s.dat", generate_random_str(tctx, 8));
2305
2306 /* Clean slate */
2307 smb2_util_unlink(tree, fname);
2308
2309 /* Create with oplock */
2310
2311 smb2_oplock_create_share(&io, fname,
2312 smb2_util_share_access(""),
2313 smb2_util_oplock_level("b"));
2314 io.in.durable_open = true;
2315
2316 status = smb2_create(tree, mem_ctx, &io);
2317 CHECK_STATUS(status, NT_STATUS_OK);
2318 h = io.out.file.handle;
2319 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2320
2321 CHECK_VAL(io.out.durable_open, true);
2322 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2323
2324 ZERO_STRUCT(lck);
2325 ZERO_STRUCT(el);
2326 lck.in.locks = el;
2327 lck.in.lock_count = 0x0001;
2328 lck.in.lock_sequence = 0x00000000;
2329 lck.in.file.handle = h;
2330 el[0].offset = 0;
2331 el[0].length = 1;
2332 el[0].reserved = 0x00000000;
2333 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
2334 status = smb2_lock(tree, &lck);
2335 CHECK_STATUS(status, NT_STATUS_OK);
2336
2337 /* Disconnect/Reconnect. */
2338 talloc_free(tree);
2339 tree = NULL;
2340
2341 if (!torture_smb2_connection(tctx, &tree)) {
2342 torture_warning(tctx, "couldn't reconnect, bailing\n");
2343 ret = false;
2344 goto done;
2345 }
2346
2347 ZERO_STRUCT(io);
2348 io.in.fname = fname;
2349 io.in.durable_handle = &h;
2350
2351 status = smb2_create(tree, mem_ctx, &io);
2352 CHECK_STATUS(status, NT_STATUS_OK);
2353 h = io.out.file.handle;
2354
2355 lck.in.file.handle = h;
2356 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2357 status = smb2_lock(tree, &lck);
2358 CHECK_STATUS(status, NT_STATUS_OK);
2359
2360 done:
2361 smb2_util_close(tree, h);
2362 smb2_util_unlink(tree, fname);
2363 talloc_free(tree);
2364
2365 return ret;
2366 }
2367
2368 /*
2369 Open(RWH), take BRL, disconnect, reconnect.
2370 */
2371 static bool test_durable_open_lock_lease(struct torture_context *tctx,
2372 struct smb2_tree *tree)
2373 {
2374 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2375 struct smb2_create io;
2376 struct smb2_lease ls;
2377 struct smb2_handle h = {{0}};
2378 struct smb2_lock lck;
2379 struct smb2_lock_element el[2];
2380 NTSTATUS status;
2381 char fname[256];
2382 bool ret = true;
2383 uint64_t lease;
2384 uint32_t caps;
2385 struct smbcli_options options;
2386
2387 options = tree->session->transport->options;
2388
2389 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
2390 if (!(caps & SMB2_CAP_LEASING)) {
2391 torture_skip(tctx, "leases are not supported");
2392 }
2393
2394 /*
2395 * Choose a random name and random lease in case the state is left a
2396 * little funky.
2397 */
2398 lease = random();
2399 snprintf(fname, 256, "durable_open_lease_lock_%s.dat", generate_random_str(tctx, 8));
2400
2401 /* Clean slate */
2402 smb2_util_unlink(tree, fname);
2403
2404 /* Create with lease */
2405
2406 smb2_lease_create(&io, &ls, false /* dir */, fname, lease,
2407 smb2_util_lease_state("RWH"));
2408 io.in.durable_open = true;
2409
2410 status = smb2_create(tree, mem_ctx, &io);
2411 CHECK_STATUS(status, NT_STATUS_OK);
2412 h = io.out.file.handle;
2413 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2414
2415 CHECK_VAL(io.out.durable_open, true);
2416 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
2417 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
2418 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
2419 CHECK_VAL(io.out.lease_response.lease_state,
2420 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
2421
2422 ZERO_STRUCT(lck);
2423 ZERO_STRUCT(el);
2424 lck.in.locks = el;
2425 lck.in.lock_count = 0x0001;
2426 lck.in.lock_sequence = 0x00000000;
2427 lck.in.file.handle = h;
2428 el[0].offset = 0;
2429 el[0].length = 1;
2430 el[0].reserved = 0x00000000;
2431 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
2432 status = smb2_lock(tree, &lck);
2433 CHECK_STATUS(status, NT_STATUS_OK);
2434
2435 /* Disconnect/Reconnect. */
2436 talloc_free(tree);
2437 tree = NULL;
2438
2439 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree)) {
2440 torture_warning(tctx, "couldn't reconnect, bailing\n");
2441 ret = false;
2442 goto done;
2443 }
2444
2445 ZERO_STRUCT(io);
2446 io.in.fname = fname;
2447 io.in.durable_handle = &h;
2448 io.in.lease_request = &ls;
2449
2450 status = smb2_create(tree, mem_ctx, &io);
2451 CHECK_STATUS(status, NT_STATUS_OK);
2452 h = io.out.file.handle;
2453
2454 lck.in.file.handle = h;
2455 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2456 status = smb2_lock(tree, &lck);
2457 CHECK_STATUS(status, NT_STATUS_OK);
2458
2459 done:
2460 smb2_util_close(tree, h);
2461 smb2_util_unlink(tree, fname);
2462 talloc_free(tree);
2463
2464 return ret;
2465 }
2466
2467 /*
2468 Open(RH), take BRL, disconnect, fails reconnect without W LEASE
2469 */
2470 static bool test_durable_open_lock_noW_lease(struct torture_context *tctx,
2471 struct smb2_tree *tree)
2472 {
2473 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2474 struct smb2_create io;
2475 struct smb2_lease ls;
2476 struct smb2_handle h = {{0}};
2477 struct smb2_lock lck;
2478 struct smb2_lock_element el[2];
2479 NTSTATUS status;
2480 char fname[256];
2481 bool ret = true;
2482 uint64_t lease;
2483 uint32_t caps;
2484 struct smbcli_options options;
2485
2486 options = tree->session->transport->options;
2487
2488 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
2489 if (!(caps & SMB2_CAP_LEASING)) {
2490 torture_skip(tctx, "leases are not supported");
2491 }
2492
2493 /*
2494 * Choose a random name and random lease in case the state is left a
2495 * little funky.
2496 */
2497 lease = random();
2498 snprintf(fname, 256, "durable_open_lease_noW_lock_%s.dat", generate_random_str(tctx, 8));
2499
2500 /* Clean slate */
2501 smb2_util_unlink(tree, fname);
2502
2503 /* Create with lease */
2504
2505 smb2_lease_create(&io, &ls, false /* dir */, fname, lease,
2506 smb2_util_lease_state("RH"));
2507 io.in.durable_open = true;
2508
2509 status = smb2_create(tree, mem_ctx, &io);
2510 CHECK_STATUS(status, NT_STATUS_OK);
2511 h = io.out.file.handle;
2512 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2513
2514 CHECK_VAL(io.out.durable_open, true);
2515 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
2516 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
2517 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
2518 CHECK_VAL(io.out.lease_response.lease_state,
2519 SMB2_LEASE_READ|SMB2_LEASE_HANDLE);
2520
2521 ZERO_STRUCT(lck);
2522 ZERO_STRUCT(el);
2523 lck.in.locks = el;
2524 lck.in.lock_count = 0x0001;
2525 lck.in.lock_sequence = 0x00000000;
2526 lck.in.file.handle = h;
2527 el[0].offset = 0;
2528 el[0].length = 1;
2529 el[0].reserved = 0x00000000;
2530 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
2531 status = smb2_lock(tree, &lck);
2532 CHECK_STATUS(status, NT_STATUS_OK);
2533
2534 /* Disconnect/Reconnect. */
2535 talloc_free(tree);
2536 tree = NULL;
2537
2538 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree)) {
2539 torture_warning(tctx, "couldn't reconnect, bailing\n");
2540 ret = false;
2541 goto done;
2542 }
2543
2544 ZERO_STRUCT(io);
2545 io.in.fname = fname;
2546 io.in.durable_handle = &h;
2547 io.in.lease_request = &ls;
2548
2549 status = smb2_create(tree, mem_ctx, &io);
2550 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
2551 h = io.out.file.handle;
2552
2553 done:
2554 smb2_util_close(tree, h);
2555 smb2_util_unlink(tree, fname);
2556 talloc_free(tree);
2557
2558 return ret;
2559 }
2560
2561 /**
2562 * Open with a RH lease, disconnect, open in another tree, reconnect.
2563 *
2564 * This test actually demonstrates a minimum level of respect for the durable
2565 * open in the face of another open. As long as this test shows an inability to
2566 * reconnect after an open, the oplock/lease tests above will certainly
2567 * demonstrate an error on reconnect.
2568 */
2569 static bool test_durable_open_open2_lease(struct torture_context *tctx,
2570 struct smb2_tree *tree1,
2571 struct smb2_tree *tree2)
2572 {
2573 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2574 struct smb2_create io1, io2;
2575 struct smb2_lease ls;
2576 struct smb2_handle h1 = {{0}};
2577 struct smb2_handle h2 = {{0}};
2578 NTSTATUS status;
2579 char fname[256];
2580 bool ret = true;
2581 uint64_t lease;
2582 uint32_t caps;
2583 struct smbcli_options options;
2584
2585 options = tree1->session->transport->options;
2586
2587 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
2588 if (!(caps & SMB2_CAP_LEASING)) {
2589 torture_skip(tctx, "leases are not supported");
2590 }
2591
2592 /*
2593 * Choose a random name and random lease in case the state is left a
2594 * little funky.
2595 */
2596 lease = random();
2597 snprintf(fname, 256, "durable_open_open2_lease_%s.dat",
2598 generate_random_str(tctx, 8));
2599
2600 /* Clean slate */
2601 smb2_util_unlink(tree1, fname);
2602
2603 /* Create with lease */
2604 smb2_lease_create_share(&io1, &ls, false /* dir */, fname,
2605 smb2_util_share_access(""),
2606 lease,
2607 smb2_util_lease_state("RH"));
2608 io1.in.durable_open = true;
2609
2610 status = smb2_create(tree1, mem_ctx, &io1);
2611 CHECK_STATUS(status, NT_STATUS_OK);
2612 h1 = io1.out.file.handle;
2613 CHECK_VAL(io1.out.durable_open, true);
2614 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2615
2616 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
2617 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease);
2618 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease);
2619 CHECK_VAL(io1.out.lease_response.lease_state,
2620 smb2_util_lease_state("RH"));
2621
2622 /* Disconnect */
2623 talloc_free(tree1);
2624 tree1 = NULL;
2625
2626 /* Open the file in tree2 */
2627 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
2628
2629 status = smb2_create(tree2, mem_ctx, &io2);
2630 CHECK_STATUS(status, NT_STATUS_OK);
2631 h2 = io2.out.file.handle;
2632 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
2633
2634 /* Reconnect */
2635 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree1)) {
2636 torture_warning(tctx, "couldn't reconnect, bailing\n");
2637 ret = false;
2638 goto done;
2639 }
2640
2641 ZERO_STRUCT(io1);
2642 io1.in.fname = fname;
2643 io1.in.durable_handle = &h1;
2644 io1.in.lease_request = &ls;
2645
2646 /*
2647 * Windows7 (build 7000) will give away an open immediately if the
2648 * original client is gone. (ZML: This seems like a bug. It should give
2649 * some time for the client to reconnect!)
2650 */
2651 status = smb2_create(tree1, mem_ctx, &io1);
2652 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
2653 h1 = io1.out.file.handle;
2654
2655 done:
2656 if (tree1 != NULL){
2657 smb2_util_close(tree1, h1);
2658 smb2_util_unlink(tree1, fname);
2659 talloc_free(tree1);
2660 }
2661
2662 smb2_util_close(tree2, h2);
2663 smb2_util_unlink(tree2, fname);
2664 talloc_free(tree2);
2665
2666 return ret;
2667 }
2668
2669 /**
2670 * Open with a batch oplock, disconnect, open in another tree, reconnect.
2671 *
2672 * This test actually demonstrates a minimum level of respect for the durable
2673 * open in the face of another open. As long as this test shows an inability to
2674 * reconnect after an open, the oplock/lease tests above will certainly
2675 * demonstrate an error on reconnect.
2676 */
2677 static bool test_durable_open_open2_oplock(struct torture_context *tctx,
2678 struct smb2_tree *tree1,
2679 struct smb2_tree *tree2)
2680 {
2681 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2682 struct smb2_create io1, io2;
2683 struct smb2_handle h1 = {{0}};
2684 struct smb2_handle h2 = {{0}};
2685 NTSTATUS status;
2686 char fname[256];
2687 bool ret = true;
2688
2689 /*
2690 * Choose a random name and random lease in case the state is left a
2691 * little funky.
2692 */
2693 snprintf(fname, 256, "durable_open_open2_oplock_%s.dat",
2694 generate_random_str(tctx, 8));
2695
2696 /* Clean slate */
2697 smb2_util_unlink(tree1, fname);
2698
2699 /* Create with batch oplock */
2700 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
2701 io1.in.durable_open = true;
2702
2703 status = smb2_create(tree1, mem_ctx, &io1);
2704 CHECK_STATUS(status, NT_STATUS_OK);
2705 h1 = io1.out.file.handle;
2706 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2707 CHECK_VAL(io1.out.durable_open, true);
2708 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
2709
2710 /* Disconnect */
2711 talloc_free(tree1);
2712 tree1 = NULL;
2713
2714 /* Open the file in tree2 */
2715 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
2716
2717 status = smb2_create(tree2, mem_ctx, &io2);
2718 CHECK_STATUS(status, NT_STATUS_OK);
2719 h2 = io2.out.file.handle;
2720 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2721
2722 /* Reconnect */
2723 if (!torture_smb2_connection(tctx, &tree1)) {
2724 torture_warning(tctx, "couldn't reconnect, bailing\n");
2725 ret = false;
2726 goto done;
2727 }
2728
2729 ZERO_STRUCT(io1);
2730 io1.in.fname = fname;
2731 io1.in.durable_handle = &h1;
2732
2733 status = smb2_create(tree1, mem_ctx, &io1);
2734 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
2735 h1 = io1.out.file.handle;
2736
2737 done:
2738 smb2_util_close(tree2, h2);
2739 smb2_util_unlink(tree2, fname);
2740 if (tree1 != NULL) {
2741 smb2_util_close(tree1, h1);
2742 smb2_util_unlink(tree1, fname);
2743 }
2744
2745 talloc_free(tree1);
2746 talloc_free(tree2);
2747
2748 return ret;
2749 }
2750
2751 /**
2752 * test behaviour with initial allocation size
2753 */
2754 static bool test_durable_open_alloc_size(struct torture_context *tctx,
2755 struct smb2_tree *tree)
2756 {
2757 NTSTATUS status;
2758 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2759 char fname[256];
2760 struct smb2_handle _h;
2761 struct smb2_handle *h = NULL;
2762 struct smb2_create io;
2763 bool ret = true;
2764 uint64_t previous_session_id;
2765 uint64_t alloc_size_step;
2766 uint64_t initial_alloc_size = 0x1000;
2767 const uint8_t *b = NULL;
2768 struct smbcli_options options;
2769
2770 options = tree->session->transport->options;
2771
2772 /* Choose a random name in case the state is left a little funky. */
2773 snprintf(fname, 256, "durable_open_alloc_size_%s.dat",
2774 generate_random_str(tctx, 8));
2775
2776 smb2_util_unlink(tree, fname);
2777
2778 smb2_oplock_create_share(&io, fname,
2779 smb2_util_share_access(""),
2780 smb2_util_oplock_level("b"));
2781 io.in.durable_open = true;
2782 io.in.alloc_size = initial_alloc_size;
2783
2784 status = smb2_create(tree, mem_ctx, &io);
2785 CHECK_STATUS(status, NT_STATUS_OK);
2786 _h = io.out.file.handle;
2787 h = &_h;
2788 CHECK_NOT_VAL(io.out.alloc_size, 0);
2789 alloc_size_step = io.out.alloc_size;
2790 CHECK_CREATED_SIZE(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE,
2791 alloc_size_step, 0);
2792 CHECK_VAL(io.out.durable_open, true);
2793 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2794
2795 /* prepare buffer */
2796 b = talloc_zero_size(mem_ctx, alloc_size_step);
2797 CHECK_NOT_NULL(b);
2798
2799 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
2800
2801 /* disconnect, reconnect and then do durable reopen */
2802 talloc_free(tree);
2803 tree = NULL;
2804
2805 if (!torture_smb2_connection_ext(tctx, previous_session_id,
2806 &options, &tree))
2807 {
2808 torture_warning(tctx, "couldn't reconnect, bailing\n");
2809 ret = false;
2810 goto done;
2811 }
2812
2813 ZERO_STRUCT(io);
2814 io.in.fname = fname;
2815 io.in.durable_handle = h;
2816 h = NULL;
2817
2818 status = smb2_create(tree, mem_ctx, &io);
2819 CHECK_STATUS(status, NT_STATUS_OK);
2820 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
2821 alloc_size_step, 0);
2822 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2823 _h = io.out.file.handle;
2824 h = &_h;
2825
2826 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
2827
2828 /* write one byte */
2829 status = smb2_util_write(tree, *h, b, 0, 1);
2830 CHECK_STATUS(status, NT_STATUS_OK);
2831
2832 /* disconnect, reconnect and then do durable reopen */
2833 talloc_free(tree);
2834 tree = NULL;
2835
2836 if (!torture_smb2_connection_ext(tctx, previous_session_id,
2837 &options, &tree))
2838 {
2839 torture_warning(tctx, "couldn't reconnect, bailing\n");
2840 ret = false;
2841 goto done;
2842 }
2843
2844 ZERO_STRUCT(io);
2845 io.in.fname = fname;
2846 io.in.durable_handle = h;
2847 h = NULL;
2848
2849 status = smb2_create(tree, mem_ctx, &io);
2850 CHECK_STATUS(status, NT_STATUS_OK);
2851 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
2852 alloc_size_step, 1);
2853 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2854 _h = io.out.file.handle;
2855 h = &_h;
2856
2857 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
2858
2859 /* write more byte than initial allocation size */
2860 status = smb2_util_write(tree, *h, b, 1, alloc_size_step);
2861
2862 /* disconnect, reconnect and then do durable reopen */
2863 talloc_free(tree);
2864 tree = NULL;
2865
2866 if (!torture_smb2_connection_ext(tctx, previous_session_id,
2867 &options, &tree))
2868 {
2869 torture_warning(tctx, "couldn't reconnect, bailing\n");
2870 ret = false;
2871 goto done;
2872 }
2873
2874 ZERO_STRUCT(io);
2875 io.in.fname = fname;
2876 io.in.durable_handle = h;
2877 h = NULL;
2878
2879 status = smb2_create(tree, mem_ctx, &io);
2880 CHECK_STATUS(status, NT_STATUS_OK);
2881 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
2882 alloc_size_step * 2, alloc_size_step + 1);
2883 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2884 _h = io.out.file.handle;
2885 h = &_h;
2886
2887 done:
2888 if (h != NULL) {
2889 smb2_util_close(tree, *h);
2890 }
2891
2892 smb2_util_unlink(tree, fname);
2893
2894 talloc_free(tree);
2895
2896 talloc_free(mem_ctx);
2897
2898 return ret;
2899 }
2900
2901 /**
2902 * test behaviour when a disconnect happens while creating a read-only file
2903 */
2904 static bool test_durable_open_read_only(struct torture_context *tctx,
2905 struct smb2_tree *tree)
2906 {
2907 NTSTATUS status;
2908 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2909 char fname[256];
2910 struct smb2_handle _h;
2911 struct smb2_handle *h = NULL;
2912 struct smb2_create io;
2913 bool ret = true;
2914 uint64_t previous_session_id;
2915 const uint8_t b = 0;
2916 uint64_t alloc_size = 0;
2917 struct smbcli_options options;
2918
2919 options = tree->session->transport->options;
2920
2921 /* Choose a random name in case the state is left a little funky. */
2922 snprintf(fname, 256, "durable_open_initial_alloc_%s.dat",
2923 generate_random_str(tctx, 8));
2924
2925 smb2_util_unlink(tree, fname);
2926
2927 smb2_oplock_create_share(&io, fname,
2928 smb2_util_share_access(""),
2929 smb2_util_oplock_level("b"));
2930 io.in.durable_open = true;
2931 io.in.file_attributes = FILE_ATTRIBUTE_READONLY;
2932
2933 status = smb2_create(tree, mem_ctx, &io);
2934 CHECK_STATUS(status, NT_STATUS_OK);
2935 _h = io.out.file.handle;
2936 h = &_h;
2937 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_ARCHIVE);
2938 CHECK_VAL(io.out.durable_open, true);
2939 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2940
2941 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
2942
2943 /* write one byte */
2944 status = smb2_util_write(tree, *h, &b, 0, 1);
2945 CHECK_STATUS(status, NT_STATUS_OK);
2946
2947 /* disconnect, reconnect and then do durable reopen */
2948 talloc_free(tree);
2949 tree = NULL;
2950
2951 if (!torture_smb2_connection_ext(tctx, previous_session_id,
2952 &options, &tree))
2953 {
2954 torture_warning(tctx, "couldn't reconnect, bailing\n");
2955 ret = false;
2956 goto done;
2957 }
2958
2959 ZERO_STRUCT(io);
2960 io.in.fname = fname;
2961 io.in.durable_handle = h;
2962 h = NULL;
2963
2964 status = smb2_create(tree, mem_ctx, &io);
2965 CHECK_STATUS(status, NT_STATUS_OK);
2966 alloc_size = io.out.alloc_size;
2967 CHECK_CREATED_SIZE(&io, EXISTED,
2968 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_ARCHIVE,
2969 alloc_size, 1);
2970 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2971 _h = io.out.file.handle;
2972 h = &_h;
2973
2974 /* write one byte */
2975 status = smb2_util_write(tree, *h, &b, 1, 1);
2976 CHECK_STATUS(status, NT_STATUS_OK);
2977
2978 done:
2979 if (h != NULL) {
2980 union smb_setfileinfo sfinfo;
2981
2982 ZERO_STRUCT(sfinfo);
2983 sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION;
2984 sfinfo.basic_info.in.file.handle = *h;
2985 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
2986 smb2_setinfo_file(tree, &sfinfo);
2987
2988 smb2_util_close(tree, *h);
2989 }
2990
2991 smb2_util_unlink(tree, fname);
2992
2993 talloc_free(tree);
2994
2995 talloc_free(mem_ctx);
2996
2997 return ret;
2998 }
2999
3000 /**
3001 * durable open with oplock, disconnect, exit
3002 */
3003 static bool test_durable_open_oplock_disconnect(struct torture_context *tctx,
3004 struct smb2_tree *tree)
3005 {
3006 TALLOC_CTX *mem_ctx = talloc_new(tctx);
3007 struct smb2_create io;
3008 struct smb2_handle _h;
3009 struct smb2_handle *h = NULL;
3010 NTSTATUS status;
3011 char fname[256];
3012 bool ret = true;
3013
3014 snprintf(fname, 256, "durable_open_oplock_disconnect_%s.dat",
3015 generate_random_str(mem_ctx, 8));
3016
3017 smb2_util_unlink(tree, fname);
3018
3019 smb2_oplock_create(&io, fname, SMB2_OPLOCK_LEVEL_BATCH);
3020 io.in.durable_open = true;
3021
3022 status = smb2_create(tree, mem_ctx, &io);
3023 CHECK_STATUS(status, NT_STATUS_OK);
3024
3025 _h = io.out.file.handle;
3026 h = &_h;
3027
3028 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
3029 CHECK_VAL(io.out.durable_open, true);
3030 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
3031
3032 /* disconnect */
3033 talloc_free(tree);
3034 tree = NULL;
3035
3036 done:
3037 if (tree != NULL) {
3038 if (h != NULL) {
3039 smb2_util_close(tree, *h);
3040 }
3041 smb2_util_unlink(tree, fname);
3042 }
3043 talloc_free(mem_ctx);
3044 return ret;
3045 }
3046
3047 /**
3048 * durable stat open with lease.
3049 */
3050 static bool test_durable_open_stat_open(struct torture_context *tctx,
3051 struct smb2_tree *tree)
3052 {
3053 TALLOC_CTX *mem_ctx = talloc_new(tctx);
3054 struct smb2_create io;
3055 struct smb2_handle _h;
3056 struct smb2_handle *h = NULL;
3057 struct smb2_lease ls;
3058 NTSTATUS status;
3059 char fname[256];
3060 bool ret = true;
3061 uint64_t lease;
3062
3063 snprintf(fname, 256, "durable_open_stat_open_%s.dat",
3064 generate_random_str(mem_ctx, 8));
3065
3066 /* Ensure file doesn't exist. */
3067 smb2_util_unlink(tree, fname);
3068
3069 /* Create a normal file. */
3070 smb2_oplock_create(&io, fname, SMB2_OPLOCK_LEVEL_NONE);
3071 status = smb2_create(tree, mem_ctx, &io);
3072 CHECK_STATUS(status, NT_STATUS_OK);
3073 _h = io.out.file.handle;
3074 h = &_h;
3075 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
3076 /* Close. */
3077 smb2_util_close(tree, *h);
3078 h = NULL;
3079
3080 /* Now try a leased, durable handle stat open. */
3081 lease = random();
3082 /* Create with lease */
3083 smb2_lease_create(&io,
3084 &ls,
3085 false /* dir */,
3086 fname,
3087 lease,
3088 smb2_util_lease_state("RH"));
3089 io.in.durable_open = true;
3090 io.in.desired_access = SEC_FILE_READ_ATTRIBUTE;
3091 io.in.create_disposition = NTCREATEX_DISP_OPEN;
3092
3093 status = smb2_create(tree, mem_ctx, &io);
3094 CHECK_STATUS(status, NT_STATUS_OK);
3095 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
3096 CHECK_VAL(io.out.durable_open, true);
3097 _h = io.out.file.handle;
3098 h = &_h;
3099
3100 done:
3101 if (h != NULL) {
3102 smb2_util_close(tree, *h);
3103 }
3104 smb2_util_unlink(tree, fname);
3105 talloc_free(mem_ctx);
3106 return ret;
3107 }
3108
3109 struct torture_suite *torture_smb2_durable_open_init(TALLOC_CTX *ctx)
3110 {
3111 struct torture_suite *suite =
3112 torture_suite_create(ctx, "durable-open");
3113
3114 torture_suite_add_1smb2_test(suite, "open-oplock", test_durable_open_open_oplock);
3115 torture_suite_add_1smb2_test(suite, "open-lease", test_durable_open_open_lease);
3116 torture_suite_add_1smb2_test(suite, "reopen1", test_durable_open_reopen1);
3117 torture_suite_add_1smb2_test(suite, "reopen1a", test_durable_open_reopen1a);
3118 torture_suite_add_1smb2_test(suite, "reopen1a-lease", test_durable_open_reopen1a_lease);
3119 torture_suite_add_1smb2_test(suite, "reopen2", test_durable_open_reopen2);
3120 torture_suite_add_1smb2_test(suite, "reopen2-lease", test_durable_open_reopen2_lease);
3121 torture_suite_add_1smb2_test(suite, "reopen2-lease-v2", test_durable_open_reopen2_lease_v2);
3122 torture_suite_add_1smb2_test(suite, "reopen2a", test_durable_open_reopen2a);
3123 torture_suite_add_1smb2_test(suite, "reopen3", test_durable_open_reopen3);
3124 torture_suite_add_1smb2_test(suite, "reopen4", test_durable_open_reopen4);
3125 torture_suite_add_1smb2_test(suite, "reopen5", test_durable_open_reopen5);
3126 torture_suite_add_1smb2_test(suite, "reopen6", test_durable_open_reopen6);
3127 torture_suite_add_1smb2_test(suite, "delete_on_close1",
3128 test_durable_open_delete_on_close1);
3129 torture_suite_add_1smb2_test(suite, "delete_on_close2",
3130 test_durable_open_delete_on_close2);
3131 torture_suite_add_1smb2_test(suite, "file-position",
3132 test_durable_open_file_position);
3133 torture_suite_add_2smb2_test(suite, "oplock", test_durable_open_oplock);
3134 torture_suite_add_2smb2_test(suite, "lease", test_durable_open_lease);
3135 torture_suite_add_1smb2_test(suite, "lock-oplock", test_durable_open_lock_oplock);
3136 torture_suite_add_1smb2_test(suite, "lock-lease", test_durable_open_lock_lease);
3137 torture_suite_add_1smb2_test(suite, "lock-noW-lease", test_durable_open_lock_noW_lease);
3138 torture_suite_add_2smb2_test(suite, "open2-lease",
3139 test_durable_open_open2_lease);
3140 torture_suite_add_2smb2_test(suite, "open2-oplock",
3141 test_durable_open_open2_oplock);
3142 torture_suite_add_1smb2_test(suite, "alloc-size",
3143 test_durable_open_alloc_size);
3144 torture_suite_add_1smb2_test(suite, "read-only",
3145 test_durable_open_read_only);
3146 torture_suite_add_1smb2_test(suite, "stat-open",
3147 test_durable_open_stat_open);
3148
3149 suite->description = talloc_strdup(suite, "SMB2-DURABLE-OPEN tests");
3150
3151 return suite;
3152 }
3153
3154 struct torture_suite *torture_smb2_durable_open_disconnect_init(TALLOC_CTX *ctx)
3155 {
3156 struct torture_suite *suite =
3157 torture_suite_create(ctx,
3158 "durable-open-disconnect");
3159
3160 torture_suite_add_1smb2_test(suite, "open-oplock-disconnect",
3161 test_durable_open_oplock_disconnect);
3162
3163 suite->description = talloc_strdup(suite,
3164 "SMB2-DURABLE-OPEN-DISCONNECT tests");
3165
3166 return suite;
3167 }