summaryrefslogtreecommitdiffstats
path: root/source4/torture/smb2/sharemode.c
blob: 97381b56f17fe17866ceeb6840636c8a7fe727ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
   Unix SMB/CIFS implementation.

   test suite for SMB2 sharemodes

   Copyright (C) Christof Schmitt 2017

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "libcli/smb2/smb2.h"
#include "libcli/smb2/smb2_calls.h"
#include "libcli/security/security.h"
#include "torture/torture.h"
#include "torture/smb2/proto.h"
#include "lib/util/smb_strtox.h"
#include <tevent.h>

#define BASEDIRHOLD "sharemode_hold_test"

struct hold_sharemode_info {
	const char *sharemode;
	const char *filename;
	struct smb2_handle handle;
} hold_sharemode_table[] = {
	{
		.sharemode = "",
		.filename  = BASEDIRHOLD "\\N",
	},
	{
		.sharemode = "R",
		.filename  = BASEDIRHOLD "\\R",
	},
	{
		.sharemode = "W",
		.filename  = BASEDIRHOLD "\\W",
	},
	{
		.sharemode = "D",
		.filename  = BASEDIRHOLD "\\D",
	},
	{
		.sharemode = "RW",
		.filename  = BASEDIRHOLD "\\RW",
	},
	{
		.sharemode = "RD",
		.filename  = BASEDIRHOLD "\\RD",
	},
	{
		.sharemode = "WD",
		.filename  = BASEDIRHOLD "\\WD",
	},
	{
		.sharemode = "RWD",
		.filename  = BASEDIRHOLD "\\RWD",
	},
};

static void signal_handler(struct tevent_context *ev,
			   struct tevent_signal *se,
			   int signum,
			   int count,
			   void *siginfo,
			   void *private_data)
{
	struct torture_context *tctx = private_data;

	torture_comment(tctx, "Received signal %d\n", signum);
}

/*
 * Used for manual testing of sharemodes - especially interaction with
 * other filesystems (such as NFS and local access). The scenario is
 * that this test holds files open and then concurrent access to the same
 * files outside of Samba can be tested.
 */
bool torture_smb2_hold_sharemode(struct torture_context *tctx)
{
	struct tevent_context *ev = tctx->ev;
	struct smb2_tree *tree = NULL;
	struct smb2_handle dir_handle;
	struct tevent_signal *s;
	NTSTATUS status;
	bool ret = true;
	int i;

	if (!torture_smb2_connection(tctx, &tree)) {
		torture_comment(tctx, "Initializing smb2 connection failed.\n");
		return false;
	}

	s = tevent_add_signal(ev, tctx, SIGINT, 0, signal_handler, tctx);
	torture_assert_not_null_goto(tctx, s, ret, done,
				     "Error registering signal handler.");

	torture_comment(tctx, "Setting up open files with sharemodes in %s\n",
			BASEDIRHOLD);

	status = torture_smb2_testdir(tree, BASEDIRHOLD, &dir_handle);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"Error creating directory.");

	for (i = 0; i < ARRAY_SIZE(hold_sharemode_table); i++) {
		struct hold_sharemode_info *info = &hold_sharemode_table[i];
		struct smb2_create create = { 0 };

		create.in.desired_access = SEC_RIGHTS_FILE_ALL;
		create.in.alloc_size = 0;
		create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
		create.in.share_access =
			smb2_util_share_access(info->sharemode);
		create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
		create.in.create_options = 0;
		create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
		create.in.security_flags = 0;
		create.in.fname = info->filename;
		create.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
		create.in.oplock_level = SMB2_OPLOCK_LEVEL_NONE;

		torture_comment(tctx, "opening %s\n", info->filename);

		status = smb2_create(tree, tctx, &create);

		torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
						"CREATE file failed\n");

		info->handle = create.out.file.handle;
	}

	torture_comment(tctx, "Waiting for SIGINT (ctrl-c)\n");
	tevent_loop_wait(ev);

	torture_comment(tctx, "Closing and deleting files\n");

	for (i = 0; i < ARRAY_SIZE(hold_sharemode_table); i++) {
		struct hold_sharemode_info *info = &hold_sharemode_table[i];

		union smb_setfileinfo sfinfo = { };

		sfinfo.disposition_info.in.delete_on_close = 1;
		sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
		sfinfo.generic.in.file.handle = info->handle;
		status = smb2_setinfo_file(tree, &sfinfo);
		torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
						"SETINFO failed\n");

		status = smb2_util_close(tree, info->handle);
		if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
			torture_comment(tctx, "File %s not found, could have "
					"been deleted outside of SMB\n",
					info->filename);
			continue;
		}
		torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
						"CLOSE failed\n");
}

