Debian Codegraph @

source4/torture/basic/base.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
4 Copyright (C) Andrew Tridgell 1997-2003
5 Copyright (C) Jelmer Vernooij 2006
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "torture/smbtorture.h"
23 #include "torture/basic/proto.h"
24 #include "libcli/libcli.h"
25 #include "libcli/raw/raw_proto.h"
26 #include "torture/util.h"
27 #include "system/filesys.h"
28 #include "system/time.h"
29 #include "libcli/resolve/resolve.h"
30 #include "lib/events/events.h"
31 #include "param/param.h"
32
33
34 #define CHECK_MAX_FAILURES(label) do { if (++failures >= torture_failures) goto label; } while (0)
35
36
37 static struct smbcli_state *open_nbt_connection(struct torture_context *tctx)
38 {
39 struct nbt_name called, calling;
40 struct smbcli_state *cli;
41 const char *host = torture_setting_string(tctx, "host", NULL);
42 struct smbcli_options options;
43 bool ok;
44
45 make_nbt_name_client(&calling, lpcfg_netbios_name(tctx->lp_ctx));
46
47 nbt_choose_called_name(NULL, &called, host, NBT_NAME_SERVER);
48
49 cli = smbcli_state_init(NULL);
50 if (!cli) {
51 torture_result(tctx, TORTURE_FAIL, "Failed initialize smbcli_struct to connect with %s\n", host);
52 goto failed;
53 }
54
55 lpcfg_smbcli_options(tctx->lp_ctx, &options);
56
57 ok = smbcli_socket_connect(cli, host,
58 tctx->ev,
59 tctx->lp_ctx,
60 lpcfg_resolve_context(tctx->lp_ctx),
61 &options,
62 lpcfg_socket_options(tctx->lp_ctx),
63 &calling, &called);
64 if (!ok) {
65 torture_result(tctx, TORTURE_FAIL, "Failed to connect with %s\n", host);
66 goto failed;
67 }
68
69 cli->transport = smbcli_transport_init(cli->sock, cli,
70 true, &cli->options);
71 cli->sock = NULL;
72 if (!cli->transport) {
73 torture_result(tctx, TORTURE_FAIL, "smbcli_transport_init failed\n");
74 goto failed;
75 }
76
77 return cli;
78
79 failed:
80 talloc_free(cli);
81 return NULL;
82 }
83
84 static bool tcon_devtest(struct torture_context *tctx,
85 struct smbcli_state *cli,
86 const char *myshare, const char *devtype,
87 NTSTATUS expected_error)
88 {
89 bool status;
90 const char *password = torture_setting_string(tctx, "password", NULL);
91
92 status = NT_STATUS_IS_OK(smbcli_tconX(cli, myshare, devtype,
93 password));
94
95 torture_comment(tctx, "Trying share %s with devtype %s\n", myshare, devtype);
96
97 if (NT_STATUS_IS_OK(expected_error)) {
98 if (!status) {
99 torture_fail(tctx, talloc_asprintf(tctx,
100 "tconX to share %s with type %s "
101 "should have succeeded but failed",
102 myshare, devtype));
103 }
104 smbcli_tdis(cli);
105 } else {
106 if (status) {
107 torture_fail(tctx, talloc_asprintf(tctx,
108 "tconx to share %s with type %s "
109 "should have failed but succeeded",
110 myshare, devtype));
111 } else {
112 if (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),
113 expected_error)) {
114 } else {
115 torture_fail(tctx, "Returned unexpected error");
116 }
117 }
118 }
119 return true;
120 }
121
122
123
124 /**
125 test whether fnums and tids open on one VC are available on another (a major
126 security hole)
127 */
128 static bool run_fdpasstest(struct torture_context *tctx,
129 struct smbcli_state *cli1,
130 struct smbcli_state *cli2)
131 {
132 const char *fname = "\\fdpass.tst";
133 int fnum1, oldtid;
134 uint8_t buf[1024];
135
136 smbcli_unlink(cli1->tree, fname);
137
138 torture_comment(tctx, "Opening a file on connection 1\n");
139
140 fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
141 torture_assert(tctx, fnum1 != -1,
142 talloc_asprintf(tctx,
143 "open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree)));
144
145 torture_comment(tctx, "writing to file on connection 1\n");
146
147 torture_assert(tctx,
148 smbcli_write(cli1->tree, fnum1, 0, "hello world\n", 0, 13) == 13,
149 talloc_asprintf(tctx,
150 "write failed (%s)\n", smbcli_errstr(cli1->tree)));
151
152 oldtid = cli2->tree->tid;
153 cli2->session->vuid = cli1->session->vuid;
154 cli2->tree->tid = cli1->tree->tid;
155 cli2->session->pid = cli1->session->pid;
156
157 torture_comment(tctx, "reading from file on connection 2\n");
158
159 torture_assert(tctx, smbcli_read(cli2->tree, fnum1, buf, 0, 13) != 13,
160 talloc_asprintf(tctx,
161 "read succeeded! nasty security hole [%s]\n", buf));
162
163 smbcli_close(cli1->tree, fnum1);
164 smbcli_unlink(cli1->tree, fname);
165
166 cli2->tree->tid = oldtid;
167
168 return true;
169 }
170
171 /**
172 This checks how the getatr calls works
173 */
174 static bool run_attrtest(struct torture_context *tctx,
175 struct smbcli_state *cli)
176 {
177 int fnum;
178 time_t t, t2;
179 const char *fname = "\\attrib123456789.tst";
180 bool correct = true;
181
182 smbcli_unlink(cli->tree, fname);
183 fnum = smbcli_open(cli->tree, fname,
184 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
185 smbcli_close(cli->tree, fnum);
186
187 if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, fname, NULL, NULL, &t))) {
188 torture_result(tctx, TORTURE_FAIL, "getatr failed (%s)\n", smbcli_errstr(cli->tree));
189 correct = false;
190 }
191
192 torture_comment(tctx, "New file time is %s", ctime(&t));
193
194 if (labs(t - time(NULL)) > 60*60*24*10) {
195 torture_result(tctx, TORTURE_FAIL, "ERROR: SMBgetatr bug. time is %s",
196 ctime(&t));
197 t = time(NULL);
198 correct = false;
199 }
200
201 t2 = t-60*60*24; /* 1 day ago */
202
203 torture_comment(tctx, "Setting file time to %s", ctime(&t2));
204
205 if (NT_STATUS_IS_ERR(smbcli_setatr(cli->tree, fname, 0, t2))) {
206 torture_comment(tctx, "setatr failed (%s)\n", smbcli_errstr(cli->tree));
207 correct = true;
208 }
209
210 if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, fname, NULL, NULL, &t))) {
211 torture_comment(tctx, "getatr failed (%s)\n", smbcli_errstr(cli->tree));
212 correct = true;
213 }
214
215 torture_comment(tctx, "Retrieved file time as %s", ctime(&t));
216
217 if (t != t2) {
218 torture_comment(tctx, "ERROR: getatr/setatr bug. times are\n%s",
219 ctime(&t));
220 torture_comment(tctx, "%s", ctime(&t2));
221 correct = true;
222 }
223
224 smbcli_unlink(cli->tree, fname);
225
226 return correct;
227 }
228
229 /**
230 This checks a couple of trans2 calls
231 */
232 static bool run_trans2test(struct torture_context *tctx,
233 struct smbcli_state *cli)
234 {
235 int fnum;
236 size_t size;
237 time_t c_time, a_time, m_time, w_time, m_time2;
238 const char *fname = "\\trans2.tst";
239 const char *dname = "\\trans2";
240 const char *fname2 = "\\trans2\\trans2.tst";
241 const char *pname;
242 bool correct = true;
243
244 smbcli_unlink(cli->tree, fname);
245
246 torture_comment(tctx, "Testing qfileinfo\n");
247
248 fnum = smbcli_open(cli->tree, fname,
249 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
250 if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &size, &c_time, &a_time, &m_time,
251 NULL, NULL))) {
252 torture_result(tctx, TORTURE_FAIL, "ERROR: qfileinfo failed (%s)\n", smbcli_errstr(cli->tree));
253 correct = false;
254 }
255
256 torture_comment(tctx, "Testing NAME_INFO\n");
257
258 if (NT_STATUS_IS_ERR(smbcli_qfilename(cli->tree, fnum, &pname))) {
259 torture_result(tctx, TORTURE_FAIL, "ERROR: qfilename failed (%s)\n", smbcli_errstr(cli->tree));
260 correct = false;
261 }
262
263 if (!pname || strcmp(pname, fname)) {
264 torture_result(tctx, TORTURE_FAIL, "qfilename gave different name? [%s] [%s]\n",
265 fname, pname);
266 correct = false;
267 }
268
269 smbcli_close(cli->tree, fnum);
270 smbcli_unlink(cli->tree, fname);
271
272 fnum = smbcli_open(cli->tree, fname,
273 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
274 if (fnum == -1) {
275 torture_result(tctx, TORTURE_FAIL, "open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
276 return false;
277 }
278 smbcli_close(cli->tree, fnum);
279
280 torture_comment(tctx, "Checking for sticky create times\n");
281
282 if (NT_STATUS_IS_ERR(smbcli_qpathinfo(cli->tree, fname, &c_time, &a_time, &m_time, &size, NULL))) {
283 torture_result(tctx, TORTURE_FAIL, "ERROR: qpathinfo failed (%s)\n", smbcli_errstr(cli->tree));
284 correct = false;
285 } else {
286 time_t t = time(NULL);
287
288 if (c_time != m_time) {
289 torture_comment(tctx, "create time=%s", ctime(&c_time));
290 torture_comment(tctx, "modify time=%s", ctime(&m_time));
291 torture_comment(tctx, "This system appears to have sticky create times\n");
292 }
293 if ((labs(a_time - t) > 60) && (a_time % (60*60) == 0)) {
294 torture_comment(tctx, "access time=%s", ctime(&a_time));
295 torture_result(tctx, TORTURE_FAIL, "This system appears to set a midnight access time\n");
296 correct = false;
297 }
298
299 if (labs(m_time - t) > 60*60*24*7) {
300 torture_result(tctx, TORTURE_FAIL, "ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
301 correct = false;
302 }
303 }
304
305
306 smbcli_unlink(cli->tree, fname);
307 fnum = smbcli_open(cli->tree, fname,
308 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
309 smbcli_close(cli->tree, fnum);
310 if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, fname, &c_time, &a_time, &m_time, &w_time, &size, NULL, NULL))) {
311 torture_result(tctx, TORTURE_FAIL, "ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
312 correct = false;
313 } else {
314 if (w_time < 60*60*24*2) {
315 torture_comment(tctx, "write time=%s", ctime(&w_time));
316 torture_result(tctx, TORTURE_FAIL, "This system appears to set a initial 0 write time\n");
317 correct = false;
318 }
319 }
320
321 smbcli_unlink(cli->tree, fname);
322
323
324 /* check if the server updates the directory modification time
325 when creating a new file */
326 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, dname))) {
327 torture_result(tctx, TORTURE_FAIL, "ERROR: mkdir failed (%s)\n", smbcli_errstr(cli->tree));
328 correct = false;
329 }
330 sleep(3);
331 if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\trans2\\", &c_time, &a_time, &m_time, &w_time, &size, NULL, NULL))) {
332 torture_result(tctx, TORTURE_FAIL, "ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
333 correct = false;
334 }
335
336 fnum = smbcli_open(cli->tree, fname2,
337 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
338 smbcli_write(cli->tree, fnum, 0, &fnum, 0, sizeof(fnum));
339 smbcli_close(cli->tree, fnum);
340 if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\trans2\\", &c_time, &a_time, &m_time2, &w_time, &size, NULL, NULL))) {
341 torture_result(tctx, TORTURE_FAIL, "ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
342 correct = false;
343 } else {
344 if (m_time2 == m_time) {
345 torture_result(tctx, TORTURE_FAIL, "This system does not update directory modification times\n");
346 correct = false;
347 }
348 }
349 smbcli_unlink(cli->tree, fname2);
350 smbcli_rmdir(cli->tree, dname);
351
352 return correct;
353 }
354
355 /* send smb negprot commands, not reading the response */
356 static bool run_negprot_nowait(struct torture_context *tctx)
357 {
358 int i;
359 struct smbcli_state *cli, *cli2;
360 bool correct = true;
361
362 torture_comment(tctx, "starting negprot nowait test\n");
363
364 cli = open_nbt_connection(tctx);
365 if (!cli) {
366 return false;
367 }
368
369 torture_comment(tctx, "Filling send buffer\n");
370
371 for (i=0;i<100;i++) {
372 struct tevent_req *req;
373 req = smb_raw_negotiate_send(cli, tctx->ev,
374 cli->transport,
375 PROTOCOL_CORE,
376 PROTOCOL_NT1);
377 tevent_loop_once(tctx->ev);
378 if (!tevent_req_is_in_progress(req)) {
379 NTSTATUS status;
380
381 status = smb_raw_negotiate_recv(req);
382 TALLOC_FREE(req);
383 if (i > 0) {
384 torture_comment(tctx, "Failed to fill pipe packet[%d] - %s (ignored)\n",
385 i+1, nt_errstr(status));
386 break;
387 } else {
388 torture_result(tctx, TORTURE_FAIL, "Failed to fill pipe - %s \n",
389 nt_errstr(status));
390 torture_close_connection(cli);
391 return false;
392 }
393 }
394 }
395
396 torture_comment(tctx, "Opening secondary connection\n");
397 if (!torture_open_connection(&cli2, tctx, 1)) {
398 torture_result(tctx, TORTURE_FAIL, "Failed to open secondary connection\n");
399 correct = false;
400 }
401
402 if (!torture_close_connection(cli2)) {
403 torture_result(tctx, TORTURE_FAIL, "Failed to close secondary connection\n");
404 correct = false;
405 }
406
407 torture_close_connection(cli);
408
409 return correct;
410 }
411
412 /**
413 this checks to see if a secondary tconx can use open files from an
414 earlier tconx
415 */
416 static bool run_tcon_test(struct torture_context *tctx, struct smbcli_state *cli)
417 {
418 const char *fname = "\\tcontest.tmp";
419 int fnum1;
420 uint16_t cnum1, cnum2, cnum3;
421 uint16_t vuid1, vuid2;
422 uint8_t buf[4];
423 bool ret = true;
424 struct smbcli_tree *tree1;
425 const char *host = torture_setting_string(tctx, "host", NULL);
426 const char *share = torture_setting_string(tctx, "share", NULL);
427 const char *password = torture_setting_string(tctx, "password", NULL);
428
429 if (smbcli_deltree(cli->tree, fname) == -1) {
430 torture_comment(tctx, "unlink of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
431 }
432
433 fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
434 if (fnum1 == -1) {
435 torture_result(tctx, TORTURE_FAIL, "open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
436 return false;
437 }
438
439 cnum1 = cli->tree->tid;
440 vuid1 = cli->session->vuid;
441
442 memset(buf, 0, 4); /* init buf so valgrind won't complain */
443 if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) != 4) {
444 torture_result(tctx, TORTURE_FAIL, "initial write failed (%s)\n", smbcli_errstr(cli->tree));
445 return false;
446 }
447
448 tree1 = cli->tree; /* save old tree connection */
449 if (NT_STATUS_IS_ERR(smbcli_tconX(cli, share, "?????", password))) {
450 torture_result(tctx, TORTURE_FAIL, "%s refused 2nd tree connect (%s)\n", host,
451 smbcli_errstr(cli->tree));
452 return false;
453 }
454
455 cnum2 = cli->tree->tid;
456 cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
457 vuid2 = cli->session->vuid + 1;
458
459 /* try a write with the wrong tid */
460 cli->tree->tid = cnum2;
461
462 if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
463 torture_result(tctx, TORTURE_FAIL, "* server allows write with wrong TID\n");
464 ret = false;
465 } else {
466 torture_comment(tctx, "server fails write with wrong TID : %s\n", smbcli_errstr(cli->tree));
467 }
468
469
470 /* try a write with an invalid tid */
471 cli->tree->tid = cnum3;
472
473 if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
474 torture_result(tctx, TORTURE_FAIL, "* server allows write with invalid TID\n");
475 ret = false;
476 } else {
477 torture_comment(tctx, "server fails write with invalid TID : %s\n", smbcli_errstr(cli->tree));
478 }
479
480 /* try a write with an invalid vuid */
481 cli->session->vuid = vuid2;
482 cli->tree->tid = cnum1;
483
484 if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
485 torture_result(tctx, TORTURE_FAIL, "* server allows write with invalid VUID\n");
486 ret = false;
487 } else {
488 torture_comment(tctx, "server fails write with invalid VUID : %s\n", smbcli_errstr(cli->tree));
489 }
490
491 cli->session->vuid = vuid1;
492 cli->tree->tid = cnum1;
493
494 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum1))) {
495 torture_result(tctx, TORTURE_FAIL, "close failed (%s)\n", smbcli_errstr(cli->tree));
496 return false;
497 }
498
499 cli->tree->tid = cnum2;
500
501 if (NT_STATUS_IS_ERR(smbcli_tdis(cli))) {
502 torture_result(tctx, TORTURE_FAIL, "secondary tdis failed (%s)\n", smbcli_errstr(cli->tree));
503 return false;
504 }
505
506 cli->tree = tree1; /* restore initial tree */
507 cli->tree->tid = cnum1;
508
509 smbcli_unlink(tree1, fname);
510
511 return ret;
512 }
513
514 /**
515 checks for correct tconX support
516 */
517 static bool run_tcon_devtype_test(struct torture_context *tctx,
518 struct smbcli_state *cli1)
519 {
520 const char *share = torture_setting_string(tctx, "share", NULL);
521
522 if (!tcon_devtest(tctx, cli1, "IPC$", "A:", NT_STATUS_BAD_DEVICE_TYPE))
523 return false;
524
525 if (!tcon_devtest(tctx, cli1, "IPC$", "?????", NT_STATUS_OK))
526 return false;
527
528 if (!tcon_devtest(tctx, cli1, "IPC$", "LPT:", NT_STATUS_BAD_DEVICE_TYPE))
529 return false;
530
531 if (!tcon_devtest(tctx, cli1, "IPC$", "IPC", NT_STATUS_OK))
532 return false;
533
534 if (!tcon_devtest(tctx, cli1, "IPC$", "FOOBA", NT_STATUS_BAD_DEVICE_TYPE))
535 return false;
536
537 if (!tcon_devtest(tctx, cli1, share, "A:", NT_STATUS_OK))
538 return false;
539
540 if (!tcon_devtest(tctx, cli1, share, "?????", NT_STATUS_OK))
541 return false;
542
543 if (!tcon_devtest(tctx, cli1, share, "LPT:", NT_STATUS_BAD_DEVICE_TYPE))
544 return false;
545
546 if (!tcon_devtest(tctx, cli1, share, "IPC", NT_STATUS_BAD_DEVICE_TYPE))
547 return false;
548
549 if (!tcon_devtest(tctx, cli1, share, "FOOBA", NT_STATUS_BAD_DEVICE_TYPE))
550 return false;
551
552 return true;
553 }
554
555 static bool rw_torture2(struct torture_context *tctx,
556 struct smbcli_state *c1, struct smbcli_state *c2)
557 {
558 const char *lockfname = "\\torture2.lck";
559 int fnum1;
560 int fnum2;
561 int i;
562 uint8_t buf[131072];
563 uint8_t buf_rd[131072];
564 bool correct = true;
565 ssize_t bytes_read, bytes_written;
566
567 torture_assert(tctx, smbcli_deltree(c1->tree, lockfname) != -1,
568 talloc_asprintf(tctx,
569 "unlink failed (%s)", smbcli_errstr(c1->tree)));
570
571 fnum1 = smbcli_open(c1->tree, lockfname, O_RDWR | O_CREAT | O_EXCL,
572 DENY_NONE);
573 torture_assert(tctx, fnum1 != -1,
574 talloc_asprintf(tctx,
575 "first open read/write of %s failed (%s)",
576 lockfname, smbcli_errstr(c1->tree)));
577 fnum2 = smbcli_open(c2->tree, lockfname, O_RDONLY,
578 DENY_NONE);
579 torture_assert(tctx, fnum2 != -1,
580 talloc_asprintf(tctx,
581 "second open read-only of %s failed (%s)",
582 lockfname, smbcli_errstr(c2->tree)));
583
584 torture_comment(tctx, "Checking data integrity over %d ops\n",
585 torture_numops);
586
587 for (i=0;i<torture_numops;i++)
588 {
589 size_t buf_size = ((unsigned int)random()%(sizeof(buf)-1))+ 1;
590 if (i % 10 == 0) {
591 if (torture_setting_bool(tctx, "progress", true)) {
592 torture_comment(tctx, "%d\r", i); fflush(stdout);
593 }
594 }
595
596 generate_random_buffer(buf, buf_size);
597
598 if ((bytes_written = smbcli_write(c1->tree, fnum1, 0, buf, 0, buf_size)) != buf_size) {
599 torture_comment(tctx, "write failed (%s)\n", smbcli_errstr(c1->tree));
600 torture_result(tctx, TORTURE_FAIL, "wrote %d, expected %d\n", (int)bytes_written, (int)buf_size);
601 correct = false;
602 break;
603 }
604
605 if ((bytes_read = smbcli_read(c2->tree, fnum2, buf_rd, 0, buf_size)) != buf_size) {
606 torture_comment(tctx, "read failed (%s)\n", smbcli_errstr(c2->tree));
607 torture_result(tctx, TORTURE_FAIL, "read %d, expected %d\n", (int)bytes_read, (int)buf_size);
608 correct = false;
609 break;
610 }
611
612 torture_assert_mem_equal(tctx, buf_rd, buf, buf_size,
613 "read/write compare failed\n");
614 }
615
616 torture_assert_ntstatus_ok(tctx, smbcli_close(c2->tree, fnum2),
617 talloc_asprintf(tctx, "close failed (%s)", smbcli_errstr(c2->tree)));
618 torture_assert_ntstatus_ok(tctx, smbcli_close(c1->tree, fnum1),
619 talloc_asprintf(tctx, "close failed (%s)", smbcli_errstr(c1->tree)));
620
621 torture_assert_ntstatus_ok(tctx, smbcli_unlink(c1->tree, lockfname),
622 talloc_asprintf(tctx, "unlink failed (%s)", smbcli_errstr(c1->tree)));
623
624 torture_comment(tctx, "\n");
625
626 return correct;
627 }
628
629
630
631 static bool run_readwritetest(struct torture_context *tctx,
632 struct smbcli_state *cli1,
633 struct smbcli_state *cli2)
634 {
635 torture_comment(tctx, "Running readwritetest v1\n");
636 if (!rw_torture2(tctx, cli1, cli2))
637 return false;
638
639 torture_comment(tctx, "Running readwritetest v2\n");
640
641 if (!rw_torture2(tctx, cli1, cli1))
642 return false;
643
644 return true;
645 }
646
647 /*
648 test the timing of deferred open requests
649 */
650 static bool run_deferopen(struct torture_context *tctx, struct smbcli_state *cli, int dummy)
651 {
652 const char *fname = "\\defer_open_test.dat";
653 int i = 0;
654 bool correct = true;
655 int nsec;
656 int msec;
657 double sec;
658 NTSTATUS status;
659
660 nsec = torture_setting_int(tctx, "sharedelay", 1000000);
661 msec = nsec / 1000;
662 sec = ((double)nsec) / ((double) 1000000);
663
664 torture_comment(tctx, "pid %u: Testing deferred open requests.\n",
665 (unsigned)getpid());
666
667 while (i < 4) {
668 int fnum = -1;
669 int j = 1;
670
671 do {
672 struct timeval tv;
673 tv = timeval_current();
674
675 torture_comment(tctx,
676 "pid %u: create[%d,%d]...\n",
677 (unsigned)getpid(), i, j);
678
679 fnum = smbcli_nt_create_full(cli->tree, fname, 0,
680 SEC_RIGHTS_FILE_ALL,
681 FILE_ATTRIBUTE_NORMAL,
682 NTCREATEX_SHARE_ACCESS_NONE,
683 NTCREATEX_DISP_OPEN_IF, 0, 0);
684 status = smbcli_nt_error(cli->tree);
685
686 torture_comment(tctx,
687 "pid %u: create[%d,%d] gave fnum %d, status %s\n",
688 (unsigned)getpid(), i, j, fnum,
689 nt_errstr(status));
690
691 if (fnum != -1) {
692 break;
693 }
694
695 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
696 double e = timeval_elapsed(&tv);
697
698 torture_comment(tctx, "pid %u: create[%d,%d] "
699 "time elapsed: %.2f (1 sec = %.2f)\n",
700 (unsigned)getpid(), i, j, e, sec);
701 if (e < (0.5 * sec) || e > ((1.5 * sec) + 1.5)) {
702 torture_comment(tctx, "pid %u: create[%d,%d] "
703 "timing incorrect\n",
704 (unsigned)getpid(), i, j);
705 torture_result(tctx, TORTURE_FAIL, "Timing incorrect %.2f violation 1 sec == %.2f\n",
706 e, sec);
707 return false;
708 }
709 }
710
711 j++;
712
713 } while (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
714
715 torture_comment(tctx,
716 "pid %u: create loop %d done: fnum %d, status %s\n",
717 (unsigned)getpid(), i, fnum, nt_errstr(status));
718
719 torture_assert(tctx, fnum != -1,
720 talloc_asprintf(tctx,
721 "pid %u: Failed to open %s, error=%s\n",
722 (unsigned)getpid(), fname,
723 smbcli_errstr(cli->tree)));
724
725 torture_comment(tctx, "pid %u: open %d\n", (unsigned)getpid(), i);
726
727 smb_msleep(10 * msec);
728
729 status = smbcli_close(cli->tree, fnum);
730
731 torture_comment(tctx, "pid %u: open %d closed, status %s\n",
732 (unsigned)getpid(), i, nt_errstr(status));
733
734 torture_assert(tctx, !NT_STATUS_IS_ERR(status),
735 talloc_asprintf(tctx,
736 "pid %u: Failed to close %s, "
737 "error=%s\n",
738 (unsigned)getpid(), fname,
739 smbcli_errstr(cli->tree)));
740
741 smb_msleep(2 * msec);
742
743 i++;
744 }
745
746 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
747 /* All until the last unlink will fail with sharing violation
748 but also the last request can fail since the file could have
749 been successfully deleted by another (test) process */
750 status = smbcli_nt_error(cli->tree);
751 if ((!NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION))
752 && (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND))) {
753 torture_result(tctx, TORTURE_FAIL, "unlink of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
754 correct = false;
755 }
756 }
757
758 torture_comment(tctx, "pid %u: deferred test finished\n",
759 (unsigned)getpid());
760 return correct;
761 }
762
763 /**
764 Try with a wrong vuid and check error message.
765 */
766
767 static bool run_vuidtest(struct torture_context *tctx,
768 struct smbcli_state *cli)
769 {
770 const char *fname = "\\vuid.tst";
771 int fnum;
772 size_t size;
773 time_t c_time, a_time, m_time;
774
775 NTSTATUS result;
776
777 smbcli_unlink(cli->tree, fname);
778
779 fnum = smbcli_open(cli->tree, fname,
780 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
781
782 cli->session->vuid += 1234;
783
784 torture_comment(tctx, "Testing qfileinfo with wrong vuid\n");
785
786 if (NT_STATUS_IS_OK(result = smbcli_qfileinfo(cli->tree, fnum, NULL,
787 &size, &c_time, &a_time,
788 &m_time, NULL, NULL))) {
789 torture_fail(tctx, "qfileinfo passed with wrong vuid");
790 }
791
792 if (!NT_STATUS_EQUAL(cli->transport->error.e.nt_status,
793 NT_STATUS_DOS(ERRSRV, ERRbaduid)) &&
794 !NT_STATUS_EQUAL(cli->transport->error.e.nt_status,
795 NT_STATUS_INVALID_HANDLE)) {
796 torture_fail(tctx, talloc_asprintf(tctx,
797 "qfileinfo should have returned DOS error "
798 "ERRSRV:ERRbaduid\n but returned %s",
799 smbcli_errstr(cli->tree)));
800 }
801
802 cli->session->vuid -= 1234;
803
804 torture_assert_ntstatus_ok(tctx, smbcli_close(cli->tree, fnum),
805 talloc_asprintf(tctx, "close failed (%s)", smbcli_errstr(cli->tree)));
806
807 smbcli_unlink(cli->tree, fname);
808
809 return true;
810 }
811
812 /*
813 Test open mode returns on read-only files.
814 */
815 static bool run_opentest(struct torture_context *tctx, struct smbcli_state *cli1,
816 struct smbcli_state *cli2)
817 {
818 const char *fname = "\\readonly.file";
819 char *control_char_fname;
820 int fnum1, fnum2;
821 uint8_t buf[20];
822 size_t fsize;
823 bool correct = true;
824 char *tmp_path;
825 int failures = 0;
826 int i;
827
828 control_char_fname = talloc_strdup(tctx, "\\readonly.afile");
829 torture_assert_not_null(tctx, control_char_fname, "asprintf failed\n");
830
831 for (i = 1; i <= 0x1f; i++) {
832 control_char_fname[10] = i;
833 fnum1 = smbcli_nt_create_full(cli1->tree, control_char_fname, 0, SEC_FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL,
834 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
835
836 if (!check_error(__location__, cli1, ERRDOS, ERRinvalidname,
837 NT_STATUS_OBJECT_NAME_INVALID)) {
838 torture_result(tctx, TORTURE_FAIL, "Error code should be NT_STATUS_OBJECT_NAME_INVALID, was %s for file with %d char\n",
839 smbcli_errstr(cli1->tree), i);
840 failures++;
841 }
842
843 if (fnum1 != -1) {
844 smbcli_close(cli1->tree, fnum1);
845 }
846 smbcli_setatr(cli1->tree, control_char_fname, 0, 0);
847 smbcli_unlink(cli1->tree, control_char_fname);
848 }
849 TALLOC_FREE(control_char_fname);
850
851 if (!failures)
852 torture_comment(tctx, "Create file with control char names passed.\n");
853
854 smbcli_setatr(cli1->tree, fname, 0, 0);
855 smbcli_unlink(cli1->tree, fname);
856
857 fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
858 if (fnum1 == -1) {
859 torture_result(tctx, TORTURE_FAIL, "open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
860 return false;
861 }
862
863 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
864 torture_result(tctx, TORTURE_FAIL, "close2 failed (%s)\n", smbcli_errstr(cli1->tree));
865 return false;
866 }
867
868 if (NT_STATUS_IS_ERR(smbcli_setatr(cli1->tree, fname, FILE_ATTRIBUTE_READONLY, 0))) {
869 torture_result(tctx, TORTURE_FAIL,
870 __location__ ": smbcli_setatr failed (%s)\n", smbcli_errstr(cli1->tree));
871 CHECK_MAX_FAILURES(error_test1);
872 return false;
873 }
874
875 fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
876 if (fnum1 == -1) {
877 torture_result(tctx, TORTURE_FAIL,
878 __location__ ": open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
879 CHECK_MAX_FAILURES(error_test1);
880 return false;
881 }
882
883 /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
884 fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
885
886 if (check_error(__location__, cli1, ERRDOS, ERRnoaccess,
887 NT_STATUS_ACCESS_DENIED)) {
888 torture_comment(tctx, "correct error code ERRDOS/ERRnoaccess returned\n");
889 }
890
891 torture_comment(tctx, "finished open test 1\n");
892
893 error_test1:
894 smbcli_close(cli1->tree, fnum1);
895
896 /* Now try not readonly and ensure ERRbadshare is returned. */
897
898 smbcli_setatr(cli1->tree, fname, 0, 0);
899
900 fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
901 if (fnum1 == -1) {
902 torture_result(tctx, TORTURE_FAIL, "open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
903 return false;
904 }
905
906 /* This will fail - but the error should be ERRshare. */
907 fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
908
909 if (check_error(__location__, cli1, ERRDOS, ERRbadshare,
910 NT_STATUS_SHARING_VIOLATION)) {
911 torture_comment(tctx, "correct error code ERRDOS/ERRbadshare returned\n");
912 }
913
914 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
915 torture_result(tctx, TORTURE_FAIL, "close2 failed (%s)\n", smbcli_errstr(cli1->tree));
916 return false;
917 }
918
919 smbcli_unlink(cli1->tree, fname);
920
921 torture_comment(tctx, "finished open test 2\n");
922
923 /* Test truncate open disposition on file opened for read. */
924
925 fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
926 if (fnum1 == -1) {
927 torture_result(tctx, TORTURE_FAIL, "(3) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
928 return false;
929 }
930
931 /* write 20 bytes. */
932
933 memset(buf, '\0', 20);
934
935 if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
936 torture_result(tctx, TORTURE_FAIL, "write failed (%s)\n", smbcli_errstr(cli1->tree));
937 correct = false;
938 }
939
940 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
941 torture_result(tctx, TORTURE_FAIL, "(3) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
942 return false;
943 }
944
945 /* Ensure size == 20. */
946 if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
947 torture_result(tctx, TORTURE_FAIL,
948 __location__ ": (3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
949 CHECK_MAX_FAILURES(error_test3);
950 return false;
951 }
952
953 if (fsize != 20) {
954 torture_result(tctx, TORTURE_FAIL,
955 __location__ ": (3) file size != 20\n");
956 CHECK_MAX_FAILURES(error_test3);
957 return false;
958 }
959
960 /* Now test if we can truncate a file opened for readonly. */
961
962 fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY|O_TRUNC, DENY_NONE);
963 if (fnum1 == -1) {
964 torture_result(tctx, TORTURE_FAIL,
965 __location__ ": (3) open (2) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
966 CHECK_MAX_FAILURES(error_test3);
967 return false;
968 }
969
970 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
971 torture_result(tctx, TORTURE_FAIL,
972 __location__ ": close2 failed (%s)\n", smbcli_errstr(cli1->tree));
973 return false;
974 }
975
976 /* Ensure size == 0. */
977 if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
978 torture_result(tctx, TORTURE_FAIL,
979 __location__ ": (3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
980 CHECK_MAX_FAILURES(error_test3);
981 return false;
982 }
983
984 if (fsize != 0) {
985 torture_result(tctx, TORTURE_FAIL,
986 __location__ ": (3) file size != 0\n");
987 CHECK_MAX_FAILURES(error_test3);
988 return false;
989 }
990 torture_comment(tctx, "finished open test 3\n");
991 error_test3:
992
993 fnum1 = fnum2 = -1;
994 smbcli_unlink(cli1->tree, fname);
995
996
997 torture_comment(tctx, "Testing ctemp\n");
998 fnum1 = smbcli_ctemp(cli1->tree, "\\", &tmp_path);
999 if (fnum1 == -1) {
1000 torture_result(tctx, TORTURE_FAIL,
1001 __location__ ": ctemp failed (%s)\n", smbcli_errstr(cli1->tree));
1002 CHECK_MAX_FAILURES(error_test4);
1003 return false;
1004 }
1005 torture_comment(tctx, "ctemp gave path %s\n", tmp_path);
1006
1007 error_test4:
1008 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1009 torture_comment(tctx, "close of temp failed (%s)\n", smbcli_errstr(cli1->tree));
1010 }
1011 if (NT_STATUS_IS_ERR(smbcli_unlink(cli1->tree, tmp_path))) {
1012 torture_comment(tctx, "unlink of temp failed (%s)\n", smbcli_errstr(cli1->tree));
1013 }
1014
1015 /* Test the non-io opens... */
1016
1017 torture_comment(tctx, "Test #1 testing 2 non-io opens (no delete)\n");
1018 fnum1 = fnum2 = -1;
1019 smbcli_setatr(cli2->tree, fname, 0, 0);
1020 smbcli_unlink(cli2->tree, fname);
1021
1022 fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1023 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1024
1025 if (fnum1 == -1) {
1026 torture_result(tctx, TORTURE_FAIL,
1027 __location__ ": Test 1 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1028 CHECK_MAX_FAILURES(error_test10);
1029 return false;
1030 }
1031
1032 fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1033 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1034 if (fnum2 == -1) {
1035 torture_result(tctx, TORTURE_FAIL,
1036 __location__ ": Test 1 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1037 CHECK_MAX_FAILURES(error_test10);
1038 return false;
1039 }
1040
1041 torture_comment(tctx, "non-io open test #1 passed.\n");
1042 error_test10:
1043
1044 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1045 torture_comment(tctx, "Test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1046 }
1047 if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1048 torture_comment(tctx, "Test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1049 }
1050
1051 torture_comment(tctx, "Test #2 testing 2 non-io opens (first with delete)\n");
1052 fnum1 = fnum2 = -1;
1053 smbcli_unlink(cli1->tree, fname);
1054
1055 fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1056 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1057
1058 if (fnum1 == -1) {
1059 torture_result(tctx, TORTURE_FAIL,
1060 __location__ ": Test 2 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1061 CHECK_MAX_FAILURES(error_test20);
1062 return false;
1063 }
1064
1065 fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1066 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1067
1068 if (fnum2 == -1) {
1069 torture_result(tctx, TORTURE_FAIL,
1070 __location__ ": Test 2 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1071 CHECK_MAX_FAILURES(error_test20);
1072 return false;
1073 }
1074
1075 torture_comment(tctx, "non-io open test #2 passed.\n");
1076 error_test20:
1077
1078 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1079 torture_comment(tctx, "Test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1080 }
1081 if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1082 torture_comment(tctx, "Test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1083 }
1084
1085 fnum1 = fnum2 = -1;
1086 smbcli_unlink(cli1->tree, fname);
1087
1088 torture_comment(tctx, "Test #3 testing 2 non-io opens (second with delete)\n");
1089
1090 fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1091 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1092
1093 if (fnum1 == -1) {
1094 torture_result(tctx, TORTURE_FAIL,
1095 __location__ ": Test 3 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1096 CHECK_MAX_FAILURES(error_test30);
1097 return false;
1098 }
1099
1100 fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1101 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1102
1103 if (fnum2 == -1) {
1104 torture_result(tctx, TORTURE_FAIL,
1105 __location__ ": Test 3 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1106 CHECK_MAX_FAILURES(error_test30);
1107 return false;
1108 }
1109
1110 torture_comment(tctx, "non-io open test #3 passed.\n");
1111 error_test30:
1112
1113 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1114 torture_comment(tctx, "Test 3 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1115 }
1116 if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1117 torture_comment(tctx, "Test 3 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1118 }
1119
1120 torture_comment(tctx, "Test #4 testing 2 non-io opens (both with delete)\n");
1121 fnum1 = fnum2 = -1;
1122 smbcli_unlink(cli1->tree, fname);
1123
1124 fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1125 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1126
1127 if (fnum1 == -1) {
1128 torture_result(tctx, TORTURE_FAIL,
1129 __location__ ": Test 4 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1130 CHECK_MAX_FAILURES(error_test40);
1131 return false;
1132 }
1133
1134 fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1135 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1136
1137 if (fnum2 != -1) {
1138 torture_result(tctx, TORTURE_FAIL,
1139 __location__ ": Test 4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1140 CHECK_MAX_FAILURES(error_test40);
1141 return false;
1142 }
1143
1144 torture_comment(tctx, "Test 4 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1145
1146 torture_comment(tctx, "non-io open test #4 passed.\n");
1147 error_test40:
1148
1149 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1150 torture_comment(tctx, "Test 4 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1151 }
1152 if (fnum2 != -1 && NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1153 torture_comment(tctx, "Test 4 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1154 }
1155
1156 torture_comment(tctx, "Test #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
1157 fnum1 = fnum2 = -1;
1158 smbcli_unlink(cli1->tree, fname);
1159
1160 fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1161 NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1162
1163 if (fnum1 == -1) {
1164 torture_result(tctx, TORTURE_FAIL,
1165 __location__ ": Test 5 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1166 CHECK_MAX_FAILURES(error_test50);
1167 return false;
1168 }
1169
1170 fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1171 NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1172
1173 if (fnum2 == -1) {
1174 torture_result(tctx, TORTURE_FAIL,
1175 __location__ ": Test 5 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1176 CHECK_MAX_FAILURES(error_test50);
1177 return false;
1178 }
1179
1180 torture_comment(tctx, "non-io open test #5 passed.\n");
1181 error_test50:
1182
1183 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1184 torture_comment(tctx, "Test 5 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1185 }
1186
1187 if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1188 torture_comment(tctx, "Test 5 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1189 }
1190
1191 torture_comment(tctx, "Test #6 testing 1 non-io open, one io open\n");
1192 fnum1 = fnum2 = -1;
1193 smbcli_unlink(cli1->tree, fname);
1194
1195 fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1196 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1197
1198 if (fnum1 == -1) {
1199 torture_result(tctx, TORTURE_FAIL,
1200 __location__ ": Test 6 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1201 CHECK_MAX_FAILURES(error_test60);
1202 return false;
1203 }
1204
1205 fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1206 NTCREATEX_SHARE_ACCESS_READ, NTCREATEX_DISP_OPEN_IF, 0, 0);
1207
1208 if (fnum2 == -1) {
1209 torture_result(tctx, TORTURE_FAIL,
1210 __location__ ": Test 6 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1211 CHECK_MAX_FAILURES(error_test60);
1212 return false;
1213 }
1214
1215 torture_comment(tctx, "non-io open test #6 passed.\n");
1216 error_test60:
1217
1218 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1219 torture_comment(tctx, "Test 6 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1220 }
1221
1222 if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1223 torture_comment(tctx, "Test 6 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1224 }
1225
1226 torture_comment(tctx, "Test #7 testing 1 non-io open, one io open with delete\n");
1227 fnum1 = fnum2 = -1;
1228 smbcli_unlink(cli1->tree, fname);
1229
1230 fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1231 NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1232
1233 if (fnum1 == -1) {
1234 torture_result(tctx, TORTURE_FAIL,
1235 __location__ ": Test 7 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1236 CHECK_MAX_FAILURES(error_test70);
1237 return false;
1238 }
1239
1240 fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1241 NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1242
1243 if (fnum2 != -1) {
1244 torture_result(tctx, TORTURE_FAIL,
1245 __location__ ": Test 7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1246 CHECK_MAX_FAILURES(error_test70);
1247 return false;
1248 }
1249
1250 torture_comment(tctx, "Test 7 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1251
1252 torture_comment(tctx, "non-io open test #7 passed.\n");
1253 error_test70:
1254
1255 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1256 torture_comment(tctx, "Test 7 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1257 }
1258 if (fnum2 != -1 && NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1259 torture_comment(tctx, "Test 7 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1260 }
1261
1262 torture_comment(tctx, "Test #8 testing one normal open, followed by lock, followed by open with truncate\n");
1263 fnum1 = fnum2 = -1;
1264 smbcli_unlink(cli1->tree, fname);
1265
1266 fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1267 if (fnum1 == -1) {
1268 torture_result(tctx, TORTURE_FAIL, "(8) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1269 return false;
1270 }
1271
1272 /* write 20 bytes. */
1273
1274 memset(buf, '\0', 20);
1275
1276 if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
1277 torture_result(tctx, TORTURE_FAIL, "(8) write failed (%s)\n", smbcli_errstr(cli1->tree));
1278 correct = false;
1279 }
1280
1281 /* Ensure size == 20. */
1282 if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1283 torture_result(tctx, TORTURE_FAIL,
1284 __location__ ": (8) getatr (1) failed (%s)\n", smbcli_errstr(cli1->tree));
1285 CHECK_MAX_FAILURES(error_test80);
1286 return false;
1287 }
1288
1289 if (fsize != 20) {
1290 torture_result(tctx, TORTURE_FAIL,
1291 __location__ ": (8) file size %lu != 20\n", (unsigned long)fsize);
1292 CHECK_MAX_FAILURES(error_test80);
1293 return false;
1294 }
1295
1296 /* Get an exclusive lock on the open file. */
1297 if (NT_STATUS_IS_ERR(smbcli_lock(cli1->tree, fnum1, 0, 4, 0, WRITE_LOCK))) {
1298 torture_result(tctx, TORTURE_FAIL,
1299 __location__ ": (8) lock1 failed (%s)\n", smbcli_errstr(cli1->tree));
1300 CHECK_MAX_FAILURES(error_test80);
1301 return false;
1302 }
1303
1304 fnum2 = smbcli_open(cli1->tree, fname, O_RDWR|O_TRUNC, DENY_NONE);
1305 if (fnum1 == -1) {
1306 torture_result(tctx, TORTURE_FAIL, "(8) open (2) of %s with truncate failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1307 return false;
1308 }
1309
1310 /* Ensure size == 0. */
1311 if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1312 torture_result(tctx, TORTURE_FAIL,
1313 __location__ ": (8) getatr (2) failed (%s)\n", smbcli_errstr(cli1->tree));
1314 CHECK_MAX_FAILURES(error_test80);
1315 return false;
1316 }
1317
1318 if (fsize != 0) {
1319 torture_result(tctx, TORTURE_FAIL,
1320 __location__ ": (8) file size %lu != 0\n", (unsigned long)fsize);
1321 CHECK_MAX_FAILURES(error_test80);
1322 return false;
1323 }
1324
1325 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1326 torture_result(tctx, TORTURE_FAIL, "(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
1327 return false;
1328 }
1329
1330 if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum2))) {
1331 torture_result(tctx, TORTURE_FAIL, "(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
1332 return false;
1333 }
1334
1335 error_test80:
1336
1337 torture_comment(tctx, "open test #8 passed.\n");
1338
1339 smbcli_unlink(cli1->tree, fname);
1340
1341 return failures > 0 ? false : correct;
1342 }
1343
1344 /* FIRST_DESIRED_ACCESS 0xf019f */
1345 #define FIRST_DESIRED_ACCESS SEC_FILE_READ_DATA|SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA|\
1346 SEC_FILE_READ_EA| /* 0xf */ \
1347 SEC_FILE_WRITE_EA|SEC_FILE_READ_ATTRIBUTE| /* 0x90 */ \
1348 SEC_FILE_WRITE_ATTRIBUTE| /* 0x100 */ \
1349 SEC_STD_DELETE|SEC_STD_READ_CONTROL|\
1350 SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER /* 0xf0000 */
1351 /* SECOND_DESIRED_ACCESS 0xe0080 */
1352 #define SECOND_DESIRED_ACCESS SEC_FILE_READ_ATTRIBUTE| /* 0x80 */ \
1353 SEC_STD_READ_CONTROL|SEC_STD_WRITE_DAC|\
1354 SEC_STD_WRITE_OWNER /* 0xe0000 */
1355
1356 #if 0
1357 #define THIRD_DESIRED_ACCESS FILE_READ_ATTRIBUTE| /* 0x80 */ \
1358 READ_CONTROL|WRITE_DAC|\
1359 SEC_FILE_READ_DATA|\
1360 WRITE_OWNER /* */
1361 #endif
1362
1363
1364
1365 /**
1366 Test ntcreate calls made by xcopy
1367 */
1368 static bool run_xcopy(struct torture_context *tctx,
1369 struct smbcli_state *cli1)
1370 {
1371 const char *fname = "\\test.txt";
1372 int fnum1, fnum2;
1373
1374 fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0,
1375 FIRST_DESIRED_ACCESS,
1376 FILE_ATTRIBUTE_ARCHIVE,
1377 NTCREATEX_SHARE_ACCESS_NONE,
1378 NTCREATEX_DISP_OVERWRITE_IF,
1379 0x4044, 0);
1380
1381 torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx,
1382 "First open failed - %s", smbcli_errstr(cli1->tree)));
1383
1384 fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0,
1385 SECOND_DESIRED_ACCESS, 0,
1386 NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN,
1387 0x200000, 0);
1388 torture_assert(tctx, fnum2 != -1, talloc_asprintf(tctx,
1389 "second open failed - %s", smbcli_errstr(cli1->tree)));
1390
1391 return true;
1392 }
1393
1394 static bool run_iometer(struct torture_context *tctx,
1395 struct smbcli_state *cli)
1396 {
1397 const char *fname = "\\iobw.tst";
1398 int fnum;
1399 size_t filesize;
1400 NTSTATUS status;
1401 char buf[2048];
1402 int ops;
1403
1404 memset(buf, 0, sizeof(buf));
1405
1406 status = smbcli_getatr(cli->tree, fname, NULL, &filesize, NULL);
1407 torture_assert_ntstatus_ok(tctx, status,
1408 talloc_asprintf(tctx, "smbcli_getatr failed: %s", nt_errstr(status)));
1409
1410 torture_comment(tctx, "size: %d\n", (int)filesize);
1411
1412 filesize -= (sizeof(buf) - 1);
1413
1414 fnum = smbcli_nt_create_full(cli->tree, fname, 0x16,
1415 0x2019f, 0, 0x3, 3, 0x42, 0x3);
1416 torture_assert(tctx, fnum != -1, talloc_asprintf(tctx, "open failed: %s",
1417 smbcli_errstr(cli->tree)));
1418
1419 ops = 0;
1420
1421 while (true) {
1422 int i, num_reads, num_writes;
1423
1424 num_reads = random() % 10;
1425 num_writes = random() % 3;
1426
1427 for (i=0; i<num_reads; i++) {
1428 ssize_t res;
1429 if (ops++ > torture_numops) {
1430 return true;
1431 }
1432 res = smbcli_read(cli->tree, fnum, buf,
1433 random() % filesize, sizeof(buf));
1434 torture_assert(tctx, res == sizeof(buf),
1435 talloc_asprintf(tctx, "read failed: %s",
1436 smbcli_errstr(cli->tree)));
1437 }
1438 for (i=0; i<num_writes; i++) {
1439 ssize_t res;
1440 if (ops++ > torture_numops) {
1441 return true;
1442 }
1443 res = smbcli_write(cli->tree, fnum, 0, buf,
1444 random() % filesize, sizeof(buf));
1445 torture_assert(tctx, res == sizeof(buf),
1446 talloc_asprintf(tctx, "read failed: %s",
1447 smbcli_errstr(cli->tree)));
1448 }
1449 }
1450 }
1451
1452 /**
1453 tries variants of chkpath
1454 */
1455 static bool torture_chkpath_test(struct torture_context *tctx,
1456 struct smbcli_state *cli)
1457 {
1458 int fnum;
1459 bool ret;
1460
1461 torture_comment(tctx, "Testing valid and invalid paths\n");
1462
1463 /* cleanup from an old run */
1464 smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
1465 smbcli_unlink_wcard(cli->tree, "\\chkpath.dir\\*");
1466 smbcli_rmdir(cli->tree, "\\chkpath.dir");
1467
1468 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir"))) {
1469 torture_result(tctx, TORTURE_FAIL, "mkdir1 failed : %s\n", smbcli_errstr(cli->tree));
1470 return false;
1471 }
1472
1473 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir\\dir2"))) {
1474 torture_result(tctx, TORTURE_FAIL, "mkdir2 failed : %s\n", smbcli_errstr(cli->tree));
1475 return false;
1476 }
1477
1478 fnum = smbcli_open(cli->tree, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1479 if (fnum == -1) {
1480 torture_result(tctx, TORTURE_FAIL, "open1 failed (%s)\n", smbcli_errstr(cli->tree));
1481 return false;
1482 }
1483 smbcli_close(cli->tree, fnum);
1484
1485 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir"))) {
1486 torture_result(tctx, TORTURE_FAIL, "chkpath1 failed: %s\n", smbcli_errstr(cli->tree));
1487 ret = false;
1488 }
1489
1490 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dir2"))) {
1491 torture_result(tctx, TORTURE_FAIL, "chkpath2 failed: %s\n", smbcli_errstr(cli->tree));
1492 ret = false;
1493 }
1494
1495 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\foo.txt"))) {
1496 ret = check_error(__location__, cli, ERRDOS, ERRbadpath,
1497 NT_STATUS_NOT_A_DIRECTORY);
1498 } else {
1499 torture_result(tctx, TORTURE_FAIL, "* chkpath on a file should fail\n");
1500 ret = false;
1501 }
1502
1503 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\bar.txt"))) {
1504 ret = check_error(__location__, cli, ERRDOS, ERRbadpath,
1505 NT_STATUS_OBJECT_NAME_NOT_FOUND);
1506 } else {
1507 torture_result(tctx, TORTURE_FAIL, "* chkpath on a non existent file should fail\n");
1508 ret = false;
1509 }
1510
1511 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dirxx\\bar.txt"))) {
1512 ret = check_error(__location__, cli, ERRDOS, ERRbadpath,
1513 NT_STATUS_OBJECT_PATH_NOT_FOUND);
1514 } else {
1515 torture_result(tctx, TORTURE_FAIL, "* chkpath on a non existent component should fail\n");
1516 ret = false;
1517 }
1518
1519 smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
1520 smbcli_unlink_wcard(cli->tree, "\\chkpath.dir\\*");
1521 smbcli_rmdir(cli->tree, "\\chkpath.dir");
1522
1523 return ret;
1524 }
1525
1526 /*
1527 * This is a test to exercise some weird Samba3 error paths.
1528 */
1529
1530 static bool torture_samba3_errorpaths(struct torture_context *tctx)
1531 {
1532 bool nt_status_support;
1533 bool client_ntlmv2_auth;
1534 struct smbcli_state *cli_nt = NULL, *cli_dos = NULL;
1535 bool result = false;
1536 int fnum;
1537 const char *os2_fname = ".+,;=[].";
1538 const char *dname = "samba3_errordir";
1539 union smb_open io;
1540 NTSTATUS status;
1541
1542 nt_status_support = lpcfg_nt_status_support(tctx->lp_ctx);
1543 client_ntlmv2_auth = lpcfg_client_ntlmv2_auth(tctx->lp_ctx);
1544
1545 if (!lpcfg_set_cmdline(tctx->lp_ctx, "nt status support", "yes")) {
1546 torture_result(tctx, TORTURE_FAIL, "Could not set 'nt status support = yes'\n");
1547 goto fail;
1548 }
1549 if (!lpcfg_set_cmdline(tctx->lp_ctx, "client ntlmv2 auth", "yes")) {
1550 torture_result(tctx, TORTURE_FAIL, "Could not set 'client ntlmv2 auth = yes'\n");
1551 goto fail;
1552 }
1553
1554 if (!torture_open_connection(&cli_nt, tctx, 0)) {
1555 goto fail;
1556 }
1557
1558 if (!lpcfg_set_cmdline(tctx->lp_ctx, "nt status support", "no")) {
1559 torture_result(tctx, TORTURE_FAIL, "Could not set 'nt status support = no'\n");
1560 goto fail;
1561 }
1562 if (!lpcfg_set_cmdline(tctx->lp_ctx, "client ntlmv2 auth", "no")) {
1563 torture_result(tctx, TORTURE_FAIL, "Could not set 'client ntlmv2 auth = no'\n");
1564 goto fail;
1565 }
1566
1567 if (!torture_open_connection(&cli_dos, tctx, 1)) {
1568 goto fail;
1569 }
1570
1571 if (!lpcfg_set_cmdline(tctx->lp_ctx, "nt status support",
1572 nt_status_support ? "yes":"no")) {
1573 torture_result(tctx, TORTURE_FAIL, "Could not reset 'nt status support'");
1574 goto fail;
1575 }
1576 if (!lpcfg_set_cmdline(tctx->lp_ctx, "client ntlmv2 auth",
1577 client_ntlmv2_auth ? "yes":"no")) {
1578 torture_result(tctx, TORTURE_FAIL, "Could not reset 'client ntlmv2 auth'");
1579 goto fail;
1580 }
1581
1582 smbcli_unlink(cli_nt->tree, os2_fname);
1583 smbcli_rmdir(cli_nt->tree, dname);
1584
1585 if (!NT_STATUS_IS_OK(smbcli_mkdir(cli_nt->tree, dname))) {
1586 torture_result(tctx, TORTURE_FAIL, "smbcli_mkdir(%s) failed: %s\n", dname,
1587 smbcli_errstr(cli_nt->tree));
1588 goto fail;
1589 }
1590
1591 io.generic.level = RAW_OPEN_NTCREATEX;
1592 io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
1593 io.ntcreatex.in.root_fid.fnum = 0;
1594 io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1595 io.ntcreatex.in.alloc_size = 1024*1024;
1596 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
1597 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
1598 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
1599 io.ntcreatex.in.create_options = 0;
1600 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1601 io.ntcreatex.in.security_flags = 0;
1602 io.ntcreatex.in.fname = dname;
1603
1604 status = smb_raw_open(cli_nt->tree, tctx, &io);
1605 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1606 torture_result(tctx, TORTURE_FAIL, "(%s) incorrect status %s should be %s\n",
1607 __location__, nt_errstr(status),
1608 nt_errstr(NT_STATUS_OBJECT_NAME_COLLISION));
1609 goto fail;
1610 }
1611 status = smb_raw_open(cli_dos->tree, tctx, &io);
1612 if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS, ERRfilexists))) {
1613 torture_result(tctx, TORTURE_FAIL, "(%s) incorrect status %s should be %s\n",
1614 __location__, nt_errstr(status),
1615 nt_errstr(NT_STATUS_DOS(ERRDOS, ERRfilexists)));
1616 goto fail;
1617 }
1618
1619 status = smbcli_mkdir(cli_nt->tree, dname);
1620 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1621 torture_result(tctx, TORTURE_FAIL, "(%s) incorrect status %s should be %s\n",
1622 __location__, nt_errstr(status),
1623 nt_errstr(NT_STATUS_OBJECT_NAME_COLLISION));
1624 goto fail;
1625 }
1626 status = smbcli_mkdir(cli_dos->tree, dname);
1627 if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS, ERRnoaccess))) {
1628 torture_result(tctx, TORTURE_FAIL, "(%s) incorrect status %s should be %s\n",
1629 __location__, nt_errstr(status),
1630 nt_errstr(NT_STATUS_DOS(ERRDOS, ERRnoaccess)));
1631 goto fail;
1632 }
1633
1634 {
1635 union smb_mkdir md;
1636 md.t2mkdir.level = RAW_MKDIR_T2MKDIR;
1637 md.t2mkdir.in.path = dname;
1638 md.t2mkdir.in.num_eas = 0;
1639 md.t2mkdir.in.eas = NULL;
1640
1641 status = smb_raw_mkdir(cli_nt->tree, &md);
1642 if (!NT_STATUS_EQUAL(status,
1643 NT_STATUS_OBJECT_NAME_COLLISION)) {
1644 torture_comment(
1645 tctx, "(%s) incorrect status %s should be "
1646 "NT_STATUS_OBJECT_NAME_COLLISION\n",
1647 __location__, nt_errstr(status));
1648 goto fail;
1649 }
1650 status = smb_raw_mkdir(cli_dos->tree, &md);
1651 if (!NT_STATUS_EQUAL(status,
1652 NT_STATUS_DOS(ERRDOS, ERRrename))) {
1653 torture_result(tctx, TORTURE_FAIL, "(%s) incorrect status %s "
1654 "should be ERRDOS:ERRrename\n",
1655 __location__, nt_errstr(status));
1656 goto fail;
1657 }
1658 }
1659
1660 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1661 status = smb_raw_open(cli_nt->tree, tctx, &io);
1662 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1663 torture_result(tctx, TORTURE_FAIL, "(%s) incorrect status %s should be %s\n",
1664 __location__, nt_errstr(status),
1665 nt_errstr(NT_STATUS_OBJECT_NAME_COLLISION));
1666 goto fail;
1667 }
1668
1669 status = smb_raw_open(cli_dos->tree, tctx, &io);
1670 if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS, ERRfilexists))) {
1671 torture_result(tctx, TORTURE_FAIL, "(%s) incorrect status %s should be %s\n",
1672 __location__, nt_errstr(status),
1673 nt_errstr(NT_STATUS_DOS(ERRDOS, ERRfilexists)));
1674 goto fail;
1675 }
1676
1677 {
1678 /* Test an invalid DOS deny mode */
1679 const char *fname = "test.txt";
1680
1681 fnum = smbcli_open(cli_nt->tree, fname, O_RDWR | O_CREAT, 5);
1682 if (fnum != -1) {
1683 torture_result(tctx, TORTURE_FAIL, "Open(%s) with invalid deny mode succeeded -- "
1684 "expected failure\n", fname);
1685 smbcli_close(cli_nt->tree, fnum);
1686 goto fail;
1687 }
1688 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_nt->tree),
1689 NT_STATUS_DOS(ERRDOS,ERRbadaccess))) {
1690 torture_result(tctx, TORTURE_FAIL, "Expected DOS error ERRDOS/ERRbadaccess, "
1691 "got %s\n", smbcli_errstr(cli_nt->tree));
1692 goto fail;
1693 }
1694
1695 fnum = smbcli_open(cli_dos->tree, fname, O_RDWR | O_CREAT, 5);
1696 if (fnum != -1) {
1697 torture_result(tctx, TORTURE_FAIL, "Open(%s) with invalid deny mode succeeded -- "
1698 "expected failure\n", fname);
1699 smbcli_close(cli_nt->tree, fnum);
1700 goto fail;
1701 }
1702 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_nt->tree),
1703 NT_STATUS_DOS(ERRDOS,ERRbadaccess))) {
1704 torture_result(tctx, TORTURE_FAIL, "Expected DOS error ERRDOS:ERRbadaccess, "
1705 "got %s\n", smbcli_errstr(cli_nt->tree));
1706 goto fail;
1707 }
1708 }
1709
1710 {
1711 /*
1712 * Samba 3.0.23 has a bug that an existing file can be opened
1713 * as a directory using ntcreate&x. Test this.
1714 */
1715
1716 const char *fname = "\\test_dir.txt";
1717
1718 fnum = smbcli_open(cli_nt->tree, fname, O_RDWR|O_CREAT,
1719 DENY_NONE);
1720 if (fnum == -1) {
1721 d_printf("(%s) smbcli_open failed: %s\n", __location__,
1722 smbcli_errstr(cli_nt->tree));
1723 }
1724 smbcli_close(cli_nt->tree, fnum);
1725
1726 io.generic.level = RAW_OPEN_NTCREATEX;
1727 io.ntcreatex.in.root_fid.fnum = 0;
1728 io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
1729 io.ntcreatex.in.alloc_size = 0;
1730 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
1731 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
1732 NTCREATEX_SHARE_ACCESS_WRITE|
1733 NTCREATEX_SHARE_ACCESS_DELETE;
1734 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1735 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1736 io.ntcreatex.in.impersonation =
1737 NTCREATEX_IMPERSONATION_ANONYMOUS;
1738 io.ntcreatex.in.security_flags = 0;
1739 io.ntcreatex.in.fname = fname;
1740 io.ntcreatex.in.flags = 0;
1741
1742 status = smb_raw_open(cli_nt->tree, tctx, &io);
1743 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_A_DIRECTORY)) {
1744 torture_result(tctx, TORTURE_FAIL, "ntcreate as dir gave %s, "
1745 "expected NT_STATUS_NOT_A_DIRECTORY\n",
1746 nt_errstr(status));
1747 result = false;
1748 }
1749
1750 if (NT_STATUS_IS_OK(status)) {
1751 smbcli_close(cli_nt->tree, io.ntcreatex.out.file.fnum);
1752 }
1753
1754 status = smb_raw_open(cli_dos->tree, tctx, &io);
1755 if (!NT_STATUS_EQUAL(status, NT_STATUS_DOS(ERRDOS,
1756 ERRbaddirectory))) {
1757 torture_result(tctx, TORTURE_FAIL, "ntcreate as dir gave %s, "
1758 "expected NT_STATUS_NOT_A_DIRECTORY\n",
1759 nt_errstr(status));
1760 result = false;
1761 }
1762
1763 if (NT_STATUS_IS_OK(status)) {
1764 smbcli_close(cli_dos->tree,
1765 io.ntcreatex.out.file.fnum);
1766 }
1767
1768 smbcli_unlink(cli_nt->tree, fname);
1769 }
1770
1771 if (!torture_setting_bool(tctx, "samba3", false)) {
1772 goto done;
1773 }
1774
1775 fnum = smbcli_open(cli_dos->tree, os2_fname,
1776 O_RDWR | O_CREAT | O_TRUNC,
1777 DENY_NONE);
1778 if (fnum != -1) {
1779 torture_result(tctx, TORTURE_FAIL, "Open(%s) succeeded -- expected failure\n",
1780 os2_fname);
1781 smbcli_close(cli_dos->tree, fnum);
1782 goto fail;
1783 }
1784
1785 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_dos->tree),
1786 NT_STATUS_DOS(ERRDOS, ERRcannotopen))) {
1787 torture_result(tctx, TORTURE_FAIL, "Expected DOS error ERRDOS/ERRcannotopen, got %s\n",
1788 smbcli_errstr(cli_dos->tree));
1789 goto fail;
1790 }
1791
1792 fnum = smbcli_open(cli_nt->tree, os2_fname,
1793 O_RDWR | O_CREAT | O_TRUNC,
1794 DENY_NONE);
1795 if (fnum != -1) {
1796 torture_result(tctx, TORTURE_FAIL, "Open(%s) succeeded -- expected failure\n",
1797 os2_fname);
1798 smbcli_close(cli_nt->tree, fnum);
1799 goto fail;
1800 }
1801
1802 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli_nt->tree),
1803 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1804 torture_result(tctx, TORTURE_FAIL, "Expected error NT_STATUS_OBJECT_NAME_NOT_FOUND, "
1805 "got %s\n", smbcli_errstr(cli_nt->tree));
1806 goto fail;
1807 }
1808
1809 done:
1810 result = true;
1811
1812 fail:
1813 if (cli_dos != NULL) {
1814 torture_close_connection(cli_dos);
1815 }
1816 if (cli_nt != NULL) {
1817 torture_close_connection(cli_nt);
1818 }
1819
1820 return result;
1821 }
1822
1823 /**
1824 This checks file/dir birthtime
1825 */
1826 static void list_fn(struct clilist_file_info *finfo, const char *name,
1827 void *state){
1828
1829 /* Just to change dir access time*/
1830 sleep(5);
1831
1832 }
1833
1834 static bool run_birthtimetest(struct torture_context *tctx,
1835 struct smbcli_state *cli)
1836 {
1837 int fnum;
1838 size_t size;
1839 time_t c_time, a_time, m_time, w_time, c_time1;
1840 const char *fname = "\\birthtime.tst";
1841 const char *dname = "\\birthtime";
1842 const char *fname2 = "\\birthtime\\birthtime.tst";
1843 bool correct = true;
1844 uint8_t buf[16];
1845
1846
1847 smbcli_unlink(cli->tree, fname);
1848
1849 torture_comment(tctx, "Testing Birthtime for File\n");
1850
1851 /* Save File birthtime/creationtime */
1852 fnum = smbcli_open(cli->tree, fname, O_RDWR | O_CREAT | O_TRUNC,
1853 DENY_NONE);
1854 if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &size,
1855 &c_time, &a_time, &m_time, NULL, NULL))) {
1856 torture_result(tctx, TORTURE_FAIL, "ERROR: qfileinfo failed (%s)\n",
1857 smbcli_errstr(cli->tree));
1858 correct = false;
1859 }
1860 smbcli_close(cli->tree, fnum);
1861
1862 sleep(10);
1863
1864 /* Change in File attribute changes file change time*/
1865 smbcli_setatr(cli->tree, fname, FILE_ATTRIBUTE_SYSTEM, 0);
1866
1867 fnum = smbcli_open(cli->tree, fname, O_RDWR | O_CREAT , DENY_NONE);
1868 /* Writing updates modification time*/
1869 smbcli_smbwrite(cli->tree, fnum, &fname, 0, sizeof(fname));
1870 /*Reading updates access time */
1871 smbcli_read(cli->tree, fnum, buf, 0, 13);
1872 smbcli_close(cli->tree, fnum);
1873
1874 if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, fname, &c_time1,
1875 &a_time, &m_time, &w_time, &size, NULL, NULL))) {
1876 torture_result(tctx, TORTURE_FAIL, "ERROR: qpathinfo2 failed (%s)\n",
1877 smbcli_errstr(cli->tree));
1878 correct = false;
1879 } else {
1880 fprintf(stdout, "c_time = %li, c_time1 = %li\n",
1881 (long) c_time, (long) c_time1);
1882 if (c_time1 != c_time) {
1883 torture_result(tctx, TORTURE_FAIL, "This system updated file \
1884 birth times! Not expected!\n");
1885 correct = false;
1886 }
1887 }
1888 smbcli_unlink(cli->tree, fname);
1889
1890 torture_comment(tctx, "Testing Birthtime for Directory\n");
1891
1892 /* check if the server does not update the directory birth time
1893 when creating a new file */
1894 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, dname))) {
1895 torture_result(tctx, TORTURE_FAIL, "ERROR: mkdir failed (%s)\n",
1896 smbcli_errstr(cli->tree));
1897 correct = false;
1898 }
1899 sleep(3);
1900 if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\birthtime\\",
1901 &c_time,&a_time,&m_time,&w_time, &size, NULL, NULL))){
1902 torture_result(tctx, TORTURE_FAIL, "ERROR: qpathinfo2 failed (%s)\n",
1903 smbcli_errstr(cli->tree));
1904 correct = false;
1905 }
1906
1907 /* Creating a new file changes dir modification time and change time*/
1908 smbcli_unlink(cli->tree, fname2);
1909 fnum = smbcli_open(cli->tree, fname2, O_RDWR | O_CREAT | O_TRUNC,
1910 DENY_NONE);
1911 smbcli_smbwrite(cli->tree, fnum, &fnum, 0, sizeof(fnum));
1912 smbcli_read(cli->tree, fnum, buf, 0, 13);
1913 smbcli_close(cli->tree, fnum);
1914
1915 /* dir listing changes dir access time*/
1916 smbcli_list(cli->tree, "\\birthtime\\*", 0, list_fn, cli );
1917
1918 if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\birthtime\\",
1919 &c_time1, &a_time, &m_time,&w_time,&size,NULL,NULL))){
1920 torture_result(tctx, TORTURE_FAIL, "ERROR: qpathinfo2 failed (%s)\n",
1921 smbcli_errstr(cli->tree));
1922 correct = false;
1923 } else {
1924 fprintf(stdout, "c_time = %li, c_time1 = %li\n",
1925 (long) c_time, (long) c_time1);
1926 if (c_time1 != c_time) {
1927 torture_result(tctx, TORTURE_FAIL, "This system updated directory \
1928 birth times! Not Expected!\n");
1929 correct = false;
1930 }
1931 }
1932 smbcli_unlink(cli->tree, fname2);
1933 smbcli_rmdir(cli->tree, dname);
1934
1935 return correct;
1936 }
1937
1938 /**
1939 SMB1 TWRP open on root of share.
1940 */
1941 static bool torture_smb1_twrp_openroot(struct torture_context *tctx,
1942 struct smbcli_state *cli)
1943 {
1944 const char *snapshot = NULL;
1945 const char *p = NULL;
1946 NTSTATUS status;
1947 struct tm tm = {};
1948 bool ret = true;
1949
1950 snapshot = torture_setting_string(tctx, "twrp_snapshot", NULL);
1951 if (snapshot == NULL) {
1952 torture_skip(tctx, "missing 'twrp_snapshot' option\n");
1953 }
1954
1955 torture_comment(tctx, "Testing open of root of "
1956 "share with timewarp (%s)\n",
1957 snapshot);
1958
1959 setenv("TZ", "GMT", 1);
1960
1961 p = strptime(snapshot, "@GMT-%Y.%m.%d-%H.%M.%S", &tm);
1962 torture_assert_goto(tctx, p != NULL, ret, done, "strptime\n");
1963 torture_assert_goto(tctx, *p == '\0', ret, done, "strptime\n");
1964
1965 cli->session->flags2 |= FLAGS2_REPARSE_PATH;
1966 status = smbcli_chkpath(cli->tree, snapshot);
1967 cli->session->flags2 &= ~FLAGS2_REPARSE_PATH;
1968
1969 if (NT_STATUS_IS_ERR(status)) {
1970 torture_result(tctx,
1971 TORTURE_FAIL,
1972 "smbcli_chkpath on %s : %s\n",
1973 snapshot,
1974 smbcli_errstr(cli->tree));
1975 return false;
1976 }
1977
1978 done:
1979
1980 return ret;
1981 }
1982
1983 static void torture_smb1_find_gmt_mask_list_fn(struct clilist_file_info *finfo,
1984 const char *name,
1985 void *state)
1986 {
1987 }
1988
1989 /**
1990 * SMB1 @GMT token as search mask is valid
1991 */
1992 static bool torture_smb1_find_gmt_mask(struct torture_context *tctx,
1993 struct smbcli_state *cli)
1994 {
1995 const char *dname = "\\torture_smb1_find_gmt_mask";
1996 const char *path = "\\torture_smb1_find_gmt_mask\\@GMT-2022.11.24-16.24.00";
1997 int fnum;
1998 int n;
1999 NTSTATUS status;
2000 bool ret = true;
2001
2002 smbcli_unlink(cli->tree, path);
2003 smbcli_rmdir(cli->tree, dname);
2004
2005 status = smbcli_mkdir(cli->tree, dname);
2006 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
2007 "smbcli_mkdir() failed\n");
2008 fnum = smbcli_open(cli->tree, path, O_RDWR | O_CREAT, DENY_NONE);
2009 smbcli_close(cli->tree, fnum);
2010
2011 /* Note: we don't set FLAGS2_REPARSE_PATH, so this is just a path */
2012 n = smbcli_list(cli->tree, path, 0, torture_smb1_find_gmt_mask_list_fn, cli);
2013 torture_assert_int_equal_goto(tctx, n, 1, ret, done, "Wrong count\n");
2014
2015 done:
2016 smbcli_unlink(cli->tree, path);
2017 smbcli_rmdir(cli->tree, dname);
2018 return ret;
2019 }
2020
2021 NTSTATUS torture_base_init(TALLOC_CTX *ctx)
2022 {
2023 struct torture_suite *suite = torture_suite_create(ctx, "base");
2024
2025 torture_suite_add_2smb_test(suite, "fdpass", run_fdpasstest);
2026 torture_suite_add_suite(suite, torture_base_locktest(suite));
2027 torture_suite_add_1smb_test(suite, "unlink", torture_unlinktest);
2028 torture_suite_add_1smb_test(suite, "attr", run_attrtest);
2029 torture_suite_add_1smb_test(suite, "trans2", run_trans2test);
2030 torture_suite_add_1smb_test(suite, "birthtime", run_birthtimetest);
2031 torture_suite_add_simple_test(suite, "negnowait", run_negprot_nowait);
2032 torture_suite_add_1smb_test(suite, "dir1", torture_dirtest1);
2033 torture_suite_add_1smb_test(suite, "dir2", torture_dirtest2);
2034 torture_suite_add_1smb_test(suite, "deny1", torture_denytest1);
2035 torture_suite_add_2smb_test(suite, "deny2", torture_denytest2);
2036 torture_suite_add_2smb_test(suite, "deny3", torture_denytest3);
2037 torture_suite_add_1smb_test(suite, "denydos", torture_denydos_sharing);
2038 torture_suite_add_smb_multi_test(suite, "ntdeny1", torture_ntdenytest1);
2039 torture_suite_add_2smb_test(suite, "ntdeny2", torture_ntdenytest2);
2040 torture_suite_add_1smb_test(suite, "tcon", run_tcon_test);
2041 torture_suite_add_1smb_test(suite, "tcondev", run_tcon_devtype_test);
2042 torture_suite_add_1smb_test(suite, "vuid", run_vuidtest);
2043 torture_suite_add_2smb_test(suite, "rw1", run_readwritetest);
2044 torture_suite_add_2smb_test(suite, "open", run_opentest);
2045 torture_suite_add_smb_multi_test(suite, "defer_open", run_deferopen);
2046 torture_suite_add_1smb_test(suite, "xcopy", run_xcopy);
2047 torture_suite_add_1smb_test(suite, "iometer", run_iometer);
2048 torture_suite_add_1smb_test(suite, "rename", torture_test_rename);
2049 torture_suite_add_suite(suite, torture_test_delete(suite));
2050 torture_suite_add_1smb_test(suite, "properties", torture_test_properties);
2051 torture_suite_add_1smb_test(suite, "mangle", torture_mangle);
2052 torture_suite_add_1smb_test(suite, "openattr", torture_openattrtest);
2053 torture_suite_add_1smb_test(suite, "winattr", torture_winattrtest);
2054 torture_suite_add_suite(suite, torture_charset(suite));
2055 torture_suite_add_1smb_test(suite, "chkpath", torture_chkpath_test);
2056 torture_suite_add_1smb_test(suite, "secleak", torture_sec_leak);
2057 torture_suite_add_simple_test(suite, "disconnect", torture_disconnect);
2058 torture_suite_add_suite(suite, torture_delay_write(suite));
2059 torture_suite_add_simple_test(suite, "samba3error", torture_samba3_errorpaths);
2060 torture_suite_add_1smb_test(suite, "casetable", torture_casetable);
2061 torture_suite_add_1smb_test(suite, "utable", torture_utable);
2062 torture_suite_add_simple_test(suite, "smb", torture_smb_scan);
2063 torture_suite_add_suite(suite, torture_trans2_aliases(suite));
2064 torture_suite_add_1smb_test(suite, "trans2-scan", torture_trans2_scan);
2065 torture_suite_add_1smb_test(suite, "nttrans", torture_nttrans_scan);
2066 torture_suite_add_1smb_test(suite, "createx_access", torture_createx_access);
2067 torture_suite_add_2smb_test(suite, "createx_sharemodes_file", torture_createx_sharemodes_file);
2068 torture_suite_add_2smb_test(suite, "createx_sharemodes_dir", torture_createx_sharemodes_dir);
2069 torture_suite_add_1smb_test(suite, "maximum_allowed", torture_maximum_allowed);
2070
2071 torture_suite_add_simple_test(suite, "bench-holdcon", torture_holdcon);
2072 torture_suite_add_1smb_test(suite, "bench-holdopen", torture_holdopen);
2073 torture_suite_add_simple_test(suite, "bench-readwrite", run_benchrw);
2074 torture_suite_add_smb_multi_test(suite, "bench-torture", run_torture);
2075 torture_suite_add_1smb_test(suite, "scan-pipe_number", run_pipe_number);
2076 torture_suite_add_1smb_test(suite, "scan-ioctl", torture_ioctl_test);
2077 torture_suite_add_1smb_test(suite, "scan-maxfid", torture_maxfid_test);
2078 torture_suite_add_1smb_test(suite,
2079 "smb1-twrp-openroot",
2080 torture_smb1_twrp_openroot);
2081 torture_suite_add_1smb_test(suite,
2082 "smb1-find-gmt-mask",
2083 torture_smb1_find_gmt_mask);
2084
2085 suite->description = talloc_strdup(suite,
2086 "Basic SMB tests (imported from the original smbtorture)");
2087
2088 torture_register_suite(ctx, suite);
2089
2090 return NT_STATUS_OK;
2091 }