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