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