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 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 "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "lib/events/events.h"
28 #include "system/time.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30
31
32 /*
33 close a handle with SMB2
34 */
smb2_util_close(struct smb2_tree * tree,struct smb2_handle h)35 NTSTATUS smb2_util_close(struct smb2_tree *tree, struct smb2_handle h)
36 {
37 struct smb2_close c;
38
39 ZERO_STRUCT(c);
40 c.in.file.handle = h;
41
42 return smb2_close(tree, &c);
43 }
44
45 /*
46 unlink a file with SMB2
47 */
smb2_util_unlink(struct smb2_tree * tree,const char * fname)48 NTSTATUS smb2_util_unlink(struct smb2_tree *tree, const char *fname)
49 {
50 struct smb2_create io;
51 NTSTATUS status;
52
53 ZERO_STRUCT(io);
54 io.in.access_mask = SEC_RIGHTS_FILE_ALL;
55 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
56 io.in.open_disposition = NTCREATEX_DISP_OPEN;
57 io.in.share_access =
58 NTCREATEX_SHARE_ACCESS_DELETE|
59 NTCREATEX_SHARE_ACCESS_READ|
60 NTCREATEX_SHARE_ACCESS_WRITE;
61 io.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
62 io.in.fname = fname;
63
64 status = smb2_create(tree, tree, &io);
65 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
66 return NT_STATUS_OK;
67 }
68 NT_STATUS_NOT_OK_RETURN(status);
69
70 return smb2_util_close(tree, io.out.file.handle);
71 }
72
73 /*
74 write to a file on SMB2
75 */
smb2_util_write(struct smb2_tree * tree,struct smb2_handle handle,const void * buf,off_t offset,size_t size)76 NTSTATUS smb2_util_write(struct smb2_tree *tree,
77 struct smb2_handle handle,
78 const void *buf, off_t offset, size_t size)
79 {
80 struct smb2_write w;
81
82 ZERO_STRUCT(w);
83 w.in.file.handle = handle;
84 w.in.offset = offset;
85 w.in.data = data_blob_const(buf, size);
86
87 return smb2_write(tree, &w);
88 }
89
90 /*
91 create a complex file/dir using the SMB2 protocol
92 */
smb2_create_complex(struct smb2_tree * tree,const char * fname,struct smb2_handle * handle,BOOL dir)93 static NTSTATUS smb2_create_complex(struct smb2_tree *tree, const char *fname,
94 struct smb2_handle *handle, BOOL dir)
95 {
96 TALLOC_CTX *tmp_ctx = talloc_new(tree);
97 char buf[7] = "abc";
98 struct smb2_create io;
99 union smb_setfileinfo setfile;
100 union smb_fileinfo fileinfo;
101 time_t t = (time(NULL) & ~1);
102 NTSTATUS status;
103
104 smb2_util_unlink(tree, fname);
105 ZERO_STRUCT(io);
106 io.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
107 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
108 io.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
109 io.in.share_access =
110 NTCREATEX_SHARE_ACCESS_DELETE|
111 NTCREATEX_SHARE_ACCESS_READ|
112 NTCREATEX_SHARE_ACCESS_WRITE;
113 io.in.create_options = 0;
114 io.in.fname = fname;
115 if (dir) {
116 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
117 io.in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
118 io.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
119 io.in.open_disposition = NTCREATEX_DISP_CREATE;
120 }
121
122 if (strchr(fname, ':') == NULL) {
123 /* setup some EAs */
124 io.in.eas.num_eas = 2;
125 io.in.eas.eas = talloc_array(tmp_ctx, struct ea_struct, 2);
126 io.in.eas.eas[0].flags = 0;
127 io.in.eas.eas[0].name.s = "EAONE";
128 io.in.eas.eas[0].value = data_blob_talloc(tmp_ctx, "VALUE1", 6);
129 io.in.eas.eas[1].flags = 0;
130 io.in.eas.eas[1].name.s = "SECONDEA";
131 io.in.eas.eas[1].value = data_blob_talloc(tmp_ctx, "ValueTwo", 8);
132 }
133
134 status = smb2_create(tree, tmp_ctx, &io);
135 talloc_free(tmp_ctx);
136 NT_STATUS_NOT_OK_RETURN(status);
137
138 *handle = io.out.file.handle;
139
140 if (!dir) {
141 status = smb2_util_write(tree, *handle, buf, 0, sizeof(buf));
142 NT_STATUS_NOT_OK_RETURN(status);
143 }
144
145 /* make sure all the timestamps aren't the same, and are also
146 in different DST zones*/
147 setfile.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
148 setfile.generic.in.file.handle = *handle;
149
150 unix_to_nt_time(&setfile.basic_info.in.create_time, t + 9*30*24*60*60);
151 unix_to_nt_time(&setfile.basic_info.in.access_time, t + 6*30*24*60*60);
152 unix_to_nt_time(&setfile.basic_info.in.write_time, t + 3*30*24*60*60);
153 unix_to_nt_time(&setfile.basic_info.in.change_time, t + 1*30*24*60*60);
154 setfile.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
155
156 status = smb2_setinfo_file(tree, &setfile);
157 if (!NT_STATUS_IS_OK(status)) {
158 printf("Failed to setup file times - %s\n", nt_errstr(status));
159 return status;
160 }
161
162 /* make sure all the timestamps aren't the same */
163 fileinfo.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
164 fileinfo.generic.in.file.handle = *handle;
165
166 status = smb2_getinfo_file(tree, tree, &fileinfo);
167 if (!NT_STATUS_IS_OK(status)) {
168 printf("Failed to query file times - %s\n", nt_errstr(status));
169 return status;
170
171 }
172
173 #define CHECK_TIME(field) do {\
174 if (setfile.basic_info.in.field != fileinfo.all_info2.out.field) { \
175 printf("(%s) " #field " not setup correctly: %s(%llu) => %s(%llu)\n", \
176 __location__, \
177 nt_time_string(tree, setfile.basic_info.in.field), \
178 (unsigned long long)setfile.basic_info.in.field, \
179 nt_time_string(tree, fileinfo.basic_info.out.field), \
180 (unsigned long long)fileinfo.basic_info.out.field); \
181 status = NT_STATUS_INVALID_PARAMETER; \
182 } \
183 } while (0)
184
185 CHECK_TIME(create_time);
186 CHECK_TIME(access_time);
187 CHECK_TIME(write_time);
188 CHECK_TIME(change_time);
189
190 return status;
191 }
192
193 /*
194 create a complex file using the SMB2 protocol
195 */
smb2_create_complex_file(struct smb2_tree * tree,const char * fname,struct smb2_handle * handle)196 NTSTATUS smb2_create_complex_file(struct smb2_tree *tree, const char *fname,
197 struct smb2_handle *handle)
198 {
199 return smb2_create_complex(tree, fname, handle, False);
200 }
201
202 /*
203 create a complex dir using the SMB2 protocol
204 */
smb2_create_complex_dir(struct smb2_tree * tree,const char * fname,struct smb2_handle * handle)205 NTSTATUS smb2_create_complex_dir(struct smb2_tree *tree, const char *fname,
206 struct smb2_handle *handle)
207 {
208 return smb2_create_complex(tree, fname, handle, True);
209 }
210
211 /*
212 show lots of information about a file
213 */
torture_smb2_all_info(struct smb2_tree * tree,struct smb2_handle handle)214 void torture_smb2_all_info(struct smb2_tree *tree, struct smb2_handle handle)
215 {
216 NTSTATUS status;
217 TALLOC_CTX *tmp_ctx = talloc_new(tree);
218 union smb_fileinfo io;
219
220 io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
221 io.generic.in.file.handle = handle;
222
223 status = smb2_getinfo_file(tree, tmp_ctx, &io);
224 if (!NT_STATUS_IS_OK(status)) {
225 DEBUG(0,("getinfo failed - %s\n", nt_errstr(status)));
226 talloc_free(tmp_ctx);
227 return;
228 }
229
230 d_printf("all_info for '%s'\n", io.all_info2.out.fname.s);
231 d_printf("\tcreate_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.create_time));
232 d_printf("\taccess_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.access_time));
233 d_printf("\twrite_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.write_time));
234 d_printf("\tchange_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.change_time));
235 d_printf("\tattrib: 0x%x\n", io.all_info2.out.attrib);
236 d_printf("\tunknown1: 0x%x\n", io.all_info2.out.unknown1);
237 d_printf("\talloc_size: %llu\n", (long long)io.all_info2.out.alloc_size);
238 d_printf("\tsize: %llu\n", (long long)io.all_info2.out.size);
239 d_printf("\tnlink: %u\n", io.all_info2.out.nlink);
240 d_printf("\tdelete_pending: %u\n", io.all_info2.out.delete_pending);
241 d_printf("\tdirectory: %u\n", io.all_info2.out.directory);
242 d_printf("\tfile_id: %llu\n", (long long)io.all_info2.out.file_id);
243 d_printf("\tea_size: %u\n", io.all_info2.out.ea_size);
244 d_printf("\taccess_mask: 0x%08x\n", io.all_info2.out.access_mask);
245 d_printf("\tposition: 0x%llx\n", (long long)io.all_info2.out.position);
246 d_printf("\tmode: 0x%llx\n", (long long)io.all_info2.out.mode);
247
248 /* short name, if any */
249 io.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
250 status = smb2_getinfo_file(tree, tmp_ctx, &io);
251 if (NT_STATUS_IS_OK(status)) {
252 d_printf("\tshort name: '%s'\n", io.alt_name_info.out.fname.s);
253 }
254
255 /* the EAs, if any */
256 io.generic.level = RAW_FILEINFO_SMB2_ALL_EAS;
257 status = smb2_getinfo_file(tree, tmp_ctx, &io);
258 if (NT_STATUS_IS_OK(status)) {
259 int i;
260 for (i=0;i<io.all_eas.out.num_eas;i++) {
261 d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
262 io.all_eas.out.eas[i].flags,
263 (int)io.all_eas.out.eas[i].value.length,
264 io.all_eas.out.eas[i].name.s);
265 }
266 }
267
268 /* streams, if available */
269 io.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
270 status = smb2_getinfo_file(tree, tmp_ctx, &io);
271 if (NT_STATUS_IS_OK(status)) {
272 int i;
273 for (i=0;i<io.stream_info.out.num_streams;i++) {
274 d_printf("\tstream %d:\n", i);
275 d_printf("\t\tsize %ld\n",
276 (long)io.stream_info.out.streams[i].size);
277 d_printf("\t\talloc size %ld\n",
278 (long)io.stream_info.out.streams[i].alloc_size);
279 d_printf("\t\tname %s\n", io.stream_info.out.streams[i].stream_name.s);
280 }
281 }
282
283 if (DEBUGLVL(1)) {
284 /* the security descriptor */
285 io.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
286 io.query_secdesc.in.secinfo_flags =
287 SECINFO_OWNER|SECINFO_GROUP|
288 SECINFO_DACL;
289 status = smb2_getinfo_file(tree, tmp_ctx, &io);
290 if (NT_STATUS_IS_OK(status)) {
291 NDR_PRINT_DEBUG(security_descriptor, io.query_secdesc.out.sd);
292 }
293 }
294
295 talloc_free(tmp_ctx);
296 }
297
298
299 /*
300 open a smb2 connection
301 */
torture_smb2_connection(TALLOC_CTX * mem_ctx,struct smb2_tree ** tree)302 BOOL torture_smb2_connection(TALLOC_CTX *mem_ctx, struct smb2_tree **tree)
303 {
304 NTSTATUS status;
305 const char *host = lp_parm_string(-1, "torture", "host");
306 const char *share = lp_parm_string(-1, "torture", "share");
307 struct cli_credentials *credentials = cmdline_credentials;
308
309 status = smb2_connect(mem_ctx, host, share, credentials, tree,
310 event_context_find(mem_ctx));
311 if (!NT_STATUS_IS_OK(status)) {
312 printf("Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
313 host, share, nt_errstr(status));
314 return False;
315 }
316 return True;
317 }
318
319
320 /*
321 create and return a handle to a test file
322 */
torture_smb2_testfile(struct smb2_tree * tree,const char * fname,struct smb2_handle * handle)323 NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname,
324 struct smb2_handle *handle)
325 {
326 struct smb2_create io;
327 struct smb2_read r;
328 NTSTATUS status;
329
330 ZERO_STRUCT(io);
331 io.in.oplock_flags = 0;
332 io.in.access_mask = SEC_RIGHTS_FILE_ALL;
333 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
334 io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
335 io.in.share_access =
336 NTCREATEX_SHARE_ACCESS_DELETE|
337 NTCREATEX_SHARE_ACCESS_READ|
338 NTCREATEX_SHARE_ACCESS_WRITE;
339 io.in.create_options = 0;
340 io.in.fname = fname;
341
342 status = smb2_create(tree, tree, &io);
343 NT_STATUS_NOT_OK_RETURN(status);
344
345 *handle = io.out.file.handle;
346
347 ZERO_STRUCT(r);
348 r.in.file.handle = *handle;
349 r.in.length = 5;
350 r.in.offset = 0;
351
352 smb2_read(tree, tree, &r);
353
354 return NT_STATUS_OK;
355 }
356
357 /*
358 create and return a handle to a test directory
359 */
torture_smb2_testdir(struct smb2_tree * tree,const char * fname,struct smb2_handle * handle)360 NTSTATUS torture_smb2_testdir(struct smb2_tree *tree, const char *fname,
361 struct smb2_handle *handle)
362 {
363 struct smb2_create io;
364 NTSTATUS status;
365
366 ZERO_STRUCT(io);
367 io.in.oplock_flags = 0;
368 io.in.access_mask = SEC_RIGHTS_DIR_ALL;
369 io.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
370 io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
371 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE;
372 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
373 io.in.fname = fname;
374
375 status = smb2_create(tree, tree, &io);
376 NT_STATUS_NOT_OK_RETURN(status);
377
378 *handle = io.out.file.handle;
379
380 return NT_STATUS_OK;
381 }
382
383
384 /*
385 create a complex file using the old SMB protocol, to make it easier to
386 find fields in SMB2 getinfo levels
387 */
torture_setup_complex_file(struct smb2_tree * tree,const char * fname)388 NTSTATUS torture_setup_complex_file(struct smb2_tree *tree, const char *fname)
389 {
390 struct smb2_handle handle;
391 NTSTATUS status = smb2_create_complex_file(tree, fname, &handle);
392 NT_STATUS_NOT_OK_RETURN(status);
393 return smb2_util_close(tree, handle);
394 }
395
396
397 /*
398 create a complex dir using the old SMB protocol, to make it easier to
399 find fields in SMB2 getinfo levels
400 */
torture_setup_complex_dir(struct smb2_tree * tree,const char * fname)401 NTSTATUS torture_setup_complex_dir(struct smb2_tree *tree, const char *fname)
402 {
403 struct smb2_handle handle;
404 NTSTATUS status = smb2_create_complex_dir(tree, fname, &handle);
405 NT_STATUS_NOT_OK_RETURN(status);
406 return smb2_util_close(tree, handle);
407 }
408
409
410 /*
411 return a handle to the root of the share
412 */
smb2_util_roothandle(struct smb2_tree * tree,struct smb2_handle * handle)413 NTSTATUS smb2_util_roothandle(struct smb2_tree *tree, struct smb2_handle *handle)
414 {
415 struct smb2_create io;
416 NTSTATUS status;
417
418 ZERO_STRUCT(io);
419 io.in.oplock_flags = 0;
420 io.in.access_mask = SEC_STD_SYNCHRONIZE | SEC_DIR_READ_ATTRIBUTE | SEC_DIR_LIST;
421 io.in.file_attr = 0;
422 io.in.open_disposition = NTCREATEX_DISP_OPEN;
423 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
424 io.in.create_options = NTCREATEX_OPTIONS_ASYNC_ALERT;
425 io.in.fname = "";
426
427 status = smb2_create(tree, tree, &io);
428 NT_STATUS_NOT_OK_RETURN(status);
429
430 *handle = io.out.file.handle;
431
432 return NT_STATUS_OK;
433 }
434