done:
	smb2_deltree(tree, BASEDIRHOLD);
	return ret;
}

/*
 * Used for manual testing of sharemodes, especially interaction with
 * file systems that can enforce sharemodes. The scenario here is that
 * a file is already open outside of Samba with a sharemode and this
 * can be used to test accessing the same file from Samba.
 */
bool torture_smb2_check_sharemode(struct torture_context *tctx)
{
	const char *sharemode_string, *access_string, *filename, *operation;
	uint32_t sharemode, access;
	struct smb2_tree *tree;
	struct smb2_create create = { 0 };
	NTSTATUS status;
	bool ret = true;
	int error = 0;

	sharemode_string = torture_setting_string(tctx, "sharemode", "RWD");
	sharemode = smb2_util_share_access(sharemode_string);

	access_string = torture_setting_string(tctx, "access", "0xf01ff");
	access = smb_strtoul(access_string, NULL, 0, &error, SMB_STR_STANDARD);
	if (error != 0) {
		torture_comment(tctx, "Initializing access failed.\n");
		return false;
	}

	filename = torture_setting_string(tctx, "filename", "testfile");
	operation = torture_setting_string(tctx, "operation", "WD");

	if (!torture_smb2_connection(tctx, &tree)) {
		torture_comment(tctx, "Initializing smb2 connection failed.\n");
		return false;
	}

	create.in.desired_access = access;
	create.in.alloc_size = 0;
	create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
	create.in.share_access = sharemode;
	create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
	create.in.create_options = 0;
	create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
	create.in.security_flags = 0;
	create.in.fname = filename;
	create.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
	create.in.oplock_level = SMB2_OPLOCK_LEVEL_NONE;

	status = smb2_create(tree, tctx, &create);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"CREATE failed\n");

	if (strchr(operation, 'R')) {
		struct smb2_read read = { 0 };

		read.in.file.handle = create.out.file.handle;
		read.in.offset = 0;
		read.in.length = 1;

		status = smb2_read(tree, tctx, &read);
		torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
						"READ failed\n");
	}

	if (strchr(operation, 'W')) {
		char buf[1];
		status = smb2_util_write(tree, create.out.file.handle,
					 &buf, 0, sizeof(buf));
		torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
						"WRITE failed\n");
	}

	if (strchr(operation, 'D')) {
		union smb_setfileinfo sfinfo = { };

		sfinfo.disposition_info.in.delete_on_close = 1;
		sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
		sfinfo.generic.in.file.handle = create.out.file.handle;

		status = smb2_setinfo_file(tree, &sfinfo);
		torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
						"SETINFO failed\n");

		status = smb2_util_close(tree, create.out.file.handle);
		torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
						"CLOSE failed\n");
	}

done:
	return ret;
}

