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