cf3731e9f1b354f60493fa0d334bb3d990f704f4
[ira/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_SEQUENTIAL_ONLY |
499             NTCREATEX_OPTIONS_ASYNC_ALERT |
500             0x00200000 |
501             (test_dir ?  NTCREATEX_OPTIONS_DIRECTORY :
502                 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE);
503
504         io.in.fname = FNAME;
505
506         torture_comment(tctx, "basic create\n");
507
508         status = smb2_create(tree, tctx, &io);
509         CHECK_STATUS(status, NT_STATUS_OK);
510
511         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
512         q.query_secdesc.in.file.handle = io.out.file.handle;
513         q.query_secdesc.in.secinfo_flags = 
514                 SECINFO_OWNER |
515                 SECINFO_GROUP |
516                 SECINFO_DACL;
517         status = smb2_getinfo_file(tree, tctx, &q);
518         CHECK_STATUS(status, NT_STATUS_OK);
519         sd = q.query_secdesc.out.sd;
520
521         status = smb2_util_close(tree, io.out.file.handle);
522         CHECK_STATUS(status, NT_STATUS_OK);
523         status = delete_func(tree, FNAME);
524         CHECK_STATUS(status, NT_STATUS_OK);
525
526         torture_comment(tctx, "adding a new ACE\n");
527         test_sid = dom_sid_parse_talloc(tctx, SID_NT_AUTHENTICATED_USERS);
528
529         ace.type = SEC_ACE_TYPE_ACCESS_ALLOWED;
530         ace.flags = 0;
531         ace.access_mask = SEC_STD_ALL;
532         ace.trustee = *test_sid;
533
534         status = security_descriptor_dacl_add(sd, &ace);
535         CHECK_STATUS(status, NT_STATUS_OK);
536
537         torture_comment(tctx, "creating a file with an initial ACL\n");
538
539         io.in.sec_desc = sd;
540         status = smb2_create(tree, tctx, &io);
541         CHECK_STATUS(status, NT_STATUS_OK);
542
543         FAIL_UNLESS(smb2_util_verify_sd(tctx, tree, io.out.file.handle, sd));
544
545         status = smb2_util_close(tree, io.out.file.handle);
546         CHECK_STATUS(status, NT_STATUS_OK);
547         status = delete_func(tree, FNAME);
548         CHECK_STATUS(status, NT_STATUS_OK);
549
550         torture_comment(tctx, "creating with attributes\n");
551
552         io.in.sec_desc = NULL;
553         io.in.file_attributes = attrib;
554         status = smb2_create(tree, tctx, &io);
555         CHECK_STATUS(status, NT_STATUS_OK);
556
557         FAIL_UNLESS(smb2_util_verify_attrib(tctx, tree, io.out.file.handle, attrib));
558
559         status = smb2_util_close(tree, io.out.file.handle);
560         CHECK_STATUS(status, NT_STATUS_OK);
561         status = delete_func(tree, FNAME);
562         CHECK_STATUS(status, NT_STATUS_OK);
563
564         torture_comment(tctx, "creating with attributes and ACL\n");
565
566         io.in.sec_desc = sd;
567         io.in.file_attributes = attrib;
568         status = smb2_create(tree, tctx, &io);
569         CHECK_STATUS(status, NT_STATUS_OK);
570
571         FAIL_UNLESS(smb2_util_verify_sd(tctx, tree, io.out.file.handle, sd));
572         FAIL_UNLESS(smb2_util_verify_attrib(tctx, tree, io.out.file.handle, attrib));
573         
574         status = smb2_util_close(tree, io.out.file.handle);
575         CHECK_STATUS(status, NT_STATUS_OK);
576         status = delete_func(tree, FNAME);
577         CHECK_STATUS(status, NT_STATUS_OK);
578
579         torture_comment(tctx, "creating with attributes, ACL and owner\n");
580         sd = security_descriptor_dacl_create(tctx,
581                                         0, SID_WORLD, SID_BUILTIN_USERS,
582                                         SID_WORLD,
583                                         SEC_ACE_TYPE_ACCESS_ALLOWED,
584                                         SEC_RIGHTS_FILE_READ | SEC_STD_ALL,
585                                         0,
586                                         NULL);
587
588         io.in.sec_desc = sd;
589         io.in.file_attributes = attrib;
590         status = smb2_create(tree, tctx, &io);
591         CHECK_STATUS(status, NT_STATUS_OK);
592
593         FAIL_UNLESS(smb2_util_verify_sd(tctx, tree, io.out.file.handle, sd));
594         FAIL_UNLESS(smb2_util_verify_attrib(tctx, tree, io.out.file.handle, attrib));
595
596  done:
597         status = smb2_util_close(tree, io.out.file.handle);
598         CHECK_STATUS(status, NT_STATUS_OK);
599         status = delete_func(tree, FNAME);
600         CHECK_STATUS(status, NT_STATUS_OK);
601
602         return ret;
603 }
604
605 /*
606   test SMB2 open
607 */
608 static bool test_smb2_open(struct torture_context *tctx,
609                            struct smb2_tree *tree)
610 {
611         union smb_open io;
612         union smb_fileinfo finfo;
613         const char *fname = DNAME "\\torture_ntcreatex.txt";
614         const char *dname = DNAME "\\torture_ntcreatex.dir";
615         NTSTATUS status;
616         struct smb2_handle h, h1;
617         bool ret = true;
618         int i;
619         struct {
620                 uint32_t create_disp;
621                 bool with_file;
622                 NTSTATUS correct_status;
623         } open_funcs[] = {
624                 { NTCREATEX_DISP_SUPERSEDE,     true,  NT_STATUS_OK },
625                 { NTCREATEX_DISP_SUPERSEDE,     false, NT_STATUS_OK },
626                 { NTCREATEX_DISP_OPEN,          true,  NT_STATUS_OK },
627                 { NTCREATEX_DISP_OPEN,          false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
628                 { NTCREATEX_DISP_CREATE,        true,  NT_STATUS_OBJECT_NAME_COLLISION },
629                 { NTCREATEX_DISP_CREATE,        false, NT_STATUS_OK },
630                 { NTCREATEX_DISP_OPEN_IF,       true,  NT_STATUS_OK },
631                 { NTCREATEX_DISP_OPEN_IF,       false, NT_STATUS_OK },
632                 { NTCREATEX_DISP_OVERWRITE,     true,  NT_STATUS_OK },
633                 { NTCREATEX_DISP_OVERWRITE,     false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
634                 { NTCREATEX_DISP_OVERWRITE_IF,  true,  NT_STATUS_OK },
635                 { NTCREATEX_DISP_OVERWRITE_IF,  false, NT_STATUS_OK },
636                 { 6,                            true,  NT_STATUS_INVALID_PARAMETER },
637                 { 6,                            false, NT_STATUS_INVALID_PARAMETER },
638         };
639
640         torture_comment(tctx, "Checking SMB2 Open\n");
641
642         smb2_util_unlink(tree, fname);
643         smb2_util_rmdir(tree, dname);
644
645         status = torture_smb2_testdir(tree, DNAME, &h);
646         CHECK_STATUS(status, NT_STATUS_OK);
647
648         ZERO_STRUCT(io.smb2);
649         /* reasonable default parameters */
650         io.generic.level = RAW_OPEN_SMB2;
651         io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
652         io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
653         io.smb2.in.alloc_size = 1024*1024;
654         io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
655         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
656         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
657         io.smb2.in.create_options = 0;
658         io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
659         io.smb2.in.security_flags = 0;
660         io.smb2.in.fname = fname;
661
662         /* test the create disposition */
663         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
664                 if (open_funcs[i].with_file) {
665                         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
666                         status= smb2_create(tree, tctx, &(io.smb2));
667                         if (!NT_STATUS_IS_OK(status)) {
668                                 torture_comment(tctx,
669                                     "Failed to create file %s status %s %d\n",
670                                     fname, nt_errstr(status), i);
671
672                                 ret = false;
673                                 goto done;
674                         }
675                         smb2_util_close(tree, io.smb2.out.file.handle);
676                 }
677                 io.smb2.in.create_disposition = open_funcs[i].create_disp;
678                 status = smb2_create(tree, tctx, &(io.smb2));
679                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
680                         torture_comment(tctx,
681                             "(%s) incorrect status %s should be %s (i=%d "
682                             "with_file=%d open_disp=%d)\n",
683                          __location__, nt_errstr(status),
684                         nt_errstr(open_funcs[i].correct_status),
685                         i, (int)open_funcs[i].with_file,
686                         (int)open_funcs[i].create_disp);
687
688                         ret = false;
689                         goto done;
690                 }
691                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
692                         smb2_util_close(tree, io.smb2.out.file.handle);
693                         smb2_util_unlink(tree, fname);
694                 }
695         }
696
697         /* basic field testing */
698         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
699
700         status = smb2_create(tree, tctx, &(io.smb2));
701         CHECK_STATUS(status, NT_STATUS_OK);
702         h1 = io.smb2.out.file.handle;
703
704         CHECK_VAL(io.smb2.out.oplock_level, 0);
705         CHECK_VAL(io.smb2.out.create_action, NTCREATEX_ACTION_CREATED);
706         CHECK_NTTIME(io.smb2.out.create_time, create_time);
707         CHECK_NTTIME(io.smb2.out.access_time, access_time);
708         CHECK_NTTIME(io.smb2.out.write_time, write_time);
709         CHECK_NTTIME(io.smb2.out.change_time, change_time);
710         CHECK_ALL_INFO(io.smb2.out.file_attr, attrib);
711         CHECK_ALL_INFO(io.smb2.out.alloc_size, alloc_size);
712         CHECK_ALL_INFO(io.smb2.out.size, size);
713
714         /* check fields when the file already existed */
715         smb2_util_close(tree, h1);
716         smb2_util_unlink(tree, fname);
717
718         status = smb2_create_complex_file(tree, fname, &h1);
719         CHECK_STATUS(status, NT_STATUS_OK);
720
721         smb2_util_close(tree, h1);
722
723         io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
724         status = smb2_create(tree, tctx, &(io.smb2));
725         CHECK_STATUS(status, NT_STATUS_OK);
726         h1 = io.smb2.out.file.handle;
727
728         CHECK_VAL(io.smb2.out.oplock_level, 0);
729         CHECK_VAL(io.smb2.out.create_action, NTCREATEX_ACTION_EXISTED);
730         CHECK_NTTIME(io.smb2.out.create_time, create_time);
731         CHECK_NTTIME(io.smb2.out.access_time, access_time);
732         CHECK_NTTIME(io.smb2.out.write_time, write_time);
733         CHECK_NTTIME(io.smb2.out.change_time, change_time);
734         CHECK_ALL_INFO(io.smb2.out.file_attr, attrib);
735         CHECK_ALL_INFO(io.smb2.out.alloc_size, alloc_size);
736         CHECK_ALL_INFO(io.smb2.out.size, size);
737         smb2_util_close(tree, h1);
738         smb2_util_unlink(tree, fname);
739
740         /* create a directory */
741         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
742         io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
743         io.smb2.in.alloc_size = 0;
744         io.smb2.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
745         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
746         io.smb2.in.create_options = 0;
747         io.smb2.in.fname = dname;
748         fname = dname;
749
750         smb2_util_rmdir(tree, fname);
751         smb2_util_unlink(tree, fname);
752
753         io.smb2.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
754         io.smb2.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
755         io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
756         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
757                                 NTCREATEX_SHARE_ACCESS_WRITE;
758         status = smb2_create(tree, tctx, &(io.smb2));
759         CHECK_STATUS(status, NT_STATUS_OK);
760         h1 = io.smb2.out.file.handle;
761
762         CHECK_VAL(io.smb2.out.oplock_level, 0);
763         CHECK_VAL(io.smb2.out.create_action, NTCREATEX_ACTION_CREATED);
764         CHECK_NTTIME(io.smb2.out.create_time, create_time);
765         CHECK_NTTIME(io.smb2.out.access_time, access_time);
766         CHECK_NTTIME(io.smb2.out.write_time, write_time);
767         CHECK_NTTIME(io.smb2.out.change_time, change_time);
768         CHECK_ALL_INFO(io.smb2.out.file_attr, attrib);
769         CHECK_VAL(io.smb2.out.file_attr & ~FILE_ATTRIBUTE_NONINDEXED,
770                   FILE_ATTRIBUTE_DIRECTORY);
771         CHECK_ALL_INFO(io.smb2.out.alloc_size, alloc_size);
772         CHECK_ALL_INFO(io.smb2.out.size, size);
773         CHECK_VAL(io.smb2.out.size, 0);
774         CHECK_VAL(io.smb2.out.alloc_size, 0);
775         smb2_util_unlink(tree, fname);
776
777 done:
778         smb2_util_close(tree, h1);
779         smb2_util_unlink(tree, fname);
780         smb2_deltree(tree, DNAME);
781         return ret;
782 }
783
784 /*
785   test with an already opened and byte range locked file
786 */
787
788 static bool test_smb2_open_brlocked(struct torture_context *tctx,
789                                     struct smb2_tree *tree)
790 {
791         union smb_open io, io1;
792         union smb_lock io2;
793         struct smb2_lock_element lock[1];
794         const char *fname = DNAME "\\torture_ntcreatex.txt";
795         NTSTATUS status;
796         bool ret = true;
797         struct smb2_handle h;
798
799         torture_comment(tctx,
800                 "Testing SMB2 open with a byte range locked file\n");
801
802         smb2_util_unlink(tree, fname);
803
804         status = torture_smb2_testdir(tree, DNAME, &h);
805         CHECK_STATUS(status, NT_STATUS_OK);
806
807         ZERO_STRUCT(io.smb2);
808         io.generic.level = RAW_OPEN_SMB2;
809         io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
810         io.smb2.in.desired_access = 0x2019f;
811         io.smb2.in.alloc_size = 0;
812         io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
813         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
814                 NTCREATEX_SHARE_ACCESS_WRITE;
815         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
816         io.smb2.in.create_options = NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
817         io.smb2.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
818         io.smb2.in.security_flags = SMB2_SECURITY_DYNAMIC_TRACKING;
819         io.smb2.in.fname = fname;
820
821         status = smb2_create(tree, tctx, &(io.smb2));
822         CHECK_STATUS(status, NT_STATUS_OK);
823
824         ZERO_STRUCT(io2.smb2);
825         io2.smb2.level = RAW_LOCK_SMB2;
826         io2.smb2.in.file.handle = io.smb2.out.file.handle;
827         io2.smb2.in.lock_count = 1;
828
829         lock[0].offset = 0;
830         lock[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
831                         SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
832         io2.smb2.in.locks = &lock[0];
833         status = smb2_lock(tree, &(io2.smb2));
834         CHECK_STATUS(status, NT_STATUS_OK);
835
836         ZERO_STRUCT(io1.smb2);
837         io1.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
838         io1.smb2.in.desired_access = 0x20196;
839         io1.smb2.in.alloc_size = 0;
840         io1.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
841         io1.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
842                 NTCREATEX_SHARE_ACCESS_WRITE;
843         io1.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
844         io1.smb2.in.create_options = 0;
845         io1.smb2.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
846         io1.smb2.in.security_flags = SMB2_SECURITY_DYNAMIC_TRACKING;
847         io1.smb2.in.fname = fname;
848
849         status = smb2_create(tree, tctx, &(io1.smb2));
850         CHECK_STATUS(status, NT_STATUS_OK);
851
852         smb2_util_close(tree, io.smb2.out.file.handle);
853         smb2_util_close(tree, io1.smb2.out.file.handle);
854         smb2_util_unlink(tree, fname);
855         smb2_deltree(tree, DNAME);
856
857         return ret;
858 }
859
860 /* A little torture test to expose a race condition in Samba 3.0.20 ... :-) */
861
862 static bool test_smb2_open_multi(struct torture_context *tctx,
863                                 struct smb2_tree *tree)
864 {
865         const char *fname = "test_oplock.dat";
866         NTSTATUS status;
867         bool ret = true;
868         union smb_open io;
869         struct smb2_tree **trees;
870         struct smb2_request **requests;
871         union smb_open *ios;
872         int i, num_files = 3;
873         int num_ok = 0;
874         int num_collision = 0;
875
876         torture_comment(tctx,
877                 "Testing SMB2 Open with multiple connections\n");
878         trees = talloc_array(tctx, struct smb2_tree *, num_files);
879         requests = talloc_array(tctx, struct smb2_request *, num_files);
880         ios = talloc_array(tctx, union smb_open, num_files);
881         if ((tctx->ev == NULL) || (trees == NULL) || (requests == NULL) ||
882             (ios == NULL)) {
883                 torture_comment(tctx, ("talloc failed\n"));
884                 ret = false;
885                 goto done;
886         }
887
888         tree->session->transport->options.request_timeout = 60;
889
890         for (i=0; i<num_files; i++) {
891                 if (!torture_smb2_connection(tctx, &(trees[i]))) {
892                         torture_comment(tctx,
893                                 "Could not open %d'th connection\n", i);
894                         ret = false;
895                         goto done;
896                 }
897                 trees[i]->session->transport->options.request_timeout = 60;
898         }
899
900         /* cleanup */
901         smb2_util_unlink(tree, fname);
902
903         /*
904           base ntcreatex parms
905         */
906         ZERO_STRUCT(io.smb2);
907         io.generic.level = RAW_OPEN_SMB2;
908         io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
909         io.smb2.in.alloc_size = 0;
910         io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
911         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
912                 NTCREATEX_SHARE_ACCESS_WRITE|
913                 NTCREATEX_SHARE_ACCESS_DELETE;
914         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
915         io.smb2.in.create_options = 0;
916         io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
917         io.smb2.in.security_flags = 0;
918         io.smb2.in.fname = fname;
919         io.smb2.in.create_flags = 0;
920
921         for (i=0; i<num_files; i++) {
922                 ios[i] = io;
923                 requests[i] = smb2_create_send(trees[i], &(ios[i].smb2));
924                 if (requests[i] == NULL) {
925                         torture_comment(tctx,
926                                 "could not send %d'th request\n", i);
927                         ret = false;
928                         goto done;
929                 }
930         }
931
932         torture_comment(tctx, "waiting for replies\n");
933         while (1) {
934                 bool unreplied = false;
935                 for (i=0; i<num_files; i++) {
936                         if (requests[i] == NULL) {
937                                 continue;
938                         }
939                         if (requests[i]->state < SMB2_REQUEST_DONE) {
940                                 unreplied = true;
941                                 break;
942                         }
943                         status = smb2_create_recv(requests[i], tctx,
944                                                   &(ios[i].smb2));
945
946                         torture_comment(tctx,
947                                 "File %d returned status %s\n", i,
948                                 nt_errstr(status));
949
950                         if (NT_STATUS_IS_OK(status)) {
951                                 num_ok += 1;
952                         }
953
954                         if (NT_STATUS_EQUAL(status,
955                                             NT_STATUS_OBJECT_NAME_COLLISION)) {
956                                 num_collision += 1;
957                         }
958
959                         requests[i] = NULL;
960                 }
961                 if (!unreplied) {
962                         break;
963                 }
964
965                 if (event_loop_once(tctx->ev) != 0) {
966                         torture_comment(tctx, "event_loop_once failed\n");
967                         ret = false;
968                         goto done;
969                 }
970         }
971
972         if ((num_ok != 1) || (num_ok + num_collision != num_files)) {
973                 ret = false;
974         }
975 done:
976         smb2_deltree(tree, fname);
977
978         return ret;
979 }
980
981 /*
982   test opening for delete on a read-only attribute file.
983 */
984
985 static bool test_smb2_open_for_delete(struct torture_context *tctx,
986                                       struct smb2_tree *tree)
987 {
988         union smb_open io;
989         union smb_fileinfo finfo;
990         const char *fname = DNAME "\\torture_open_for_delete.txt";
991         NTSTATUS status;
992         struct smb2_handle h, h1;
993         bool ret = true;
994
995         torture_comment(tctx,
996                 "Checking SMB2_OPEN for delete on a readonly file.\n");
997         smb2_util_unlink(tree, fname);
998         smb2_deltree(tree, fname);
999
1000         status = torture_smb2_testdir(tree, DNAME, &h);
1001         CHECK_STATUS(status, NT_STATUS_OK);
1002
1003         /* reasonable default parameters */
1004         ZERO_STRUCT(io.smb2);
1005         io.generic.level = RAW_OPEN_SMB2;
1006         io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
1007         io.smb2.in.alloc_size = 0;
1008         io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
1009         io.smb2.in.file_attributes = FILE_ATTRIBUTE_READONLY;
1010         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
1011         io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
1012         io.smb2.in.create_options = 0;
1013         io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
1014         io.smb2.in.security_flags = 0;
1015         io.smb2.in.fname = fname;
1016
1017         /* Create the readonly file. */
1018
1019         status = smb2_create(tree, tctx, &(io.smb2));
1020         CHECK_STATUS(status, NT_STATUS_OK);
1021         h1 = io.smb2.out.file.handle;
1022
1023         CHECK_VAL(io.smb2.out.oplock_level, 0);
1024         io.smb2.in.create_options = 0;
1025         CHECK_VAL(io.smb2.out.create_action, NTCREATEX_ACTION_CREATED);
1026         CHECK_ALL_INFO(io.smb2.out.file_attr, attrib);
1027         smb2_util_close(tree, h1);
1028
1029         /* Now try and open for delete only - should succeed. */
1030         io.smb2.in.desired_access = SEC_STD_DELETE;
1031         io.smb2.in.file_attributes = 0;
1032         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1033                                 NTCREATEX_SHARE_ACCESS_WRITE |
1034                                 NTCREATEX_SHARE_ACCESS_DELETE;
1035         io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
1036         status = smb2_create(tree, tctx, &(io.smb2));
1037         CHECK_STATUS(status, NT_STATUS_OK);
1038
1039         smb2_util_unlink(tree, fname);
1040
1041         smb2_util_close(tree, h1);
1042         smb2_util_unlink(tree, fname);
1043         smb2_deltree(tree, DNAME);
1044
1045         return ret;
1046 }
1047
1048 /*
1049   test SMB2 open with a leading slash on the path.
1050   Trying to create a directory with a leading slash
1051   should give NT_STATUS_INVALID_PARAMETER error
1052 */
1053 static bool test_smb2_leading_slash(struct torture_context *tctx,
1054                                     struct smb2_tree *tree)
1055 {
1056         union smb_open io;
1057         const char *dnameslash = "\\"DNAME;
1058         NTSTATUS status;
1059         bool ret = true;
1060
1061         torture_comment(tctx,
1062                 "Trying to create a directory with leading slash on path\n");
1063         smb2_deltree(tree, dnameslash);
1064
1065         ZERO_STRUCT(io.smb2);
1066         io.generic.level = RAW_OPEN_SMB2;
1067         io.smb2.in.oplock_level = 0;
1068         io.smb2.in.desired_access = SEC_RIGHTS_DIR_ALL;
1069         io.smb2.in.file_attributes   = FILE_ATTRIBUTE_DIRECTORY;
1070         io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
1071         io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1072                                 NTCREATEX_SHARE_ACCESS_WRITE |
1073                                 NTCREATEX_SHARE_ACCESS_DELETE;
1074         io.smb2.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1075         io.smb2.in.fname = dnameslash;
1076
1077         status = smb2_create(tree, tree, &(io.smb2));
1078         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
1079
1080         smb2_deltree(tree, dnameslash);
1081         return ret;
1082 }
1083
1084
1085 static bool test_create_acl_file(struct torture_context *tctx,
1086     struct smb2_tree *tree)
1087 {
1088         torture_comment(tctx, "testing nttrans create with sec_desc on files\n");
1089
1090         return test_create_acl_ext(tctx, tree, false);
1091 }
1092
1093 static bool test_create_acl_dir(struct torture_context *tctx,
1094     struct smb2_tree *tree)
1095 {
1096         torture_comment(tctx, "testing nttrans create with sec_desc on directories\n");
1097
1098         return test_create_acl_ext(tctx, tree, true);
1099 }
1100
1101 #define CHECK_ACCESS_FLAGS(_fh, flags) do { \
1102         union smb_fileinfo _q; \
1103         _q.access_information.level = RAW_FILEINFO_ACCESS_INFORMATION; \
1104         _q.access_information.in.file.handle = (_fh); \
1105         status = smb2_getinfo_file(tree, tctx, &_q); \
1106         CHECK_STATUS(status, NT_STATUS_OK); \
1107         if (_q.access_information.out.access_flags != (flags)) { \
1108                 torture_result(tctx, TORTURE_FAIL, "(%s) Incorrect access_flags 0x%08x - should be 0x%08x\n", \
1109                        __location__, _q.access_information.out.access_flags, (flags)); \
1110                 ret = false; \
1111                 goto done; \
1112         } \
1113 } while (0)
1114
1115 /*
1116  * Test creating a file with a NULL DACL.
1117  */
1118 static bool test_create_null_dacl(struct torture_context *tctx,
1119     struct smb2_tree *tree)
1120 {
1121         NTSTATUS status;
1122         struct smb2_create io;
1123         const char *fname = "nulldacl.txt";
1124         bool ret = true;
1125         struct smb2_handle handle;
1126         union smb_fileinfo q;
1127         union smb_setfileinfo s;
1128         struct security_descriptor *sd = security_descriptor_initialise(tctx);
1129         struct security_acl dacl;
1130
1131         torture_comment(tctx, "TESTING SEC_DESC WITH A NULL DACL\n");
1132
1133         smb2_util_unlink(tree, fname);
1134
1135         ZERO_STRUCT(io);
1136         io.level = RAW_OPEN_SMB2;
1137         io.in.create_flags = 0;
1138         io.in.desired_access = SEC_STD_READ_CONTROL | SEC_STD_WRITE_DAC
1139                 | SEC_STD_WRITE_OWNER;
1140         io.in.create_options = 0;
1141         io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
1142         io.in.share_access =
1143                 NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1144         io.in.alloc_size = 0;
1145         io.in.create_disposition = NTCREATEX_DISP_CREATE;
1146         io.in.impersonation_level = NTCREATEX_IMPERSONATION_ANONYMOUS;
1147         io.in.security_flags = 0;
1148         io.in.fname = fname;
1149         io.in.sec_desc = sd;
1150         /* XXX create_options ? */
1151         io.in.create_options            = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
1152                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
1153                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
1154                                           0x00200000;
1155
1156         torture_comment(tctx, "creating a file with a empty sd\n");
1157         status = smb2_create(tree, tctx, &io);
1158         CHECK_STATUS(status, NT_STATUS_OK);
1159         handle = io.out.file.handle;
1160
1161         torture_comment(tctx, "get the original sd\n");
1162         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
1163         q.query_secdesc.in.file.handle = handle;
1164         q.query_secdesc.in.secinfo_flags =
1165                 SECINFO_OWNER |
1166                 SECINFO_GROUP |
1167                 SECINFO_DACL;
1168         status = smb2_getinfo_file(tree, tctx, &q);
1169         CHECK_STATUS(status, NT_STATUS_OK);
1170
1171         /*
1172          * Testing the created DACL,
1173          * the server should add the inherited DACL
1174          * when SEC_DESC_DACL_PRESENT isn't specified
1175          */
1176         if (!(q.query_secdesc.out.sd->type & SEC_DESC_DACL_PRESENT)) {
1177                 ret = false;
1178                 torture_fail_goto(tctx, done, "DACL_PRESENT flag not set by the server!\n");
1179         }
1180         if (q.query_secdesc.out.sd->dacl == NULL) {
1181                 ret = false;
1182                 torture_fail_goto(tctx, done, "no DACL has been created on the server!\n");
1183         }
1184
1185         torture_comment(tctx, "set NULL DACL\n");
1186         sd->type |= SEC_DESC_DACL_PRESENT;
1187
1188         s.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
1189         s.set_secdesc.in.file.handle = handle;
1190         s.set_secdesc.in.secinfo_flags = SECINFO_DACL;
1191         s.set_secdesc.in.sd = sd;
1192         status = smb2_setinfo_file(tree, &s);
1193         CHECK_STATUS(status, NT_STATUS_OK);
1194
1195         torture_comment(tctx, "get the sd\n");
1196         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
1197         q.query_secdesc.in.file.handle = handle;
1198         q.query_secdesc.in.secinfo_flags =
1199                 SECINFO_OWNER |
1200                 SECINFO_GROUP |
1201                 SECINFO_DACL;
1202         status = smb2_getinfo_file(tree, tctx, &q);
1203         CHECK_STATUS(status, NT_STATUS_OK);
1204
1205         /* Testing the modified DACL */
1206         if (!(q.query_secdesc.out.sd->type & SEC_DESC_DACL_PRESENT)) {
1207                 ret = false;
1208                 torture_fail_goto(tctx, done, "DACL_PRESENT flag not set by the server!\n");
1209         }
1210         if (q.query_secdesc.out.sd->dacl != NULL) {
1211                 ret = false;
1212                 torture_fail_goto(tctx, done, "DACL has been created on the server!\n");
1213         }
1214
1215         io.in.create_disposition = NTCREATEX_DISP_OPEN;
1216
1217         torture_comment(tctx, "try open for read control\n");
1218         io.in.desired_access = SEC_STD_READ_CONTROL;
1219         status = smb2_create(tree, tctx, &io);
1220         CHECK_STATUS(status, NT_STATUS_OK);
1221         CHECK_ACCESS_FLAGS(io.out.file.handle,
1222                 SEC_STD_READ_CONTROL);
1223         smb2_util_close(tree, io.out.file.handle);
1224
1225         torture_comment(tctx, "try open for write\n");
1226         io.in.desired_access = SEC_FILE_WRITE_DATA;
1227         status = smb2_create(tree, tctx, &io);
1228         CHECK_STATUS(status, NT_STATUS_OK);
1229         CHECK_ACCESS_FLAGS(io.out.file.handle,
1230                 SEC_FILE_WRITE_DATA);
1231         smb2_util_close(tree, io.out.file.handle);
1232
1233         torture_comment(tctx, "try open for read\n");
1234         io.in.desired_access = SEC_FILE_READ_DATA;
1235         status = smb2_create(tree, tctx, &io);
1236         CHECK_STATUS(status, NT_STATUS_OK);
1237         CHECK_ACCESS_FLAGS(io.out.file.handle,
1238                 SEC_FILE_READ_DATA);
1239         smb2_util_close(tree, io.out.file.handle);
1240
1241         torture_comment(tctx, "try open for generic write\n");
1242         io.in.desired_access = SEC_GENERIC_WRITE;
1243         status = smb2_create(tree, tctx, &io);
1244         CHECK_STATUS(status, NT_STATUS_OK);
1245         CHECK_ACCESS_FLAGS(io.out.file.handle,
1246                 SEC_RIGHTS_FILE_WRITE);
1247         smb2_util_close(tree, io.out.file.handle);
1248
1249         torture_comment(tctx, "try open for generic read\n");
1250         io.in.desired_access = SEC_GENERIC_READ;
1251         status = smb2_create(tree, tctx, &io);
1252         CHECK_STATUS(status, NT_STATUS_OK);
1253         CHECK_ACCESS_FLAGS(io.out.file.handle,
1254                 SEC_RIGHTS_FILE_READ);
1255         smb2_util_close(tree, io.out.file.handle);
1256
1257         torture_comment(tctx, "set DACL with 0 aces\n");
1258         ZERO_STRUCT(dacl);
1259         dacl.revision = SECURITY_ACL_REVISION_NT4;
1260         dacl.num_aces = 0;
1261         sd->dacl = &dacl;
1262
1263         s.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
1264         s.set_secdesc.in.file.handle = handle;
1265         s.set_secdesc.in.secinfo_flags = SECINFO_DACL;
1266         s.set_secdesc.in.sd = sd;
1267         status = smb2_setinfo_file(tree, &s);
1268         CHECK_STATUS(status, NT_STATUS_OK);
1269
1270         torture_comment(tctx, "get the sd\n");
1271         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
1272         q.query_secdesc.in.file.handle = handle;
1273         q.query_secdesc.in.secinfo_flags =
1274                 SECINFO_OWNER |
1275                 SECINFO_GROUP |
1276                 SECINFO_DACL;
1277         status = smb2_getinfo_file(tree, tctx, &q);
1278         CHECK_STATUS(status, NT_STATUS_OK);
1279
1280         /* Testing the modified DACL */
1281         if (!(q.query_secdesc.out.sd->type & SEC_DESC_DACL_PRESENT)) {
1282                 ret = false;
1283                 torture_fail_goto(tctx, done, "DACL_PRESENT flag not set by the server!\n");
1284         }
1285         if (q.query_secdesc.out.sd->dacl == NULL) {
1286                 ret = false;
1287                 torture_fail_goto(tctx, done, "no DACL has been created on the server!\n");
1288         }
1289         if (q.query_secdesc.out.sd->dacl->num_aces != 0) {
1290                 torture_result(tctx, TORTURE_FAIL, "DACL has %u aces!\n",
1291                        q.query_secdesc.out.sd->dacl->num_aces);
1292                 ret = false;
1293                 goto done;
1294         }
1295
1296         torture_comment(tctx, "try open for read control\n");
1297         io.in.desired_access = SEC_STD_READ_CONTROL;
1298         status = smb2_create(tree, tctx, &io);
1299         CHECK_STATUS(status, NT_STATUS_OK);
1300         CHECK_ACCESS_FLAGS(io.out.file.handle,
1301                 SEC_STD_READ_CONTROL);
1302         smb2_util_close(tree, io.out.file.handle);
1303
1304         torture_comment(tctx, "try open for write => access_denied\n");
1305         io.in.desired_access = SEC_FILE_WRITE_DATA;
1306         status = smb2_create(tree, tctx, &io);
1307         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
1308
1309         torture_comment(tctx, "try open for read => access_denied\n");
1310         io.in.desired_access = SEC_FILE_READ_DATA;
1311         status = smb2_create(tree, tctx, &io);
1312         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
1313
1314         torture_comment(tctx, "try open for generic write => access_denied\n");
1315         io.in.desired_access = SEC_GENERIC_WRITE;
1316         status = smb2_create(tree, tctx, &io);
1317         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
1318
1319         torture_comment(tctx, "try open for generic read => access_denied\n");
1320         io.in.desired_access = SEC_GENERIC_READ;
1321         status = smb2_create(tree, tctx, &io);
1322         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
1323
1324         torture_comment(tctx, "set empty sd\n");
1325         sd->type &= ~SEC_DESC_DACL_PRESENT;
1326         sd->dacl = NULL;
1327
1328         s.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
1329         s.set_secdesc.in.file.handle = handle;
1330         s.set_secdesc.in.secinfo_flags = SECINFO_DACL;
1331         s.set_secdesc.in.sd = sd;
1332         status = smb2_setinfo_file(tree, &s);
1333         CHECK_STATUS(status, NT_STATUS_OK);
1334
1335         torture_comment(tctx, "get the sd\n");
1336         q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
1337         q.query_secdesc.in.file.handle = handle;
1338         q.query_secdesc.in.secinfo_flags =
1339                 SECINFO_OWNER |
1340                 SECINFO_GROUP |
1341                 SECINFO_DACL;
1342         status = smb2_getinfo_file(tree, tctx, &q);
1343         CHECK_STATUS(status, NT_STATUS_OK);
1344
1345         /* Testing the modified DACL */
1346         if (!(q.query_secdesc.out.sd->type & SEC_DESC_DACL_PRESENT)) {
1347                 ret = false;
1348                 torture_fail_goto(tctx, done, "DACL_PRESENT flag not set by the server!\n");
1349         }
1350         if (q.query_secdesc.out.sd->dacl != NULL) {
1351                 ret = false;
1352                 torture_fail_goto(tctx, done, "DACL has been created on the server!\n");
1353         }
1354 done:
1355         smb2_util_close(tree, handle);
1356         smb2_util_unlink(tree, fname);
1357         smb2_tdis(tree);
1358         smb2_logoff(tree->session);
1359         return ret;
1360 }
1361
1362 /*
1363    basic testing of SMB2 read
1364 */
1365 struct torture_suite *torture_smb2_create_init(void)
1366 {
1367         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "CREATE");
1368
1369         torture_suite_add_1smb2_test(suite, "GENTEST", test_create_gentest);
1370         torture_suite_add_1smb2_test(suite, "BLOB", test_create_blob);
1371         torture_suite_add_1smb2_test(suite, "OPEN", test_smb2_open);
1372         torture_suite_add_1smb2_test(suite, "BRLOCKED", test_smb2_open_brlocked);
1373         torture_suite_add_1smb2_test(suite, "MULTI", test_smb2_open_multi);
1374         torture_suite_add_1smb2_test(suite, "DELETE", test_smb2_open_for_delete);
1375         torture_suite_add_1smb2_test(suite, "LEADING-SLASH", test_smb2_leading_slash);
1376         torture_suite_add_1smb2_test(suite, "ACLFILE", test_create_acl_file);
1377         torture_suite_add_1smb2_test(suite, "ACLDIR", test_create_acl_dir);
1378         torture_suite_add_1smb2_test(suite, "NULLDACL", test_create_null_dacl);
1379
1380         suite->description = talloc_strdup(suite, "SMB2-CREATE tests");
1381
1382         return suite;
1383 }