struct sharemode_info {
	const char *sharemode;
	uint32_t access_mask;
	bool expect_ok;
} sharemode_table[] = {

	/*
	 * Basic tests, check each permission bit against every
	 * possible sharemode combination.
	 */

	{ "R",	 SEC_FILE_READ_DATA,		true,	},
	{ "R",	 SEC_FILE_WRITE_DATA,		false,	},
	{ "R",	 SEC_FILE_APPEND_DATA,		false,	},
	{ "R",	 SEC_FILE_READ_EA,		true,	},
	{ "R",	 SEC_FILE_WRITE_EA,		true,	},
	{ "R",	 SEC_FILE_EXECUTE,		true,	},
	{ "R",	 SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "R",	 SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "R",	 SEC_STD_DELETE,		false,	},
	{ "R",	 SEC_STD_READ_CONTROL,		true,	},
	{ "R",	 SEC_STD_WRITE_DAC,		true,	},
	{ "R",	 SEC_STD_WRITE_OWNER,		true,	},
	{ "R",	 SEC_STD_SYNCHRONIZE,		true,	},

	{ "W",	 SEC_FILE_READ_DATA,		false	},
	{ "W",	 SEC_FILE_WRITE_DATA,		true,	},
	{ "W",	 SEC_FILE_APPEND_DATA,		true,	},
	{ "W",	 SEC_FILE_READ_EA,		true,	},
	{ "W",	 SEC_FILE_WRITE_EA,		true,	},
	{ "W",	 SEC_FILE_EXECUTE,		false,	},
	{ "W",	 SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "W",	 SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "W",	 SEC_STD_DELETE,		false,	},
	{ "W",	 SEC_STD_READ_CONTROL,		true,	},
	{ "W",	 SEC_STD_WRITE_DAC,		true,	},
	{ "W",	 SEC_STD_WRITE_OWNER,		true,	},
	{ "W",	 SEC_STD_SYNCHRONIZE,		true,	},

	{ "D",	 SEC_FILE_READ_DATA,		false	},
	{ "D",	 SEC_FILE_WRITE_DATA,		false	},
	{ "D",	 SEC_FILE_APPEND_DATA,		false	},
	{ "D",	 SEC_FILE_READ_EA,		true,	},
	{ "D",	 SEC_FILE_WRITE_EA,		true,	},
	{ "D",	 SEC_FILE_EXECUTE,		false,	},
	{ "D",	 SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "D",	 SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "D",	 SEC_STD_DELETE,		true,	},
	{ "D",	 SEC_STD_READ_CONTROL,		true,	},
	{ "D",	 SEC_STD_WRITE_DAC,		true,	},
	{ "D",	 SEC_STD_WRITE_OWNER,		true,	},
	{ "D",	 SEC_STD_SYNCHRONIZE,		true,	},

	{ "RW",  SEC_FILE_READ_DATA,		true,	},
	{ "RW",  SEC_FILE_WRITE_DATA,		true,	},
	{ "RW",  SEC_FILE_APPEND_DATA,		true,	},
	{ "RW",  SEC_FILE_READ_EA,		true,	},
	{ "RW",  SEC_FILE_WRITE_EA,		true,	},
	{ "RW",  SEC_FILE_EXECUTE,		true,	},
	{ "RW",  SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "RW",  SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "RW",  SEC_STD_DELETE,		false,	},
	{ "RW",  SEC_STD_READ_CONTROL,		true,	},
	{ "RW",  SEC_STD_WRITE_DAC,		true,	},
	{ "RW",  SEC_STD_WRITE_OWNER,		true,	},
	{ "RW",  SEC_STD_SYNCHRONIZE,		true,	},

	{ "RD",  SEC_FILE_READ_DATA,		true,	},
	{ "RD",  SEC_FILE_WRITE_DATA,		false,	},
	{ "RD",  SEC_FILE_APPEND_DATA,		false,	},
	{ "RD",  SEC_FILE_READ_EA,		true,	},
	{ "RD",  SEC_FILE_WRITE_EA,		true,	},
	{ "RD",  SEC_FILE_EXECUTE,		true,	},
	{ "RD",  SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "RD",  SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "RD",  SEC_STD_DELETE,		true,	},
	{ "RD",  SEC_STD_READ_CONTROL,		true,	},
	{ "RD",  SEC_STD_WRITE_DAC,		true,	},
	{ "RD",  SEC_STD_WRITE_OWNER,		true,	},
	{ "RD",  SEC_STD_SYNCHRONIZE,		true,	},

	{ "WD",  SEC_FILE_READ_DATA,		false	},
	{ "WD",  SEC_FILE_WRITE_DATA,		true,	},
	{ "WD",  SEC_FILE_APPEND_DATA,		true,	},
	{ "WD",  SEC_FILE_READ_EA,		true	},
	{ "WD",  SEC_FILE_WRITE_EA,		true,	},
	{ "WD",  SEC_FILE_EXECUTE,		false	},
	{ "WD",  SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "WD",  SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "WD",  SEC_STD_DELETE,		true,	},
	{ "WD",  SEC_STD_READ_CONTROL,		true,	},
	{ "WD",  SEC_STD_WRITE_DAC,		true,	},
	{ "WD",  SEC_STD_WRITE_OWNER,		true,	},
	{ "WD",  SEC_STD_SYNCHRONIZE,		true,	},

	{ "RWD",  SEC_FILE_READ_DATA,		true	},
	{ "RWD",  SEC_FILE_WRITE_DATA,		true,	},
	{ "RWD",  SEC_FILE_APPEND_DATA,	true,	},
	{ "RWD",  SEC_FILE_READ_EA,		true	},
	{ "RWD",  SEC_FILE_WRITE_EA,		true,	},
	{ "RWD",  SEC_FILE_EXECUTE,		true,	},
	{ "RWD",  SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "RWD",  SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "RWD",  SEC_STD_DELETE,		true,	},
	{ "RWD",  SEC_STD_READ_CONTROL,	true,	},
	{ "RWD",  SEC_STD_WRITE_DAC,		true,	},
	{ "RWD",  SEC_STD_WRITE_OWNER,		true,	},
	{ "RWD",  SEC_STD_SYNCHRONIZE,		 true,  },

	/*
	 * Some more interesting cases. Always request READ or WRITE
	 * access, as that will trigger the opening of a file
	 * description in Samba. This especially useful for file
	 * systems that enforce share modes on open file descriptors.
	 */

	{ "R",	 SEC_FILE_READ_DATA,				true,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_FILE_WRITE_DATA,	false,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_FILE_APPEND_DATA,	false,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_FILE_READ_EA,		true,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_FILE_WRITE_EA,		true,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_FILE_EXECUTE,		true,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_STD_DELETE,		false,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_STD_READ_CONTROL,	true,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_STD_WRITE_DAC,		true,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_STD_WRITE_OWNER,	true,	},
	{ "R",	 SEC_FILE_READ_DATA|SEC_STD_SYNCHRONIZE,	true,	},

	{ "W",	 SEC_FILE_WRITE_DATA|SEC_FILE_READ_DATA,	false,	},
	{ "W",	 SEC_FILE_WRITE_DATA,				true,	},
	{ "W",	 SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA,	true,	},
	{ "W",	 SEC_FILE_WRITE_DATA|SEC_FILE_READ_EA,		true,	},
	{ "W",	 SEC_FILE_WRITE_DATA|SEC_FILE_WRITE_EA,	true,	},
	{ "W",	 SEC_FILE_WRITE_DATA|SEC_FILE_EXECUTE,		false,	},
	{ "W",	 SEC_FILE_WRITE_DATA|SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "W",	 SEC_FILE_WRITE_DATA|SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "W",	 SEC_FILE_WRITE_DATA|SEC_STD_DELETE,		false,	},
	{ "W",	 SEC_FILE_WRITE_DATA|SEC_STD_READ_CONTROL,	true,	},
	{ "W",	 SEC_FILE_WRITE_DATA|SEC_STD_WRITE_DAC,	true,	},
	{ "W",	 SEC_FILE_WRITE_DATA|SEC_STD_WRITE_OWNER,	true,	},
	{ "W",	 SEC_FILE_WRITE_DATA|SEC_STD_SYNCHRONIZE,	true,	},

	{ "RW",  SEC_FILE_READ_DATA,				true,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_FILE_WRITE_DATA,	true,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_FILE_APPEND_DATA,	true,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_FILE_READ_EA,		true,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_FILE_WRITE_EA,		true,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_FILE_EXECUTE,		true,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_STD_DELETE,		false,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_STD_READ_CONTROL,	true,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_STD_WRITE_DAC,		true,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_STD_WRITE_OWNER,	true,	},
	{ "RW",  SEC_FILE_READ_DATA|SEC_STD_SYNCHRONIZE,	true,	},

	{ "RD",  SEC_FILE_READ_DATA,				true,	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_FILE_WRITE_DATA,	false,	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_FILE_APPEND_DATA,	false,	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_FILE_READ_EA,		true	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_FILE_WRITE_EA,		true,	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_FILE_EXECUTE,		true,	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_STD_DELETE,		true,	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_STD_READ_CONTROL,	true,	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_STD_WRITE_DAC,		true,	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_STD_WRITE_OWNER,	true,	},
	{ "RD",  SEC_FILE_READ_DATA|SEC_STD_SYNCHRONIZE,	true,	},

	{ "WD",  SEC_FILE_WRITE_DATA|SEC_FILE_READ_DATA,	false	},
	{ "WD",  SEC_FILE_WRITE_DATA,				true,	},
	{ "WD",  SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA,	true,	},
	{ "WD",  SEC_FILE_WRITE_DATA|SEC_FILE_READ_EA,		true	},
	{ "WD",  SEC_FILE_WRITE_DATA|SEC_FILE_WRITE_EA,	true,	},
	{ "WD",  SEC_FILE_WRITE_DATA|SEC_FILE_EXECUTE,		false	},
	{ "WD",  SEC_FILE_WRITE_DATA|SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "WD",  SEC_FILE_WRITE_DATA|SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "WD",  SEC_FILE_WRITE_DATA|SEC_STD_DELETE,		true,	},
	{ "WD",  SEC_FILE_WRITE_DATA|SEC_STD_READ_CONTROL,	true,	},
	{ "WD",  SEC_FILE_WRITE_DATA|SEC_STD_WRITE_DAC,	true,	},
	{ "WD",  SEC_FILE_WRITE_DATA|SEC_STD_WRITE_OWNER,	true,	},
	{ "WD",  SEC_FILE_WRITE_DATA|SEC_STD_SYNCHRONIZE,	true,	},

	{ "RWD", SEC_FILE_READ_DATA,				true	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_FILE_WRITE_DATA,	true,	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_FILE_APPEND_DATA,	true,	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_FILE_READ_EA,		true,	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_FILE_WRITE_EA,		true,	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_FILE_EXECUTE,		true,	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_FILE_READ_ATTRIBUTE,	true,	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_FILE_WRITE_ATTRIBUTE,	true,	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_STD_DELETE,		true,	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_STD_READ_CONTROL,	true,	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_STD_WRITE_DAC,		true,	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_STD_WRITE_OWNER,	true,	},
	{ "RWD", SEC_FILE_READ_DATA|SEC_STD_SYNCHRONIZE,	true,	},
};

