s4:dsdb/subtree_delete: do an early return and avoid some nesting
[Samba/gebeck_regimport.git] / source4 / torture / raw / samba3hide.c
blobf980ffceb6dc78571cd480762ae1fa5176600b99
1 /*
2 Unix SMB/CIFS implementation.
3 Test samba3 hide unreadable/unwriteable
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/>.
20 #include "includes.h"
21 #include "system/time.h"
22 #include "system/filesys.h"
23 #include "libcli/libcli.h"
24 #include "torture/util.h"
25 #include "torture/raw/proto.h"
27 static void init_unixinfo_nochange(union smb_setfileinfo *info)
29 ZERO_STRUCTP(info);
30 info->unix_basic.level = RAW_SFILEINFO_UNIX_BASIC;
31 info->unix_basic.in.mode = SMB_MODE_NO_CHANGE;
33 info->unix_basic.in.end_of_file = SMB_SIZE_NO_CHANGE_HI;
34 info->unix_basic.in.end_of_file <<= 32;
35 info->unix_basic.in.end_of_file |= SMB_SIZE_NO_CHANGE_LO;
37 info->unix_basic.in.num_bytes = SMB_SIZE_NO_CHANGE_HI;
38 info->unix_basic.in.num_bytes <<= 32;
39 info->unix_basic.in.num_bytes |= SMB_SIZE_NO_CHANGE_LO;
41 info->unix_basic.in.status_change_time = SMB_TIME_NO_CHANGE_HI;
42 info->unix_basic.in.status_change_time <<= 32;
43 info->unix_basic.in.status_change_time |= SMB_TIME_NO_CHANGE_LO;
45 info->unix_basic.in.access_time = SMB_TIME_NO_CHANGE_HI;
46 info->unix_basic.in.access_time <<= 32;
47 info->unix_basic.in.access_time |= SMB_TIME_NO_CHANGE_LO;
49 info->unix_basic.in.change_time = SMB_TIME_NO_CHANGE_HI;
50 info->unix_basic.in.change_time <<= 32;
51 info->unix_basic.in.change_time |= SMB_TIME_NO_CHANGE_LO;
53 info->unix_basic.in.uid = SMB_UID_NO_CHANGE;
54 info->unix_basic.in.gid = SMB_GID_NO_CHANGE;
57 struct list_state {
58 const char *fname;
59 bool visible;
62 static void set_visible(struct clilist_file_info *i, const char *mask,
63 void *priv)
65 struct list_state *state = (struct list_state *)priv;
67 if (strcasecmp_m(state->fname, i->name) == 0)
68 state->visible = true;
71 static bool is_visible(struct smbcli_tree *tree, const char *fname)
73 struct list_state state;
75 state.visible = false;
76 state.fname = fname;
78 if (smbcli_list(tree, "*.*", 0, set_visible, &state) < 0) {
79 return false;
81 return state.visible;
84 static bool is_readable(struct smbcli_tree *tree, const char *fname)
86 int fnum;
87 fnum = smbcli_open(tree, fname, O_RDONLY, DENY_NONE);
88 if (fnum < 0) {
89 return false;
91 smbcli_close(tree, fnum);
92 return true;
95 static bool is_writeable(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
96 const char *fname)
98 int fnum;
99 fnum = smbcli_open(tree, fname, O_WRONLY, DENY_NONE);
100 if (fnum < 0) {
101 return false;
103 smbcli_close(tree, fnum);
104 return true;
108 * This is not an exact method because there's a ton of reasons why a getatr
109 * might fail. But for our purposes it's sufficient.
112 static bool smbcli_file_exists(struct smbcli_tree *tree, const char *fname)
114 return NT_STATUS_IS_OK(smbcli_getatr(tree, fname, NULL, NULL, NULL));
117 static NTSTATUS smbcli_setup_unix(struct smbcli_tree *tree)
119 union smb_fsinfo fsinfo;
120 union smb_setfsinfo set_fsinfo;
121 NTSTATUS status;
123 ZERO_STRUCT(fsinfo);
124 ZERO_STRUCT(set_fsinfo);
126 fsinfo.generic.level = RAW_QFS_UNIX_INFO;
127 status = smb_raw_fsinfo(tree, NULL, &fsinfo);
128 if (!NT_STATUS_IS_OK(status)) {
129 printf("smb_raw_fsinfo failed %s\n",
130 nt_errstr(status));
131 return status;
134 set_fsinfo.generic.level = RAW_SETFS_UNIX_INFO;
135 set_fsinfo.unix_info.in.major_version = fsinfo.unix_info.out.major_version;
136 set_fsinfo.unix_info.in.minor_version = fsinfo.unix_info.out.minor_version;
137 set_fsinfo.unix_info.in.capability = fsinfo.unix_info.out.capability;
139 status = smb_raw_setfsinfo(tree, NULL, &set_fsinfo);
140 if (!NT_STATUS_IS_OK(status)) {
141 printf("smb_raw_setfsinfo failed %s\n",
142 nt_errstr(status));
144 return status;
147 static NTSTATUS smbcli_chmod(struct smbcli_tree *tree, const char *fname,
148 uint64_t permissions)
150 union smb_setfileinfo sfinfo;
151 init_unixinfo_nochange(&sfinfo);
152 sfinfo.unix_basic.in.file.path = fname;
153 sfinfo.unix_basic.in.permissions = permissions;
154 return smb_raw_setpathinfo(tree, &sfinfo);
157 bool torture_samba3_hide(struct torture_context *torture, struct smbcli_state *cli)
159 const char *fname = "test.txt";
160 int fnum;
161 NTSTATUS status;
162 struct smbcli_tree *hideunread;
163 struct smbcli_tree *hideunwrite;
165 status = smbcli_setup_unix(cli->tree);
166 if (!NT_STATUS_IS_OK(status)) {
167 torture_fail(torture,
168 talloc_asprintf(torture, "smbcli_setup_unix failed %s\n",
169 nt_errstr(status)));
172 status = torture_second_tcon(torture, cli->session, "hideunread",
173 &hideunread);
174 torture_assert_ntstatus_ok(torture, status, "second_tcon(hideunread) failed\n");
176 status = torture_second_tcon(torture, cli->session, "hideunwrite",
177 &hideunwrite);
178 torture_assert_ntstatus_ok(torture, status, "second_tcon(hideunwrite) failed\n");
180 status = smbcli_unlink(cli->tree, fname);
181 if (NT_STATUS_EQUAL(status, NT_STATUS_CANNOT_DELETE)) {
182 smbcli_setatr(cli->tree, fname, 0, -1);
183 smbcli_unlink(cli->tree, fname);
186 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
187 if (fnum == -1) {
188 torture_fail(torture,
189 talloc_asprintf(torture, "Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree)));
192 smbcli_close(cli->tree, fnum);
194 if (!smbcli_file_exists(cli->tree, fname)) {
195 torture_fail(torture, talloc_asprintf(torture, "%s does not exist\n", fname));
198 /* R/W file should be visible everywhere */
200 status = smbcli_chmod(cli->tree, fname, UNIX_R_USR|UNIX_W_USR);
201 torture_assert_ntstatus_ok(torture, status, "smbcli_chmod failed\n");
203 if (!is_writeable(torture, cli->tree, fname)) {
204 torture_fail(torture, "File not writable\n");
206 if (!is_readable(cli->tree, fname)) {
207 torture_fail(torture, "File not readable\n");
209 if (!is_visible(cli->tree, fname)) {
210 torture_fail(torture, "r/w file not visible via normal share\n");
212 if (!is_visible(hideunread, fname)) {
213 torture_fail(torture, "r/w file not visible via hide unreadable\n");
215 if (!is_visible(hideunwrite, fname)) {
216 torture_fail(torture, "r/w file not visible via hide unwriteable\n");
219 /* R/O file should not be visible via hide unwriteable files */
221 status = smbcli_chmod(cli->tree, fname, UNIX_R_USR);
222 torture_assert_ntstatus_ok(torture, status, "smbcli_chmod failed\n");
224 if (is_writeable(torture, cli->tree, fname)) {
225 torture_fail(torture, "r/o is writable\n");
227 if (!is_readable(cli->tree, fname)) {
228 torture_fail(torture, "r/o not readable\n");
230 if (!is_visible(cli->tree, fname)) {
231 torture_fail(torture, "r/o file not visible via normal share\n");
233 if (!is_visible(hideunread, fname)) {
234 torture_fail(torture, "r/o file not visible via hide unreadable\n");
236 if (is_visible(hideunwrite, fname)) {
237 torture_fail(torture, "r/o file visible via hide unwriteable\n");
240 /* inaccessible file should be only visible on normal share */
242 status = smbcli_chmod(cli->tree, fname, 0);
243 torture_assert_ntstatus_ok(torture, status, "smbcli_chmod failed\n");
245 if (is_writeable(torture, cli->tree, fname)) {
246 torture_fail(torture, "inaccessible file is writable\n");
248 if (is_readable(cli->tree, fname)) {
249 torture_fail(torture, "inaccessible file is readable\n");
251 if (!is_visible(cli->tree, fname)) {
252 torture_fail(torture, "inaccessible file not visible via normal share\n");
254 if (is_visible(hideunread, fname)) {
255 torture_fail(torture, "inaccessible file visible via hide unreadable\n");
257 if (is_visible(hideunwrite, fname)) {
258 torture_fail(torture, "inaccessible file visible via hide unwriteable\n");
261 smbcli_chmod(cli->tree, fname, UNIX_R_USR|UNIX_W_USR);
262 smbcli_unlink(cli->tree, fname);
264 return true;
268 * Try to force smb_close to return an error. The only way I can think of is
269 * to open a file with delete on close, chmod the parent dir to 000 and then
270 * close. smb_close should return NT_STATUS_ACCESS_DENIED.
273 bool torture_samba3_closeerr(struct torture_context *tctx, struct smbcli_state *cli)
275 bool result = false;
276 NTSTATUS status;
277 const char *dname = "closeerr.dir";
278 const char *fname = "closeerr.dir\\closerr.txt";
279 int fnum;
281 smbcli_deltree(cli->tree, dname);
283 torture_assert_ntstatus_ok(
284 tctx, smbcli_mkdir(cli->tree, dname),
285 talloc_asprintf(tctx, "smbcli_mdir failed: (%s)\n",
286 smbcli_errstr(cli->tree)));
288 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR,
289 DENY_NONE);
290 torture_assert(tctx, fnum != -1,
291 talloc_asprintf(tctx, "smbcli_open failed: %s\n",
292 smbcli_errstr(cli->tree)));
293 smbcli_close(cli->tree, fnum);
295 fnum = smbcli_nt_create_full(cli->tree, fname, 0,
296 SEC_RIGHTS_FILE_ALL,
297 FILE_ATTRIBUTE_NORMAL,
298 NTCREATEX_SHARE_ACCESS_DELETE,
299 NTCREATEX_DISP_OPEN, 0, 0);
301 torture_assert(tctx, fnum != -1,
302 talloc_asprintf(tctx, "smbcli_open failed: %s\n",
303 smbcli_errstr(cli->tree)));
305 status = smbcli_nt_delete_on_close(cli->tree, fnum, true);
307 torture_assert_ntstatus_ok(tctx, status,
308 "setting delete_on_close on file failed !");
310 status = smbcli_chmod(cli->tree, dname, 0);
312 torture_assert_ntstatus_ok(tctx, status,
313 "smbcli_chmod on file failed !");
315 status = smbcli_close(cli->tree, fnum);
317 smbcli_chmod(cli->tree, dname, UNIX_R_USR|UNIX_W_USR|UNIX_X_USR);
318 smbcli_deltree(cli->tree, dname);
320 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_ACCESS_DENIED,
321 "smbcli_close");
323 result = true;
325 return result;