s4 torture: Add a new torture:hide_on_access_denied parameter
[metze/samba/wip.git] / source4 / torture / smb2 / create.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 create test suite
5
6    Copyright (C) Andrew Tridgell 2008
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25 #include "torture/torture.h"
26 #include "torture/util.h"
27 #include "torture/smb2/proto.h"
28 #include "librpc/gen_ndr/ndr_security.h"
29 #include "libcli/security/security.h"
30
31 #include "system/filesys.h"
32 #include "auth/credentials/credentials.h"
33 #include "lib/cmdline/popt_common.h"
34 #include "librpc/gen_ndr/security.h"
35 #include "lib/events/events.h"
36
37 #define FNAME "test_create.dat"
38 #define DNAME "smb2_open"
39
40 #define CHECK_STATUS(status, correct) do { \
41         if (!NT_STATUS_EQUAL(status, correct)) { \
42                 torture_result(tctx, TORTURE_FAIL, \
43                         "(%s) Incorrect status %s - should be %s\n", \
44                          __location__, nt_errstr(status), nt_errstr(correct)); \
45                 return false; \
46         }} while (0)
47
48 #define CHECK_EQUAL(v, correct) do { \
49         if (v != correct) { \
50                 torture_result(tctx, TORTURE_FAIL, \
51                         "(%s) Incorrect value for %s 0x%08llx - " \
52                         "should be 0x%08llx\n", \
53                          __location__, #v, \
54                         (unsigned long long)v, \
55                         (unsigned long long)correct); \
56                 return false;                                   \
57         }} while (0)
58
59 #define CHECK_TIME(t, field) do { \
60         time_t t1, t2; \
61         finfo.all_info.level = RAW_FILEINFO_ALL_INFORMATION; \
62         finfo.all_info.in.file.handle = h1; \
63         status = smb2_getinfo_file(tree, tctx, &finfo); \
64         CHECK_STATUS(status, NT_STATUS_OK); \
65         t1 = t & ~1; \
66         t2 = nt_time_to_unix(finfo.all_info.out.field) & ~1; \
67         if (abs(t1-t2) > 2) { \
68                 torture_result(tctx, TORTURE_FAIL, \
69                         "(%s) wrong time for field %s  %s - %s\n", \
70                         __location__, #field, \
71                         timestring(tctx, t1), \
72                         timestring(tctx, t2)); \
73                 dump_all_info(tctx, &finfo); \
74                 ret = false; \
75         }} while (0)
76
77 #define CHECK_NTTIME(t, field) do { \
78         NTTIME t2; \
79         finfo.all_info.level = RAW_FILEINFO_ALL_INFORMATION; \
80         finfo.all_info.in.file.handle = h1; \
81         status = smb2_getinfo_file(tree, tctx, &finfo); \
82         CHECK_STATUS(status, NT_STATUS_OK); \
83         t2 = finfo.all_info.out.field; \
84         if (t != t2) { \
85                 torture_result(tctx, TORTURE_FAIL, \
86                         "(%s) wrong time for field %s  %s - %s\n", \
87                        __location__, #field, \
88                        nt_time_string(tctx, t), \
89                        nt_time_string(tctx, t2)); \
90                 dump_all_info(tctx, &finfo); \
91                 ret = false; \
92         }} while (0)
93
94 #define CHECK_ALL_INFO(v, field) do { \
95         finfo.all_info.level = RAW_FILEINFO_ALL_INFORMATION; \
96         finfo.all_info.in.file.handle = h1; \
97         status = smb2_getinfo_file(tree, tctx, &finfo); \
98         CHECK_STATUS(status, NT_STATUS_OK); \
99         if ((v) != (finfo.all_info.out.field)) { \
100                torture_result(tctx, TORTURE_FAIL, \
101                         "(%s) wrong value for field %s  0x%x - 0x%x\n", \
102                         __location__, #field, (int)v,\
103                         (int)(finfo.all_info.out.field)); \
104                 dump_all_info(tctx, &finfo); \
105                 ret = false; \
106         }} while (0)
107
108 #define CHECK_VAL(v, correct) do { \
109         if ((v) != (correct)) { \
110                 torture_result(tctx, TORTURE_FAIL, \
111                         "(%s) wrong value for %s  0x%x - should be 0x%x\n", \
112                        __location__, #v, (int)(v), (int)correct); \
113                 ret = false; \
114         }} while (0)
115
116 #define SET_ATTRIB(sattrib) do { \
117         union smb_setfileinfo sfinfo; \
118         ZERO_STRUCT(sfinfo.basic_info.in); \
119         sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION; \
120         sfinfo.basic_info.in.file.handle = h1; \
121         sfinfo.basic_info.in.attrib = sattrib; \
122         status = smb2_setinfo_file(tree, &sfinfo); \
123         if (!NT_STATUS_IS_OK(status)) { \
124                 torture_comment(tctx, \
125                     "(%s) Failed to set attrib 0x%x on %s\n", \
126                        __location__, sattrib, fname); \
127         }} while (0)
128
129 /*
130   test some interesting combinations found by gentest
131  */
132 static bool test_create_gentest(struct torture_context *tctx, struct smb2_tree *tree)
133 {
134         struct smb2_create io;
135         NTSTATUS status;
136         uint32_t access_mask, file_attributes_set;
137         uint32_t ok_mask, not_supported_mask, invalid_parameter_mask;
138         uint32_t not_a_directory_mask, unexpected_mask;
139         union smb_fileinfo q;
140
141         ZERO_STRUCT(io);
142         io.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED;
143         io.in.file_attributes    = FILE_ATTRIBUTE_NORMAL;
144         io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
145         io.in.share_access = 
146                 NTCREATEX_SHARE_ACCESS_DELETE|
147                 NTCREATEX_SHARE_ACCESS_READ|
148                 NTCREATEX_SHARE_ACCESS_WRITE;
149         io.in.create_options = 0;
150         io.in.fname = FNAME;
151
152         status = smb2_create(tree, tctx, &io);
153         CHECK_STATUS(status, NT_STATUS_OK);
154
155         status = smb2_util_close(tree, io.out.file.handle);
156         CHECK_STATUS(status, NT_STATUS_OK);
157
158         io.in.create_options = 0xF0000000;
159         status = smb2_create(tree, tctx, &io);
160         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
161
162         io.in.create_options = 0;
163
164         io.in.file_attributes = FILE_ATTRIBUTE_DEVICE;
165         status = smb2_create(tree, tctx, &io);
166         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
167
168         io.in.file_attributes = FILE_ATTRIBUTE_VOLUME;
169         status = smb2_create(tree, tctx, &io);
170         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
171
172         io.in.create_disposition = NTCREATEX_DISP_OPEN;
173         io.in.file_attributes = FILE_ATTRIBUTE_VOLUME;
174         status = smb2_create(tree, tctx, &io);
175         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
176         
177         io.in.create_disposition = NTCREATEX_DISP_CREATE;
178         io.in.desired_access = 0x08000000;
179         status = smb2_create(tree, tctx, &io);
180         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
181
182         io.in.desired_access = 0x04000000;
183         status = smb2_create(tree, tctx, &io);
184         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
185
186         io.in.file_attributes = 0;
187         io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
188         io.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED;
189         ok_mask = 0;
190         not_supported_mask = 0;
191         invalid_parameter_mask = 0;
192         not_a_directory_mask = 0;
193         unexpected_mask = 0;
194         {
195                 int i;
196                 for (i=0;i<32;i++) {
197                         io.in.create_options = 1<<i;
198                         if (io.in.create_options & NTCREATEX_OPTIONS_DELETE_ON_CLOSE) {
199                                 continue;
200                         }
201                         status = smb2_create(tree, tctx, &io);
202                         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
203                                 not_supported_mask |= 1<<i;
204                         } else if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
205                                 invalid_parameter_mask |= 1<<i;
206                         } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_A_DIRECTORY)) {
207                                 not_a_directory_mask |= 1<<i;
208                         } else if (NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
209                                 ok_mask |= 1<<i;
210                                 status = smb2_util_close(tree, io.out.file.handle);
211                                 CHECK_STATUS(status, NT_STATUS_OK);
212                         } else {
213                                 unexpected_mask |= 1<<i;
214                                 torture_comment(tctx,
215                                     "create option 0x%08x returned %s\n",
216                                     1<<i, nt_errstr(status));
217                         }
218                 }
219         }
220         io.in.create_options = 0;
221
222         CHECK_EQUAL(ok_mask,                0x00efcf7e);
223         CHECK_EQUAL(not_a_directory_mask,   0x00000001);
224         CHECK_EQUAL(not_supported_mask,     0x00102080);
225         CHECK_EQUAL(invalid_parameter_mask, 0xff000000);
226         CHECK_EQUAL(unexpected_mask,        0x00000000);
227
228         io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
229         io.in.file_attributes = 0;
230         access_mask = 0;
231         {
232                 int i;
233                 for (i=0;i<32;i++) {
234                         io.in.desired_access = 1<<i;
235                         status = smb2_create(tree, tctx, &io);
236                         if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
237                             NT_STATUS_EQUAL(status, NT_STATUS_PRIVILEGE_NOT_HELD)) {
238                                 access_mask |= io.in.desired_access;
239                         } else {
240                                 CHECK_STATUS(status, NT_STATUS_OK);
241                                 status = smb2_util_close(tree, io.out.file.handle);
242                                 CHECK_STATUS(status, NT_STATUS_OK);
243                         }
244                 }
245         }
246
247         if (TARGET_IS_WIN7(tctx)) {
248                 CHECK_EQUAL(access_mask, 0x0de0fe00);
249         } else if (torture_setting_bool(tctx, "samba4", false)) {
250                 CHECK_EQUAL(access_mask, 0x0cf0fe00);
251         } else {
252                 CHECK_EQUAL(access_mask, 0x0df0fe00);
253         }
254
255         io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
256         io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
257         io.in.file_attributes = 0;
258         ok_mask = 0;
259         invalid_parameter_mask = 0;
260         unexpected_mask = 0;
261         file_attributes_set = 0;
262         {
263                 int i;
264                 for (i=0;i<32;i++) {
265                         io.in.file_attributes = 1<<i;
266                         if (io.in.file_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
267                                 continue;
268                         }
269                         smb2_deltree(tree, FNAME);
270                         status = smb2_create(tree, tctx, &io);
271                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
272                                 invalid_parameter_mask |= 1<<i;
273                         } else if (NT_STATUS_IS_OK(status)) {
274                                 uint32_t expected;
275                                 ok_mask |= 1<<i;
276
277                                 expected = (io.in.file_attributes | FILE_ATTRIBUTE_ARCHIVE) & 0x00005127;
278                                 CHECK_EQUAL(io.out.file_attr, expected);
279                                 file_attributes_set |= io.out.file_attr;
280
281                                 status = smb2_util_close(tree, io.out.file.handle);
282                                 CHECK_STATUS(status, NT_STATUS_OK);
283                         } else {
284                                 unexpected_mask |= 1<<i;
285                                 torture_comment(tctx,
286                                     "file attribute 0x%08x returned %s\n",
287                                     1<<i, nt_errstr(status));
288                         }
289                 }
290         }
291
292         CHECK_EQUAL(ok_mask,                0x00003fb7);
293         CHECK_EQUAL(invalid_parameter_mask, 0xffff8048);
294         CHECK_EQUAL(unexpected_mask,        0x00000000);
295         CHECK_EQUAL(file_attributes_set,    0x00001127);
296
297         smb2_deltree(tree, FNAME);
298
299         /*
300          * Standalone servers doesn't support encryption
301          */
302         io.in.file_attributes = FILE_ATTRIBUTE_ENCRYPTED;
303         status = smb2_create(tree, tctx, &io);
304         if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
305                 torture_comment(tctx,
306                     "FILE_ATTRIBUTE_ENCRYPTED returned %s\n",
307                     nt_errstr(status));
308         } else {
309                 CHECK_STATUS(status, NT_STATUS_OK);
310                 CHECK_EQUAL(io.out.file_attr, (FILE_ATTRIBUTE_ENCRYPTED | FILE_ATTRIBUTE_ARCHIVE));
311                 status = smb2_util_close(tree, io.out.file.handle);
312                 CHECK_STATUS(status, NT_STATUS_OK);
313         }
314
315         smb2_deltree(tree, FNAME);
316
317         ZERO_STRUCT(io);
318         io.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED;
319         io.in.file_attributes    = 0;
320         io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
321         io.in.share_access = 
322                 NTCREATEX_SHARE_ACCESS_READ|
323                 NTCREATEX_SHARE_ACCESS_WRITE;
324         io.in.create_options = 0;
325         io.in.fname = FNAME ":stream1";
326         status = smb2_create(tree, tctx, &io);
327         CHECK_STATUS(status, NT_STATUS_OK);
328
329         status = smb2_util_close(tree, io.out.file.handle);
330         CHECK_STATUS(status, NT_STATUS_OK);
331
332         io.in.fname = FNAME;
333         io.in.file_attributes = 0x8040;
334         io.in.share_access = 
335                 NTCREATEX_SHARE_ACCESS_READ;
336         status = smb2_create(tree, tctx, &io);
337         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
338
339         io.in.fname = FNAME;
340         io.in.file_attributes = 0;
341         io.in.desired_access  = SEC_FILE_READ_DATA | SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA;
342         io.in.query_maximal_access = true;
343         status = smb2_create(tree, tctx, &io);
344         CHECK_STATUS(status, NT_STATUS_OK);
345         CHECK_EQUAL(io.out.maximal_access, 0x001f01ff);
346
347         q.access_information.level = RAW_FILEINFO_ACCESS_INFORMATION;
348         q.access_information.in.file.handle = io.out.file.handle;
349         status = smb2_getinfo_file(tree, tctx, &q);
350         CHECK_STATUS(status, NT_STATUS_OK);
351         CHECK_EQUAL(q.access_information.out.access_flags, io.in.desired_access);
352
353         io.in.file_attributes = 0;
354         io.in.desired_access  = 0;
355         io.in.query_maximal_access = false;
356         io.in.share_access = 0;
357         status = smb2_create(tree, tctx, &io);
358         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
359         
360         smb2_deltree(tree, FNAME);
361
362         return true;
363 }
364
365
366 /*
367   try the various request blobs
368  */
369 static bool test_create_blob(struct torture_context *tctx, struct smb2_tree *tree)
370 {
371         struct smb2_create io;
372         NTSTATUS status;
373
374         smb2_deltree(tree, FNAME);
375
376         ZERO_STRUCT(io);
377         io.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED;
378         io.in.file_attributes    = FILE_ATTRIBUTE_NORMAL;
379         io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
380         io.in.share_access = 
381                 NTCREATEX_SHARE_ACCESS_DELETE|
382                 NTCREATEX_SHARE_ACCESS_READ|
383                 NTCREATEX_SHARE_ACCESS_WRITE;
384         io.in.create_options            = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
385                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
386                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
387                                           0x00200000;
388         io.in.fname = FNAME;
389
390         status = smb2_create(tree, tctx, &io);
391         CHECK_STATUS(status, NT_STATUS_OK);
392
393         status = smb2_util_close(tree, io.out.file.handle);
394         CHECK_STATUS(status, NT_STATUS_OK);
395
396         torture_comment(tctx, "testing alloc size\n");
397         io.in.alloc_size = 4096;
398         status = smb2_create(tree, tctx, &io);
399         CHECK_STATUS(status, NT_STATUS_OK);
400         CHECK_EQUAL(io.out.alloc_size, io.in.alloc_size);
401
402         status = smb2_util_close(tree, io.out.file.handle);
403         CHECK_STATUS(status, NT_STATUS_OK);
404
405         torture_comment(tctx, "testing durable open\n");
406         io.in.durable_open = true;
407         status = smb2_create(tree, tctx, &io);
408         CHECK_STATUS(status, NT_STATUS_OK);
409
410         status = smb2_util_close(tree, io.out.file.handle);
411         CHECK_STATUS(status, NT_STATUS_OK);
412
413         torture_comment(tctx, "testing query maximal access\n");
414         io.in.query_maximal_access = true;
415         status = smb2_create(tree, tctx, &io);
416         CHECK_STATUS(status, NT_STATUS_OK);
417         CHECK_EQUAL(io.out.maximal_access, 0x001f01ff);
418
419         status = smb2_util_close(tree, io.out.file.handle);
420         CHECK_STATUS(status, NT_STATUS_OK);
421
422         torture_comment(tctx, "testing timewarp\n");
423         io.in.timewarp = 10000;
424         status = smb2_create(tree, tctx, &io);
425         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
426         io.in.timewarp = 0;
427
428         torture_comment(tctx, "testing query_on_disk\n");
429         io.in.query_on_disk_id = true;
430         status = smb2_create(tree, tctx, &io);
431         CHECK_STATUS(status, NT_STATUS_OK);
432
433         status = smb2_util_close(tree, io.out.file.handle);
434         CHECK_STATUS(status, NT_STATUS_OK);
435
436         torture_comment(tctx, "testing unknown tag\n");
437         status = smb2_create_blob_add(tctx, &io.in.blobs,
438                                       "FooO", data_blob(NULL, 0));
439         CHECK_STATUS(status, NT_STATUS_OK);
440
441         status = smb2_create(tree, tctx, &io);
442         CHECK_STATUS(status, NT_STATUS_OK);
443
444         status = smb2_util_close(tree, io.out.file.handle);
445         CHECK_STATUS(status, NT_STATUS_OK);
446
447         torture_comment(tctx, "testing bad tag length\n");
448         status = smb2_create_blob_add(tctx, &io.in.blobs,
449                                       "xxx", data_blob(NULL, 0));
450         CHECK_STATUS(status, NT_STATUS_OK);
451
452         status = smb2_create(tree, tctx, &io);
453         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
454
455         smb2_deltree(tree, FNAME);
456         
457         return true;
458 }
459
460 #define FAIL_UNLESS(__cond)                                     \
461         do {                                                    \
462                 if (__cond) {} else {                           \
463                         torture_result(tctx, TORTURE_FAIL, "%s) condition violated: %s\n",      \
464                                __location__, #__cond);          \
465                         ret = false; goto done;                 \
466                 }                                               \
467         } while(0)
468
469 /*
470   try creating with acls
471  */
472 static bool test_create_acl_ext(struct torture_context *tctx, struct smb2_tree *tree, bool test_dir)
473 {
474         bool ret = true;
475         struct smb2_create io;
476         NTSTATUS status;
477         struct security_ace ace;
478         struct security_descriptor *sd, *sd2;
479         struct dom_sid *test_sid;
480         union smb_fileinfo q = {};
481         uint32_t attrib =
482             FILE_ATTRIBUTE_HIDDEN |
483             FILE_ATTRIBUTE_SYSTEM |
484             (test_dir ? FILE_ATTRIBUTE_DIRECTORY : 0);
485         NTSTATUS (*delete_func)(struct smb2_tree *, const char *) =
486             test_dir ? smb2_util_rmdir : smb2_util_unlink;
487
488         smb2_deltree(tree, FNAME);
489
490         ZERO_STRUCT(io);
491         io.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED;
492         io.in.file_attributes    = FILE_ATTRIBUTE_NORMAL;
493         io.in.create_disposition = NTCREATEX_DISP_CREATE;
494         io.in.share_access = 
495                 NTCREATEX_SHARE_ACCESS_DELETE |
496                 NTCREATEX_SHARE_ACCESS_READ |
497                 NTCREATEX_SHARE_ACCESS_WRITE;
498         io.in.create_options = NTCREATEX_OPTIONS_ASYNC_ALERT | 0x00200000 |
499             (test_dir ?  NTCREATEX_OPTIONS_DIRECTORY :
500                 (NTCREATEX_OPTIONS_NON_DIRECTORY_FILE));
501
502         io.in.fname = FNAME;
503
504         torture_comment(tctx, "basic create\n");
505
506         status = smb2_create(tree, tctx, &io);
507         CHECK_STATUS(status, NT_STATUS_OK);
508
509         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
510         q.query_secdesc.in.file.handle = io.out.file.handle;
511         q.query_secdesc.in.secinfo_flags = 
512                 SECINFO_OWNER |
513                 SECINFO_GROUP |
514                 SECINFO_DACL;
515         status = smb2_getinfo_file(tree, tctx, &q);
516         CHECK_STATUS(status, NT_STATUS_OK);
517         sd = q.query_secdesc.out.sd;
518
519         status = smb2_util_close(tree, io.out.file.handle);
520         CHECK_STATUS(status, NT_STATUS_OK);
521         status = delete_func(tree, FNAME);
522         CHECK_STATUS(status, NT_STATUS_OK);
523
524         torture_comment(tctx, "adding a new ACE\n");
525         test_sid = dom_sid_parse_talloc(tctx, SID_NT_AUTHENTICATED_USERS);
526
527         ace.type = SEC_ACE_TYPE_ACCESS_ALLOWED;
528         ace.flags = 0;
529         ace.access_mask = SEC_STD_ALL;
530         ace.trustee = *test_sid;
531
532         status = security_descriptor_dacl_add(sd, &ace);
533         CHECK_STATUS(status, NT_STATUS_OK);
534
535         torture_comment(tctx, "creating a file with an initial ACL\n");
536
537         io.in.sec_desc = sd;
538         status = smb2_create(tree, tctx, &io);
539         CHECK_STATUS(status, NT_STATUS_OK);
540
541         FAIL_UNLESS(smb2_util_verify_sd(tctx, tree, io.out.file.handle, sd));
542
543         status = smb2_util_close(tree, io.out.file.handle);
544         CHECK_STATUS(status, NT_STATUS_OK);
545         status = delete_func(tree, FNAME);
546         CHECK_STATUS(status, NT_STATUS_OK);
547
548         torture_comment(tctx, "creating with attributes\n");
549
550         io.in.sec_desc = NULL;
551         io.in.file_attributes = attrib;
552         status = smb2_create(tree, tctx, &io);
553         CHECK_STATUS(status, NT_STATUS_OK);
554
555         FAIL_UNLESS(smb2_util_verify_attrib(tctx, tree, io.out.file.handle, attrib));
556
557         status = smb2_util_close(tree, io.out.file.handle);
558         CHECK_STATUS(status, NT_STATUS_OK);
559         status = delete_func(tree, FNAME);
560         CHECK_STATUS(status, NT_STATUS_OK);
561
562         torture_comment(tctx, "creating with attributes and ACL\n");
563
564         io.in.sec_desc = sd;
565         io.in.file_attributes = attrib;
566         status = smb2_create(tree, tctx, &io);
567         CHECK_STATUS(status, NT_STATUS_OK);
568
569         FAIL_UNLESS(smb2_util_verify_sd(tctx, tree, io.out.file.handle, sd));
570         FAIL_UNLESS(smb2_util_verify_attrib(tctx, tree, io.out.file.handle, attrib));
571         
572         status = smb2_util_close(tree, io.out.file.handle);
573         CHECK_STATUS(status, NT_STATUS_OK);
574         status = delete_func(tree, FNAME);
575         CHECK_STATUS(status, NT_STATUS_OK);
576
577         torture_comment(tctx, "creating with attributes, ACL and owner\n");
578         sd = security_descriptor_dacl_create(tctx,
579                                         0, SID_WORLD, SID_BUILTIN_USERS,
580                                         SID_WORLD,
581                                         SEC_ACE_TYPE_ACCESS_ALLOWED,
582                                         SEC_RIGHTS_FILE_READ | SEC_STD_ALL,
583                                         0,
584                                         NULL);
585
586         io.in.sec_desc = sd;
587         io.in.file_attributes = attrib;
588         status = smb2_create(tree, tctx, &io);
589         CHECK_STATUS(status, NT_STATUS_OK);
590
591         FAIL_UNLESS(smb2_util_verify_sd(tctx, tree, io.out.file.handle, sd));
592         FAIL_UNLESS(smb2_util_verify_attrib(tctx, tree, io.out.file.handle, attrib));
593
594  done:
595         status = smb2_util_close(tree, io.out.file.handle);
596         CHECK_STATUS(status, NT_STATUS_OK);
597         status = delete_func(tree, FNAME);
598         CHECK_STATUS(status, NT_STATUS_OK);
599
600         return ret;
601 }
602
603 /*
604   test SMB2 open
605 */
606 static bool test_smb2_open(struct torture_context *tctx,
607                            struct smb2_tree *tree)
608 {
609         union smb_open io;
610         union smb_fileinfo finfo;
611         const char *fname = DNAME "\\torture_ntcreatex.txt";
612         const char *dname = DNAME "\\torture_ntcreatex.dir";
613         NTSTATUS status;
614         struct smb2_handle h, h1;
615         bool ret = true;
616         int i;
617         struct {
618                 uint32_t create_disp;
619                 bool with_file;
620                 NTSTATUS correct_status;
621         } open_funcs[] = {
622                 { NTCREATEX_DISP_SUPERSEDE,     true,  NT_STATUS_OK },
623                 { NTCREATEX_DISP_SUPERSEDE,     false, NT_STATUS_OK },
624                 { NTCREATEX_DISP_OPEN,          true,  NT_STATUS_OK },
625                 { NTCREATEX_DISP_OPEN,          false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
626                 { NTCREATEX_DISP_CREATE,        true,  NT_STATUS_OBJECT_NAME_COLLISION },
627                 { NTCREATEX_DISP_CREATE,        false, NT_STATUS_OK },
628                 { NTCREATEX_DISP_OPEN_IF,       true,  NT_STATUS_OK },
629                 { NTCREATEX_DISP_OPEN_IF,       false, NT_STATUS_OK },
630                 { NTCREATEX_DISP_OVERWRITE,     true,  NT_STATUS_OK },
631                 { NTCREATEX_DISP_OVERWRITE,     false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
632                 { NTCREATEX_DISP_OVERWRITE_IF,  true,  NT_STATUS_OK },
633                 { NTCREATEX_DISP_OVERWRITE_IF,  false, NT_STATUS_OK },
634                 { 6,                            true,  NT_STATUS_INVALID_PARAMETER },
635                 { 6,                            false, NT_STATUS_INVALID_PARAMETER },
636         };
637
638         torture_comment(tctx, "Checking SMB2 Open\n");
639
640         smb2_util_unlink(tree, fname);
641         smb2_util_rmdir(tree, dname);
642
643         status = torture_smb2_testdir(tree, DNAME, &h);
644         CHECK_STATUS(status, NT_STATUS_OK);
645
646         ZERO_STRUCT(io.smb2);
647         /* reasonable default parameters */
648         io.generic.level = RAW_OPEN_SMB2;
649         io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
650         io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
651         io.smb2.in.alloc_size = 1024*1024;
652         io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
653         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
654         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
655         io.smb2.in.create_options = 0;
656         io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
657         io.smb2.in.security_flags = 0;
658         io.smb2.in.fname = fname;
659
660         /* test the create disposition */
661         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
662                 if (open_funcs[i].with_file) {
663                         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
664                         status= smb2_create(tree, tctx, &(io.smb2));
665                         if (!NT_STATUS_IS_OK(status)) {
666                                 torture_comment(tctx,
667                                     "Failed to create file %s status %s %d\n",
668                                     fname, nt_errstr(status), i);
669
670                                 ret = false;
671                                 goto done;
672                         }
673                         smb2_util_close(tree, io.smb2.out.file.handle);
674                 }
675                 io.smb2.in.create_disposition = open_funcs[i].create_disp;
676                 status = smb2_create(tree, tctx, &(io.smb2));
677                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
678                         torture_comment(tctx,
679                             "(%s) incorrect status %s should be %s (i=%d "
680                             "with_file=%d open_disp=%d)\n",
681                          __location__, nt_errstr(status),
682                         nt_errstr(open_funcs[i].correct_status),
683                         i, (int)open_funcs[i].with_file,
684                         (int)open_funcs[i].create_disp);
685
686                         ret = false;
687                         goto done;
688                 }
689                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
690                         smb2_util_close(tree, io.smb2.out.file.handle);
691                         smb2_util_unlink(tree, fname);
692                 }
693         }
694
695         /* basic field testing */
696         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
697
698         status = smb2_create(tree, tctx, &(io.smb2));
699         CHECK_STATUS(status, NT_STATUS_OK);
700         h1 = io.smb2.out.file.handle;
701
702         CHECK_VAL(io.smb2.out.oplock_level, 0);
703         CHECK_VAL(io.smb2.out.create_action, NTCREATEX_ACTION_CREATED);
704         CHECK_NTTIME(io.smb2.out.create_time, create_time);
705         CHECK_NTTIME(io.smb2.out.access_time, access_time);
706         CHECK_NTTIME(io.smb2.out.write_time, write_time);
707         CHECK_NTTIME(io.smb2.out.change_time, change_time);
708         CHECK_ALL_INFO(io.smb2.out.file_attr, attrib);
709         CHECK_ALL_INFO(io.smb2.out.alloc_size, alloc_size);
710         CHECK_ALL_INFO(io.smb2.out.size, size);
711
712         /* check fields when the file already existed */
713         smb2_util_close(tree, h1);
714         smb2_util_unlink(tree, fname);
715
716         status = smb2_create_complex_file(tree, fname, &h1);
717         CHECK_STATUS(status, NT_STATUS_OK);
718
719         smb2_util_close(tree, h1);
720
721         io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
722         status = smb2_create(tree, tctx, &(io.smb2));
723         CHECK_STATUS(status, NT_STATUS_OK);
724         h1 = io.smb2.out.file.handle;
725
726         CHECK_VAL(io.smb2.out.oplock_level, 0);
727         CHECK_VAL(io.smb2.out.create_action, NTCREATEX_ACTION_EXISTED);
728         CHECK_NTTIME(io.smb2.out.create_time, create_time);
729         CHECK_NTTIME(io.smb2.out.access_time, access_time);
730         CHECK_NTTIME(io.smb2.out.write_time, write_time);
731         CHECK_NTTIME(io.smb2.out.change_time, change_time);
732         CHECK_ALL_INFO(io.smb2.out.file_attr, attrib);
733         CHECK_ALL_INFO(io.smb2.out.alloc_size, alloc_size);
734         CHECK_ALL_INFO(io.smb2.out.size, size);
735         smb2_util_close(tree, h1);
736         smb2_util_unlink(tree, fname);
737
738         /* create a directory */
739         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
740         io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
741         io.smb2.in.alloc_size = 0;
742         io.smb2.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
743         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
744         io.smb2.in.create_options = 0;
745         io.smb2.in.fname = dname;
746         fname = dname;
747
748         smb2_util_rmdir(tree, fname);
749         smb2_util_unlink(tree, fname);
750
751         io.smb2.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
752         io.smb2.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
753         io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
754         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
755                                 NTCREATEX_SHARE_ACCESS_WRITE;
756         status = smb2_create(tree, tctx, &(io.smb2));
757         CHECK_STATUS(status, NT_STATUS_OK);
758         h1 = io.smb2.out.file.handle;
759
760         CHECK_VAL(io.smb2.out.oplock_level, 0);
761         CHECK_VAL(io.smb2.out.create_action, NTCREATEX_ACTION_CREATED);
762         CHECK_NTTIME(io.smb2.out.create_time, create_time);
763         CHECK_NTTIME(io.smb2.out.access_time, access_time);
764         CHECK_NTTIME(io.smb2.out.write_time, write_time);
765         CHECK_NTTIME(io.smb2.out.change_time, change_time);
766         CHECK_ALL_INFO(io.smb2.out.file_attr, attrib);
767         CHECK_VAL(io.smb2.out.file_attr & ~FILE_ATTRIBUTE_NONINDEXED,
768                   FILE_ATTRIBUTE_DIRECTORY);
769         CHECK_ALL_INFO(io.smb2.out.alloc_size, alloc_size);
770         CHECK_ALL_INFO(io.smb2.out.size, size);
771         CHECK_VAL(io.smb2.out.size, 0);
772         CHECK_VAL(io.smb2.out.alloc_size, 0);
773         smb2_util_unlink(tree, fname);
774
775 done:
776         smb2_util_close(tree, h1);
777         smb2_util_unlink(tree, fname);
778         smb2_deltree(tree, DNAME);
779         return ret;
780 }
781
782 /*
783   test with an already opened and byte range locked file
784 */
785
786 static bool test_smb2_open_brlocked(struct torture_context *tctx,
787                                     struct smb2_tree *tree)
788 {
789         union smb_open io, io1;
790         union smb_lock io2;
791         struct smb2_lock_element lock[1];
792         const char *fname = DNAME "\\torture_ntcreatex.txt";
793         NTSTATUS status;
794         bool ret = true;
795         struct smb2_handle h;
796
797         torture_comment(tctx,
798                 "Testing SMB2 open with a byte range locked file\n");
799
800         smb2_util_unlink(tree, fname);
801
802         status = torture_smb2_testdir(tree, DNAME, &h);
803         CHECK_STATUS(status, NT_STATUS_OK);
804
805         ZERO_STRUCT(io.smb2);
806         io.generic.level = RAW_OPEN_SMB2;
807         io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
808         io.smb2.in.desired_access = 0x2019f;
809         io.smb2.in.alloc_size = 0;
810         io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
811         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
812                 NTCREATEX_SHARE_ACCESS_WRITE;
813         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
814         io.smb2.in.create_options = NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
815         io.smb2.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
816         io.smb2.in.security_flags = SMB2_SECURITY_DYNAMIC_TRACKING;
817         io.smb2.in.fname = fname;
818
819         status = smb2_create(tree, tctx, &(io.smb2));
820         CHECK_STATUS(status, NT_STATUS_OK);
821
822         ZERO_STRUCT(io2.smb2);
823         io2.smb2.level = RAW_LOCK_SMB2;
824         io2.smb2.in.file.handle = io.smb2.out.file.handle;
825         io2.smb2.in.lock_count = 1;
826
827         lock[0].offset = 0;
828         lock[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
829                         SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
830         io2.smb2.in.locks = &lock[0];
831         status = smb2_lock(tree, &(io2.smb2));
832         CHECK_STATUS(status, NT_STATUS_OK);
833
834         ZERO_STRUCT(io1.smb2);
835         io1.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
836         io1.smb2.in.desired_access = 0x20196;
837         io1.smb2.in.alloc_size = 0;
838         io1.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
839         io1.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
840                 NTCREATEX_SHARE_ACCESS_WRITE;
841         io1.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
842         io1.smb2.in.create_options = 0;
843         io1.smb2.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
844         io1.smb2.in.security_flags = SMB2_SECURITY_DYNAMIC_TRACKING;
845         io1.smb2.in.fname = fname;
846
847         status = smb2_create(tree, tctx, &(io1.smb2));
848         CHECK_STATUS(status, NT_STATUS_OK);
849
850         smb2_util_close(tree, io.smb2.out.file.handle);
851         smb2_util_close(tree, io1.smb2.out.file.handle);
852         smb2_util_unlink(tree, fname);
853         smb2_deltree(tree, DNAME);
854
855         return ret;
856 }
857
858 /* A little torture test to expose a race condition in Samba 3.0.20 ... :-) */
859
860 static bool test_smb2_open_multi(struct torture_context *tctx,
861                                 struct smb2_tree *tree)
862 {
863         const char *fname = "test_oplock.dat";
864         NTSTATUS status;
865         bool ret = true;
866         union smb_open io;
867         struct smb2_tree **trees;
868         struct smb2_request **requests;
869         union smb_open *ios;
870         int i, num_files = 3;
871         int num_ok = 0;
872         int num_collision = 0;
873
874         torture_comment(tctx,
875                 "Testing SMB2 Open with multiple connections\n");
876         trees = talloc_array(tctx, struct smb2_tree *, num_files);
877         requests = talloc_array(tctx, struct smb2_request *, num_files);
878         ios = talloc_array(tctx, union smb_open, num_files);
879         if ((tctx->ev == NULL) || (trees == NULL) || (requests == NULL) ||
880             (ios == NULL)) {
881                 torture_comment(tctx, ("talloc failed\n"));
882                 ret = false;
883                 goto done;
884         }
885
886         tree->session->transport->options.request_timeout = 60;
887
888         for (i=0; i<num_files; i++) {
889                 if (!torture_smb2_connection(tctx, &(trees[i]))) {
890                         torture_comment(tctx,
891                                 "Could not open %d'th connection\n", i);
892                         ret = false;
893                         goto done;
894                 }
895                 trees[i]->session->transport->options.request_timeout = 60;
896         }
897
898         /* cleanup */
899         smb2_util_unlink(tree, fname);
900
901         /*
902           base ntcreatex parms
903         */
904         ZERO_STRUCT(io.smb2);
905         io.generic.level = RAW_OPEN_SMB2;
906         io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
907         io.smb2.in.alloc_size = 0;
908         io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
909         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
910                 NTCREATEX_SHARE_ACCESS_WRITE|
911                 NTCREATEX_SHARE_ACCESS_DELETE;
912         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
913         io.smb2.in.create_options = 0;
914         io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
915         io.smb2.in.security_flags = 0;
916         io.smb2.in.fname = fname;
917         io.smb2.in.create_flags = 0;
918
919         for (i=0; i<num_files; i++) {
920                 ios[i] = io;
921                 requests[i] = smb2_create_send(trees[i], &(ios[i].smb2));
922                 if (requests[i] == NULL) {
923                         torture_comment(tctx,
924                                 "could not send %d'th request\n", i);
925                         ret = false;
926                         goto done;
927                 }
928         }
929
930         torture_comment(tctx, "waiting for replies\n");
931         while (1) {
932                 bool unreplied = false;
933                 for (i=0; i<num_files; i++) {
934                         if (requests[i] == NULL) {
935                                 continue;
936                         }
937                         if (requests[i]->state < SMB2_REQUEST_DONE) {
938                                 unreplied = true;
939                                 break;
940                         }
941                         status = smb2_create_recv(requests[i], tctx,
942                                                   &(ios[i].smb2));
943
944                         torture_comment(tctx,
945                                 "File %d returned status %s\n", i,
946                                 nt_errstr(status));
947
948                         if (NT_STATUS_IS_OK(status)) {
949                                 num_ok += 1;
950                         }
951
952                         if (NT_STATUS_EQUAL(status,
953                                             NT_STATUS_OBJECT_NAME_COLLISION)) {
954                                 num_collision += 1;
955                         }
956
957                         requests[i] = NULL;
958                 }
959                 if (!unreplied) {
960                         break;
961                 }
962
963                 if (event_loop_once(tctx->ev) != 0) {
964                         torture_comment(tctx, "event_loop_once failed\n");
965                         ret = false;
966                         goto done;
967                 }
968         }
969
970         if ((num_ok != 1) || (num_ok + num_collision != num_files)) {
971                 ret = false;
972         }
973 done:
974         smb2_deltree(tree, fname);
975
976         return ret;
977 }
978
979 /*
980   test opening for delete on a read-only attribute file.
981 */
982
983 static bool test_smb2_open_for_delete(struct torture_context *tctx,
984                                       struct smb2_tree *tree)
985 {
986         union smb_open io;
987         union smb_fileinfo finfo;
988         const char *fname = DNAME "\\torture_open_for_delete.txt";
989         NTSTATUS status;
990         struct smb2_handle h, h1;
991         bool ret = true;
992
993         torture_comment(tctx,
994                 "Checking SMB2_OPEN for delete on a readonly file.\n");
995         smb2_util_unlink(tree, fname);
996         smb2_deltree(tree, fname);
997
998         status = torture_smb2_testdir(tree, DNAME, &h);
999         CHECK_STATUS(status, NT_STATUS_OK);
1000
1001         /* reasonable default parameters */
1002         ZERO_STRUCT(io.smb2);
1003         io.generic.level = RAW_OPEN_SMB2;
1004         io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
1005         io.smb2.in.alloc_size = 0;
1006         io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
1007         io.smb2.in.file_attributes = FILE_ATTRIBUTE_READONLY;
1008         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
1009         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
1010         io.smb2.in.create_options = 0;
1011         io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
1012         io.smb2.in.security_flags = 0;
1013         io.smb2.in.fname = fname;
1014
1015         /* Create the readonly file. */
1016
1017         status = smb2_create(tree, tctx, &(io.smb2));
1018         CHECK_STATUS(status, NT_STATUS_OK);
1019         h1 = io.smb2.out.file.handle;
1020
1021         CHECK_VAL(io.smb2.out.oplock_level, 0);
1022         io.smb2.in.create_options = 0;
1023         CHECK_VAL(io.smb2.out.create_action, NTCREATEX_ACTION_CREATED);
1024         CHECK_ALL_INFO(io.smb2.out.file_attr, attrib);
1025         smb2_util_close(tree, h1);
1026
1027         /* Now try and open for delete only - should succeed. */
1028         io.smb2.in.desired_access = SEC_STD_DELETE;
1029         io.smb2.in.file_attributes = 0;
1030         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1031                                 NTCREATEX_SHARE_ACCESS_WRITE |
1032                                 NTCREATEX_SHARE_ACCESS_DELETE;
1033         io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
1034         status = smb2_create(tree, tctx, &(io.smb2));
1035         CHECK_STATUS(status, NT_STATUS_OK);
1036
1037         smb2_util_unlink(tree, fname);
1038
1039         smb2_util_close(tree, h1);
1040         smb2_util_unlink(tree, fname);
1041         smb2_deltree(tree, DNAME);
1042
1043         return ret;
1044 }
1045
1046 /*
1047   test SMB2 open with a leading slash on the path.
1048   Trying to create a directory with a leading slash
1049   should give NT_STATUS_INVALID_PARAMETER error
1050 */
1051 static bool test_smb2_leading_slash(struct torture_context *tctx,
1052                                     struct smb2_tree *tree)
1053 {
1054         union smb_open io;
1055         const char *dnameslash = "\\"DNAME;
1056         NTSTATUS status;
1057         bool ret = true;
1058
1059         torture_comment(tctx,
1060                 "Trying to create a directory with leading slash on path\n");
1061         smb2_deltree(tree, dnameslash);
1062
1063         ZERO_STRUCT(io.smb2);
1064         io.generic.level = RAW_OPEN_SMB2;
1065         io.smb2.in.oplock_level = 0;
1066         io.smb2.in.desired_access = SEC_RIGHTS_DIR_ALL;
1067         io.smb2.in.file_attributes   = FILE_ATTRIBUTE_DIRECTORY;
1068         io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
1069         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1070                                 NTCREATEX_SHARE_ACCESS_WRITE |
1071                                 NTCREATEX_SHARE_ACCESS_DELETE;
1072         io.smb2.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1073         io.smb2.in.fname = dnameslash;
1074
1075         status = smb2_create(tree, tree, &(io.smb2));
1076         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
1077
1078         smb2_deltree(tree, dnameslash);
1079         return ret;
1080 }
1081
1082
1083 static bool test_create_acl_file(struct torture_context *tctx,
1084     struct smb2_tree *tree)
1085 {
1086         torture_comment(tctx, "testing nttrans create with sec_desc on files\n");
1087
1088         return test_create_acl_ext(tctx, tree, false);
1089 }
1090
1091 static bool test_create_acl_dir(struct torture_context *tctx,
1092     struct smb2_tree *tree)
1093 {
1094         torture_comment(tctx, "testing nttrans create with sec_desc on directories\n");
1095
1096         return test_create_acl_ext(tctx, tree, true);
1097 }
1098
1099 #define CHECK_ACCESS_FLAGS(_fh, flags) do { \
1100         union smb_fileinfo _q; \
1101         _q.access_information.level = RAW_FILEINFO_ACCESS_INFORMATION; \
1102         _q.access_information.in.file.handle = (_fh); \
1103         status = smb2_getinfo_file(tree, tctx, &_q); \
1104         CHECK_STATUS(status, NT_STATUS_OK); \
1105         if (_q.access_information.out.access_flags != (flags)) { \
1106                 torture_result(tctx, TORTURE_FAIL, "(%s) Incorrect access_flags 0x%08x - should be 0x%08x\n", \
1107                        __location__, _q.access_information.out.access_flags, (flags)); \
1108                 ret = false; \
1109                 goto done; \
1110         } \
1111 } while (0)
1112
1113 /*
1114  * Test creating a file with a NULL DACL.
1115  */
1116 static bool test_create_null_dacl(struct torture_context *tctx,
1117     struct smb2_tree *tree)
1118 {
1119         NTSTATUS status;
1120         struct smb2_create io;
1121         const char *fname = "nulldacl.txt";
1122         bool ret = true;
1123         struct smb2_handle handle;
1124         union smb_fileinfo q;
1125         union smb_setfileinfo s;
1126         struct security_descriptor *sd = security_descriptor_initialise(tctx);
1127         struct security_acl dacl;
1128
1129         torture_comment(tctx, "TESTING SEC_DESC WITH A NULL DACL\n");
1130
1131         smb2_util_unlink(tree, fname);
1132
1133         ZERO_STRUCT(io);
1134         io.level = RAW_OPEN_SMB2;
1135         io.in.create_flags = 0;
1136         io.in.desired_access = SEC_STD_READ_CONTROL | SEC_STD_WRITE_DAC
1137                 | SEC_STD_WRITE_OWNER;
1138         io.in.create_options = 0;
1139         io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
1140         io.in.share_access =
1141                 NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1142         io.in.alloc_size = 0;
1143         io.in.create_disposition = NTCREATEX_DISP_CREATE;
1144         io.in.impersonation_level = NTCREATEX_IMPERSONATION_ANONYMOUS;
1145         io.in.security_flags = 0;
1146         io.in.fname = fname;
1147         io.in.sec_desc = sd;
1148         /* XXX create_options ? */
1149         io.in.create_options            = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
1150                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
1151                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
1152                                           0x00200000;
1153
1154         torture_comment(tctx, "creating a file with a empty sd\n");
1155         status = smb2_create(tree, tctx, &io);
1156         CHECK_STATUS(status, NT_STATUS_OK);
1157         handle = io.out.file.handle;
1158
1159         torture_comment(tctx, "get the original sd\n");
1160         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
1161         q.query_secdesc.in.file.handle = handle;
1162         q.query_secdesc.in.secinfo_flags =
1163                 SECINFO_OWNER |
1164                 SECINFO_GROUP |
1165                 SECINFO_DACL;
1166         status = smb2_getinfo_file(tree, tctx, &q);
1167         CHECK_STATUS(status, NT_STATUS_OK);
1168
1169         /*
1170          * Testing the created DACL,
1171          * the server should add the inherited DACL
1172          * when SEC_DESC_DACL_PRESENT isn't specified
1173          */
1174         if (!(q.query_secdesc.out.sd->type & SEC_DESC_DACL_PRESENT)) {
1175                 ret = false;
1176                 torture_fail_goto(tctx, done, "DACL_PRESENT flag not set by the server!\n");
1177         }
1178         if (q.query_secdesc.out.sd->dacl == NULL) {
1179                 ret = false;
1180                 torture_fail_goto(tctx, done, "no DACL has been created on the server!\n");
1181         }
1182
1183         torture_comment(tctx, "set NULL DACL\n");
1184         sd->type |= SEC_DESC_DACL_PRESENT;
1185
1186         s.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
1187         s.set_secdesc.in.file.handle = handle;
1188         s.set_secdesc.in.secinfo_flags = SECINFO_DACL;
1189         s.set_secdesc.in.sd = sd;
1190         status = smb2_setinfo_file(tree, &s);
1191         CHECK_STATUS(status, NT_STATUS_OK);
1192
1193         torture_comment(tctx, "get the sd\n");
1194         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
1195         q.query_secdesc.in.file.handle = handle;
1196         q.query_secdesc.in.secinfo_flags =
1197                 SECINFO_OWNER |
1198                 SECINFO_GROUP |
1199                 SECINFO_DACL;
1200         status = smb2_getinfo_file(tree, tctx, &q);
1201         CHECK_STATUS(status, NT_STATUS_OK);
1202
1203         /* Testing the modified DACL */
1204         if (!(q.query_secdesc.out.sd->type & SEC_DESC_DACL_PRESENT)) {
1205                 ret = false;
1206                 torture_fail_goto(tctx, done, "DACL_PRESENT flag not set by the server!\n");
1207         }
1208         if (q.query_secdesc.out.sd->dacl != NULL) {
1209                 ret = false;
1210                 torture_fail_goto(tctx, done, "DACL has been created on the server!\n");
1211         }
1212
1213         io.in.create_disposition = NTCREATEX_DISP_OPEN;
1214
1215         torture_comment(tctx, "try open for read control\n");
1216         io.in.desired_access = SEC_STD_READ_CONTROL;
1217         status = smb2_create(tree, tctx, &io);
1218         CHECK_STATUS(status, NT_STATUS_OK);
1219         CHECK_ACCESS_FLAGS(io.out.file.handle,
1220                 SEC_STD_READ_CONTROL);
1221         smb2_util_close(tree, io.out.file.handle);
1222
1223         torture_comment(tctx, "try open for write\n");
1224         io.in.desired_access = SEC_FILE_WRITE_DATA;
1225         status = smb2_create(tree, tctx, &io);
1226         CHECK_STATUS(status, NT_STATUS_OK);
1227         CHECK_ACCESS_FLAGS(io.out.file.handle,
1228                 SEC_FILE_WRITE_DATA);
1229         smb2_util_close(tree, io.out.file.handle);
1230
1231         torture_comment(tctx, "try open for read\n");
1232         io.in.desired_access = SEC_FILE_READ_DATA;
1233         status = smb2_create(tree, tctx, &io);
1234         CHECK_STATUS(status, NT_STATUS_OK);
1235         CHECK_ACCESS_FLAGS(io.out.file.handle,
1236                 SEC_FILE_READ_DATA);
1237         smb2_util_close(tree, io.out.file.handle);
1238
1239         torture_comment(tctx, "try open for generic write\n");
1240         io.in.desired_access = SEC_GENERIC_WRITE;
1241         status = smb2_create(tree, tctx, &io);
1242         CHECK_STATUS(status, NT_STATUS_OK);
1243         CHECK_ACCESS_FLAGS(io.out.file.handle,
1244                 SEC_RIGHTS_FILE_WRITE);
1245         smb2_util_close(tree, io.out.file.handle);
1246
1247         torture_comment(tctx, "try open for generic read\n");
1248         io.in.desired_access = SEC_GENERIC_READ;
1249         status = smb2_create(tree, tctx, &io);
1250         CHECK_STATUS(status, NT_STATUS_OK);
1251         CHECK_ACCESS_FLAGS(io.out.file.handle,
1252                 SEC_RIGHTS_FILE_READ);
1253         smb2_util_close(tree, io.out.file.handle);
1254
1255         torture_comment(tctx, "set DACL with 0 aces\n");
1256         ZERO_STRUCT(dacl);
1257         dacl.revision = SECURITY_ACL_REVISION_NT4;
1258         dacl.num_aces = 0;
1259         sd->dacl = &dacl;
1260
1261         s.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
1262         s.set_secdesc.in.file.handle = handle;
1263         s.set_secdesc.in.secinfo_flags = SECINFO_DACL;
1264         s.set_secdesc.in.sd = sd;
1265         status = smb2_setinfo_file(tree, &s);
1266         CHECK_STATUS(status, NT_STATUS_OK);
1267
1268         torture_comment(tctx, "get the sd\n");
1269         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
1270         q.query_secdesc.in.file.handle = handle;
1271         q.query_secdesc.in.secinfo_flags =
1272                 SECINFO_OWNER |
1273                 SECINFO_GROUP |
1274                 SECINFO_DACL;
1275         status = smb2_getinfo_file(tree, tctx, &q);
1276         CHECK_STATUS(status, NT_STATUS_OK);
1277
1278         /* Testing the modified DACL */
1279         if (!(q.query_secdesc.out.sd->type & SEC_DESC_DACL_PRESENT)) {
1280                 ret = false;
1281                 torture_fail_goto(tctx, done, "DACL_PRESENT flag not set by the server!\n");
1282         }
1283         if (q.query_secdesc.out.sd->dacl == NULL) {
1284                 ret = false;
1285                 torture_fail_goto(tctx, done, "no DACL has been created on the server!\n");
1286         }
1287         if (q.query_secdesc.out.sd->dacl->num_aces != 0) {
1288                 torture_result(tctx, TORTURE_FAIL, "DACL has %u aces!\n",
1289                        q.query_secdesc.out.sd->dacl->num_aces);
1290                 ret = false;
1291                 goto done;
1292         }
1293
1294         torture_comment(tctx, "try open for read control\n");
1295         io.in.desired_access = SEC_STD_READ_CONTROL;
1296         status = smb2_create(tree, tctx, &io);
1297         CHECK_STATUS(status, NT_STATUS_OK);
1298         CHECK_ACCESS_FLAGS(io.out.file.handle,
1299                 SEC_STD_READ_CONTROL);
1300         smb2_util_close(tree, io.out.file.handle);
1301
1302         torture_comment(tctx, "try open for write => access_denied\n");
1303         io.in.desired_access = SEC_FILE_WRITE_DATA;
1304         status = smb2_create(tree, tctx, &io);
1305         if (torture_setting_bool(tctx, "hide_on_access_denied", false)) {
1306                 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1307         } else {
1308                 CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
1309         }
1310
1311         torture_comment(tctx, "try open for read => access_denied\n");
1312         io.in.desired_access = SEC_FILE_READ_DATA;
1313         status = smb2_create(tree, tctx, &io);
1314         if (torture_setting_bool(tctx, "hide_on_access_denied", false)) {
1315                 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1316         } else {
1317                 CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
1318         }
1319
1320         torture_comment(tctx, "try open for generic write => access_denied\n");
1321         io.in.desired_access = SEC_GENERIC_WRITE;
1322         status = smb2_create(tree, tctx, &io);
1323         if (torture_setting_bool(tctx, "hide_on_access_denied", false)) {
1324                 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1325         } else {
1326                 CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
1327         }
1328
1329         torture_comment(tctx, "try open for generic read => access_denied\n");
1330         io.in.desired_access = SEC_GENERIC_READ;
1331         status = smb2_create(tree, tctx, &io);
1332         if (torture_setting_bool(tctx, "hide_on_access_denied", false)) {
1333                 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1334         } else {
1335                 CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
1336         }
1337
1338         torture_comment(tctx, "set empty sd\n");
1339         sd->type &= ~SEC_DESC_DACL_PRESENT;
1340         sd->dacl = NULL;
1341
1342         s.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
1343         s.set_secdesc.in.file.handle = handle;
1344         s.set_secdesc.in.secinfo_flags = SECINFO_DACL;
1345         s.set_secdesc.in.sd = sd;
1346         status = smb2_setinfo_file(tree, &s);
1347         CHECK_STATUS(status, NT_STATUS_OK);
1348
1349         torture_comment(tctx, "get the sd\n");
1350         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
1351         q.query_secdesc.in.file.handle = handle;
1352         q.query_secdesc.in.secinfo_flags =
1353                 SECINFO_OWNER |
1354                 SECINFO_GROUP |
1355                 SECINFO_DACL;
1356         status = smb2_getinfo_file(tree, tctx, &q);
1357         CHECK_STATUS(status, NT_STATUS_OK);
1358
1359         /* Testing the modified DACL */
1360         if (!(q.query_secdesc.out.sd->type & SEC_DESC_DACL_PRESENT)) {
1361                 ret = false;
1362                 torture_fail_goto(tctx, done, "DACL_PRESENT flag not set by the server!\n");
1363         }
1364         if (q.query_secdesc.out.sd->dacl != NULL) {
1365                 ret = false;
1366                 torture_fail_goto(tctx, done, "DACL has been created on the server!\n");
1367         }
1368 done:
1369         smb2_util_close(tree, handle);
1370         smb2_util_unlink(tree, fname);
1371         smb2_tdis(tree);
1372         smb2_logoff(tree->session);
1373         return ret;
1374 }
1375
1376 /*
1377    basic testing of SMB2 read
1378 */
1379 struct torture_suite *torture_smb2_create_init(void)
1380 {
1381         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "CREATE");
1382
1383         torture_suite_add_1smb2_test(suite, "GENTEST", test_create_gentest);
1384         torture_suite_add_1smb2_test(suite, "BLOB", test_create_blob);
1385         torture_suite_add_1smb2_test(suite, "OPEN", test_smb2_open);
1386         torture_suite_add_1smb2_test(suite, "BRLOCKED", test_smb2_open_brlocked);
1387         torture_suite_add_1smb2_test(suite, "MULTI", test_smb2_open_multi);
1388         torture_suite_add_1smb2_test(suite, "DELETE", test_smb2_open_for_delete);
1389         torture_suite_add_1smb2_test(suite, "LEADING-SLASH", test_smb2_leading_slash);
1390         torture_suite_add_1smb2_test(suite, "ACLFILE", test_create_acl_file);
1391         torture_suite_add_1smb2_test(suite, "ACLDIR", test_create_acl_dir);
1392         torture_suite_add_1smb2_test(suite, "NULLDACL", test_create_null_dacl);
1393
1394         suite->description = talloc_strdup(suite, "SMB2-CREATE tests");
1395
1396         return suite;
1397 }