/*
 * Test conflicting sharemodes through SMB2: First open takes a
 * sharemode, second open with potentially conflicting access.
 */
static bool test_smb2_sharemode_access(struct torture_context *tctx,
				       struct smb2_tree *tree1,
				       struct smb2_tree *tree2)
{
	const char *fname = "test_sharemode";
	NTSTATUS status;
	bool ret = true;
	int i;

	for (i = 0; i < ARRAY_SIZE(sharemode_table); i++) {
		struct sharemode_info *info = &sharemode_table[i];
		struct smb2_create create1 = { 0 }, create2 = { 0 };
		NTSTATUS expected_status;

		torture_comment(tctx, "index %3d, sharemode %3s, "
				"access mask 0x%06x\n",
				i, info->sharemode, info->access_mask);

		create1.in.desired_access = SEC_RIGHTS_FILE_ALL;
		create1.in.alloc_size = 0;
		create1.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
		create1.in.share_access =
			smb2_util_share_access(info->sharemode);
		create1.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
		create1.in.create_options = 0;
		create1.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
		create1.in.fname = fname;
		create1.in.security_flags = 0;
		create1.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
		create1.in.oplock_level = SMB2_OPLOCK_LEVEL_NONE;

		status = smb2_create(tree1, tctx, &create1);

		torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
						"CREATE file failed\n");

