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