638150e91bc3deedba4d82aa963357763d89763b
[samba.git] / source4 / torture / torture.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 1997-2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "dynconfig.h"
23 #include "clilist.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "system/time.h"
27 #include "system/wait.h"
28 #include "ioctl.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30
31 int torture_nprocs=4;
32 int torture_numops=100;
33 int torture_entries=1000;
34 int torture_failures=1;
35 int torture_seed=0;
36 static int procnum; /* records process count number when forking */
37 static struct smbcli_state *current_cli;
38 static BOOL use_oplocks;
39 static BOOL use_level_II_oplocks;
40
41 BOOL torture_showall = False;
42
43 #define CHECK_MAX_FAILURES(label) do { if (++failures >= torture_failures) goto label; } while (0)
44
45 static struct smbcli_state *open_nbt_connection(void)
46 {
47         struct nbt_name called, calling;
48         struct smbcli_state *cli;
49         const char *host = lp_parm_string(-1, "torture", "host");
50
51         calling.name = lp_netbios_name();
52         calling.type = NBT_NAME_CLIENT;
53         calling.scope = NULL;
54
55         nbt_choose_called_name(NULL, &called, host, NBT_NAME_SERVER);
56
57         cli = smbcli_state_init(NULL);
58         if (!cli) {
59                 printf("Failed initialize smbcli_struct to connect with %s\n", host);
60                 return NULL;
61         }
62
63         if (!smbcli_socket_connect(cli, host)) {
64                 printf("Failed to connect with %s\n", host);
65                 return cli;
66         }
67
68         if (!smbcli_transport_establish(cli, &calling, &called)) {
69                 printf("%s rejected the session\n",host);
70                 smbcli_shutdown(cli);
71                 return NULL;
72         }
73
74         return cli;
75 }
76
77 BOOL torture_open_connection_share(struct smbcli_state **c, 
78                                    const char *hostname, 
79                                    const char *sharename)
80 {
81         NTSTATUS status;
82         const char *username = lp_parm_string(-1, "torture", "username");
83         const char *userdomain = lp_parm_string(-1, "torture", "userdomain");
84         const char *password = lp_parm_string(-1, "torture", "password");
85
86         status = smbcli_full_connection(NULL,
87                                         c, lp_netbios_name(),
88                                         hostname, 
89                                         sharename, NULL,
90                                         username, username[0]?userdomain:"",
91                                         password);
92         if (!NT_STATUS_IS_OK(status)) {
93                 printf("Failed to open connection - %s\n", nt_errstr(status));
94                 return False;
95         }
96
97         (*c)->transport->options.use_oplocks = use_oplocks;
98         (*c)->transport->options.use_level2_oplocks = use_level_II_oplocks;
99
100         return True;
101 }
102
103 BOOL torture_open_connection(struct smbcli_state **c)
104 {
105         const char *host = lp_parm_string(-1, "torture", "host");
106         const char *share = lp_parm_string(-1, "torture", "share");
107
108         return torture_open_connection_share(c, host, share);
109 }
110
111
112
113 BOOL torture_close_connection(struct smbcli_state *c)
114 {
115         BOOL ret = True;
116         if (!c) return True;
117         if (NT_STATUS_IS_ERR(smbcli_tdis(c))) {
118                 printf("tdis failed (%s)\n", smbcli_errstr(c->tree));
119                 ret = False;
120         }
121         smbcli_shutdown(c);
122         return ret;
123 }
124
125 /* initialise a DCOM context */
126 NTSTATUS torture_dcom_init(struct dcom_context **ctx)
127 {
128         dcom_init(ctx, lp_parm_string(-1, "torture", "userdomain"),
129                                         lp_parm_string(-1, "torture", "username"), 
130                                         lp_parm_string(-1, "torture", "password"));
131
132         return NT_STATUS_OK;
133 }
134
135 /* open a rpc connection to the chosen binding string */
136 NTSTATUS torture_rpc_connection(struct dcerpc_pipe **p, 
137                                 const char *pipe_name,
138                                 const char *pipe_uuid, 
139                                 uint32_t pipe_version)
140 {
141         NTSTATUS status;
142         const char *binding = lp_parm_string(-1, "torture", "binding");
143
144         if (!binding) {
145                 printf("You must specify a ncacn binding string\n");
146                 return NT_STATUS_INVALID_PARAMETER;
147         }
148
149         status = dcerpc_pipe_connect(p, binding, pipe_uuid, pipe_version,
150                                      lp_parm_string(-1, "torture", "userdomain"), 
151                                      lp_parm_string(-1, "torture", "username"),
152                                      lp_parm_string(-1, "torture", "password"));
153  
154         return status;
155 }
156
157 /* open a rpc connection to a specific transport */
158 NTSTATUS torture_rpc_connection_transport(struct dcerpc_pipe **p, 
159                                           const char *pipe_name,
160                                           const char *pipe_uuid, 
161                                           uint32_t pipe_version,
162                                           enum dcerpc_transport_t transport)
163 {
164         NTSTATUS status;
165         const char *binding = lp_parm_string(-1, "torture", "binding");
166         struct dcerpc_binding b;
167         TALLOC_CTX *mem_ctx = talloc_init("torture_rpc_connection_smb");
168
169         if (!binding) {
170                 printf("You must specify a ncacn binding string\n");
171                 return NT_STATUS_INVALID_PARAMETER;
172         }
173
174         status = dcerpc_parse_binding(mem_ctx, binding, &b);
175         if (!NT_STATUS_IS_OK(status)) {
176                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
177                 talloc_destroy(mem_ctx);
178                 return status;
179         }
180
181         b.transport = transport;
182
183         status = dcerpc_pipe_connect_b(p, &b, pipe_uuid, pipe_version,
184                                        lp_parm_string(-1, "torture", "userdomain"), 
185                                        lp_parm_string(-1, "torture", "username"),
186                                        lp_parm_string(-1, "torture", "password"));
187  
188         return status;
189 }
190
191 /* close a rpc connection to a named pipe */
192 NTSTATUS torture_rpc_close(struct dcerpc_pipe *p)
193 {
194         dcerpc_pipe_close(p);
195         return NT_STATUS_OK;
196 }
197
198
199 /* check if the server produced the expected error code */
200 BOOL check_error(const char *location, struct smbcli_state *c, 
201                  uint8_t eclass, uint32_t ecode, NTSTATUS nterr)
202 {
203         if (smbcli_is_dos_error(c->tree)) {
204                 uint8_t class;
205                 uint32_t num;
206
207                 /* Check DOS error */
208
209                 smbcli_dos_error(c, &class, &num);
210
211                 if (eclass != class || ecode != num) {
212                         printf("unexpected error code class=%d code=%d\n", 
213                                (int)class, (int)num);
214                         printf(" expected %d/%d %s (at %s)\n", 
215                                (int)eclass, (int)ecode, nt_errstr(nterr), location);
216                         return False;
217                 }
218
219         } else {
220                 NTSTATUS status;
221
222                 /* Check NT error */
223
224                 status = smbcli_nt_error(c->tree);
225
226                 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
227                         printf("unexpected error code %s\n", nt_errstr(status));
228                         printf(" expected %s (at %s)\n", nt_errstr(nterr), location);
229                         return False;
230                 }
231         }
232
233         return True;
234 }
235
236
237 static BOOL wait_lock(struct smbcli_state *c, int fnum, uint32_t offset, uint32_t len)
238 {
239         while (NT_STATUS_IS_ERR(smbcli_lock(c->tree, fnum, offset, len, -1, WRITE_LOCK))) {
240                 if (!check_error(__location__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
241         }
242         return True;
243 }
244
245
246 static BOOL rw_torture(struct smbcli_state *c)
247 {
248         const char *lockfname = "\\torture.lck";
249         char *fname;
250         int fnum;
251         int fnum2;
252         pid_t pid2, pid = getpid();
253         int i, j;
254         uint8_t buf[1024];
255         BOOL correct = True;
256
257         fnum2 = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
258                          DENY_NONE);
259         if (fnum2 == -1)
260                 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR, DENY_NONE);
261         if (fnum2 == -1) {
262                 printf("open of %s failed (%s)\n", lockfname, smbcli_errstr(c->tree));
263                 return False;
264         }
265
266
267         for (i=0;i<torture_numops;i++) {
268                 uint_t n = (uint_t)random()%10;
269                 if (i % 10 == 0) {
270                         printf("%d\r", i); fflush(stdout);
271                 }
272                 asprintf(&fname, "\\torture.%u", n);
273
274                 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
275                         return False;
276                 }
277
278                 fnum = smbcli_open(c->tree, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
279                 if (fnum == -1) {
280                         printf("open failed (%s)\n", smbcli_errstr(c->tree));
281                         correct = False;
282                         break;
283                 }
284
285                 if (smbcli_write(c->tree, fnum, 0, &pid, 0, sizeof(pid)) != sizeof(pid)) {
286                         printf("write failed (%s)\n", smbcli_errstr(c->tree));
287                         correct = False;
288                 }
289
290                 for (j=0;j<50;j++) {
291                         if (smbcli_write(c->tree, fnum, 0, buf, 
292                                       sizeof(pid)+(j*sizeof(buf)), 
293                                       sizeof(buf)) != sizeof(buf)) {
294                                 printf("write failed (%s)\n", smbcli_errstr(c->tree));
295                                 correct = False;
296                         }
297                 }
298
299                 pid2 = 0;
300
301                 if (smbcli_read(c->tree, fnum, &pid2, 0, sizeof(pid)) != sizeof(pid)) {
302                         printf("read failed (%s)\n", smbcli_errstr(c->tree));
303                         correct = False;
304                 }
305
306                 if (pid2 != pid) {
307                         printf("data corruption!\n");
308                         correct = False;
309                 }
310
311                 if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
312                         printf("close failed (%s)\n", smbcli_errstr(c->tree));
313                         correct = False;
314                 }
315
316                 if (NT_STATUS_IS_ERR(smbcli_unlink(c->tree, fname))) {
317                         printf("unlink failed (%s)\n", smbcli_errstr(c->tree));
318                         correct = False;
319                 }
320
321                 if (NT_STATUS_IS_ERR(smbcli_unlock(c->tree, fnum2, n*sizeof(int), sizeof(int)))) {
322                         printf("unlock failed (%s)\n", smbcli_errstr(c->tree));
323                         correct = False;
324                 }
325                 free(fname);
326         }
327
328         smbcli_close(c->tree, fnum2);
329         smbcli_unlink(c->tree, lockfname);
330
331         printf("%d\n", i);
332
333         return correct;
334 }
335
336 static BOOL run_torture(struct smbcli_state *cli, int dummy)
337 {
338         BOOL ret;
339
340         ret = rw_torture(cli);
341         
342         if (!torture_close_connection(cli)) {
343                 ret = False;
344         }
345
346         return ret;
347 }
348
349 static BOOL rw_torture3(struct smbcli_state *c, const char *lockfname)
350 {
351         int fnum = -1;
352         uint_t i = 0;
353         uint8_t buf[131072];
354         uint8_t buf_rd[131072];
355         uint_t count;
356         uint_t countprev = 0;
357         ssize_t sent = 0;
358         BOOL correct = True;
359
360         for (i = 0; i < sizeof(buf); i += sizeof(uint32_t))
361         {
362                 SIVAL(buf, i, random());
363         }
364
365         if (procnum == 0)
366         {
367                 fnum = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
368                                 DENY_NONE);
369                 if (fnum == -1) {
370                         printf("first open read/write of %s failed (%s)\n",
371                                         lockfname, smbcli_errstr(c->tree));
372                         return False;
373                 }
374         }
375         else
376         {
377                 for (i = 0; i < 500 && fnum == -1; i++)
378                 {
379                         fnum = smbcli_open(c->tree, lockfname, O_RDONLY, 
380                                         DENY_NONE);
381                         msleep(10);
382                 }
383                 if (fnum == -1) {
384                         printf("second open read-only of %s failed (%s)\n",
385                                         lockfname, smbcli_errstr(c->tree));
386                         return False;
387                 }
388         }
389
390         i = 0;
391         for (count = 0; count < sizeof(buf); count += sent)
392         {
393                 if (count >= countprev) {
394                         printf("%d %8d\r", i, count);
395                         fflush(stdout);
396                         i++;
397                         countprev += (sizeof(buf) / 20);
398                 }
399
400                 if (procnum == 0)
401                 {
402                         sent = ((uint_t)random()%(20))+ 1;
403                         if (sent > sizeof(buf) - count)
404                         {
405                                 sent = sizeof(buf) - count;
406                         }
407
408                         if (smbcli_write(c->tree, fnum, 0, buf+count, count, (size_t)sent) != sent) {
409                                 printf("write failed (%s)\n", smbcli_errstr(c->tree));
410                                 correct = False;
411                         }
412                 }
413                 else
414                 {
415                         sent = smbcli_read(c->tree, fnum, buf_rd+count, count,
416                                         sizeof(buf)-count);
417                         if (sent < 0)
418                         {
419                                 printf("read failed offset:%d size:%d (%s)\n",
420                                                 count, sizeof(buf)-count,
421                                                 smbcli_errstr(c->tree));
422                                 correct = False;
423                                 sent = 0;
424                         }
425                         if (sent > 0)
426                         {
427                                 if (memcmp(buf_rd+count, buf+count, sent) != 0)
428                                 {
429                                         printf("read/write compare failed\n");
430                                         printf("offset: %d req %d recvd %d\n",
431                                                 count, sizeof(buf)-count, sent);
432                                         correct = False;
433                                         break;
434                                 }
435                         }
436                 }
437
438         }
439
440         if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
441                 printf("close failed (%s)\n", smbcli_errstr(c->tree));
442                 correct = False;
443         }
444
445         return correct;
446 }
447
448 static BOOL rw_torture2(struct smbcli_state *c1, struct smbcli_state *c2)
449 {
450         const char *lockfname = "\\torture2.lck";
451         int fnum1;
452         int fnum2;
453         int i;
454         uint8_t buf[131072];
455         uint8_t buf_rd[131072];
456         BOOL correct = True;
457         ssize_t bytes_read, bytes_written;
458
459         if (smbcli_deltree(c1->tree, lockfname) == -1) {
460                 printf("unlink failed (%s)\n", smbcli_errstr(c1->tree));
461         }
462
463         fnum1 = smbcli_open(c1->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
464                          DENY_NONE);
465         if (fnum1 == -1) {
466                 printf("first open read/write of %s failed (%s)\n",
467                                 lockfname, smbcli_errstr(c1->tree));
468                 return False;
469         }
470         fnum2 = smbcli_open(c2->tree, lockfname, O_RDONLY, 
471                          DENY_NONE);
472         if (fnum2 == -1) {
473                 printf("second open read-only of %s failed (%s)\n",
474                                 lockfname, smbcli_errstr(c2->tree));
475                 smbcli_close(c1->tree, fnum1);
476                 return False;
477         }
478
479         printf("Checking data integrity over %d ops\n", torture_numops);
480
481         for (i=0;i<torture_numops;i++)
482         {
483                 size_t buf_size = ((uint_t)random()%(sizeof(buf)-1))+ 1;
484                 if (i % 10 == 0) {
485                         printf("%d\r", i); fflush(stdout);
486                 }
487
488                 generate_random_buffer(buf, buf_size);
489
490                 if ((bytes_written = smbcli_write(c1->tree, fnum1, 0, buf, 0, buf_size)) != buf_size) {
491                         printf("write failed (%s)\n", smbcli_errstr(c1->tree));
492                         printf("wrote %d, expected %d\n", bytes_written, buf_size); 
493                         correct = False;
494                         break;
495                 }
496
497                 if ((bytes_read = smbcli_read(c2->tree, fnum2, buf_rd, 0, buf_size)) != buf_size) {
498                         printf("read failed (%s)\n", smbcli_errstr(c2->tree));
499                         printf("read %d, expected %d\n", bytes_read, buf_size); 
500                         correct = False;
501                         break;
502                 }
503
504                 if (memcmp(buf_rd, buf, buf_size) != 0)
505                 {
506                         printf("read/write compare failed\n");
507                         correct = False;
508                         break;
509                 }
510         }
511
512         if (NT_STATUS_IS_ERR(smbcli_close(c2->tree, fnum2))) {
513                 printf("close failed (%s)\n", smbcli_errstr(c2->tree));
514                 correct = False;
515         }
516         if (NT_STATUS_IS_ERR(smbcli_close(c1->tree, fnum1))) {
517                 printf("close failed (%s)\n", smbcli_errstr(c1->tree));
518                 correct = False;
519         }
520
521         if (NT_STATUS_IS_ERR(smbcli_unlink(c1->tree, lockfname))) {
522                 printf("unlink failed (%s)\n", smbcli_errstr(c1->tree));
523                 correct = False;
524         }
525
526         return correct;
527 }
528
529 static BOOL run_readwritetest(void)
530 {
531         struct smbcli_state *cli1, *cli2;
532         BOOL test1, test2 = True;
533
534         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
535                 return False;
536         }
537
538         printf("starting readwritetest\n");
539
540         test1 = rw_torture2(cli1, cli2);
541         printf("Passed readwritetest v1: %s\n", BOOLSTR(test1));
542
543         if (test1) {
544                 test2 = rw_torture2(cli1, cli1);
545                 printf("Passed readwritetest v2: %s\n", BOOLSTR(test2));
546         }
547
548         if (!torture_close_connection(cli1)) {
549                 test1 = False;
550         }
551
552         if (!torture_close_connection(cli2)) {
553                 test2 = False;
554         }
555
556         return (test1 && test2);
557 }
558
559 static BOOL run_readwritemulti(struct smbcli_state *cli, int dummy)
560 {
561         BOOL test;
562
563         test = rw_torture3(cli, "\\multitest.txt");
564
565         if (!torture_close_connection(cli)) {
566                 test = False;
567         }
568         
569         return test;
570 }
571
572
573 /*
574   this checks to see if a secondary tconx can use open files from an
575   earlier tconx
576  */
577 static BOOL run_tcon_test(void)
578 {
579         struct smbcli_state *cli;
580         const char *fname = "\\tcontest.tmp";
581         int fnum1;
582         uint16_t cnum1, cnum2, cnum3;
583         uint16_t vuid1, vuid2;
584         uint8_t buf[4];
585         BOOL ret = True;
586         struct smbcli_tree *tree1;
587         const char *host = lp_parm_string(-1, "torture", "host");
588         const char *share = lp_parm_string(-1, "torture", "share");
589         const char *password = lp_parm_string(-1, "torture", "password");
590
591         if (!torture_open_connection(&cli)) {
592                 return False;
593         }
594
595         printf("starting tcontest\n");
596
597         if (smbcli_deltree(cli->tree, fname) == -1) {
598                 printf("unlink of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
599         }
600
601         fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
602         if (fnum1 == -1) {
603                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
604                 return False;
605         }
606
607         cnum1 = cli->tree->tid;
608         vuid1 = cli->session->vuid;
609
610         memset(&buf, 0, 4); /* init buf so valgrind won't complain */
611         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) != 4) {
612                 printf("initial write failed (%s)\n", smbcli_errstr(cli->tree));
613                 return False;
614         }
615
616         tree1 = cli->tree;      /* save old tree connection */
617         if (NT_STATUS_IS_ERR(smbcli_send_tconX(cli, share, "?????", password))) {
618                 printf("%s refused 2nd tree connect (%s)\n", host,
619                            smbcli_errstr(cli->tree));
620                 smbcli_shutdown(cli);
621                 return False;
622         }
623
624         cnum2 = cli->tree->tid;
625         cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
626         vuid2 = cli->session->vuid + 1;
627
628         /* try a write with the wrong tid */
629         cli->tree->tid = cnum2;
630
631         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
632                 printf("* server allows write with wrong TID\n");
633                 ret = False;
634         } else {
635                 printf("server fails write with wrong TID : %s\n", smbcli_errstr(cli->tree));
636         }
637
638
639         /* try a write with an invalid tid */
640         cli->tree->tid = cnum3;
641
642         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
643                 printf("* server allows write with invalid TID\n");
644                 ret = False;
645         } else {
646                 printf("server fails write with invalid TID : %s\n", smbcli_errstr(cli->tree));
647         }
648
649         /* try a write with an invalid vuid */
650         cli->session->vuid = vuid2;
651         cli->tree->tid = cnum1;
652
653         if (smbcli_write(cli->tree, fnum1, 0, buf, 130, 4) == 4) {
654                 printf("* server allows write with invalid VUID\n");
655                 ret = False;
656         } else {
657                 printf("server fails write with invalid VUID : %s\n", smbcli_errstr(cli->tree));
658         }
659
660         cli->session->vuid = vuid1;
661         cli->tree->tid = cnum1;
662
663         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum1))) {
664                 printf("close failed (%s)\n", smbcli_errstr(cli->tree));
665                 return False;
666         }
667
668         cli->tree->tid = cnum2;
669
670         if (NT_STATUS_IS_ERR(smbcli_tdis(cli))) {
671                 printf("secondary tdis failed (%s)\n", smbcli_errstr(cli->tree));
672                 return False;
673         }
674
675         cli->tree = tree1;  /* restore initial tree */
676         cli->tree->tid = cnum1;
677
678         if (!torture_close_connection(cli)) {
679                 return False;
680         }
681
682         return ret;
683 }
684
685
686
687 static BOOL tcon_devtest(struct smbcli_state *cli,
688                          const char *myshare, const char *devtype,
689                          NTSTATUS expected_error)
690 {
691         BOOL status;
692         BOOL ret;
693         const char *password = lp_parm_string(-1, "torture", "password");
694
695         status = NT_STATUS_IS_OK(smbcli_send_tconX(cli, myshare, devtype, 
696                                                 password));
697
698         printf("Trying share %s with devtype %s\n", myshare, devtype);
699
700         if (NT_STATUS_IS_OK(expected_error)) {
701                 if (status) {
702                         ret = True;
703                 } else {
704                         printf("tconX to share %s with type %s "
705                                "should have succeeded but failed\n",
706                                myshare, devtype);
707                         ret = False;
708                 }
709                 smbcli_tdis(cli);
710         } else {
711                 if (status) {
712                         printf("tconx to share %s with type %s "
713                                "should have failed but succeeded\n",
714                                myshare, devtype);
715                         ret = False;
716                 } else {
717                         if (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),
718                                             expected_error)) {
719                                 ret = True;
720                         } else {
721                                 printf("Returned unexpected error\n");
722                                 ret = False;
723                         }
724                 }
725         }
726         return ret;
727 }
728
729 /*
730  checks for correct tconX support
731  */
732 static BOOL run_tcon_devtype_test(void)
733 {
734         struct smbcli_state *cli1 = NULL;
735         NTSTATUS status;
736         BOOL ret = True;
737         const char *host = lp_parm_string(-1, "torture", "host");
738         const char *share = lp_parm_string(-1, "torture", "share");
739         const char *username = lp_parm_string(-1, "torture", "username");
740         const char *userdomain = lp_parm_string(-1, "torture", "userdomain");
741         const char *password = lp_parm_string(-1, "torture", "password");
742         
743         status = smbcli_full_connection(NULL,
744                                         &cli1, lp_netbios_name(),
745                                         host, 
746                                         share, NULL,
747                                         username, userdomain,
748                                         password);
749
750         if (!NT_STATUS_IS_OK(status)) {
751                 printf("could not open connection\n");
752                 return False;
753         }
754
755         if (!tcon_devtest(cli1, "IPC$", "A:", NT_STATUS_BAD_DEVICE_TYPE))
756                 ret = False;
757
758         if (!tcon_devtest(cli1, "IPC$", "?????", NT_STATUS_OK))
759                 ret = False;
760
761         if (!tcon_devtest(cli1, "IPC$", "LPT:", NT_STATUS_BAD_DEVICE_TYPE))
762                 ret = False;
763
764         if (!tcon_devtest(cli1, "IPC$", "IPC", NT_STATUS_OK))
765                 ret = False;
766                         
767         if (!tcon_devtest(cli1, "IPC$", "FOOBA", NT_STATUS_BAD_DEVICE_TYPE))
768                 ret = False;
769
770         if (!tcon_devtest(cli1, share, "A:", NT_STATUS_OK))
771                 ret = False;
772
773         if (!tcon_devtest(cli1, share, "?????", NT_STATUS_OK))
774                 ret = False;
775
776         if (!tcon_devtest(cli1, share, "LPT:", NT_STATUS_BAD_DEVICE_TYPE))
777                 ret = False;
778
779         if (!tcon_devtest(cli1, share, "IPC", NT_STATUS_BAD_DEVICE_TYPE))
780                 ret = False;
781                         
782         if (!tcon_devtest(cli1, share, "FOOBA", NT_STATUS_BAD_DEVICE_TYPE))
783                 ret = False;
784
785         smbcli_shutdown(cli1);
786
787         if (ret)
788                 printf("Passed tcondevtest\n");
789
790         return ret;
791 }
792
793
794 /*
795 test whether fnums and tids open on one VC are available on another (a major
796 security hole)
797 */
798 static BOOL run_fdpasstest(void)
799 {
800         struct smbcli_state *cli1, *cli2;
801         const char *fname = "\\fdpass.tst";
802         int fnum1, oldtid;
803         uint8_t buf[1024];
804
805         if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
806                 return False;
807         }
808
809         printf("starting fdpasstest\n");
810
811         smbcli_unlink(cli1->tree, fname);
812
813         printf("Opening a file on connection 1\n");
814
815         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
816         if (fnum1 == -1) {
817                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
818                 return False;
819         }
820
821         printf("writing to file on connection 1\n");
822
823         if (smbcli_write(cli1->tree, fnum1, 0, "hello world\n", 0, 13) != 13) {
824                 printf("write failed (%s)\n", smbcli_errstr(cli1->tree));
825                 return False;
826         }
827
828         oldtid = cli2->tree->tid;
829         cli2->session->vuid = cli1->session->vuid;
830         cli2->tree->tid = cli1->tree->tid;
831         cli2->session->pid = cli1->session->pid;
832
833         printf("reading from file on connection 2\n");
834
835         if (smbcli_read(cli2->tree, fnum1, buf, 0, 13) == 13) {
836                 printf("read succeeded! nasty security hole [%s]\n",
837                        buf);
838                 return False;
839         }
840
841         smbcli_close(cli1->tree, fnum1);
842         smbcli_unlink(cli1->tree, fname);
843
844         cli2->tree->tid = oldtid;
845
846         torture_close_connection(cli1);
847         torture_close_connection(cli2);
848
849         printf("finished fdpasstest\n");
850         return True;
851 }
852
853
854 /*
855 test the timing of deferred open requests
856 */
857 static BOOL run_deferopen(struct smbcli_state *cli, int dummy)
858 {
859         const char *fname = "\\defer_open_test.dat";
860         int retries=4;
861         int i = 0;
862         BOOL correct = True;
863
864         if (retries <= 0) {
865                 printf("failed to connect\n");
866                 return False;
867         }
868
869         printf("Testing deferred open requests.\n");
870
871         while (i < 4) {
872                 int fnum = -1;
873
874                 do {
875                         struct timeval tv;
876                         tv = timeval_current();
877                         fnum = smbcli_nt_create_full(cli->tree, fname, 0, 
878                                                      SEC_RIGHTS_FILE_ALL,
879                                                      FILE_ATTRIBUTE_NORMAL, 
880                                                      NTCREATEX_SHARE_ACCESS_NONE,
881                                                      NTCREATEX_DISP_OPEN_IF, 0, 0);
882                         if (fnum != -1) {
883                                 break;
884                         }
885                         if (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION)) {
886                                 double e = timeval_elapsed(&tv);
887                                 if (e < 0.5 || e > 1.5) {
888                                         fprintf(stderr,"Timing incorrect %.2f violation\n",
889                                                 e);
890                                 }
891                         }
892                 } while (NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION));
893
894                 if (fnum == -1) {
895                         fprintf(stderr,"Failed to open %s, error=%s\n", fname, smbcli_errstr(cli->tree));
896                         return False;
897                 }
898
899                 printf("pid %u open %d\n", getpid(), i);
900
901                 sleep(10);
902                 i++;
903                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
904                         fprintf(stderr,"Failed to close %s, error=%s\n", fname, smbcli_errstr(cli->tree));
905                         return False;
906                 }
907                 sleep(2);
908         }
909
910         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
911                 /* All until the last unlink will fail with sharing violation. */
912                 if (!NT_STATUS_EQUAL(smbcli_nt_error(cli->tree),NT_STATUS_SHARING_VIOLATION)) {
913                         printf("unlink of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
914                         correct = False;
915                 }
916         }
917
918         printf("deferred test finished\n");
919         if (!torture_close_connection(cli)) {
920                 correct = False;
921         }
922         return correct;
923 }
924
925 /*
926 test how many open files this server supports on the one socket
927 */
928 static BOOL run_maxfidtest(struct smbcli_state *cli, int dummy)
929 {
930 #define MAXFID_TEMPLATE "\\maxfid\\fid%d\\maxfid.%d.%d"
931         char *fname;
932         int fnums[0x11000], i;
933         int retries=4, maxfid;
934         BOOL correct = True;
935
936         if (retries <= 0) {
937                 printf("failed to connect\n");
938                 return False;
939         }
940
941         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
942                 printf("Failed to deltree \\maxfid - %s\n",
943                        smbcli_errstr(cli->tree));
944                 return False;
945         }
946         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\maxfid"))) {
947                 printf("Failed to mkdir \\maxfid, error=%s\n", 
948                        smbcli_errstr(cli->tree));
949                 return False;
950         }
951
952         printf("Testing maximum number of open files\n");
953
954         for (i=0; i<0x11000; i++) {
955                 if (i % 1000 == 0) {
956                         asprintf(&fname, "\\maxfid\\fid%d", i/1000);
957                         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
958                                 printf("Failed to mkdir %s, error=%s\n", 
959                                        fname, smbcli_errstr(cli->tree));
960                                 return False;
961                         }
962                         free(fname);
963                 }
964                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
965                 if ((fnums[i] = smbcli_open(cli->tree, fname, 
966                                         O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
967                     -1) {
968                         printf("open of %s failed (%s)\n", 
969                                fname, smbcli_errstr(cli->tree));
970                         printf("maximum fnum is %d\n", i);
971                         break;
972                 }
973                 free(fname);
974                 printf("%6d\r", i);
975         }
976         printf("%6d\n", i);
977         i--;
978
979         maxfid = i;
980
981         printf("cleaning up\n");
982         for (i=0;i<maxfid/2;i++) {
983                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
984                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[i]))) {
985                         printf("Close of fnum %d failed - %s\n", fnums[i], smbcli_errstr(cli->tree));
986                 }
987                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
988                         printf("unlink of %s failed (%s)\n", 
989                                fname, smbcli_errstr(cli->tree));
990                         correct = False;
991                 }
992                 free(fname);
993
994                 asprintf(&fname, MAXFID_TEMPLATE, (maxfid-i)/1000, maxfid-i,(int)getpid());
995                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[maxfid-i]))) {
996                         printf("Close of fnum %d failed - %s\n", fnums[maxfid-i], smbcli_errstr(cli->tree));
997                 }
998                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
999                         printf("unlink of %s failed (%s)\n", 
1000                                fname, smbcli_errstr(cli->tree));
1001                         correct = False;
1002                 }
1003                 free(fname);
1004
1005                 printf("%6d %6d\r", i, maxfid-i);
1006         }
1007         printf("%6d\n", 0);
1008
1009         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
1010                 printf("Failed to deltree \\maxfid - %s\n",
1011                        smbcli_errstr(cli->tree));
1012                 return False;
1013         }
1014
1015         printf("maxfid test finished\n");
1016         if (!torture_close_connection(cli)) {
1017                 correct = False;
1018         }
1019         return correct;
1020 #undef MAXFID_TEMPLATE
1021 }
1022
1023 /* send smb negprot commands, not reading the response */
1024 static BOOL run_negprot_nowait(void)
1025 {
1026         int i;
1027         struct smbcli_state *cli, *cli2;
1028         BOOL correct = True;
1029
1030         printf("starting negprot nowait test\n");
1031
1032         cli = open_nbt_connection();
1033         if (!cli) {
1034                 return False;
1035         }
1036
1037         printf("Filling send buffer\n");
1038
1039         for (i=0;i<10000;i++) {
1040                 struct smbcli_request *req;
1041                 time_t t1 = time(NULL);
1042                 req = smb_raw_negotiate_send(cli->transport, PROTOCOL_NT1);
1043                 while (req->state == SMBCLI_REQUEST_SEND && time(NULL) < t1+5) {
1044                         smbcli_transport_process(cli->transport);
1045                 }
1046                 if (req->state == SMBCLI_REQUEST_ERROR) {
1047                         printf("Failed to fill pipe - %s\n", nt_errstr(req->status));
1048                         torture_close_connection(cli);
1049                         return correct;
1050                 }
1051                 if (req->state == SMBCLI_REQUEST_SEND) {
1052                         break;
1053                 }
1054         }
1055
1056         if (i == 10000) {
1057                 printf("send buffer failed to fill\n");
1058                 if (!torture_close_connection(cli)) {
1059                         correct = False;
1060                 }
1061                 return correct;
1062         }
1063
1064         printf("send buffer filled after %d requests\n", i);
1065
1066         printf("Opening secondary connection\n");
1067         if (!torture_open_connection(&cli2)) {
1068                 return False;
1069         }
1070
1071         if (!torture_close_connection(cli)) {
1072                 correct = False;
1073         }
1074
1075         if (!torture_close_connection(cli2)) {
1076                 correct = False;
1077         }
1078
1079         printf("finished negprot nowait test\n");
1080
1081         return correct;
1082 }
1083
1084
1085 /*
1086   This checks how the getatr calls works
1087 */
1088 static BOOL run_attrtest(void)
1089 {
1090         struct smbcli_state *cli;
1091         int fnum;
1092         time_t t, t2;
1093         const char *fname = "\\attrib123456789.tst";
1094         BOOL correct = True;
1095
1096         printf("starting attrib test\n");
1097
1098         if (!torture_open_connection(&cli)) {
1099                 return False;
1100         }
1101
1102         smbcli_unlink(cli->tree, fname);
1103         fnum = smbcli_open(cli->tree, fname, 
1104                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1105         smbcli_close(cli->tree, fnum);
1106
1107         if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, fname, NULL, NULL, &t))) {
1108                 printf("getatr failed (%s)\n", smbcli_errstr(cli->tree));
1109                 correct = False;
1110         }
1111
1112         printf("New file time is %s", ctime(&t));
1113
1114         if (abs(t - time(NULL)) > 60*60*24*10) {
1115                 printf("ERROR: SMBgetatr bug. time is %s",
1116                        ctime(&t));
1117                 t = time(NULL);
1118                 correct = False;
1119         }
1120
1121         t2 = t-60*60*24; /* 1 day ago */
1122
1123         printf("Setting file time to %s", ctime(&t2));
1124
1125         if (NT_STATUS_IS_ERR(smbcli_setatr(cli->tree, fname, 0, t2))) {
1126                 printf("setatr failed (%s)\n", smbcli_errstr(cli->tree));
1127                 correct = True;
1128         }
1129
1130         if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, fname, NULL, NULL, &t))) {
1131                 printf("getatr failed (%s)\n", smbcli_errstr(cli->tree));
1132                 correct = True;
1133         }
1134
1135         printf("Retrieved file time as %s", ctime(&t));
1136
1137         if (t != t2) {
1138                 printf("ERROR: getatr/setatr bug. times are\n%s",
1139                        ctime(&t));
1140                 printf("%s", ctime(&t2));
1141                 correct = True;
1142         }
1143
1144         smbcli_unlink(cli->tree, fname);
1145
1146         if (!torture_close_connection(cli)) {
1147                 correct = False;
1148         }
1149
1150         printf("attrib test finished\n");
1151
1152         return correct;
1153 }
1154
1155
1156 /*
1157   This checks a couple of trans2 calls
1158 */
1159 static BOOL run_trans2test(void)
1160 {
1161         struct smbcli_state *cli;
1162         int fnum;
1163         size_t size;
1164         time_t c_time, a_time, m_time, w_time, m_time2;
1165         const char *fname = "\\trans2.tst";
1166         const char *dname = "\\trans2";
1167         const char *fname2 = "\\trans2\\trans2.tst";
1168         const char *pname;
1169         BOOL correct = True;
1170
1171         printf("starting trans2 test\n");
1172
1173         if (!torture_open_connection(&cli)) {
1174                 return False;
1175         }
1176
1177         smbcli_unlink(cli->tree, fname);
1178
1179         printf("Testing qfileinfo\n");
1180         
1181         fnum = smbcli_open(cli->tree, fname, 
1182                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1183         if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &size, &c_time, &a_time, &m_time,
1184                            NULL, NULL))) {
1185                 printf("ERROR: qfileinfo failed (%s)\n", smbcli_errstr(cli->tree));
1186                 correct = False;
1187         }
1188
1189         printf("Testing NAME_INFO\n");
1190
1191         if (NT_STATUS_IS_ERR(smbcli_qfilename(cli->tree, fnum, &pname))) {
1192                 printf("ERROR: qfilename failed (%s)\n", smbcli_errstr(cli->tree));
1193                 correct = False;
1194         }
1195
1196         if (!pname || strcmp(pname, fname)) {
1197                 printf("qfilename gave different name? [%s] [%s]\n",
1198                        fname, pname);
1199                 correct = False;
1200         }
1201
1202         smbcli_close(cli->tree, fnum);
1203         smbcli_unlink(cli->tree, fname);
1204
1205         fnum = smbcli_open(cli->tree, fname, 
1206                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1207         if (fnum == -1) {
1208                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
1209                 return False;
1210         }
1211         smbcli_close(cli->tree, fnum);
1212
1213         printf("Checking for sticky create times\n");
1214
1215         if (NT_STATUS_IS_ERR(smbcli_qpathinfo(cli->tree, fname, &c_time, &a_time, &m_time, &size, NULL))) {
1216                 printf("ERROR: qpathinfo failed (%s)\n", smbcli_errstr(cli->tree));
1217                 correct = False;
1218         } else {
1219                 if (c_time != m_time) {
1220                         printf("create time=%s", ctime(&c_time));
1221                         printf("modify time=%s", ctime(&m_time));
1222                         printf("This system appears to have sticky create times\n");
1223                 }
1224                 if (a_time % (60*60) == 0) {
1225                         printf("access time=%s", ctime(&a_time));
1226                         printf("This system appears to set a midnight access time\n");
1227                         correct = False;
1228                 }
1229
1230                 if (abs(m_time - time(NULL)) > 60*60*24*7) {
1231                         printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
1232                         correct = False;
1233                 }
1234         }
1235
1236
1237         smbcli_unlink(cli->tree, fname);
1238         fnum = smbcli_open(cli->tree, fname, 
1239                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1240         smbcli_close(cli->tree, fnum);
1241         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, fname, &c_time, &a_time, &m_time, &w_time, &size, NULL, NULL))) {
1242                 printf("ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
1243                 correct = False;
1244         } else {
1245                 if (w_time < 60*60*24*2) {
1246                         printf("write time=%s", ctime(&w_time));
1247                         printf("This system appears to set a initial 0 write time\n");
1248                         correct = False;
1249                 }
1250         }
1251
1252         smbcli_unlink(cli->tree, fname);
1253
1254
1255         /* check if the server updates the directory modification time
1256            when creating a new file */
1257         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, dname))) {
1258                 printf("ERROR: mkdir failed (%s)\n", smbcli_errstr(cli->tree));
1259                 correct = False;
1260         }
1261         sleep(3);
1262         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\trans2\\", &c_time, &a_time, &m_time, &w_time, &size, NULL, NULL))) {
1263                 printf("ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
1264                 correct = False;
1265         }
1266
1267         fnum = smbcli_open(cli->tree, fname2, 
1268                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1269         smbcli_write(cli->tree, fnum,  0, &fnum, 0, sizeof(fnum));
1270         smbcli_close(cli->tree, fnum);
1271         if (NT_STATUS_IS_ERR(smbcli_qpathinfo2(cli->tree, "\\trans2\\", &c_time, &a_time, &m_time2, &w_time, &size, NULL, NULL))) {
1272                 printf("ERROR: qpathinfo2 failed (%s)\n", smbcli_errstr(cli->tree));
1273                 correct = False;
1274         } else {
1275                 if (m_time2 == m_time) {
1276                         printf("This system does not update directory modification times\n");
1277                         correct = False;
1278                 }
1279         }
1280         smbcli_unlink(cli->tree, fname2);
1281         smbcli_rmdir(cli->tree, dname);
1282
1283         if (!torture_close_connection(cli)) {
1284                 correct = False;
1285         }
1286
1287         printf("trans2 test finished\n");
1288
1289         return correct;
1290 }
1291
1292
1293
1294 /* FIRST_DESIRED_ACCESS   0xf019f */
1295 #define FIRST_DESIRED_ACCESS   SEC_FILE_READ_DATA|SEC_FILE_WRITE_DATA|SEC_FILE_APPEND_DATA|\
1296                                SEC_FILE_READ_EA|                           /* 0xf */ \
1297                                SEC_FILE_WRITE_EA|SEC_FILE_READ_ATTRIBUTE|     /* 0x90 */ \
1298                                SEC_FILE_WRITE_ATTRIBUTE|                  /* 0x100 */ \
1299                                SEC_STD_DELETE|SEC_STD_READ_CONTROL|\
1300                                SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER     /* 0xf0000 */
1301 /* SECOND_DESIRED_ACCESS  0xe0080 */
1302 #define SECOND_DESIRED_ACCESS  SEC_FILE_READ_ATTRIBUTE|                   /* 0x80 */ \
1303                                SEC_STD_READ_CONTROL|SEC_STD_WRITE_DAC|\
1304                                SEC_STD_WRITE_OWNER                      /* 0xe0000 */
1305
1306 #if 0
1307 #define THIRD_DESIRED_ACCESS   FILE_READ_ATTRIBUTE|                   /* 0x80 */ \
1308                                READ_CONTROL|WRITE_DAC|\
1309                                SEC_FILE_READ_DATA|\
1310                                WRITE_OWNER                      /* */
1311 #endif
1312
1313 /*
1314   Test ntcreate calls made by xcopy
1315  */
1316 static BOOL run_xcopy(void)
1317 {
1318         struct smbcli_state *cli1;
1319         const char *fname = "\\test.txt";
1320         BOOL correct = True;
1321         int fnum1, fnum2;
1322
1323         printf("starting xcopy test\n");
1324         
1325         if (!torture_open_connection(&cli1)) {
1326                 return False;
1327         }
1328         
1329         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0,
1330                                       FIRST_DESIRED_ACCESS, 
1331                                       FILE_ATTRIBUTE_ARCHIVE,
1332                                       NTCREATEX_SHARE_ACCESS_NONE, 
1333                                       NTCREATEX_DISP_OVERWRITE_IF, 
1334                                       0x4044, 0);
1335
1336         if (fnum1 == -1) {
1337                 printf("First open failed - %s\n", smbcli_errstr(cli1->tree));
1338                 return False;
1339         }
1340
1341         fnum2 = smbcli_nt_create_full(cli1->tree, fname, 0,
1342                                    SECOND_DESIRED_ACCESS, 0,
1343                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN, 
1344                                    0x200000, 0);
1345         if (fnum2 == -1) {
1346                 printf("second open failed - %s\n", smbcli_errstr(cli1->tree));
1347                 return False;
1348         }
1349         
1350         if (!torture_close_connection(cli1)) {
1351                 correct = False;
1352         }
1353         
1354         return correct;
1355 }
1356
1357
1358 /*
1359   see how many RPC pipes we can open at once
1360 */
1361 static BOOL run_pipe_number(void)
1362 {
1363         struct smbcli_state *cli1;
1364         const char *pipe_name = "\\WKSSVC";
1365         int fnum;
1366         int num_pipes = 0;
1367
1368         printf("starting pipenumber test\n");
1369         if (!torture_open_connection(&cli1)) {
1370                 return False;
1371         }
1372
1373         while(1) {
1374                 fnum = smbcli_nt_create_full(cli1->tree, pipe_name, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1375                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1376
1377                 if (fnum == -1) {
1378                         printf("Open of pipe %s failed with error (%s)\n", pipe_name, smbcli_errstr(cli1->tree));
1379                         break;
1380                 }
1381                 num_pipes++;
1382                 printf("%d\r", num_pipes);
1383                 fflush(stdout);
1384         }
1385
1386         printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
1387         torture_close_connection(cli1);
1388         return True;
1389 }
1390
1391
1392
1393
1394 /*
1395   open N connections to the server and just hold them open
1396   used for testing performance when there are N idle users
1397   already connected
1398  */
1399  static BOOL torture_holdcon(void)
1400 {
1401         int i;
1402         struct smbcli_state **cli;
1403         int num_dead = 0;
1404
1405         printf("Opening %d connections\n", torture_numops);
1406         
1407         cli = malloc_array_p(struct smbcli_state *, torture_numops);
1408
1409         for (i=0;i<torture_numops;i++) {
1410                 if (!torture_open_connection(&cli[i])) {
1411                         return False;
1412                 }
1413                 printf("opened %d connections\r", i);
1414                 fflush(stdout);
1415         }
1416
1417         printf("\nStarting pings\n");
1418
1419         while (1) {
1420                 for (i=0;i<torture_numops;i++) {
1421                         NTSTATUS status;
1422                         if (cli[i]) {
1423                                 status = smbcli_chkpath(cli[i]->tree, "\\");
1424                                 if (!NT_STATUS_IS_OK(status)) {
1425                                         printf("Connection %d is dead\n", i);
1426                                         cli[i] = NULL;
1427                                         num_dead++;
1428                                 }
1429                                 usleep(100);
1430                         }
1431                 }
1432
1433                 if (num_dead == torture_numops) {
1434                         printf("All connections dead - finishing\n");
1435                         break;
1436                 }
1437
1438                 printf(".");
1439                 fflush(stdout);
1440         }
1441
1442         return True;
1443 }
1444
1445 /*
1446   Try with a wrong vuid and check error message.
1447  */
1448
1449 static BOOL run_vuidtest(void)
1450 {
1451         struct smbcli_state *cli;
1452         const char *fname = "\\vuid.tst";
1453         int fnum;
1454         size_t size;
1455         time_t c_time, a_time, m_time;
1456         BOOL correct = True;
1457
1458         uint16_t orig_vuid;
1459         NTSTATUS result;
1460
1461         printf("starting vuid test\n");
1462
1463         if (!torture_open_connection(&cli)) {
1464                 return False;
1465         }
1466
1467         smbcli_unlink(cli->tree, fname);
1468
1469         fnum = smbcli_open(cli->tree, fname, 
1470                         O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
1471
1472         orig_vuid = cli->session->vuid;
1473
1474         cli->session->vuid += 1234;
1475
1476         printf("Testing qfileinfo with wrong vuid\n");
1477         
1478         if (NT_STATUS_IS_OK(result = smbcli_qfileinfo(cli->tree, fnum, NULL,
1479                                                    &size, &c_time, &a_time,
1480                                                    &m_time, NULL, NULL))) {
1481                 printf("ERROR: qfileinfo passed with wrong vuid\n");
1482                 correct = False;
1483         }
1484
1485         if ( (cli->transport->error.etype != ETYPE_DOS) ||
1486              (cli->transport->error.e.dos.eclass != ERRSRV) ||
1487              (cli->transport->error.e.dos.ecode != ERRbaduid) ) {
1488                 printf("ERROR: qfileinfo should have returned DOS error "
1489                        "ERRSRV:ERRbaduid\n  but returned %s\n",
1490                        smbcli_errstr(cli->tree));
1491                 correct = False;
1492         }
1493
1494         cli->session->vuid -= 1234;
1495
1496         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
1497                 printf("close failed (%s)\n", smbcli_errstr(cli->tree));
1498                 correct = False;
1499         }
1500
1501         smbcli_unlink(cli->tree, fname);
1502
1503         if (!torture_close_connection(cli)) {
1504                 correct = False;
1505         }
1506
1507         printf("vuid test finished\n");
1508
1509         return correct;
1510 }
1511
1512 /*
1513   Test open mode returns on read-only files.
1514  */
1515  static BOOL run_opentest(void)
1516 {
1517         static struct smbcli_state *cli1;
1518         static struct smbcli_state *cli2;
1519         const char *fname = "\\readonly.file";
1520         int fnum1, fnum2;
1521         uint8_t buf[20];
1522         size_t fsize;
1523         BOOL correct = True;
1524         char *tmp_path;
1525         int failures = 0;
1526
1527         printf("starting open test\n");
1528         
1529         if (!torture_open_connection(&cli1)) {
1530                 return False;
1531         }
1532         
1533         smbcli_setatr(cli1->tree, fname, 0, 0);
1534         smbcli_unlink(cli1->tree, fname);
1535         
1536         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1537         if (fnum1 == -1) {
1538                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1539                 return False;
1540         }
1541
1542         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1543                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
1544                 return False;
1545         }
1546         
1547         if (NT_STATUS_IS_ERR(smbcli_setatr(cli1->tree, fname, FILE_ATTRIBUTE_READONLY, 0))) {
1548                 printf("smbcli_setatr failed (%s)\n", smbcli_errstr(cli1->tree));
1549                 CHECK_MAX_FAILURES(error_test1);
1550                 return False;
1551         }
1552         
1553         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
1554         if (fnum1 == -1) {
1555                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1556                 CHECK_MAX_FAILURES(error_test1);
1557                 return False;
1558         }
1559         
1560         /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
1561         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
1562         
1563         if (check_error(__location__, cli1, ERRDOS, ERRnoaccess, 
1564                         NT_STATUS_ACCESS_DENIED)) {
1565                 printf("correct error code ERRDOS/ERRnoaccess returned\n");
1566         }
1567         
1568         printf("finished open test 1\n");
1569 error_test1:
1570         smbcli_close(cli1->tree, fnum1);
1571         
1572         /* Now try not readonly and ensure ERRbadshare is returned. */
1573         
1574         smbcli_setatr(cli1->tree, fname, 0, 0);
1575         
1576         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY, DENY_WRITE);
1577         if (fnum1 == -1) {
1578                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1579                 return False;
1580         }
1581         
1582         /* This will fail - but the error should be ERRshare. */
1583         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR, DENY_ALL);
1584         
1585         if (check_error(__location__, cli1, ERRDOS, ERRbadshare, 
1586                         NT_STATUS_SHARING_VIOLATION)) {
1587                 printf("correct error code ERRDOS/ERRbadshare returned\n");
1588         }
1589         
1590         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1591                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
1592                 return False;
1593         }
1594         
1595         smbcli_unlink(cli1->tree, fname);
1596         
1597         printf("finished open test 2\n");
1598         
1599         /* Test truncate open disposition on file opened for read. */
1600         
1601         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1602         if (fnum1 == -1) {
1603                 printf("(3) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1604                 return False;
1605         }
1606         
1607         /* write 20 bytes. */
1608         
1609         memset(buf, '\0', 20);
1610
1611         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
1612                 printf("write failed (%s)\n", smbcli_errstr(cli1->tree));
1613                 correct = False;
1614         }
1615
1616         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1617                 printf("(3) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
1618                 return False;
1619         }
1620         
1621         /* Ensure size == 20. */
1622         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1623                 printf("(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
1624                 CHECK_MAX_FAILURES(error_test3);
1625                 return False;
1626         }
1627         
1628         if (fsize != 20) {
1629                 printf("(3) file size != 20\n");
1630                 CHECK_MAX_FAILURES(error_test3);
1631                 return False;
1632         }
1633
1634         /* Now test if we can truncate a file opened for readonly. */
1635         
1636         fnum1 = smbcli_open(cli1->tree, fname, O_RDONLY|O_TRUNC, DENY_NONE);
1637         if (fnum1 == -1) {
1638                 printf("(3) open (2) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1639                 CHECK_MAX_FAILURES(error_test3);
1640                 return False;
1641         }
1642         
1643         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1644                 printf("close2 failed (%s)\n", smbcli_errstr(cli1->tree));
1645                 return False;
1646         }
1647
1648         /* Ensure size == 0. */
1649         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1650                 printf("(3) getatr failed (%s)\n", smbcli_errstr(cli1->tree));
1651                 CHECK_MAX_FAILURES(error_test3);
1652                 return False;
1653         }
1654
1655         if (fsize != 0) {
1656                 printf("(3) file size != 0\n");
1657                 CHECK_MAX_FAILURES(error_test3);
1658                 return False;
1659         }
1660         printf("finished open test 3\n");
1661 error_test3:    
1662         smbcli_unlink(cli1->tree, fname);
1663
1664
1665         printf("testing ctemp\n");
1666         fnum1 = smbcli_ctemp(cli1->tree, "\\", &tmp_path);
1667         if (fnum1 == -1) {
1668                 printf("ctemp failed (%s)\n", smbcli_errstr(cli1->tree));
1669                 CHECK_MAX_FAILURES(error_test4);
1670                 return False;
1671         }
1672         printf("ctemp gave path %s\n", tmp_path);
1673         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1674                 printf("close of temp failed (%s)\n", smbcli_errstr(cli1->tree));
1675         }
1676         if (NT_STATUS_IS_ERR(smbcli_unlink(cli1->tree, tmp_path))) {
1677                 printf("unlink of temp failed (%s)\n", smbcli_errstr(cli1->tree));
1678         }
1679 error_test4:    
1680         /* Test the non-io opens... */
1681
1682         if (!torture_open_connection(&cli2)) {
1683                 return False;
1684         }
1685         
1686         smbcli_setatr(cli2->tree, fname, 0, 0);
1687         smbcli_unlink(cli2->tree, fname);
1688         
1689         printf("TEST #1 testing 2 non-io opens (no delete)\n");
1690         
1691         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1692                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1693
1694         if (fnum1 == -1) {
1695                 printf("test 1 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1696                 CHECK_MAX_FAILURES(error_test10);
1697                 return False;
1698         }
1699
1700         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1701                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1702         if (fnum2 == -1) {
1703                 printf("test 1 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1704                 CHECK_MAX_FAILURES(error_test10);
1705                 return False;
1706         }
1707
1708         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1709                 printf("test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1710                 return False;
1711         }
1712         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1713                 printf("test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1714                 return False;
1715         }
1716
1717         printf("non-io open test #1 passed.\n");
1718 error_test10:
1719         smbcli_unlink(cli1->tree, fname);
1720
1721         printf("TEST #2 testing 2 non-io opens (first with delete)\n");
1722         
1723         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1724                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1725
1726         if (fnum1 == -1) {
1727                 printf("test 2 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1728                 CHECK_MAX_FAILURES(error_test20);
1729                 return False;
1730         }
1731
1732         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1733                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1734
1735         if (fnum2 == -1) {
1736                 printf("test 2 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1737                 CHECK_MAX_FAILURES(error_test20);
1738                 return False;
1739         }
1740
1741         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1742                 printf("test 1 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1743                 return False;
1744         }
1745         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1746                 printf("test 1 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1747                 return False;
1748         }
1749
1750         printf("non-io open test #2 passed.\n");
1751 error_test20:
1752         smbcli_unlink(cli1->tree, fname);
1753
1754         printf("TEST #3 testing 2 non-io opens (second with delete)\n");
1755         
1756         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1757                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1758
1759         if (fnum1 == -1) {
1760                 printf("test 3 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1761                 CHECK_MAX_FAILURES(error_test30);
1762                 return False;
1763         }
1764
1765         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1766                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1767
1768         if (fnum2 == -1) {
1769                 printf("test 3 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1770                 CHECK_MAX_FAILURES(error_test30);
1771                 return False;
1772         }
1773
1774         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1775                 printf("test 3 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1776                 return False;
1777         }
1778         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1779                 printf("test 3 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1780                 return False;
1781         }
1782
1783         printf("non-io open test #3 passed.\n");
1784 error_test30:
1785         smbcli_unlink(cli1->tree, fname);
1786
1787         printf("TEST #4 testing 2 non-io opens (both with delete)\n");
1788         
1789         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1790                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1791
1792         if (fnum1 == -1) {
1793                 printf("test 4 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1794                 CHECK_MAX_FAILURES(error_test40);
1795                 return False;
1796         }
1797
1798         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1799                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1800
1801         if (fnum2 != -1) {
1802                 printf("test 4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1803                 CHECK_MAX_FAILURES(error_test40);
1804                 return False;
1805         }
1806
1807         printf("test 4 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1808
1809         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1810                 printf("test 4 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1811                 return False;
1812         }
1813
1814         printf("non-io open test #4 passed.\n");
1815 error_test40:
1816         smbcli_unlink(cli1->tree, fname);
1817
1818         printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
1819         
1820         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1821                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1822
1823         if (fnum1 == -1) {
1824                 printf("test 5 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1825                 CHECK_MAX_FAILURES(error_test50);
1826                 return False;
1827         }
1828
1829         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1830                                    NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1831
1832         if (fnum2 == -1) {
1833                 printf("test 5 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1834                 CHECK_MAX_FAILURES(error_test50);
1835                 return False;
1836         }
1837
1838         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1839                 printf("test 5 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1840                 return False;
1841         }
1842
1843         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1844                 printf("test 5 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1845                 return False;
1846         }
1847
1848         printf("non-io open test #5 passed.\n");
1849 error_test50:
1850         printf("TEST #6 testing 1 non-io open, one io open\n");
1851         
1852         smbcli_unlink(cli1->tree, fname);
1853
1854         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1855                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1856
1857         if (fnum1 == -1) {
1858                 printf("test 6 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1859                 CHECK_MAX_FAILURES(error_test60);
1860                 return False;
1861         }
1862
1863         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1864                                    NTCREATEX_SHARE_ACCESS_READ, NTCREATEX_DISP_OPEN_IF, 0, 0);
1865
1866         if (fnum2 == -1) {
1867                 printf("test 6 open 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1868                 CHECK_MAX_FAILURES(error_test60);
1869                 return False;
1870         }
1871
1872         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1873                 printf("test 6 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1874                 return False;
1875         }
1876
1877         if (NT_STATUS_IS_ERR(smbcli_close(cli2->tree, fnum2))) {
1878                 printf("test 6 close 2 of %s failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1879                 return False;
1880         }
1881
1882         printf("non-io open test #6 passed.\n");
1883 error_test60:
1884         printf("TEST #7 testing 1 non-io open, one io open with delete\n");
1885
1886         smbcli_unlink(cli1->tree, fname);
1887
1888         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
1889                                    NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
1890
1891         if (fnum1 == -1) {
1892                 printf("test 7 open 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1893                 CHECK_MAX_FAILURES(error_test70);
1894                 return False;
1895         }
1896
1897         fnum2 = smbcli_nt_create_full(cli2->tree, fname, 0, SEC_STD_DELETE|SEC_FILE_READ_ATTRIBUTE, FILE_ATTRIBUTE_NORMAL,
1898                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE, NTCREATEX_DISP_OPEN_IF, 0, 0);
1899
1900         if (fnum2 != -1) {
1901                 printf("test 7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, smbcli_errstr(cli2->tree));
1902                 CHECK_MAX_FAILURES(error_test70);
1903                 return False;
1904         }
1905
1906         printf("test 7 open 2 of %s gave %s (correct error should be %s)\n", fname, smbcli_errstr(cli2->tree), "sharing violation");
1907
1908         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1909                 printf("test 7 close 1 of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1910                 return False;
1911         }
1912
1913         printf("non-io open test #7 passed.\n");
1914
1915 error_test70:
1916
1917         printf("TEST #8 testing one normal open, followed by lock, followed by open with truncate\n");
1918
1919         smbcli_unlink(cli1->tree, fname);
1920
1921         fnum1 = smbcli_open(cli1->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1922         if (fnum1 == -1) {
1923                 printf("(8) open (1) of %s failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1924                 return False;
1925         }
1926         
1927         /* write 20 bytes. */
1928         
1929         memset(buf, '\0', 20);
1930
1931         if (smbcli_write(cli1->tree, fnum1, 0, buf, 0, 20) != 20) {
1932                 printf("(8) write failed (%s)\n", smbcli_errstr(cli1->tree));
1933                 correct = False;
1934         }
1935
1936         /* Ensure size == 20. */
1937         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1938                 printf("(8) getatr (1) failed (%s)\n", smbcli_errstr(cli1->tree));
1939                 CHECK_MAX_FAILURES(error_test80);
1940                 return False;
1941         }
1942         
1943         if (fsize != 20) {
1944                 printf("(8) file size != 20\n");
1945                 CHECK_MAX_FAILURES(error_test80);
1946                 return False;
1947         }
1948
1949         /* Get an exclusive lock on the open file. */
1950         if (NT_STATUS_IS_ERR(smbcli_lock(cli1->tree, fnum1, 0, 4, 0, WRITE_LOCK))) {
1951                 printf("(8) lock1 failed (%s)\n", smbcli_errstr(cli1->tree));
1952                 CHECK_MAX_FAILURES(error_test80);
1953                 return False;
1954         }
1955
1956         fnum2 = smbcli_open(cli1->tree, fname, O_RDWR|O_TRUNC, DENY_NONE);
1957         if (fnum1 == -1) {
1958                 printf("(8) open (2) of %s with truncate failed (%s)\n", fname, smbcli_errstr(cli1->tree));
1959                 return False;
1960         }
1961
1962         /* Ensure size == 0. */
1963         if (NT_STATUS_IS_ERR(smbcli_getatr(cli1->tree, fname, NULL, &fsize, NULL))) {
1964                 printf("(8) getatr (2) failed (%s)\n", smbcli_errstr(cli1->tree));
1965                 CHECK_MAX_FAILURES(error_test80);
1966                 return False;
1967         }
1968         
1969         if (fsize != 0) {
1970                 printf("(8) file size != 0\n");
1971                 CHECK_MAX_FAILURES(error_test80);
1972                 return False;
1973         }
1974
1975         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum1))) {
1976                 printf("(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
1977                 return False;
1978         }
1979         
1980         if (NT_STATUS_IS_ERR(smbcli_close(cli1->tree, fnum2))) {
1981                 printf("(8) close1 failed (%s)\n", smbcli_errstr(cli1->tree));
1982                 return False;
1983         }
1984         
1985 error_test80:
1986
1987         printf("open test #8 passed.\n");
1988
1989         smbcli_unlink(cli1->tree, fname);
1990
1991         if (!torture_close_connection(cli1)) {
1992                 correct = False;
1993         }
1994         if (!torture_close_connection(cli2)) {
1995                 correct = False;
1996         }
1997         
1998         return correct;
1999 }
2000
2001
2002 /*
2003   sees what IOCTLs are supported
2004  */
2005 BOOL torture_ioctl_test(void)
2006 {
2007         struct smbcli_state *cli;
2008         uint16_t device, function;
2009         int fnum;
2010         const char *fname = "\\ioctl.dat";
2011         NTSTATUS status;
2012         union smb_ioctl parms;
2013         TALLOC_CTX *mem_ctx;
2014
2015         if (!torture_open_connection(&cli)) {
2016                 return False;
2017         }
2018
2019         mem_ctx = talloc_init("ioctl_test");
2020
2021         printf("starting ioctl test\n");
2022
2023         smbcli_unlink(cli->tree, fname);
2024
2025         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2026         if (fnum == -1) {
2027                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
2028                 return False;
2029         }
2030
2031         parms.ioctl.level = RAW_IOCTL_IOCTL;
2032         parms.ioctl.in.fnum = fnum;
2033         parms.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
2034         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
2035         printf("ioctl job info: %s\n", smbcli_errstr(cli->tree));
2036
2037         for (device=0;device<0x100;device++) {
2038                 printf("testing device=0x%x\n", device);
2039                 for (function=0;function<0x100;function++) {
2040                         parms.ioctl.in.request = (device << 16) | function;
2041                         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
2042
2043                         if (NT_STATUS_IS_OK(status)) {
2044                                 printf("ioctl device=0x%x function=0x%x OK : %d bytes\n", 
2045                                         device, function, parms.ioctl.out.blob.length);
2046                         }
2047                 }
2048         }
2049
2050         if (!torture_close_connection(cli)) {
2051                 return False;
2052         }
2053
2054         return True;
2055 }
2056
2057
2058 /*
2059   tries variants of chkpath
2060  */
2061 BOOL torture_chkpath_test(void)
2062 {
2063         struct smbcli_state *cli;
2064         int fnum;
2065         BOOL ret;
2066
2067         if (!torture_open_connection(&cli)) {
2068                 return False;
2069         }
2070
2071         printf("starting chkpath test\n");
2072
2073         printf("Testing valid and invalid paths\n");
2074
2075         /* cleanup from an old run */
2076         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
2077         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
2078         smbcli_rmdir(cli->tree, "\\chkpath.dir");
2079
2080         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir"))) {
2081                 printf("mkdir1 failed : %s\n", smbcli_errstr(cli->tree));
2082                 return False;
2083         }
2084
2085         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\chkpath.dir\\dir2"))) {
2086                 printf("mkdir2 failed : %s\n", smbcli_errstr(cli->tree));
2087                 return False;
2088         }
2089
2090         fnum = smbcli_open(cli->tree, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2091         if (fnum == -1) {
2092                 printf("open1 failed (%s)\n", smbcli_errstr(cli->tree));
2093                 return False;
2094         }
2095         smbcli_close(cli->tree, fnum);
2096
2097         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir"))) {
2098                 printf("chkpath1 failed: %s\n", smbcli_errstr(cli->tree));
2099                 ret = False;
2100         }
2101
2102         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dir2"))) {
2103                 printf("chkpath2 failed: %s\n", smbcli_errstr(cli->tree));
2104                 ret = False;
2105         }
2106
2107         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\foo.txt"))) {
2108                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
2109                                   NT_STATUS_NOT_A_DIRECTORY);
2110         } else {
2111                 printf("* chkpath on a file should fail\n");
2112                 ret = False;
2113         }
2114
2115         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\bar.txt"))) {
2116                 ret = check_error(__location__, cli, ERRDOS, ERRbadfile, 
2117                                   NT_STATUS_OBJECT_NAME_NOT_FOUND);
2118         } else {
2119                 printf("* chkpath on a non existent file should fail\n");
2120                 ret = False;
2121         }
2122
2123         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, "\\chkpath.dir\\dirxx\\bar.txt"))) {
2124                 ret = check_error(__location__, cli, ERRDOS, ERRbadpath, 
2125                                   NT_STATUS_OBJECT_PATH_NOT_FOUND);
2126         } else {
2127                 printf("* chkpath on a non existent component should fail\n");
2128                 ret = False;
2129         }
2130
2131         smbcli_rmdir(cli->tree, "\\chkpath.dir\\dir2");
2132         smbcli_unlink(cli->tree, "\\chkpath.dir\\*");
2133         smbcli_rmdir(cli->tree, "\\chkpath.dir");
2134
2135         if (!torture_close_connection(cli)) {
2136                 return False;
2137         }
2138
2139         return ret;
2140 }
2141
2142
2143 static void sigcont(int sig)
2144 {
2145 }
2146
2147 double torture_create_procs(BOOL (*fn)(struct smbcli_state *, int), BOOL *result)
2148 {
2149         int i, status;
2150         volatile pid_t *child_status;
2151         volatile BOOL *child_status_out;
2152         int synccount;
2153         int tries = 8;
2154         double start_time_limit = 10 + (torture_nprocs * 1.5);
2155         char **unc_list = NULL;
2156         const char *p;
2157         int num_unc_names = 0;
2158         struct timeval tv;
2159
2160         *result = True;
2161
2162         synccount = 0;
2163
2164         signal(SIGCONT, sigcont);
2165
2166         child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*torture_nprocs);
2167         if (!child_status) {
2168                 printf("Failed to setup shared memory\n");
2169                 return -1;
2170         }
2171
2172         child_status_out = (volatile BOOL *)shm_setup(sizeof(BOOL)*torture_nprocs);
2173         if (!child_status_out) {
2174                 printf("Failed to setup result status shared memory\n");
2175                 return -1;
2176         }
2177
2178         p = lp_parm_string(-1, "torture", "unclist");
2179         if (p) {
2180                 unc_list = file_lines_load(p, &num_unc_names);
2181                 if (!unc_list || num_unc_names <= 0) {
2182                         printf("Failed to load unc names list from '%s'\n", p);
2183                         exit(1);
2184                 }
2185         }
2186
2187         for (i = 0; i < torture_nprocs; i++) {
2188                 child_status[i] = 0;
2189                 child_status_out[i] = True;
2190         }
2191
2192         tv = timeval_current();
2193
2194         for (i=0;i<torture_nprocs;i++) {
2195                 procnum = i;
2196                 if (fork() == 0) {
2197                         char *myname;
2198                         const char *hostname=NULL, *sharename;
2199
2200                         pid_t mypid = getpid();
2201                         srandom(((int)mypid) ^ ((int)time(NULL)));
2202
2203                         asprintf(&myname, "CLIENT%d", i);
2204                         lp_set_cmdline("netbios name", myname);
2205                         free(myname);
2206
2207
2208                         if (unc_list) {
2209                                 if (!smbcli_parse_unc(unc_list[i % num_unc_names],
2210                                                       NULL, &hostname, &sharename)) {
2211                                         printf("Failed to parse UNC name %s\n",
2212                                                unc_list[i % num_unc_names]);
2213                                         exit(1);
2214                                 }
2215                         }
2216
2217                         while (1) {
2218                                 if (hostname) {
2219                                         if (torture_open_connection_share(&current_cli,
2220                                                                           hostname, 
2221                                                                           sharename)) {
2222                                                 break;
2223                                         }
2224                                 } else if (torture_open_connection(&current_cli)) {
2225                                                 break;
2226                                 }
2227                                 if (tries-- == 0) {
2228                                         printf("pid %d failed to start\n", (int)getpid());
2229                                         _exit(1);
2230                                 }
2231                                 msleep(100);    
2232                         }
2233
2234                         child_status[i] = getpid();
2235
2236                         pause();
2237
2238                         if (child_status[i]) {
2239                                 printf("Child %d failed to start!\n", i);
2240                                 child_status_out[i] = 1;
2241                                 _exit(1);
2242                         }
2243
2244                         child_status_out[i] = fn(current_cli, i);
2245                         _exit(0);
2246                 }
2247         }
2248
2249         do {
2250                 synccount = 0;
2251                 for (i=0;i<torture_nprocs;i++) {
2252                         if (child_status[i]) synccount++;
2253                 }
2254                 if (synccount == torture_nprocs) break;
2255                 msleep(100);
2256         } while (timeval_elapsed(&tv) < start_time_limit);
2257
2258         if (synccount != torture_nprocs) {
2259                 printf("FAILED TO START %d CLIENTS (started %d)\n", torture_nprocs, synccount);
2260                 *result = False;
2261                 return timeval_elapsed(&tv);
2262         }
2263
2264         printf("Starting %d clients\n", torture_nprocs);
2265
2266         /* start the client load */
2267         tv = timeval_current();
2268         for (i=0;i<torture_nprocs;i++) {
2269                 child_status[i] = 0;
2270         }
2271
2272         printf("%d clients started\n", torture_nprocs);
2273
2274         kill(0, SIGCONT);
2275
2276         for (i=0;i<torture_nprocs;i++) {
2277                 int ret;
2278                 while ((ret=sys_waitpid(0, &status, 0)) == -1 && errno == EINTR) /* noop */ ;
2279                 if (ret == -1 || WEXITSTATUS(status) != 0) {
2280                         *result = False;
2281                 }
2282         }
2283
2284         printf("\n");
2285         
2286         for (i=0;i<torture_nprocs;i++) {
2287                 if (!child_status_out[i]) {
2288                         *result = False;
2289                 }
2290         }
2291         return timeval_elapsed(&tv);
2292 }
2293
2294 #define FLAG_MULTIPROC 1
2295
2296 static struct {
2297         const char *name;
2298         BOOL (*fn)(void);
2299         BOOL (*multi_fn)(struct smbcli_state *, int );
2300 } torture_ops[] = {
2301         /* base tests */
2302         {"BASE-FDPASS", run_fdpasstest, 0},
2303         {"BASE-LOCK1",  torture_locktest1,  0},
2304         {"BASE-LOCK2",  torture_locktest2,  0},
2305         {"BASE-LOCK3",  torture_locktest3,  0},
2306         {"BASE-LOCK4",  torture_locktest4,  0},
2307         {"BASE-LOCK5",  torture_locktest5,  0},
2308         {"BASE-LOCK6",  torture_locktest6,  0},
2309         {"BASE-LOCK7",  torture_locktest7,  0},
2310         {"BASE-UNLINK", torture_unlinktest, 0},
2311         {"BASE-ATTR",   run_attrtest,   0},
2312         {"BASE-TRANS2", run_trans2test, 0},
2313         {"BASE-NEGNOWAIT", run_negprot_nowait, 0},
2314         {"BASE-DIR1",  torture_dirtest1, 0},
2315         {"BASE-DIR2",  torture_dirtest2, 0},
2316         {"BASE-DENY1",  torture_denytest1, 0},
2317         {"BASE-DENY2",  torture_denytest2, 0},
2318         {"BASE-DENY3",  torture_denytest3, 0},
2319         {"BASE-DENYDOS",  torture_denydos_sharing, 0},
2320         {"BASE-NTDENY1",  NULL, torture_ntdenytest1},
2321         {"BASE-NTDENY2",  torture_ntdenytest2, 0},
2322         {"BASE-TCON",  run_tcon_test, 0},
2323         {"BASE-TCONDEV",  run_tcon_devtype_test, 0},
2324         {"BASE-VUID", run_vuidtest, 0},
2325         {"BASE-RW1",  run_readwritetest, 0},
2326         {"BASE-RW2",  NULL, run_readwritemulti},
2327         {"BASE-OPEN", run_opentest, 0},
2328         {"BASE-DEFER_OPEN", NULL, run_deferopen},
2329         {"BASE-XCOPY", run_xcopy, 0},
2330         {"BASE-RENAME", torture_test_rename, 0},
2331         {"BASE-DELETE", torture_test_delete, 0},
2332         {"BASE-PROPERTIES", torture_test_properties, 0},
2333         {"BASE-MANGLE", torture_mangle, 0},
2334         {"BASE-OPENATTR", torture_openattrtest, 0},
2335         {"BASE-CHARSET", torture_charset, 0},
2336         {"BASE-CHKPATH",  torture_chkpath_test, 0},
2337         {"BASE-SECLEAK",  torture_sec_leak, 0},
2338         {"BASE-DISCONNECT",  torture_disconnect, 0},
2339         {"BASE-DELAYWRITE", torture_delay_write, 0},
2340
2341         /* benchmarking tests */
2342         {"BENCH-HOLDCON",  torture_holdcon, 0},
2343         {"BENCH-NBENCH",  torture_nbench, 0},
2344         {"BENCH-TORTURE", NULL, run_torture},
2345
2346         /* RAW smb tests */
2347         {"RAW-QFSINFO", torture_raw_qfsinfo, 0},
2348         {"RAW-QFILEINFO", torture_raw_qfileinfo, 0},
2349         {"RAW-SFILEINFO", torture_raw_sfileinfo, 0},
2350         {"RAW-SFILEINFO-BUG", torture_raw_sfileinfo_bug, 0},
2351         {"RAW-SEARCH", torture_raw_search, 0},
2352         {"RAW-CLOSE", torture_raw_close, 0},
2353         {"RAW-OPEN", torture_raw_open, 0},
2354         {"RAW-MKDIR", torture_raw_mkdir, 0},
2355         {"RAW-OPLOCK", torture_raw_oplock, 0},
2356         {"RAW-NOTIFY", torture_raw_notify, 0},
2357         {"RAW-MUX", torture_raw_mux, 0},
2358         {"RAW-IOCTL", torture_raw_ioctl, 0},
2359         {"RAW-CHKPATH", torture_raw_chkpath, 0},
2360         {"RAW-UNLINK", torture_raw_unlink, 0},
2361         {"RAW-READ", torture_raw_read, 0},
2362         {"RAW-WRITE", torture_raw_write, 0},
2363         {"RAW-LOCK", torture_raw_lock, 0},
2364         {"RAW-CONTEXT", torture_raw_context, 0},
2365         {"RAW-RENAME", torture_raw_rename, 0},
2366         {"RAW-SEEK", torture_raw_seek, 0},
2367         {"RAW-EAS", torture_raw_eas, 0},
2368         {"RAW-STREAMS", torture_raw_streams, 0},
2369         {"RAW-ACLS", torture_raw_acls, 0},
2370         {"RAW-RAP", torture_raw_rap, 0},
2371         {"RAW-COMPOSITE", torture_raw_composite, 0},
2372
2373         /* protocol scanners */
2374         {"SCAN-TRANS2", torture_trans2_scan, 0},
2375         {"SCAN-NTTRANS", torture_nttrans_scan, 0},
2376         {"SCAN-ALIASES", torture_trans2_aliases, 0},
2377         {"SCAN-SMB", torture_smb_scan, 0},
2378         {"SCAN-MAXFID", NULL, run_maxfidtest},
2379         {"SCAN-UTABLE", torture_utable, 0},
2380         {"SCAN-CASETABLE", torture_casetable, 0},
2381         {"SCAN-PIPE_NUMBER", run_pipe_number, 0},
2382         {"SCAN-IOCTL",  torture_ioctl_test, 0},
2383
2384         /* rpc testers */
2385         {"RPC-LSA", torture_rpc_lsa, 0},
2386         {"RPC-ECHO", torture_rpc_echo, 0},
2387         {"RPC-DFS", torture_rpc_dfs, 0},
2388         {"RPC-SPOOLSS", torture_rpc_spoolss, 0},
2389         {"RPC-SAMR", torture_rpc_samr, 0},
2390         {"RPC-NETLOGON", torture_rpc_netlogon, 0},
2391         {"RPC-SAMLOGON", torture_rpc_samlogon, 0},
2392         {"RPC-SAMSYNC", torture_rpc_samsync, 0},
2393         {"RPC-SCHANNEL", torture_rpc_schannel, 0},
2394         {"RPC-WKSSVC", torture_rpc_wkssvc, 0},
2395         {"RPC-SRVSVC", torture_rpc_srvsvc, 0},
2396         {"RPC-SVCCTL", torture_rpc_svcctl, 0},
2397         {"RPC-ATSVC", torture_rpc_atsvc, 0},
2398         {"RPC-EVENTLOG", torture_rpc_eventlog, 0},
2399         {"RPC-EPMAPPER", torture_rpc_epmapper, 0},
2400         {"RPC-WINREG", torture_rpc_winreg, 0},
2401         {"RPC-OXIDRESOLVE", torture_rpc_oxidresolve, 0},
2402         {"RPC-REMACT", torture_rpc_remact, 0},
2403         {"RPC-MGMT", torture_rpc_mgmt, 0},
2404         {"RPC-SCANNER", torture_rpc_scanner, 0},
2405         {"RPC-AUTOIDL", torture_rpc_autoidl, 0},
2406         {"RPC-COUNTCALLS", torture_rpc_countcalls, 0},
2407         {"RPC-MULTIBIND", torture_multi_bind, 0},
2408         {"RPC-DRSUAPI", torture_rpc_drsuapi, 0},
2409         {"RPC-LOGIN", torture_rpc_login, 0},
2410         {"RPC-ROT", torture_rpc_rot, 0},
2411         {"RPC-DSSETUP", torture_rpc_dssetup, 0},
2412         {"RPC-ALTERCONTEXT", torture_rpc_alter_context, 0},
2413
2414         /* Distributed COM testers */
2415         {"DCOM-SIMPLE", torture_dcom_simple, 0},
2416
2417         /* local (no server) testers */
2418         {"LOCAL-NTLMSSP", torture_ntlmssp_self_check, 0},
2419         {"LOCAL-ICONV", torture_local_iconv, 0},
2420         {"LOCAL-TALLOC", torture_local_talloc, 0},
2421         {"LOCAL-MESSAGING", torture_local_messaging, 0},
2422         {"LOCAL-BINDING", torture_local_binding_string, 0},
2423         {"LOCAL-IDTREE", torture_local_idtree, 0},
2424         {"LOCAL-SOCKET", torture_local_socket, 0},
2425
2426         /* ldap testers */
2427         {"LDAP-BASIC", torture_ldap_basic, 0},
2428
2429         {NULL, NULL, 0}};
2430
2431
2432
2433 /****************************************************************************
2434 run a specified test or "ALL"
2435 ****************************************************************************/
2436 static BOOL run_test(const char *name)
2437 {
2438         BOOL ret = True;
2439         int i;
2440         BOOL matched = False;
2441
2442         if (strequal(name,"ALL")) {
2443                 for (i=0;torture_ops[i].name;i++) {
2444                         if (!run_test(torture_ops[i].name)) {
2445                                 ret = False;
2446                         }
2447                 }
2448                 return ret;
2449         }
2450
2451         for (i=0;torture_ops[i].name;i++) {
2452                 if (gen_fnmatch(name, torture_ops[i].name) == 0) {
2453                         double t;
2454                         matched = True;
2455                         init_iconv();
2456                         printf("Running %s\n", torture_ops[i].name);
2457                         if (torture_ops[i].multi_fn) {
2458                                 BOOL result = False;
2459                                 t = torture_create_procs(torture_ops[i].multi_fn, 
2460                                                          &result);
2461                                 if (!result) { 
2462                                         ret = False;
2463                                         printf("TEST %s FAILED!\n", torture_ops[i].name);
2464                                 }
2465                                          
2466                         } else {
2467                                 struct timeval tv = timeval_current();
2468                                 if (!torture_ops[i].fn()) {
2469                                         ret = False;
2470                                         printf("TEST %s FAILED!\n", torture_ops[i].name);
2471                                 }
2472                                 t = timeval_elapsed(&tv);
2473                         }
2474                         printf("%s took %g secs\n\n", torture_ops[i].name, t);
2475                 }
2476         }
2477
2478         if (!matched) {
2479                 printf("Unknown torture operation '%s'\n", name);
2480         }
2481
2482         return ret;
2483 }
2484
2485
2486 static void parse_dns(const char *dns)
2487 {
2488         char *userdn, *basedn, *secret;
2489         char *p, *d;
2490
2491         /* retrievieng the userdn */
2492         p = strchr_m(dns, '#');
2493         if (!p) {
2494                 lp_set_cmdline("torture:ldap_userdn", "");
2495                 lp_set_cmdline("torture:ldap_basedn", "");
2496                 lp_set_cmdline("torture:ldap_secret", "");
2497                 return;
2498         }
2499         userdn = strndup(dns, p - dns);
2500         lp_set_cmdline("torture:ldap_userdn", userdn);
2501
2502         /* retrieve the basedn */
2503         d = p + 1;
2504         p = strchr_m(d, '#');
2505         if (!p) {
2506                 lp_set_cmdline("torture:ldap_basedn", "");
2507                 lp_set_cmdline("torture:ldap_secret", "");
2508                 return;
2509         }
2510         basedn = strndup(d, p - d);
2511         lp_set_cmdline("torture:ldap_basedn", basedn);
2512
2513         /* retrieve the secret */
2514         p = p + 1;
2515         if (!p) {
2516                 lp_set_cmdline("torture:ldap_secret", "");
2517                 return;
2518         }
2519         secret = strdup(p);
2520         lp_set_cmdline("torture:ldap_secret", secret);
2521
2522         printf ("%s - %s - %s\n", userdn, basedn, secret);
2523
2524 }
2525
2526 static void usage(poptContext pc)
2527 {
2528         int i;
2529         int perline = 5;
2530
2531         poptPrintUsage(pc, stdout, 0);
2532         printf("\n");
2533
2534         printf("The binding format is:\n\n");
2535
2536         printf("  TRANSPORT:host[flags]\n\n");
2537
2538         printf("  where TRANSPORT is either ncacn_np for SMB or ncacn_ip_tcp for RPC/TCP\n\n");
2539
2540         printf("  'host' is an IP or hostname or netbios name. If the binding string\n");
2541         printf("  identifies the server side of an endpoint, 'host' may be an empty\n");
2542         printf("  string.\n\n");
2543
2544         printf("  'flags' can include a SMB pipe name if using the ncacn_np transport or\n");
2545         printf("  a TCP port number if using the ncacn_ip_tcp transport, otherwise they\n");
2546         printf("  will be auto-determined.\n\n");
2547
2548         printf("  other recognised flags are:\n\n");
2549
2550         printf("    sign : enable ntlmssp signing\n");
2551         printf("    seal : enable ntlmssp sealing\n");
2552         printf("    connect : enable rpc connect level auth (auth, but no sign or seal)\n");
2553         printf("    validate: enable the NDR validator\n");
2554         printf("    print: enable debugging of the packets\n");
2555         printf("    bigendian: use bigendian RPC\n");
2556         printf("    padcheck: check reply data for non-zero pad bytes\n\n");
2557
2558         printf("  For example, these all connect to the samr pipe:\n\n");
2559
2560         printf("    ncacn_np:myserver\n");
2561         printf("    ncacn_np:myserver[samr]\n");
2562         printf("    ncacn_np:myserver[\\pipe\\samr]\n");
2563         printf("    ncacn_np:myserver[/pipe/samr]\n");
2564         printf("    ncacn_np:myserver[samr,sign,print]\n");
2565         printf("    ncacn_np:myserver[\\pipe\\samr,sign,seal,bigendian]\n");
2566         printf("    ncacn_np:myserver[/pipe/samr,seal,validate]\n");
2567         printf("    ncacn_np:\n");
2568         printf("    ncacn_np:[/pipe/samr]\n\n");
2569
2570         printf("    ncacn_ip_tcp:myserver\n");
2571         printf("    ncacn_ip_tcp:myserver[1024]\n");
2572         printf("    ncacn_ip_tcp:myserver[1024,sign,seal]\n\n");
2573
2574         printf("The unc format is:\n\n");
2575
2576         printf("    //server/share\n\n");
2577
2578         printf("tests are:");
2579         for (i=0;torture_ops[i].name;i++) {
2580                 if ((i%perline)==0) {
2581                         printf("\n");
2582                 }
2583                 printf("%s ", torture_ops[i].name);
2584         }
2585         printf("\n\n");
2586
2587         printf("default test is ALL\n");
2588
2589         exit(1);
2590 }
2591
2592 static BOOL is_binding_string(const char *binding_string)
2593 {
2594         TALLOC_CTX *mem_ctx = talloc_init("is_binding_string");
2595         struct dcerpc_binding binding_struct;
2596         NTSTATUS status;
2597         
2598         status = dcerpc_parse_binding(mem_ctx, binding_string, &binding_struct);
2599
2600         talloc_destroy(mem_ctx);
2601         return NT_STATUS_IS_OK(status);
2602 }
2603
2604 /****************************************************************************
2605   main program
2606 ****************************************************************************/
2607  int main(int argc,char *argv[])
2608 {
2609         int opt, i;
2610         char *p;
2611         BOOL correct = True;
2612         int argc_new;
2613         char **argv_new;
2614         poptContext pc;
2615         enum {OPT_LOADFILE=1000,OPT_UNCLIST,OPT_TIMELIMIT,OPT_DNS,OPT_DANGEROUS};
2616         struct poptOption long_options[] = {
2617                 POPT_AUTOHELP
2618                 {"smb-ports",   'p', POPT_ARG_STRING, NULL,             0,      "SMB ports",    NULL},
2619                 {"seed",          0, POPT_ARG_INT,  &torture_seed,      0,      "seed",         NULL},
2620                 {"num-progs",     0, POPT_ARG_INT,  &torture_nprocs,    0,      "num progs",    NULL},
2621                 {"num-ops",       0, POPT_ARG_INT,  &torture_numops,    0,      "num ops",      NULL},
2622                 {"entries",       0, POPT_ARG_INT,  &torture_entries,   0,      "entries",      NULL},
2623                 {"use-oplocks", 'L', POPT_ARG_NONE, &use_oplocks,       0,      "use oplocks",  NULL},
2624                 {"show-all",      0, POPT_ARG_NONE, &torture_showall,   0,      "show all",     NULL},
2625                 {"loadfile",      0, POPT_ARG_STRING,   NULL,   OPT_LOADFILE,   "loadfile",     NULL},
2626                 {"unclist",       0, POPT_ARG_STRING,   NULL,   OPT_UNCLIST,    "unclist",      NULL},
2627                 {"timelimit",   't', POPT_ARG_STRING,   NULL,   OPT_TIMELIMIT,  "timelimit",    NULL},
2628                 {"failures",    'f', POPT_ARG_INT,  &torture_failures,  0,      "failures",     NULL},
2629                 {"parse-dns",   'D', POPT_ARG_STRING,   NULL,   OPT_DNS,        "parse-dns",    NULL},
2630                 {"dangerous",   'X', POPT_ARG_NONE,     NULL,   OPT_DANGEROUS,  "dangerous",    NULL},
2631                 POPT_COMMON_SAMBA
2632                 POPT_COMMON_CONNECTION
2633                 POPT_COMMON_CREDENTIALS
2634                 POPT_COMMON_VERSION
2635                 POPT_TABLEEND
2636         };
2637
2638         setup_logging("smbtorture", DEBUG_STDOUT);
2639
2640 #ifdef HAVE_SETBUFFER
2641         setbuffer(stdout, NULL, 0);
2642 #endif
2643
2644         pc = poptGetContext("smbtorture", argc, (const char **) argv, long_options, 
2645                                 POPT_CONTEXT_KEEP_FIRST);
2646
2647         poptSetOtherOptionHelp(pc, "<binding>|<unc> TEST1 TEST2 ...");
2648
2649         while((opt = poptGetNextOpt(pc)) != -1) {
2650                 switch (opt) {
2651                 case OPT_LOADFILE:
2652                         lp_set_cmdline("torture:loadfile", poptGetOptArg(pc));
2653                         break;
2654                 case OPT_UNCLIST:
2655                         lp_set_cmdline("torture:unclist", poptGetOptArg(pc));
2656                         break;
2657                 case OPT_TIMELIMIT:
2658                         lp_set_cmdline("torture:timelimit", poptGetOptArg(pc));
2659                         break;
2660                 case OPT_DNS:
2661                         parse_dns(poptGetOptArg(pc));
2662                         break;
2663                 case OPT_DANGEROUS:
2664                         lp_set_cmdline("torture:dangerous", "Yes");
2665                         break;
2666                 default:
2667                         d_printf("Invalid option %s: %s\n", 
2668                                  poptBadOption(pc, 0), poptStrerror(opt));
2669                         usage(pc);
2670                         exit(1);
2671                 }
2672         }
2673
2674         lp_load(dyn_CONFIGFILE,True,False,False);
2675         load_interfaces();
2676
2677         smbtorture_init_subsystems;
2678
2679
2680         if (torture_seed == 0) {
2681                 torture_seed = time(NULL);
2682         } 
2683         printf("Using seed %d\n", torture_seed);
2684         srandom(torture_seed);
2685
2686         argv_new = discard_const_p(char *, poptGetArgs(pc));
2687
2688         argc_new = argc;
2689         for (i=0; i<argc; i++) {
2690                 if (argv_new[i] == NULL) {
2691                         argc_new = i;
2692                         break;
2693                 }
2694         }
2695
2696         if (argc_new < 3) {
2697                 usage(pc);
2698                 exit(1);
2699         }
2700
2701         for(p = argv_new[1]; *p; p++) {
2702                 if(*p == '\\')
2703                         *p = '/';
2704         }
2705
2706         /* see if its a RPC transport specifier */
2707         if (is_binding_string(argv_new[1])) {
2708                 lp_set_cmdline("torture:binding", argv_new[1]);
2709         } else {
2710                 char *binding = NULL;
2711                 const char *host = NULL, *share = NULL;
2712
2713                 if (!smbcli_parse_unc(argv_new[1], NULL, &host, &share)) {
2714                         d_printf("Invalid option: %s is not a valid torture target (share or binding string)\n\n", argv_new[1]);
2715                         usage(pc);
2716                 }
2717
2718                 lp_set_cmdline("torture:host", host);
2719                 lp_set_cmdline("torture:share", share);
2720                 asprintf(&binding, "ncacn_np:%s", host);
2721                 lp_set_cmdline("torture:binding", binding);
2722         }
2723
2724         if (!lp_parm_string(-1,"torture","username")) {
2725                 lp_set_cmdline("torture:username", cmdline_get_username());
2726         }
2727         if (!lp_parm_string(-1,"torture","userdomain")) {
2728                 /* 
2729                  * backward compatibility
2730                  * maybe we should remove this to make this consistent
2731                  * for all cmdline tools
2732                  * --metze
2733                  */
2734                 if (strequal(lp_netbios_name(),cmdline_get_userdomain())) {
2735                         cmdline_set_userdomain(lp_workgroup());
2736                 }
2737                 lp_set_cmdline("torture:userdomain", cmdline_get_userdomain());
2738         }
2739         if (!lp_parm_string(-1,"torture","password")) {
2740                 lp_set_cmdline("torture:password", cmdline_get_userpassword());
2741         }
2742
2743         if (argc_new == 0) {
2744                 printf("You must specify a test to run, or 'ALL'\n");
2745         } else {
2746                 for (i=2;i<argc_new;i++) {
2747                         if (!run_test(argv_new[i])) {
2748                                 correct = False;
2749                         }
2750                 }
2751         }
2752
2753         if (correct) {
2754                 return(0);
2755         } else {
2756                 return(1);
2757         }
2758 }