		create2.in.desired_access = info->access_mask;
		create2.in.alloc_size = 0;
		create2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
		create2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
			NTCREATEX_SHARE_ACCESS_WRITE |
			NTCREATEX_SHARE_ACCESS_DELETE;
		create2.in.create_disposition = NTCREATEX_DISP_OPEN;
		create2.in.create_options = 0;
		create2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
		create2.in.fname = fname;
		create2.in.security_flags = 0;
		create2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
		create2.in.oplock_level = SMB2_OPLOCK_LEVEL_NONE;

		status = smb2_create(tree2, tctx, &create2);
		expected_status = info->expect_ok ?
			NT_STATUS_OK : NT_STATUS_SHARING_VIOLATION;
		torture_assert_ntstatus_equal_goto(tctx, status,
						   expected_status, ret,
						   done, "Unexpected status on "
						   "second create.\n");

		status = smb2_util_close(tree1, create1.out.file.handle);
		torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
						"Failed to close "
						"first handle.\n");

		if (info->expect_ok) {
			status = smb2_util_close(tree2, create2.out.file.handle);
			torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
							"Failed to close  "
							"second handle.\n");
		}
	}

done:
	smb2_util_unlink(tree1, fname);
	return ret;
}

/*
 * Test conflicting sharemodes through SMB2: First open file with
 * different access masks, second open requests potentially conflicting
 * sharemode.
 */
