Debian Codegraph @

source4/torture/smb2/streams.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3
4 test alternate data streams
5
6 Copyright (C) Andrew Tridgell 2004
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
26 #include "smb_constants.h"
27 #include "torture/torture.h"
28 #include "torture/smb2/proto.h"
29
30 #include "system/filesys.h"
31 #include "system/locale.h"
32 #include "lib/util/tsort.h"
33 #include "libcli/security/security_descriptor.h"
34
35 #define DNAME "teststreams"
36
37 #define CHECK_STATUS(status, correct) \
38 torture_assert_ntstatus_equal_goto(tctx, status, correct, ret, done, "CHECK_STATUS")
39
40 #define CHECK_VALUE(v, correct) \
41 torture_assert_u64_equal_goto(tctx, v, correct, ret, done, "CHECK_VALUE")
42
43 #define CHECK_NTTIME(v, correct) \
44 torture_assert_nttime_equal_goto(tctx, v, correct, ret, done, "CHECK_NTTIME")
45
46 #define CHECK_STR(v, correct) \
47 torture_assert_str_equal_goto(tctx, v, correct, ret, done, "CHECK_STR")
48
49 static int qsort_string(char * const *s1, char * const *s2)
50 {
51 return strcmp(*s1, *s2);
52 }
53
54 static int qsort_stream(const struct stream_struct * s1, const struct stream_struct *s2)
55 {
56 return strcmp(s1->stream_name.s, s2->stream_name.s);
57 }
58
59 static bool check_stream(struct torture_context *tctx,
60 struct smb2_tree *tree,
61 const char *location,
62 TALLOC_CTX *mem_ctx,
63 const char *fname,
64 const char *sname,
65 const char *value)
66 {
67 struct smb2_handle handle;
68 struct smb2_create create;
69 struct smb2_read r;
70 NTSTATUS status;
71 const char *full_name;
72
73 full_name = talloc_asprintf(mem_ctx, "%s:%s", fname, sname);
74
75 ZERO_STRUCT(create);
76 create.in.desired_access = SEC_RIGHTS_FILE_ALL;
77 create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
78 create.in.create_disposition = NTCREATEX_DISP_OPEN;
79 create.in.fname = full_name;
80
81 status = smb2_create(tree, mem_ctx, &create);
82 if (!NT_STATUS_IS_OK(status)) {
83 if (value == NULL) {
84 return true;
85 } else {
86 torture_comment(tctx, "Unable to open stream %s\n",
87 full_name);
88 return false;
89 }
90 }
91
92 handle = create.out.file.handle;
93 if (value == NULL) {
94 return true;
95 }
96
97
98 ZERO_STRUCT(r);
99 r.in.file.handle = handle;
100 r.in.length = strlen(value)+11;
101 r.in.offset = 0;
102
103 status = smb2_read(tree, tree, &r);
104
105 if (!NT_STATUS_IS_OK(status)) {
106 torture_comment(tctx, "(%s) Failed to read %lu bytes from "
107 "stream '%s'\n", location, (long)strlen(value), full_name);
108 return false;
109 }
110
111 if (memcmp(r.out.data.data, value, strlen(value)) != 0) {
112 torture_comment(tctx, "(%s) Bad data in stream\n", location);
113 return false;
114 }
115
116 smb2_util_close(tree, handle);
117 return true;
118 }
119
120 static bool check_stream_list(struct smb2_tree *tree,
121 struct torture_context *tctx,
122 const char *fname,
123 unsigned int num_exp,
124 const char **exp,
125 struct smb2_handle h)
126 {
127 union smb_fileinfo finfo;
128 NTSTATUS status;
129 unsigned int i;
130 TALLOC_CTX *tmp_ctx = talloc_new(tctx);
131 char **exp_sort;
132 struct stream_struct *stream_sort;
133 bool ret = false;
134
135 finfo.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
136 finfo.generic.in.file.handle = h;
137
138 status = smb2_getinfo_file(tree, tctx, &finfo);
139 if (!NT_STATUS_IS_OK(status)) {
140 torture_comment(tctx, "(%s) smb_raw_pathinfo failed: %s\n",
141 __location__, nt_errstr(status));
142 goto fail;
143 }
144
145 if (finfo.stream_info.out.num_streams != num_exp) {
146 torture_comment(tctx, "(%s) expected %d streams, got %d\n",
147 __location__, num_exp, finfo.stream_info.out.num_streams);
148 goto fail;
149 }
150
151 if (num_exp == 0) {
152 ret = true;
153 goto fail;
154 }
155
156 exp_sort = talloc_memdup(tmp_ctx, exp, num_exp * sizeof(*exp));
157
158 if (exp_sort == NULL) {
159 goto fail;
160 }
161
162 TYPESAFE_QSORT(exp_sort, num_exp, qsort_string);
163
164 stream_sort = talloc_memdup(tmp_ctx, finfo.stream_info.out.streams,
165 finfo.stream_info.out.num_streams *
166 sizeof(*stream_sort));
167
168 if (stream_sort == NULL) {
169 goto fail;
170 }
171
172 TYPESAFE_QSORT(stream_sort, finfo.stream_info.out.num_streams, qsort_stream);
173
174 for (i=0; i<num_exp; i++) {
175 if (strcmp(exp_sort[i], stream_sort[i].stream_name.s) != 0) {
176 torture_comment(tctx,
177 "(%s) expected stream name %s, got %s\n",
178 __location__, exp_sort[i],
179 stream_sort[i].stream_name.s);
180 goto fail;
181 }
182 }
183
184 ret = true;
185 fail:
186 talloc_free(tmp_ctx);
187 return ret;
188 }
189
190
191 static bool test_stream_dir(struct torture_context *tctx,
192 struct smb2_tree *tree)
193 {
194 TALLOC_CTX *mem_ctx = talloc_new(tctx);
195 NTSTATUS status;
196 union smb_open io;
197 const char *fname = DNAME "\\stream.txt";
198 const char *sname1;
199 bool ret = true;
200 const char *basedir_data;
201 struct smb2_handle h;
202
203 smb2_util_unlink(tree, fname);
204 smb2_deltree(tree, DNAME);
205
206 status = torture_smb2_testdir(tree, DNAME, &h);
207 CHECK_STATUS(status, NT_STATUS_OK);
208
209 basedir_data = talloc_asprintf(mem_ctx, "%s::$DATA", DNAME);
210 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
211 torture_comment(tctx, "%s\n", sname1);
212
213 torture_comment(tctx, "(%s) opening non-existent directory stream\n",
214 __location__);
215 ZERO_STRUCT(io.smb2);
216 io.smb2.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
217 io.smb2.in.desired_access = SEC_FILE_WRITE_DATA;
218 io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
219 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
220 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
221 io.smb2.in.share_access = 0;
222 io.smb2.in.alloc_size = 0;
223 io.smb2.in.security_flags = 0;
224 io.smb2.in.fname = sname1;
225 io.smb2.in.create_flags = 0;
226 status = smb2_create(tree, mem_ctx, &(io.smb2));
227 CHECK_STATUS(status, NT_STATUS_NOT_A_DIRECTORY);
228
229 torture_comment(tctx, "(%s) opening basedir stream\n", __location__);
230 ZERO_STRUCT(io.smb2);
231 io.smb2.in.create_flags = 0;
232 io.smb2.in.desired_access = SEC_FILE_WRITE_DATA;
233 io.smb2.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
234 io.smb2.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
235 io.smb2.in.share_access = 0;
236 io.smb2.in.alloc_size = 0;
237 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
238 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
239 io.smb2.in.security_flags = 0;
240 io.smb2.in.fname = basedir_data;
241 status = smb2_create(tree, mem_ctx, &(io.smb2));
242 CHECK_STATUS(status, NT_STATUS_NOT_A_DIRECTORY);
243
244 torture_comment(tctx, "(%s) opening basedir ::$DATA stream\n",
245 __location__);
246 ZERO_STRUCT(io.smb2);
247 io.smb2.in.create_flags = 0x10;
248 io.smb2.in.desired_access = SEC_FILE_WRITE_DATA;
249 io.smb2.in.create_options = 0;
250 io.smb2.in.file_attributes = 0;
251 io.smb2.in.share_access = 0;
252 io.smb2.in.alloc_size = 0;
253 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
254 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
255 io.smb2.in.security_flags = 0;
256 io.smb2.in.fname = basedir_data;
257 status = smb2_create(tree, mem_ctx, &(io.smb2));
258 CHECK_STATUS(status, NT_STATUS_FILE_IS_A_DIRECTORY);
259
260 torture_comment(tctx, "(%s) list the streams on the basedir\n",
261 __location__);
262 ret &= check_stream_list(tree, mem_ctx, DNAME, 0, NULL, h);
263 done:
264 smb2_util_unlink(tree, fname);
265 smb2_deltree(tree, DNAME);
266 talloc_free(mem_ctx);
267
268 return ret;
269 }
270
271 static bool test_stream_io(struct torture_context *tctx,
272 struct smb2_tree *tree)
273 {
274 TALLOC_CTX *mem_ctx = talloc_new(tctx);
275 NTSTATUS status;
276 union smb_open io;
277 const char *fname = DNAME "\\stream.txt";
278 const char *sname1, *sname2;
279 bool ret = true;
280 struct smb2_handle h, h2;
281
282 const char *one[] = { "::$DATA" };
283 const char *two[] = { "::$DATA", ":Second Stream:$DATA" };
284 const char *three[] = { "::$DATA", ":Stream One:$DATA",
285 ":Second Stream:$DATA" };
286
287 ZERO_STRUCT(h);
288 ZERO_STRUCT(h2);
289
290 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
291 sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname,
292 "Second Stream");
293
294 smb2_util_unlink(tree, fname);
295 smb2_deltree(tree, DNAME);
296
297 status = torture_smb2_testdir(tree, DNAME, &h);
298 CHECK_STATUS(status, NT_STATUS_OK);
299
300 torture_comment(tctx, "(%s) creating a stream on a non-existent file\n",
301 __location__);
302
303 ZERO_STRUCT(io.smb2);
304 io.smb2.in.create_flags = 0;
305 io.smb2.in.desired_access = SEC_FILE_WRITE_DATA;
306 io.smb2.in.create_options = 0;
307 io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
308 io.smb2.in.share_access = 0;
309 io.smb2.in.alloc_size = 0;
310 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
311 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
312 io.smb2.in.security_flags = 0;
313 io.smb2.in.fname = sname1;
314 status = smb2_create(tree, mem_ctx, &(io.smb2));
315 CHECK_STATUS(status, NT_STATUS_OK);
316 h2 = io.smb2.out.file.handle;
317
318 ret &= check_stream(tctx, tree, __location__, mem_ctx, fname,
319 "Stream One", NULL);
320
321 torture_comment(tctx, "(%s) check that open of base file is allowed\n", __location__);
322 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
323 io.smb2.in.fname = fname;
324 status = smb2_create(tree, mem_ctx, &(io.smb2));
325 CHECK_STATUS(status, NT_STATUS_OK);
326 smb2_util_close(tree, io.smb2.out.file.handle);
327
328 torture_comment(tctx, "(%s) writing to stream\n", __location__);
329 status = smb2_util_write(tree, h2, "test data", 0, 9);
330 CHECK_STATUS(status, NT_STATUS_OK);
331
332 smb2_util_close(tree, h2);
333
334 ret &= check_stream(tctx, tree, __location__, mem_ctx, fname,
335 "Stream One", "test data");
336
337 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
338 io.smb2.in.fname = sname1;
339 status = smb2_create(tree, mem_ctx, &(io.smb2));
340 CHECK_STATUS(status, NT_STATUS_OK);
341 h2 = io.smb2.out.file.handle;
342
343 torture_comment(tctx, "(%s) modifying stream\n", __location__);
344 status = smb2_util_write(tree, h2, "MORE DATA ", 5, 10);
345 CHECK_STATUS(status, NT_STATUS_OK);
346
347 smb2_util_close(tree, h2);
348
349 ret &= check_stream(tctx, tree, __location__, mem_ctx, fname,
350 "Stream One:$FOO", NULL);
351
352 torture_comment(tctx, "(%s) creating a stream2 on a existing file\n",
353 __location__);
354 io.smb2.in.fname = sname2;
355 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
356 status = smb2_create(tree, mem_ctx, &(io.smb2));
357 CHECK_STATUS(status, NT_STATUS_OK);
358 h2 = io.smb2.out.file.handle;
359
360 torture_comment(tctx, "(%s) modifying stream\n", __location__);
361 status= smb2_util_write(tree, h2, "SECOND STREAM", 0, 13);
362 CHECK_STATUS(status, NT_STATUS_OK);
363 smb2_util_close(tree, h2);
364
365 ret &= check_stream(tctx, tree, __location__, mem_ctx, fname,
366 "Stream One", "test MORE DATA ");
367
368 ret &= check_stream(tctx, tree, __location__, mem_ctx, fname,
369 "Stream One:$DATA", "test MORE DATA ");
370
371 ret &= check_stream(tctx, tree, __location__, mem_ctx, fname,
372 "Stream One:", NULL);
373
374 if (!ret) {
375 torture_result(tctx, TORTURE_FAIL,
376 "check_stream(\"Stream One:*\") failed\n");
377 goto done;
378 }
379
380 ret &= check_stream(tctx, tree, __location__, mem_ctx, fname,
381 "Second Stream", "SECOND STREAM");
382
383 ret &= check_stream(tctx, tree, __location__, mem_ctx, fname,
384 "SECOND STREAM:$DATA", "SECOND STREAM");
385 ret &= check_stream(tctx, tree, __location__, mem_ctx, fname,
386 "Second Stream:$DATA", "SECOND STREAM");
387
388 ret &= check_stream(tctx, tree, __location__, mem_ctx, fname,
389 "Second Stream:", NULL);
390
391 ret &= check_stream(tctx, tree, __location__, mem_ctx, fname,
392 "Second Stream:$FOO", NULL);
393
394 if (!ret) {
395 torture_result(tctx, TORTURE_FAIL,
396 "check_stream(\"Second Stream:*\") failed\n");
397 goto done;
398 }
399
400 io.smb2.in.fname = sname2;
401 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
402 status = smb2_create(tree, mem_ctx, &(io.smb2));
403 CHECK_STATUS(status, NT_STATUS_OK);
404 h2 = io.smb2.out.file.handle;
405 check_stream_list(tree, tctx, fname, 3, three, h2);
406
407 smb2_util_close(tree, h2);
408
409 torture_comment(tctx, "(%s) deleting stream\n", __location__);
410 status = smb2_util_unlink(tree, sname1);
411 CHECK_STATUS(status, NT_STATUS_OK);
412
413 io.smb2.in.fname = sname2;
414 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
415 status = smb2_create(tree, mem_ctx, &(io.smb2));
416 CHECK_STATUS(status, NT_STATUS_OK);
417 h2 = io.smb2.out.file.handle;
418 check_stream_list(tree, tctx, fname, 2, two, h2);
419 smb2_util_close(tree, h2);
420
421 torture_comment(tctx, "(%s) delete a stream via delete-on-close\n",
422 __location__);
423 io.smb2.in.fname = sname2;
424 io.smb2.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
425 io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_DELETE;
426 io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
427 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
428
429 status = smb2_create(tree, mem_ctx, &(io.smb2));
430 CHECK_STATUS(status, NT_STATUS_OK);
431 h2 = io.smb2.out.file.handle;
432
433 smb2_util_close(tree, h2);
434 status = smb2_util_unlink(tree, sname2);
435 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
436
437 io.smb2.in.fname = fname;
438 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
439 status = smb2_create(tree, mem_ctx, &(io.smb2));
440 h2 = io.smb2.out.file.handle;
441 check_stream_list(tree,tctx, fname, 1, one, h2);
442 smb2_util_close(tree, h2);
443
444 if (!torture_setting_bool(tctx, "samba4", false)) {
445 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
446 io.smb2.in.fname = sname1;
447 status = smb2_create(tree, mem_ctx, &(io.smb2));
448 CHECK_STATUS(status, NT_STATUS_OK);
449 smb2_util_close(tree, io.ntcreatex.out.file.handle);
450 io.smb2.in.fname = sname2;
451 status = smb2_create(tree, mem_ctx, &(io.smb2));
452 CHECK_STATUS(status, NT_STATUS_OK);
453 smb2_util_close(tree, io.ntcreatex.out.file.handle);
454 torture_comment(tctx, "(%s) deleting file\n", __location__);
455 status = smb2_util_unlink(tree, fname);
456 CHECK_STATUS(status, NT_STATUS_OK);
457 }
458
459
460 done:
461 smb2_util_close(tree, h2);
462 smb2_deltree(tree, DNAME);
463 talloc_free(mem_ctx);
464
465 return ret;
466 }
467
468 static bool test_zero_byte_stream(struct torture_context *tctx,
469 struct smb2_tree *tree)
470 {
471 TALLOC_CTX *mem_ctx = talloc_new(tctx);
472 NTSTATUS status;
473 union smb_open io;
474 const char *fname = DNAME "\\stream.txt";
475 const char *sname;
476 bool ret = true;
477 struct smb2_handle h, bh;
478 const char *streams[] = { "::$DATA", ":foo:$DATA" };
479
480 sname = talloc_asprintf(mem_ctx, "%s:%s", fname, "foo");
481
482 smb2_util_unlink(tree, fname);
483 smb2_deltree(tree, DNAME);
484
485 status = torture_smb2_testdir(tree, DNAME, &h);
486 torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "testdir");
487 smb2_util_close(tree, h);
488
489 torture_comment(tctx, "(%s) Check 0 byte named stream\n",
490 __location__);
491
492 /* Create basefile */
493 ZERO_STRUCT(io);
494 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
495 io.smb2.in.fname = fname;
496 io.smb2.in.desired_access = SEC_FILE_READ_ATTRIBUTE |
497 SEC_FILE_WRITE_ATTRIBUTE |
498 SEC_FILE_READ_DATA |
499 SEC_FILE_WRITE_DATA;
500 status = smb2_create(tree, mem_ctx, &(io.smb2));
501 torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "create");
502 smb2_util_close(tree, io.smb2.out.file.handle);
503
504 /* Create named stream and close it */
505 ZERO_STRUCT(io);
506 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
507 io.smb2.in.fname = sname;
508 io.smb2.in.desired_access = SEC_FILE_READ_ATTRIBUTE |
509 SEC_FILE_WRITE_ATTRIBUTE |
510 SEC_FILE_READ_DATA |
511 SEC_FILE_WRITE_DATA;
512 status = smb2_create(tree, mem_ctx, &(io.smb2));
513 torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "create");
514 smb2_util_close(tree, io.smb2.out.file.handle);
515
516 /*
517 * Check stream list, the 0 byte stream MUST be returned by
518 * the server.
519 */
520 ZERO_STRUCT(io);
521 io.smb2.in.fname = fname;
522 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
523 io.smb2.in.desired_access = SEC_FILE_READ_ATTRIBUTE |
524 SEC_FILE_WRITE_ATTRIBUTE |
525 SEC_FILE_READ_DATA |
526 SEC_FILE_WRITE_DATA;
527 status = smb2_create(tree, mem_ctx, &(io.smb2));
528 bh = io.smb2.out.file.handle;
529
530 ret = check_stream_list(tree,tctx, fname, 2, streams, bh);
531 torture_assert_goto(tctx, ret == true, ret, done, "smb2_create");
532 smb2_util_close(tree, bh);
533
534 done:
535 smb2_deltree(tree, DNAME);
536 talloc_free(mem_ctx);
537
538 return ret;
539 }
540
541 /*
542 test stream sharemodes
543 */
544 static bool test_stream_sharemodes(struct torture_context *tctx,
545 struct smb2_tree *tree)
546 {
547 TALLOC_CTX *mem_ctx = talloc_new(tctx);
548 NTSTATUS status;
549 union smb_open io;
550 const char *fname = DNAME "\\stream_share.txt";
551 const char *sname1, *sname2;
552 bool ret = true;
553 struct smb2_handle h, h1, h2;
554
555 ZERO_STRUCT(h);
556 ZERO_STRUCT(h1);
557 ZERO_STRUCT(h2);
558
559 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
560 sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname,
561 "Second Stream");
562
563 smb2_util_unlink(tree, fname);
564 smb2_deltree(tree, DNAME);
565
566 status = torture_smb2_testdir(tree, DNAME, &h);
567 CHECK_STATUS(status, NT_STATUS_OK);
568
569 torture_comment(tctx, "(%s) Testing stream share mode conflicts\n",
570 __location__);
571 ZERO_STRUCT(io.smb2);
572 io.generic.level = RAW_OPEN_SMB2;
573 io.smb2.in.create_flags = 0;
574 io.smb2.in.desired_access = SEC_FILE_WRITE_DATA;
575 io.smb2.in.create_options = 0;
576 io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
577 io.smb2.in.share_access = 0;
578 io.smb2.in.alloc_size = 0;
579 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
580 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
581 io.smb2.in.security_flags = 0;
582 io.smb2.in.fname = sname1;
583
584 status = smb2_create(tree, mem_ctx, &(io.smb2));
585 CHECK_STATUS(status, NT_STATUS_OK);
586 h1 = io.smb2.out.file.handle;
587
588 /*
589 * A different stream does not give a sharing violation
590 */
591
592 io.smb2.in.fname = sname2;
593 status = smb2_create(tree, mem_ctx, &(io.smb2));
594 CHECK_STATUS(status, NT_STATUS_OK);
595 h2 = io.smb2.out.file.handle;
596
597 /*
598 * ... whereas the same stream does with unchanged access/share_access
599 * flags
600 */
601
602 io.smb2.in.fname = sname1;
603 io.smb2.in.create_disposition = 0;
604 status = smb2_create(tree, mem_ctx, &(io.smb2));
605 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
606
607 io.smb2.in.fname = sname2;
608 status = smb2_create(tree, mem_ctx, &(io.smb2));
609 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
610
611 done:
612 smb2_util_close(tree, h1);
613 smb2_util_close(tree, h2);
614 status = smb2_util_unlink(tree, fname);
615 smb2_deltree(tree, DNAME);
616 talloc_free(mem_ctx);
617
618 return ret;
619 }
620
621 /*
622 * Test FILE_SHARE_DELETE on streams
623 *
624 * A stream opened with !FILE_SHARE_DELETE prevents the main file to be opened
625 * with SEC_STD_DELETE.
626 *
627 * The main file opened with !FILE_SHARE_DELETE does *not* prevent a stream to
628 * be opened with SEC_STD_DELETE.
629 *
630 * A stream held open with FILE_SHARE_DELETE allows the file to be
631 * deleted. After the main file is deleted, access to the open file descriptor
632 * still works, but all name-based access to both the main file as well as the
633 * stream is denied with DELETE pending.
634 *
635 * This means, an open of the main file with SEC_STD_DELETE should walk all
636 * streams and also open them with SEC_STD_DELETE. If any of these opens gives
637 * SHARING_VIOLATION, the main open fails.
638 *
639 * Closing the main file after delete_on_close has been set does not really
640 * unlink it but leaves the corresponding share mode entry with
641 * delete_on_close being set around until all streams are closed.
642 *
643 * Opening a stream must also look at the main file's share mode entry, look
644 * at the delete_on_close bit and potentially return DELETE_PENDING.
645 */
646
647 static bool test_stream_delete(struct torture_context *tctx,
648 struct smb2_tree *tree)
649 {
650 TALLOC_CTX *mem_ctx = talloc_new(tctx);
651 NTSTATUS status;
652 union smb_open io;
653 const char *fname = DNAME "\\stream_delete.txt";
654 const char *sname1;
655 bool ret = true;
656 struct smb2_handle h = {{0}};
657 struct smb2_handle h1 = {{0}};
658 struct smb2_read r;
659
660 if (torture_setting_bool(tctx, "samba4", false)) {
661 torture_comment(tctx, "Skipping test as samba4 is enabled\n");
662 goto done;
663 }
664
665 ZERO_STRUCT(h);
666 ZERO_STRUCT(h1);
667
668 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
669
670 /* clean slate .. */
671 smb2_util_unlink(tree, fname);
672 smb2_deltree(tree, fname);
673 smb2_deltree(tree, DNAME);
674
675 status = torture_smb2_testdir(tree, DNAME, &h);
676 CHECK_STATUS(status, NT_STATUS_OK);
677
678 torture_comment(tctx, "(%s) opening non-existent file stream\n",
679 __location__);
680 ZERO_STRUCT(io.smb2);
681 io.smb2.in.create_flags = 0;
682 io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
683 io.smb2.in.create_options = 0;
684 io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
685 io.smb2.in.share_access = 0;
686 io.smb2.in.alloc_size = 0;
687 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
688 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
689 io.smb2.in.security_flags = 0;
690 io.smb2.in.fname = sname1;
691
692 status = smb2_create(tree, mem_ctx, &(io.smb2));
693 CHECK_STATUS(status, NT_STATUS_OK);
694 h1 = io.smb2.out.file.handle;
695
696 status = smb2_util_write(tree, h1, "test data", 0, 9);
697 CHECK_STATUS(status, NT_STATUS_OK);
698
699 /*
700 * One stream opened without FILE_SHARE_DELETE prevents the main file
701 * to be deleted or even opened with DELETE access
702 */
703
704 status = smb2_util_unlink(tree, fname);
705 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
706
707 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
708 io.smb2.in.fname = fname;
709 io.smb2.in.desired_access = SEC_STD_DELETE;
710 status = smb2_create(tree, mem_ctx, &(io.smb2));
711 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
712
713 smb2_util_close(tree, h1);
714
715 /*
716 * ... but unlink works if a stream is opened with FILE_SHARE_DELETE
717 */
718
719 io.smb2.in.fname = sname1;
720 io.smb2.in.desired_access = SEC_FILE_READ_DATA|SEC_FILE_WRITE_DATA;
721 io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_DELETE |
722 NTCREATEX_SHARE_ACCESS_READ |
723 NTCREATEX_SHARE_ACCESS_WRITE;
724 status = smb2_create(tree, mem_ctx, &(io.smb2));
725 CHECK_STATUS(status, NT_STATUS_OK);
726 h1 = io.smb2.out.file.handle;
727
728 status = smb2_util_unlink(tree, fname);
729 CHECK_STATUS(status, NT_STATUS_OK);
730
731 /*
732 * file access still works on the stream while the main file is closed
733 */
734 ZERO_STRUCT(r);
735 r.in.file.handle = h1;
736 r.in.length = 9;
737 r.in.offset = 0;
738
739 status = smb2_read(tree, tree, &r);
740 CHECK_STATUS(status, NT_STATUS_OK);
741
742 /*
743 * name-based access to both the main file and the stream does not
744 * work anymore but gives DELETE_PENDING
745 */
746
747 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
748 io.smb2.in.fname = fname;
749 status = smb2_create(tree, mem_ctx, &(io.smb2));
750 CHECK_STATUS(status, NT_STATUS_DELETE_PENDING);
751
752 /*
753 * older S3 doesn't do this
754 */
755
756 io.smb2.in.fname = sname1;
757 status = smb2_create(tree, mem_ctx, &(io.smb2));
758 CHECK_STATUS(status, NT_STATUS_DELETE_PENDING);
759
760 smb2_util_close(tree, h1);
761 ZERO_STRUCT(h1);
762
763 /*
764 * After closing the stream the file is really gone.
765 */
766
767 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
768 io.smb2.in.fname = fname;
769 status = smb2_create(tree, mem_ctx, &(io.smb2));
770 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
771
772 done:
773 if (!smb2_util_handle_empty(h1)) {
774 smb2_util_close(tree, h1);
775 }
776 smb2_util_unlink(tree, fname);
777 smb2_deltree(tree, DNAME);
778 talloc_free(mem_ctx);
779
780 return ret;
781 }
782
783 /*
784 test stream names
785 */
786 static bool test_stream_names(struct torture_context *tctx,
787 struct smb2_tree *tree)
788 {
789 TALLOC_CTX *mem_ctx = talloc_new(tctx);
790 NTSTATUS status;
791 union smb_open io;
792 union smb_fileinfo finfo;
793 union smb_fileinfo stinfo;
794 union smb_setfileinfo sinfo;
795 const char *fname = DNAME "\\stream_names.txt";
796 const char *sname1, *sname1b, *sname1c, *sname1d;
797 const char *sname2, *snamew, *snamew2;
798 const char *snamer1;
799 bool ret = true;
800 struct smb2_handle h, h1, h2, h3;
801 int i;
802 const char *four[4] = {
803 "::$DATA",
804 ":\x05Stream\n One:$DATA",
805 ":MStream Two:$DATA",
806 ":?Stream*:$DATA"
807 };
808 const char *five1[5] = {
809 "::$DATA",
810 ":\x05Stream\n One:$DATA",
811 ":BeforeRename:$DATA",
812 ":MStream Two:$DATA",
813 ":?Stream*:$DATA"
814 };
815 const char *five2[5] = {
816 "::$DATA",
817 ":\x05Stream\n One:$DATA",
818 ":AfterRename:$DATA",
819 ":MStream Two:$DATA",
820 ":?Stream*:$DATA"
821 };
822
823 ZERO_STRUCT(h);
824 ZERO_STRUCT(h1);
825 ZERO_STRUCT(h2);
826 ZERO_STRUCT(h3);
827
828 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "\x05Stream\n One");
829 sname1b = talloc_asprintf(mem_ctx, "%s:", sname1);
830 sname1c = talloc_asprintf(mem_ctx, "%s:$FOO", sname1);
831 sname1d = talloc_asprintf(mem_ctx, "%s:?D*a", sname1);
832 sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname, "MStream Two");
833 snamew = talloc_asprintf(mem_ctx, "%s:%s:$DATA", fname, "?Stream*");
834 snamew2 = talloc_asprintf(mem_ctx, "%s\\stream*:%s:$DATA", DNAME,
835 "?Stream*");
836 snamer1 = talloc_asprintf(mem_ctx, "%s:%s:$DATA", fname,
837 "BeforeRename");
838
839 /* clean slate ...*/
840 smb2_util_unlink(tree, fname);
841 smb2_deltree(tree, fname);
842 smb2_deltree(tree, DNAME);
843
844 status = torture_smb2_testdir(tree, DNAME, &h);
845 CHECK_STATUS(status, NT_STATUS_OK);
846
847 torture_comment(tctx, "(%s) testing stream names\n", __location__);
848 ZERO_STRUCT(io.smb2);
849 io.smb2.in.create_flags = 0;
850 io.smb2.in.desired_access = SEC_FILE_WRITE_DATA;
851 io.smb2.in.create_options = 0;
852 io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
853 io.smb2.in.share_access = 0;
854 io.smb2.in.alloc_size = 0;
855 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
856 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
857 io.smb2.in.security_flags = 0;
858 io.smb2.in.fname = sname1;
859
860 status = smb2_create(tree, mem_ctx, &(io.smb2));
861 CHECK_STATUS(status, NT_STATUS_OK);
862 h1 = io.smb2.out.file.handle;
863
864 /*
865 * A different stream does not give a sharing violation
866 */
867
868 io.smb2.in.fname = sname2;
869 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
870 status = smb2_create(tree, mem_ctx, &(io.smb2));
871 CHECK_STATUS(status, NT_STATUS_OK);
872 h2 = io.smb2.out.file.handle;
873
874 /*
875 * ... whereas the same stream does with unchanged access/share_access
876 * flags
877 */
878
879 io.smb2.in.fname = sname1;
880 io.smb2.in.create_disposition = NTCREATEX_DISP_SUPERSEDE;
881 status = smb2_create(tree, mem_ctx, &(io.smb2));
882 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
883
884 io.smb2.in.fname = sname1b;
885 status = smb2_create(tree, mem_ctx, &(io.smb2));
886 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
887
888 io.smb2.in.fname = sname1c;
889 status = smb2_create(tree, mem_ctx, &(io.smb2));
890 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
891 /* w2k returns INVALID_PARAMETER */
892 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
893 } else {
894 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
895 }
896
897 io.smb2.in.fname = sname1d;
898 status = smb2_create(tree, mem_ctx, &(io.smb2));
899 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
900 /* w2k returns INVALID_PARAMETER */
901 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
902 } else {
903 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
904 }
905
906 io.smb2.in.fname = sname2;
907 status = smb2_create(tree, mem_ctx, &(io.smb2));
908 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
909
910 io.smb2.in.fname = snamew;
911 status = smb2_create(tree, mem_ctx, &(io.smb2));
912 CHECK_STATUS(status, NT_STATUS_OK);
913 h3 = io.smb2.out.file.handle;
914
915 io.smb2.in.fname = snamew2;
916 status = smb2_create(tree, mem_ctx, &(io.smb2));
917 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_INVALID);
918
919 io.smb2.in.fname = fname;
920 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
921 status = smb2_create(tree, mem_ctx, &(io.smb2));
922 CHECK_STATUS(status, NT_STATUS_OK);
923 ret &= check_stream_list(tree, tctx, fname, 4, four,
924 io.smb2.out.file.handle);
925 CHECK_VALUE(ret, true);
926 smb2_util_close(tree, h1);
927 smb2_util_close(tree, h2);
928 smb2_util_close(tree, h3);
929
930 if (torture_setting_bool(tctx, "samba4", true)) {
931 goto done;
932 }
933
934 finfo.generic.level = RAW_FILEINFO_ALL_INFORMATION;
935 finfo.generic.in.file.handle = io.smb2.out.file.handle;
936 status = smb2_getinfo_file(tree, mem_ctx, &finfo);
937 CHECK_STATUS(status, NT_STATUS_OK);
938 ret &= check_stream_list(tree, tctx, fname, 4, four,
939 io.smb2.out.file.handle);
940
941 CHECK_VALUE(ret, true);
942 for (i=0; i < 4; i++) {
943 NTTIME write_time;
944 uint64_t stream_size;
945 char *path = talloc_asprintf(tctx, "%s%s",
946 fname, four[i]);
947
948 char *rpath = talloc_strdup(path, path);
949 char *p = strrchr(rpath, ':');
950 /* eat :$DATA */
951 *p = 0;
952 p--;
953 if (*p == ':') {
954 /* eat ::$DATA */
955 *p = 0;
956 }
957 torture_comment(tctx, "(%s): i[%u][%s]\n",
958 __location__, i,path);
959 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
960 io.smb2.in.desired_access = SEC_FILE_READ_ATTRIBUTE |
961 SEC_FILE_WRITE_ATTRIBUTE |
962 SEC_RIGHTS_FILE_ALL;
963 io.smb2.in.fname = path;
964 status = smb2_create(tree, mem_ctx, &(io.smb2));
965 CHECK_STATUS(status, NT_STATUS_OK);
966 h1 = io.smb2.out.file.handle;
967
968 finfo.generic.level = RAW_FILEINFO_ALL_INFORMATION;
969 finfo.generic.in.file.path = fname;
970 status = smb2_getinfo_file(tree, mem_ctx, &finfo);
971 CHECK_STATUS(status, NT_STATUS_OK);
972
973 stinfo.generic.level = RAW_FILEINFO_ALL_INFORMATION;
974 stinfo.generic.in.file.handle = h1;
975 status = smb2_getinfo_file(tree, mem_ctx, &stinfo);
976 CHECK_STATUS(status, NT_STATUS_OK);
977 if (!torture_setting_bool(tctx, "samba3", false)) {
978 CHECK_NTTIME(stinfo.all_info.out.create_time,
979 finfo.all_info.out.create_time);
980 CHECK_NTTIME(stinfo.all_info.out.access_time,
981 finfo.all_info.out.access_time);
982 CHECK_NTTIME(stinfo.all_info.out.write_time,
983 finfo.all_info.out.write_time);
984 CHECK_NTTIME(stinfo.all_info.out.change_time,
985 finfo.all_info.out.change_time);
986 }
987 CHECK_VALUE(stinfo.all_info.out.attrib,
988 finfo.all_info.out.attrib);
989 CHECK_VALUE(stinfo.all_info.out.size,
990 finfo.all_info.out.size);
991 CHECK_VALUE(stinfo.all_info.out.delete_pending,
992 finfo.all_info.out.delete_pending);
993 CHECK_VALUE(stinfo.all_info.out.directory,
994 finfo.all_info.out.directory);
995 CHECK_VALUE(stinfo.all_info.out.ea_size,
996 finfo.all_info.out.ea_size);
997
998 stinfo.generic.level = RAW_FILEINFO_NAME_INFORMATION;
999 stinfo.generic.in.file.handle = h1;
1000 status = smb2_getinfo_file(tree, mem_ctx, &stinfo);
1001 CHECK_STATUS(status, NT_STATUS_OK);
1002 if (!torture_setting_bool(tctx, "samba3", false)) {
1003 CHECK_STR(rpath, stinfo.name_info.out.fname.s);
1004 }
1005
1006 write_time = finfo.all_info.out.write_time;
1007 write_time += i*1000000;
1008 write_time /= 1000000;
1009 write_time *= 1000000;
1010
1011 ZERO_STRUCT(sinfo);
1012 sinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION;
1013 sinfo.basic_info.in.file.handle = h1;
1014 sinfo.basic_info.in.write_time = write_time;
1015 sinfo.basic_info.in.attrib = stinfo.all_info.out.attrib;
1016 status = smb2_setinfo_file(tree, &sinfo);
1017 CHECK_STATUS(status, NT_STATUS_OK);
1018
1019 stream_size = i*8192;
1020
1021 ZERO_STRUCT(sinfo);
1022 sinfo.end_of_file_info.level =
1023 RAW_SFILEINFO_END_OF_FILE_INFORMATION;
1024 sinfo.end_of_file_info.in.file.handle = h1;
1025 sinfo.end_of_file_info.in.size = stream_size;
1026 status = smb2_setinfo_file(tree, &sinfo);
1027 CHECK_STATUS(status, NT_STATUS_OK);
1028
1029 stinfo.generic.level = RAW_FILEINFO_ALL_INFORMATION;
1030 stinfo.generic.in.file.handle = h1;
1031 status = smb2_getinfo_file(tree, mem_ctx, &stinfo);
1032 CHECK_STATUS(status, NT_STATUS_OK);
1033 if (!torture_setting_bool(tctx, "samba3", false)) {
1034 CHECK_NTTIME(stinfo.all_info.out.write_time,
1035 write_time);
1036 CHECK_VALUE(stinfo.all_info.out.attrib,
1037 finfo.all_info.out.attrib);
1038 }
1039 CHECK_VALUE(stinfo.all_info.out.size,
1040 stream_size);
1041 CHECK_VALUE(stinfo.all_info.out.delete_pending,
1042 finfo.all_info.out.delete_pending);
1043 CHECK_VALUE(stinfo.all_info.out.directory,
1044 finfo.all_info.out.directory);
1045 CHECK_VALUE(stinfo.all_info.out.ea_size,
1046 finfo.all_info.out.ea_size);
1047
1048 io.smb2.in.fname = fname;
1049 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
1050 status = smb2_create(tree, mem_ctx, &(io.smb2));
1051 CHECK_STATUS(status, NT_STATUS_OK);
1052 ret &= check_stream_list(tree, tctx, fname, 4, four,
1053 io.smb2.out.file.handle);
1054
1055 smb2_util_close(tree, h1);
1056 talloc_free(path);
1057 }
1058
1059 torture_comment(tctx, "(%s): testing stream renames\n", __location__);
1060 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
1061 io.smb2.in.desired_access = SEC_FILE_READ_ATTRIBUTE |
1062 SEC_FILE_WRITE_ATTRIBUTE |
1063 SEC_RIGHTS_FILE_ALL;
1064 io.smb2.in.fname = snamer1;
1065 status = smb2_create(tree, mem_ctx, &(io.smb2));
1066 CHECK_STATUS(status, NT_STATUS_OK);
1067 h1 = io.smb2.out.file.handle;
1068 ret &= check_stream_list(tree,tctx, fname, 5, five1,
1069 io.smb2.out.file.handle);
1070
1071 ZERO_STRUCT(sinfo);
1072 sinfo.rename_information.level = RAW_SFILEINFO_RENAME_INFORMATION;
1073 sinfo.rename_information.in.file.handle = h1;
1074 sinfo.rename_information.in.overwrite = true;
1075 sinfo.rename_information.in.root_fid = 0;
1076 sinfo.rename_information.in.new_name = ":AfterRename:$DATA";
1077 status = smb2_setinfo_file(tree, &sinfo);
1078 CHECK_STATUS(status, NT_STATUS_OK);
1079
1080 ret &= check_stream_list(tree,tctx, fname, 5, five2,
1081 io.smb2.out.file.handle);
1082
1083 CHECK_VALUE(ret, true);
1084 ZERO_STRUCT(sinfo);
1085 sinfo.rename_information.level = RAW_SFILEINFO_RENAME_INFORMATION;
1086 sinfo.rename_information.in.file.handle = h1;
1087 sinfo.rename_information.in.overwrite = false;
1088 sinfo.rename_information.in.root_fid = 0;
1089 sinfo.rename_information.in.new_name = ":MStream Two:$DATA";
1090 status = smb2_setinfo_file(tree, &sinfo);
1091 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_COLLISION);
1092
1093 ret &= check_stream_list(tree,tctx, fname, 5, five2,
1094 io.smb2.out.file.handle);
1095
1096 ZERO_STRUCT(sinfo);
1097 sinfo.rename_information.level = RAW_SFILEINFO_RENAME_INFORMATION;
1098 sinfo.rename_information.in.file.handle = h1;
1099 sinfo.rename_information.in.overwrite = true;
1100 sinfo.rename_information.in.root_fid = 0;
1101 sinfo.rename_information.in.new_name = ":MStream Two:$DATA";
1102 status = smb2_setinfo_file(tree, &sinfo);
1103 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
1104
1105 ret &= check_stream_list(tree,tctx, fname, 5, five2,
1106 io.smb2.out.file.handle);
1107
1108 CHECK_VALUE(ret, true);
1109 /* TODO: we need to test more rename combinations */
1110
1111 done:
1112 smb2_util_close(tree, h1);
1113 status = smb2_util_unlink(tree, fname);
1114 smb2_deltree(tree, DNAME);
1115 talloc_free(mem_ctx);
1116
1117 return ret;
1118 }
1119
1120 /*
1121 test stream names
1122 */
1123 static bool test_stream_names2(struct torture_context *tctx,
1124 struct smb2_tree *tree)
1125 {
1126 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1127 NTSTATUS status;
1128 union smb_open io;
1129 const char *fname = DNAME "\\stream_names2.txt";
1130 bool ret = true;
1131 struct smb2_handle h = {{0}};
1132 struct smb2_handle h1 = {{0}};
1133 uint8_t i;
1134
1135 smb2_util_unlink(tree, fname);
1136 smb2_deltree(tree, DNAME);
1137
1138 status = torture_smb2_testdir(tree, DNAME, &h);
1139 CHECK_STATUS(status, NT_STATUS_OK);
1140
1141 torture_comment(tctx, "(%s) testing stream names\n", __location__);
1142 ZERO_STRUCT(io.smb2);
1143 io.smb2.in.create_flags = 0;
1144 io.smb2.in.desired_access = SEC_FILE_WRITE_DATA;
1145 io.smb2.in.create_options = 0;
1146 io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
1147 io.smb2.in.share_access = 0;
1148 io.smb2.in.alloc_size = 0;
1149 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
1150 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
1151 io.smb2.in.security_flags = 0;
1152 io.smb2.in.fname = fname;
1153 status = smb2_create(tree, mem_ctx, &(io.smb2));
1154 CHECK_STATUS(status, NT_STATUS_OK);
1155 h1 = io.smb2.out.file.handle;
1156
1157 for (i=0x01; i < 0x7F; i++) {
1158 char *path = talloc_asprintf(mem_ctx, "%s:Stream%c0x%02X:$DATA",
1159 fname, i, i);
1160 NTSTATUS expected;
1161
1162 switch (i) {
1163 case '/':/*0x2F*/
1164 case ':':/*0x3A*/
1165 case '\\':/*0x5C*/
1166 expected = NT_STATUS_OBJECT_NAME_INVALID;
1167 break;
1168 default:
1169 expected = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1170 break;
1171 }
1172
1173
1174 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
1175 io.smb2.in.fname = path;
1176 status = smb2_create(tree, mem_ctx, &(io.smb2));
1177 if (!NT_STATUS_EQUAL(status, expected)) {
1178 torture_comment(tctx,
1179 "(%s) %s:Stream%c0x%02X:$DATA%s => expected[%s]\n",
1180 __location__, fname, isprint(i)?(char)i:' ', i,
1181 isprint(i)?"":" (not printable)",
1182 nt_errstr(expected));
1183 }
1184 CHECK_STATUS(status, expected);
1185
1186 talloc_free(path);
1187 }
1188
1189 done:
1190 smb2_util_close(tree, h1);
1191 status = smb2_util_unlink(tree, fname);
1192 smb2_deltree(tree, DNAME);
1193 talloc_free(mem_ctx);
1194
1195 return ret;
1196 }
1197
1198 /*
1199 test case insensitive stream names
1200 */
1201 static bool test_stream_names3(struct torture_context *tctx,
1202 struct smb2_tree *tree)
1203 {
1204 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1205 NTSTATUS status;
1206 union smb_fsinfo info;
1207 const char *fname = DNAME "\\stream_names3.txt";
1208 const char *sname = NULL;
1209 const char *snamel = NULL;
1210 const char *snameu = NULL;
1211 const char *sdname = NULL;
1212 const char *sdnamel = NULL;
1213 const char *sdnameu = NULL;
1214 bool ret = true;
1215 struct smb2_handle h = {{0}};
1216 struct smb2_handle hf = {{0}};
1217 struct smb2_handle hs = {{0}};
1218 struct smb2_handle hsl = {{0}};
1219 struct smb2_handle hsu = {{0}};
1220 struct smb2_handle hsd = {{0}};
1221 struct smb2_handle hsdl = {{0}};
1222 struct smb2_handle hsdu = {{0}};
1223 const char *streams[] = { "::$DATA", ":StreamName:$DATA", };
1224
1225 smb2_deltree(tree, DNAME);
1226 status = torture_smb2_testdir(tree, DNAME, &h);
1227 CHECK_STATUS(status, NT_STATUS_OK);
1228
1229 ZERO_STRUCT(info);
1230 info.generic.level = RAW_QFS_ATTRIBUTE_INFORMATION;
1231 info.generic.handle = h;
1232 status = smb2_getinfo_fs(tree, tree, &info);
1233 CHECK_STATUS(status, NT_STATUS_OK);
1234 if (!(info.attribute_info.out.fs_attr & FILE_CASE_SENSITIVE_SEARCH)) {
1235 torture_skip(tctx, "No FILE_CASE_SENSITIVE_SEARCH supported");
1236 }
1237
1238 /*
1239 * We create the following file:
1240 *
1241 * teststreams\\stream_names3.txt
1242 *
1243 * and add a stream named 'StreamName'
1244 *
1245 * Then we try to open the stream using the following names:
1246 *
1247 * teststreams\\stream_names3.txt:StreamName
1248 * teststreams\\stream_names3.txt:streamname
1249 * teststreams\\stream_names3.txt:STREAMNAME
1250 * teststreams\\stream_names3.txt:StreamName:$dAtA
1251 * teststreams\\stream_names3.txt:streamname:$data
1252 * teststreams\\stream_names3.txt:STREAMNAME:$DATA
1253 */
1254 sname = talloc_asprintf(tctx, "%s:StreamName", fname);
1255 torture_assert_not_null(tctx, sname, __location__);
1256 snamel = strlower_talloc(tctx, sname);
1257 torture_assert_not_null(tctx, snamel, __location__);
1258 snameu = strupper_talloc(tctx, sname);
1259 torture_assert_not_null(tctx, snameu, __location__);
1260
1261 sdname = talloc_asprintf(tctx, "%s:$dAtA", sname);
1262 torture_assert_not_null(tctx, sdname, __location__);
1263 sdnamel = strlower_talloc(tctx, sdname);
1264 torture_assert_not_null(tctx, sdnamel, __location__);
1265 sdnameu = strupper_talloc(tctx, sdname);
1266 torture_assert_not_null(tctx, sdnameu, __location__);
1267
1268 torture_comment(tctx, "(%s) testing case insensitive stream names\n",
1269 __location__);
1270 status = torture_smb2_testfile(tree, fname, &hf);
1271 CHECK_STATUS(status, NT_STATUS_OK);
1272 status = torture_smb2_testfile(tree, sname, &hs);
1273 CHECK_STATUS(status, NT_STATUS_OK);
1274 smb2_util_close(tree, hs);
1275
1276 torture_assert(tctx,
1277 check_stream_list(tree, tctx, fname,
1278 ARRAY_SIZE(streams),
1279 streams,
1280 hf),
1281 "streams");
1282
1283 status = torture_smb2_open(tree, sname, SEC_RIGHTS_FILE_ALL, &hs);
1284 CHECK_STATUS(status, NT_STATUS_OK);
1285 status = torture_smb2_open(tree, snamel, SEC_RIGHTS_FILE_ALL, &hsl);
1286 CHECK_STATUS(status, NT_STATUS_OK);
1287 status = torture_smb2_open(tree, snameu, SEC_RIGHTS_FILE_ALL, &hsu);
1288 CHECK_STATUS(status, NT_STATUS_OK);
1289 status = torture_smb2_open(tree, sdname, SEC_RIGHTS_FILE_ALL, &hsd);
1290 CHECK_STATUS(status, NT_STATUS_OK);
1291 status = torture_smb2_open(tree, sdnamel, SEC_RIGHTS_FILE_ALL, &hsdl);
1292 CHECK_STATUS(status, NT_STATUS_OK);
1293 status = torture_smb2_open(tree, sdnameu, SEC_RIGHTS_FILE_ALL, &hsdu);
1294 CHECK_STATUS(status, NT_STATUS_OK);
1295
1296 done:
1297 smb2_util_close(tree, hsdu);
1298 smb2_util_close(tree, hsdl);
1299 smb2_util_close(tree, hsd);
1300 smb2_util_close(tree, hsu);
1301 smb2_util_close(tree, hsl);
1302 smb2_util_close(tree, hs);
1303 smb2_util_close(tree, hf);
1304 smb2_util_close(tree, h);
1305 status = smb2_util_unlink(tree, fname);
1306 smb2_deltree(tree, DNAME);
1307 talloc_free(mem_ctx);
1308
1309 return ret;
1310 }
1311
1312 #define CHECK_CALL_HANDLE(call, rightstatus) do { \
1313 sfinfo.generic.level = RAW_SFILEINFO_ ## call; \
1314 sfinfo.generic.in.file.handle = h1; \
1315 status = smb2_setinfo_file(tree, &sfinfo); \
1316 torture_assert_ntstatus_equal_goto(tctx, status, rightstatus, ret, done, #call); \
1317 finfo1.generic.level = RAW_FILEINFO_ALL_INFORMATION; \
1318 finfo1.generic.in.file.handle = h1; \
1319 status2 = smb2_getinfo_file(tree, tctx, &finfo1); \
1320 torture_assert_ntstatus_ok_goto(tctx, status2, ret, done, "ALL_INFO"); \
1321 } while (0)
1322
1323 /*
1324 test stream renames
1325 */
1326 static bool test_stream_rename(struct torture_context *tctx,
1327 struct smb2_tree *tree)
1328 {
1329 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1330 NTSTATUS status, status2;
1331 union smb_open io;
1332 const char *fname = DNAME "\\stream_rename.txt";
1333 const char *sname1, *sname2;
1334 union smb_fileinfo finfo1;
1335 union smb_setfileinfo sfinfo;
1336 bool ret = true;
1337 struct smb2_handle h = {{0}};
1338 struct smb2_handle h1 = {{0}};
1339
1340 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
1341 sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname,
1342 "Second Stream");
1343
1344 smb2_util_unlink(tree, fname);
1345 smb2_deltree(tree, DNAME);
1346
1347 status = torture_smb2_testdir(tree, DNAME, &h);
1348 CHECK_STATUS(status, NT_STATUS_OK);
1349
1350 torture_comment(tctx, "(%s) testing stream renames\n", __location__);
1351 ZERO_STRUCT(io.smb2);
1352 io.smb2.in.create_flags = 0;
1353 io.smb2.in.desired_access = SEC_FILE_READ_ATTRIBUTE |
1354 SEC_FILE_WRITE_ATTRIBUTE |
1355 SEC_RIGHTS_FILE_ALL;
1356 io.smb2.in.create_options = 0;
1357 io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
1358 io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1359 NTCREATEX_SHARE_ACCESS_WRITE |
1360 NTCREATEX_SHARE_ACCESS_DELETE;
1361 io.smb2.in.alloc_size = 0;
1362 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
1363 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
1364 io.smb2.in.security_flags = 0;
1365 io.smb2.in.fname = sname1;
1366
1367 /* Create two streams. */
1368 status = smb2_create(tree, mem_ctx, &(io.smb2));
1369 CHECK_STATUS(status, NT_STATUS_OK);
1370 h1 = io.smb2.out.file.handle;
1371 smb2_util_close(tree, h1);
1372
1373 io.smb2.in.fname = sname2;
1374 status = smb2_create(tree, mem_ctx, &(io.smb2));
1375 CHECK_STATUS(status, NT_STATUS_OK);
1376 h1 = io.smb2.out.file.handle;
1377
1378 smb2_util_close(tree, h1);
1379
1380 /*
1381 * Open the second stream.
1382 */
1383
1384 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
1385 status = smb2_create(tree, mem_ctx, &(io.smb2));
1386 CHECK_STATUS(status, NT_STATUS_OK);
1387 h1 = io.smb2.out.file.handle;
1388
1389 /*
1390 * Now rename the second stream onto the first.
1391 */
1392
1393 ZERO_STRUCT(sfinfo);
1394
1395 sfinfo.rename_information.in.overwrite = 1;
1396 sfinfo.rename_information.in.root_fid = 0;
1397 sfinfo.rename_information.in.new_name = ":Stream One";
1398 CHECK_CALL_HANDLE(RENAME_INFORMATION, NT_STATUS_OK);
1399 done:
1400 smb2_util_close(tree, h1);
1401 status = smb2_util_unlink(tree, fname);
1402 smb2_deltree(tree, DNAME);
1403 talloc_free(mem_ctx);
1404
1405 return ret;
1406 }
1407
1408 static bool test_stream_rename2(struct torture_context *tctx,
1409 struct smb2_tree *tree)
1410 {
1411 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1412 NTSTATUS status;
1413 union smb_open io;
1414 const char *fname1 = DNAME "\\stream_rename2.txt";
1415 const char *fname2 = DNAME "\\stream2_rename2.txt";
1416 const char *stream_name1 = ":Stream One:$DATA";
1417 const char *stream_name2 = ":Stream Two:$DATA";
1418 const char *stream_name_default = "::$DATA";
1419 const char *sname1;
1420 const char *sname2;
1421 bool ret = true;
1422 struct smb2_handle h, h1;
1423 union smb_setfileinfo sinfo;
1424
1425 ZERO_STRUCT(h);
1426 ZERO_STRUCT(h1);
1427
1428 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname1, "Stream One");
1429 sname2 = talloc_asprintf(mem_ctx, "%s:%s", fname1, "Stream Two");
1430
1431 smb2_util_unlink(tree, fname1);
1432 smb2_util_unlink(tree, fname2);
1433 smb2_deltree(tree, DNAME);
1434
1435 status = torture_smb2_testdir(tree, DNAME, &h);
1436 CHECK_STATUS(status, NT_STATUS_OK);
1437
1438 ZERO_STRUCT(io.smb2);
1439 io.smb2.in.create_flags = 0;
1440 io.smb2.in.desired_access = SEC_FILE_READ_DATA |
1441 SEC_FILE_WRITE_DATA |
1442 SEC_STD_DELETE |
1443 SEC_FILE_APPEND_DATA |
1444 SEC_STD_READ_CONTROL;
1445 io.smb2.in.create_options = 0;
1446 io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
1447 io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1448 NTCREATEX_SHARE_ACCESS_WRITE |
1449 NTCREATEX_SHARE_ACCESS_DELETE;
1450 io.smb2.in.alloc_size = 0;
1451 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
1452 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
1453 io.smb2.in.security_flags = 0;
1454 io.smb2.in.fname = sname1;
1455
1456 /* Open/create new stream. */
1457 status = smb2_create(tree, mem_ctx, &(io.smb2));
1458 CHECK_STATUS(status, NT_STATUS_OK);
1459
1460 smb2_util_close(tree, io.smb2.out.file.handle);
1461
1462 /*
1463 * Reopen the stream for SMB2 renames.
1464 */
1465 io.smb2.in.fname = sname1;
1466 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
1467 status = smb2_create(tree, mem_ctx, &(io.smb2));
1468 CHECK_STATUS(status, NT_STATUS_OK);
1469 h1 = io.smb2.out.file.handle;
1470
1471 /*
1472 * Check SMB2 rename of a stream using :<stream>.
1473 */
1474 torture_comment(tctx, "(%s) Checking SMB2 rename of a stream using "
1475 ":<stream>\n", __location__);
1476 ZERO_STRUCT(sinfo);
1477 sinfo.rename_information.level = RAW_SFILEINFO_RENAME_INFORMATION_SMB2;
1478 sinfo.rename_information.in.file.handle = h1;
1479 sinfo.rename_information.in.overwrite = 1;
1480 sinfo.rename_information.in.root_fid = 0;
1481 sinfo.rename_information.in.new_name = stream_name1;
1482 status = smb2_setinfo_file(tree, &sinfo);
1483 CHECK_STATUS(status, NT_STATUS_OK);
1484
1485 /*
1486 * Check SMB2 rename of an overwriting stream using :<stream>.
1487 */
1488 torture_comment(tctx, "(%s) Checking SMB2 rename of an overwriting "
1489 "stream using :<stream>\n", __location__);
1490
1491 /* Create second stream. */
1492 io.smb2.in.fname = sname2;
1493 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
1494 status = smb2_create(tree, mem_ctx, &(io.smb2));
1495 CHECK_STATUS(status, NT_STATUS_OK);
1496 smb2_util_close(tree, io.smb2.out.file.handle);
1497
1498 /* Rename the first stream onto the second. */
1499 sinfo.rename_information.in.file.handle = h1;
1500 sinfo.rename_information.in.new_name = stream_name2;
1501 status = smb2_setinfo_file(tree, &sinfo);
1502 CHECK_STATUS(status, NT_STATUS_OK);
1503
1504 smb2_util_close(tree, h1);
1505
1506 /*
1507 * Reopen the stream with the new name.
1508 */
1509 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
1510 io.smb2.in.fname = sname2;
1511 status = smb2_create(tree, mem_ctx, &(io.smb2));
1512 CHECK_STATUS(status, NT_STATUS_OK);
1513 h1 = io.smb2.out.file.handle;
1514
1515 /*
1516 * Check SMB2 rename of a stream using <base>:<stream>.
1517 */
1518 torture_comment(tctx, "(%s) Checking SMB2 rename of a stream using "
1519 "<base>:<stream>\n", __location__);
1520 sinfo.rename_information.in.file.handle = h1;
1521 sinfo.rename_information.in.new_name = sname1;
1522 status = smb2_setinfo_file(tree, &sinfo);
1523 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
1524
1525 /*
1526 * Check SMB2 rename to the default stream using :<stream>.
1527 */
1528 torture_comment(tctx, "(%s) Checking SMB2 rename to default stream "
1529 "using :<stream>\n", __location__);
1530 sinfo.rename_information.in.file.handle = h1;
1531 sinfo.rename_information.in.new_name = stream_name_default;
1532 status = smb2_setinfo_file(tree, &sinfo);
1533 CHECK_STATUS(status, NT_STATUS_OK);
1534
1535 smb2_util_close(tree, h1);
1536
1537 done:
1538 smb2_util_close(tree, h1);
1539 status = smb2_util_unlink(tree, fname1);
1540 status = smb2_util_unlink(tree, fname2);
1541 smb2_deltree(tree, DNAME);
1542 talloc_free(mem_ctx);
1543
1544 return ret;
1545 }
1546
1547 static bool create_file_with_stream(struct torture_context *tctx,
1548 struct smb2_tree *tree,
1549 TALLOC_CTX *mem_ctx,
1550 const char *base_fname,
1551 const char *stream)
1552 {
1553 NTSTATUS status;
1554 bool ret = true;
1555 union smb_open io;
1556
1557 /* Create a file with a stream */
1558 ZERO_STRUCT(io.smb2);
1559 io.smb2.in.create_flags = 0;
1560 io.smb2.in.desired_access = SEC_FILE_READ_DATA |
1561 SEC_FILE_WRITE_DATA |
1562 SEC_FILE_APPEND_DATA |
1563 SEC_STD_READ_CONTROL;
1564 io.smb2.in.create_options = 0;
1565 io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
1566 io.smb2.in.share_access = 0;
1567 io.smb2.in.alloc_size = 0;
1568 io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
1569 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
1570 io.smb2.in.security_flags = 0;
1571 io.smb2.in.fname = stream;
1572
1573 status = smb2_create(tree, mem_ctx, &(io.smb2));
1574 CHECK_STATUS(status, NT_STATUS_OK);
1575
1576 done:
1577 smb2_util_close(tree, io.smb2.out.file.handle);
1578 return ret;
1579 }
1580
1581
1582 /* Test how streams interact with create dispositions */
1583 static bool test_stream_create_disposition(struct torture_context *tctx,
1584 struct smb2_tree *tree)
1585 {
1586 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1587 NTSTATUS status;
1588 union smb_open io;
1589 const char *fname = DNAME "\\stream_create_disp.txt";
1590 const char *stream = "Stream One:$DATA";
1591 const char *fname_stream;
1592 const char *default_stream_name = "::$DATA";
1593 const char *stream_list[2];
1594 bool ret = true;
1595 struct smb2_handle h = {{0}};
1596 struct smb2_handle h1 = {{0}};
1597
1598 /* clean slate .. */
1599 smb2_util_unlink(tree, fname);
1600 smb2_deltree(tree, fname);
1601 smb2_deltree(tree, DNAME);
1602
1603 status = torture_smb2_testdir(tree, DNAME, &h);
1604 CHECK_STATUS(status, NT_STATUS_OK);
1605
1606 fname_stream = talloc_asprintf(mem_ctx, "%s:%s", fname, stream);
1607
1608 stream_list[0] = talloc_asprintf(mem_ctx, ":%s", stream);
1609 stream_list[1] = default_stream_name;
1610
1611 if (!create_file_with_stream(tctx, tree, mem_ctx, fname,
1612 fname_stream)) {
1613 goto done;
1614 }
1615
1616 /* Open the base file with OPEN */
1617 ZERO_STRUCT(io.smb2);
1618 io.smb2.in.create_flags = 0;
1619 io.smb2.in.desired_access = SEC_FILE_READ_DATA |
1620 SEC_FILE_WRITE_DATA |
1621 SEC_FILE_APPEND_DATA |
1622 SEC_STD_READ_CONTROL;
1623 io.smb2.in.create_options = 0;
1624 io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
1625 io.smb2.in.share_access = 0;
1626 io.smb2.in.alloc_size = 0;
1627 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
1628 io.smb2.in.security_flags = 0;
1629 io.smb2.in.fname = fname;
1630
1631 /*
1632 * check create open: sanity check
1633 */
1634 torture_comment(tctx, "(%s) Checking create disp: open\n",
1635 __location__);
1636 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
1637 status = smb2_create(tree, mem_ctx, &(io.smb2));
1638 CHECK_STATUS(status, NT_STATUS_OK);
1639 if (!check_stream_list(tree, tctx, fname, 2, stream_list,
1640 io.smb2.out.file.handle)) {
1641 goto done;
1642 }
1643 smb2_util_close(tree, io.smb2.out.file.handle);
1644
1645 /*
1646 * check create overwrite
1647 */
1648 torture_comment(tctx, "(%s) Checking create disp: overwrite\n",
1649 __location__);
1650 io.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
1651 status = smb2_create(tree, mem_ctx, &(io.smb2));
1652 CHECK_STATUS(status, NT_STATUS_OK);
1653 if (!check_stream_list(tree, tctx, fname, 1, &default_stream_name,
1654 io.smb2.out.file.handle)) {
1655 goto done;
1656 }
1657 smb2_util_close(tree, io.smb2.out.file.handle);
1658
1659 /*
1660 * check create overwrite_if
1661 */
1662 torture_comment(tctx, "(%s) Checking create disp: overwrite_if\n",
1663 __location__);
1664 smb2_util_unlink(tree, fname);
1665 if (!create_file_with_stream(tctx, tree, mem_ctx, fname, fname_stream))
1666 goto done;
1667
1668 io.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
1669 status = smb2_create(tree, mem_ctx, &(io.smb2));
1670 CHECK_STATUS(status, NT_STATUS_OK);
1671 if (!check_stream_list(tree, tctx, fname, 1, &default_stream_name,
1672 io.smb2.out.file.handle)) {
1673 goto done;
1674 }
1675 smb2_util_close(tree, io.smb2.out.file.handle);
1676
1677 /*
1678 * check create supersede
1679 */
1680 torture_comment(tctx, "(%s) Checking create disp: supersede\n",
1681 __location__);
1682 smb2_util_unlink(tree, fname);
1683 if (!create_file_with_stream(tctx, tree, mem_ctx, fname,
1684 fname_stream)) {
1685 goto done;
1686 }
1687
1688 io.smb2.in.create_disposition = NTCREATEX_DISP_SUPERSEDE;
1689 status = smb2_create(tree, mem_ctx, &(io.smb2));
1690 CHECK_STATUS(status, NT_STATUS_OK);
1691 if (!check_stream_list(tree, tctx, fname, 1, &default_stream_name,
1692 io.smb2.out.file.handle)) {
1693 goto done;
1694 }
1695 smb2_util_close(tree, io.smb2.out.file.handle);
1696
1697 /*
1698 * check create overwrite_if on a stream.
1699 */
1700 torture_comment(tctx, "(%s) Checking create disp: overwrite_if on "
1701 "stream\n", __location__);
1702 smb2_util_unlink(tree, fname);
1703 if (!create_file_with_stream(tctx, tree, mem_ctx, fname,
1704 fname_stream)) {
1705 goto done;
1706 }
1707
1708 io.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
1709 io.smb2.in.fname = fname_stream;
1710 status = smb2_create(tree, mem_ctx, &(io.smb2));
1711 CHECK_STATUS(status, NT_STATUS_OK);
1712 if (!check_stream_list(tree, tctx, fname, 2, stream_list,
1713 io.smb2.out.file.handle)) {
1714 goto done;
1715 }
1716 smb2_util_close(tree, io.smb2.out.file.handle);
1717 done:
1718 smb2_util_close(tree, h1);
1719 smb2_util_unlink(tree, fname);
1720 smb2_deltree(tree, DNAME);
1721 talloc_free(mem_ctx);
1722
1723 return ret;
1724 }
1725
1726 static bool open_stream(struct smb2_tree *tree,
1727 struct torture_context *mem_ctx,
1728 const char *fname,
1729 struct smb2_handle *h_out)
1730 {
1731 NTSTATUS status;
1732 union smb_open io;
1733
1734 ZERO_STRUCT(io.smb2);
1735 io.smb2.in.create_flags = 0;
1736 io.smb2.in.desired_access = SEC_FILE_READ_DATA |
1737 SEC_FILE_WRITE_DATA |
1738 SEC_FILE_APPEND_DATA |
1739 SEC_STD_READ_CONTROL |
1740 SEC_FILE_WRITE_ATTRIBUTE;
1741 io.smb2.in.create_options = 0;
1742 io.smb2.in.file_attributes = 0;
1743 io.smb2.in.share_access = 0;
1744 io.smb2.in.alloc_size = 0;
1745 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
1746 io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
1747 io.smb2.in.security_flags = 0;
1748 io.smb2.in.fname = fname;
1749
1750 status = smb2_create(tree, mem_ctx, &(io.smb2));
1751 if (!NT_STATUS_IS_OK(status)) {
1752 return false;
1753 }
1754 *h_out = io.smb2.out.file.handle;
1755 return true;
1756 }
1757
1758
1759 /* Test the effect of setting attributes on a stream. */
1760 static bool test_stream_attributes1(struct torture_context *tctx,
1761 struct smb2_tree *tree)
1762 {
1763 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1764 bool ret = true;
1765 NTSTATUS status;
1766 union smb_open io;
1767 const char *fname = DNAME "\\stream_attr.txt";
1768 const char *stream = "Stream One:$DATA";
1769 const char *fname_stream;
1770 struct smb2_handle h, h1;
1771 union smb_fileinfo finfo;
1772 union smb_setfileinfo sfinfo;
1773 time_t basetime = (time(NULL) - 86400) & ~1;
1774
1775 ZERO_STRUCT(h);
1776 ZERO_STRUCT(h1);
1777
1778 torture_comment(tctx, "(%s) testing attribute setting on stream\n",
1779 __location__);
1780
1781 /* clean slate .. */
1782 smb2_util_unlink(tree, fname);
1783 smb2_deltree(tree, fname);
1784 smb2_deltree(tree, DNAME);
1785
1786 status = torture_smb2_testdir(tree, DNAME, &h);
1787 CHECK_STATUS(status, NT_STATUS_OK);
1788
1789 fname_stream = talloc_asprintf(mem_ctx, "%s:%s", fname, stream);
1790
1791 /* Create a file with a stream with attribute FILE_ATTRIBUTE_ARCHIVE. */
1792 ret = create_file_with_stream(tctx, tree, mem_ctx, fname,
1793 fname_stream);
1794 if (!ret) {
1795 goto done;
1796 }
1797
1798 ZERO_STRUCT(io.smb2);
1799 io.smb2.in.fname = fname;
1800 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
1801 io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
1802 io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1803 NTCREATEX_SHARE_ACCESS_WRITE |
1804 NTCREATEX_SHARE_ACCESS_DELETE;
1805 status = smb2_create(tree, mem_ctx, &(io.smb2));
1806 CHECK_STATUS(status, NT_STATUS_OK);
1807
1808 ZERO_STRUCT(finfo);
1809 finfo.generic.level = RAW_FILEINFO_BASIC_INFORMATION;
1810 finfo.generic.in.file.handle = io.smb2.out.file.handle;
1811 status = smb2_getinfo_file(tree, mem_ctx, &finfo);
1812 CHECK_STATUS(status, NT_STATUS_OK);
1813
1814 if (finfo.basic_info.out.attrib != FILE_ATTRIBUTE_ARCHIVE) {
1815 torture_comment(tctx, "(%s) Incorrect attrib %x - should be "
1816 "%x\n", __location__,
1817 (unsigned int)finfo.basic_info.out.attrib,
1818 (unsigned int)FILE_ATTRIBUTE_ARCHIVE);
1819 ret = false;
1820 goto done;
1821 }
1822
1823 smb2_util_close(tree, io.smb2.out.file.handle);
1824 /* Now open the stream name. */
1825
1826 if (!open_stream(tree, tctx, fname_stream, &h1)) {
1827 goto done;
1828 }
1829
1830 /* Change the time on the stream. */
1831 ZERO_STRUCT(sfinfo);
1832 unix_to_nt_time(&sfinfo.basic_info.in.write_time, basetime);
1833 sfinfo.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
1834 sfinfo.generic.in.file.handle = h1;
1835 status = smb2_setinfo_file(tree, &sfinfo);
1836 if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
1837 torture_comment(tctx, "(%s) %s - %s (should be %s)\n",
1838 __location__, "SETATTR",
1839 nt_errstr(status), nt_errstr(NT_STATUS_OK));
1840 ret = false;
1841 goto done;
1842 }
1843
1844 smb2_util_close(tree, h1);
1845
1846 ZERO_STRUCT(io.smb2);
1847 io.smb2.in.fname = fname;
1848 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
1849 io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
1850 status = smb2_create(tree, mem_ctx, &(io.smb2));
1851 CHECK_STATUS(status, NT_STATUS_OK);
1852 h1 = io.smb2.out.file.handle;
1853
1854 ZERO_STRUCT(finfo);
1855 finfo.generic.level = RAW_FILEINFO_BASIC_INFORMATION;
1856 finfo.generic.in.file.handle = h1;
1857 status = smb2_getinfo_file(tree, mem_ctx, &finfo);
1858 if (!NT_STATUS_IS_OK(status)) {
1859 torture_comment(tctx, "(%s) %s pathinfo - %s\n",
1860 __location__, "SETATTRE", nt_errstr(status));
1861 ret = false;
1862 goto done;
1863 }
1864
1865 if (nt_time_to_unix(finfo.basic_info.out.write_time) != basetime) {
1866 torture_comment(tctx, "(%s) time incorrect.\n", __location__);
1867 ret = false;
1868 goto done;
1869 }
1870 smb2_util_close(tree, h1);
1871
1872 if (!open_stream(tree, tctx, fname_stream, &h1)) {
1873 goto done;
1874 }
1875
1876 /* Changing attributes on stream */
1877 ZERO_STRUCT(sfinfo);
1878 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_READONLY;
1879
1880 sfinfo.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
1881 sfinfo.generic.in.file.handle = h1;
1882 status = smb2_setinfo_file(tree, &sfinfo);
1883 if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
1884 torture_comment(tctx, "(%s) %s - %s (should be %s)\n",
1885 __location__, "SETATTR",
1886 nt_errstr(status), nt_errstr(NT_STATUS_OK));
1887 ret = false;
1888 goto done;
1889 }
1890
1891 smb2_util_close(tree, h1);
1892
1893 ZERO_STRUCT(io.smb2);
1894 io.smb2.in.fname = fname;
1895 io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
1896 io.smb2.in.desired_access = SEC_FILE_READ_DATA;
1897 status = smb2_create(tree, mem_ctx, &(io.smb2));
1898 CHECK_STATUS(status, NT_STATUS_OK);
1899 h1 = io.smb2.out.file.handle;
1900
1901 ZERO_STRUCT(finfo);
1902 finfo.generic.level = RAW_FILEINFO_BASIC_INFORMATION;
1903 finfo.generic.in.file.handle = h1;
1904 status = smb2_getinfo_file(tree, mem_ctx, &finfo);
1905 CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
1906
1907 done:
1908 smb2_util_close(tree, h1);
1909 smb2_util_unlink(tree, fname);
1910 smb2_deltree(tree, DNAME);
1911 talloc_free(mem_ctx);
1912
1913 return ret;
1914 }
1915
1916 static bool check_metadata(struct torture_context *tctx,
1917 struct smb2_tree *tree,
1918 const char *path,
1919 struct smb2_handle _h,
1920 NTTIME expected_btime,
1921 uint32_t expected_attribs)
1922 {
1923 struct smb2_handle h = _h;
1924 union smb_fileinfo getinfo;
1925 NTSTATUS status;
1926 bool ret = true;
1927
1928 if (smb2_util_handle_empty(h)) {
1929 struct smb2_create c;
1930
1931 c = (struct smb2_create) {
1932 .in.desired_access = SEC_FILE_ALL,
1933 .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
1934 .in.file_attributes = FILE_ATTRIBUTE_HIDDEN,
1935 .in.create_disposition = NTCREATEX_DISP_OPEN,
1936 .in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION,
1937 .in.fname = path,
1938 };
1939 status = smb2_create(tree, tctx, &c);
1940 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1941 "smb2_create failed\n");
1942
1943 h = c.out.file.handle;
1944 }
1945
1946 getinfo = (union smb_fileinfo) {
1947 .generic.level = SMB_QFILEINFO_BASIC_INFORMATION,
1948 .generic.in.file.handle = h,
1949 };
1950
1951 status = smb2_getinfo_file(tree, tctx, &getinfo);
1952 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1953 "smb2_getinfo_file failed\n");
1954
1955 torture_assert_u64_equal_goto(tctx,
1956 expected_btime,
1957 getinfo.basic_info.out.create_time,
1958 ret, done,
1959 "btime was updated\n");
1960
1961 torture_assert_u32_equal_goto(tctx,
1962 expected_attribs,
1963 getinfo.basic_info.out.attrib,
1964 ret, done,
1965 "btime was updated\n");
1966
1967 done:
1968 if (smb2_util_handle_empty(_h)) {
1969 smb2_util_close(tree, h);
1970 }
1971
1972 return ret;
1973 }
1974
1975 static bool test_stream_attributes2(struct torture_context *tctx,
1976 struct smb2_tree *tree)
1977 {
1978 NTSTATUS status;
1979 struct smb2_create c1;
1980 struct smb2_handle h1 = {{0}};
1981 const char *fname = DNAME "\\test_stream_btime";
1982 const char *sname = DNAME "\\test_stream_btime:stream";
1983 union smb_fileinfo getinfo;
1984 union smb_setfileinfo setinfo;
1985 const char *data = "test data";
1986 struct timespec ts;
1987 NTTIME btime;
1988 uint32_t attrib = FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_ARCHIVE;
1989 bool ret;
1990
1991 smb2_deltree(tree, DNAME);
1992
1993 status = torture_smb2_testdir(tree, DNAME, &h1);
1994 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1995 "torture_smb2_testdir failed\n");
1996 smb2_util_close(tree, h1);
1997
1998 torture_comment(tctx, "Let's dance!\n");
1999
2000 /*
2001 * Step 1: create file and get creation date
2002 */
2003
2004 c1 = (struct smb2_create) {
2005 .in.desired_access = SEC_FILE_ALL,
2006 .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
2007 .in.file_attributes = FILE_ATTRIBUTE_HIDDEN,
2008 .in.create_disposition = NTCREATEX_DISP_CREATE,
2009 .in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION,
2010 .in.fname = fname,
2011 };
2012 status = smb2_create(tree, tctx, &c1);
2013 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2014 "smb2_create failed\n");
2015 h1 = c1.out.file.handle;
2016
2017 getinfo = (union smb_fileinfo) {
2018 .generic.level = SMB_QFILEINFO_BASIC_INFORMATION,
2019 .generic.in.file.handle = h1,
2020 };
2021 status = smb2_getinfo_file(tree, tctx, &getinfo);
2022 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2023 "smb2_getinfo_file failed\n");
2024
2025 btime = getinfo.basic_info.out.create_time;
2026
2027 status = smb2_util_close(tree, h1);
2028 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2029 "smb2_util_close failed\n");
2030 ZERO_STRUCT(h1);
2031
2032 /*
2033 * Step X: write to file, assert btime was not updated
2034 */
2035
2036 c1 = (struct smb2_create) {
2037 .in.desired_access = SEC_FILE_ALL,
2038 .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
2039 .in.file_attributes = attrib,
2040 .in.create_disposition = NTCREATEX_DISP_OPEN,
2041 .in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION,
2042 .in.fname = fname,
2043 };
2044 status = smb2_create(tree, tctx, &c1);
2045 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2046 "smb2_create failed\n");
2047 h1 = c1.out.file.handle;
2048
2049 status = smb2_util_write(tree, h1, data, 0, strlen(data));
2050 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2051 "smb2_util_write failed\n");
2052
2053 ret = check_metadata(tctx, tree, NULL, h1, btime, attrib);
2054 torture_assert_goto(tctx, ret, ret, done, "Bad metadata\n");
2055
2056 status = smb2_util_close(tree, h1);
2057 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2058 "smb2_util_close failed\n");
2059 ZERO_STRUCT(h1);
2060
2061 ret = check_metadata(tctx, tree, fname, (struct smb2_handle){{0}},
2062 btime, attrib);
2063 torture_assert_goto(tctx, ret, ret, done, "Bad metadata\n");
2064
2065 /*
2066 * Step X: create stream, assert creation date is the same
2067 * as the one on the basefile
2068 */
2069
2070 c1 = (struct smb2_create) {
2071 .in.desired_access = SEC_FILE_ALL,
2072 .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
2073 .in.file_attributes = attrib,
2074 .in.create_disposition = NTCREATEX_DISP_CREATE,
2075 .in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION,
2076 .in.fname = sname,
2077 };
2078 status = smb2_create(tree, tctx, &c1);
2079 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2080 "smb2_create failed\n");
2081 h1 = c1.out.file.handle;
2082
2083 status = smb2_util_close(tree, h1);
2084 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2085 "smb2_util_close failed\n");
2086 ZERO_STRUCT(h1);
2087
2088 ret = check_metadata(tctx, tree, sname, (struct smb2_handle){{0}},
2089 btime, attrib);
2090 torture_assert_goto(tctx, ret, ret, done, "Bad metadata\n");
2091
2092 /*
2093 * Step X: set btime on stream, verify basefile has the same btime.
2094 */
2095
2096 c1 = (struct smb2_create) {
2097 .in.desired_access = SEC_FILE_ALL,
2098 .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
2099 .in.file_attributes = attrib,
2100 .in.create_disposition = NTCREATEX_DISP_OPEN,
2101 .in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION,
2102 .in.fname = sname,
2103 };
2104 status = smb2_create(tree, tctx, &c1);
2105 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2106 "smb2_create failed\n");
2107 h1 = c1.out.file.handle;
2108
2109 setinfo = (union smb_setfileinfo) {
2110 .basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION,
2111 .basic_info.in.file.handle = h1,
2112 };
2113 clock_gettime_mono(&ts);
2114 btime = setinfo.basic_info.in.create_time = full_timespec_to_nt_time(&ts);
2115
2116 status = smb2_setinfo_file(tree, &setinfo);
2117 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2118 "smb2_setinfo_file failed\n");
2119
2120 ret = check_metadata(tctx, tree, NULL, h1, btime, attrib);
2121 torture_assert_goto(tctx, ret, ret, done, "Bad time on stream\n");
2122
2123 status = smb2_util_close(tree, h1);
2124 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2125 "smb2_util_close failed\n");
2126 ZERO_STRUCT(h1);
2127
2128 ret = check_metadata(tctx, tree, fname, (struct smb2_handle){{0}},
2129 btime, attrib);
2130 torture_assert_goto(tctx, ret, ret, done, "Bad time on basefile\n");
2131
2132 ret = check_metadata(tctx, tree, sname, (struct smb2_handle){{0}},
2133 btime, attrib);
2134 torture_assert_goto(tctx, ret, ret, done, "Bad time on stream\n");
2135
2136 /*
2137 * Step X: write to stream, assert btime was not updated
2138 */
2139
2140 c1 = (struct smb2_create) {
2141 .in.desired_access = SEC_FILE_ALL,
2142 .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
2143 .in.file_attributes = attrib,
2144 .in.create_disposition = NTCREATEX_DISP_OPEN,
2145 .in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION,
2146 .in.fname = sname,
2147 };
2148 status = smb2_create(tree, tctx, &c1);
2149 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2150 "smb2_create failed\n");
2151 h1 = c1.out.file.handle;
2152
2153 status = smb2_util_write(tree, h1, data, 0, strlen(data));
2154 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2155 "smb2_util_write failed\n");
2156
2157 ret = check_metadata(tctx, tree, NULL, h1, btime, attrib);
2158 torture_assert_goto(tctx, ret, ret, done, "Bad metadata\n");
2159
2160 status = smb2_util_close(tree, h1);
2161 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2162 "smb2_util_close failed\n");
2163 ZERO_STRUCT(h1);
2164
2165 ret = check_metadata(tctx, tree, fname, (struct smb2_handle){{0}},
2166 btime, attrib);
2167 torture_assert_goto(tctx, ret, ret, done, "Bad metadata\n");
2168
2169 ret = check_metadata(tctx, tree, sname, (struct smb2_handle){{0}},
2170 btime, attrib);
2171 torture_assert_goto(tctx, ret, ret, done, "Bad metadata\n");
2172
2173 /*
2174 * Step X: modify attributes via stream, verify it's "also" set on the
2175 * basefile.
2176 */
2177
2178 c1 = (struct smb2_create) {
2179 .in.desired_access = SEC_FILE_ALL,
2180 .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
2181 .in.file_attributes = attrib,
2182 .in.create_disposition = NTCREATEX_DISP_OPEN,
2183 .in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION,
2184 .in.fname = sname,
2185 };
2186 status = smb2_create(tree, tctx, &c1);
2187 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2188 "smb2_create failed\n");
2189 h1 = c1.out.file.handle;
2190
2191 attrib = FILE_ATTRIBUTE_NORMAL;
2192
2193 setinfo = (union smb_setfileinfo) {
2194 .basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION,
2195 .basic_info.in.file.handle = h1,
2196 .basic_info.in.attrib = attrib,
2197 };
2198
2199 status = smb2_setinfo_file(tree, &setinfo);
2200 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2201 "smb2_setinfo_file failed\n");
2202
2203 status = smb2_util_close(tree, h1);
2204 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2205 "smb2_util_close failed\n");
2206 ZERO_STRUCT(h1);
2207
2208 ret = check_metadata(tctx, tree, fname, (struct smb2_handle){{0}},
2209 btime, attrib);
2210 torture_assert_goto(tctx, ret, ret, done, "Bad metadata\n");
2211
2212 ret = check_metadata(tctx, tree, sname, (struct smb2_handle){{0}},
2213 btime, attrib);
2214 torture_assert_goto(tctx, ret, ret, done, "Bad metadata\n");
2215
2216 /*
2217 * Step X: modify attributes via basefile, verify it's "also" set on the
2218 * stream.
2219 */
2220
2221 c1 = (struct smb2_create) {
2222 .in.desired_access = SEC_FILE_ALL,
2223 .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
2224 .in.file_attributes = attrib,
2225 .in.create_disposition = NTCREATEX_DISP_OPEN,
2226 .in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION,
2227 .in.fname = fname,
2228 };
2229 status = smb2_create(tree, tctx, &c1);
2230 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2231 "smb2_create failed\n");
2232 h1 = c1.out.file.handle;
2233
2234 attrib = FILE_ATTRIBUTE_HIDDEN;
2235
2236 setinfo = (union smb_setfileinfo) {
2237 .basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION,
2238 .basic_info.in.file.handle = h1,
2239 .basic_info.in.attrib = attrib,
2240 };
2241
2242 status = smb2_setinfo_file(tree, &setinfo);
2243 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2244 "smb2_setinfo_file failed\n");
2245
2246 status = smb2_util_close(tree, h1);
2247 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2248 "smb2_util_close failed\n");
2249 ZERO_STRUCT(h1);
2250
2251 ret = check_metadata(tctx, tree, fname, (struct smb2_handle){{0}},
2252 btime, attrib);
2253 torture_assert_goto(tctx, ret, ret, done, "Bad metadata\n");
2254
2255 ret = check_metadata(tctx, tree, sname, (struct smb2_handle){{0}},
2256 btime, attrib);
2257 torture_assert_goto(tctx, ret, ret, done, "Bad metadata\n");
2258
2259 done:
2260 if (!smb2_util_handle_empty(h1)) {
2261 smb2_util_close(tree, h1);
2262 }
2263
2264 smb2_deltree(tree, DNAME);
2265
2266 return ret;
2267 }
2268
2269 static bool test_basefile_rename_with_open_stream(struct torture_context *tctx,
2270 struct smb2_tree *tree)
2271 {
2272 bool ret = true;
2273 NTSTATUS status;
2274 struct smb2_tree *tree2 = NULL;
2275 struct smb2_create create, create2;
2276 struct smb2_handle h1 = {{0}}, h2 = {{0}};
2277 const char *fname = "test_rename_openfile";
2278 const char *sname = "test_rename_openfile:foo";
2279 const char *fname_renamed = "test_rename_openfile_renamed";
2280 union smb_setfileinfo sinfo;
2281 const char *data = "test data";
2282
2283 ret = torture_smb2_connection(tctx, &tree2);
2284 torture_assert_goto(tctx, ret == true, ret, done,
2285 "torture_smb2_connection failed\n");
2286
2287 torture_comment(tctx, "Creating file with stream\n");
2288
2289 ZERO_STRUCT(create);
2290 create.in.desired_access = SEC_FILE_ALL;
2291 create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
2292 create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
2293 create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
2294 create.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
2295 create.in.fname = sname;
2296
2297 status = smb2_create(tree, tctx, &create);
2298 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2299 "smb2_create failed\n");
2300
2301 h1 = create.out.file.handle;
2302
2303 torture_comment(tctx, "Writing to stream\n");
2304
2305 status = smb2_util_write(tree, h1, data, 0, strlen(data));
2306 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2307 "smb2_util_write failed\n");
2308
2309 torture_comment(tctx, "Renaming base file\n");
2310
2311 ZERO_STRUCT(create2);
2312 create2.in.desired_access = SEC_FILE_ALL;
2313 create2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
2314 create2.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
2315 create2.in.create_disposition = NTCREATEX_DISP_OPEN;
2316 create2.in.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
2317 create2.in.fname = fname;
2318
2319 status = smb2_create(tree2, tctx, &create2);
2320 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2321 "smb2_create failed\n");
2322
2323 h2 = create2.out.file.handle;
2324
2325 ZERO_STRUCT(sinfo);
2326 sinfo.rename_information.level = RAW_SFILEINFO_RENAME_INFORMATION;
2327 sinfo.rename_information.in.file.handle = h2;
2328 sinfo.rename_information.in.new_name = fname_renamed;
2329
2330 status = smb2_setinfo_file(tree2, &sinfo);
2331 torture_assert_ntstatus_equal_goto(
2332 tctx, status, NT_STATUS_ACCESS_DENIED, ret, done,
2333 "smb2_setinfo_file didn't return NT_STATUS_ACCESS_DENIED\n");
2334
2335 smb2_util_close(tree2, h2);
2336
2337 done:
2338 if (!smb2_util_handle_empty(h1)) {
2339 smb2_util_close(tree, h1);
2340 }
2341 if (!smb2_util_handle_empty(h2)) {
2342 smb2_util_close(tree2, h2);
2343 }
2344 smb2_util_unlink(tree, fname);
2345 smb2_util_unlink(tree, fname_renamed);
2346
2347 return ret;
2348 }
2349
2350 /*
2351 * Simple test creating a stream on a share with "inherit permissions"
2352 * enabled. This tests specifically bug 15695.
2353 */
2354 bool test_stream_inherit_perms(struct torture_context *tctx,
2355 struct smb2_tree *tree)
2356 {
2357 NTSTATUS status;
2358 struct smb2_handle h = {};
2359 union smb_fileinfo q = {};
2360 union smb_setfileinfo setinfo = {};
2361 struct security_descriptor *sd = NULL;
2362 struct security_ace ace = {};
2363 const char *fname = DNAME "\\test_stream_inherit_perms:stream";
2364 bool ret = true;
2365
2366 smb2_deltree(tree, DNAME);
2367
2368 status = torture_smb2_testdir(tree, DNAME, &h);
2369 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2370 "torture_smb2_testdir failed\n");
2371
2372 torture_comment(tctx, "getting original sd\n");
2373
2374 q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
2375 q.query_secdesc.in.file.handle = h;
2376 q.query_secdesc.in.secinfo_flags = SECINFO_DACL | SECINFO_OWNER;
2377
2378 status = smb2_getinfo_file(tree, tctx, &q);
2379 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2380 "smb2_getinfo_file failed\n");
2381
2382 sd = q.query_secdesc.out.sd;
2383
2384 /*
2385 * Add one explicit non-inheriting ACE which will be stored
2386 * as a non-inheriting POSIX ACE. These are the ACEs that
2387 * "inherit permissions" will want to inherit.
2388 */
2389 ace.type = SEC_ACE_TYPE_ACCESS_ALLOWED;
2390 ace.access_mask = SEC_STD_ALL;
2391 ace.trustee = *(sd->owner_sid);
2392
2393 status = security_descriptor_dacl_add(sd, &ace);
2394 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2395 "security_descriptor_dacl_add failed\n");
2396
2397 setinfo.set_secdesc.level = RAW_SFILEINFO_SEC_DESC;
2398 setinfo.set_secdesc.in.file.handle = h;
2399 setinfo.set_secdesc.in.secinfo_flags = SECINFO_DACL;
2400 setinfo.set_secdesc.in.sd = sd;
2401
2402 status = smb2_setinfo_file(tree, &setinfo);
2403 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2404 "smb2_setinfo_file failed");
2405
2406 smb2_util_close(tree, h);
2407 ZERO_STRUCT(h);
2408
2409 /* This triggers the crash */
2410 status = torture_smb2_testfile(tree, fname, &h);
2411 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2412 "torture_smb2_testfile failed");
2413
2414 done:
2415 if (!smb2_util_handle_empty(h)) {
2416 smb2_util_close(tree, h);
2417 }
2418 smb2_deltree(tree, DNAME);
2419 return ret;
2420 }
2421
2422 /*
2423 basic testing of streams calls SMB2
2424 */
2425 struct torture_suite *torture_smb2_streams_init(TALLOC_CTX *ctx)
2426 {
2427 struct torture_suite *suite =
2428 torture_suite_create(ctx, "streams");
2429
2430 torture_suite_add_1smb2_test(suite, "dir", test_stream_dir);
2431 torture_suite_add_1smb2_test(suite, "io", test_stream_io);
2432 torture_suite_add_1smb2_test(suite, "sharemodes", test_stream_sharemodes);
2433 torture_suite_add_1smb2_test(suite, "names", test_stream_names);
2434 torture_suite_add_1smb2_test(suite, "names2", test_stream_names2);
2435 torture_suite_add_1smb2_test(suite, "names3", test_stream_names3);
2436 torture_suite_add_1smb2_test(suite, "rename", test_stream_rename);
2437 torture_suite_add_1smb2_test(suite, "rename2", test_stream_rename2);
2438 torture_suite_add_1smb2_test(suite, "create-disposition", test_stream_create_disposition);
2439 torture_suite_add_1smb2_test(suite, "attributes1", test_stream_attributes1);
2440 torture_suite_add_1smb2_test(suite, "attributes2", test_stream_attributes2);
2441 torture_suite_add_1smb2_test(suite, "delete", test_stream_delete);
2442 torture_suite_add_1smb2_test(suite, "zero-byte", test_zero_byte_stream);
2443 torture_suite_add_1smb2_test(suite, "basefile-rename-with-open-stream",
2444 test_basefile_rename_with_open_stream);
2445
2446 suite->description = talloc_strdup(suite, "SMB2-STREAM tests");
2447 return suite;
2448 }