2 Unix SMB/CIFS implementation.
3 Test some misc Samba3 code paths
4 Copyright (C) Volker Lendecke 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "torture/torture.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "libcli/libcli.h"
26 #include "torture/util.h"
28 #define CHECK_STATUS(status, correct) do { \
29 if (!NT_STATUS_EQUAL(status, correct)) { \
30 printf("(%s) Incorrect status %s - should be %s\n", \
31 __location__, nt_errstr(status), nt_errstr(correct)); \
36 BOOL torture_samba3_checkfsp(struct torture_context *torture)
38 struct smbcli_state *cli;
39 const char *fname = "test.txt";
40 const char *dirname = "testdir";
47 struct smbcli_tree *tree2;
49 if ((mem_ctx = talloc_init("torture_samba3_checkfsp")) == NULL) {
50 d_printf("talloc_init failed\n");
54 if (!torture_open_connection_share(
55 torture, &cli, torture_setting_string(torture, "host", NULL),
56 torture_setting_string(torture, "share", NULL), NULL)) {
57 d_printf("torture_open_connection_share failed\n");
62 smbcli_deltree(cli->tree, dirname);
64 status = torture_second_tcon(torture, cli->session,
65 torture_setting_string(torture, "share", NULL),
67 CHECK_STATUS(status, NT_STATUS_OK);
68 if (!NT_STATUS_IS_OK(status))
71 /* Try a read on an invalid FID */
73 nread = smbcli_read(cli->tree, 4711, buf, 0, sizeof(buf));
74 CHECK_STATUS(smbcli_nt_error(cli->tree), NT_STATUS_INVALID_HANDLE);
76 /* Try a read on a directory handle */
78 status = smbcli_mkdir(cli->tree, dirname);
79 if (!NT_STATUS_IS_OK(status)) {
80 d_printf("smbcli_mkdir failed: %s\n", nt_errstr(status));
85 /* Open the directory */
88 io.generic.level = RAW_OPEN_NTCREATEX;
89 io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
90 io.ntcreatex.in.root_fid = 0;
91 io.ntcreatex.in.security_flags = 0;
92 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
93 io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
94 io.ntcreatex.in.alloc_size = 0;
95 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
96 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
97 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
98 io.ntcreatex.in.create_options = 0;
99 io.ntcreatex.in.fname = dirname;
100 status = smb_raw_open(cli->tree, mem_ctx, &io);
101 if (!NT_STATUS_IS_OK(status)) {
102 d_printf("smb_open on the directory failed: %s\n",
107 fnum = io.ntcreatex.out.file.fnum;
110 /* Try a read on the directory */
112 nread = smbcli_read(cli->tree, fnum, buf, 0, sizeof(buf));
114 d_printf("smbcli_read on a directory succeeded, expected "
119 CHECK_STATUS(smbcli_nt_error(cli->tree),
120 NT_STATUS_INVALID_DEVICE_REQUEST);
122 /* Same test on the second tcon */
124 nread = smbcli_read(tree2, fnum, buf, 0, sizeof(buf));
126 d_printf("smbcli_read on a directory succeeded, expected "
131 CHECK_STATUS(smbcli_nt_error(tree2), NT_STATUS_INVALID_HANDLE);
133 smbcli_close(cli->tree, fnum);
135 /* Try a normal file read on a second tcon */
137 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
139 d_printf("Failed to create %s - %s\n", fname,
140 smbcli_errstr(cli->tree));
145 nread = smbcli_read(tree2, fnum, buf, 0, sizeof(buf));
146 CHECK_STATUS(smbcli_nt_error(tree2), NT_STATUS_INVALID_HANDLE);
148 smbcli_close(cli->tree, fnum);
151 smbcli_deltree(cli->tree, dirname);
152 torture_close_connection(cli);
153 talloc_free(mem_ctx);
158 static NTSTATUS raw_smbcli_open(struct smbcli_tree *tree, const char *fname, int flags, int share_mode, int *fnum)
160 union smb_open open_parms;
166 mem_ctx = talloc_init("raw_open");
167 if (!mem_ctx) return NT_STATUS_NO_MEMORY;
169 if (flags & O_CREAT) {
170 openfn |= OPENX_OPEN_FUNC_CREATE;
172 if (!(flags & O_EXCL)) {
173 if (flags & O_TRUNC) {
174 openfn |= OPENX_OPEN_FUNC_TRUNC;
176 openfn |= OPENX_OPEN_FUNC_OPEN;
180 accessmode = (share_mode<<OPENX_MODE_DENY_SHIFT);
182 if ((flags & O_ACCMODE) == O_RDWR) {
183 accessmode |= OPENX_MODE_ACCESS_RDWR;
184 } else if ((flags & O_ACCMODE) == O_WRONLY) {
185 accessmode |= OPENX_MODE_ACCESS_WRITE;
186 } else if ((flags & O_ACCMODE) == O_RDONLY) {
187 accessmode |= OPENX_MODE_ACCESS_READ;
191 if ((flags & O_SYNC) == O_SYNC) {
192 accessmode |= OPENX_MODE_WRITE_THRU;
196 if (share_mode == DENY_FCB) {
197 accessmode = OPENX_MODE_ACCESS_FCB | OPENX_MODE_DENY_FCB;
200 open_parms.openx.level = RAW_OPEN_OPENX;
201 open_parms.openx.in.flags = 0;
202 open_parms.openx.in.open_mode = accessmode;
203 open_parms.openx.in.search_attrs = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
204 open_parms.openx.in.file_attrs = 0;
205 open_parms.openx.in.write_time = 0;
206 open_parms.openx.in.open_func = openfn;
207 open_parms.openx.in.size = 0;
208 open_parms.openx.in.timeout = 0;
209 open_parms.openx.in.fname = fname;
211 status = smb_raw_open(tree, mem_ctx, &open_parms);
212 talloc_free(mem_ctx);
214 if (fnum && NT_STATUS_IS_OK(status)) {
215 *fnum = open_parms.openx.out.file.fnum;
221 static NTSTATUS raw_smbcli_t2open(struct smbcli_tree *tree, const char *fname, int flags, int share_mode, int *fnum)
229 mem_ctx = talloc_init("raw_t2open");
230 if (!mem_ctx) return NT_STATUS_NO_MEMORY;
232 if (flags & O_CREAT) {
233 openfn |= OPENX_OPEN_FUNC_CREATE;
235 if (!(flags & O_EXCL)) {
236 if (flags & O_TRUNC) {
237 openfn |= OPENX_OPEN_FUNC_TRUNC;
239 openfn |= OPENX_OPEN_FUNC_OPEN;
243 accessmode = (share_mode<<OPENX_MODE_DENY_SHIFT);
245 if ((flags & O_ACCMODE) == O_RDWR) {
246 accessmode |= OPENX_MODE_ACCESS_RDWR;
247 } else if ((flags & O_ACCMODE) == O_WRONLY) {
248 accessmode |= OPENX_MODE_ACCESS_WRITE;
249 } else if ((flags & O_ACCMODE) == O_RDONLY) {
250 accessmode |= OPENX_MODE_ACCESS_READ;
254 if ((flags & O_SYNC) == O_SYNC) {
255 accessmode |= OPENX_MODE_WRITE_THRU;
259 if (share_mode == DENY_FCB) {
260 accessmode = OPENX_MODE_ACCESS_FCB | OPENX_MODE_DENY_FCB;
263 memset(&io, '\0', sizeof(io));
264 io.t2open.level = RAW_OPEN_T2OPEN;
265 io.t2open.in.flags = 0;
266 io.t2open.in.open_mode = accessmode;
267 io.t2open.in.search_attrs = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
268 io.t2open.in.file_attrs = 0;
269 io.t2open.in.write_time = 0;
270 io.t2open.in.open_func = openfn;
271 io.t2open.in.size = 0;
272 io.t2open.in.timeout = 0;
273 io.t2open.in.fname = fname;
275 io.t2open.in.num_eas = 1;
276 io.t2open.in.eas = talloc_array(mem_ctx, struct ea_struct, io.t2open.in.num_eas);
277 io.t2open.in.eas[0].flags = 0;
278 io.t2open.in.eas[0].name.s = ".CLASSINFO";
279 io.t2open.in.eas[0].value = data_blob_talloc(mem_ctx, "first value", 11);
281 status = smb_raw_open(tree, mem_ctx, &io);
282 talloc_free(mem_ctx);
284 if (fnum && NT_STATUS_IS_OK(status)) {
285 *fnum = io.openx.out.file.fnum;
291 static NTSTATUS raw_smbcli_ntcreate(struct smbcli_tree *tree, const char *fname, int *fnum)
297 mem_ctx = talloc_init("raw_t2open");
298 if (!mem_ctx) return NT_STATUS_NO_MEMORY;
300 memset(&io, '\0', sizeof(io));
301 io.generic.level = RAW_OPEN_NTCREATEX;
302 io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
303 io.ntcreatex.in.root_fid = 0;
304 io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
305 io.ntcreatex.in.alloc_size = 0;
306 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
307 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
308 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
309 io.ntcreatex.in.create_options = 0;
310 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
311 io.ntcreatex.in.security_flags = 0;
312 io.ntcreatex.in.fname = fname;
314 status = smb_raw_open(tree, mem_ctx, &io);
315 talloc_free(mem_ctx);
317 if (fnum && NT_STATUS_IS_OK(status)) {
318 *fnum = io.openx.out.file.fnum;
325 BOOL torture_samba3_badpath(struct torture_context *torture)
327 struct smbcli_state *cli_nt;
328 struct smbcli_state *cli_dos;
329 const char *fname = "test.txt";
330 const char *dirname = "testdir";
336 BOOL nt_status_support;
338 if (!(mem_ctx = talloc_init("torture_samba3_badpath"))) {
339 d_printf("talloc_init failed\n");
343 nt_status_support = lp_nt_status_support();
345 if (!lp_set_cmdline("nt status support", "yes")) {
346 printf("Could not set 'nt status support = yes'\n");
350 if (!torture_open_connection(&cli_nt, 0)) {
354 if (!lp_set_cmdline("nt status support", "no")) {
355 printf("Could not set 'nt status support = yes'\n");
359 if (!torture_open_connection(&cli_dos, 1)) {
363 if (!lp_set_cmdline("nt status support",
364 nt_status_support ? "yes":"no")) {
365 printf("Could not reset 'nt status support = yes'");
369 smbcli_deltree(cli_nt->tree, dirname);
371 status = smbcli_mkdir(cli_nt->tree, dirname);
372 if (!NT_STATUS_IS_OK(status)) {
373 d_printf("smbcli_mkdir failed: %s\n", nt_errstr(status));
378 status = smbcli_chkpath(cli_nt->tree, dirname);
379 CHECK_STATUS(status, NT_STATUS_OK);
381 status = smbcli_chkpath(cli_nt->tree,
382 talloc_asprintf(mem_ctx, "%s\\bla", dirname));
383 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
385 status = smbcli_chkpath(cli_dos->tree,
386 talloc_asprintf(mem_ctx, "%s\\bla", dirname));
387 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRbadpath));
389 status = smbcli_chkpath(cli_nt->tree,
390 talloc_asprintf(mem_ctx, "%s\\bla\\blub",
392 CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_NOT_FOUND);
393 status = smbcli_chkpath(cli_dos->tree,
394 talloc_asprintf(mem_ctx, "%s\\bla\\blub",
396 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRbadpath));
398 if (!(fpath = talloc_asprintf(mem_ctx, "%s\\%s", dirname, fname))) {
401 fnum = smbcli_open(cli_nt->tree, fpath, O_RDWR | O_CREAT, DENY_NONE);
403 d_printf("Could not create file %s: %s\n", fpath,
404 smbcli_errstr(cli_nt->tree));
407 smbcli_close(cli_nt->tree, fnum);
410 * Do a whole bunch of error code checks on chkpath
413 status = smbcli_chkpath(cli_nt->tree, fpath);
414 CHECK_STATUS(status, NT_STATUS_NOT_A_DIRECTORY);
415 status = smbcli_chkpath(cli_dos->tree, fpath);
416 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRbadpath));
418 status = smbcli_chkpath(cli_nt->tree, "..");
419 CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
420 status = smbcli_chkpath(cli_dos->tree, "..");
421 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidpath));
423 status = smbcli_chkpath(cli_nt->tree, ".");
424 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
425 status = smbcli_chkpath(cli_dos->tree, ".");
426 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRbadpath));
428 status = smbcli_chkpath(cli_nt->tree, "\t");
429 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
430 status = smbcli_chkpath(cli_dos->tree, "\t");
431 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRbadpath));
433 status = smbcli_chkpath(cli_nt->tree, "\t\\bla");
434 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
435 status = smbcli_chkpath(cli_dos->tree, "\t\\bla");
436 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRbadpath));
438 status = smbcli_chkpath(cli_nt->tree, "<");
439 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
440 status = smbcli_chkpath(cli_dos->tree, "<");
441 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRbadpath));
443 status = smbcli_chkpath(cli_nt->tree, "<\\bla");
444 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
445 status = smbcli_chkpath(cli_dos->tree, "<\\bla");
446 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRbadpath));
449 * .... And the same gang against getatr. Note that the DOS error codes
453 status = smbcli_getatr(cli_nt->tree, fpath, NULL, NULL, NULL);
454 CHECK_STATUS(status, NT_STATUS_OK);
455 status = smbcli_getatr(cli_dos->tree, fpath, NULL, NULL, NULL);
456 CHECK_STATUS(status, NT_STATUS_OK);
458 status = smbcli_getatr(cli_nt->tree, "..", NULL, NULL, NULL);
459 CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
460 status = smbcli_getatr(cli_dos->tree, "..", NULL, NULL, NULL);
461 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidpath));
463 status = smbcli_getatr(cli_nt->tree, ".", NULL, NULL, NULL);
464 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
465 status = smbcli_getatr(cli_dos->tree, ".", NULL, NULL, NULL);
466 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidname));
468 status = smbcli_getatr(cli_nt->tree, "\t", NULL, NULL, NULL);
469 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
470 status = smbcli_getatr(cli_dos->tree, "\t", NULL, NULL, NULL);
471 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidname));
473 status = smbcli_getatr(cli_nt->tree, "\t\\bla", NULL, NULL, NULL);
474 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
475 status = smbcli_getatr(cli_dos->tree, "\t\\bla", NULL, NULL, NULL);
476 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidname));
478 status = smbcli_getatr(cli_nt->tree, "<", NULL, NULL, NULL);
479 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
480 status = smbcli_getatr(cli_dos->tree, "<", NULL, NULL, NULL);
481 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidname));
483 status = smbcli_getatr(cli_nt->tree, "<\\bla", NULL, NULL, NULL);
484 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
485 status = smbcli_getatr(cli_dos->tree, "<\\bla", NULL, NULL, NULL);
486 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidname));
488 /* Try the same set with openX. */
490 status = raw_smbcli_open(cli_nt->tree, "..", O_RDONLY, DENY_NONE, NULL);
491 CHECK_STATUS(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
492 status = raw_smbcli_open(cli_dos->tree, "..", O_RDONLY, DENY_NONE, NULL);
493 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidpath));
495 status = raw_smbcli_open(cli_nt->tree, ".", O_RDONLY, DENY_NONE, NULL);
496 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
497 status = raw_smbcli_open(cli_dos->tree, ".", O_RDONLY, DENY_NONE, NULL);
498 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidname));
500 status = raw_smbcli_open(cli_nt->tree, "\t", O_RDONLY, DENY_NONE, NULL);
501 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
502 status = raw_smbcli_open(cli_dos->tree, "\t", O_RDONLY, DENY_NONE, NULL);
503 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidname));
505 status = raw_smbcli_open(cli_nt->tree, "\t\\bla", O_RDONLY, DENY_NONE, NULL);
506 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
507 status = raw_smbcli_open(cli_dos->tree, "\t\\bla", O_RDONLY, DENY_NONE, NULL);
508 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidname));
510 status = raw_smbcli_open(cli_nt->tree, "<", O_RDONLY, DENY_NONE, NULL);
511 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
512 status = raw_smbcli_open(cli_dos->tree, "<", O_RDONLY, DENY_NONE, NULL);
513 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidname));
515 status = raw_smbcli_open(cli_nt->tree, "<\\bla", O_RDONLY, DENY_NONE, NULL);
516 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
517 status = raw_smbcli_open(cli_dos->tree, "<\\bla", O_RDONLY, DENY_NONE, NULL);
518 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRinvalidname));
520 /* Let's test EEXIST error code mapping. */
521 status = raw_smbcli_open(cli_nt->tree, fpath, O_RDONLY | O_CREAT| O_EXCL, DENY_NONE, NULL);
522 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_COLLISION);
523 status = raw_smbcli_open(cli_dos->tree, fpath, O_RDONLY | O_CREAT| O_EXCL, DENY_NONE, NULL);
524 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS,ERRfilexists));
526 status = raw_smbcli_t2open(cli_nt->tree, fpath, O_RDONLY | O_CREAT| O_EXCL, DENY_NONE, NULL);
527 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_COLLISION);
528 status = raw_smbcli_t2open(cli_dos->tree, fpath, O_RDONLY | O_CREAT| O_EXCL, DENY_NONE, NULL);
529 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS,ERRfilexists));
531 status = raw_smbcli_ntcreate(cli_nt->tree, fpath, NULL);
532 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_COLLISION);
533 status = raw_smbcli_ntcreate(cli_dos->tree, fpath, NULL);
534 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS,ERRfilexists));
542 if (cli_nt != NULL) {
543 smbcli_deltree(cli_nt->tree, dirname);
544 torture_close_connection(cli_nt);
546 if (cli_dos != NULL) {
547 torture_close_connection(cli_dos);
549 talloc_free(mem_ctx);