static bool test_smb2_access_sharemode(struct torture_context *tctx,
				       struct smb2_tree *tree1,
				       struct smb2_tree *tree2)
{
	const char *fname = "test_sharemode";
	NTSTATUS status;
	bool ret = true;
	int i;

	for (i = 0; i < ARRAY_SIZE(sharemode_table); i++) {
		struct sharemode_info *info = &sharemode_table[i];
		struct smb2_create create1 = { 0 }, create2 = { 0 };
		NTSTATUS expected_status;

		torture_comment(tctx, "index %3d, access mask 0x%06x, "
				"sharemode %3s\n",
				i, info->access_mask, info->sharemode);

		create1.in.desired_access = info->access_mask;
		create1.in.alloc_size = 0;
		create1.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
		create1.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
			NTCREATEX_SHARE_ACCESS_WRITE |
			NTCREATEX_SHARE_ACCESS_DELETE;
		create1.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
		create1.in.create_options = 0;
		create1.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
		create1.in.fname = fname;
		create1.in.security_flags = 0;
		create1.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
		create1.in.oplock_level = SMB2_OPLOCK_LEVEL_NONE;

		status = smb2_create(tree1, tctx, &create1);

		torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
						"CREATE file failed\n");

		create2.in.desired_access = SEC_RIGHTS_FILE_ALL;
		create2.in.alloc_size = 0;
		create2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
		create2.in.share_access =
			smb2_util_share_access(info->sharemode);
		create2.in.create_disposition = NTCREATEX_DISP_OPEN;
		create2.in.create_options = 0;
		create2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
		create2.in.fname = fname;
		create2.in.security_flags = 0;
		create2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
		create2.in.oplock_level = SMB2_OPLOCK_LEVEL_NONE;

		status = smb2_create(tree2, tctx, &create2);

		expected_status = info->expect_ok ?
			NT_STATUS_OK : NT_STATUS_SHARING_VIOLATION;
		torture_assert_ntstatus_equal_goto(tctx, status,
						   expected_status, ret,
						   done, "Unexpected status on "
						   "second create.\n");

		status = smb2_util_close(tree1, create1.out.file.handle);
		torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
						"Failed to close "
						"first handle.\n");

		if (info->expect_ok) {
			status = smb2_util_close(tree2, create2.out.file.handle);
			torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
							"Failed to close "
							"second handle.\n");
		}
	}

done:
	smb2_util_unlink(tree1, fname);
	return ret;
}

/*
 * Test initial stat open with share nothing doesn't trigger SHARING_VIOLTION
 * errors.
 */
