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