4fb7cfbef251533094242b25f027fb3d58250b7b
[ira/wip.git] / source4 / torture / smb2 / util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    helper functions for SMB2 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 "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "system/time.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "param/param.h"
29 #include "libcli/resolve/resolve.h"
30
31 #include "torture/torture.h"
32
33
34 /*
35   write to a file on SMB2
36 */
37 NTSTATUS smb2_util_write(struct smb2_tree *tree,
38                          struct smb2_handle handle, 
39                          const void *buf, off_t offset, size_t size)
40 {
41         struct smb2_write w;
42
43         ZERO_STRUCT(w);
44         w.in.file.handle = handle;
45         w.in.offset      = offset;
46         w.in.data        = data_blob_const(buf, size);
47
48         return smb2_write(tree, &w);
49 }
50
51 /*
52   create a complex file/dir using the SMB2 protocol
53 */
54 static NTSTATUS smb2_create_complex(struct smb2_tree *tree, const char *fname, 
55                                          struct smb2_handle *handle, bool dir)
56 {
57         TALLOC_CTX *tmp_ctx = talloc_new(tree);
58         char buf[7] = "abc";
59         struct smb2_create io;
60         union smb_setfileinfo setfile;
61         union smb_fileinfo fileinfo;
62         time_t t = (time(NULL) & ~1);
63         NTSTATUS status;
64
65         smb2_util_unlink(tree, fname);
66         ZERO_STRUCT(io);
67         io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
68         io.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
69         io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
70         io.in.share_access = 
71                 NTCREATEX_SHARE_ACCESS_DELETE|
72                 NTCREATEX_SHARE_ACCESS_READ|
73                 NTCREATEX_SHARE_ACCESS_WRITE;
74         io.in.create_options = 0;
75         io.in.fname = fname;
76         if (dir) {
77                 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
78                 io.in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
79                 io.in.file_attributes   = FILE_ATTRIBUTE_DIRECTORY;
80                 io.in.create_disposition = NTCREATEX_DISP_CREATE;
81         }
82
83         /* it seems vista is now fussier about alignment? */
84         if (strchr(fname, ':') == NULL) {
85                 /* setup some EAs */
86                 io.in.eas.num_eas = 2;
87                 io.in.eas.eas = talloc_array(tmp_ctx, struct ea_struct, 2);
88                 io.in.eas.eas[0].flags = 0;
89                 io.in.eas.eas[0].name.s = "EAONE";
90                 io.in.eas.eas[0].value = data_blob_talloc(tmp_ctx, "VALUE1", 6);
91                 io.in.eas.eas[1].flags = 0;
92                 io.in.eas.eas[1].name.s = "SECONDEA";
93                 io.in.eas.eas[1].value = data_blob_talloc(tmp_ctx, "ValueTwo", 8);
94         }
95
96         status = smb2_create(tree, tmp_ctx, &io);
97         talloc_free(tmp_ctx);
98         NT_STATUS_NOT_OK_RETURN(status);
99
100         *handle = io.out.file.handle;
101
102         if (!dir) {
103                 status = smb2_util_write(tree, *handle, buf, 0, sizeof(buf));
104                 NT_STATUS_NOT_OK_RETURN(status);
105         }
106
107         /* make sure all the timestamps aren't the same, and are also 
108            in different DST zones*/
109         setfile.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
110         setfile.generic.in.file.handle = *handle;
111
112         unix_to_nt_time(&setfile.basic_info.in.create_time, t + 9*30*24*60*60);
113         unix_to_nt_time(&setfile.basic_info.in.access_time, t + 6*30*24*60*60);
114         unix_to_nt_time(&setfile.basic_info.in.write_time,  t + 3*30*24*60*60);
115         unix_to_nt_time(&setfile.basic_info.in.change_time, t + 1*30*24*60*60);
116         setfile.basic_info.in.attrib      = FILE_ATTRIBUTE_NORMAL;
117
118         status = smb2_setinfo_file(tree, &setfile);
119         if (!NT_STATUS_IS_OK(status)) {
120                 printf("Failed to setup file times - %s\n", nt_errstr(status));
121                 return status;
122         }
123
124         /* make sure all the timestamps aren't the same */
125         fileinfo.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
126         fileinfo.generic.in.file.handle = *handle;
127
128         status = smb2_getinfo_file(tree, tree, &fileinfo);
129         if (!NT_STATUS_IS_OK(status)) {
130                 printf("Failed to query file times - %s\n", nt_errstr(status));
131                 return status;
132                 
133         }
134
135 #define CHECK_TIME(field) do {\
136         if (setfile.basic_info.in.field != fileinfo.all_info2.out.field) { \
137                 printf("(%s) " #field " not setup correctly: %s(%llu) => %s(%llu)\n", \
138                         __location__, \
139                         nt_time_string(tree, setfile.basic_info.in.field), \
140                         (unsigned long long)setfile.basic_info.in.field, \
141                         nt_time_string(tree, fileinfo.basic_info.out.field), \
142                         (unsigned long long)fileinfo.basic_info.out.field); \
143                 status = NT_STATUS_INVALID_PARAMETER; \
144         } \
145 } while (0)
146
147         CHECK_TIME(create_time);
148         CHECK_TIME(access_time);
149         CHECK_TIME(write_time);
150         CHECK_TIME(change_time);
151
152         return status;
153 }
154
155 /*
156   create a complex file using the SMB2 protocol
157 */
158 NTSTATUS smb2_create_complex_file(struct smb2_tree *tree, const char *fname, 
159                                          struct smb2_handle *handle)
160 {
161         return smb2_create_complex(tree, fname, handle, false);
162 }
163
164 /*
165   create a complex dir using the SMB2 protocol
166 */
167 NTSTATUS smb2_create_complex_dir(struct smb2_tree *tree, const char *fname, 
168                                  struct smb2_handle *handle)
169 {
170         return smb2_create_complex(tree, fname, handle, true);
171 }
172
173 /*
174   show lots of information about a file
175 */
176 void torture_smb2_all_info(struct smb2_tree *tree, struct smb2_handle handle)
177 {
178         NTSTATUS status;
179         TALLOC_CTX *tmp_ctx = talloc_new(tree);
180         union smb_fileinfo io;
181
182         io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
183         io.generic.in.file.handle = handle;
184
185         status = smb2_getinfo_file(tree, tmp_ctx, &io);
186         if (!NT_STATUS_IS_OK(status)) {
187                 DEBUG(0,("getinfo failed - %s\n", nt_errstr(status)));
188                 talloc_free(tmp_ctx);
189                 return;
190         }
191
192         d_printf("all_info for '%s'\n", io.all_info2.out.fname.s);
193         d_printf("\tcreate_time:    %s\n", nt_time_string(tmp_ctx, io.all_info2.out.create_time));
194         d_printf("\taccess_time:    %s\n", nt_time_string(tmp_ctx, io.all_info2.out.access_time));
195         d_printf("\twrite_time:     %s\n", nt_time_string(tmp_ctx, io.all_info2.out.write_time));
196         d_printf("\tchange_time:    %s\n", nt_time_string(tmp_ctx, io.all_info2.out.change_time));
197         d_printf("\tattrib:         0x%x\n", io.all_info2.out.attrib);
198         d_printf("\tunknown1:       0x%x\n", io.all_info2.out.unknown1);
199         d_printf("\talloc_size:     %llu\n", (long long)io.all_info2.out.alloc_size);
200         d_printf("\tsize:           %llu\n", (long long)io.all_info2.out.size);
201         d_printf("\tnlink:          %u\n", io.all_info2.out.nlink);
202         d_printf("\tdelete_pending: %u\n", io.all_info2.out.delete_pending);
203         d_printf("\tdirectory:      %u\n", io.all_info2.out.directory);
204         d_printf("\tfile_id:        %llu\n", (long long)io.all_info2.out.file_id);
205         d_printf("\tea_size:        %u\n", io.all_info2.out.ea_size);
206         d_printf("\taccess_mask:    0x%08x\n", io.all_info2.out.access_mask);
207         d_printf("\tposition:       0x%llx\n", (long long)io.all_info2.out.position);
208         d_printf("\tmode:           0x%llx\n", (long long)io.all_info2.out.mode);
209
210         /* short name, if any */
211         io.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
212         status = smb2_getinfo_file(tree, tmp_ctx, &io);
213         if (NT_STATUS_IS_OK(status)) {
214                 d_printf("\tshort name:     '%s'\n", io.alt_name_info.out.fname.s);
215         }
216
217         /* the EAs, if any */
218         io.generic.level = RAW_FILEINFO_SMB2_ALL_EAS;
219         status = smb2_getinfo_file(tree, tmp_ctx, &io);
220         if (NT_STATUS_IS_OK(status)) {
221                 int i;
222                 for (i=0;i<io.all_eas.out.num_eas;i++) {
223                         d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
224                                  io.all_eas.out.eas[i].flags,
225                                  (int)io.all_eas.out.eas[i].value.length,
226                                  io.all_eas.out.eas[i].name.s);
227                 }
228         }
229
230         /* streams, if available */
231         io.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
232         status = smb2_getinfo_file(tree, tmp_ctx, &io);
233         if (NT_STATUS_IS_OK(status)) {
234                 int i;
235                 for (i=0;i<io.stream_info.out.num_streams;i++) {
236                         d_printf("\tstream %d:\n", i);
237                         d_printf("\t\tsize       %ld\n", 
238                                  (long)io.stream_info.out.streams[i].size);
239                         d_printf("\t\talloc size %ld\n", 
240                                  (long)io.stream_info.out.streams[i].alloc_size);
241                         d_printf("\t\tname       %s\n", io.stream_info.out.streams[i].stream_name.s);
242                 }
243         }       
244
245         if (DEBUGLVL(1)) {
246                 /* the security descriptor */
247                 io.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
248                 io.query_secdesc.in.secinfo_flags = 
249                         SECINFO_OWNER|SECINFO_GROUP|
250                         SECINFO_DACL;
251                 status = smb2_getinfo_file(tree, tmp_ctx, &io);
252                 if (NT_STATUS_IS_OK(status)) {
253                         NDR_PRINT_DEBUG(security_descriptor, io.query_secdesc.out.sd);
254                 }
255         }
256
257         talloc_free(tmp_ctx);   
258 }
259
260
261 /*
262   open a smb2 connection
263 */
264 bool torture_smb2_connection(struct torture_context *tctx, struct smb2_tree **tree)
265 {
266         NTSTATUS status;
267         const char *host = torture_setting_string(tctx, "host", NULL);
268         const char *share = torture_setting_string(tctx, "share", NULL);
269         struct cli_credentials *credentials = cmdline_credentials;
270         struct smbcli_options options;
271
272         lp_smbcli_options(tctx->lp_ctx, &options);
273
274         status = smb2_connect(tctx, host, 
275                                                   lp_smb_ports(tctx->lp_ctx),
276                                                   share, 
277                               lp_resolve_context(tctx->lp_ctx),
278                               credentials, tree, 
279                               tctx->ev, &options,
280                                   lp_socket_options(tctx->lp_ctx),
281                                   lp_gensec_settings(tctx, tctx->lp_ctx)
282                                   );
283         if (!NT_STATUS_IS_OK(status)) {
284                 printf("Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
285                        host, share, nt_errstr(status));
286                 return false;
287         }
288         return true;
289 }
290
291
292 /*
293   create and return a handle to a test file
294 */
295 NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname, 
296                                struct smb2_handle *handle)
297 {
298         struct smb2_create io;
299         struct smb2_read r;
300         NTSTATUS status;
301
302         ZERO_STRUCT(io);
303         io.in.oplock_level = 0;
304         io.in.desired_access = SEC_RIGHTS_FILE_ALL;
305         io.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
306         io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
307         io.in.share_access = 
308                 NTCREATEX_SHARE_ACCESS_DELETE|
309                 NTCREATEX_SHARE_ACCESS_READ|
310                 NTCREATEX_SHARE_ACCESS_WRITE;
311         io.in.create_options = 0;
312         io.in.fname = fname;
313
314         status = smb2_create(tree, tree, &io);
315         NT_STATUS_NOT_OK_RETURN(status);
316
317         *handle = io.out.file.handle;
318
319         ZERO_STRUCT(r);
320         r.in.file.handle = *handle;
321         r.in.length      = 5;
322         r.in.offset      = 0;
323
324         smb2_read(tree, tree, &r);
325
326         return NT_STATUS_OK;
327 }
328
329 /*
330   create and return a handle to a test directory
331 */
332 NTSTATUS torture_smb2_testdir(struct smb2_tree *tree, const char *fname, 
333                               struct smb2_handle *handle)
334 {
335         struct smb2_create io;
336         NTSTATUS status;
337
338         ZERO_STRUCT(io);
339         io.in.oplock_level = 0;
340         io.in.desired_access = SEC_RIGHTS_DIR_ALL;
341         io.in.file_attributes   = FILE_ATTRIBUTE_DIRECTORY;
342         io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
343         io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE;
344         io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
345         io.in.fname = fname;
346
347         status = smb2_create(tree, tree, &io);
348         NT_STATUS_NOT_OK_RETURN(status);
349
350         *handle = io.out.file.handle;
351
352         return NT_STATUS_OK;
353 }
354
355
356 /*
357   create a complex file using SMB2, to make it easier to
358   find fields in SMB2 getinfo levels
359 */
360 NTSTATUS torture_setup_complex_file(struct smb2_tree *tree, const char *fname)
361 {
362         struct smb2_handle handle;
363         NTSTATUS status = smb2_create_complex_file(tree, fname, &handle);
364         NT_STATUS_NOT_OK_RETURN(status);
365         return smb2_util_close(tree, handle);
366 }
367
368
369 /*
370   create a complex dir using SMB2, to make it easier to
371   find fields in SMB2 getinfo levels
372 */
373 NTSTATUS torture_setup_complex_dir(struct smb2_tree *tree, const char *fname)
374 {
375         struct smb2_handle handle;
376         NTSTATUS status = smb2_create_complex_dir(tree, fname, &handle);
377         NT_STATUS_NOT_OK_RETURN(status);
378         return smb2_util_close(tree, handle);
379 }
380
381
382 /*
383   return a handle to the root of the share
384 */
385 NTSTATUS smb2_util_roothandle(struct smb2_tree *tree, struct smb2_handle *handle)
386 {
387         struct smb2_create io;
388         NTSTATUS status;
389
390         ZERO_STRUCT(io);
391         io.in.oplock_level = 0;
392         io.in.desired_access = SEC_STD_SYNCHRONIZE | SEC_DIR_READ_ATTRIBUTE | SEC_DIR_LIST;
393         io.in.file_attributes   = 0;
394         io.in.create_disposition = NTCREATEX_DISP_OPEN;
395         io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
396         io.in.create_options = NTCREATEX_OPTIONS_ASYNC_ALERT;
397         io.in.fname = NULL;
398
399         status = smb2_create(tree, tree, &io);
400         NT_STATUS_NOT_OK_RETURN(status);
401
402         *handle = io.out.file.handle;
403
404         return NT_STATUS_OK;
405 }
406
407 /* Comparable to torture_setup_dir, but for SMB2. */
408 bool smb2_util_setup_dir(struct torture_context *tctx, struct smb2_tree *tree,
409     const char *dname)
410 {
411         NTSTATUS status;
412
413         /* XXX: smb_raw_exit equivalent?
414         smb_raw_exit(cli->session); */
415         if (smb2_deltree(tree, dname) == -1) {
416                 torture_result(tctx, TORTURE_ERROR, "Unable to deltree when setting up %s.\n", dname);
417                 return false;
418         }
419
420         status = smb2_util_mkdir(tree, dname);
421         if (NT_STATUS_IS_ERR(status)) {
422                 torture_result(tctx, TORTURE_ERROR, "Unable to mkdir when setting up %s - %s\n", dname,
423                     nt_errstr(status));
424                 return false;
425         }
426
427         return true;
428 }
429
430 #define CHECK_STATUS(status, correct) do { \
431         if (!NT_STATUS_EQUAL(status, correct)) { \
432                 torture_result(tctx, TORTURE_FAIL, "(%s) Incorrect status %s - should be %s\n", \
433                        __location__, nt_errstr(status), nt_errstr(correct)); \
434                 ret = false; \
435                 goto done; \
436         }} while (0)
437
438 /*
439  * Helper function to verify a security descriptor, by querying
440  * and comparing against the passed in sd.
441  */
442 bool smb2_util_verify_sd(TALLOC_CTX *tctx, struct smb2_tree *tree,
443     struct smb2_handle handle, struct security_descriptor *sd)
444 {
445         NTSTATUS status;
446         bool ret = true;
447         union smb_fileinfo q = {}, q2 = {};
448
449         if (sd) {
450                 q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
451                 q.query_secdesc.in.file.handle = handle;
452                 q.query_secdesc.in.secinfo_flags =
453                     SECINFO_OWNER |
454                     SECINFO_GROUP |
455                     SECINFO_DACL;
456                 status = smb2_getinfo_file(tree, tctx, &q);
457                 CHECK_STATUS(status, NT_STATUS_OK);
458
459                 if (!security_acl_equal(
460                     q.query_secdesc.out.sd->dacl, sd->dacl)) {
461                         torture_warning(tctx, "%s: security descriptors don't match!\n",
462                             __location__);
463                         torture_warning(tctx, "got:\n");
464                         NDR_PRINT_DEBUG(security_descriptor,
465                             q.query_secdesc.out.sd);
466                         torture_warning(tctx, "expected:\n");
467                         NDR_PRINT_DEBUG(security_descriptor, sd);
468                         ret = false;
469                 }
470         }
471
472  done:
473         return ret;
474 }
475
476 /*
477  * Helper function to verify attributes, by querying
478  * and comparing against the passed in attrib.
479  */
480 bool smb2_util_verify_attrib(TALLOC_CTX *tctx, struct smb2_tree *tree,
481     struct smb2_handle handle, uint32_t attrib)
482 {
483         NTSTATUS status;
484         bool ret = true;
485         union smb_fileinfo q = {}, q2 = {};
486
487         if (attrib) {
488                 q2.standard.level = RAW_FILEINFO_STANDARD;
489                 q2.standard.in.file.handle = handle;
490                 status = smb2_getinfo_file(tree, tctx, &q2);
491                 CHECK_STATUS(status, NT_STATUS_OK);
492
493                 q2.standard.out.attrib &= ~FILE_ATTRIBUTE_ARCHIVE;
494
495                 if (q2.standard.out.attrib != attrib) {
496                         torture_warning(tctx, "%s: attributes don't match! "
497                             "got %x, expected %x\n", __location__,
498                             (uint32_t)q2.standard.out.attrib,
499                             (uint32_t)attrib);
500                         ret = false;
501                 }
502         }
503
504  done:
505         return ret;
506 }
507
508