r3821: added client side code and test code for NTTRANS_CREATE
[kai/samba.git] / source / 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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "system/time.h"
24
25 /* enum for whether reads/writes are possible on a file */
26 enum rdwr_mode {RDWR_NONE, RDWR_RDONLY, RDWR_WRONLY, RDWR_RDWR};
27
28 #define BASEDIR "\\rawopen"
29
30 /*
31   check if a open file can be read/written
32 */
33 static enum rdwr_mode check_rdwr(struct smbcli_tree *tree, int fnum)
34 {
35         char c = 1;
36         BOOL can_read  = (smbcli_read(tree, fnum, &c, 0, 1) == 1);
37         BOOL can_write = (smbcli_write(tree, fnum, 0, &c, 0, 1) == 1);
38         if ( can_read &&  can_write) return RDWR_RDWR;
39         if ( can_read && !can_write) return RDWR_RDONLY;
40         if (!can_read &&  can_write) return RDWR_WRONLY;
41         return RDWR_NONE;
42 }
43
44 /*
45   describe a RDWR mode as a string
46 */
47 static const char *rdwr_string(enum rdwr_mode m)
48 {
49         switch (m) {
50         case RDWR_NONE: return "NONE";
51         case RDWR_RDONLY: return "RDONLY";
52         case RDWR_WRONLY: return "WRONLY";
53         case RDWR_RDWR: return "RDWR";
54         }
55         return "-";
56 }
57
58 #define CHECK_STATUS(status, correct) do { \
59         if (!NT_STATUS_EQUAL(status, correct)) { \
60                 printf("(%s) Incorrect status %s - should be %s\n", \
61                        __location__, nt_errstr(status), nt_errstr(correct)); \
62                 ret = False; \
63                 goto done; \
64         }} while (0)
65
66 #define CREATE_FILE do { \
67         fnum = create_complex_file(cli, mem_ctx, fname); \
68         if (fnum == -1) { \
69                 printf("(%s) Failed to create %s - %s\n", __location__, fname, smbcli_errstr(cli->tree)); \
70                 ret = False; \
71                 goto done; \
72         }} while (0)
73
74 #define CHECK_RDWR(fnum, correct) do { \
75         enum rdwr_mode m = check_rdwr(cli->tree, fnum); \
76         if (m != correct) { \
77                 printf("(%s) Incorrect readwrite mode %s - expected %s\n", \
78                        __location__, rdwr_string(m), rdwr_string(correct)); \
79                 ret = False; \
80         }} while (0)
81
82 #define CHECK_TIME(t, field) do { \
83         time_t t1, t2; \
84         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
85         finfo.all_info.in.fname = fname; \
86         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo); \
87         CHECK_STATUS(status, NT_STATUS_OK); \
88         t1 = t & ~1; \
89         t2 = nt_time_to_unix(finfo.all_info.out.field) & ~1; \
90         if (ABS(t1-t2) > 2) { \
91                 printf("(%s) wrong time for field %s  %s - %s\n", \
92                        __location__, #field, \
93                        timestring(mem_ctx, t1), \
94                        timestring(mem_ctx, t2)); \
95                 dump_all_info(mem_ctx, &finfo); \
96                 ret = False; \
97         }} while (0)
98
99 #define CHECK_NTTIME(t, field) do { \
100         NTTIME t2; \
101         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
102         finfo.all_info.in.fname = fname; \
103         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo); \
104         CHECK_STATUS(status, NT_STATUS_OK); \
105         t2 = finfo.all_info.out.field; \
106         if (t != t2) { \
107                 printf("(%s) wrong time for field %s  %s - %s\n", \
108                        __location__, #field, \
109                        nt_time_string(mem_ctx, t), \
110                        nt_time_string(mem_ctx, t2)); \
111                 dump_all_info(mem_ctx, &finfo); \
112                 ret = False; \
113         }} while (0)
114
115 #define CHECK_ALL_INFO(v, field) do { \
116         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
117         finfo.all_info.in.fname = fname; \
118         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo); \
119         CHECK_STATUS(status, NT_STATUS_OK); \
120         if ((v) != (finfo.all_info.out.field)) { \
121                 printf("(%s) wrong value for field %s  0x%x - 0x%x\n", \
122                        __location__, #field, (int)v, (int)(finfo.all_info.out.field)); \
123                 dump_all_info(mem_ctx, &finfo); \
124                 ret = False; \
125         }} while (0)
126
127 #define CHECK_VAL(v, correct) do { \
128         if ((v) != (correct)) { \
129                 printf("(%s) wrong value for %s  0x%x - should be 0x%x\n", \
130                        __location__, #v, (int)(v), (int)correct); \
131                 ret = False; \
132         }} while (0)
133
134 #define SET_ATTRIB(sattrib) do { \
135         union smb_setfileinfo sfinfo; \
136         sfinfo.generic.level = RAW_SFILEINFO_BASIC_INFORMATION; \
137         sfinfo.generic.file.fname = fname; \
138         ZERO_STRUCT(sfinfo.basic_info.in); \
139         sfinfo.basic_info.in.attrib = sattrib; \
140         status = smb_raw_setpathinfo(cli->tree, &sfinfo); \
141         if (!NT_STATUS_IS_OK(status)) { \
142                 printf("(%s) Failed to set attrib 0x%x on %s\n", \
143                        __location__, sattrib, fname); \
144         }} while (0)
145
146 /*
147   test RAW_OPEN_OPEN
148 */
149 static BOOL test_open(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
150 {
151         union smb_open io;
152         union smb_fileinfo finfo;
153         const char *fname = BASEDIR "\\torture_open.txt";
154         NTSTATUS status;
155         int fnum = -1, fnum2;
156         BOOL ret = True;
157
158         printf("Checking RAW_OPEN_OPEN\n");
159
160         io.openold.level = RAW_OPEN_OPEN;
161         io.openold.in.fname = fname;
162         io.openold.in.flags = OPEN_FLAGS_FCB;
163         io.openold.in.search_attrs = 0;
164         status = smb_raw_open(cli->tree, mem_ctx, &io);
165         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
166         fnum = io.openold.out.fnum;
167
168         smbcli_unlink(cli->tree, fname);
169         CREATE_FILE;
170         smbcli_close(cli->tree, fnum);
171
172         status = smb_raw_open(cli->tree, mem_ctx, &io);
173         CHECK_STATUS(status, NT_STATUS_OK);
174         fnum = io.openold.out.fnum;
175         CHECK_RDWR(fnum, RDWR_RDWR);
176
177         status = smb_raw_open(cli->tree, mem_ctx, &io);
178         CHECK_STATUS(status, NT_STATUS_OK);
179         fnum2 = io.openold.out.fnum;
180         CHECK_RDWR(fnum2, RDWR_RDWR);
181         smbcli_close(cli->tree, fnum2);
182         smbcli_close(cli->tree, fnum);
183
184         /* check the read/write modes */
185         io.openold.level = RAW_OPEN_OPEN;
186         io.openold.in.fname = fname;
187         io.openold.in.search_attrs = 0;
188
189         io.openold.in.flags = OPEN_FLAGS_OPEN_READ;
190         status = smb_raw_open(cli->tree, mem_ctx, &io);
191         CHECK_STATUS(status, NT_STATUS_OK);
192         fnum = io.openold.out.fnum;
193         CHECK_RDWR(fnum, RDWR_RDONLY);
194         smbcli_close(cli->tree, fnum);
195
196         io.openold.in.flags = OPEN_FLAGS_OPEN_WRITE;
197         status = smb_raw_open(cli->tree, mem_ctx, &io);
198         CHECK_STATUS(status, NT_STATUS_OK);
199         fnum = io.openold.out.fnum;
200         CHECK_RDWR(fnum, RDWR_WRONLY);
201         smbcli_close(cli->tree, fnum);
202
203         io.openold.in.flags = OPEN_FLAGS_OPEN_RDWR;
204         status = smb_raw_open(cli->tree, mem_ctx, &io);
205         CHECK_STATUS(status, NT_STATUS_OK);
206         fnum = io.openold.out.fnum;
207         CHECK_RDWR(fnum, RDWR_RDWR);
208         smbcli_close(cli->tree, fnum);
209
210         /* check the share modes roughly - not a complete matrix */
211         io.openold.in.flags = OPEN_FLAGS_OPEN_RDWR | OPEN_FLAGS_DENY_WRITE;
212         status = smb_raw_open(cli->tree, mem_ctx, &io);
213         CHECK_STATUS(status, NT_STATUS_OK);
214         fnum = io.openold.out.fnum;
215         CHECK_RDWR(fnum, RDWR_RDWR);
216         
217         if (io.openold.in.flags != io.openold.out.rmode) {
218                 printf("(%s) rmode should equal flags - 0x%x 0x%x\n",
219                        __location__, io.openold.out.rmode, io.openold.in.flags);
220         }
221
222         io.openold.in.flags = OPEN_FLAGS_OPEN_RDWR | OPEN_FLAGS_DENY_NONE;
223         status = smb_raw_open(cli->tree, mem_ctx, &io);
224         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
225
226         io.openold.in.flags = OPEN_FLAGS_OPEN_READ | OPEN_FLAGS_DENY_NONE;
227         status = smb_raw_open(cli->tree, mem_ctx, &io);
228         CHECK_STATUS(status, NT_STATUS_OK);
229         fnum2 = io.openold.out.fnum;
230         CHECK_RDWR(fnum2, RDWR_RDONLY);
231         smbcli_close(cli->tree, fnum);
232         smbcli_close(cli->tree, fnum2);
233
234
235         /* check the returned write time */
236         io.openold.level = RAW_OPEN_OPEN;
237         io.openold.in.fname = fname;
238         io.openold.in.search_attrs = 0;
239         io.openold.in.flags = OPEN_FLAGS_OPEN_READ;
240         status = smb_raw_open(cli->tree, mem_ctx, &io);
241         CHECK_STATUS(status, NT_STATUS_OK);
242         fnum = io.openold.out.fnum;
243
244         /* check other reply fields */
245         CHECK_TIME(io.openold.out.write_time, write_time);
246         CHECK_ALL_INFO(io.openold.out.size, size);
247         CHECK_ALL_INFO(io.openold.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
248
249 done:
250         smbcli_close(cli->tree, fnum);
251         smbcli_unlink(cli->tree, fname);
252
253         return ret;
254 }
255
256
257 /*
258   test RAW_OPEN_OPENX
259 */
260 static BOOL test_openx(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
261 {
262         union smb_open io;
263         union smb_fileinfo finfo;
264         const char *fname = BASEDIR "\\torture_openx.txt";
265         NTSTATUS status;
266         int fnum = -1, fnum2;
267         BOOL ret = True;
268         int i;
269         struct timeval tv;
270         struct {
271                 uint16_t open_func;
272                 BOOL with_file;
273                 NTSTATUS correct_status;
274         } open_funcs[] = {
275                 { OPENX_OPEN_FUNC_OPEN,                           True,  NT_STATUS_OK },
276                 { OPENX_OPEN_FUNC_OPEN,                           False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
277                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OK },
278                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OK },
279                 { OPENX_OPEN_FUNC_FAIL,                           True,  NT_STATUS_INVALID_LOCK_SEQUENCE },
280                 { OPENX_OPEN_FUNC_FAIL,                           False, NT_STATUS_INVALID_LOCK_SEQUENCE },
281                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OBJECT_NAME_COLLISION },
282                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OK },
283                 { OPENX_OPEN_FUNC_TRUNC,                          True,  NT_STATUS_OK },
284                 { OPENX_OPEN_FUNC_TRUNC,                          False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
285                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OK },
286                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_OK },
287         };
288
289         printf("Checking RAW_OPEN_OPENX\n");
290         smbcli_unlink(cli->tree, fname);
291
292         io.openx.level = RAW_OPEN_OPENX;
293         io.openx.in.fname = fname;
294         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
295         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
296         io.openx.in.search_attrs = 0;
297         io.openx.in.file_attrs = 0;
298         io.openx.in.write_time = 0;
299         io.openx.in.size = 1024*1024;
300         io.openx.in.timeout = 0;
301
302         /* check all combinations of open_func */
303         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
304                 if (open_funcs[i].with_file) {
305                         fnum = create_complex_file(cli, mem_ctx, fname);
306                         if (fnum == -1) {
307                                 d_printf("Failed to create file %s - %s\n", fname, smbcli_errstr(cli->tree));
308                                 ret = False;
309                                 goto done;
310                         }
311                         smbcli_close(cli->tree, fnum);
312                 }
313                 io.openx.in.open_func = open_funcs[i].open_func;
314                 status = smb_raw_open(cli->tree, mem_ctx, &io);
315                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
316                         printf("(%s) incorrect status %s should be %s (i=%d with_file=%d open_func=0x%x)\n", 
317                                __location__, nt_errstr(status), nt_errstr(open_funcs[i].correct_status),
318                                i, (int)open_funcs[i].with_file, (int)open_funcs[i].open_func);
319                         ret = False;
320                 }
321                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
322                         smbcli_close(cli->tree, io.openx.out.fnum);
323                         smbcli_unlink(cli->tree, fname);
324                 }
325         }
326
327         /* check the basic return fields */
328         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
329         status = smb_raw_open(cli->tree, mem_ctx, &io);
330         CHECK_STATUS(status, NT_STATUS_OK);
331         fnum = io.openx.out.fnum;
332
333         CHECK_ALL_INFO(io.openx.out.size, size);
334         CHECK_TIME(io.openx.out.write_time, write_time);
335         CHECK_ALL_INFO(io.openx.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
336         CHECK_VAL(io.openx.out.access, OPENX_MODE_ACCESS_RDWR);
337         CHECK_VAL(io.openx.out.ftype, 0);
338         CHECK_VAL(io.openx.out.devstate, 0);
339         CHECK_VAL(io.openx.out.action, OPENX_ACTION_CREATED);
340         CHECK_VAL(io.openx.out.size, 1024*1024);
341         CHECK_ALL_INFO(io.openx.in.size, size);
342         smbcli_close(cli->tree, fnum);
343         smbcli_unlink(cli->tree, fname);
344
345         /* check the fields when the file already existed */
346         fnum2 = create_complex_file(cli, mem_ctx, fname);
347         if (fnum2 == -1) {
348                 ret = False;
349                 goto done;
350         }
351         smbcli_close(cli->tree, fnum2);
352
353         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
354         status = smb_raw_open(cli->tree, mem_ctx, &io);
355         CHECK_STATUS(status, NT_STATUS_OK);
356         fnum = io.openx.out.fnum;
357
358         CHECK_ALL_INFO(io.openx.out.size, size);
359         CHECK_TIME(io.openx.out.write_time, write_time);
360         CHECK_VAL(io.openx.out.action, OPENX_ACTION_EXISTED);
361         CHECK_VAL(io.openx.out.unknown, 0);
362         CHECK_ALL_INFO(io.openx.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
363         smbcli_close(cli->tree, fnum);
364
365         /* now check the search attrib for hidden files - win2003 ignores this? */
366         SET_ATTRIB(FILE_ATTRIBUTE_HIDDEN);
367         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN, attrib);
368
369         io.openx.in.search_attrs = FILE_ATTRIBUTE_HIDDEN;
370         status = smb_raw_open(cli->tree, mem_ctx, &io);
371         CHECK_STATUS(status, NT_STATUS_OK);
372         smbcli_close(cli->tree, io.openx.out.fnum);
373
374         io.openx.in.search_attrs = 0;
375         status = smb_raw_open(cli->tree, mem_ctx, &io);
376         CHECK_STATUS(status, NT_STATUS_OK);
377         smbcli_close(cli->tree, io.openx.out.fnum);
378
379         SET_ATTRIB(FILE_ATTRIBUTE_NORMAL);
380         smbcli_unlink(cli->tree, fname);
381
382         /* and check attrib on create */
383         io.openx.in.open_func = OPENX_OPEN_FUNC_FAIL | OPENX_OPEN_FUNC_CREATE;
384         io.openx.in.search_attrs = 0;
385         io.openx.in.file_attrs = FILE_ATTRIBUTE_SYSTEM;
386         status = smb_raw_open(cli->tree, mem_ctx, &io);
387         CHECK_STATUS(status, NT_STATUS_OK);
388         CHECK_ALL_INFO(FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE, 
389                        attrib & ~FILE_ATTRIBUTE_NONINDEXED);
390         smbcli_close(cli->tree, io.openx.out.fnum);
391         smbcli_unlink(cli->tree, fname);
392
393         /* check timeout on create - win2003 ignores the timeout! */
394         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
395         io.openx.in.file_attrs = 0;
396         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_ALL;
397         status = smb_raw_open(cli->tree, mem_ctx, &io);
398         CHECK_STATUS(status, NT_STATUS_OK);
399         fnum = io.openx.out.fnum;
400
401         io.openx.in.timeout = 20000;
402         tv = timeval_current();
403         io.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_NONE;
404         status = smb_raw_open(cli->tree, mem_ctx, &io);
405         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
406         if (timeval_elapsed(&tv) > 3.0) {
407                 printf("(%s) Incorrect timing in openx with timeout - waited %.2f seconds\n",
408                        __location__, timeval_elapsed(&tv));
409                 ret = False;
410         }
411         smbcli_close(cli->tree, fnum);
412         smbcli_unlink(cli->tree, fname);
413
414         /* now this is a really weird one - open for execute implies create?! */
415         io.openx.in.fname = fname;
416         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
417         io.openx.in.open_mode = OPENX_MODE_ACCESS_EXEC | OPENX_MODE_DENY_NONE;
418         io.openx.in.search_attrs = 0;
419         io.openx.in.open_func = OPENX_OPEN_FUNC_FAIL;
420         io.openx.in.file_attrs = 0;
421         io.openx.in.write_time = 0;
422         io.openx.in.size = 0;
423         io.openx.in.timeout = 0;
424         status = smb_raw_open(cli->tree, mem_ctx, &io);
425         CHECK_STATUS(status, NT_STATUS_OK);
426         smbcli_close(cli->tree, io.openx.out.fnum);
427
428         /* check the extended return flag */
429         io.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO | OPENX_FLAGS_EXTENDED_RETURN;
430         io.openx.in.open_func = OPENX_OPEN_FUNC_OPEN;
431         status = smb_raw_open(cli->tree, mem_ctx, &io);
432         CHECK_STATUS(status, NT_STATUS_OK);
433         CHECK_VAL(io.openx.out.access_mask, STD_RIGHT_ALL_ACCESS);
434         smbcli_close(cli->tree, io.openx.out.fnum);
435
436 done:
437         smbcli_close(cli->tree, fnum);
438         smbcli_unlink(cli->tree, fname);
439
440         return ret;
441 }
442
443
444 /*
445   test RAW_OPEN_T2OPEN
446
447   I can't work out how to get win2003 to accept a create file via TRANS2_OPEN, which
448   is why you see all the ACCESS_DENIED results below. When we finally work this out then this
449   test will make more sense
450 */
451 static BOOL test_t2open(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
452 {
453         union smb_open io;
454         union smb_fileinfo finfo;
455         const char *fname1 = BASEDIR "\\torture_t2open_yes.txt";
456         const char *fname2 = BASEDIR "\\torture_t2open_no.txt";
457         const char *fname = BASEDIR "\\torture_t2open_3.txt";
458         NTSTATUS status;
459         int fnum;
460         BOOL ret = True;
461         int i;
462         struct {
463                 uint16_t open_func;
464                 BOOL with_file;
465                 NTSTATUS correct_status;
466         } open_funcs[] = {
467                 { OPENX_OPEN_FUNC_OPEN,                           True,  NT_STATUS_OK },
468                 { OPENX_OPEN_FUNC_OPEN,                           False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
469                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OK },
470                 { OPENX_OPEN_FUNC_OPEN  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_ACCESS_DENIED },
471                 { OPENX_OPEN_FUNC_FAIL,                           True,  NT_STATUS_OBJECT_NAME_COLLISION },
472                 { OPENX_OPEN_FUNC_FAIL,                           False, NT_STATUS_ACCESS_DENIED },
473                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_OBJECT_NAME_COLLISION },
474                 { OPENX_OPEN_FUNC_FAIL  | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_ACCESS_DENIED },
475                 { OPENX_OPEN_FUNC_TRUNC,                          True,  NT_STATUS_ACCESS_DENIED },
476                 { OPENX_OPEN_FUNC_TRUNC,                          False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
477                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, True,  NT_STATUS_ACCESS_DENIED },
478                 { OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE, False, NT_STATUS_ACCESS_DENIED },
479         };
480
481         fnum = create_complex_file(cli, mem_ctx, fname1);
482         if (fnum == -1) {
483                 d_printf("Failed to create file %s - %s\n", fname1, smbcli_errstr(cli->tree));
484                 ret = False;
485                 goto done;
486         }
487         smbcli_close(cli->tree, fnum);
488
489         printf("Checking RAW_OPEN_T2OPEN\n");
490
491         io.t2open.level = RAW_OPEN_T2OPEN;
492         io.t2open.in.flags = OPENX_FLAGS_ADDITIONAL_INFO | 
493                 OPENX_FLAGS_EA_LEN | OPENX_FLAGS_EXTENDED_RETURN;
494         io.t2open.in.open_mode = OPENX_MODE_DENY_NONE | OPENX_MODE_ACCESS_RDWR;
495         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
496         io.t2open.in.file_attrs = 0;
497         io.t2open.in.write_time = 0;
498         io.t2open.in.size = 0;
499         io.t2open.in.timeout = 0;
500
501         io.t2open.in.eas = talloc(mem_ctx, sizeof(io.t2open.in.eas[0]));
502         io.t2open.in.num_eas = 1;
503         io.t2open.in.eas[0].flags = 0;
504         io.t2open.in.eas[0].name.s = "EAONE";
505         io.t2open.in.eas[0].value = data_blob_talloc(mem_ctx, "1", 1);
506
507         /* check all combinations of open_func */
508         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
509                 if (open_funcs[i].with_file) {
510                         io.t2open.in.fname = fname1;
511                 } else {
512                         io.t2open.in.fname = fname2;
513                 }
514                 io.t2open.in.open_func = open_funcs[i].open_func;
515                 status = smb_raw_open(cli->tree, mem_ctx, &io);
516                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
517                         printf("(%s) incorrect status %s should be %s (i=%d with_file=%d open_func=0x%x)\n", 
518                                __location__, nt_errstr(status), nt_errstr(open_funcs[i].correct_status),
519                                i, (int)open_funcs[i].with_file, (int)open_funcs[i].open_func);
520                         ret = False;
521                 }
522                 if (NT_STATUS_IS_OK(status)) {
523                         smbcli_close(cli->tree, io.t2open.out.fnum);
524                 }
525         }
526
527         smbcli_unlink(cli->tree, fname1);
528
529         /* check the basic return fields */
530         fnum = create_complex_file(cli, mem_ctx, fname);
531         smbcli_close(cli->tree, fnum);
532         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
533         io.t2open.in.fname = fname;
534         status = smb_raw_open(cli->tree, mem_ctx, &io);
535         CHECK_STATUS(status, NT_STATUS_OK);
536         fnum = io.t2open.out.fnum;
537
538         CHECK_ALL_INFO(io.t2open.out.size, size);
539         CHECK_VAL(io.t2open.out.write_time, 0);
540         CHECK_ALL_INFO(io.t2open.out.attrib, attrib & ~FILE_ATTRIBUTE_NONINDEXED);
541         CHECK_VAL(io.t2open.out.access, OPENX_MODE_DENY_NONE | OPENX_MODE_ACCESS_RDWR);
542         CHECK_VAL(io.t2open.out.ftype, 0);
543         CHECK_VAL(io.t2open.out.devstate, 0);
544         CHECK_VAL(io.t2open.out.action, OPENX_ACTION_EXISTED);
545         smbcli_close(cli->tree, fnum);
546
547         /* now check the search attrib for hidden files - win2003 ignores this? */
548         SET_ATTRIB(FILE_ATTRIBUTE_HIDDEN);
549         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN, attrib);
550
551         status = smb_raw_open(cli->tree, mem_ctx, &io);
552         CHECK_STATUS(status, NT_STATUS_OK);
553         smbcli_close(cli->tree, io.t2open.out.fnum);
554
555         status = smb_raw_open(cli->tree, mem_ctx, &io);
556         CHECK_STATUS(status, NT_STATUS_OK);
557         smbcli_close(cli->tree, io.t2open.out.fnum);
558
559         SET_ATTRIB(FILE_ATTRIBUTE_NORMAL);
560         smbcli_unlink(cli->tree, fname);
561
562         /* and check attrib on create */
563         io.t2open.in.open_func = OPENX_OPEN_FUNC_FAIL | OPENX_OPEN_FUNC_CREATE;
564         io.t2open.in.file_attrs = FILE_ATTRIBUTE_SYSTEM;
565         status = smb_raw_open(cli->tree, mem_ctx, &io);
566         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
567
568         /* check timeout on create - win2003 ignores the timeout! */
569         io.t2open.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
570         io.t2open.in.file_attrs = 0;
571         io.t2open.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_ALL;
572         status = smb_raw_open(cli->tree, mem_ctx, &io);
573         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
574
575 done:
576         smbcli_close(cli->tree, fnum);
577         smbcli_unlink(cli->tree, fname);
578
579         return ret;
580 }
581         
582
583 /*
584   test RAW_OPEN_NTCREATEX
585 */
586 static BOOL test_ntcreatex(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
587 {
588         union smb_open io;
589         union smb_fileinfo finfo;
590         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
591         const char *dname = BASEDIR "\\torture_ntcreatex.dir";
592         NTSTATUS status;
593         int fnum = -1;
594         BOOL ret = True;
595         int i;
596         struct {
597                 uint32_t open_disp;
598                 BOOL with_file;
599                 NTSTATUS correct_status;
600         } open_funcs[] = {
601                 { NTCREATEX_DISP_SUPERSEDE,     True,  NT_STATUS_OK },
602                 { NTCREATEX_DISP_SUPERSEDE,     False, NT_STATUS_OK },
603                 { NTCREATEX_DISP_OPEN,          True,  NT_STATUS_OK },
604                 { NTCREATEX_DISP_OPEN,          False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
605                 { NTCREATEX_DISP_CREATE,        True,  NT_STATUS_OBJECT_NAME_COLLISION },
606                 { NTCREATEX_DISP_CREATE,        False, NT_STATUS_OK },
607                 { NTCREATEX_DISP_OPEN_IF,       True,  NT_STATUS_OK },
608                 { NTCREATEX_DISP_OPEN_IF,       False, NT_STATUS_OK },
609                 { NTCREATEX_DISP_OVERWRITE,     True,  NT_STATUS_OK },
610                 { NTCREATEX_DISP_OVERWRITE,     False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
611                 { NTCREATEX_DISP_OVERWRITE_IF,  True,  NT_STATUS_OK },
612                 { NTCREATEX_DISP_OVERWRITE_IF,  False, NT_STATUS_OK },
613                 { 6,                            True,  NT_STATUS_INVALID_PARAMETER },
614                 { 6,                            False, NT_STATUS_INVALID_PARAMETER },
615         };
616
617         printf("Checking RAW_OPEN_NTCREATEX\n");
618
619         /* reasonable default parameters */
620         io.generic.level = RAW_OPEN_NTCREATEX;
621         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
622         io.ntcreatex.in.root_fid = 0;
623         io.ntcreatex.in.access_mask = GENERIC_RIGHTS_FILE_ALL_ACCESS;
624         io.ntcreatex.in.alloc_size = 1024*1024;
625         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
626         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
627         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
628         io.ntcreatex.in.create_options = 0;
629         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
630         io.ntcreatex.in.security_flags = 0;
631         io.ntcreatex.in.fname = fname;
632
633         /* test the open disposition */
634         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
635                 if (open_funcs[i].with_file) {
636                         fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR|O_TRUNC, DENY_NONE);
637                         if (fnum == -1) {
638                                 d_printf("Failed to create file %s - %s\n", fname, smbcli_errstr(cli->tree));
639                                 ret = False;
640                                 goto done;
641                         }
642                         smbcli_close(cli->tree, fnum);
643                 }
644                 io.ntcreatex.in.open_disposition = open_funcs[i].open_disp;
645                 status = smb_raw_open(cli->tree, mem_ctx, &io);
646                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
647                         printf("(%s) incorrect status %s should be %s (i=%d with_file=%d open_disp=%d)\n", 
648                                __location__, nt_errstr(status), nt_errstr(open_funcs[i].correct_status),
649                                i, (int)open_funcs[i].with_file, (int)open_funcs[i].open_disp);
650                         ret = False;
651                 }
652                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
653                         smbcli_close(cli->tree, io.ntcreatex.out.fnum);
654                         smbcli_unlink(cli->tree, fname);
655                 }
656         }
657
658         /* basic field testing */
659         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
660
661         status = smb_raw_open(cli->tree, mem_ctx, &io);
662         CHECK_STATUS(status, NT_STATUS_OK);
663         fnum = io.ntcreatex.out.fnum;
664
665         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
666         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
667         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
668         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
669         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
670         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
671         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
672         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
673         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
674         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
675         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
676
677         /* check fields when the file already existed */
678         smbcli_close(cli->tree, fnum);
679         smbcli_unlink(cli->tree, fname);
680         fnum = create_complex_file(cli, mem_ctx, fname);
681         if (fnum == -1) {
682                 ret = False;
683                 goto done;
684         }
685         smbcli_close(cli->tree, fnum);
686
687         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
688         status = smb_raw_open(cli->tree, mem_ctx, &io);
689         CHECK_STATUS(status, NT_STATUS_OK);
690         fnum = io.ntcreatex.out.fnum;
691
692         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
693         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
694         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
695         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
696         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
697         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
698         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
699         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
700         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
701         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
702         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
703         smbcli_close(cli->tree, fnum);
704         smbcli_unlink(cli->tree, fname);
705
706
707         /* create a directory */
708         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
709         io.ntcreatex.in.access_mask = GENERIC_RIGHTS_FILE_ALL_ACCESS;
710         io.ntcreatex.in.alloc_size = 0;
711         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
712         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
713         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
714         io.ntcreatex.in.create_options = 0;
715         io.ntcreatex.in.fname = dname;
716         fname = dname;
717
718         smbcli_rmdir(cli->tree, fname);
719         smbcli_unlink(cli->tree, fname);
720
721         io.ntcreatex.in.access_mask = SEC_RIGHT_MAXIMUM_ALLOWED;
722         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
723         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
724         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
725         status = smb_raw_open(cli->tree, mem_ctx, &io);
726         CHECK_STATUS(status, NT_STATUS_OK);
727         fnum = io.ntcreatex.out.fnum;
728
729         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
730         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
731         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
732         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
733         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
734         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
735         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
736         CHECK_VAL(io.ntcreatex.out.attrib & ~FILE_ATTRIBUTE_NONINDEXED, 
737                   FILE_ATTRIBUTE_DIRECTORY);
738         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
739         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
740         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
741         CHECK_VAL(io.ntcreatex.out.is_directory, 1);
742         CHECK_VAL(io.ntcreatex.out.size, 0);
743         CHECK_VAL(io.ntcreatex.out.alloc_size, 0);
744         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
745         smbcli_unlink(cli->tree, fname);
746         
747
748 done:
749         smbcli_close(cli->tree, fnum);
750         smbcli_unlink(cli->tree, fname);
751
752         return ret;
753 }
754
755
756 /*
757   test RAW_OPEN_NTTRANS_CREATE
758 */
759 static BOOL test_nttrans_create(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
760 {
761         union smb_open io;
762         union smb_fileinfo finfo;
763         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
764         const char *dname = BASEDIR "\\torture_ntcreatex.dir";
765         NTSTATUS status;
766         int fnum = -1;
767         BOOL ret = True;
768         int i;
769         struct {
770                 uint32_t open_disp;
771                 BOOL with_file;
772                 NTSTATUS correct_status;
773         } open_funcs[] = {
774                 { NTCREATEX_DISP_SUPERSEDE,     True,  NT_STATUS_OK },
775                 { NTCREATEX_DISP_SUPERSEDE,     False, NT_STATUS_OK },
776                 { NTCREATEX_DISP_OPEN,          True,  NT_STATUS_OK },
777                 { NTCREATEX_DISP_OPEN,          False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
778                 { NTCREATEX_DISP_CREATE,        True,  NT_STATUS_OBJECT_NAME_COLLISION },
779                 { NTCREATEX_DISP_CREATE,        False, NT_STATUS_OK },
780                 { NTCREATEX_DISP_OPEN_IF,       True,  NT_STATUS_OK },
781                 { NTCREATEX_DISP_OPEN_IF,       False, NT_STATUS_OK },
782                 { NTCREATEX_DISP_OVERWRITE,     True,  NT_STATUS_OK },
783                 { NTCREATEX_DISP_OVERWRITE,     False, NT_STATUS_OBJECT_NAME_NOT_FOUND },
784                 { NTCREATEX_DISP_OVERWRITE_IF,  True,  NT_STATUS_OK },
785                 { NTCREATEX_DISP_OVERWRITE_IF,  False, NT_STATUS_OK },
786                 { 6,                            True,  NT_STATUS_INVALID_PARAMETER },
787                 { 6,                            False, NT_STATUS_INVALID_PARAMETER },
788         };
789
790         printf("Checking RAW_OPEN_NTTRANS_CREATE\n");
791
792         /* reasonable default parameters */
793         io.generic.level = RAW_OPEN_NTTRANS_CREATE;
794         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
795         io.ntcreatex.in.root_fid = 0;
796         io.ntcreatex.in.access_mask = GENERIC_RIGHTS_FILE_ALL_ACCESS;
797         io.ntcreatex.in.alloc_size = 1024*1024;
798         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
799         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
800         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
801         io.ntcreatex.in.create_options = 0;
802         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
803         io.ntcreatex.in.security_flags = 0;
804         io.ntcreatex.in.fname = fname;
805         io.ntcreatex.in.sec_desc = NULL;
806         io.ntcreatex.in.ea_list = NULL;
807
808         /* test the open disposition */
809         for (i=0; i<ARRAY_SIZE(open_funcs); i++) {
810                 if (open_funcs[i].with_file) {
811                         fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR|O_TRUNC, DENY_NONE);
812                         if (fnum == -1) {
813                                 d_printf("Failed to create file %s - %s\n", fname, smbcli_errstr(cli->tree));
814                                 ret = False;
815                                 goto done;
816                         }
817                         smbcli_close(cli->tree, fnum);
818                 }
819                 io.ntcreatex.in.open_disposition = open_funcs[i].open_disp;
820                 status = smb_raw_open(cli->tree, mem_ctx, &io);
821                 if (!NT_STATUS_EQUAL(status, open_funcs[i].correct_status)) {
822                         printf("(%s) incorrect status %s should be %s (i=%d with_file=%d open_disp=%d)\n", 
823                                __location__, nt_errstr(status), nt_errstr(open_funcs[i].correct_status),
824                                i, (int)open_funcs[i].with_file, (int)open_funcs[i].open_disp);
825                         ret = False;
826                 }
827                 if (NT_STATUS_IS_OK(status) || open_funcs[i].with_file) {
828                         smbcli_close(cli->tree, io.ntcreatex.out.fnum);
829                         smbcli_unlink(cli->tree, fname);
830                 }
831         }
832
833         /* basic field testing */
834         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
835
836         status = smb_raw_open(cli->tree, mem_ctx, &io);
837         CHECK_STATUS(status, NT_STATUS_OK);
838         fnum = io.ntcreatex.out.fnum;
839
840         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
841         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
842         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
843         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
844         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
845         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
846         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
847         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
848         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
849         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
850         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
851
852         /* check fields when the file already existed */
853         smbcli_close(cli->tree, fnum);
854         smbcli_unlink(cli->tree, fname);
855         fnum = create_complex_file(cli, mem_ctx, fname);
856         if (fnum == -1) {
857                 ret = False;
858                 goto done;
859         }
860         smbcli_close(cli->tree, fnum);
861
862         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
863         status = smb_raw_open(cli->tree, mem_ctx, &io);
864         CHECK_STATUS(status, NT_STATUS_OK);
865         fnum = io.ntcreatex.out.fnum;
866
867         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
868         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_EXISTED);
869         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
870         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
871         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
872         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
873         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
874         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
875         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
876         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
877         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
878         smbcli_close(cli->tree, fnum);
879         smbcli_unlink(cli->tree, fname);
880
881
882         /* create a directory */
883         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
884         io.ntcreatex.in.access_mask = GENERIC_RIGHTS_FILE_ALL_ACCESS;
885         io.ntcreatex.in.alloc_size = 0;
886         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
887         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
888         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
889         io.ntcreatex.in.create_options = 0;
890         io.ntcreatex.in.fname = dname;
891         fname = dname;
892
893         smbcli_rmdir(cli->tree, fname);
894         smbcli_unlink(cli->tree, fname);
895
896         io.ntcreatex.in.access_mask = SEC_RIGHT_MAXIMUM_ALLOWED;
897         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
898         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
899         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
900         status = smb_raw_open(cli->tree, mem_ctx, &io);
901         CHECK_STATUS(status, NT_STATUS_OK);
902         fnum = io.ntcreatex.out.fnum;
903
904         CHECK_VAL(io.ntcreatex.out.oplock_level, 0);
905         CHECK_VAL(io.ntcreatex.out.create_action, NTCREATEX_ACTION_CREATED);
906         CHECK_NTTIME(io.ntcreatex.out.create_time, create_time);
907         CHECK_NTTIME(io.ntcreatex.out.access_time, access_time);
908         CHECK_NTTIME(io.ntcreatex.out.write_time, write_time);
909         CHECK_NTTIME(io.ntcreatex.out.change_time, change_time);
910         CHECK_ALL_INFO(io.ntcreatex.out.attrib, attrib);
911         CHECK_VAL(io.ntcreatex.out.attrib & ~FILE_ATTRIBUTE_NONINDEXED, 
912                   FILE_ATTRIBUTE_DIRECTORY);
913         CHECK_ALL_INFO(io.ntcreatex.out.alloc_size, alloc_size);
914         CHECK_ALL_INFO(io.ntcreatex.out.size, size);
915         CHECK_ALL_INFO(io.ntcreatex.out.is_directory, directory);
916         CHECK_VAL(io.ntcreatex.out.is_directory, 1);
917         CHECK_VAL(io.ntcreatex.out.size, 0);
918         CHECK_VAL(io.ntcreatex.out.alloc_size, 0);
919         CHECK_VAL(io.ntcreatex.out.file_type, FILE_TYPE_DISK);
920         smbcli_unlink(cli->tree, fname);
921         
922
923 done:
924         smbcli_close(cli->tree, fnum);
925         smbcli_unlink(cli->tree, fname);
926
927         return ret;
928 }
929
930 /*
931   test RAW_OPEN_NTCREATEX with an already opened and byte range locked file
932
933   I've got an application that does a similar sequence of ntcreate&x,
934   locking&x and another ntcreate&x with
935   open_disposition==NTCREATEX_DISP_OVERWRITE_IF. Windows 2003 allows the
936   second open.
937 */
938 static BOOL test_ntcreatex_brlocked(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
939 {
940         union smb_open io, io1;
941         union smb_lock io2;
942         struct smb_lock_entry lock[1];
943         const char *fname = BASEDIR "\\torture_ntcreatex.txt";
944         NTSTATUS status;
945         BOOL ret = True;
946
947         /* reasonable default parameters */
948         io.generic.level = RAW_OPEN_NTCREATEX;
949         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
950         io.ntcreatex.in.root_fid = 0;
951         io.ntcreatex.in.access_mask = 0x2019f;
952         io.ntcreatex.in.alloc_size = 0;
953         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
954         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
955                 NTCREATEX_SHARE_ACCESS_WRITE;
956         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
957         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
958         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
959         io.ntcreatex.in.security_flags = NTCREATEX_SECURITY_DYNAMIC |
960                 NTCREATEX_SECURITY_ALL;
961         io.ntcreatex.in.fname = fname;
962
963         status = smb_raw_open(cli->tree, mem_ctx, &io);
964         CHECK_STATUS(status, NT_STATUS_OK);
965
966         io2.lockx.level = RAW_LOCK_LOCKX;
967         io2.lockx.in.fnum = io.ntcreatex.out.fnum;
968         io2.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
969         io2.lockx.in.timeout = 0;
970         io2.lockx.in.ulock_cnt = 0;
971         io2.lockx.in.lock_cnt = 1;
972         lock[0].pid = cli->session->pid;
973         lock[0].offset = 0;
974         lock[0].count = 0x1;
975         io2.lockx.in.locks = &lock[0];
976         status = smb_raw_lock(cli->tree, &io2);
977         CHECK_STATUS(status, NT_STATUS_OK);
978
979         io1.generic.level = RAW_OPEN_NTCREATEX;
980         io1.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
981         io1.ntcreatex.in.root_fid = 0;
982         io1.ntcreatex.in.access_mask = 0x20196;
983         io1.ntcreatex.in.alloc_size = 0;
984         io1.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
985         io1.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
986                 NTCREATEX_SHARE_ACCESS_WRITE;
987         io1.ntcreatex.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
988         io1.ntcreatex.in.create_options = 0;
989         io1.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
990         io1.ntcreatex.in.security_flags = NTCREATEX_SECURITY_DYNAMIC |
991                 NTCREATEX_SECURITY_ALL;
992         io1.ntcreatex.in.fname = fname;
993
994         status = smb_raw_open(cli->tree, mem_ctx, &io1);
995         CHECK_STATUS(status, NT_STATUS_OK);
996
997  done:
998         smbcli_close(cli->tree, io.ntcreatex.out.fnum);
999         smbcli_close(cli->tree, io1.ntcreatex.out.fnum);
1000         smbcli_unlink(cli->tree, fname);
1001         return ret;
1002 }
1003
1004 /*
1005   test RAW_OPEN_MKNEW
1006 */
1007 static BOOL test_mknew(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1008 {
1009         union smb_open io;
1010         const char *fname = BASEDIR "\\torture_mknew.txt";
1011         NTSTATUS status;
1012         int fnum = -1;
1013         BOOL ret = True;
1014         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1015         union smb_fileinfo finfo;
1016
1017         printf("Checking RAW_OPEN_MKNEW\n");
1018
1019         io.mknew.level = RAW_OPEN_MKNEW;
1020         io.mknew.in.attrib = 0;
1021         io.mknew.in.write_time = 0;
1022         io.mknew.in.fname = fname;
1023         status = smb_raw_open(cli->tree, mem_ctx, &io);
1024         CHECK_STATUS(status, NT_STATUS_OK);
1025         fnum = io.mknew.out.fnum;
1026
1027         status = smb_raw_open(cli->tree, mem_ctx, &io);
1028         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_COLLISION);
1029
1030         smbcli_close(cli->tree, fnum);
1031         smbcli_unlink(cli->tree, fname);
1032
1033         /* make sure write_time works */
1034         io.mknew.in.write_time = basetime;
1035         status = smb_raw_open(cli->tree, mem_ctx, &io);
1036         CHECK_STATUS(status, NT_STATUS_OK);
1037         fnum = io.mknew.out.fnum;
1038         CHECK_TIME(basetime, write_time);
1039
1040         smbcli_close(cli->tree, fnum);
1041         smbcli_unlink(cli->tree, fname);
1042
1043         /* make sure file_attrs works */
1044         io.mknew.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1045         status = smb_raw_open(cli->tree, mem_ctx, &io);
1046         CHECK_STATUS(status, NT_STATUS_OK);
1047         fnum = io.mknew.out.fnum;
1048         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE, 
1049                        attrib & ~FILE_ATTRIBUTE_NONINDEXED);
1050         
1051 done:
1052         smbcli_close(cli->tree, fnum);
1053         smbcli_unlink(cli->tree, fname);
1054
1055         return ret;
1056 }
1057
1058
1059 /*
1060   test RAW_OPEN_CREATE
1061 */
1062 static BOOL test_create(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1063 {
1064         union smb_open io;
1065         const char *fname = BASEDIR "\\torture_create.txt";
1066         NTSTATUS status;
1067         int fnum = -1;
1068         BOOL ret = True;
1069         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1070         union smb_fileinfo finfo;
1071
1072         printf("Checking RAW_OPEN_CREATE\n");
1073
1074         io.create.level = RAW_OPEN_CREATE;
1075         io.create.in.attrib = 0;
1076         io.create.in.write_time = 0;
1077         io.create.in.fname = fname;
1078         status = smb_raw_open(cli->tree, mem_ctx, &io);
1079         CHECK_STATUS(status, NT_STATUS_OK);
1080         fnum = io.create.out.fnum;
1081
1082         status = smb_raw_open(cli->tree, mem_ctx, &io);
1083         CHECK_STATUS(status, NT_STATUS_OK);
1084
1085         smbcli_close(cli->tree, io.create.out.fnum);
1086         smbcli_close(cli->tree, fnum);
1087         smbcli_unlink(cli->tree, fname);
1088
1089         /* make sure write_time works */
1090         io.create.in.write_time = basetime;
1091         status = smb_raw_open(cli->tree, mem_ctx, &io);
1092         CHECK_STATUS(status, NT_STATUS_OK);
1093         fnum = io.create.out.fnum;
1094         CHECK_TIME(basetime, write_time);
1095
1096         smbcli_close(cli->tree, fnum);
1097         smbcli_unlink(cli->tree, fname);
1098
1099         /* make sure file_attrs works */
1100         io.create.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1101         status = smb_raw_open(cli->tree, mem_ctx, &io);
1102         CHECK_STATUS(status, NT_STATUS_OK);
1103         fnum = io.create.out.fnum;
1104         CHECK_ALL_INFO(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE, 
1105                        attrib & ~FILE_ATTRIBUTE_NONINDEXED);
1106         
1107 done:
1108         smbcli_close(cli->tree, fnum);
1109         smbcli_unlink(cli->tree, fname);
1110
1111         return ret;
1112 }
1113
1114
1115 /*
1116   test RAW_OPEN_CTEMP
1117 */
1118 static BOOL test_ctemp(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1119 {
1120         union smb_open io;
1121         NTSTATUS status;
1122         int fnum = -1;
1123         BOOL ret = True;
1124         time_t basetime = (time(NULL) + 3600*24*3) & ~1;
1125         union smb_fileinfo finfo;
1126         const char *name, *fname = NULL;
1127
1128         printf("Checking RAW_OPEN_CTEMP\n");
1129
1130         io.ctemp.level = RAW_OPEN_CTEMP;
1131         io.ctemp.in.attrib = FILE_ATTRIBUTE_HIDDEN;
1132         io.ctemp.in.write_time = basetime;
1133         io.ctemp.in.directory = BASEDIR;
1134         status = smb_raw_open(cli->tree, mem_ctx, &io);
1135         CHECK_STATUS(status, NT_STATUS_OK);
1136         fnum = io.ctemp.out.fnum;
1137
1138         name = io.ctemp.out.name;
1139
1140         finfo.generic.level = RAW_FILEINFO_NAME_INFO;
1141         finfo.generic.in.fnum = fnum;
1142         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
1143         CHECK_STATUS(status, NT_STATUS_OK);
1144
1145         fname = finfo.name_info.out.fname.s;
1146         d_printf("ctemp name=%s  real name=%s\n", name, fname);
1147
1148 done:
1149         smbcli_close(cli->tree, fnum);
1150         if (fname) {
1151                 smbcli_unlink(cli->tree, fname);
1152         }
1153
1154         return ret;
1155 }
1156
1157 /* basic testing of all RAW_OPEN_* calls 
1158 */
1159 BOOL torture_raw_open(void)
1160 {
1161         struct smbcli_state *cli;
1162         BOOL ret = True;
1163         TALLOC_CTX *mem_ctx;
1164
1165         if (!torture_open_connection(&cli)) {
1166                 return False;
1167         }
1168
1169         mem_ctx = talloc_init("torture_raw_open");
1170
1171         if (!torture_setup_dir(cli, BASEDIR)) {
1172                 return False;
1173         }
1174
1175         ret &= test_ntcreatex_brlocked(cli, mem_ctx);
1176         ret &= test_open(cli, mem_ctx);
1177         ret &= test_openx(cli, mem_ctx);
1178         ret &= test_ntcreatex(cli, mem_ctx);
1179         ret &= test_nttrans_create(cli, mem_ctx);
1180         ret &= test_t2open(cli, mem_ctx);
1181         ret &= test_mknew(cli, mem_ctx);
1182         ret &= test_create(cli, mem_ctx);
1183         ret &= test_ctemp(cli, mem_ctx);
1184
1185         smb_raw_exit(cli->session);
1186         smbcli_deltree(cli->tree, BASEDIR);
1187
1188         torture_close_connection(cli);
1189         talloc_destroy(mem_ctx);
1190         return ret;
1191 }