2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1997-2003
5 Copyright (C) Jelmer Vernooij 2006
6 Copyright (C) David Mulder 2020
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25 #include "torture/smbtorture.h"
26 #include "torture/smb2/proto.h"
27 #include "libcli/smb/smbXcli_base.h"
28 #include "torture/util.h"
29 #include "system/filesys.h"
30 #include "system/time.h"
31 #include "libcli/resolve/resolve.h"
32 #include "lib/events/events.h"
33 #include "param/param.h"
35 static void smb2cli_session_set_id(struct smbXcli_session
*session
,
38 smb2cli_session_set_id_and_flags(session
, session_id
,
39 smb2cli_session_get_flags(session
));
43 this checks to see if a secondary tconx can use open files from an
46 bool run_tcon_test(struct torture_context
*tctx
, struct smb2_tree
*tree
)
48 const char *fname
= "tcontest.tmp";
49 struct smb2_handle fnum1
;
50 uint32_t cnum1
, cnum2
, cnum3
;
51 uint64_t sessid1
, sessid2
;
54 struct smb2_tree
*tree1
= NULL
;
55 const char *host
= torture_setting_string(tctx
, "host", NULL
);
56 struct smb2_create io
= {0};
60 if (smb2_deltree(tree
, fname
) == -1) {
61 torture_comment(tctx
, "unlink of %s failed\n", fname
);
65 io
.in
.desired_access
= SEC_FILE_READ_DATA
| SEC_FILE_WRITE_DATA
;
66 io
.in
.create_disposition
= NTCREATEX_DISP_CREATE
;
67 io
.in
.share_access
= NTCREATEX_SHARE_ACCESS_READ
|
68 NTCREATEX_SHARE_ACCESS_WRITE
|
69 NTCREATEX_SHARE_ACCESS_DELETE
;
70 status
= smb2_create(tree
, tree
, &io
);
71 if (NT_STATUS_IS_ERR(status
)) {
72 torture_result(tctx
, TORTURE_FAIL
, "open of %s failed (%s)\n", fname
, nt_errstr(status
));
75 fnum1
= io
.out
.file
.handle
;
77 cnum1
= smb2cli_tcon_current_id(tree
->smbXcli
);
78 sessid1
= smb2cli_session_current_id(tree
->session
->smbXcli
);
80 memset(buf
, 0, 4); /* init buf so valgrind won't complain */
81 status
= smb2_util_write(tree
, fnum1
, buf
, 130, 4);
82 if (NT_STATUS_IS_ERR(status
)) {
83 torture_result(tctx
, TORTURE_FAIL
, "initial write failed (%s)\n", nt_errstr(status
));
87 ok
= torture_smb2_tree_connect(tctx
, tree
->session
, tctx
, &tree1
);
89 torture_result(tctx
, TORTURE_FAIL
, "%s refused 2nd tree connect\n", host
);
93 cnum2
= smb2cli_tcon_current_id(tree1
->smbXcli
);
94 cnum3
= MAX(cnum1
, cnum2
) + 1; /* any invalid number */
95 sessid2
= smb2cli_session_current_id(tree1
->session
->smbXcli
) + 1;
97 /* try a write with the wrong tid */
98 smb2cli_tcon_set_id(tree1
->smbXcli
, cnum2
);
100 status
= smb2_util_write(tree1
, fnum1
, buf
, 130, 4);
101 if (NT_STATUS_IS_OK(status
)) {
102 torture_result(tctx
, TORTURE_FAIL
, "* server allows write with wrong TID\n");
105 torture_comment(tctx
, "server fails write with wrong TID : %s\n", nt_errstr(status
));
109 /* try a write with an invalid tid */
110 smb2cli_tcon_set_id(tree1
->smbXcli
, cnum3
);
112 status
= smb2_util_write(tree1
, fnum1
, buf
, 130, 4);
113 if (NT_STATUS_IS_OK(status
)) {
114 torture_result(tctx
, TORTURE_FAIL
, "* server allows write with invalid TID\n");
117 torture_comment(tctx
, "server fails write with invalid TID : %s\n", nt_errstr(status
));
120 /* try a write with an invalid session id */
121 smb2cli_session_set_id(tree1
->session
->smbXcli
, sessid2
);
122 smb2cli_tcon_set_id(tree1
->smbXcli
, cnum1
);
124 status
= smb2_util_write(tree1
, fnum1
, buf
, 130, 4);
125 if (NT_STATUS_IS_OK(status
)) {
126 torture_result(tctx
, TORTURE_FAIL
, "* server allows write with invalid VUID\n");
129 torture_comment(tctx
, "server fails write with invalid VUID : %s\n", nt_errstr(status
));
132 smb2cli_session_set_id(tree1
->session
->smbXcli
, sessid1
);
133 smb2cli_tcon_set_id(tree1
->smbXcli
, cnum1
);
135 status
= smb2_util_close(tree1
, fnum1
);
136 if (NT_STATUS_IS_ERR(status
)) {
137 torture_result(tctx
, TORTURE_FAIL
, "close failed (%s)\n", nt_errstr(status
));
141 smb2cli_tcon_set_id(tree1
->smbXcli
, cnum2
);
143 smb2_util_unlink(tree1
, fname
);