73f9c3f839e2bb5675e108379b5857ae6371af10
[samba.git] / source4 / torture / raw / open.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RAW_OPEN_* individual test suite
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "torture/torture.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "libcli/raw/raw_proto.h"
24 #include "system/time.h"
25 #include "system/filesys.h"
26 #include "librpc/gen_ndr/security.h"
27 #include "lib/events/events.h"
28 #include "libcli/libcli.h"
29 #include "torture/util.h"
30 #include "auth/credentials/credentials.h"
31 #include "lib/cmdline/popt_common.h"
32
33 /* enum for whether reads/writes are possible on a file */
34 enum rdwr_mode {RDWR_NONE, RDWR_RDONLY, RDWR_WRONLY, RDWR_RDWR};
35
36 #define BASEDIR "\\rawopen"
37
38 /*
39   check if a open file can be read/written
40 */
41 static enum rdwr_mode check_rdwr(struct smbcli_tree *tree, int fnum)
42 {
43         uint8_t c = 1;
44         bool can_read  = (smbcli_read(tree, fnum, &c, 0, 1) == 1);
45         bool can_write = (smbcli_write(tree, fnum, 0, &c, 0, 1) == 1);
46         if ( can_read &&  can_write) return RDWR_RDWR;
47         if ( can_read && !can_write) return RDWR_RDONLY;
48         if (!can_read &&  can_write) return RDWR_WRONLY;
49         return RDWR_NONE;
50 }
51
52 /*
53   describe a RDWR mode as a string
54 */
55 static const char *rdwr_string(enum rdwr_mode m)
56 {
57         switch (m) {
58         case RDWR_NONE: return "NONE";
59         case RDWR_RDONLY: return "RDONLY";
60         case RDWR_WRONLY: return "WRONLY";
61         case RDWR_RDWR: return "RDWR";
62         }
63         return "-";
64 }
65
66 #define CHECK_STATUS(status, correct) do { \
67         if (!NT_STATUS_EQUAL(status, correct)) { \
68                 torture_result(tctx, TORTURE_FAIL, \
69                         "(%s) Incorrect status %s - should be %s\n", \
70                        __location__, nt_errstr(status), nt_errstr(correct)); \
71                 ret = false; \
72                 goto done; \
73         }} while (0)
74
75 #define CREATE_FILE do { \
76         fnum = create_complex_file(cli, tctx, fname); \
77         if (fnum == -1) { \
78                 torture_result(tctx, TORTURE_FAIL, \
79                         "(%s) Failed to create %s - %s\n", \
80                          __location__, fname, smbcli_errstr(cli->tree)); \
81                 ret = false; \
82                 goto done; \
83         }} while (0)
84
85 #define CHECK_RDWR(fnum, correct) do { \
86         enum rdwr_mode m = check_rdwr(cli->tree, fnum); \
87         if (m != correct) { \
88                 torture_result(tctx, TORTURE_FAIL, \
89                        "(%s) Incorrect readwrite mode %s - expected %s\n", \
90                        __location__, rdwr_string(m), rdwr_string(correct)); \
91                 ret = false; \
92         }} while (0)
93
94 #define CHECK_TIME(t, field) do { \
95         time_t t1, t2; \
96         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
97         finfo.all_info.in.file.path = fname; \
98         status = smb_raw_pathinfo(cli->tree, tctx, &finfo); \
99         CHECK_STATUS(status, NT_STATUS_OK); \
100         t1 = t & ~1; \
101         t2 = nt_time_to_unix(finfo.all_info.out.field) & ~1; \
102         if (abs(t1-t2) > 2) { \
103                 torture_result(tctx, TORTURE_FAIL, \
104                        "(%s) wrong time for field %s  %s - %s\n", \
105                        __location__, #field, \
106                        timestring(tctx, t1), \
107                        timestring(tctx, t2)); \
108                 dump_all_info(tctx, &finfo); \
109                 ret = false; \
110         }} while (0)
111
112 #define CHECK_NTTIME(t, field) do { \
113         NTTIME t2; \
114         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
115         finfo.all_info.in.file.path = fname; \
116         status = smb_raw_pathinfo(cli->tree, tctx, &finfo); \
117         CHECK_STATUS(status, NT_STATUS_OK); \
118         t2 = finfo.all_info.out.field; \
119         if (t != t2) { \
120                 torture_result(tctx, TORTURE_FAIL, \
121                        "(%s) wrong time for field %s  %s - %s\n", \
122                        __location__, #field, \
123                        nt_time_string(tctx, t), \
124                        nt_time_string(tctx, t2)); \
125                 dump_all_info(tctx, &finfo); \
126                 ret = false; \
127         }} while (0)
128
129 #define CHECK_ALL_INFO(v, field) do { \
130         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
131         finfo.all_info.in.file.path = fname; \
132         status = smb_raw_pathinfo(cli->tree, tctx, &finfo); \
133         CHECK_STATUS(status, NT_STATUS_OK); \
134         if ((v) != (finfo.all_info.out.field)) { \
135                 torture_result(tctx, TORTURE_FAIL, \
136                        "(%s) wrong value for field %s  0x%x - 0x%x\n", \
137                        __location__, #field, (int)v, (int)(finfo.all_info.out.field)); \
138                 dump_all_info(tctx, &finfo); \
139                 ret = false; \
140         }} while (0)
141
142 #define CHECK_VAL(v, correct) do { \
143         if ((v) != (correct)) { \
144                 torture_result(tctx, TORTURE_FAIL, \
145                        "(%s) wrong value for %s  0x%x - should be 0x%x\n", \
146                        __location__, #v, (int)(v), (int)correct); \
147                 ret = false; \
148         }} while (0)
149
150 #define SET_ATTRIB(sattrib) do { \
151         union smb_setfileinfo sfinfo; \
152         ZERO_STRUCT(sfinfo.basic_info.in); \
153         sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION; \
154         sfinfo.basic_info.in.file.path = fname; \
155         sfinfo.basic_info.in.attrib = sattrib; \
156         status = smb_raw_setpathinfo(cli->tree, &sfinfo); \
157         if (!NT_STATUS_IS_OK(status)) { \
158                 torture_warning(tctx, "(%s) Failed to set attrib 0x%x on %s\n", \
159                        __location__, sattrib, fname); \
160         }} while (0)
161
162 /*
163   test RAW_OPEN_OPEN
164 */
165 static bool test_open(struct smbcli_state *cli, struct torture_context *tctx)
166 {
167         union smb_open io;
168         union smb_fileinfo finfo;
169         const char *fname = BASEDIR "\\torture_open.txt";
170         NTSTATUS status;
171         int fnum = -1, fnum2;
172         bool ret = true;
173
174         torture_comment(tctx, "Checking RAW_OPEN_OPEN\n");
175
176         io.openold.level = RAW_OPEN_OPEN;
177         io.openold.in.fname = fname;
178         io.openold.in.open_mode = OPEN_FLAGS_FCB;
179         io.openold.in.search_attrs = 0;
180         status = smb_raw_open(cli->tree, tctx, &io);
181         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
182         fnum = io.openold.out.file.fnum;
183
184         smbcli_unlink(cli->tree, fname);
185         CREATE_FILE;
186         smbcli_close(cli->tree, fnum);
187
188         status = smb_raw_open(cli->tree, tctx, &io);
189         CHECK_STATUS(status, NT_STATUS_OK);
190         fnum = io.openold.out.file.fnum;
191         CHECK_RDWR(fnum, RDWR_RDWR);
192
193         status = smb_raw_open(cli->tree, tctx, &io);
194         CHECK_STATUS(status, NT_STATUS_OK);
195         fnum2 = io.openold.out.file.fnum;
196         CHECK_RDWR(fnum2, RDWR_RDWR);
197         smbcli_close(cli->tree, fnum2);
198         smbcli_close(cli->tree, fnum);
199
200         /* check the read/write modes */
201         io.openold.level = RAW_OPEN_OPEN;
202         io.openold.in.fname = fname;
203         io.openold.in.search_attrs = 0;
204
205         io.openold.in.open_mode = OPEN_FLAGS_OPEN_READ;
206         status = smb_raw_open(cli->tree, tctx, &io);
207         CHECK_STATUS(status, NT_STATUS_OK);
208         fnum = io.openold.out.file.fnum;
209         CHECK_RDWR(fnum, RDWR_RDONLY);
210         smbcli_close(cli->tree, fnum);
211
212         io.openold.in.open_mode = OPEN_FLAGS_OPEN_WRITE;
213         status = smb_raw_open(cli->tree, tctx, &io);
214         CHECK_STATUS(status, NT_STATUS_OK);
215         fnum = io.openold.out.file.fnum;
216         CHECK_RDWR(fnum, RDWR_WRONLY);
217         smbcli_close(cli->tree, fnum);
218
219         io.openold.in.open_mode = OPEN_FLAGS_OPEN_RDWR;
220         status = smb_raw_open(cli->tree, tctx, &io);
221         CHECK_STATUS(status, NT_STATUS_OK);
222         fnum = io.openold.out.file.fnum;
223         CHECK_RDWR(fnum, RDWR_RDWR);
224         smbcli_close(cli->tree, fnum);
225
226         /* check the share modes roughly - not a complete matrix */
227         io.openold.in.open_mode = OPEN_FLAGS_OPEN_RDWR | OPEN_FLAGS_DENY_WRITE;
228         status = smb_raw_open(cli->tree, tctx, &io);
229         CHECK_STATUS(status, NT_STATUS_OK);
230         fnum = io.openold.out.file.fnum;
231         CHECK_RDWR(fnum, RDWR_RDWR);
232         
233         if (io.openold.in.open_mode != io.openold.out.rmode) {
234                 torture_warning(tctx, "(%s) rmode should equal open_mode - 0x%x 0x%x\n",
235                        __location__, io.openold.out.rmode, io.openold.in.open_mode);
236         }
237
238         io.openold.in.open_mode = OPEN_FLAGS_OPEN_RDWR | OPEN_FLAGS_DENY_NONE;
239         status = smb_raw_open(cli->tree, tctx, &io);
240         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
241
242         io.openold.in.open_mode = OPEN_FLAGS_OPEN_READ | OPEN_FLAGS_DENY_NONE;
243         status = smb_raw_open(cli->tree, tctx, &io);
244         CHECK_STATUS(status, NT_STATUS_OK);
245         fnum2 = io.openold.out.file.fnum;
246         CHECK_RDWR(fnum2, RDWR_RDONLY);
247         smbcli_close(cli->tree, fnum);
248         smbcli_close(cli->tree, fnum2);
249
250
251         /* check the returned write time */
252         io.openold.level = RAW_OPEN_OPEN;
253         io.openold.in.fname = fname;
254         io.openold.in.search_attrs = 0;
255         io.openold.in.open_mode = OPEN_FLAGS_OPEN_READ;
256         status = smb_raw_open(cli->tree, tctx, &io);
257         CHECK_STATUS(status, NT_STATUS_OK);
258         fnum = io.openold.out.file.fnum;
259
260         /* check other reply fields */
261         CHECK_TIME(io.openold.out.write_time, write_time);
262         CHECK_ALL_INFO(io.openold.out.size, size);
263         CHECK_ALL_INFO(io.openold.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
264
265 done:
266         smbcli_close(cli->tree, fnum);
267         smbcli_unlink(cli->tree, fname);
268
269         return ret;
270 }
271
272
273 /*
274   test RAW_OPEN_OPENX
275 */
276 static bool test_openx(struct smbcli_state *cli, struct torture_context *tctx)
277 {
278         union smb_open io;
279         union smb_fileinfo finfo;
280         const char *fname = BASEDIR "\\torture_openx.txt";
281         const char *fname_exe = BASEDIR "\\torture_openx.exe";
282         NTSTATUS status;
283         int fnum = -1, fnum2;
284         bool ret = true;
285         int i;
286         struct timeval tv;
287         struct {
288                 uint16_t open_func;
289                 bool with_file;
290                 NTSTATUS correct_status;
291         } open_funcs[] = {
292                 { OPENX_OPEN_FUNC_OPEN,                           true,  NT_STATUS_OK },
293                 { OPENX_OPEN_FUNC_OPEN,                           false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
294                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OK },
295                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OK },
296                 { OPENX_OPEN_FUNC_FAIL,                           true,  NT_STATUS_DOS(ERRDOS, ERRbadaccess) },
297                 { OPENX_OPEN_FUNC_FAIL,                           false, NT_STATUS_DOS(ERRDOS, ERRbadaccess) },
298                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OBJECT_NAME_COLLISION },
299                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OK },
300                 { OPENX_OPEN_FUNC_TRUNC,                          true,  NT_STATUS_OK },
301                 { OPENX_OPEN_FUNC_TRUNC,                          false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
302                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OK },
303                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OK },
304         };
305
306         torture_comment(tctx, "Checking RAW_OPEN_OPENX\n");
307         smbcli_unlink(cli->tree, fname);
308
309         io.openx.level = RAW_OPEN_OPENX;
310         io.openx.in.fname = fname;
311         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
312         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
313         io.openx.in.search_attrs = 0;
314         io.openx.in.file_attrs = 0;
315         io.openx.in.write_time = 0;
316         io.openx.in.size = 1024*1024;
317         io.openx.in.timeout = 0;
318
319         /* check all combinations of open_func */
320         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
321                 if (open_funcs[i].with_file) {
322                         fnum = create_complex_file(cli, tctx, fname);
323                         if (fnum == -1) {
324                                 torture_result(tctx, TORTURE_FAIL,
325                                         "Failed to create file %s - %s\n",
326                                         fname, smbcli_errstr(cli->tree));
327                                 ret = false;
328                                 goto done;
329                         }
330                         smbcli_close(cli->tree, fnum);
331                 }
332                 io.openx.in.open_func = open_funcs[i].open_func;
333                 status = smb_raw_open(cli->tree, tctx, &io);
334                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
335                         torture_result(tctx, TORTURE_FAIL,
336                                 "(%s) incorrect status %s should be %s "
337                                 "(i=%d with_file=%d open_func=0x%x)\n",
338                                 __location__, nt_errstr(status),
339                                 nt_errstr(open_funcs[i].correct_status),
340                                 i, (int)open_funcs[i].with_file,
341                                 (int)open_funcs[i].open_func);
342                         ret = false;
343                 }
344                 if (NT_STATUS_IS_OK(status)) {
345                         smbcli_close(cli->tree, io.openx.out.file.fnum);
346                 }
347                 if (open_funcs[i].with_file) {
348                         smbcli_unlink(cli->tree, fname);
349                 }
350         }
351
352         smbcli_unlink(cli->tree, fname);
353
354         /* check the basic return fields */
355         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
356         status = smb_raw_open(cli->tree, tctx, &io);
357         CHECK_STATUS(status, NT_STATUS_OK);
358         fnum = io.openx.out.file.fnum;
359
360         CHECK_ALL_INFO(io.openx.out.size, size);
361         CHECK_TIME(io.openx.out.write_time, write_time);
362         CHECK_ALL_INFO(io.openx.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
363         CHECK_VAL(io.openx.out.access, OPENX_MODE_ACCESS_RDWR);
364         CHECK_VAL(io.openx.out.ftype, 0);
365         CHECK_VAL(io.openx.out.devstate, 0);
366         CHECK_VAL(io.openx.out.action, OPENX_ACTION_CREATED);
367         CHECK_VAL(io.openx.out.size, 1024*1024);
368         CHECK_ALL_INFO(io.openx.in.size, size);
369         smbcli_close(cli->tree, fnum);
370         smbcli_unlink(cli->tree, fname);
371
372         /* check the fields when the file already existed */
373         fnum2 = create_complex_file(cli, tctx, fname);
374         if (fnum2 == -1) {
375                 ret = false;
376                 goto done;
377         }
378         smbcli_close(cli->tree, fnum2);
379
380         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
381         status = smb_raw_open(cli->tree, tctx, &io);
382         CHECK_STATUS(status, NT_STATUS_OK);
383         fnum = io.openx.out.file.fnum;
384
385         CHECK_ALL_INFO(io.openx.out.size, size);
386         CHECK_TIME(io.openx.out.write_time, write_time);
387         CHECK_VAL(io.openx.out.action, OPENX_ACTION_EXISTED);
388         CHECK_VAL(io.openx.out.unknown, 0);
389         CHECK_ALL_INFO(io.openx.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
390         smbcli_close(cli->tree, fnum);
391
392         /* now check the search attrib for hidden files - win2003 ignores this? */
393         SET_ATTRIB(FILE_ATTRIBUTE_HIDDEN);
394         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN, attrib);
395
396         io.openx.in.search_attrs = FILE_ATTRIBUTE_HIDDEN;
397         status = smb_raw_open(cli->tree, tctx, &io);
398         CHECK_STATUS(status, NT_STATUS_OK);
399         smbcli_close(cli->tree, io.openx.out.file.fnum);
400
401         io.openx.in.search_attrs = 0;
402         status = smb_raw_open(cli->tree, tctx, &io);
403         CHECK_STATUS(status, NT_STATUS_OK);
404         smbcli_close(cli->tree, io.openx.out.file.fnum);
405
406         SET_ATTRIB(FILE_ATTRIBUTE_NORMAL);
407         smbcli_unlink(cli->tree, fname);
408
409         /* and check attrib on create */
410         io.openx.in.open_func = OPENX_OPEN_FUNC_FAIL | OPENX_OPEN_FUNC_CREATE;
411         io.openx.in.search_attrs = 0;
412         io.openx.in.file_attrs = FILE_ATTRIBUTE_SYSTEM;
413         status = smb_raw_open(cli->tree, tctx, &io);
414         CHECK_STATUS(status, NT_STATUS_OK);
415         if (torture_setting_bool(tctx, "samba3", false)) {
416                 CHECK_ALL_INFO(FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE, 
417                                attrib & ~(FILE_ATTRIBUTE_NONINDEXED|
418                                           FILE_ATTRIBUTE_SPARSE));
419         }
420         else {
421                 CHECK_ALL_INFO(FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE, 
422                                attrib & ~(FILE_ATTRIBUTE_NONINDEXED));
423         }
424         smbcli_close(cli->tree, io.openx.out.file.fnum);
425         smbcli_unlink(cli->tree, fname);
426
427         /* check timeout on create - win2003 ignores the timeout! */
428         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
429         io.openx.in.file_attrs = 0;
430         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_ALL;
431         status = smb_raw_open(cli->tree, tctx, &io);
432         CHECK_STATUS(status, NT_STATUS_OK);
433         fnum = io.openx.out.file.fnum;
434
435         io.openx.in.timeout = 20000;
436         tv = timeval_current();
437         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_NONE;
438         status = smb_raw_open(cli->tree, tctx, &io);
439         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
440         if (timeval_elapsed(&tv) > 3.0) {
441                 torture_result(tctx, TORTURE_FAIL,
442                         "(%s) Incorrect timing in openx with timeout "
443                         "- waited %.2f seconds\n",
444                         __location__, timeval_elapsed(&tv));
445                 ret = false;
446         }
447         smbcli_close(cli->tree, fnum);
448         smbcli_unlink(cli->tree, fname);
449
450         /* now this is a really weird one - open for execute implies create?! */
451         io.openx.in.fname = fname;
452         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
453         io.openx.in.open_mode = OPENX_MODE_ACCESS_EXEC | OPENX_MODE_DENY_NONE;
454         io.openx.in.search_attrs = 0;
455         io.openx.in.open_func = OPENX_OPEN_FUNC_FAIL;
456         io.openx.in.file_attrs = 0;
457         io.openx.in.write_time = 0;
458         io.openx.in.size = 0;
459         io.openx.in.timeout = 0;
460         status = smb_raw_open(cli->tree, tctx, &io);
461         CHECK_STATUS(status, NT_STATUS_OK);
462         smbcli_close(cli->tree, io.openx.out.file.fnum);
463
464         /* check the extended return flag */
465         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO | OPENX_FLAGS_EXTENDED_RETURN;
466         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
467         status = smb_raw_open(cli->tree, tctx, &io);
468         CHECK_STATUS(status, NT_STATUS_OK);
469         CHECK_VAL(io.openx.out.access_mask, SEC_STD_ALL);
470         smbcli_close(cli->tree, io.openx.out.file.fnum);
471
472         io.openx.in.fname = "\\A.+,;=[].B";
473         status = smb_raw_open(cli->tree, tctx, &io);
474         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
475
476         /* Check the mapping for open exec. */
477
478         /* First create an .exe file. */
479         smbcli_unlink(cli->tree, fname_exe);
480         fnum = create_complex_file(cli, tctx, fname_exe);
481         smbcli_close(cli->tree, fnum);
482
483         io.openx.level = RAW_OPEN_OPENX;
484         io.openx.in.fname = fname_exe;
485         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
486         io.openx.in.open_mode = OPENX_MODE_ACCESS_EXEC | OPENX_MODE_DENY_NONE;
487         io.openx.in.search_attrs = 0;
488         io.openx.in.file_attrs = 0;
489         io.openx.in.write_time = 0;
490         io.openx.in.size = 0;
491         io.openx.in.timeout = 0;
492         status = smb_raw_open(cli->tree, tctx, &io);
493         CHECK_STATUS(status, NT_STATUS_OK);
494
495         /* Can we read and write ? */
496         CHECK_RDWR(io.openx.out.file.fnum, RDWR_RDONLY);
497         smbcli_close(cli->tree, io.openx.out.file.fnum);
498         smbcli_unlink(cli->tree, fname);
499
500 done:
501         smbcli_close(cli->tree, fnum);
502         smbcli_unlink(cli->tree, fname_exe);
503         smbcli_unlink(cli->tree, fname);
504
505         return ret;
506 }
507
508
509 /*
510   test RAW_OPEN_T2OPEN
511
512   many thanks to kukks for a sniff showing how this works with os2->w2k
513 */
514 static bool test_t2open(struct smbcli_state *cli, struct torture_context *tctx)
515 {
516         union smb_open io;
517         union smb_fileinfo finfo;
518         const char *fname1 = BASEDIR "\\torture_t2open_yes.txt";
519         const char *fname2 = BASEDIR "\\torture_t2open_no.txt";
520         const char *fname = BASEDIR "\\torture_t2open_3.txt";
521         NTSTATUS status;
522         int fnum;
523         bool ret = true;
524         int i;
525         struct {
526                 uint16_t open_func;
527                 bool with_file;
528                 NTSTATUS correct_status;
529         } open_funcs[] = {
530                 { OPENX_OPEN_FUNC_OPEN,                           true,  NT_STATUS_OK },
531                 { OPENX_OPEN_FUNC_OPEN,                           false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
532                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OK },
533                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OK },
534                 { OPENX_OPEN_FUNC_FAIL,                           true,  NT_STATUS_OBJECT_NAME_COLLISION },
535                 { OPENX_OPEN_FUNC_FAIL,                           false, NT_STATUS_OBJECT_NAME_COLLISION },
536                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OBJECT_NAME_COLLISION },
537                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OBJECT_NAME_COLLISION },
538                 { OPENX_OPEN_FUNC_TRUNC,                          true,  NT_STATUS_OK },
539                 { OPENX_OPEN_FUNC_TRUNC,                          false, NT_STATUS_OK },
540                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, true,  NT_STATUS_OK },
541                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, false, NT_STATUS_OK },
542         };
543
544         fnum = create_complex_file(cli, tctx, fname1);
545         if (fnum == -1) {
546                 torture_result(tctx, TORTURE_FAIL,
547                         "(%s): Failed to create file %s - %s\n",
548                         __location__, fname1, smbcli_errstr(cli->tree));
549                 ret = false;
550                 goto done;
551         }
552         smbcli_close(cli->tree, fnum);
553
554         torture_comment(tctx, "Checking RAW_OPEN_T2OPEN\n");
555
556         io.t2open.level = RAW_OPEN_T2OPEN;
557         io.t2open.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
558         io.t2open.in.open_mode = OPENX_MODE_DENY_NONE | OPENX_MODE_ACCESS_RDWR;
559         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
560         io.t2open.in.search_attrs = 0;
561         io.t2open.in.file_attrs = 0;
562         io.t2open.in.write_time = 0;
563         io.t2open.in.size = 0;
564         io.t2open.in.timeout = 0;
565
566         io.t2open.in.num_eas = 3;
567         io.t2open.in.eas = talloc_array(tctx, struct ea_struct, io.t2open.in.num_eas);
568         io.t2open.in.eas[0].flags = 0;
569         io.t2open.in.eas[0].name.s = ".CLASSINFO";
570         io.t2open.in.eas[0].value = data_blob_talloc(tctx, "first value", 11);
571         io.t2open.in.eas[1].flags = 0;
572         io.t2open.in.eas[1].name.s = "EA TWO";
573         io.t2open.in.eas[1].value = data_blob_talloc(tctx, "foo", 3);
574         io.t2open.in.eas[2].flags = 0;
575         io.t2open.in.eas[2].name.s = "X THIRD";
576         io.t2open.in.eas[2].value = data_blob_talloc(tctx, "xy", 2);
577
578         /* check all combinations of open_func */
579         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
580         again:
581                 if (open_funcs[i].with_file) {
582                         io.t2open.in.fname = fname1;
583                 } else {
584                         io.t2open.in.fname = fname2;
585                 }
586                 io.t2open.in.open_func = open_funcs[i].open_func;
587                 status = smb_raw_open(cli->tree, tctx, &io);
588                 if ((io.t2open.in.num_eas != 0)
589                     && NT_STATUS_EQUAL(status, NT_STATUS_EAS_NOT_SUPPORTED)
590                     && torture_setting_bool(tctx, "samba3", false)) {
591                         torture_warning(tctx, "(%s) EAs not supported, not "
592                                 "treating as fatal in Samba3 test\n",
593                                 __location__);
594                         io.t2open.in.num_eas = 0;
595                         goto again;
596                 }
597
598                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
599                         torture_result(tctx, TORTURE_FAIL,
600                                 "(%s) incorrect status %s should be %s "
601                                 "(i=%d with_file=%d open_func=0x%x)\n",
602                                  __location__, nt_errstr(status),
603                                 nt_errstr(open_funcs[i].correct_status),
604                                 i, (int)open_funcs[i].with_file,
605                                 (int)open_funcs[i].open_func);
606                         ret = false;
607                 }
608                 if (NT_STATUS_IS_OK(status)) {
609                         smbcli_close(cli->tree, io.t2open.out.file.fnum);
610                 }
611         }
612
613         smbcli_unlink(cli->tree, fname1);
614         smbcli_unlink(cli->tree, fname2);
615
616         /* check the basic return fields */
617         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
618         io.t2open.in.write_time = 0;
619         io.t2open.in.fname = fname;
620         status = smb_raw_open(cli->tree, tctx, &io);
621         CHECK_STATUS(status, NT_STATUS_OK);
622         fnum = io.t2open.out.file.fnum;
623
624         CHECK_ALL_INFO(io.t2open.out.size, size);
625 #if 0
626         /* windows appears to leak uninitialised memory here */
627         CHECK_VAL(io.t2open.out.write_time, 0);
628 #endif
629         CHECK_ALL_INFO(io.t2open.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
630         CHECK_VAL(io.t2open.out.access, OPENX_MODE_DENY_NONE | OPENX_MODE_ACCESS_RDWR);
631         CHECK_VAL(io.t2open.out.ftype, 0);
632         CHECK_VAL(io.t2open.out.devstate, 0);
633         CHECK_VAL(io.t2open.out.action, OPENX_ACTION_CREATED);
634         smbcli_close(cli->tree, fnum);
635
636         status = torture_check_ea(cli, fname, ".CLASSINFO", "first value");
637         CHECK_STATUS(status, io.t2open.in.num_eas
638                      ? NT_STATUS_OK : NT_STATUS_EAS_NOT_SUPPORTED);
639         status = torture_check_ea(cli, fname, "EA TWO", "foo");
640         CHECK_STATUS(status, io.t2open.in.num_eas
641                      ? NT_STATUS_OK : NT_STATUS_EAS_NOT_SUPPORTED);
642         status = torture_check_ea(cli, fname, "X THIRD", "xy");
643         CHECK_STATUS(status, io.t2open.in.num_eas
644                      ? NT_STATUS_OK : NT_STATUS_EAS_NOT_SUPPORTED);
645
646         /* now check the search attrib for hidden files - win2003 ignores this? */
647         SET_ATTRIB(FILE_ATTRIBUTE_HIDDEN);
648         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN, attrib);
649
650         status = smb_raw_open(cli->tree, tctx, &io);
651         CHECK_STATUS(status, NT_STATUS_OK);
652         smbcli_close(cli->tree, io.t2open.out.file.fnum);
653
654         status = smb_raw_open(cli->tree, tctx, &io);
655         CHECK_STATUS(status, NT_STATUS_OK);
656         smbcli_close(cli->tree, io.t2open.out.file.fnum);
657
658         SET_ATTRIB(FILE_ATTRIBUTE_NORMAL);
659         smbcli_unlink(cli->tree, fname);
660
661         /* and check attrib on create */
662         io.t2open.in.open_func = OPENX_OPEN_FUNC_FAIL | OPENX_OPEN_FUNC_CREATE;
663         io.t2open.in.file_attrs = FILE_ATTRIBUTE_SYSTEM;
664         status = smb_raw_open(cli->tree, tctx, &io);
665         CHECK_STATUS(status, NT_STATUS_OK);
666
667         /* check timeout on create - win2003 ignores the timeout! */
668         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
669         io.t2open.in.file_attrs = 0;
670         io.t2open.in.timeout = 20000;
671         io.t2open.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_ALL;
672         status = smb_raw_open(cli->tree, tctx, &io);
673         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
674
675 done:
676         smbcli_close(cli->tree, fnum);
677         smbcli_unlink(cli->tree, fname);
678
679         return ret;
680 }
681         
682
683 /*
684   test RAW_OPEN_NTCREATEX
685 */
686 static bool test_ntcreatex(struct smbcli_state *cli, struct torture_context *tctx)
687 {
688         union smb_open io;
689         union smb_fileinfo finfo;
690         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
691         const char *dname = BASEDIR "\\torture_ntcreatex.dir";
692         NTSTATUS status;
693         int fnum = -1;
694         bool ret = true;
695         int i;
696         struct {
697                 uint32_t open_disp;
698                 bool with_file;
699                 NTSTATUS correct_status;
700         } open_funcs[] = {
701                 { NTCREATEX_DISP_SUPERSEDE,     true,  NT_STATUS_OK },
702                 { NTCREATEX_DISP_SUPERSEDE,     false, NT_STATUS_OK },
703                 { NTCREATEX_DISP_OPEN,          true,  NT_STATUS_OK },
704                 { NTCREATEX_DISP_OPEN,          false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
705                 { NTCREATEX_DISP_CREATE,        true,  NT_STATUS_OBJECT_NAME_COLLISION },
706                 { NTCREATEX_DISP_CREATE,        false, NT_STATUS_OK },
707                 { NTCREATEX_DISP_OPEN_IF,       true,  NT_STATUS_OK },
708                 { NTCREATEX_DISP_OPEN_IF,       false, NT_STATUS_OK },
709                 { NTCREATEX_DISP_OVERWRITE,     true,  NT_STATUS_OK },
710                 { NTCREATEX_DISP_OVERWRITE,     false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
711                 { NTCREATEX_DISP_OVERWRITE_IF,  true,  NT_STATUS_OK },
712                 { NTCREATEX_DISP_OVERWRITE_IF,  false, NT_STATUS_OK },
713                 { 6,                            true,  NT_STATUS_INVALID_PARAMETER },
714                 { 6,                            false, NT_STATUS_INVALID_PARAMETER },
715         };
716
717         torture_comment(tctx, "Checking RAW_OPEN_NTCREATEX\n");
718
719         /* reasonable default parameters */
720         io.generic.level = RAW_OPEN_NTCREATEX;
721         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
722         io.ntcreatex.in.root_fid.fnum = 0;
723         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
724         io.ntcreatex.in.alloc_size = 1024*1024;
725         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
726         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
727         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
728         io.ntcreatex.in.create_options = 0;
729         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
730         io.ntcreatex.in.security_flags = 0;
731         io.ntcreatex.in.fname = fname;
732
733         /* test the open disposition */
734         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
735                 if (open_funcs[i].with_file) {
736                         fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR|O_TRUNC, DENY_NONE);
737                         if (fnum == -1) {
738                                 torture_result(tctx, TORTURE_FAIL,
739                                         "Failed to create file %s - %s\n",
740                                         fname, smbcli_errstr(cli->tree));
741                                 ret = false;
742                                 goto done;
743                         }
744                         smbcli_close(cli->tree, fnum);
745                 }
746                 io.ntcreatex.in.open_disposition = open_funcs[i].open_disp;
747                 status = smb_raw_open(cli->tree, tctx, &io);
748                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
749                         torture_result(tctx, TORTURE_FAIL,
750                                 "(%s) incorrect status %s should be %s "
751                                 "(i=%d with_file=%d open_disp=%d)\n",
752                                 __location__, nt_errstr(status),
753                                 nt_errstr(open_funcs[i].correct_status),
754                                 i, (int)open_funcs[i].with_file,
755                                 (int)open_funcs[i].open_disp);
756                         ret = false;
757                 }
758                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
759                         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
760                         smbcli_unlink(cli->tree, fname);
761                 }
762         }
763
764         /* basic field testing */
765         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
766
767         status = smb_raw_open(cli->tree, tctx, &io);
768         CHECK_STATUS(status, NT_STATUS_OK);
769         fnum = io.ntcreatex.out.file.fnum;
770
771         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
772         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
773         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
774         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
775         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
776         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
777         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
778         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
779         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
780         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
781         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
782
783         /* check fields when the file already existed */
784         smbcli_close(cli->tree, fnum);
785         smbcli_unlink(cli->tree, fname);
786         fnum = create_complex_file(cli, tctx, fname);
787         if (fnum == -1) {
788                 ret = false;
789                 goto done;
790         }
791         smbcli_close(cli->tree, fnum);
792
793         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
794         status = smb_raw_open(cli->tree, tctx, &io);
795         CHECK_STATUS(status, NT_STATUS_OK);
796         fnum = io.ntcreatex.out.file.fnum;
797
798         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
799         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
800         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
801         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
802         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
803         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
804         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
805         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
806         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
807         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
808         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
809         smbcli_close(cli->tree, fnum);
810         smbcli_unlink(cli->tree, fname);
811
812
813         /* create a directory */
814         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
815         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
816         io.ntcreatex.in.alloc_size = 0;
817         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
818         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
819         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
820         io.ntcreatex.in.create_options = 0;
821         io.ntcreatex.in.fname = dname;
822         fname = dname;
823
824         smbcli_rmdir(cli->tree, fname);
825         smbcli_unlink(cli->tree, fname);
826
827         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
828         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
829         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
830         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
831         status = smb_raw_open(cli->tree, tctx, &io);
832         CHECK_STATUS(status, NT_STATUS_OK);
833         fnum = io.ntcreatex.out.file.fnum;
834
835         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
836         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
837         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
838         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
839         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
840         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
841         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
842         CHECK_VAL(io.ntcreatex.out.attrib & ~FILE_ATTRIBUTE_NONINDEXED, 
843                   FILE_ATTRIBUTE_DIRECTORY);
844         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
845         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
846         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
847         CHECK_VAL(io.ntcreatex.out.is_directory, 1);
848         CHECK_VAL(io.ntcreatex.out.size, 0);
849         CHECK_VAL(io.ntcreatex.out.alloc_size, 0);
850         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
851         smbcli_unlink(cli->tree, fname);
852         
853
854 done:
855         smbcli_close(cli->tree, fnum);
856         smbcli_unlink(cli->tree, fname);
857
858         return ret;
859 }
860
861
862 /*
863   test RAW_OPEN_NTTRANS_CREATE
864 */
865 static bool test_nttrans_create(struct smbcli_state *cli, struct torture_context *tctx)
866 {
867         union smb_open io;
868         union smb_fileinfo finfo;
869         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
870         const char *dname = BASEDIR "\\torture_ntcreatex.dir";
871         NTSTATUS status;
872         int fnum = -1;
873         bool ret = true;
874         int i;
875         uint32_t ok_mask, not_supported_mask, invalid_parameter_mask;
876         uint32_t not_a_directory_mask, unexpected_mask;
877         struct {
878                 uint32_t open_disp;
879                 bool with_file;
880                 NTSTATUS correct_status;
881         } open_funcs[] = {
882                 { NTCREATEX_DISP_SUPERSEDE,     true,  NT_STATUS_OK },
883                 { NTCREATEX_DISP_SUPERSEDE,     false, NT_STATUS_OK },
884                 { NTCREATEX_DISP_OPEN,          true,  NT_STATUS_OK },
885                 { NTCREATEX_DISP_OPEN,          false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
886                 { NTCREATEX_DISP_CREATE,        true,  NT_STATUS_OBJECT_NAME_COLLISION },
887                 { NTCREATEX_DISP_CREATE,        false, NT_STATUS_OK },
888                 { NTCREATEX_DISP_OPEN_IF,       true,  NT_STATUS_OK },
889                 { NTCREATEX_DISP_OPEN_IF,       false, NT_STATUS_OK },
890                 { NTCREATEX_DISP_OVERWRITE,     true,  NT_STATUS_OK },
891                 { NTCREATEX_DISP_OVERWRITE,     false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
892                 { NTCREATEX_DISP_OVERWRITE_IF,  true,  NT_STATUS_OK },
893                 { NTCREATEX_DISP_OVERWRITE_IF,  false, NT_STATUS_OK },
894                 { 6,                            true,  NT_STATUS_INVALID_PARAMETER },
895                 { 6,                            false, NT_STATUS_INVALID_PARAMETER },
896         };
897
898         torture_comment(tctx, "Checking RAW_OPEN_NTTRANS_CREATE\n");
899
900         /* reasonable default parameters */
901         io.generic.level = RAW_OPEN_NTTRANS_CREATE;
902         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
903         io.ntcreatex.in.root_fid.fnum = 0;
904         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
905         io.ntcreatex.in.alloc_size = 1024*1024;
906         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
907         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
908         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
909         io.ntcreatex.in.create_options = 0;
910         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
911         io.ntcreatex.in.security_flags = 0;
912         io.ntcreatex.in.fname = fname;
913         io.ntcreatex.in.sec_desc = NULL;
914         io.ntcreatex.in.ea_list = NULL;
915
916         /* test the open disposition */
917         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
918                 if (open_funcs[i].with_file) {
919                         fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR|O_TRUNC, DENY_NONE);
920                         if (fnum == -1) {
921                                 torture_result(tctx, TORTURE_FAIL,
922                                         "Failed to create file %s - %s\n",
923                                         fname, smbcli_errstr(cli->tree));
924                                 ret = false;
925                                 goto done;
926                         }
927                         smbcli_close(cli->tree, fnum);
928                 }
929                 io.ntcreatex.in.open_disposition = open_funcs[i].open_disp;
930                 status = smb_raw_open(cli->tree, tctx, &io);
931                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
932                         torture_result(tctx, TORTURE_FAIL,
933                                 "(%s) incorrect status %s should be %s "
934                                 "(i=%d with_file=%d open_disp=%d)\n",
935                                 __location__, nt_errstr(status),
936                                 nt_errstr(open_funcs[i].correct_status),
937                                 i, (int)open_funcs[i].with_file,
938                                 (int)open_funcs[i].open_disp);
939                         ret = false;
940                 }
941                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
942                         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
943                         smbcli_unlink(cli->tree, fname);
944                 }
945         }
946
947         /* basic field testing */
948         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
949
950         status = smb_raw_open(cli->tree, tctx, &io);
951         CHECK_STATUS(status, NT_STATUS_OK);
952         fnum = io.ntcreatex.out.file.fnum;
953
954         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
955         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
956         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
957         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
958         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
959         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
960         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
961         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
962         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
963         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
964         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
965
966         /* check fields when the file already existed */
967         smbcli_close(cli->tree, fnum);
968         smbcli_unlink(cli->tree, fname);
969         fnum = create_complex_file(cli, tctx, fname);
970         if (fnum == -1) {
971                 ret = false;
972                 goto done;
973         }
974         smbcli_close(cli->tree, fnum);
975
976         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
977         status = smb_raw_open(cli->tree, tctx, &io);
978         CHECK_STATUS(status, NT_STATUS_OK);
979         fnum = io.ntcreatex.out.file.fnum;
980
981         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
982         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
983         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
984         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
985         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
986         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
987         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
988         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
989         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
990         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
991         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
992         smbcli_close(cli->tree, fnum);
993
994         /* check no-recall - don't pull a file from tape on a HSM */
995         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_NO_RECALL;
996         status = smb_raw_open(cli->tree, tctx, &io);
997         CHECK_STATUS(status, NT_STATUS_OK);
998         fnum = io.ntcreatex.out.file.fnum;
999
1000         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
1001         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
1002         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
1003         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
1004         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
1005         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
1006         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
1007         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
1008         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
1009         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
1010         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
1011         smbcli_close(cli->tree, fnum);
1012
1013         /* Check some create options (these all should be ignored) */
1014         for (i=0; i < 32; i++) {
1015                 uint32_t create_option = (1 << i) & NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1016                 if (create_option == 0) {
1017                         continue;
1018                 }
1019                 io.ntcreatex.in.create_options = create_option;
1020                 status = smb_raw_open(cli->tree, tctx, &io);
1021                 if (!NT_STATUS_IS_OK(status)) {
1022                         torture_warning(tctx, "ntcreatex create option 0x%08x "
1023                                 "gave %s - should give NT_STATUS_OK\n",
1024                                 create_option, nt_errstr(status));
1025                 }
1026                 CHECK_STATUS(status, NT_STATUS_OK);
1027                 fnum = io.ntcreatex.out.file.fnum;
1028
1029                 CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
1030                 CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
1031                 CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
1032                 CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
1033                 CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
1034                 CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
1035                 CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
1036                 CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
1037                 CHECK_ALL_INFO(io.ntcreatex.out.size, size);
1038                 CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
1039                 CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
1040                 smbcli_close(cli->tree, fnum);
1041         }
1042
1043         io.ntcreatex.in.file_attr = 0;
1044         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
1045         io.ntcreatex.in.access_mask     = SEC_FLAG_MAXIMUM_ALLOWED;
1046
1047         /* Check for options that should return NOT_SUPPORTED, OK or INVALID_PARAMETER */
1048         ok_mask = 0;
1049         not_supported_mask = 0;
1050         invalid_parameter_mask = 0;
1051         not_a_directory_mask = 0;
1052         unexpected_mask = 0;
1053         for (i=0; i < 32; i++) {
1054                 uint32_t create_option = 1<<i;
1055                 if (create_option & NTCREATEX_OPTIONS_DELETE_ON_CLOSE) {
1056                         continue;
1057                 }
1058                 io.ntcreatex.in.create_options = create_option;
1059                 status = smb_raw_open(cli->tree, tctx, &io);
1060                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
1061                         not_supported_mask |= create_option;
1062                 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
1063                         ok_mask |= create_option;
1064                         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
1065                 } else if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1066                         invalid_parameter_mask |= create_option;
1067                 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_A_DIRECTORY)) {
1068                         not_a_directory_mask |= 1<<i;
1069                 } else {
1070                         unexpected_mask |= 1<<i;
1071                         torture_comment(tctx, "create option 0x%08x returned %s\n",
1072                                         create_option, nt_errstr(status));
1073                 }
1074         }
1075
1076         CHECK_VAL(ok_mask,                0x00efcfce);
1077         CHECK_VAL(not_a_directory_mask,   0x00000001);
1078         CHECK_VAL(not_supported_mask,     0x00002000);
1079         CHECK_VAL(invalid_parameter_mask, 0xff100030);
1080         CHECK_VAL(unexpected_mask,        0x00000000);
1081
1082         smbcli_unlink(cli->tree, fname);
1083
1084
1085         /* create a directory */
1086         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1087         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1088         io.ntcreatex.in.alloc_size = 0;
1089         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
1090         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
1091         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1092         io.ntcreatex.in.create_options = 0;
1093         io.ntcreatex.in.fname = dname;
1094         fname = dname;
1095
1096         smbcli_rmdir(cli->tree, fname);
1097         smbcli_unlink(cli->tree, fname);
1098
1099         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1100         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1101         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1102         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1103         status = smb_raw_open(cli->tree, tctx, &io);
1104         CHECK_STATUS(status, NT_STATUS_OK);
1105         fnum = io.ntcreatex.out.file.fnum;
1106
1107         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
1108         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
1109         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
1110         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
1111         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
1112         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
1113         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
1114         CHECK_VAL(io.ntcreatex.out.attrib & ~FILE_ATTRIBUTE_NONINDEXED, 
1115                   FILE_ATTRIBUTE_DIRECTORY);
1116         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
1117         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
1118         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
1119         CHECK_VAL(io.ntcreatex.out.is_directory, 1);
1120         CHECK_VAL(io.ntcreatex.out.size, 0);
1121         CHECK_VAL(io.ntcreatex.out.alloc_size, 0);
1122         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
1123         smbcli_unlink(cli->tree, fname);
1124         
1125
1126 done:
1127         smbcli_close(cli->tree, fnum);
1128         smbcli_unlink(cli->tree, fname);
1129
1130         return ret;
1131 }
1132
1133 /*
1134   test RAW_OPEN_NTCREATEX with an already opened and byte range locked file
1135
1136   I've got an application that does a similar sequence of ntcreate&x,
1137   locking&x and another ntcreate&x with
1138   open_disposition==NTCREATEX_DISP_OVERWRITE_IF. Windows 2003 allows the
1139   second open.
1140 */
1141 static bool test_ntcreatex_brlocked(struct smbcli_state *cli, struct torture_context *tctx)
1142 {
1143         union smb_open io, io1;
1144         union smb_lock io2;
1145         struct smb_lock_entry lock[1];
1146         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
1147         NTSTATUS status;
1148         bool ret = true;
1149
1150         torture_comment(tctx, "Testing ntcreatex with a byte range locked file\n");
1151
1152         io.generic.level = RAW_OPEN_NTCREATEX;
1153         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1154         io.ntcreatex.in.root_fid.fnum = 0;
1155         io.ntcreatex.in.access_mask = 0x2019f;
1156         io.ntcreatex.in.alloc_size = 0;
1157         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1158         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1159                 NTCREATEX_SHARE_ACCESS_WRITE;
1160         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1161         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
1162         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
1163         io.ntcreatex.in.security_flags = NTCREATEX_SECURITY_DYNAMIC |
1164                 NTCREATEX_SECURITY_ALL;
1165         io.ntcreatex.in.fname = fname;
1166
1167         status = smb_raw_open(cli->tree, tctx, &io);
1168         CHECK_STATUS(status, NT_STATUS_OK);
1169
1170         io2.lockx.level = RAW_LOCK_LOCKX;
1171         io2.lockx.in.file.fnum = io.ntcreatex.out.file.fnum;
1172         io2.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
1173         io2.lockx.in.timeout = 0;
1174         io2.lockx.in.ulock_cnt = 0;
1175         io2.lockx.in.lock_cnt = 1;
1176         lock[0].pid = cli->session->pid;
1177         lock[0].offset = 0;
1178         lock[0].count = 0x1;
1179         io2.lockx.in.locks = &lock[0];
1180         status = smb_raw_lock(cli->tree, &io2);
1181         CHECK_STATUS(status, NT_STATUS_OK);
1182
1183         io1.generic.level = RAW_OPEN_NTCREATEX;
1184         io1.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1185         io1.ntcreatex.in.root_fid.fnum = 0;
1186         io1.ntcreatex.in.access_mask = 0x20196;
1187         io1.ntcreatex.in.alloc_size = 0;
1188         io1.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1189         io1.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1190                 NTCREATEX_SHARE_ACCESS_WRITE;
1191         io1.ntcreatex.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
1192         io1.ntcreatex.in.create_options = 0;
1193         io1.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
1194         io1.ntcreatex.in.security_flags = NTCREATEX_SECURITY_DYNAMIC |
1195                 NTCREATEX_SECURITY_ALL;
1196         io1.ntcreatex.in.fname = fname;
1197
1198         status = smb_raw_open(cli->tree, tctx, &io1);
1199         CHECK_STATUS(status, NT_STATUS_OK);
1200
1201  done:
1202         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
1203         smbcli_close(cli->tree, io1.ntcreatex.out.file.fnum);
1204         smbcli_unlink(cli->tree, fname);
1205         return ret;
1206 }
1207
1208 /*
1209   test RAW_OPEN_MKNEW
1210 */
1211 static bool test_mknew(struct smbcli_state *cli, struct torture_context *tctx)
1212 {
1213         union smb_open io;
1214         const char *fname = BASEDIR "\\torture_mknew.txt";
1215         NTSTATUS status;
1216         int fnum = -1;
1217         bool ret = true;
1218         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1219         union smb_fileinfo finfo;
1220
1221         torture_comment(tctx, "Checking RAW_OPEN_MKNEW\n");
1222
1223         io.mknew.level = RAW_OPEN_MKNEW;
1224         io.mknew.in.attrib = 0;
1225         io.mknew.in.write_time = 0;
1226         io.mknew.in.fname = fname;
1227         status = smb_raw_open(cli->tree, tctx, &io);
1228         CHECK_STATUS(status, NT_STATUS_OK);
1229         fnum = io.mknew.out.file.fnum;
1230
1231         status = smb_raw_open(cli->tree, tctx, &io);
1232         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_COLLISION);
1233
1234         smbcli_close(cli->tree, fnum);
1235         smbcli_unlink(cli->tree, fname);
1236
1237         /* make sure write_time works */
1238         io.mknew.in.write_time = basetime;
1239         status = smb_raw_open(cli->tree, tctx, &io);
1240         CHECK_STATUS(status, NT_STATUS_OK);
1241         fnum = io.mknew.out.file.fnum;
1242         CHECK_TIME(basetime, write_time);
1243
1244         smbcli_close(cli->tree, fnum);
1245         smbcli_unlink(cli->tree, fname);
1246
1247         /* make sure file_attrs works */
1248         io.mknew.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1249         status = smb_raw_open(cli->tree, tctx, &io);
1250         CHECK_STATUS(status, NT_STATUS_OK);
1251         fnum = io.mknew.out.file.fnum;
1252         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE, 
1253                        attrib & ~FILE_ATTRIBUTE_NONINDEXED);
1254         
1255 done:
1256         smbcli_close(cli->tree, fnum);
1257         smbcli_unlink(cli->tree, fname);
1258
1259         return ret;
1260 }
1261
1262
1263 /*
1264   test RAW_OPEN_CREATE
1265 */
1266 static bool test_create(struct smbcli_state *cli, struct torture_context *tctx)
1267 {
1268         union smb_open io;
1269         const char *fname = BASEDIR "\\torture_create.txt";
1270         NTSTATUS status;
1271         int fnum = -1;
1272         bool ret = true;
1273         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1274         union smb_fileinfo finfo;
1275
1276         torture_comment(tctx, "Checking RAW_OPEN_CREATE\n");
1277
1278         io.create.level = RAW_OPEN_CREATE;
1279         io.create.in.attrib = 0;
1280         io.create.in.write_time = 0;
1281         io.create.in.fname = fname;
1282         status = smb_raw_open(cli->tree, tctx, &io);
1283         CHECK_STATUS(status, NT_STATUS_OK);
1284         fnum = io.create.out.file.fnum;
1285
1286         status = smb_raw_open(cli->tree, tctx, &io);
1287         CHECK_STATUS(status, NT_STATUS_OK);
1288
1289         smbcli_close(cli->tree, io.create.out.file.fnum);
1290         smbcli_close(cli->tree, fnum);
1291         smbcli_unlink(cli->tree, fname);
1292
1293         /* make sure write_time works */
1294         io.create.in.write_time = basetime;
1295         status = smb_raw_open(cli->tree, tctx, &io);
1296         CHECK_STATUS(status, NT_STATUS_OK);
1297         fnum = io.create.out.file.fnum;
1298         CHECK_TIME(basetime, write_time);
1299
1300         smbcli_close(cli->tree, fnum);
1301         smbcli_unlink(cli->tree, fname);
1302
1303         /* make sure file_attrs works */
1304         io.create.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1305         status = smb_raw_open(cli->tree, tctx, &io);
1306         CHECK_STATUS(status, NT_STATUS_OK);
1307         fnum = io.create.out.file.fnum;
1308         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE, 
1309                        attrib & ~FILE_ATTRIBUTE_NONINDEXED);
1310         
1311 done:
1312         smbcli_close(cli->tree, fnum);
1313         smbcli_unlink(cli->tree, fname);
1314
1315         return ret;
1316 }
1317
1318
1319 /*
1320   test RAW_OPEN_CTEMP
1321 */
1322 static bool test_ctemp(struct smbcli_state *cli, TALLOC_CTX *tctx)
1323 {
1324         union smb_open io;
1325         NTSTATUS status;
1326         int fnum = -1;
1327         bool ret = true;
1328         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1329         union smb_fileinfo finfo;
1330         const char *name, *fname = NULL;
1331
1332         torture_comment(tctx, "Checking RAW_OPEN_CTEMP\n");
1333
1334         io.ctemp.level = RAW_OPEN_CTEMP;
1335         io.ctemp.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1336         io.ctemp.in.write_time = basetime;
1337         io.ctemp.in.directory = BASEDIR;
1338         status = smb_raw_open(cli->tree, tctx, &io);
1339         CHECK_STATUS(status, NT_STATUS_OK);
1340         fnum = io.ctemp.out.file.fnum;
1341
1342         name = io.ctemp.out.name;
1343
1344         finfo.generic.level = RAW_FILEINFO_NAME_INFO;
1345         finfo.generic.in.file.fnum = fnum;
1346         status = smb_raw_fileinfo(cli->tree, tctx, &finfo);
1347         CHECK_STATUS(status, NT_STATUS_OK);
1348
1349         fname = finfo.name_info.out.fname.s;
1350         torture_comment(tctx, "ctemp name=%s  real name=%s\n", name, fname);
1351
1352 done:
1353         smbcli_close(cli->tree, fnum);
1354         if (fname) {
1355                 smbcli_unlink(cli->tree, fname);
1356         }
1357
1358         return ret;
1359 }
1360
1361
1362 /*
1363   test chained RAW_OPEN_OPENX_READX
1364 */
1365 static bool test_chained(struct smbcli_state *cli, TALLOC_CTX *tctx)
1366 {
1367         union smb_open io;
1368         const char *fname = BASEDIR "\\torture_chained.txt";
1369         NTSTATUS status;
1370         int fnum = -1;
1371         bool ret = true;
1372         const char *buf = "test";
1373         char buf2[4];
1374
1375         torture_comment(tctx, "Checking RAW_OPEN_OPENX chained with READX\n");
1376         smbcli_unlink(cli->tree, fname);
1377
1378         fnum = create_complex_file(cli, tctx, fname);
1379
1380         smbcli_write(cli->tree, fnum, 0, buf, 0, sizeof(buf));
1381
1382         smbcli_close(cli->tree, fnum);  
1383
1384         io.openxreadx.level = RAW_OPEN_OPENX_READX;
1385         io.openxreadx.in.fname = fname;
1386         io.openxreadx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
1387         io.openxreadx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
1388         io.openxreadx.in.open_func = OPENX_OPEN_FUNC_OPEN;
1389         io.openxreadx.in.search_attrs = 0;
1390         io.openxreadx.in.file_attrs = 0;
1391         io.openxreadx.in.write_time = 0;
1392         io.openxreadx.in.size = 1024*1024;
1393         io.openxreadx.in.timeout = 0;
1394         
1395         io.openxreadx.in.offset = 0;
1396         io.openxreadx.in.mincnt = sizeof(buf);
1397         io.openxreadx.in.maxcnt = sizeof(buf);
1398         io.openxreadx.in.remaining = 0;
1399         io.openxreadx.out.data = (uint8_t *)buf2;
1400
1401         status = smb_raw_open(cli->tree, tctx, &io);
1402         CHECK_STATUS(status, NT_STATUS_OK);
1403         fnum = io.openxreadx.out.file.fnum;
1404
1405         if (memcmp(buf, buf2, sizeof(buf)) != 0) {
1406                 torture_result(tctx, TORTURE_FAIL,
1407                         "wrong data in reply buffer\n");
1408                 ret = false;
1409         }
1410
1411 done:
1412         smbcli_close(cli->tree, fnum);
1413         smbcli_unlink(cli->tree, fname);
1414
1415         return ret;
1416 }
1417
1418 /*
1419   test RAW_OPEN_OPENX without a leading slash on the path.
1420   NetApp filers are known to fail on this.
1421   
1422 */
1423 static bool test_no_leading_slash(struct smbcli_state *cli, TALLOC_CTX *tctx)
1424 {
1425         union smb_open io;
1426         const char *fname = BASEDIR "\\torture_no_leading_slash.txt";
1427         NTSTATUS status;
1428         int fnum = -1;
1429         bool ret = true;
1430         const char *buf = "test";
1431
1432         torture_comment(tctx, "Checking RAW_OPEN_OPENX without leading "
1433                         "slash on path\n");
1434         smbcli_unlink(cli->tree, fname);
1435
1436         /* Create the file */
1437         fnum = create_complex_file(cli, tctx, fname);
1438         smbcli_write(cli->tree, fnum, 0, buf, 0, sizeof(buf));
1439         smbcli_close(cli->tree, fnum);  
1440
1441         /* Prepare to open the file using path without leading slash */
1442         io.openx.level = RAW_OPEN_OPENX;
1443         io.openx.in.fname = fname + 1;
1444         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
1445         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
1446         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
1447         io.openx.in.search_attrs = 0;
1448         io.openx.in.file_attrs = 0;
1449         io.openx.in.write_time = 0;
1450         io.openx.in.size = 1024*1024;
1451         io.openx.in.timeout = 0;
1452
1453         status = smb_raw_open(cli->tree, tctx, &io);
1454         CHECK_STATUS(status, NT_STATUS_OK);
1455         fnum = io.openx.out.file.fnum;
1456
1457 done:
1458         smbcli_close(cli->tree, fnum);
1459         smbcli_unlink(cli->tree, fname);
1460
1461         return ret;
1462 }
1463
1464 /*
1465   test RAW_OPEN_OPENX against an existing directory to
1466   ensure it returns NT_STATUS_FILE_IS_A_DIRECTORY.
1467   Samba 3.2.0 - 3.2.6 are known to fail this.
1468   
1469 */
1470 static bool test_openx_over_dir(struct smbcli_state *cli, TALLOC_CTX *tctx)
1471 {
1472         union smb_open io;
1473         const char *fname = BASEDIR "\\openx_over_dir";
1474         NTSTATUS status;
1475         int d_fnum = -1;
1476         int fnum = -1;
1477         bool ret = true;
1478
1479         torture_comment(tctx, "Checking RAW_OPEN_OPENX over an existing directory\n");
1480         smbcli_unlink(cli->tree, fname);
1481
1482         /* Create the Directory */
1483         status = create_directory_handle(cli->tree, fname, &d_fnum);
1484         smbcli_close(cli->tree, d_fnum);        
1485
1486         /* Prepare to open the file over the directory. */
1487         io.openx.level = RAW_OPEN_OPENX;
1488         io.openx.in.fname = fname;
1489         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
1490         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
1491         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
1492         io.openx.in.search_attrs = 0;
1493         io.openx.in.file_attrs = 0;
1494         io.openx.in.write_time = 0;
1495         io.openx.in.size = 1024*1024;
1496         io.openx.in.timeout = 0;
1497
1498         status = smb_raw_open(cli->tree, tctx, &io);
1499         CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
1500         fnum = io.openx.out.file.fnum;
1501
1502 done:
1503         smbcli_close(cli->tree, fnum);
1504         smbcli_unlink(cli->tree, fname);
1505
1506         return ret;
1507 }
1508
1509
1510 /* A little torture test to expose a race condition in Samba 3.0.20 ... :-) */
1511
1512 static bool test_raw_open_multi(struct torture_context *tctx)
1513 {
1514         struct smbcli_state *cli;
1515         TALLOC_CTX *mem_ctx = talloc_init("torture_test_oplock_multi");
1516         const char *fname = "\\test_oplock.dat";
1517         NTSTATUS status;
1518         bool ret = true;
1519         union smb_open io;
1520         struct smbcli_state **clients;
1521         struct smbcli_request **requests;
1522         union smb_open *ios;
1523         const char *host = torture_setting_string(tctx, "host", NULL);
1524         const char *share = torture_setting_string(tctx, "share", NULL);
1525         int i, num_files = 3;
1526         int num_ok = 0;
1527         int num_collision = 0;
1528         
1529         clients = talloc_array(mem_ctx, struct smbcli_state *, num_files);
1530         requests = talloc_array(mem_ctx, struct smbcli_request *, num_files);
1531         ios = talloc_array(mem_ctx, union smb_open, num_files);
1532         if ((tctx->ev == NULL) || (clients == NULL) || (requests == NULL) ||
1533             (ios == NULL)) {
1534                 torture_result(tctx, TORTURE_FAIL, "(%s): talloc failed\n",
1535                                 __location__);
1536                 return false;
1537         }
1538
1539         if (!torture_open_connection_share(mem_ctx, &cli, tctx, host, share, tctx->ev)) {
1540                 return false;
1541         }
1542
1543         cli->tree->session->transport->options.request_timeout = 60;
1544
1545         for (i=0; i<num_files; i++) {
1546                 if (!torture_open_connection_share(mem_ctx, &(clients[i]),
1547                                                    tctx, host, share, tctx->ev)) {
1548                         torture_result(tctx, TORTURE_FAIL,
1549                                        "(%s): Could not open %d'th connection\n",
1550                                        __location__, i);
1551                         return false;
1552                 }
1553                 clients[i]->tree->session->transport->options.request_timeout = 60;
1554         }
1555
1556         /* cleanup */
1557         smbcli_unlink(cli->tree, fname);
1558
1559         /*
1560           base ntcreatex parms
1561         */
1562         io.generic.level = RAW_OPEN_NTCREATEX;
1563         io.ntcreatex.in.root_fid.fnum = 0;
1564         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1565         io.ntcreatex.in.alloc_size = 0;
1566         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1567         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
1568                 NTCREATEX_SHARE_ACCESS_WRITE|
1569                 NTCREATEX_SHARE_ACCESS_DELETE;
1570         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1571         io.ntcreatex.in.create_options = 0;
1572         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1573         io.ntcreatex.in.security_flags = 0;
1574         io.ntcreatex.in.fname = fname;
1575         io.ntcreatex.in.flags = 0;
1576
1577         for (i=0; i<num_files; i++) {
1578                 ios[i] = io;
1579                 requests[i] = smb_raw_open_send(clients[i]->tree, &ios[i]);
1580                 if (requests[i] == NULL) {
1581                         torture_result(tctx, TORTURE_FAIL,
1582                                 "(%s): could not send %d'th request\n",
1583                                 __location__, i);
1584                         return false;
1585                 }
1586         }
1587
1588         torture_comment(tctx, "waiting for replies\n");
1589         while (1) {
1590                 bool unreplied = false;
1591                 for (i=0; i<num_files; i++) {
1592                         if (requests[i] == NULL) {
1593                                 continue;
1594                         }
1595                         if (requests[i]->state < SMBCLI_REQUEST_DONE) {
1596                                 unreplied = true;
1597                                 break;
1598                         }
1599                         status = smb_raw_open_recv(requests[i], mem_ctx,
1600                                                    &ios[i]);
1601
1602                         torture_comment(tctx, "File %d returned status %s\n", i,
1603                                   nt_errstr(status));
1604
1605                         if (NT_STATUS_IS_OK(status)) {
1606                                 num_ok += 1;
1607                         } 
1608
1609                         if (NT_STATUS_EQUAL(status,
1610                                             NT_STATUS_OBJECT_NAME_COLLISION)) {
1611                                 num_collision += 1;
1612                         }
1613
1614                         requests[i] = NULL;
1615                 }
1616                 if (!unreplied) {
1617                         break;
1618                 }
1619
1620                 if (event_loop_once(tctx->ev) != 0) {
1621                         torture_result(tctx, TORTURE_FAIL,
1622                                 "(%s): event_loop_once failed\n", __location__);
1623                         return false;
1624                 }
1625         }
1626
1627         if ((num_ok != 1) || (num_ok + num_collision != num_files)) {
1628                 ret = false;
1629         }
1630
1631         for (i=0; i<num_files; i++) {
1632                 torture_close_connection(clients[i]);
1633         }
1634         talloc_free(mem_ctx);
1635         return ret;
1636 }
1637
1638 /*
1639   test opening for delete on a read-only attribute file.
1640 */
1641 static bool test_open_for_delete(struct smbcli_state *cli, struct torture_context *tctx)
1642 {
1643         union smb_open io;
1644         union smb_fileinfo finfo;
1645         const char *fname = BASEDIR "\\torture_open_for_delete.txt";
1646         NTSTATUS status;
1647         int fnum = -1;
1648         bool ret = true;
1649
1650         torture_comment(tctx, "Checking RAW_NTCREATEX for delete on a readonly file.\n");
1651
1652         /* reasonable default parameters */
1653         io.generic.level = RAW_OPEN_NTCREATEX;
1654         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1655         io.ntcreatex.in.root_fid.fnum = 0;
1656         io.ntcreatex.in.alloc_size = 0;
1657         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1658         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_READONLY;
1659         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
1660         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1661         io.ntcreatex.in.create_options = 0;
1662         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1663         io.ntcreatex.in.security_flags = 0;
1664         io.ntcreatex.in.fname = fname;
1665
1666         /* Create the readonly file. */
1667
1668         status = smb_raw_open(cli->tree, tctx, &io);
1669         CHECK_STATUS(status, NT_STATUS_OK);
1670         fnum = io.ntcreatex.out.file.fnum;
1671
1672         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
1673         io.ntcreatex.in.create_options = 0;
1674         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
1675         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
1676         smbcli_close(cli->tree, fnum);
1677
1678         /* Now try and open for delete only - should succeed. */
1679         io.ntcreatex.in.access_mask = SEC_STD_DELETE;
1680         io.ntcreatex.in.file_attr = 0;
1681         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE | NTCREATEX_SHARE_ACCESS_DELETE;
1682         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1683         status = smb_raw_open(cli->tree, tctx, &io);
1684         CHECK_STATUS(status, NT_STATUS_OK);
1685
1686         smbcli_unlink(cli->tree, fname);
1687
1688 done:
1689         smbcli_close(cli->tree, fnum);
1690         smbcli_unlink(cli->tree, fname);
1691
1692         return ret;
1693 }
1694
1695 /*
1696   test chained RAW_OPEN_NTCREATEX_READX
1697   Send chained NTCREATEX_READX on a file that doesn't exist, then create
1698   the file and try again.
1699 */
1700 static bool test_chained_ntcreatex_readx(struct smbcli_state *cli, struct torture_context *tctx)
1701 {
1702         TALLOC_CTX *mem_ctx = talloc_new(tctx);
1703         union smb_open io;
1704         const char *fname = BASEDIR "\\torture_chained.txt";
1705         NTSTATUS status;
1706         int fnum = -1;
1707         bool ret = true;
1708         const char *buf = "test";
1709         char buf2[4];
1710
1711         torture_comment(tctx, "Checking RAW_NTCREATEX_READX chained on "
1712                               "non-existant file \n");
1713         smbcli_unlink(cli->tree, fname);
1714
1715         /* ntcreatex parameters */
1716         io.generic.level = RAW_OPEN_NTCREATEX_READX;
1717         io.ntcreatexreadx.in.flags = 0;
1718         io.ntcreatexreadx.in.root_fid.fnum = 0;
1719         io.ntcreatexreadx.in.access_mask = SEC_FILE_READ_DATA;
1720         io.ntcreatexreadx.in.alloc_size = 0;
1721         io.ntcreatexreadx.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1722         io.ntcreatexreadx.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1723                 NTCREATEX_SHARE_ACCESS_WRITE | NTCREATEX_SHARE_ACCESS_DELETE;
1724         io.ntcreatexreadx.in.open_disposition = NTCREATEX_DISP_OPEN;
1725         io.ntcreatexreadx.in.create_options = 0;
1726         io.ntcreatexreadx.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
1727         io.ntcreatexreadx.in.security_flags = 0;
1728         io.ntcreatexreadx.in.fname = fname;
1729
1730         /* readx parameters */
1731         io.ntcreatexreadx.in.offset = 0;
1732         io.ntcreatexreadx.in.mincnt = sizeof(buf);
1733         io.ntcreatexreadx.in.maxcnt = sizeof(buf);
1734         io.ntcreatexreadx.in.remaining = 0;
1735         io.ntcreatexreadx.out.data = (uint8_t *)buf2;
1736
1737         /* try to open the non-existant file */
1738         status = smb_raw_open(cli->tree, mem_ctx, &io);
1739         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1740         fnum = io.ntcreatexreadx.out.file.fnum;
1741
1742         smbcli_close(cli->tree, fnum);
1743         smbcli_unlink(cli->tree, fname);
1744
1745         torture_comment(tctx, "Checking RAW_NTCREATEX_READX chained on "
1746                               "existing file \n");
1747
1748         fnum = create_complex_file(cli, mem_ctx, fname);
1749         smbcli_write(cli->tree, fnum, 0, buf, 0, sizeof(buf));
1750         smbcli_close(cli->tree, fnum);
1751
1752         status = smb_raw_open(cli->tree, mem_ctx, &io);
1753         CHECK_STATUS(status, NT_STATUS_OK);
1754         fnum = io.ntcreatexreadx.out.file.fnum;
1755
1756         if (memcmp(buf, buf2, sizeof(buf)) != 0) {
1757                 torture_result(tctx, TORTURE_FAIL,
1758                         "(%s): wrong data in reply buffer\n", __location__);
1759                 ret = false;
1760         }
1761
1762 done:
1763         smbcli_close(cli->tree, fnum);
1764         smbcli_unlink(cli->tree, fname);
1765         talloc_free(mem_ctx);
1766
1767         return ret;
1768 }
1769
1770 #define FILL_NTCREATEX(_struct, _init...)                       \
1771         do {                                                    \
1772                 (_struct)->generic.level = RAW_OPEN_NTCREATEX;  \
1773                 (_struct)->ntcreatex.in                         \
1774                     = (typeof((_struct)->ntcreatex.in)) {_init};\
1775         } while (0)
1776
1777 static bool test_ntcreatex_opendisp_dir(struct smbcli_state *cli,
1778                                         struct torture_context *tctx)
1779 {
1780         union smb_open io;
1781         const char *dname = BASEDIR "\\torture_ntcreatex_opendisp_dir";
1782         NTSTATUS status;
1783         bool ret = true;
1784         int i;
1785         struct {
1786                 uint32_t open_disp;
1787                 bool dir_exists;
1788                 NTSTATUS correct_status;
1789         } open_funcs_dir[] = {
1790                 { NTCREATEX_DISP_SUPERSEDE,     true,  NT_STATUS_INVALID_PARAMETER },
1791                 { NTCREATEX_DISP_SUPERSEDE,     false, NT_STATUS_INVALID_PARAMETER },
1792                 { NTCREATEX_DISP_OPEN,          true,  NT_STATUS_OK },
1793                 { NTCREATEX_DISP_OPEN,          false, NT_STATUS_OBJECT_NAME_NOT_FOUND },
1794                 { NTCREATEX_DISP_CREATE,        true,  NT_STATUS_OBJECT_NAME_COLLISION },
1795                 { NTCREATEX_DISP_CREATE,        false, NT_STATUS_OK },
1796                 { NTCREATEX_DISP_OPEN_IF,       true,  NT_STATUS_OK },
1797                 { NTCREATEX_DISP_OPEN_IF,       false, NT_STATUS_OK },
1798                 { NTCREATEX_DISP_OVERWRITE,     true,  NT_STATUS_INVALID_PARAMETER },
1799                 { NTCREATEX_DISP_OVERWRITE,     false, NT_STATUS_INVALID_PARAMETER },
1800                 { NTCREATEX_DISP_OVERWRITE_IF,  true,  NT_STATUS_INVALID_PARAMETER },
1801                 { NTCREATEX_DISP_OVERWRITE_IF,  false, NT_STATUS_INVALID_PARAMETER },
1802                 { 6,                            true,  NT_STATUS_INVALID_PARAMETER },
1803                 { 6,                            false, NT_STATUS_INVALID_PARAMETER },
1804         };
1805
1806         if (!torture_setup_dir(cli, BASEDIR)) {
1807                 return false;
1808         }
1809
1810         FILL_NTCREATEX(&io,
1811             .flags = NTCREATEX_FLAGS_EXTENDED,
1812             .access_mask = SEC_FLAG_MAXIMUM_ALLOWED,
1813             .file_attr = FILE_ATTRIBUTE_DIRECTORY,
1814             .share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE,
1815             .create_options = NTCREATEX_OPTIONS_DIRECTORY,
1816             .fname = dname,
1817         );
1818
1819         smbcli_rmdir(cli->tree, dname);
1820         smbcli_unlink(cli->tree, dname);
1821
1822         /* test the open disposition for directories */
1823         torture_comment(tctx, "Testing open dispositions for directories...\n");
1824
1825         for (i=0; i<ARRAY_SIZE(open_funcs_dir); i++) {
1826                 if (open_funcs_dir[i].dir_exists) {
1827                         status = smbcli_mkdir(cli->tree, dname);
1828                         if (!NT_STATUS_IS_OK(status)) {
1829                                 torture_result(tctx, TORTURE_FAIL,
1830                                         "(%s): Failed to make directory "
1831                                         "%s - %s\n", __location__, dname,
1832                                         smbcli_errstr(cli->tree));
1833                                 ret = false;
1834                                 goto done;
1835                         }
1836                 }
1837
1838                 io.ntcreatex.in.open_disposition = open_funcs_dir[i].open_disp;
1839                 status = smb_raw_open(cli->tree, tctx, &io);
1840                 if (!NT_STATUS_EQUAL(status, open_funcs_dir[i].correct_status)) {
1841                         torture_result(tctx, TORTURE_FAIL,
1842                                 "(%s) incorrect status %s should be %s "
1843                                 "(i=%d dir_exists=%d open_disp=%d)\n",
1844                                 __location__, nt_errstr(status),
1845                                 nt_errstr(open_funcs_dir[i].correct_status),
1846                                 i, (int)open_funcs_dir[i].dir_exists,
1847                                 (int)open_funcs_dir[i].open_disp);
1848                         ret = false;
1849                 }
1850                 if (NT_STATUS_IS_OK(status) || open_funcs_dir[i].dir_exists) {
1851                         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
1852                         smbcli_rmdir(cli->tree, dname);
1853                 }
1854         }
1855
1856 done:
1857         smbcli_rmdir(cli->tree, dname);
1858         smbcli_deltree(cli->tree, BASEDIR);
1859         return ret;
1860 }
1861
1862
1863 /* basic testing of all RAW_OPEN_* calls
1864 */
1865 bool torture_raw_open(struct torture_context *torture, struct smbcli_state *cli)
1866 {
1867         bool ret = true;
1868
1869         if (!torture_setup_dir(cli, BASEDIR)) {
1870                 return false;
1871         }
1872
1873         ret &= test_ntcreatex_brlocked(cli, torture);
1874         ret &= test_open(cli, torture);
1875         ret &= test_raw_open_multi(torture);
1876         ret &= test_openx(cli, torture);
1877         ret &= test_ntcreatex(cli, torture);
1878         ret &= test_nttrans_create(cli, torture);
1879         ret &= test_t2open(cli, torture);
1880         ret &= test_mknew(cli, torture);
1881         ret &= test_create(cli, torture);
1882         ret &= test_ctemp(cli, torture);
1883         ret &= test_chained(cli, torture);
1884         ret &= test_chained_ntcreatex_readx(cli, torture);
1885         ret &= test_no_leading_slash(cli, torture);
1886         ret &= test_openx_over_dir(cli, torture);
1887         ret &= test_open_for_delete(cli, torture);
1888         ret &= test_ntcreatex_opendisp_dir(cli, torture);
1889
1890         smb_raw_exit(cli->session);
1891         smbcli_deltree(cli->tree, BASEDIR);
1892
1893         return ret;
1894 }