Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : SMB2 dir list test suite
5 :
6 : Copyright (C) Andrew Tridgell 2005
7 : Copyright (C) Zachary Loafman 2009
8 : Copyright (C) Aravind Srinivasan 2009
9 :
10 : This program is free software; you can redistribute it and/or modify
11 : it under the terms of the GNU General Public License as published by
12 : the Free Software Foundation; either version 3 of the License, or
13 : (at your option) any later version.
14 :
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 :
20 : You should have received a copy of the GNU General Public License
21 : along with this program. If not, see <http://www.gnu.org/licenses/>.
22 : */
23 :
24 : #include "includes.h"
25 : #include "libcli/smb2/smb2.h"
26 : #include "libcli/smb2/smb2_calls.h"
27 : #include "libcli/smb_composite/smb_composite.h"
28 : #include "libcli/raw/libcliraw.h"
29 : #include "libcli/raw/raw_proto.h"
30 : #include "libcli/libcli.h"
31 :
32 : #include "torture/torture.h"
33 : #include "torture/smb2/proto.h"
34 : #include "torture/util.h"
35 :
36 : #include "system/filesys.h"
37 : #include "lib/util/tsort.h"
38 :
39 : #define DNAME "smb2_dir"
40 : #define NFILES 100
41 :
42 : struct file_elem {
43 : char *name;
44 : NTTIME create_time;
45 : bool found;
46 : };
47 :
48 0 : static NTSTATUS populate_tree(struct torture_context *tctx,
49 : TALLOC_CTX *mem_ctx,
50 : struct smb2_tree *tree,
51 : struct file_elem *files,
52 : int nfiles,
53 : struct smb2_handle *h_out)
54 : {
55 : struct smb2_create create;
56 0 : char **strs = NULL;
57 : NTSTATUS status;
58 0 : bool ret = true;
59 : int i;
60 :
61 0 : smb2_deltree(tree, DNAME);
62 :
63 0 : ZERO_STRUCT(create);
64 0 : create.in.desired_access = SEC_RIGHTS_DIR_ALL;
65 0 : create.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
66 0 : create.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
67 0 : create.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
68 : NTCREATEX_SHARE_ACCESS_WRITE |
69 : NTCREATEX_SHARE_ACCESS_DELETE;
70 0 : create.in.create_disposition = NTCREATEX_DISP_CREATE;
71 0 : create.in.fname = DNAME;
72 :
73 0 : status = smb2_create(tree, mem_ctx, &create);
74 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
75 0 : *h_out = create.out.file.handle;
76 :
77 0 : ZERO_STRUCT(create);
78 0 : create.in.desired_access = SEC_RIGHTS_FILE_ALL;
79 0 : create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
80 0 : create.in.create_disposition = NTCREATEX_DISP_CREATE;
81 :
82 0 : strs = generate_unique_strs(mem_ctx, 8, nfiles);
83 0 : if (strs == NULL) {
84 0 : status = NT_STATUS_OBJECT_NAME_COLLISION;
85 0 : goto done;
86 : }
87 0 : for (i = 0; i < nfiles; i++) {
88 0 : files[i].name = strs[i];
89 0 : create.in.fname = talloc_asprintf(mem_ctx, "%s\\%s",
90 0 : DNAME, files[i].name);
91 0 : status = smb2_create(tree, mem_ctx, &create);
92 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
93 0 : files[i].create_time = create.out.create_time;
94 0 : smb2_util_close(tree, create.out.file.handle);
95 : }
96 0 : done:
97 0 : if (!ret) {
98 0 : return status;
99 : }
100 :
101 0 : return status;
102 : }
103 :
104 : /*
105 : test find continue
106 : */
107 :
108 0 : static bool test_find(struct torture_context *tctx,
109 : struct smb2_tree *tree)
110 : {
111 0 : TALLOC_CTX *mem_ctx = talloc_new(tctx);
112 : struct smb2_handle h;
113 : struct smb2_find f;
114 : union smb_search_data *d;
115 0 : struct file_elem files[NFILES] = {};
116 : NTSTATUS status;
117 0 : bool ret = true;
118 : unsigned int count;
119 0 : int i, j = 0, file_count = 0;
120 :
121 0 : status = populate_tree(tctx, mem_ctx, tree, files, NFILES, &h);
122 :
123 0 : ZERO_STRUCT(f);
124 0 : f.in.file.handle = h;
125 0 : f.in.pattern = "*";
126 0 : f.in.continue_flags = SMB2_CONTINUE_FLAG_SINGLE;
127 0 : f.in.max_response_size = 0x100;
128 0 : f.in.level = SMB2_FIND_BOTH_DIRECTORY_INFO;
129 :
130 : do {
131 0 : status = smb2_find_level(tree, tree, &f, &count, &d);
132 0 : if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
133 0 : break;
134 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
135 :
136 0 : for (i = 0; i < count; i++) {
137 : bool expected;
138 0 : const char *found = d[i].both_directory_info.name.s;
139 0 : NTTIME ct = d[i].both_directory_info.create_time;
140 :
141 0 : if (!strcmp(found, ".") || !strcmp(found, ".."))
142 0 : continue;
143 :
144 0 : expected = false;
145 0 : for (j = 0; j < NFILES; j++) {
146 0 : if (strcmp(files[j].name, found) != 0) {
147 0 : continue;
148 : }
149 :
150 0 : torture_assert_u64_equal_goto(tctx,
151 : files[j].create_time,
152 : ct, ret, done,
153 : talloc_asprintf(tctx,
154 : "file[%d]\n", j));
155 :
156 0 : files[j].found = true;
157 0 : expected = true;
158 0 : break;
159 : }
160 :
161 0 : if (expected)
162 0 : continue;
163 :
164 0 : torture_result(tctx, TORTURE_FAIL,
165 : "(%s): didn't expect %s\n",
166 : __location__, found);
167 0 : ret = false;
168 0 : goto done;
169 : }
170 :
171 0 : file_count = file_count + i;
172 0 : f.in.continue_flags = 0;
173 0 : f.in.max_response_size = 4096;
174 0 : } while (count != 0);
175 :
176 0 : torture_assert_int_equal_goto(tctx, file_count, NFILES + 2, ret, done,
177 : "");
178 :
179 0 : for (i = 0; i < NFILES; i++) {
180 0 : if (files[j].found)
181 0 : continue;
182 :
183 0 : torture_result(tctx, TORTURE_FAIL,
184 : "(%s): expected to find %s, but didn't\n",
185 : __location__, files[j].name);
186 0 : ret = false;
187 0 : goto done;
188 : }
189 :
190 0 : done:
191 0 : smb2_deltree(tree, DNAME);
192 0 : talloc_free(mem_ctx);
193 :
194 0 : return ret;
195 : }
196 :
197 : /*
198 : test fixed enumeration
199 : */
200 :
201 0 : static bool test_fixed(struct torture_context *tctx,
202 : struct smb2_tree *tree)
203 : {
204 0 : TALLOC_CTX *mem_ctx = talloc_new(tctx);
205 : struct smb2_create create;
206 0 : struct smb2_handle h = {{0}};
207 0 : struct smb2_handle h2 = {{0}};
208 : struct smb2_find f;
209 : union smb_search_data *d;
210 0 : struct file_elem files[NFILES] = {};
211 : NTSTATUS status;
212 0 : bool ret = true;
213 : unsigned int count;
214 : int i;
215 :
216 0 : status = populate_tree(tctx, mem_ctx, tree, files, NFILES, &h);
217 :
218 0 : ZERO_STRUCT(create);
219 0 : create.in.desired_access = SEC_RIGHTS_DIR_ALL;
220 0 : create.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
221 0 : create.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
222 0 : create.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
223 : NTCREATEX_SHARE_ACCESS_WRITE |
224 : NTCREATEX_SHARE_ACCESS_DELETE;
225 0 : create.in.create_disposition = NTCREATEX_DISP_OPEN;
226 0 : create.in.fname = DNAME;
227 :
228 0 : status = smb2_create(tree, mem_ctx, &create);
229 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
230 0 : h2 = create.out.file.handle;
231 :
232 0 : ZERO_STRUCT(f);
233 0 : f.in.file.handle = h;
234 0 : f.in.pattern = "*";
235 0 : f.in.continue_flags = SMB2_CONTINUE_FLAG_SINGLE;
236 0 : f.in.max_response_size = 0x100;
237 0 : f.in.level = SMB2_FIND_BOTH_DIRECTORY_INFO;
238 :
239 : /* Start enumeration on h, then delete all from h2 */
240 0 : status = smb2_find_level(tree, tree, &f, &count, &d);
241 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
242 :
243 0 : f.in.file.handle = h2;
244 :
245 : do {
246 0 : status = smb2_find_level(tree, tree, &f, &count, &d);
247 0 : if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
248 0 : break;
249 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
250 :
251 0 : for (i = 0; i < count; i++) {
252 0 : const char *found = d[i].both_directory_info.name.s;
253 0 : char *path = talloc_asprintf(mem_ctx, "%s\\%s",
254 : DNAME, found);
255 :
256 0 : if (!strcmp(found, ".") || !strcmp(found, ".."))
257 0 : continue;
258 :
259 0 : status = smb2_util_unlink(tree, path);
260 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
261 : "");
262 :
263 0 : talloc_free(path);
264 : }
265 :
266 0 : f.in.continue_flags = 0;
267 0 : f.in.max_response_size = 4096;
268 0 : } while (count != 0);
269 :
270 : /* Now finish h enumeration. */
271 0 : f.in.file.handle = h;
272 :
273 : do {
274 0 : status = smb2_find_level(tree, tree, &f, &count, &d);
275 0 : if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
276 0 : break;
277 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
278 :
279 0 : for (i = 0; i < count; i++) {
280 0 : const char *found = d[i].both_directory_info.name.s;
281 :
282 0 : if (!strcmp(found, ".") || !strcmp(found, ".."))
283 0 : continue;
284 :
285 0 : torture_result(tctx, TORTURE_FAIL,
286 : "(%s): didn't expect %s (count=%u)\n",
287 : __location__, found, count);
288 0 : ret = false;
289 0 : goto done;
290 : }
291 :
292 0 : f.in.continue_flags = 0;
293 0 : f.in.max_response_size = 4096;
294 0 : } while (count != 0);
295 :
296 0 : done:
297 0 : smb2_util_close(tree, h);
298 0 : smb2_util_close(tree, h2);
299 0 : smb2_deltree(tree, DNAME);
300 0 : talloc_free(mem_ctx);
301 :
302 0 : return ret;
303 : }
304 :
305 : static struct {
306 : const char *name;
307 : uint8_t level;
308 : enum smb_search_data_level data_level;
309 : int name_offset;
310 : int resume_key_offset;
311 : uint32_t capability_mask;
312 : NTSTATUS status;
313 : union smb_search_data data;
314 : } levels[] = {
315 : {
316 : .name = "SMB2_FIND_DIRECTORY_INFO",
317 : .level = SMB2_FIND_DIRECTORY_INFO,
318 : .data_level = RAW_SEARCH_DATA_DIRECTORY_INFO,
319 : .name_offset = offsetof(union smb_search_data,
320 : directory_info.name.s),
321 : .resume_key_offset = offsetof(union smb_search_data,
322 : directory_info.file_index),
323 : },
324 : {
325 : .name = "SMB2_FIND_FULL_DIRECTORY_INFO",
326 : .level = SMB2_FIND_FULL_DIRECTORY_INFO,
327 : .data_level = RAW_SEARCH_DATA_FULL_DIRECTORY_INFO,
328 : .name_offset = offsetof(union smb_search_data,
329 : full_directory_info.name.s),
330 : .resume_key_offset = offsetof(union smb_search_data,
331 : full_directory_info.file_index),
332 : },
333 : {
334 : .name = "SMB2_FIND_NAME_INFO",
335 : .level = SMB2_FIND_NAME_INFO,
336 : .data_level = RAW_SEARCH_DATA_NAME_INFO,
337 : .name_offset = offsetof(union smb_search_data,
338 : name_info.name.s),
339 : .resume_key_offset = offsetof(union smb_search_data,
340 : name_info.file_index),
341 : },
342 : {
343 : .name = "SMB2_FIND_BOTH_DIRECTORY_INFO",
344 : .level = SMB2_FIND_BOTH_DIRECTORY_INFO,
345 : .data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
346 : .name_offset = offsetof(union smb_search_data,
347 : both_directory_info.name.s),
348 : .resume_key_offset = offsetof(union smb_search_data,
349 : both_directory_info.file_index),
350 : },
351 : {
352 : .name = "SMB2_FIND_ID_FULL_DIRECTORY_INFO",
353 : .level = SMB2_FIND_ID_FULL_DIRECTORY_INFO,
354 : .data_level = RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO,
355 : .name_offset = offsetof(union smb_search_data,
356 : id_full_directory_info.name.s),
357 : .resume_key_offset = offsetof(union smb_search_data,
358 : id_full_directory_info.file_index),
359 : },
360 : {
361 : .name = "SMB2_FIND_ID_BOTH_DIRECTORY_INFO",
362 : .level = SMB2_FIND_ID_BOTH_DIRECTORY_INFO,
363 : .data_level = RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO,
364 : .name_offset = offsetof(union smb_search_data,
365 : id_both_directory_info.name.s),
366 : .resume_key_offset = offsetof(union smb_search_data,
367 : id_both_directory_info.file_index),
368 : }
369 : };
370 :
371 : /*
372 : extract the name from a smb_data structure and level
373 : */
374 0 : static const char *extract_name(union smb_search_data *data,
375 : uint8_t level,
376 : enum smb_search_data_level data_level)
377 : {
378 : int i;
379 0 : for (i=0;i<ARRAY_SIZE(levels);i++) {
380 0 : if (level == levels[i].level &&
381 0 : data_level == levels[i].data_level) {
382 0 : return *(const char **)(levels[i].name_offset + (char *)data);
383 : }
384 : }
385 0 : return NULL;
386 : }
387 :
388 : /* find a level in the table by name */
389 0 : static union smb_search_data *find(const char *name)
390 : {
391 : int i;
392 0 : for (i=0;i<ARRAY_SIZE(levels);i++) {
393 0 : if (NT_STATUS_IS_OK(levels[i].status) &&
394 0 : strcmp(levels[i].name, name) == 0) {
395 0 : return &levels[i].data;
396 : }
397 : }
398 0 : return NULL;
399 : }
400 :
401 0 : static bool fill_level_data(TALLOC_CTX *mem_ctx,
402 : union smb_search_data *data,
403 : union smb_search_data *d,
404 : unsigned int count,
405 : uint8_t level,
406 : enum smb_search_data_level data_level)
407 : {
408 : int i;
409 0 : const char *sname = NULL;
410 0 : for (i=0; i < count ; i++) {
411 0 : sname = extract_name(&d[i], level, data_level);
412 0 : if (sname == NULL)
413 0 : return false;
414 0 : if (!strcmp(sname, ".") || !strcmp(sname, ".."))
415 0 : continue;
416 0 : *data = d[i];
417 : }
418 0 : return true;
419 : }
420 :
421 :
422 0 : NTSTATUS torture_single_file_search(struct smb2_tree *tree,
423 : TALLOC_CTX *mem_ctx,
424 : const char *pattern,
425 : uint8_t level,
426 : enum smb_search_data_level data_level,
427 : int idx,
428 : union smb_search_data *d,
429 : unsigned int *count,
430 : struct smb2_handle *h)
431 : {
432 : struct smb2_find f;
433 : NTSTATUS status;
434 :
435 0 : ZERO_STRUCT(f);
436 0 : f.in.file.handle = *h;
437 0 : f.in.pattern = pattern;
438 0 : f.in.continue_flags = SMB2_CONTINUE_FLAG_RESTART;
439 0 : f.in.max_response_size = 0x100;
440 0 : f.in.level = level;
441 :
442 0 : status = smb2_find_level(tree, tree, &f, count, &d);
443 0 : if (NT_STATUS_IS_OK(status))
444 0 : fill_level_data(mem_ctx, &levels[idx].data, d, *count, level,
445 : data_level);
446 0 : return status;
447 : }
448 :
449 : /*
450 : basic testing of all File Information Classes using a single file
451 : */
452 0 : static bool test_one_file(struct torture_context *tctx,
453 : struct smb2_tree *tree)
454 : {
455 0 : TALLOC_CTX *mem_ctx = talloc_new(tctx);
456 0 : bool ret = true;
457 0 : const char *fname = "torture_search.txt";
458 : NTSTATUS status;
459 : int i;
460 : unsigned int count;
461 : union smb_fileinfo all_info2, alt_info, internal_info;
462 : union smb_search_data *s;
463 : union smb_search_data d;
464 : struct smb2_handle h, h2;
465 :
466 0 : status = torture_smb2_testdir(tree, DNAME, &h);
467 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
468 :
469 0 : status = smb2_create_complex_file(tctx, tree, DNAME "\\torture_search.txt",
470 : &h2);
471 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
472 :
473 : /* call all the File Information Classes */
474 0 : for (i=0;i<ARRAY_SIZE(levels);i++) {
475 0 : torture_comment(tctx, "Testing %s %d\n", levels[i].name,
476 0 : levels[i].level);
477 :
478 0 : levels[i].status = torture_single_file_search(tree, mem_ctx,
479 0 : fname, levels[i].level, levels[i].data_level,
480 : i, &d, &count, &h);
481 0 : torture_assert_ntstatus_ok_goto(tctx, levels[i].status, ret,
482 : done, "");
483 : }
484 :
485 : /* get the all_info file into to check against */
486 0 : all_info2.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
487 0 : all_info2.generic.in.file.handle = h2;
488 0 : status = smb2_getinfo_file(tree, tctx, &all_info2);
489 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
490 : "RAW_FILEINFO_ALL_INFO failed");
491 :
492 0 : alt_info.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
493 0 : alt_info.generic.in.file.handle = h2;
494 0 : status = smb2_getinfo_file(tree, tctx, &alt_info);
495 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
496 : "RAW_FILEINFO_ALT_NAME_INFO failed");
497 :
498 0 : internal_info.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
499 0 : internal_info.generic.in.file.handle = h2;
500 0 : status = smb2_getinfo_file(tree, tctx, &internal_info);
501 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
502 : "RAW_FILEINFO_INTERNAL_INFORMATION "
503 : "failed");
504 :
505 : #define CHECK_VAL(name, sname1, field1, v, sname2, field2) do { \
506 : s = find(name); \
507 : if (s) { \
508 : if ((s->sname1.field1) != (v.sname2.out.field2)) { \
509 : torture_result(tctx, TORTURE_FAIL, \
510 : "(%s) %s/%s [0x%x] != %s/%s [0x%x]\n", \
511 : __location__, \
512 : #sname1, #field1, (int)s->sname1.field1, \
513 : #sname2, #field2, (int)v.sname2.out.field2); \
514 : ret = false; \
515 : } \
516 : }} while (0)
517 :
518 : #define CHECK_TIME(name, sname1, field1, v, sname2, field2) do { \
519 : s = find(name); \
520 : if (s) { \
521 : if (s->sname1.field1 != \
522 : (~1 & nt_time_to_unix(v.sname2.out.field2))) { \
523 : torture_result(tctx, TORTURE_FAIL, \
524 : "(%s) %s/%s [%s] != %s/%s [%s]\n", \
525 : __location__, \
526 : #sname1, #field1, \
527 : timestring(tctx, s->sname1.field1), \
528 : #sname2, #field2, \
529 : nt_time_string(tctx, v.sname2.out.field2)); \
530 : ret = false; \
531 : } \
532 : }} while (0)
533 :
534 : #define CHECK_NTTIME(name, sname1, field1, v, sname2, field2) do { \
535 : s = find(name); \
536 : if (s) { \
537 : if (s->sname1.field1 != v.sname2.out.field2) { \
538 : torture_result(tctx, TORTURE_FAIL, \
539 : "(%s) %s/%s [%s] != %s/%s [%s]\n", \
540 : __location__, \
541 : #sname1, #field1, \
542 : nt_time_string(tctx, s->sname1.field1), \
543 : #sname2, #field2, \
544 : nt_time_string(tctx, v.sname2.out.field2)); \
545 : ret = false; \
546 : } \
547 : }} while (0)
548 :
549 : #define CHECK_STR(name, sname1, field1, v, sname2, field2) do { \
550 : s = find(name); \
551 : if (s) { \
552 : if (!s->sname1.field1 || \
553 : strcmp(s->sname1.field1, v.sname2.out.field2.s)) { \
554 : torture_result(tctx, TORTURE_FAIL, \
555 : "(%s) %s/%s [%s] != %s/%s [%s]\n", \
556 : __location__, \
557 : #sname1, #field1, s->sname1.field1, \
558 : #sname2, #field2, v.sname2.out.field2.s); \
559 : ret = false; \
560 : } \
561 : }} while (0)
562 :
563 : #define CHECK_WSTR(name, sname1, field1, v, sname2, field2, flags) do { \
564 : s = find(name); \
565 : if (s) { \
566 : if (!s->sname1.field1.s || \
567 : strcmp(s->sname1.field1.s, v.sname2.out.field2.s)) { \
568 : torture_result(tctx, TORTURE_FAIL, \
569 : "(%s) %s/%s [%s] != %s/%s [%s]\n", \
570 : __location__, \
571 : #sname1, #field1, s->sname1.field1.s, \
572 : #sname2, #field2, v.sname2.out.field2.s); \
573 : ret = false; \
574 : } \
575 : }} while (0)
576 :
577 : #define CHECK_NAME(name, sname1, field1, fname, flags) do { \
578 : s = find(name); \
579 : if (s) { \
580 : if (!s->sname1.field1.s || \
581 : strcmp(s->sname1.field1.s, fname)) { \
582 : torture_result(tctx, TORTURE_FAIL, \
583 : "(%s) %s/%s [%s] != %s\n", \
584 : __location__, \
585 : #sname1, #field1, s->sname1.field1.s, fname); \
586 : ret = false; \
587 : } \
588 : }} while (0)
589 :
590 : #define CHECK_UNIX_NAME(name, sname1, field1, fname, flags) do { \
591 : s = find(name); \
592 : if (s) { \
593 : if (!s->sname1.field1 || \
594 : strcmp(s->sname1.field1, fname)) { \
595 : torture_result(tctx, TORTURE_FAIL, \
596 : "(%s) %s/%s [%s] != %s\n", \
597 : __location__, \
598 : #sname1, #field1, s->sname1.field1, fname); \
599 : ret = false; \
600 : } \
601 : }} while (0)
602 :
603 : /* check that all the results are as expected */
604 0 : CHECK_VAL("SMB2_FIND_DIRECTORY_INFO", directory_info, attrib, all_info2, all_info2, attrib);
605 0 : CHECK_VAL("SMB2_FIND_FULL_DIRECTORY_INFO", full_directory_info, attrib, all_info2, all_info2, attrib);
606 0 : CHECK_VAL("SMB2_FIND_BOTH_DIRECTORY_INFO", both_directory_info, attrib, all_info2, all_info2, attrib);
607 0 : CHECK_VAL("SMB2_FIND_ID_FULL_DIRECTORY_INFO", id_full_directory_info, attrib, all_info2, all_info2, attrib);
608 0 : CHECK_VAL("SMB2_FIND_ID_BOTH_DIRECTORY_INFO", id_both_directory_info, attrib, all_info2, all_info2, attrib);
609 :
610 0 : CHECK_NTTIME("SMB2_FIND_DIRECTORY_INFO", directory_info, write_time, all_info2, all_info2, write_time);
611 0 : CHECK_NTTIME("SMB2_FIND_FULL_DIRECTORY_INFO", full_directory_info, write_time, all_info2, all_info2, write_time);
612 0 : CHECK_NTTIME("SMB2_FIND_BOTH_DIRECTORY_INFO", both_directory_info, write_time, all_info2, all_info2, write_time);
613 0 : CHECK_NTTIME("SMB2_FIND_ID_FULL_DIRECTORY_INFO", id_full_directory_info, write_time, all_info2, all_info2, write_time);
614 0 : CHECK_NTTIME("SMB2_FIND_ID_BOTH_DIRECTORY_INFO", id_both_directory_info, write_time, all_info2, all_info2, write_time);
615 :
616 0 : CHECK_NTTIME("SMB2_FIND_DIRECTORY_INFO", directory_info, create_time, all_info2, all_info2, create_time);
617 0 : CHECK_NTTIME("SMB2_FIND_FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info2, all_info2, create_time);
618 0 : CHECK_NTTIME("SMB2_FIND_BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info2, all_info2, create_time);
619 0 : CHECK_NTTIME("SMB2_FIND_ID_FULL_DIRECTORY_INFO", id_full_directory_info, create_time, all_info2, all_info2, create_time);
620 0 : CHECK_NTTIME("SMB2_FIND_ID_BOTH_DIRECTORY_INFO", id_both_directory_info, create_time, all_info2, all_info2, create_time);
621 :
622 0 : CHECK_NTTIME("SMB2_FIND_DIRECTORY_INFO", directory_info, access_time, all_info2, all_info2, access_time);
623 0 : CHECK_NTTIME("SMB2_FIND_FULL_DIRECTORY_INFO", full_directory_info, access_time, all_info2, all_info2, access_time);
624 0 : CHECK_NTTIME("SMB2_FIND_BOTH_DIRECTORY_INFO", both_directory_info, access_time, all_info2, all_info2, access_time);
625 0 : CHECK_NTTIME("SMB2_FIND_ID_FULL_DIRECTORY_INFO", id_full_directory_info, access_time, all_info2, all_info2, access_time);
626 0 : CHECK_NTTIME("SMB2_FIND_ID_BOTH_DIRECTORY_INFO", id_both_directory_info, access_time, all_info2, all_info2, access_time);
627 :
628 0 : CHECK_NTTIME("SMB2_FIND_DIRECTORY_INFO", directory_info, change_time, all_info2, all_info2, change_time);
629 0 : CHECK_NTTIME("SMB2_FIND_FULL_DIRECTORY_INFO", full_directory_info, change_time, all_info2, all_info2, change_time);
630 0 : CHECK_NTTIME("SMB2_FIND_BOTH_DIRECTORY_INFO", both_directory_info, change_time, all_info2, all_info2, change_time);
631 0 : CHECK_NTTIME("SMB2_FIND_ID_FULL_DIRECTORY_INFO", id_full_directory_info, change_time, all_info2, all_info2, change_time);
632 0 : CHECK_NTTIME("SMB2_FIND_ID_BOTH_DIRECTORY_INFO", id_both_directory_info, change_time, all_info2, all_info2, change_time);
633 :
634 0 : CHECK_VAL("SMB2_FIND_DIRECTORY_INFO", directory_info, size, all_info2, all_info2, size);
635 0 : CHECK_VAL("SMB2_FIND_FULL_DIRECTORY_INFO", full_directory_info, size, all_info2, all_info2, size);
636 0 : CHECK_VAL("SMB2_FIND_BOTH_DIRECTORY_INFO", both_directory_info, size, all_info2, all_info2, size);
637 0 : CHECK_VAL("SMB2_FIND_ID_FULL_DIRECTORY_INFO", id_full_directory_info, size, all_info2, all_info2, size);
638 0 : CHECK_VAL("SMB2_FIND_ID_BOTH_DIRECTORY_INFO", id_both_directory_info, size, all_info2, all_info2, size);
639 :
640 0 : CHECK_VAL("SMB2_FIND_DIRECTORY_INFO", directory_info, alloc_size, all_info2, all_info2, alloc_size);
641 0 : CHECK_VAL("SMB2_FIND_FULL_DIRECTORY_INFO", full_directory_info, alloc_size, all_info2, all_info2, alloc_size);
642 0 : CHECK_VAL("SMB2_FIND_BOTH_DIRECTORY_INFO", both_directory_info, alloc_size, all_info2, all_info2, alloc_size);
643 0 : CHECK_VAL("SMB2_FIND_ID_FULL_DIRECTORY_INFO", id_full_directory_info, alloc_size, all_info2, all_info2, alloc_size);
644 0 : CHECK_VAL("SMB2_FIND_ID_BOTH_DIRECTORY_INFO", id_both_directory_info, alloc_size, all_info2, all_info2, alloc_size);
645 :
646 0 : CHECK_VAL("SMB2_FIND_FULL_DIRECTORY_INFO", full_directory_info, ea_size, all_info2, all_info2, ea_size);
647 0 : CHECK_VAL("SMB2_FIND_BOTH_DIRECTORY_INFO", both_directory_info, ea_size, all_info2, all_info2, ea_size);
648 0 : CHECK_VAL("SMB2_FIND_ID_FULL_DIRECTORY_INFO", id_full_directory_info, ea_size, all_info2, all_info2, ea_size);
649 0 : CHECK_VAL("SMB2_FIND_ID_BOTH_DIRECTORY_INFO", id_both_directory_info, ea_size, all_info2, all_info2, ea_size);
650 :
651 0 : CHECK_NAME("SMB2_FIND_DIRECTORY_INFO", directory_info, name, fname, STR_TERMINATE_ASCII);
652 0 : CHECK_NAME("SMB2_FIND_FULL_DIRECTORY_INFO", full_directory_info, name, fname, STR_TERMINATE_ASCII);
653 0 : CHECK_NAME("SMB2_FIND_NAME_INFO", name_info, name, fname, STR_TERMINATE_ASCII);
654 0 : CHECK_NAME("SMB2_FIND_BOTH_DIRECTORY_INFO", both_directory_info, name, fname, STR_TERMINATE_ASCII);
655 0 : CHECK_NAME("SMB2_FIND_ID_FULL_DIRECTORY_INFO", id_full_directory_info, name, fname, STR_TERMINATE_ASCII);
656 0 : CHECK_NAME("SMB2_FIND_ID_BOTH_DIRECTORY_INFO", id_both_directory_info, name, fname, STR_TERMINATE_ASCII);
657 :
658 0 : CHECK_WSTR("SMB2_FIND_BOTH_DIRECTORY_INFO", both_directory_info, short_name, alt_info, alt_name_info, fname, STR_UNICODE);
659 :
660 0 : CHECK_VAL("SMB2_FIND_ID_FULL_DIRECTORY_INFO", id_full_directory_info, file_id, internal_info, internal_information, file_id);
661 0 : CHECK_VAL("SMB2_FIND_ID_BOTH_DIRECTORY_INFO", id_both_directory_info, file_id, internal_info, internal_information, file_id);
662 :
663 0 : done:
664 0 : smb2_util_close(tree, h);
665 0 : smb2_util_unlink(tree, fname);
666 0 : talloc_free(mem_ctx);
667 :
668 0 : return ret;
669 : }
670 :
671 :
672 : struct multiple_result {
673 : TALLOC_CTX *tctx;
674 : int count;
675 : union smb_search_data *list;
676 : };
677 :
678 0 : bool fill_result(void *private_data,
679 : union smb_search_data *file,
680 : int count,
681 : uint8_t level,
682 : enum smb_search_data_level data_level)
683 : {
684 : int i;
685 : const char *sname;
686 0 : struct multiple_result *data = (struct multiple_result *)private_data;
687 :
688 0 : for (i=0; i<count; i++) {
689 0 : sname = extract_name(&file[i], level, data_level);
690 0 : if (!strcmp(sname, ".") || !(strcmp(sname, "..")))
691 0 : continue;
692 0 : data->count++;
693 0 : data->list = talloc_realloc(data->tctx,
694 : data->list,
695 : union smb_search_data,
696 : data->count);
697 0 : data->list[data->count-1] = file[i];
698 : }
699 0 : return true;
700 : }
701 :
702 : enum continue_type {CONT_SINGLE, CONT_INDEX, CONT_RESTART, CONT_REOPEN};
703 :
704 0 : static NTSTATUS multiple_smb2_search(struct smb2_tree *tree,
705 : TALLOC_CTX *tctx,
706 : const char *pattern,
707 : uint8_t level,
708 : enum smb_search_data_level data_level,
709 : enum continue_type cont_type,
710 : void *data,
711 : struct smb2_handle *h)
712 : {
713 : struct smb2_find f;
714 0 : bool ret = true;
715 0 : unsigned int count = 0;
716 : union smb_search_data *d;
717 : NTSTATUS status;
718 0 : struct multiple_result *result = (struct multiple_result *)data;
719 :
720 0 : ZERO_STRUCT(f);
721 0 : f.in.file.handle = *h;
722 0 : f.in.pattern = pattern;
723 0 : f.in.max_response_size = 0x1000;
724 0 : f.in.level = level;
725 :
726 : /* The search should start from the beginning everytime */
727 0 : f.in.continue_flags = SMB2_CONTINUE_FLAG_RESTART;
728 0 : if (cont_type == CONT_REOPEN) {
729 0 : f.in.continue_flags = SMB2_CONTINUE_FLAG_REOPEN;
730 : }
731 :
732 : do {
733 0 : status = smb2_find_level(tree, tree, &f, &count, &d);
734 0 : if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
735 0 : break;
736 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
737 0 : if (!fill_result(result, d, count, level, data_level)) {
738 0 : return NT_STATUS_UNSUCCESSFUL;
739 : }
740 :
741 0 : if (count == 0 || result == NULL || result->count == 0) {
742 0 : return NT_STATUS_UNSUCCESSFUL;
743 : }
744 :
745 : /*
746 : * After the first iteration is complete set the CONTINUE
747 : * FLAGS appropriately
748 : */
749 0 : switch (cont_type) {
750 0 : case CONT_INDEX:
751 0 : f.in.continue_flags = SMB2_CONTINUE_FLAG_INDEX;
752 0 : switch (data_level) {
753 0 : case RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO:
754 0 : f.in.file_index =
755 0 : result->list[result->count-1].both_directory_info.file_index;
756 0 : break;
757 0 : case RAW_SEARCH_DATA_DIRECTORY_INFO:
758 0 : f.in.file_index =
759 0 : result->list[result->count-1].directory_info.file_index;
760 0 : break;
761 0 : case RAW_SEARCH_DATA_FULL_DIRECTORY_INFO:
762 0 : f.in.file_index =
763 0 : result->list[result->count-1].full_directory_info.file_index;
764 0 : break;
765 0 : case RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO:
766 0 : f.in.file_index =
767 0 : result->list[result->count-1].id_full_directory_info.file_index;
768 0 : break;
769 0 : case RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO:
770 0 : f.in.file_index =
771 0 : result->list[result->count-1].id_both_directory_info.file_index;
772 0 : break;
773 0 : default:
774 0 : return NT_STATUS_INVALID_PARAMETER;
775 : }
776 0 : break;
777 0 : case CONT_SINGLE:
778 0 : f.in.continue_flags = SMB2_CONTINUE_FLAG_SINGLE;
779 0 : break;
780 0 : case CONT_RESTART:
781 : default:
782 : /* we should prevent staying in the loop
783 : * forever */
784 0 : f.in.continue_flags = 0;
785 0 : break;
786 : }
787 0 : } while (count != 0);
788 0 : done:
789 0 : if (!ret) {
790 0 : return status;
791 : }
792 0 : return status;
793 : }
794 :
795 :
796 : static enum smb_search_data_level compare_data_level;
797 : uint8_t level_sort;
798 :
799 0 : static int search_compare(union smb_search_data *d1,
800 : union smb_search_data *d2)
801 : {
802 : const char *s1, *s2;
803 :
804 0 : s1 = extract_name(d1, level_sort, compare_data_level);
805 0 : s2 = extract_name(d2, level_sort, compare_data_level);
806 0 : return strcmp_safe(s1, s2);
807 : }
808 :
809 : /*
810 : basic testing of search calls using many files
811 : */
812 0 : static bool test_many_files(struct torture_context *tctx,
813 : struct smb2_tree *tree)
814 : {
815 0 : TALLOC_CTX *mem_ctx = talloc_new(tctx);
816 0 : const int num_files = 700;
817 : int i, t;
818 : char *fname;
819 0 : bool ret = true;
820 : NTSTATUS status;
821 : struct multiple_result result;
822 : struct smb2_create create;
823 : struct smb2_handle h;
824 : struct {
825 : const char *name;
826 : const char *cont_name;
827 : uint8_t level;
828 : enum smb_search_data_level data_level;
829 : enum continue_type cont_type;
830 0 : } search_types[] = {
831 : {"SMB2_FIND_BOTH_DIRECTORY_INFO", "SINGLE", SMB2_FIND_BOTH_DIRECTORY_INFO, RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_SINGLE},
832 : {"SMB2_FIND_BOTH_DIRECTORY_INFO", "INDEX", SMB2_FIND_BOTH_DIRECTORY_INFO, RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_INDEX},
833 : {"SMB2_FIND_BOTH_DIRECTORY_INFO", "RESTART", SMB2_FIND_BOTH_DIRECTORY_INFO, RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_RESTART},
834 : {"SMB2_FIND_BOTH_DIRECTORY_INFO", "REOPEN", SMB2_FIND_BOTH_DIRECTORY_INFO, RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_REOPEN},
835 : {"SMB2_FIND_DIRECTORY_INFO", "SINGLE", SMB2_FIND_DIRECTORY_INFO, RAW_SEARCH_DATA_DIRECTORY_INFO, CONT_SINGLE},
836 : {"SMB2_FIND_DIRECTORY_INFO", "INDEX", SMB2_FIND_DIRECTORY_INFO, RAW_SEARCH_DATA_DIRECTORY_INFO, CONT_INDEX},
837 : {"SMB2_FIND_DIRECTORY_INFO", "RESTART", SMB2_FIND_DIRECTORY_INFO, RAW_SEARCH_DATA_DIRECTORY_INFO, CONT_RESTART},
838 : {"SMB2_FIND_DIRECTORY_INFO", "REOPEN", SMB2_FIND_DIRECTORY_INFO, RAW_SEARCH_DATA_DIRECTORY_INFO, CONT_REOPEN},
839 : {"SMB2_FIND_FULL_DIRECTORY_INFO", "SINGLE", SMB2_FIND_FULL_DIRECTORY_INFO, RAW_SEARCH_DATA_FULL_DIRECTORY_INFO, CONT_SINGLE},
840 : {"SMB2_FIND_FULL_DIRECTORY_INFO", "INDEX", SMB2_FIND_FULL_DIRECTORY_INFO, RAW_SEARCH_DATA_FULL_DIRECTORY_INFO, CONT_INDEX},
841 : {"SMB2_FIND_FULL_DIRECTORY_INFO", "RESTART", SMB2_FIND_FULL_DIRECTORY_INFO, RAW_SEARCH_DATA_FULL_DIRECTORY_INFO, CONT_RESTART},
842 : {"SMB2_FIND_FULL_DIRECTORY_INFO", "REOPEN", SMB2_FIND_FULL_DIRECTORY_INFO, RAW_SEARCH_DATA_FULL_DIRECTORY_INFO, CONT_REOPEN},
843 : {"SMB2_FIND_ID_FULL_DIRECTORY_INFO", "SINGLE", SMB2_FIND_ID_FULL_DIRECTORY_INFO, RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_SINGLE},
844 : {"SMB2_FIND_ID_FULL_DIRECTORY_INFO", "INDEX", SMB2_FIND_ID_FULL_DIRECTORY_INFO, RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_INDEX},
845 : {"SMB2_FIND_ID_FULL_DIRECTORY_INFO", "RESTART", SMB2_FIND_ID_FULL_DIRECTORY_INFO, RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_RESTART},
846 : {"SMB2_FIND_ID_FULL_DIRECTORY_INFO", "REOPEN", SMB2_FIND_ID_FULL_DIRECTORY_INFO, RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_REOPEN},
847 : {"SMB2_FIND_ID_BOTH_DIRECTORY_INFO", "SINGLE", SMB2_FIND_ID_BOTH_DIRECTORY_INFO, RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_SINGLE},
848 : {"SMB2_FIND_ID_BOTH_DIRECTORY_INFO", "INDEX", SMB2_FIND_ID_BOTH_DIRECTORY_INFO, RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_INDEX},
849 : {"SMB2_FIND_ID_BOTH_DIRECTORY_INFO", "RESTART", SMB2_FIND_ID_BOTH_DIRECTORY_INFO, RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_RESTART},
850 : {"SMB2_FIND_ID_BOTH_DIRECTORY_INFO", "REOPEN", SMB2_FIND_ID_BOTH_DIRECTORY_INFO, RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_REOPEN},
851 : };
852 :
853 0 : smb2_deltree(tree, DNAME);
854 0 : status = torture_smb2_testdir(tree, DNAME, &h);
855 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
856 :
857 0 : torture_comment(tctx, "Testing with %d files\n", num_files);
858 0 : ZERO_STRUCT(create);
859 0 : create.in.desired_access = SEC_RIGHTS_FILE_ALL;
860 0 : create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
861 0 : create.in.create_disposition = NTCREATEX_DISP_CREATE;
862 :
863 0 : for (i=num_files-1;i>=0;i--) {
864 0 : fname = talloc_asprintf(mem_ctx, DNAME "\\t%03d-%d.txt", i, i);
865 0 : create.in.fname = talloc_asprintf(mem_ctx, "%s", fname);
866 0 : status = smb2_create(tree, mem_ctx, &create);
867 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
868 0 : smb2_util_close(tree, create.out.file.handle);
869 0 : talloc_free(fname);
870 : }
871 :
872 0 : for (t=0;t<ARRAY_SIZE(search_types);t++) {
873 0 : ZERO_STRUCT(result);
874 0 : result.tctx = talloc_new(tctx);
875 :
876 0 : torture_comment(tctx,
877 : "Continue %s via %s\n", search_types[t].name,
878 : search_types[t].cont_name);
879 0 : status = multiple_smb2_search(tree, tctx, "*",
880 0 : search_types[t].level,
881 : search_types[t].data_level,
882 : search_types[t].cont_type,
883 : &result, &h);
884 :
885 0 : torture_assert_int_equal_goto(tctx, result.count, num_files,
886 : ret, done, "");
887 :
888 0 : compare_data_level = search_types[t].data_level;
889 0 : level_sort = search_types[t].level;
890 :
891 0 : TYPESAFE_QSORT(result.list, result.count, search_compare);
892 :
893 0 : for (i=0;i<result.count;i++) {
894 : const char *s;
895 0 : s = extract_name(&result.list[i],
896 0 : search_types[t].level,
897 : compare_data_level);
898 0 : fname = talloc_asprintf(mem_ctx, "t%03d-%d.txt", i, i);
899 0 : torture_assert_str_equal_goto(tctx, s, fname, ret,
900 : done, "Incorrect name");
901 0 : talloc_free(fname);
902 : }
903 0 : talloc_free(result.tctx);
904 : }
905 :
906 0 : done:
907 0 : smb2_util_close(tree, h);
908 0 : smb2_deltree(tree, DNAME);
909 0 : talloc_free(mem_ctx);
910 :
911 0 : return ret;
912 : }
913 :
914 : /*
915 : check an individual file result
916 : */
917 0 : static bool check_result(struct torture_context *tctx,
918 : struct multiple_result *result,
919 : const char *name,
920 : bool exist,
921 : uint32_t attrib)
922 : {
923 : int i;
924 0 : for (i=0;i<result->count;i++) {
925 0 : if (strcmp(name,
926 0 : result->list[i].both_directory_info.name.s) == 0) {
927 0 : break;
928 : }
929 : }
930 0 : if (i == result->count) {
931 0 : if (exist) {
932 0 : torture_result(tctx, TORTURE_FAIL,
933 : "failed: '%s' should exist with attribute %s\n",
934 0 : name, attrib_string(result->list, attrib));
935 0 : return false;
936 : }
937 0 : return true;
938 : }
939 :
940 0 : if (!exist) {
941 0 : torture_result(tctx, TORTURE_FAIL,
942 : "failed: '%s' should NOT exist (has attribute %s)\n",
943 0 : name, attrib_string(result->list,
944 0 : result->list[i].both_directory_info.attrib));
945 0 : return false;
946 : }
947 :
948 0 : if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
949 0 : torture_result(tctx, TORTURE_FAIL,
950 : "failed: '%s' should have attribute 0x%x (has 0x%x)\n",
951 0 : name, attrib, result->list[i].both_directory_info.attrib);
952 0 : return false;
953 : }
954 0 : return true;
955 : }
956 :
957 : /*
958 : test what happens when the directory is modified during a search
959 : */
960 0 : static bool test_modify_search(struct torture_context *tctx,
961 : struct smb2_tree *tree)
962 : {
963 : struct multiple_result result;
964 : union smb_setfileinfo sfinfo;
965 0 : TALLOC_CTX *mem_ctx = talloc_new(tctx);
966 : struct smb2_create create;
967 : struct smb2_handle h;
968 : struct smb2_find f;
969 : union smb_search_data *d;
970 0 : struct file_elem files[703] = {};
971 0 : int num_files = ARRAY_SIZE(files)-3;
972 : NTSTATUS status;
973 0 : bool ret = true;
974 : int i;
975 : unsigned int count;
976 :
977 0 : smb2_deltree(tree, DNAME);
978 :
979 0 : status = torture_smb2_testdir(tree, DNAME, &h);
980 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
981 :
982 0 : torture_comment(tctx, "Creating %d files\n", num_files);
983 :
984 0 : ZERO_STRUCT(create);
985 0 : create.in.desired_access = SEC_RIGHTS_FILE_ALL;
986 0 : create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
987 0 : create.in.create_disposition = NTCREATEX_DISP_CREATE;
988 :
989 0 : for (i = num_files-1; i >= 0; i--) {
990 0 : files[i].name = talloc_asprintf(mem_ctx, "t%03d-%d.txt", i, i);
991 0 : create.in.fname = talloc_asprintf(mem_ctx, "%s\\%s",
992 : DNAME, files[i].name);
993 0 : status = smb2_create(tree, mem_ctx, &create);
994 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
995 0 : smb2_util_close(tree, create.out.file.handle);
996 : }
997 :
998 0 : torture_comment(tctx, "pulling the first two files\n");
999 0 : ZERO_STRUCT(result);
1000 0 : result.tctx = talloc_new(tctx);
1001 :
1002 0 : ZERO_STRUCT(f);
1003 0 : f.in.file.handle = h;
1004 0 : f.in.pattern = "*";
1005 0 : f.in.continue_flags = SMB2_CONTINUE_FLAG_SINGLE;
1006 0 : f.in.max_response_size = 0x100;
1007 0 : f.in.level = SMB2_FIND_BOTH_DIRECTORY_INFO;
1008 :
1009 : do {
1010 0 : status = smb2_find_level(tree, tree, &f, &count, &d);
1011 0 : if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
1012 0 : break;
1013 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1014 0 : if (!fill_result(&result, d, count, f.in.level,
1015 : RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO)) {
1016 0 : ret = false;
1017 0 : goto done;
1018 : }
1019 0 : } while (result.count < 2);
1020 :
1021 0 : torture_comment(tctx, "Changing attributes and deleting\n");
1022 :
1023 0 : ZERO_STRUCT(create);
1024 0 : create.in.desired_access = SEC_RIGHTS_FILE_ALL;
1025 0 : create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
1026 0 : create.in.create_disposition = NTCREATEX_DISP_CREATE;
1027 :
1028 0 : files[num_files].name = talloc_asprintf(mem_ctx, "T003-03.txt.2");
1029 0 : create.in.fname = talloc_asprintf(mem_ctx, "%s\\%s", DNAME,
1030 : files[num_files].name);
1031 0 : status = smb2_create(tree, mem_ctx, &create);
1032 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1033 0 : smb2_util_close(tree, create.out.file.handle);
1034 :
1035 0 : ZERO_STRUCT(create);
1036 0 : create.in.desired_access = SEC_RIGHTS_FILE_ALL;
1037 0 : create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
1038 0 : create.in.create_disposition = NTCREATEX_DISP_CREATE;
1039 :
1040 0 : files[num_files + 1].name = talloc_asprintf(mem_ctx, "T013-13.txt.2");
1041 0 : create.in.fname = talloc_asprintf(mem_ctx, "%s\\%s", DNAME,
1042 0 : files[num_files + 1].name);
1043 0 : status = smb2_create(tree, mem_ctx, &create);
1044 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1045 0 : smb2_util_close(tree, create.out.file.handle);
1046 :
1047 0 : files[num_files + 2].name = talloc_asprintf(mem_ctx, "T013-13.txt.3");
1048 0 : status = smb2_create_complex_file(tctx, tree, DNAME "\\T013-13.txt.3", &h);
1049 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1050 :
1051 0 : smb2_util_unlink(tree, DNAME "\\T014-14.txt");
1052 0 : smb2_util_setatr(tree, DNAME "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
1053 0 : smb2_util_setatr(tree, DNAME "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
1054 0 : smb2_util_setatr(tree, DNAME "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);
1055 0 : smb2_util_setatr(tree, DNAME "\\T018-18.txt", 0);
1056 0 : smb2_util_setatr(tree, DNAME "\\T039-39.txt", FILE_ATTRIBUTE_HIDDEN);
1057 0 : smb2_util_setatr(tree, DNAME "\\T000-0.txt", FILE_ATTRIBUTE_HIDDEN);
1058 0 : sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
1059 0 : sfinfo.generic.in.file.path = DNAME "\\T013-13.txt.3";
1060 0 : sfinfo.disposition_info.in.delete_on_close = 1;
1061 0 : status = smb2_composite_setpathinfo(tree, &sfinfo);
1062 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1063 :
1064 : /* Reset the numfiles to include the new files and start the
1065 : * search from the beginning */
1066 0 : num_files = num_files + 2;
1067 0 : f.in.pattern = "*";
1068 0 : f.in.continue_flags = SMB2_CONTINUE_FLAG_RESTART;
1069 0 : result.count = 0;
1070 :
1071 : do {
1072 0 : status = smb2_find_level(tree, tree, &f, &count, &d);
1073 0 : if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
1074 0 : break;
1075 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1076 0 : if (!fill_result(&result, d, count, f.in.level,
1077 : RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO)) {
1078 0 : ret = false;
1079 0 : goto done;
1080 : }
1081 0 : f.in.continue_flags = 0;
1082 0 : f.in.max_response_size = 4096;
1083 0 : } while (count != 0);
1084 :
1085 :
1086 0 : ret &= check_result(tctx, &result, "t039-39.txt", true, FILE_ATTRIBUTE_HIDDEN);
1087 0 : ret &= check_result(tctx, &result, "t000-0.txt", true, FILE_ATTRIBUTE_HIDDEN);
1088 0 : ret &= check_result(tctx, &result, "t014-14.txt", false, 0);
1089 0 : ret &= check_result(tctx, &result, "t015-15.txt", true, FILE_ATTRIBUTE_HIDDEN);
1090 0 : ret &= check_result(tctx, &result, "t016-16.txt", true, FILE_ATTRIBUTE_NORMAL);
1091 0 : ret &= check_result(tctx, &result, "t017-17.txt", true, FILE_ATTRIBUTE_SYSTEM);
1092 0 : ret &= check_result(tctx, &result, "t018-18.txt", true, FILE_ATTRIBUTE_ARCHIVE);
1093 0 : ret &= check_result(tctx, &result, "t019-19.txt", true, FILE_ATTRIBUTE_ARCHIVE);
1094 0 : ret &= check_result(tctx, &result, "T013-13.txt.2", true, FILE_ATTRIBUTE_ARCHIVE);
1095 0 : ret &= check_result(tctx, &result, "T003-3.txt.2", false, 0);
1096 0 : ret &= check_result(tctx, &result, "T013-13.txt.3", true, FILE_ATTRIBUTE_NORMAL);
1097 :
1098 0 : if (!ret) {
1099 0 : for (i=0;i<result.count;i++) {
1100 0 : torture_warning(tctx, "%s %s (0x%x)\n",
1101 0 : result.list[i].both_directory_info.name.s,
1102 : attrib_string(tctx,
1103 0 : result.list[i].both_directory_info.attrib),
1104 0 : result.list[i].both_directory_info.attrib);
1105 : }
1106 : }
1107 0 : done:
1108 0 : smb2_util_close(tree, h);
1109 0 : smb2_deltree(tree, DNAME);
1110 0 : talloc_free(mem_ctx);
1111 :
1112 0 : return ret;
1113 : }
1114 :
1115 : /*
1116 : testing if directories always come back sorted
1117 : */
1118 0 : static bool test_sorted(struct torture_context *tctx,
1119 : struct smb2_tree *tree)
1120 : {
1121 0 : TALLOC_CTX *mem_ctx = talloc_new(tctx);
1122 0 : const int num_files = 700;
1123 : int i;
1124 0 : struct file_elem files[700] = {};
1125 0 : bool ret = true;
1126 : NTSTATUS status;
1127 : struct multiple_result result;
1128 : struct smb2_handle h;
1129 :
1130 0 : torture_comment(tctx, "Testing if directories always come back "
1131 : "sorted\n");
1132 0 : status = populate_tree(tctx, mem_ctx, tree, files, num_files, &h);
1133 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1134 :
1135 0 : ZERO_STRUCT(result);
1136 0 : result.tctx = tctx;
1137 :
1138 0 : status = multiple_smb2_search(tree, tctx, "*",
1139 : SMB2_FIND_BOTH_DIRECTORY_INFO,
1140 : RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
1141 : SMB2_CONTINUE_FLAG_SINGLE,
1142 : &result, &h);
1143 :
1144 0 : torture_assert_int_equal_goto(tctx, result.count, num_files, ret, done,
1145 : "");
1146 :
1147 0 : for (i=0;i<num_files-1;i++) {
1148 : const char *name1, *name2;
1149 0 : name1 = result.list[i].both_directory_info.name.s;
1150 0 : name2 = result.list[i+1].both_directory_info.name.s;
1151 0 : if (strcasecmp_m(name1, name2) > 0) {
1152 0 : torture_comment(tctx, "non-alphabetical order at entry "
1153 : "%d '%s' '%s'\n", i, name1, name2);
1154 0 : torture_comment(tctx,
1155 : "Server does not produce sorted directory listings"
1156 : "(not an error)\n");
1157 0 : goto done;
1158 : }
1159 : }
1160 0 : talloc_free(result.list);
1161 0 : done:
1162 0 : smb2_util_close(tree, h);
1163 0 : smb2_deltree(tree, DNAME);
1164 0 : talloc_free(mem_ctx);
1165 :
1166 0 : return ret;
1167 : }
1168 :
1169 : /* test the behavior of file_index field in the SMB2_FIND struct */
1170 0 : static bool test_file_index(struct torture_context *tctx,
1171 : struct smb2_tree *tree)
1172 : {
1173 0 : TALLOC_CTX *mem_ctx = talloc_new(tctx);
1174 0 : const int num_files = 100;
1175 0 : int resume_index = 4;
1176 : int i;
1177 : char *fname;
1178 0 : bool ret = true;
1179 : NTSTATUS status;
1180 : struct multiple_result result;
1181 : struct smb2_create create;
1182 : struct smb2_find f;
1183 : struct smb2_handle h;
1184 : union smb_search_data *d;
1185 : unsigned count;
1186 :
1187 0 : smb2_deltree(tree, DNAME);
1188 :
1189 0 : status = torture_smb2_testdir(tree, DNAME, &h);
1190 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1191 :
1192 0 : torture_comment(tctx, "Testing the behavior of file_index flag\n");
1193 :
1194 0 : ZERO_STRUCT(create);
1195 0 : create.in.desired_access = SEC_RIGHTS_FILE_ALL;
1196 0 : create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
1197 0 : create.in.create_disposition = NTCREATEX_DISP_CREATE;
1198 0 : for (i = num_files-1; i >= 0; i--) {
1199 0 : fname = talloc_asprintf(mem_ctx, DNAME "\\file%u.txt", i);
1200 0 : create.in.fname = fname;
1201 0 : status = smb2_create(tree, mem_ctx, &create);
1202 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1203 0 : talloc_free(fname);
1204 0 : smb2_util_close(tree, create.out.file.handle);
1205 : }
1206 :
1207 0 : ZERO_STRUCT(result);
1208 0 : result.tctx = tctx;
1209 :
1210 0 : ZERO_STRUCT(f);
1211 0 : f.in.file.handle = h;
1212 0 : f.in.pattern = "*";
1213 0 : f.in.continue_flags = SMB2_CONTINUE_FLAG_SINGLE;
1214 0 : f.in.max_response_size = 0x1000;
1215 0 : f.in.level = SMB2_FIND_FULL_DIRECTORY_INFO;
1216 :
1217 : do {
1218 0 : status = smb2_find_level(tree, tree, &f, &count, &d);
1219 0 : if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
1220 0 : break;
1221 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1222 0 : if (!fill_result(&result, d, count, f.in.level,
1223 : RAW_SEARCH_DATA_FULL_DIRECTORY_INFO)) {
1224 0 : ret = false;
1225 0 : goto done;
1226 : }
1227 0 : } while(result.count < 10);
1228 :
1229 0 : if (result.list[0].full_directory_info.file_index == 0) {
1230 0 : torture_skip_goto(tctx, done,
1231 : "Talking to a server that doesn't provide a "
1232 : "file index.\nWindows servers using NTFS do "
1233 : "not provide a file_index. Skipping test\n");
1234 : } else {
1235 : /* We are not talking to a Windows based server. Windows
1236 : * servers using NTFS do not provide a file_index. Windows
1237 : * servers using FAT do provide a file index, however in both
1238 : * cases they do not honor a file index on a resume request.
1239 : * See MS-FSCC <62> and MS-SMB2 <54> for more information. */
1240 :
1241 : /* Set the file_index flag to point to the fifth file from the
1242 : * previous enumeration and try to start the subsequent
1243 : * searches from that point */
1244 0 : f.in.file_index =
1245 0 : result.list[resume_index].full_directory_info.file_index;
1246 0 : f.in.continue_flags = SMB2_CONTINUE_FLAG_INDEX;
1247 :
1248 : /* get the name of the next expected file */
1249 0 : fname = talloc_asprintf(mem_ctx, DNAME "\\%s",
1250 0 : result.list[resume_index].full_directory_info.name.s);
1251 :
1252 0 : ZERO_STRUCT(result);
1253 0 : result.tctx = tctx;
1254 0 : status = smb2_find_level(tree, tree, &f, &count, &d);
1255 0 : if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
1256 0 : goto done;
1257 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1258 0 : if (!fill_result(&result, d, count, f.in.level,
1259 : RAW_SEARCH_DATA_FULL_DIRECTORY_INFO)) {
1260 0 : ret = false;
1261 0 : goto done;
1262 : }
1263 0 : if (strcmp(fname,
1264 0 : result.list[0].full_directory_info.name.s)) {
1265 0 : torture_comment(tctx, "Next expected file: %s but the "
1266 : "server returned %s\n", fname,
1267 0 : result.list[0].full_directory_info.name.s);
1268 0 : torture_comment(tctx,
1269 : "Not an error. Resuming using a file "
1270 : "index is an optional feature of the "
1271 : "protocol.\n");
1272 0 : goto done;
1273 : }
1274 : }
1275 0 : done:
1276 0 : smb2_util_close(tree, h);
1277 0 : smb2_deltree(tree, DNAME);
1278 0 : talloc_free(mem_ctx);
1279 :
1280 0 : return ret;
1281 : }
1282 :
1283 : /*
1284 : * Tests directory enumeration in a directory containing >1000 files with
1285 : * names of varying lengths.
1286 : */
1287 0 : static bool test_large_files(struct torture_context *tctx,
1288 : struct smb2_tree *tree)
1289 : {
1290 0 : TALLOC_CTX *mem_ctx = talloc_new(tctx);
1291 0 : const int num_files = 2000;
1292 0 : int max_len = 200;
1293 : /* These should be evenly divisible */
1294 0 : int num_at_len = num_files / max_len;
1295 0 : struct file_elem files[2000] = {};
1296 0 : size_t len = 1;
1297 0 : bool ret = true;
1298 : NTSTATUS status;
1299 : struct smb2_create create;
1300 : struct smb2_find f;
1301 0 : struct smb2_handle h = {{0}};
1302 : union smb_search_data *d;
1303 0 : int i, j = 0, file_count = 0;
1304 0 : char **strs = NULL;
1305 : unsigned count;
1306 :
1307 0 : torture_comment(tctx,
1308 : "Testing directory enumeration in a directory with >1000 files\n");
1309 :
1310 0 : smb2_deltree(tree, DNAME);
1311 :
1312 0 : ZERO_STRUCT(create);
1313 0 : create.in.desired_access = SEC_RIGHTS_DIR_ALL;
1314 0 : create.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1315 0 : create.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
1316 0 : create.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1317 : NTCREATEX_SHARE_ACCESS_WRITE |
1318 : NTCREATEX_SHARE_ACCESS_DELETE;
1319 0 : create.in.create_disposition = NTCREATEX_DISP_CREATE;
1320 0 : create.in.fname = DNAME;
1321 :
1322 0 : status = smb2_create(tree, mem_ctx, &create);
1323 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1324 0 : h = create.out.file.handle;
1325 :
1326 0 : ZERO_STRUCT(create);
1327 0 : create.in.desired_access = SEC_RIGHTS_FILE_ALL;
1328 0 : create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
1329 0 : create.in.create_disposition = NTCREATEX_DISP_CREATE;
1330 :
1331 0 : for (i = 0; i < num_files; i++) {
1332 0 : if (i % num_at_len == 0) {
1333 0 : strs = generate_unique_strs(mem_ctx, len, num_at_len);
1334 0 : len++;
1335 : }
1336 0 : files[i].name = strs[i % num_at_len];
1337 0 : create.in.fname = talloc_asprintf(mem_ctx, "%s\\%s",
1338 : DNAME, files[i].name);
1339 0 : status = smb2_create(tree, mem_ctx, &create);
1340 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1341 0 : smb2_util_close(tree, create.out.file.handle);
1342 : }
1343 :
1344 0 : ZERO_STRUCT(f);
1345 0 : f.in.file.handle = h;
1346 0 : f.in.pattern = "*";
1347 0 : f.in.max_response_size = 0x100;
1348 0 : f.in.level = SMB2_FIND_BOTH_DIRECTORY_INFO;
1349 :
1350 : do {
1351 0 : status = smb2_find_level(tree, tree, &f, &count, &d);
1352 0 : if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES))
1353 0 : break;
1354 0 : torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "");
1355 :
1356 0 : for (i = 0; i < count; i++) {
1357 : bool expected;
1358 0 : const char *found = d[i].both_directory_info.name.s;
1359 :
1360 0 : if (!strcmp(found, ".") || !strcmp(found, ".."))
1361 0 : continue;
1362 :
1363 0 : expected = false;
1364 0 : for (j = 0; j < 2000; j++) {
1365 0 : if (!strcmp(files[j].name, found)) {
1366 0 : files[j].found = true;
1367 0 : expected = true;
1368 0 : break;
1369 : }
1370 : }
1371 :
1372 0 : if (expected)
1373 0 : continue;
1374 :
1375 0 : torture_result(tctx, TORTURE_FAIL,
1376 : "(%s): didn't expect %s\n",
1377 : __location__, found);
1378 0 : ret = false;
1379 0 : goto done;
1380 : }
1381 0 : file_count = file_count + i;
1382 0 : f.in.continue_flags = 0;
1383 0 : f.in.max_response_size = 4096;
1384 0 : } while (count != 0);
1385 :
1386 0 : torture_assert_int_equal_goto(tctx, file_count, num_files + 2, ret,
1387 : done, "");
1388 :
1389 0 : for (i = 0; i < num_files; i++) {
1390 0 : if (files[j].found)
1391 0 : continue;
1392 :
1393 0 : torture_result(tctx, TORTURE_FAIL,
1394 : "(%s): expected to find %s, but didn't\n",
1395 : __location__, files[j].name);
1396 0 : ret = false;
1397 0 : goto done;
1398 : }
1399 0 : done:
1400 0 : smb2_util_close(tree, h);
1401 0 : smb2_deltree(tree, DNAME);
1402 0 : talloc_free(mem_ctx);
1403 :
1404 0 : return ret;
1405 : }
1406 :
1407 964 : struct torture_suite *torture_smb2_dir_init(TALLOC_CTX *ctx)
1408 : {
1409 738 : struct torture_suite *suite =
1410 226 : torture_suite_create(ctx, "dir");
1411 :
1412 964 : torture_suite_add_1smb2_test(suite, "find", test_find);
1413 964 : torture_suite_add_1smb2_test(suite, "fixed", test_fixed);
1414 964 : torture_suite_add_1smb2_test(suite, "one", test_one_file);
1415 964 : torture_suite_add_1smb2_test(suite, "many", test_many_files);
1416 964 : torture_suite_add_1smb2_test(suite, "modify", test_modify_search);
1417 964 : torture_suite_add_1smb2_test(suite, "sorted", test_sorted);
1418 964 : torture_suite_add_1smb2_test(suite, "file-index", test_file_index);
1419 964 : torture_suite_add_1smb2_test(suite, "large-files", test_large_files);
1420 964 : suite->description = talloc_strdup(suite, "SMB2-DIR tests");
1421 :
1422 964 : return suite;
1423 : }
|