1 /*
2    Unix SMB/CIFS implementation.
3 
4    SMB2 setinfo individual test suite
5 
6    Copyright (C) Ralph Boehme 2016
7 
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.
12 
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.
17 
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/>.
20 */
21 
22 #include "includes.h"
23 #include "system/time.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 
27 #include "torture/torture.h"
28 #include "torture/smb2/proto.h"
29 
30 /*
31   test dosmode and hidden files
32 */
torture_smb2_dosmode(struct torture_context * tctx)33 bool torture_smb2_dosmode(struct torture_context *tctx)
34 {
35 	bool ret = true;
36 	NTSTATUS status;
37 	struct smb2_tree *tree = NULL;
38 	const char *dname = "torture_dosmode";
39 	const char *fname = "torture_dosmode\\file";
40 	const char *hidefile = "torture_dosmode\\hidefile";
41 	const char *dotfile = "torture_dosmode\\.dotfile";
42 	struct smb2_handle h1 = {{0}};
43 	struct smb2_create io;
44 	union smb_setfileinfo sfinfo;
45 	union smb_fileinfo finfo2;
46 
47 	torture_comment(tctx, "Checking dosmode with \"hide files\" "
48 			"and \"hide dot files\"\n");
49 
50 	if (!torture_smb2_connection(tctx, &tree)) {
51 		return false;
52 	}
53 
54 	smb2_deltree(tree, dname);
55 
56 	status = torture_smb2_testdir(tree, dname, &h1);
57 	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
58 					"torture_smb2_testdir failed");
59 
60 	ZERO_STRUCT(io);
61 	io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
62 	io.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
63 	io.in.create_disposition = NTCREATEX_DISP_CREATE;
64 	io.in.create_options = 0;
65 	io.in.fname = fname;
66 
67 	status = smb2_create(tree, tctx, &io);
68 	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
69 					"smb2_create failed");
70 
71 	ZERO_STRUCT(sfinfo);
72 	sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_HIDDEN;
73 	sfinfo.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
74 	sfinfo.generic.in.file.handle = io.out.file.handle;
75 	status = smb2_setinfo_file(tree, &sfinfo);
76 	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
77 					"smb2_setinfo_filefailed");
78 
79 	ZERO_STRUCT(finfo2);
80 	finfo2.generic.level = RAW_FILEINFO_BASIC_INFORMATION;
81 	finfo2.generic.in.file.handle = io.out.file.handle;
82 	status = smb2_getinfo_file(tree, tctx, &finfo2);
83 	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
84 					"smb2_getinfo_file failed");
85 	torture_assert_int_equal_goto(tctx, finfo2.all_info2.out.attrib & FILE_ATTRIBUTE_HIDDEN,
86 				      FILE_ATTRIBUTE_HIDDEN, ret, done,
87 				      "FILE_ATTRIBUTE_HIDDEN is not set");
88 
89 	smb2_util_close(tree, io.out.file.handle);
90 
91 	/* This must fail with attribute mismatch */
92 	ZERO_STRUCT(io);
93 	io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
94 	io.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
95 	io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
96 	io.in.create_options = 0;
97 	io.in.fname = fname;
98 
99 	status = smb2_create(tree, tctx, &io);
100 	torture_assert_ntstatus_equal_goto(tctx, status, NT_STATUS_ACCESS_DENIED,
101 					   ret, done,"smb2_create failed");
102 
103 	/* Create a file in "hide files" */
104 	ZERO_STRUCT(io);
105 	io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
106 	io.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
107 	io.in.create_disposition = NTCREATEX_DISP_CREATE;
108 	io.in.create_options = 0;
109 	io.in.fname = hidefile;
110 
111 	status = smb2_create(tree, tctx, &io);
112 	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
113 					"smb2_create failed");
114 
115 	ZERO_STRUCT(finfo2);
116 	finfo2.generic.level = RAW_FILEINFO_BASIC_INFORMATION;
117 	finfo2.generic.in.file.handle = io.out.file.handle;
118 	status = smb2_getinfo_file(tree, tctx, &finfo2);
119 	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
120 					"smb2_getinfo_file failed");
121 	torture_assert_int_equal_goto(tctx, finfo2.all_info2.out.attrib & FILE_ATTRIBUTE_HIDDEN,
122 				      FILE_ATTRIBUTE_HIDDEN, ret, done,
123 				      "FILE_ATTRIBUTE_HIDDEN is not set");
124 
125 	smb2_util_close(tree, io.out.file.handle);
126 
127 	/* Overwrite a file in "hide files", should pass */
128 	ZERO_STRUCT(io);
129 	io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
130 	io.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
131 	io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
132 	io.in.create_options = 0;
133 	io.in.fname = hidefile;
134 
135 	status = smb2_create(tree, tctx, &io);
136 	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
137 					"smb2_create failed");
138 	smb2_util_close(tree, io.out.file.handle);
139 
140 	/* Create a "hide dot files" */
141 	ZERO_STRUCT(io);
142 	io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
143 	io.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
144 	io.in.create_disposition = NTCREATEX_DISP_CREATE;
145 	io.in.create_options = 0;
146 	io.in.fname = dotfile;
147 
148 	status = smb2_create(tree, tctx, &io);
149 	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
150 					"smb2_create failed");
151 
152 	ZERO_STRUCT(finfo2);
153 	finfo2.generic.level = RAW_FILEINFO_BASIC_INFORMATION;
154 	finfo2.generic.in.file.handle = io.out.file.handle;
155 	status = smb2_getinfo_file(tree, tctx, &finfo2);
156 	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
157 					"smb2_getinfo_file failed");
158 	torture_assert_int_equal_goto(tctx, finfo2.all_info2.out.attrib & FILE_ATTRIBUTE_HIDDEN,
159 				      FILE_ATTRIBUTE_HIDDEN, ret, done,
160 				      "FILE_ATTRIBUTE_HIDDEN is not set");
161 
162 	smb2_util_close(tree, io.out.file.handle);
163 
164 	/* Overwrite a "hide dot files", should pass */
165 	ZERO_STRUCT(io);
166 	io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
167 	io.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
168 	io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
169 	io.in.create_options = 0;
170 	io.in.fname = dotfile;
171 
172 	status = smb2_create(tree, tctx, &io);
173 	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
174 					"smb2_create failed");
175 	smb2_util_close(tree, io.out.file.handle);
176 
177 done:
178 	if (!smb2_util_handle_empty(h1)) {
179 		smb2_util_close(tree, h1);
180 	}
181 	smb2_deltree(tree, dname);
182 	return ret;
183 }
184