static bool test_smb2_bug14375(struct torture_context *tctx,
			       struct smb2_tree *tree)
{
	const char *fname = "test_bug14375";
	struct smb2_create cr1;
	struct smb2_create cr2;
	struct smb2_create cr3;
	NTSTATUS status;
	bool ret = true;

	smb2_util_unlink(tree, fname);

	cr1 = (struct smb2_create) {
		.in.desired_access = SEC_FILE_READ_ATTRIBUTE,
		.in.file_attributes = FILE_ATTRIBUTE_NORMAL,
		.in.share_access = NTCREATEX_SHARE_ACCESS_NONE,
		.in.create_disposition = NTCREATEX_DISP_CREATE,
		.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS,
		.in.fname = fname,
	};

	status = smb2_create(tree, tctx, &cr1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"CREATE file failed\n");

	cr2 = (struct smb2_create) {
		.in.desired_access = SEC_FILE_READ_DATA,
		.in.file_attributes = FILE_ATTRIBUTE_NORMAL,
		.in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
		.in.create_disposition = NTCREATEX_DISP_OPEN,
		.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS,
		.in.fname = fname,
	};

	status = smb2_create(tree, tctx, &cr2);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"CREATE file failed\n");

	cr3 = (struct smb2_create) {
		.in.desired_access = SEC_FILE_READ_DATA,
		.in.file_attributes = FILE_ATTRIBUTE_NORMAL,
		.in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
		.in.create_disposition = NTCREATEX_DISP_OPEN,
		.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS,
		.in.fname = fname,
	};

	status = smb2_create(tree, tctx, &cr3);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"CREATE file failed\n");

	status = smb2_util_close(tree, cr1.out.file.handle);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"CLOSE file failed\n");
	status = smb2_util_close(tree, cr2.out.file.handle);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"CLOSE file failed\n");
	status = smb2_util_close(tree, cr3.out.file.handle);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"CLOSE file failed\n");

	cr1 = (struct smb2_create) {
		.in.desired_access = SEC_FILE_READ_DATA,
		.in.file_attributes = FILE_ATTRIBUTE_NORMAL,
		.in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
		.in.create_disposition = NTCREATEX_DISP_OPEN,
		.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS,
		.in.fname = fname,
	};

	status = smb2_create(tree, tctx, &cr1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"CREATE file failed\n");

	cr2 = (struct smb2_create) {
		.in.desired_access = SEC_FILE_READ_ATTRIBUTE,
		.in.file_attributes = FILE_ATTRIBUTE_NORMAL,
		.in.share_access = NTCREATEX_SHARE_ACCESS_NONE,
		.in.create_disposition = NTCREATEX_DISP_OPEN,
		.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS,
		.in.fname = fname,
	};

	status = smb2_create(tree, tctx, &cr2);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"CREATE file failed\n");

	cr3 = (struct smb2_create) {
		.in.desired_access = SEC_FILE_READ_DATA,
		.in.file_attributes = FILE_ATTRIBUTE_NORMAL,
		.in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
		.in.create_disposition = NTCREATEX_DISP_OPEN,
		.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS,
		.in.fname = fname,
	};

	status = smb2_create(tree, tctx, &cr3);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"CREATE file failed\n");

done:
	smb2_util_close(tree, cr1.out.file.handle);
	smb2_util_close(tree, cr2.out.file.handle);
	smb2_util_close(tree, cr3.out.file.handle);
	smb2_util_unlink(tree, fname);
	return ret;
}

struct torture_suite *torture_smb2_sharemode_init(TALLOC_CTX *ctx)
{
	struct torture_suite *suite = torture_suite_create(ctx, "sharemode");

	torture_suite_add_2smb2_test(suite, "sharemode-access",
				     test_smb2_sharemode_access);
	torture_suite_add_2smb2_test(suite, "access-sharemode",
				     test_smb2_access_sharemode);
	torture_suite_add_1smb2_test(suite, "bug14375",
				     test_smb2_bug14375);

	suite->description = talloc_strdup(suite, "SMB2-SHAREMODE tests");

	return suite;
}