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