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