Fix samba3.raw.samba3hide test - ensure we set up POSIX capabilities
[Samba.git] / source4 / torture / raw / samba3hide.c
blobc3a572c21cf7608af0d7ad201b914b2fffe64768
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)
159 struct smbcli_state *cli;
160 const char *fname = "test.txt";
161 int fnum;
162 NTSTATUS status;
163 struct smbcli_tree *hideunread;
164 struct smbcli_tree *hideunwrite;
166 if (!torture_open_connection_share(
167 torture, &cli, torture, torture_setting_string(torture, "host", NULL),
168 torture_setting_string(torture, "share", NULL), torture->ev)) {
169 torture_fail(torture, "torture_open_connection_share failed\n");
172 status = smbcli_setup_unix(cli->tree);
173 if (!NT_STATUS_IS_OK(status)) {
174 torture_fail(torture,
175 talloc_asprintf(torture, "smbcli_setup_unix failed %s\n",
176 nt_errstr(status)));
179 status = torture_second_tcon(torture, cli->session, "hideunread",
180 &hideunread);
181 torture_assert_ntstatus_ok(torture, status, "second_tcon(hideunread) failed\n");
183 status = torture_second_tcon(torture, cli->session, "hideunwrite",
184 &hideunwrite);
185 torture_assert_ntstatus_ok(torture, status, "second_tcon(hideunwrite) failed\n");
187 status = smbcli_unlink(cli->tree, fname);
188 if (NT_STATUS_EQUAL(status, NT_STATUS_CANNOT_DELETE)) {
189 smbcli_setatr(cli->tree, fname, 0, -1);
190 smbcli_unlink(cli->tree, fname);
193 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
194 if (fnum == -1) {
195 torture_fail(torture,
196 talloc_asprintf(torture, "Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree)));
199 smbcli_close(cli->tree, fnum);
201 if (!smbcli_file_exists(cli->tree, fname)) {
202 torture_fail(torture, talloc_asprintf(torture, "%s does not exist\n", fname));
205 /* R/W file should be visible everywhere */
207 status = smbcli_chmod(cli->tree, fname, UNIX_R_USR|UNIX_W_USR);
208 torture_assert_ntstatus_ok(torture, status, "smbcli_chmod failed\n");
210 if (!is_writeable(torture, cli->tree, fname)) {
211 torture_fail(torture, "File not writable\n");
213 if (!is_readable(cli->tree, fname)) {
214 torture_fail(torture, "File not readable\n");
216 if (!is_visible(cli->tree, fname)) {
217 torture_fail(torture, "r/w file not visible via normal share\n");
219 if (!is_visible(hideunread, fname)) {
220 torture_fail(torture, "r/w file not visible via hide unreadable\n");
222 if (!is_visible(hideunwrite, fname)) {
223 torture_fail(torture, "r/w file not visible via hide unwriteable\n");
226 /* R/O file should not be visible via hide unwriteable files */
228 status = smbcli_chmod(cli->tree, fname, UNIX_R_USR);
229 torture_assert_ntstatus_ok(torture, status, "smbcli_chmod failed\n");
231 if (is_writeable(torture, cli->tree, fname)) {
232 torture_fail(torture, "r/o is writable\n");
234 if (!is_readable(cli->tree, fname)) {
235 torture_fail(torture, "r/o not readable\n");
237 if (!is_visible(cli->tree, fname)) {
238 torture_fail(torture, "r/o file not visible via normal share\n");
240 if (!is_visible(hideunread, fname)) {
241 torture_fail(torture, "r/o file not visible via hide unreadable\n");
243 if (is_visible(hideunwrite, fname)) {
244 torture_fail(torture, "r/o file visible via hide unwriteable\n");
247 /* inaccessible file should be only visible on normal share */
249 status = smbcli_chmod(cli->tree, fname, 0);
250 torture_assert_ntstatus_ok(torture, status, "smbcli_chmod failed\n");
252 if (is_writeable(torture, cli->tree, fname)) {
253 torture_fail(torture, "inaccessible file is writable\n");
255 if (is_readable(cli->tree, fname)) {
256 torture_fail(torture, "inaccessible file is readable\n");
258 if (!is_visible(cli->tree, fname)) {
259 torture_fail(torture, "inaccessible file not visible via normal share\n");
261 if (is_visible(hideunread, fname)) {
262 torture_fail(torture, "inaccessible file visible via hide unreadable\n");
264 if (is_visible(hideunwrite, fname)) {
265 torture_fail(torture, "inaccessible file visible via hide unwriteable\n");
268 smbcli_chmod(cli->tree, fname, UNIX_R_USR|UNIX_W_USR);
269 smbcli_unlink(cli->tree, fname);
271 return true;
275 * Try to force smb_close to return an error. The only way I can think of is
276 * to open a file with delete on close, chmod the parent dir to 000 and then
277 * close. smb_close should return NT_STATUS_ACCESS_DENIED.
280 bool torture_samba3_closeerr(struct torture_context *tctx)
282 struct smbcli_state *cli = NULL;
283 bool result = false;
284 NTSTATUS status;
285 const char *dname = "closeerr.dir";
286 const char *fname = "closeerr.dir\\closerr.txt";
287 int fnum;
289 if (!torture_open_connection(&cli, tctx, 0)) {
290 goto fail;
293 smbcli_deltree(cli->tree, dname);
295 torture_assert_ntstatus_ok(
296 tctx, smbcli_mkdir(cli->tree, dname),
297 talloc_asprintf(tctx, "smbcli_mdir failed: (%s)\n",
298 smbcli_errstr(cli->tree)));
300 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR,
301 DENY_NONE);
302 torture_assert(tctx, fnum != -1,
303 talloc_asprintf(tctx, "smbcli_open failed: %s\n",
304 smbcli_errstr(cli->tree)));
305 smbcli_close(cli->tree, fnum);
307 fnum = smbcli_nt_create_full(cli->tree, fname, 0,
308 SEC_RIGHTS_FILE_ALL,
309 FILE_ATTRIBUTE_NORMAL,
310 NTCREATEX_SHARE_ACCESS_DELETE,
311 NTCREATEX_DISP_OPEN, 0, 0);
313 torture_assert(tctx, fnum != -1,
314 talloc_asprintf(tctx, "smbcli_open failed: %s\n",
315 smbcli_errstr(cli->tree)));
317 status = smbcli_nt_delete_on_close(cli->tree, fnum, true);
319 torture_assert_ntstatus_ok(tctx, status,
320 "setting delete_on_close on file failed !");
322 status = smbcli_chmod(cli->tree, dname, 0);
324 torture_assert_ntstatus_ok(tctx, status,
325 "smbcli_chmod on file failed !");
327 status = smbcli_close(cli->tree, fnum);
329 smbcli_chmod(cli->tree, dname, UNIX_R_USR|UNIX_W_USR|UNIX_X_USR);
330 smbcli_deltree(cli->tree, dname);
332 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_ACCESS_DENIED,
333 "smbcli_close");
335 result = true;
337 fail:
338 if (cli) {
339 torture_close_connection(cli);
341 return result;