719e8f0b0151b4902aa4893e0f1142072cbfdc4d
[samba.git] / source4 / torture / smb2 / setinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 setinfo individual test suite
5
6    Copyright (C) Andrew Tridgell 2005
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 #include "libcli/security/security.h"
31 #include "librpc/gen_ndr/ndr_security.h"
32
33 #define BASEDIR ""
34
35 #define FAIL_UNLESS(__cond)                                     \
36         do {                                                    \
37                 if (__cond) {} else {                           \
38                         torture_result(tctx, TORTURE_FAIL, "%s) condition violated: %s\n",      \
39                                __location__, #__cond);          \
40                         ret = false; goto done;                 \
41                 }                                               \
42         } while(0)
43
44 /* basic testing of all SMB2 setinfo calls 
45    for each call we test that it succeeds, and where possible test 
46    for consistency between the calls. 
47 */
48 bool torture_smb2_setinfo(struct torture_context *tctx)
49 {
50         struct smb2_tree *tree;
51         bool ret = true;
52         struct smb2_handle handle;
53         char *fname;
54         union smb_fileinfo finfo2;
55         union smb_setfileinfo sfinfo;
56         struct security_ace ace;
57         struct security_descriptor *sd;
58         struct dom_sid *test_sid;
59         NTSTATUS status, status2=NT_STATUS_OK;
60         const char *call_name;
61         time_t basetime = (time(NULL) - 86400) & ~1;
62         int n = time(NULL) % 100;
63         
64         ZERO_STRUCT(handle);
65         
66         fname = talloc_asprintf(tctx, BASEDIR "fnum_test_%d.txt", n);
67
68         if (!torture_smb2_connection(tctx, &tree)) {
69                 return false;
70         }
71
72 #define RECREATE_FILE(fname) do { \
73         smb2_util_close(tree, handle); \
74         status = smb2_create_complex_file(tree, fname, &handle); \
75         if (!NT_STATUS_IS_OK(status)) { \
76                 torture_result(tctx, TORTURE_FAIL, "(%s) ERROR: open of %s failed (%s)\n", \
77                        __location__, fname, nt_errstr(status)); \
78                 ret = false; \
79                 goto done; \
80         }} while (0)
81
82 #define RECREATE_BOTH do { \
83                 RECREATE_FILE(fname); \
84         } while (0)
85
86         RECREATE_BOTH;
87         
88 #define CHECK_CALL(call, rightstatus) do { \
89         call_name = #call; \
90         sfinfo.generic.level = RAW_SFILEINFO_ ## call; \
91         sfinfo.generic.in.file.handle = handle; \
92         status = smb2_setinfo_file(tree, &sfinfo); \
93         if (!NT_STATUS_EQUAL(status, rightstatus)) { \
94                 torture_result(tctx, TORTURE_FAIL, "(%s) %s - %s (should be %s)\n", __location__, #call, \
95                         nt_errstr(status), nt_errstr(rightstatus)); \
96                 ret = false; \
97                 goto done; \
98         } \
99         } while (0)
100
101 #define CHECK1(call) \
102         do { if (NT_STATUS_IS_OK(status)) { \
103                 finfo2.generic.level = RAW_FILEINFO_ ## call; \
104                 finfo2.generic.in.file.handle = handle; \
105                 status2 = smb2_getinfo_file(tree, tctx, &finfo2); \
106                 if (!NT_STATUS_IS_OK(status2)) { \
107                         torture_result(tctx, TORTURE_FAIL, "(%s) %s - %s\n", __location__, #call, nt_errstr(status2)); \
108                 ret = false; \
109                 goto done; \
110                 } \
111         }} while (0)
112
113 #define CHECK_VALUE(call, stype, field, value) do { \
114         CHECK1(call); \
115         if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && finfo2.stype.out.field != value) { \
116                 torture_result(tctx, TORTURE_FAIL, "(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
117                        call_name, #stype, #field, \
118                        (unsigned int)value, (unsigned int)finfo2.stype.out.field); \
119                 torture_smb2_all_info(tree, handle); \
120                 ret = false; \
121                 goto done; \
122         }} while (0)
123
124 #define CHECK_TIME(call, stype, field, value) do { \
125         CHECK1(call); \
126         if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && nt_time_to_unix(finfo2.stype.out.field) != value) { \
127                 torture_result(tctx, TORTURE_FAIL, "(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
128                         call_name, #stype, #field, \
129                         (unsigned int)value, \
130                         (unsigned int)nt_time_to_unix(finfo2.stype.out.field)); \
131                 torture_warning(tctx, "\t%s", timestring(tctx, value)); \
132                 torture_warning(tctx, "\t%s\n", nt_time_string(tctx, finfo2.stype.out.field)); \
133                 torture_smb2_all_info(tree, handle); \
134                 ret = false; \
135                 goto done; \
136         }} while (0)
137
138 #define CHECK_STATUS(status, correct) do { \
139         if (!NT_STATUS_EQUAL(status, correct)) { \
140                 torture_result(tctx, TORTURE_FAIL, "(%s) Incorrect status %s - should be %s\n", \
141                        __location__, nt_errstr(status), nt_errstr(correct)); \
142                 ret = false; \
143                 goto done; \
144         }} while (0)
145
146         torture_smb2_all_info(tree, handle);
147         
148         torture_comment(tctx, "Test basic_information level\n");
149         basetime += 86400;
150         unix_to_nt_time(&sfinfo.basic_info.in.create_time, basetime + 100);
151         unix_to_nt_time(&sfinfo.basic_info.in.access_time, basetime + 200);
152         unix_to_nt_time(&sfinfo.basic_info.in.write_time,  basetime + 300);
153         unix_to_nt_time(&sfinfo.basic_info.in.change_time, basetime + 400);
154         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_READONLY;
155         CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
156         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, create_time, basetime + 100);
157         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, access_time, basetime + 200);
158         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, write_time,  basetime + 300);
159         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, change_time, basetime + 400);
160         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib,     FILE_ATTRIBUTE_READONLY);
161
162         torture_comment(tctx, "a zero time means don't change\n");
163         unix_to_nt_time(&sfinfo.basic_info.in.create_time, 0);
164         unix_to_nt_time(&sfinfo.basic_info.in.access_time, 0);
165         unix_to_nt_time(&sfinfo.basic_info.in.write_time,  0);
166         unix_to_nt_time(&sfinfo.basic_info.in.change_time, 0);
167         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
168         CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
169         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, create_time, basetime + 100);
170         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, access_time, basetime + 200);
171         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, write_time,  basetime + 300);
172         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, change_time, basetime + 400);
173         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib,     FILE_ATTRIBUTE_NORMAL);
174
175         torture_comment(tctx, "change the attribute\n");
176         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_HIDDEN;
177         CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
178         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_HIDDEN);
179
180         torture_comment(tctx, "zero attrib means don't change\n");
181         sfinfo.basic_info.in.attrib = 0;
182         CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
183         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_HIDDEN);
184
185         torture_comment(tctx, "can't change a file to a directory\n");
186         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_DIRECTORY;
187         CHECK_CALL(BASIC_INFORMATION, NT_STATUS_INVALID_PARAMETER);
188
189         torture_comment(tctx, "restore attribute\n");
190         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
191         CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
192         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_NORMAL);
193
194         torture_comment(tctx, "Test disposition_information level\n");
195         sfinfo.disposition_info.in.delete_on_close = 1;
196         CHECK_CALL(DISPOSITION_INFORMATION, NT_STATUS_OK);
197         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, delete_pending, 1);
198         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, nlink, 0);
199
200         sfinfo.disposition_info.in.delete_on_close = 0;
201         CHECK_CALL(DISPOSITION_INFORMATION, NT_STATUS_OK);
202         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, delete_pending, 0);
203         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, nlink, 1);
204
205         torture_comment(tctx, "Test allocation_information level\n");
206         sfinfo.allocation_info.in.alloc_size = 0;
207         CHECK_CALL(ALLOCATION_INFORMATION, NT_STATUS_OK);
208         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 0);
209         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, alloc_size, 0);
210
211         sfinfo.allocation_info.in.alloc_size = 4096;
212         CHECK_CALL(ALLOCATION_INFORMATION, NT_STATUS_OK);
213         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, alloc_size, 4096);
214         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 0);
215
216         torture_comment(tctx, "Test end_of_file_info level\n");
217         sfinfo.end_of_file_info.in.size = 37;
218         CHECK_CALL(END_OF_FILE_INFORMATION, NT_STATUS_OK);
219         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 37);
220
221         sfinfo.end_of_file_info.in.size = 7;
222         CHECK_CALL(END_OF_FILE_INFORMATION, NT_STATUS_OK);
223         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 7);
224
225         torture_comment(tctx, "Test position_information level\n");
226         sfinfo.position_information.in.position = 123456;
227         CHECK_CALL(POSITION_INFORMATION, NT_STATUS_OK);
228         CHECK_VALUE(POSITION_INFORMATION, position_information, position, 123456);
229         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, position, 123456);
230
231         torture_comment(tctx, "Test mode_information level\n");
232         sfinfo.mode_information.in.mode = 2;
233         CHECK_CALL(MODE_INFORMATION, NT_STATUS_OK);
234         CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 2);
235         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, mode, 2);
236
237         sfinfo.mode_information.in.mode = 1;
238         CHECK_CALL(MODE_INFORMATION, NT_STATUS_INVALID_PARAMETER);
239
240         sfinfo.mode_information.in.mode = 0;
241         CHECK_CALL(MODE_INFORMATION, NT_STATUS_OK);
242         CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 0);
243
244         torture_comment(tctx, "Test sec_desc level\n");
245         ZERO_STRUCT(finfo2);
246         finfo2.query_secdesc.in.secinfo_flags =
247                 SECINFO_OWNER |
248                 SECINFO_GROUP |
249                 SECINFO_DACL;
250         CHECK1(SEC_DESC);
251         sd = finfo2.query_secdesc.out.sd;
252
253         test_sid = dom_sid_parse_talloc(tctx, SID_NT_AUTHENTICATED_USERS);
254         ZERO_STRUCT(ace);
255         ace.type = SEC_ACE_TYPE_ACCESS_ALLOWED;
256         ace.flags = 0;
257         ace.access_mask = SEC_STD_ALL;
258         ace.trustee = *test_sid;
259         status = security_descriptor_dacl_add(sd, &ace);
260         CHECK_STATUS(status, NT_STATUS_OK);
261
262         torture_comment(tctx, "add a new ACE to the DACL\n");
263
264         sfinfo.set_secdesc.in.secinfo_flags = finfo2.query_secdesc.in.secinfo_flags;
265         sfinfo.set_secdesc.in.sd = sd;
266         CHECK_CALL(SEC_DESC, NT_STATUS_OK);
267         FAIL_UNLESS(smb2_util_verify_sd(tctx, tree, handle, sd));
268
269         torture_comment(tctx, "remove it again\n");
270
271         status = security_descriptor_dacl_del(sd, test_sid);
272         CHECK_STATUS(status, NT_STATUS_OK);
273
274         sfinfo.set_secdesc.in.secinfo_flags = finfo2.query_secdesc.in.secinfo_flags;
275         sfinfo.set_secdesc.in.sd = sd;
276         CHECK_CALL(SEC_DESC, NT_STATUS_OK);
277         FAIL_UNLESS(smb2_util_verify_sd(tctx, tree, handle, sd));
278
279 done:
280         status = smb2_util_close(tree, handle);
281         if (NT_STATUS_IS_ERR(status)) {
282                 torture_warning(tctx, "Failed to delete %s - %s\n", fname, nt_errstr(status));
283         }
284         smb2_util_unlink(tree, fname);
285
286         return ret;
287 }
288
289