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