s3-spoolss: fix _spoolss_GetPrinterDataEx after IDL change.
[Samba/nascimento.git] / source4 / torture / smb2 / util.c
blob1dc8fca2b0f93fcce8415001ed4888480392f2e3
1 /*
2 Unix SMB/CIFS implementation.
4 helper functions for SMB2 test suite
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
22 #include "includes.h"
23 #include "libcli/security/security_descriptor.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "system/time.h"
28 #include "librpc/gen_ndr/ndr_security.h"
29 #include "param/param.h"
30 #include "libcli/resolve/resolve.h"
32 #include "torture/torture.h"
36 write to a file on SMB2
38 NTSTATUS smb2_util_write(struct smb2_tree *tree,
39 struct smb2_handle handle,
40 const void *buf, off_t offset, size_t size)
42 struct smb2_write w;
44 ZERO_STRUCT(w);
45 w.in.file.handle = handle;
46 w.in.offset = offset;
47 w.in.data = data_blob_const(buf, size);
49 return smb2_write(tree, &w);
53 create a complex file/dir using the SMB2 protocol
55 static NTSTATUS smb2_create_complex(struct smb2_tree *tree, const char *fname,
56 struct smb2_handle *handle, bool dir)
58 TALLOC_CTX *tmp_ctx = talloc_new(tree);
59 char buf[7] = "abc";
60 struct smb2_create io;
61 union smb_setfileinfo setfile;
62 union smb_fileinfo fileinfo;
63 time_t t = (time(NULL) & ~1);
64 NTSTATUS status;
66 smb2_util_unlink(tree, fname);
67 ZERO_STRUCT(io);
68 io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
69 io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
70 io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
71 io.in.share_access =
72 NTCREATEX_SHARE_ACCESS_DELETE|
73 NTCREATEX_SHARE_ACCESS_READ|
74 NTCREATEX_SHARE_ACCESS_WRITE;
75 io.in.create_options = 0;
76 io.in.fname = fname;
77 if (dir) {
78 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
79 io.in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
80 io.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
81 io.in.create_disposition = NTCREATEX_DISP_CREATE;
84 /* it seems vista is now fussier about alignment? */
85 if (strchr(fname, ':') == NULL) {
86 /* setup some EAs */
87 io.in.eas.num_eas = 2;
88 io.in.eas.eas = talloc_array(tmp_ctx, struct ea_struct, 2);
89 io.in.eas.eas[0].flags = 0;
90 io.in.eas.eas[0].name.s = "EAONE";
91 io.in.eas.eas[0].value = data_blob_talloc(tmp_ctx, "VALUE1", 6);
92 io.in.eas.eas[1].flags = 0;
93 io.in.eas.eas[1].name.s = "SECONDEA";
94 io.in.eas.eas[1].value = data_blob_talloc(tmp_ctx, "ValueTwo", 8);
97 status = smb2_create(tree, tmp_ctx, &io);
98 talloc_free(tmp_ctx);
99 NT_STATUS_NOT_OK_RETURN(status);
101 *handle = io.out.file.handle;
103 if (!dir) {
104 status = smb2_util_write(tree, *handle, buf, 0, sizeof(buf));
105 NT_STATUS_NOT_OK_RETURN(status);
108 /* make sure all the timestamps aren't the same, and are also
109 in different DST zones*/
110 setfile.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
111 setfile.generic.in.file.handle = *handle;
113 unix_to_nt_time(&setfile.basic_info.in.create_time, t + 9*30*24*60*60);
114 unix_to_nt_time(&setfile.basic_info.in.access_time, t + 6*30*24*60*60);
115 unix_to_nt_time(&setfile.basic_info.in.write_time, t + 3*30*24*60*60);
116 unix_to_nt_time(&setfile.basic_info.in.change_time, t + 1*30*24*60*60);
117 setfile.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
119 status = smb2_setinfo_file(tree, &setfile);
120 if (!NT_STATUS_IS_OK(status)) {
121 printf("Failed to setup file times - %s\n", nt_errstr(status));
122 return status;
125 /* make sure all the timestamps aren't the same */
126 fileinfo.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
127 fileinfo.generic.in.file.handle = *handle;
129 status = smb2_getinfo_file(tree, tree, &fileinfo);
130 if (!NT_STATUS_IS_OK(status)) {
131 printf("Failed to query file times - %s\n", nt_errstr(status));
132 return status;
136 #define CHECK_TIME(field) do {\
137 if (setfile.basic_info.in.field != fileinfo.all_info2.out.field) { \
138 printf("(%s) " #field " not setup correctly: %s(%llu) => %s(%llu)\n", \
139 __location__, \
140 nt_time_string(tree, setfile.basic_info.in.field), \
141 (unsigned long long)setfile.basic_info.in.field, \
142 nt_time_string(tree, fileinfo.basic_info.out.field), \
143 (unsigned long long)fileinfo.basic_info.out.field); \
144 status = NT_STATUS_INVALID_PARAMETER; \
146 } while (0)
148 CHECK_TIME(create_time);
149 CHECK_TIME(access_time);
150 CHECK_TIME(write_time);
151 CHECK_TIME(change_time);
153 return status;
157 create a complex file using the SMB2 protocol
159 NTSTATUS smb2_create_complex_file(struct smb2_tree *tree, const char *fname,
160 struct smb2_handle *handle)
162 return smb2_create_complex(tree, fname, handle, false);
166 create a complex dir using the SMB2 protocol
168 NTSTATUS smb2_create_complex_dir(struct smb2_tree *tree, const char *fname,
169 struct smb2_handle *handle)
171 return smb2_create_complex(tree, fname, handle, true);
175 show lots of information about a file
177 void torture_smb2_all_info(struct smb2_tree *tree, struct smb2_handle handle)
179 NTSTATUS status;
180 TALLOC_CTX *tmp_ctx = talloc_new(tree);
181 union smb_fileinfo io;
183 io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
184 io.generic.in.file.handle = handle;
186 status = smb2_getinfo_file(tree, tmp_ctx, &io);
187 if (!NT_STATUS_IS_OK(status)) {
188 DEBUG(0,("getinfo failed - %s\n", nt_errstr(status)));
189 talloc_free(tmp_ctx);
190 return;
193 d_printf("all_info for '%s'\n", io.all_info2.out.fname.s);
194 d_printf("\tcreate_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.create_time));
195 d_printf("\taccess_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.access_time));
196 d_printf("\twrite_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.write_time));
197 d_printf("\tchange_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.change_time));
198 d_printf("\tattrib: 0x%x\n", io.all_info2.out.attrib);
199 d_printf("\tunknown1: 0x%x\n", io.all_info2.out.unknown1);
200 d_printf("\talloc_size: %llu\n", (long long)io.all_info2.out.alloc_size);
201 d_printf("\tsize: %llu\n", (long long)io.all_info2.out.size);
202 d_printf("\tnlink: %u\n", io.all_info2.out.nlink);
203 d_printf("\tdelete_pending: %u\n", io.all_info2.out.delete_pending);
204 d_printf("\tdirectory: %u\n", io.all_info2.out.directory);
205 d_printf("\tfile_id: %llu\n", (long long)io.all_info2.out.file_id);
206 d_printf("\tea_size: %u\n", io.all_info2.out.ea_size);
207 d_printf("\taccess_mask: 0x%08x\n", io.all_info2.out.access_mask);
208 d_printf("\tposition: 0x%llx\n", (long long)io.all_info2.out.position);
209 d_printf("\tmode: 0x%llx\n", (long long)io.all_info2.out.mode);
211 /* short name, if any */
212 io.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
213 status = smb2_getinfo_file(tree, tmp_ctx, &io);
214 if (NT_STATUS_IS_OK(status)) {
215 d_printf("\tshort name: '%s'\n", io.alt_name_info.out.fname.s);
218 /* the EAs, if any */
219 io.generic.level = RAW_FILEINFO_SMB2_ALL_EAS;
220 status = smb2_getinfo_file(tree, tmp_ctx, &io);
221 if (NT_STATUS_IS_OK(status)) {
222 int i;
223 for (i=0;i<io.all_eas.out.num_eas;i++) {
224 d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
225 io.all_eas.out.eas[i].flags,
226 (int)io.all_eas.out.eas[i].value.length,
227 io.all_eas.out.eas[i].name.s);
231 /* streams, if available */
232 io.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
233 status = smb2_getinfo_file(tree, tmp_ctx, &io);
234 if (NT_STATUS_IS_OK(status)) {
235 int i;
236 for (i=0;i<io.stream_info.out.num_streams;i++) {
237 d_printf("\tstream %d:\n", i);
238 d_printf("\t\tsize %ld\n",
239 (long)io.stream_info.out.streams[i].size);
240 d_printf("\t\talloc size %ld\n",
241 (long)io.stream_info.out.streams[i].alloc_size);
242 d_printf("\t\tname %s\n", io.stream_info.out.streams[i].stream_name.s);
246 if (DEBUGLVL(1)) {
247 /* the security descriptor */
248 io.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
249 io.query_secdesc.in.secinfo_flags =
250 SECINFO_OWNER|SECINFO_GROUP|
251 SECINFO_DACL;
252 status = smb2_getinfo_file(tree, tmp_ctx, &io);
253 if (NT_STATUS_IS_OK(status)) {
254 NDR_PRINT_DEBUG(security_descriptor, io.query_secdesc.out.sd);
258 talloc_free(tmp_ctx);
263 open a smb2 connection
265 bool torture_smb2_connection(struct torture_context *tctx, struct smb2_tree **tree)
267 NTSTATUS status;
268 const char *host = torture_setting_string(tctx, "host", NULL);
269 const char *share = torture_setting_string(tctx, "share", NULL);
270 struct cli_credentials *credentials = cmdline_credentials;
271 struct smbcli_options options;
273 lp_smbcli_options(tctx->lp_ctx, &options);
275 status = smb2_connect(tctx, host,
276 lp_smb_ports(tctx->lp_ctx),
277 share,
278 lp_resolve_context(tctx->lp_ctx),
279 credentials, tree,
280 tctx->ev, &options,
281 lp_socket_options(tctx->lp_ctx),
282 lp_gensec_settings(tctx, tctx->lp_ctx)
284 if (!NT_STATUS_IS_OK(status)) {
285 printf("Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
286 host, share, nt_errstr(status));
287 return false;
289 return true;
294 create and return a handle to a test file
296 NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname,
297 struct smb2_handle *handle)
299 struct smb2_create io;
300 struct smb2_read r;
301 NTSTATUS status;
303 ZERO_STRUCT(io);
304 io.in.oplock_level = 0;
305 io.in.desired_access = SEC_RIGHTS_FILE_ALL;
306 io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
307 io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
308 io.in.share_access =
309 NTCREATEX_SHARE_ACCESS_DELETE|
310 NTCREATEX_SHARE_ACCESS_READ|
311 NTCREATEX_SHARE_ACCESS_WRITE;
312 io.in.create_options = 0;
313 io.in.fname = fname;
315 status = smb2_create(tree, tree, &io);
316 NT_STATUS_NOT_OK_RETURN(status);
318 *handle = io.out.file.handle;
320 ZERO_STRUCT(r);
321 r.in.file.handle = *handle;
322 r.in.length = 5;
323 r.in.offset = 0;
325 smb2_read(tree, tree, &r);
327 return NT_STATUS_OK;
331 create and return a handle to a test directory
333 NTSTATUS torture_smb2_testdir(struct smb2_tree *tree, const char *fname,
334 struct smb2_handle *handle)
336 struct smb2_create io;
337 NTSTATUS status;
339 ZERO_STRUCT(io);
340 io.in.oplock_level = 0;
341 io.in.desired_access = SEC_RIGHTS_DIR_ALL;
342 io.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
343 io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
344 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE;
345 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
346 io.in.fname = fname;
348 status = smb2_create(tree, tree, &io);
349 NT_STATUS_NOT_OK_RETURN(status);
351 *handle = io.out.file.handle;
353 return NT_STATUS_OK;
358 create a complex file using SMB2, to make it easier to
359 find fields in SMB2 getinfo levels
361 NTSTATUS torture_setup_complex_file(struct smb2_tree *tree, const char *fname)
363 struct smb2_handle handle;
364 NTSTATUS status = smb2_create_complex_file(tree, fname, &handle);
365 NT_STATUS_NOT_OK_RETURN(status);
366 return smb2_util_close(tree, handle);
371 create a complex dir using SMB2, to make it easier to
372 find fields in SMB2 getinfo levels
374 NTSTATUS torture_setup_complex_dir(struct smb2_tree *tree, const char *fname)
376 struct smb2_handle handle;
377 NTSTATUS status = smb2_create_complex_dir(tree, fname, &handle);
378 NT_STATUS_NOT_OK_RETURN(status);
379 return smb2_util_close(tree, handle);
384 return a handle to the root of the share
386 NTSTATUS smb2_util_roothandle(struct smb2_tree *tree, struct smb2_handle *handle)
388 struct smb2_create io;
389 NTSTATUS status;
391 ZERO_STRUCT(io);
392 io.in.oplock_level = 0;
393 io.in.desired_access = SEC_STD_SYNCHRONIZE | SEC_DIR_READ_ATTRIBUTE | SEC_DIR_LIST;
394 io.in.file_attributes = 0;
395 io.in.create_disposition = NTCREATEX_DISP_OPEN;
396 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
397 io.in.create_options = NTCREATEX_OPTIONS_ASYNC_ALERT;
398 io.in.fname = NULL;
400 status = smb2_create(tree, tree, &io);
401 NT_STATUS_NOT_OK_RETURN(status);
403 *handle = io.out.file.handle;
405 return NT_STATUS_OK;
408 /* Comparable to torture_setup_dir, but for SMB2. */
409 bool smb2_util_setup_dir(struct torture_context *tctx, struct smb2_tree *tree,
410 const char *dname)
412 NTSTATUS status;
414 /* XXX: smb_raw_exit equivalent?
415 smb_raw_exit(cli->session); */
416 if (smb2_deltree(tree, dname) == -1) {
417 torture_result(tctx, TORTURE_ERROR, "Unable to deltree when setting up %s.\n", dname);
418 return false;
421 status = smb2_util_mkdir(tree, dname);
422 if (NT_STATUS_IS_ERR(status)) {
423 torture_result(tctx, TORTURE_ERROR, "Unable to mkdir when setting up %s - %s\n", dname,
424 nt_errstr(status));
425 return false;
428 return true;
431 #define CHECK_STATUS(status, correct) do { \
432 if (!NT_STATUS_EQUAL(status, correct)) { \
433 torture_result(tctx, TORTURE_FAIL, "(%s) Incorrect status %s - should be %s\n", \
434 __location__, nt_errstr(status), nt_errstr(correct)); \
435 ret = false; \
436 goto done; \
437 }} while (0)
440 * Helper function to verify a security descriptor, by querying
441 * and comparing against the passed in sd.
443 bool smb2_util_verify_sd(TALLOC_CTX *tctx, struct smb2_tree *tree,
444 struct smb2_handle handle, struct security_descriptor *sd)
446 NTSTATUS status;
447 bool ret = true;
448 union smb_fileinfo q = {};
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);
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;
471 done:
472 return ret;
476 * Helper function to verify attributes, by querying
477 * and comparing against the passed in attrib.
479 bool smb2_util_verify_attrib(TALLOC_CTX *tctx, struct smb2_tree *tree,
480 struct smb2_handle handle, uint32_t attrib)
482 NTSTATUS status;
483 bool ret = true;
484 union smb_fileinfo q = {};
486 q.standard.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
487 q.standard.in.file.handle = handle;
488 status = smb2_getinfo_file(tree, tctx, &q);
489 CHECK_STATUS(status, NT_STATUS_OK);
491 q.all_info2.out.attrib &= ~FILE_ATTRIBUTE_ARCHIVE;
493 if (q.all_info2.out.attrib != attrib) {
494 torture_warning(tctx, "%s: attributes don't match! "
495 "got %x, expected %x\n", __location__,
496 (uint32_t)q.standard.out.attrib,
497 (uint32_t)attrib);
498 ret = false;
501 done:
502 return ret;