Fix typo.
[ab/samba.git/.git] / source4 / torture / gentest_smb2.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    generic testing tool - version with SMB2 support
5
6    Copyright (C) Andrew Tridgell 2003-2008
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "lib/events/events.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
27 #include "libcli/raw/request.h"
28 #include "libcli/libcli.h"
29 #include "libcli/raw/libcliraw.h"
30 #include "libcli/smb2/smb2.h"
31 #include "libcli/smb2/smb2_calls.h"
32 #include "librpc/gen_ndr/security.h"
33 #include "auth/credentials/credentials.h"
34 #include "libcli/resolve/resolve.h"
35 #include "auth/gensec/gensec.h"
36 #include "param/param.h"
37 #include "dynconfig.h"
38
39 #define NSERVERS 2
40 #define NINSTANCES 2
41
42 /* global options */
43 static struct gentest_options {
44         int showall;
45         int analyze;
46         int analyze_always;
47         int analyze_continuous;
48         uint_t max_open_handles;
49         uint_t seed;
50         uint_t numops;
51         int use_oplocks;
52         char **ignore_patterns;
53         const char *seeds_file;
54         int use_preset_seeds;
55         int fast_reconnect;
56 } options;
57
58 /* mapping between open handles on the server and local handles */
59 static struct {
60         bool active;
61         uint_t instance;
62         struct smb2_handle server_handle[NSERVERS];
63         const char *name;
64 } *open_handles;
65 static uint_t num_open_handles;
66
67 /* state information for the servers. We open NINSTANCES connections to
68    each server */
69 static struct {
70         struct smb2_tree *tree[NINSTANCES];
71         char *server_name;
72         char *share_name;
73         struct cli_credentials *credentials;
74 } servers[NSERVERS];
75
76 /* the seeds and flags for each operation */
77 static struct {
78         uint_t seed;
79         bool disabled;
80 } *op_parms;
81
82
83 /* oplock break info */
84 static struct {
85         bool got_break;
86         struct smb2_handle server_handle;
87         uint16_t handle;
88         uint8_t level;
89         bool do_close;
90 } oplocks[NSERVERS][NINSTANCES];
91
92 /* change notify reply info */
93 static struct {
94         int notify_count;
95         NTSTATUS status;
96         union smb_notify notify;
97 } notifies[NSERVERS][NINSTANCES];
98
99 /* info relevant to the current operation */
100 static struct {
101         const char *name;
102         uint_t seed;
103         NTSTATUS status;
104         uint_t opnum;
105         TALLOC_CTX *mem_ctx;
106 } current_op;
107
108 static struct smb2_handle bad_smb2_handle;
109
110
111 #define BAD_HANDLE 0xFFFE
112
113 static bool oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private);
114 static void idle_func(struct smb2_transport *transport, void *private);
115
116 /*
117   check if a string should be ignored. This is used as the basis
118   for all error ignore settings
119 */
120 static bool ignore_pattern(const char *str)
121 {
122         int i;
123         if (!options.ignore_patterns) return false;
124
125         for (i=0;options.ignore_patterns[i];i++) {
126                 if (strcmp(options.ignore_patterns[i], str) == 0 ||
127                     gen_fnmatch(options.ignore_patterns[i], str) == 0) {
128                         DEBUG(2,("Ignoring '%s'\n", str));
129                         return true;
130                 }
131         }
132         return false;
133 }
134
135 /***************************************************** 
136 connect to the servers
137 *******************************************************/
138 static bool connect_servers_fast(void)
139 {
140         int h, i;
141
142         /* close all open files */
143         for (h=0;h<options.max_open_handles;h++) {
144                 if (!open_handles[h].active) continue;
145                 for (i=0;i<NSERVERS;i++) {
146                         NTSTATUS status = smb2_util_close(servers[i].tree[open_handles[h].instance],
147                                                           open_handles[h].server_handle[i]);
148                         if (NT_STATUS_IS_ERR(status)) {
149                                 return false;
150                         }
151                         open_handles[h].active = false;
152                 }
153         }
154
155         return true;
156 }
157
158
159
160
161 /***************************************************** 
162 connect to the servers
163 *******************************************************/
164 static bool connect_servers(struct event_context *ev,
165                             struct loadparm_context *lp_ctx)
166 {
167         int i, j;
168
169         if (options.fast_reconnect && servers[0].tree[0]) {
170                 if (connect_servers_fast()) {
171                         return true;
172                 }
173         }
174
175         /* close any existing connections */
176         for (i=0;i<NSERVERS;i++) {
177                 for (j=0;j<NINSTANCES;j++) {
178                         if (servers[i].tree[j]) {
179                                 smb2_tdis(servers[i].tree[j]);
180                                 talloc_free(servers[i].tree[j]);
181                                 servers[i].tree[j] = NULL;
182                         }
183                 }
184         }
185
186         for (i=0;i<NSERVERS;i++) {
187                 for (j=0;j<NINSTANCES;j++) {
188                         NTSTATUS status;
189                         printf("Connecting to \\\\%s\\%s as %s - instance %d\n",
190                                servers[i].server_name, servers[i].share_name, 
191                                servers[i].credentials->username, j);
192
193                         cli_credentials_set_workstation(servers[i].credentials, 
194                                                         "gentest", CRED_SPECIFIED);
195
196                         status = smb2_connect(NULL, servers[i].server_name, 
197                                               servers[i].share_name,
198                                               lp_resolve_context(lp_ctx),
199                                               servers[i].credentials,
200                                               &servers[i].tree[j],
201                                               ev);
202                         if (!NT_STATUS_IS_OK(status)) {
203                                 printf("Failed to connect to \\\\%s\\%s - %s\n",
204                                        servers[i].server_name, servers[i].share_name,
205                                        nt_errstr(status));
206                                 return false;
207                         }
208
209 //                      smb2_oplock_handler(servers[i].cli[j]->transport, oplock_handler, NULL);
210                         smb2_transport_idle_handler(servers[i].tree[j]->session->transport, idle_func, 50000, NULL);
211                 }
212         }
213
214         return true;
215 }
216
217 /*
218   work out the time skew between the servers - be conservative
219 */
220 static uint_t time_skew(void)
221 {
222         uint_t ret;
223         ret = labs(servers[0].tree[0]->session->transport->negotiate.system_time -
224                   servers[1].tree[0]->session->transport->negotiate.system_time);
225         return ret + 300;
226 }
227
228
229 static bool smb2_handle_equal(const struct smb2_handle *h1, const struct smb2_handle *h2)
230 {
231         return memcmp(h1, h2, sizeof(struct smb2_handle)) == 0;
232 }
233
234 /*
235   turn a server handle into a local handle
236 */
237 static uint_t fnum_to_handle(int server, int instance, struct smb2_handle server_handle)
238 {
239         uint_t i;
240         for (i=0;i<options.max_open_handles;i++) {
241                 if (!open_handles[i].active ||
242                     instance != open_handles[i].instance) continue;
243                 if (smb2_handle_equal(&open_handles[i].server_handle[server], &server_handle)) {
244                         return i;
245                 }
246         }
247         printf("Invalid server handle in fnum_to_handle on server %d instance %d\n", 
248                server, instance);
249         return BAD_HANDLE;
250 }
251
252 /*
253   add some newly opened handles
254 */
255 static void gen_add_handle(int instance, const char *name, struct smb2_handle handles[NSERVERS])
256 {
257         int i, h;
258         for (h=0;h<options.max_open_handles;h++) {
259                 if (!open_handles[h].active) break;
260         }
261         if (h == options.max_open_handles) {
262                 /* we have to force close a random handle */
263                 h = random() % options.max_open_handles;
264                 for (i=0;i<NSERVERS;i++) {
265                         NTSTATUS status;
266                         status = smb2_util_close(servers[i].tree[open_handles[h].instance], 
267                                                  open_handles[h].server_handle[i]);
268                         if (NT_STATUS_IS_ERR(status)) {
269                                 printf("INTERNAL ERROR: Close failed when recovering handle! - %s\n",
270                                        nt_errstr(status));
271                         }
272                 }
273                 printf("Recovered handle %d\n", h);
274                 num_open_handles--;
275         }
276         for (i=0;i<NSERVERS;i++) {
277                 open_handles[h].server_handle[i] = handles[i];
278                 open_handles[h].instance = instance;
279                 open_handles[h].active = true;
280                 open_handles[h].name = name;
281         }
282         num_open_handles++;
283
284         printf("OPEN num_open_handles=%d h=%d (%s)\n", 
285                num_open_handles, h, name);
286 }
287
288 /*
289   remove a closed handle
290 */
291 static void gen_remove_handle(int instance, struct smb2_handle handles[NSERVERS])
292 {
293         int h;
294         for (h=0;h<options.max_open_handles;h++) {
295                 if (instance == open_handles[h].instance &&
296                     smb2_handle_equal(&open_handles[h].server_handle[0], &handles[0])) {
297                         open_handles[h].active = false;                 
298                         num_open_handles--;
299                         printf("CLOSE num_open_handles=%d h=%d (%s)\n", 
300                                num_open_handles, h, 
301                                open_handles[h].name);
302                         return;
303                 }
304         }
305         printf("Removing invalid handle!?\n");
306         exit(1);
307 }
308
309 /*
310   return true with 'chance' probability as a percentage
311 */
312 static bool gen_chance(uint_t chance)
313 {
314         return ((random() % 100) <= chance);
315 }
316
317 /*
318   map an internal handle number to a server handle
319 */
320 static struct smb2_handle gen_lookup_handle(int server, uint16_t handle)
321 {
322         if (handle == BAD_HANDLE) return bad_smb2_handle;
323         return open_handles[handle].server_handle[server];
324 }
325
326 /*
327   return a file handle
328 */
329 static uint16_t gen_fnum(int instance)
330 {
331         uint16_t h;
332         int count = 0;
333
334         if (gen_chance(20)) return BAD_HANDLE;
335
336         while (num_open_handles > 0 && count++ < 10*options.max_open_handles) {
337                 h = random() % options.max_open_handles;
338                 if (open_handles[h].active && 
339                     open_handles[h].instance == instance) {
340                         return h;
341                 }
342         }
343         return BAD_HANDLE;
344 }
345
346 /*
347   return a file handle, but skewed so we don't close the last
348   couple of handles too readily
349 */
350 static uint16_t gen_fnum_close(int instance)
351 {
352         if (num_open_handles < 3) {
353                 if (gen_chance(80)) return BAD_HANDLE;
354         }
355
356         return gen_fnum(instance);
357 }
358
359 /*
360   generate an integer in a specified range
361 */
362 static int gen_int_range(uint64_t min, uint64_t max)
363 {
364         uint_t r = random();
365         return min + (r % (1+max-min));
366 }
367
368 /*
369   return a fnum for use as a root fid
370   be careful to call GEN_SET_FNUM() when you use this!
371 */
372 static uint16_t gen_root_fid(int instance)
373 {
374         if (gen_chance(5)) return gen_fnum(instance);
375         return 0;
376 }
377
378 /*
379   generate a file offset
380 */
381 static int gen_offset(void)
382 {
383         if (gen_chance(20)) return 0;
384 //      if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF);
385         return gen_int_range(0, 1024*1024);
386 }
387
388 /*
389   generate a io count
390 */
391 static int gen_io_count(void)
392 {
393         if (gen_chance(20)) return 0;
394 //      if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF);
395         return gen_int_range(0, 4096);
396 }
397
398 /*
399   generate a filename
400 */
401 static const char *gen_fname(void)
402 {
403         const char *names[] = {"gentest\\gentest.dat", 
404                                "gentest\\foo", 
405                                "gentest\\foo2.sym", 
406                                "gentest\\foo3.dll", 
407                                "gentest\\foo4", 
408                                "gentest\\foo4:teststream1", 
409                                "gentest\\foo4:teststream2", 
410                                "gentest\\foo5.exe", 
411                                "gentest\\foo5.exe:teststream3", 
412                                "gentest\\foo5.exe:teststream4", 
413                                "gentest\\foo6.com", 
414                                "gentest\\blah", 
415                                "gentest\\blah\\blergh.txt", 
416                                "gentest\\blah\\blergh2", 
417                                "gentest\\blah\\blergh3.txt", 
418                                "gentest\\blah\\blergh4", 
419                                "gentest\\blah\\blergh5.txt", 
420                                "gentest\\blah\\blergh5", 
421                                "gentest\\blah\\.", 
422 #if 0
423                                /* this causes problem with w2k3 */
424                                "gentest\\blah\\..", 
425 #endif
426                                "gentest\\a_very_long_name.bin", 
427                                "gentest\\x.y", 
428                                "gentest\\blah"};
429         int i;
430
431         do {
432                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
433         } while (ignore_pattern(names[i]));
434
435         return names[i];
436 }
437
438 /*
439   generate a filename with a higher chance of choosing an already 
440   open file
441 */
442 static const char *gen_fname_open(int instance)
443 {
444         uint16_t h;
445         h = gen_fnum(instance);
446         if (h == BAD_HANDLE) {
447                 return gen_fname();
448         }
449         return open_handles[h].name;
450 }
451
452 /*
453   generate a wildcard pattern
454 */
455 static const char *gen_pattern(void)
456 {
457         int i;
458         const char *names[] = {"gentest\\*.dat", 
459                                "gentest\\*", 
460                                "gentest\\*.*", 
461                                "gentest\\blah\\*.*", 
462                                "gentest\\blah\\*", 
463                                "gentest\\?"};
464
465         if (gen_chance(50)) return gen_fname();
466
467         do {
468                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
469         } while (ignore_pattern(names[i]));
470
471         return names[i];
472 }
473
474 static uint32_t gen_bits_levels(int nlevels, ...)
475 {
476         va_list ap;
477         uint32_t pct;
478         uint32_t mask;
479         int i;
480         va_start(ap, nlevels);
481         for (i=0;i<nlevels;i++) {
482                 pct = va_arg(ap, uint32_t);
483                 mask = va_arg(ap, uint32_t);
484                 if (pct == 100 || gen_chance(pct)) {
485                         va_end(ap);
486                         return mask & random();
487                 }
488         }
489         va_end(ap);
490         return 0;
491 }
492
493 /*
494   generate a bitmask
495 */
496 static uint32_t gen_bits_mask(uint_t mask)
497 {
498         uint_t ret = random();
499         return ret & mask;
500 }
501
502 /*
503   generate a bitmask with high probability of the first mask
504   and low of the second
505 */
506 static uint32_t gen_bits_mask2(uint32_t mask1, uint32_t mask2)
507 {
508         if (gen_chance(10)) return gen_bits_mask(mask2);
509         return gen_bits_mask(mask1);
510 }
511
512 /*
513   generate a boolean
514 */
515 static bool gen_bool(void)
516 {
517         return gen_bits_mask2(0x1, 0xFF);
518 }
519
520 /*
521   generate ntrename flags
522 */
523 static uint16_t gen_rename_flags(void)
524 {
525         if (gen_chance(30)) return RENAME_FLAG_RENAME;
526         if (gen_chance(30)) return RENAME_FLAG_HARD_LINK;
527         if (gen_chance(30)) return RENAME_FLAG_COPY;
528         return gen_bits_mask(0xFFFF);
529 }
530
531
532 /*
533   return a lockingx lock mode
534 */
535 static uint16_t gen_lock_mode(void)
536 {
537         if (gen_chance(5))  return gen_bits_mask(0xFFFF);
538         if (gen_chance(20)) return gen_bits_mask(0x1F);
539         return gen_bits_mask(LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES);
540 }
541
542 /*
543   generate a pid 
544 */
545 static uint16_t gen_pid(void)
546 {
547         if (gen_chance(10)) return gen_bits_mask(0xFFFF);
548         return getpid();
549 }
550
551 /*
552   generate a lock count
553 */
554 static off_t gen_lock_count(void)
555 {
556         return gen_int_range(0, 3);
557 }
558
559 /*
560   generate a ntcreatex flags field
561 */
562 static uint32_t gen_ntcreatex_flags(void)
563 {
564         if (gen_chance(70)) return NTCREATEX_FLAGS_EXTENDED;
565         return gen_bits_mask2(0x1F, 0xFFFFFFFF);
566 }
567
568 /*
569   generate a NT access mask
570 */
571 static uint32_t gen_access_mask(void)
572 {
573         if (gen_chance(50)) return SEC_FLAG_MAXIMUM_ALLOWED;
574         if (gen_chance(20)) return SEC_FILE_ALL;
575         return gen_bits_mask(0xFFFFFFFF);
576 }
577
578 /*
579   generate a ntcreatex create options bitfield
580 */
581 static uint32_t gen_create_options(void)
582 {
583         if (gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
584         if (gen_chance(50)) return 0;
585         return gen_bits_mask(NTCREATEX_OPTIONS_DELETE_ON_CLOSE | NTCREATEX_OPTIONS_DIRECTORY);
586 }
587
588 /*
589   generate a ntcreatex open disposition
590 */
591 static uint32_t gen_open_disp(void)
592 {
593         if (gen_chance(10)) return gen_bits_mask(0xFFFFFFFF);
594         return gen_int_range(0, 5);
595 }
596
597 /*
598   generate an openx open mode
599 */
600 static uint16_t gen_openx_mode(void)
601 {
602         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
603         if (gen_chance(20)) return gen_bits_mask(0xFF);
604         return OPENX_MODE_DENY_NONE | gen_bits_mask(0x3);
605 }
606
607 /*
608   generate an openx flags field
609 */
610 static uint16_t gen_openx_flags(void)
611 {
612         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
613         return gen_bits_mask(0x7);
614 }
615
616 /*
617   generate an openx open function
618 */
619 static uint16_t gen_openx_func(void)
620 {
621         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
622         return gen_bits_mask(0x13);
623 }
624
625 /*
626   generate a file attrib combination
627 */
628 static uint32_t gen_attrib(void)
629 {
630         if (gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
631         return gen_bits_mask(FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
632 }
633
634 /*
635   generate a unix timestamp
636 */
637 static time_t gen_timet(void)
638 {
639         if (gen_chance(30)) return 0;
640         return (time_t)random();
641 }
642
643 /*
644   generate a unix timestamp
645 */
646 static NTTIME gen_nttime(void)
647 {
648         NTTIME ret;
649         unix_to_nt_time(&ret, gen_timet());
650         return ret;
651 }
652
653 /*
654   generate a milliseconds protocol timeout
655 */
656 static uint32_t gen_timeout(void)
657 {
658         if (gen_chance(98)) return 0;
659         return random() % 50;
660 }
661
662 /*
663   generate a file allocation size
664 */
665 static uint_t gen_alloc_size(void)
666 {
667         uint_t ret;
668
669         if (gen_chance(30)) return 0;
670
671         ret = random() % 4*1024*1024;
672         /* give a high chance of a round number */
673         if (gen_chance(60)) {
674                 ret &= ~(1024*1024 - 1);
675         }
676         return ret;
677 }
678
679 /*
680   generate an ea_struct
681 */
682 static struct ea_struct gen_ea_struct(void)
683 {
684         struct ea_struct ea;
685         const char *names[] = {"EAONE", 
686                                "", 
687                                "FOO!", 
688                                " WITH SPACES ", 
689                                ".", 
690                                "AVERYLONGATTRIBUTENAME"};
691         const char *values[] = {"VALUE1", 
692                                "", 
693                                "NOT MUCH FOO", 
694                                " LEADING SPACES ", 
695                                ":", 
696                                "ASOMEWHATLONGERATTRIBUTEVALUE"};
697         int i;
698
699         ZERO_STRUCT(ea);
700
701         do {
702                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
703         } while (ignore_pattern(names[i]));
704
705         ea.name.s = names[i];
706
707         do {
708                 i = gen_int_range(0, ARRAY_SIZE(values)-1);
709         } while (ignore_pattern(values[i]));
710
711         ea.value = data_blob(values[i], strlen(values[i]));
712
713         if (gen_chance(10)) ea.flags = gen_bits_mask(0xFF);
714         ea.flags = 0;
715
716         return ea;
717 }
718
719 /*
720   the idle function tries to cope with getting an oplock break on a connection, and
721   an operation on another connection blocking until that break is acked
722   we check for operations on all transports in the idle function
723 */
724 static void idle_func(struct smb2_transport *transport, void *private)
725 {
726         int i, j;
727         for (i=0;i<NSERVERS;i++) {
728                 for (j=0;j<NINSTANCES;j++) {
729                         if (servers[i].tree[j] &&
730                             transport != servers[i].tree[j]->session->transport) {
731                                 // smb2_transport_process(servers[i].tree[j]->session->transport);
732                         }
733                 }
734         }
735
736 }
737
738
739 /*
740   compare NTSTATUS, using checking ignored patterns
741 */
742 static bool compare_status(NTSTATUS status1, NTSTATUS status2)
743 {
744         if (NT_STATUS_EQUAL(status1, status2)) return true;
745
746         /* one code being an error and the other OK is always an error */
747         if (NT_STATUS_IS_OK(status1) || NT_STATUS_IS_OK(status2)) return false;
748
749         /* if we are ignoring one of the status codes then consider this a match */
750         if (ignore_pattern(nt_errstr(status1)) ||
751             ignore_pattern(nt_errstr(status2))) {
752                 return true;
753         }
754         return false;
755 }
756
757
758 /*
759   check for pending packets on all connections
760 */
761 static void check_pending(void)
762 {
763         int i, j;
764
765         msleep(20);
766
767         for (j=0;j<NINSTANCES;j++) {
768                 for (i=0;i<NSERVERS;i++) {
769                         // smb2_transport_process(servers[i].tree[j]->session->transport);
770                 }
771         }       
772 }
773
774 /*
775   check that the same oplock breaks have been received by all instances
776 */
777 static bool check_oplocks(const char *call)
778 {
779 #if 0
780         int i, j;
781         int tries = 0;
782
783 again:
784         check_pending();
785
786         for (j=0;j<NINSTANCES;j++) {
787                 for (i=1;i<NSERVERS;i++) {
788                         if (oplocks[0][j].got_break != oplocks[i][j].got_break ||
789                             oplocks[0][j].handle != oplocks[i][j].handle ||
790                             oplocks[0][j].level != oplocks[i][j].level) {
791                                 if (tries++ < 10) goto again;
792                                 printf("oplock break inconsistent - %d/%d/%d vs %d/%d/%d\n",
793                                        oplocks[0][j].got_break, 
794                                        oplocks[0][j].handle, 
795                                        oplocks[0][j].level, 
796                                        oplocks[i][j].got_break, 
797                                        oplocks[i][j].handle, 
798                                        oplocks[i][j].level);
799                                 return false;
800                         }
801                 }
802         }
803
804         /* if we got a break and closed then remove the handle */
805         for (j=0;j<NINSTANCES;j++) {
806                 if (oplocks[0][j].got_break &&
807                     oplocks[0][j].do_close) {
808                         uint16_t fnums[NSERVERS];
809                         for (i=0;i<NSERVERS;i++) {
810                                 fnums[i] = oplocks[i][j].fnum;
811                         }
812                         gen_remove_handle(j, fnums);
813                         break;
814                 }
815         }       
816 #endif
817         return true;
818 }
819
820
821 /*
822   check that the same change notify info has been received by all instances
823 */
824 static bool check_notifies(const char *call)
825 {
826 #if 0
827         int i, j;
828         int tries = 0;
829
830 again:
831         check_pending();
832
833         for (j=0;j<NINSTANCES;j++) {
834                 for (i=1;i<NSERVERS;i++) {
835                         int n;
836                         union smb_notify not1, not2;
837
838                         if (notifies[0][j].notify_count != notifies[i][j].notify_count) {
839                                 if (tries++ < 10) goto again;
840                                 printf("Notify count inconsistent %d %d\n",
841                                        notifies[0][j].notify_count,
842                                        notifies[i][j].notify_count);
843                                 return false;
844                         }
845
846                         if (notifies[0][j].notify_count == 0) continue;
847
848                         if (!NT_STATUS_EQUAL(notifies[0][j].status,
849                                              notifies[i][j].status)) {
850                                 printf("Notify status mismatch - %s - %s\n",
851                                        nt_errstr(notifies[0][j].status),
852                                        nt_errstr(notifies[i][j].status));
853                                 return false;
854                         }
855
856                         if (!NT_STATUS_IS_OK(notifies[0][j].status)) {
857                                 continue;
858                         }
859
860                         not1 = notifies[0][j].notify;
861                         not2 = notifies[i][j].notify;
862
863                         for (n=0;n<not1.nttrans.out.num_changes;n++) {
864                                 if (not1.nttrans.out.changes[n].action != 
865                                     not2.nttrans.out.changes[n].action) {
866                                         printf("Notify action %d inconsistent %d %d\n", n,
867                                                not1.nttrans.out.changes[n].action,
868                                                not2.nttrans.out.changes[n].action);
869                                         return false;
870                                 }
871                                 if (strcmp(not1.nttrans.out.changes[n].name.s,
872                                            not2.nttrans.out.changes[n].name.s)) {
873                                         printf("Notify name %d inconsistent %s %s\n", n,
874                                                not1.nttrans.out.changes[n].name.s,
875                                                not2.nttrans.out.changes[n].name.s);
876                                         return false;
877                                 }
878                                 if (not1.nttrans.out.changes[n].name.private_length !=
879                                     not2.nttrans.out.changes[n].name.private_length) {
880                                         printf("Notify name length %d inconsistent %d %d\n", n,
881                                                not1.nttrans.out.changes[n].name.private_length,
882                                                not2.nttrans.out.changes[n].name.private_length);
883                                         return false;
884                                 }
885                         }
886                 }
887         }
888
889         ZERO_STRUCT(notifies);
890
891 #endif
892         return true;
893 }
894
895 #define GEN_COPY_PARM do { \
896         int i; \
897         for (i=1;i<NSERVERS;i++) { \
898                 parm[i] = parm[0]; \
899         } \
900 } while (0)
901
902 #define GEN_CALL(call) do { \
903         int i; \
904         ZERO_STRUCT(oplocks); \
905         ZERO_STRUCT(notifies); \
906         for (i=0;i<NSERVERS;i++) { \
907                 struct smb2_tree *tree = servers[i].tree[instance]; \
908                 status[i] = call; \
909         } \
910         current_op.status = status[0]; \
911         for (i=1;i<NSERVERS;i++) { \
912                 if (!compare_status(status[i], status[0])) { \
913                         printf("status different in %s - %s %s\n", #call, \
914                                nt_errstr(status[0]), nt_errstr(status[i])); \
915                         return false; \
916                 } \
917         } \
918         if (!check_oplocks(#call)) return false;        \
919         if (!check_notifies(#call)) return false;       \
920         if (!NT_STATUS_IS_OK(status[0])) { \
921                 return true; \
922         } \
923 } while(0)
924
925 #define ADD_HANDLE(name, field) do { \
926         struct smb2_handle handles[NSERVERS]; \
927         int i; \
928         for (i=0;i<NSERVERS;i++) { \
929                 handles[i] = parm[i].field; \
930         } \
931         gen_add_handle(instance, name, handles); \
932 } while(0)
933
934 #define REMOVE_HANDLE(field) do { \
935         struct smb2_handle handles[NSERVERS]; \
936         int i; \
937         for (i=0;i<NSERVERS;i++) { \
938                 handles[i] = parm[i].field; \
939         } \
940         gen_remove_handle(instance, handles); \
941 } while(0)
942
943 #define GEN_SET_FNUM(field) do { \
944         int i; \
945         for (i=0;i<NSERVERS;i++) { \
946                 parm[i].field = gen_lookup_handle(i, parm[i].field.data[0]); \
947         } \
948 } while(0)
949
950 #define CHECK_EQUAL(field) do { \
951         if (parm[0].field != parm[1].field && !ignore_pattern(#field)) { \
952                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
953                        (int)parm[0].field, (int)parm[1].field); \
954                 return false; \
955         } \
956 } while(0)
957
958 #define CHECK_WSTR_EQUAL(field) do { \
959         if ((!parm[0].field.s && parm[1].field.s) || (parm[0].field.s && !parm[1].field.s)) { \
960                 printf("%s is NULL!\n", #field); \
961                 return false; \
962         } \
963         if (parm[0].field.s && strcmp(parm[0].field.s, parm[1].field.s) != 0 && !ignore_pattern(#field)) { \
964                 printf("Mismatch in %s - %s %s\n", #field, \
965                        parm[0].field.s, parm[1].field.s); \
966                 return false; \
967         } \
968         CHECK_EQUAL(field.private_length); \
969 } while(0)
970
971 #define CHECK_BLOB_EQUAL(field) do { \
972         if (memcmp(parm[0].field.data, parm[1].field.data, parm[0].field.length) != 0 && !ignore_pattern(#field)) { \
973                 printf("Mismatch in %s\n", #field); \
974                 return false; \
975         } \
976         CHECK_EQUAL(field.length); \
977 } while(0)
978
979 #define CHECK_TIMES_EQUAL(field) do { \
980         if (labs(parm[0].field - parm[1].field) > time_skew() && \
981             !ignore_pattern(#field)) { \
982                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
983                        (int)parm[0].field, (int)parm[1].field); \
984                 return false; \
985         } \
986 } while(0)
987
988 #define CHECK_NTTIMES_EQUAL(field) do { \
989         if (labs(nt_time_to_unix(parm[0].field) - \
990                 nt_time_to_unix(parm[1].field)) > time_skew() && \
991             !ignore_pattern(#field)) { \
992                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
993                        (int)nt_time_to_unix(parm[0].field), \
994                        (int)nt_time_to_unix(parm[1].field)); \
995                 return false; \
996         } \
997 } while(0)
998
999 /*
1000   generate ntcreatex operations
1001 */
1002 static bool handler_ntcreatex(int instance)
1003 {
1004         struct smb2_create parm[NSERVERS];
1005         NTSTATUS status[NSERVERS];
1006
1007         ZERO_STRUCT(parm[0]);
1008         parm[0].in.security_flags             = gen_bits_levels(3, 70, 0x0, 70, 0x3, 100, 0xFF);
1009         parm[0].in.oplock_level               = gen_bits_levels(3, 70, 0x0, 70, 0x9, 100, 0xFF);
1010         parm[0].in.impersonation_level        = gen_bits_levels(3, 70, 0x0, 70, 0x3, 100, 0xFFFFFFFF);
1011         parm[0].in.create_flags               = gen_bits_levels(2, 80, 0x0, 100, 0xFFFFFFFF);
1012         if (gen_chance(2)) {
1013                 parm[0].in.create_flags       |= gen_bits_mask(0xFFFFFFFF);
1014         }
1015         parm[0].in.reserved                   = gen_bits_levels(2, 80, 0x0, 100, 0xFFFFFFFF);
1016         if (gen_chance(2)) {
1017                 parm[0].in.reserved           |= gen_bits_mask(0xFFFFFFFF);
1018         }
1019         parm[0].in.desired_access             = gen_access_mask();
1020         parm[0].in.file_attributes            = gen_attrib();
1021         parm[0].in.share_access               = gen_bits_mask2(0x7, 0xFFFFFFFF);
1022         parm[0].in.create_disposition         = gen_open_disp();
1023         parm[0].in.create_options             = gen_create_options();
1024         parm[0].in.fname                      = gen_fname_open(instance);
1025
1026         if (!options.use_oplocks) {
1027                 /* mask out oplocks */
1028                 parm[0].in.oplock_level = 0;
1029         }
1030         
1031         GEN_COPY_PARM;
1032         GEN_CALL(smb2_create(tree, current_op.mem_ctx, &parm[i]));
1033
1034         CHECK_EQUAL(out.oplock_level);
1035         CHECK_EQUAL(out.reserved);
1036         CHECK_EQUAL(out.create_action);
1037         CHECK_NTTIMES_EQUAL(out.create_time);
1038         CHECK_NTTIMES_EQUAL(out.access_time);
1039         CHECK_NTTIMES_EQUAL(out.write_time);
1040         CHECK_NTTIMES_EQUAL(out.change_time);
1041         CHECK_EQUAL(out.alloc_size);
1042         CHECK_EQUAL(out.size);
1043         CHECK_EQUAL(out.file_attr);
1044         CHECK_EQUAL(out.reserved2);
1045
1046         /* ntcreatex creates a new file handle */
1047         ADD_HANDLE(parm[0].in.fname, out.file.handle);
1048
1049         return true;
1050 }
1051
1052 /*
1053   generate close operations
1054 */
1055 static bool handler_close(int instance)
1056 {
1057         struct smb2_close parm[NSERVERS];
1058         NTSTATUS status[NSERVERS];
1059
1060         ZERO_STRUCT(parm[0]);
1061         parm[0].in.file.handle.data[0] = gen_fnum_close(instance);
1062         parm[0].in.flags               = gen_bits_mask2(0x1, 0xFFFF);
1063
1064         GEN_COPY_PARM;
1065         GEN_SET_FNUM(in.file.handle);
1066         GEN_CALL(smb2_close(tree, &parm[i]));
1067
1068         CHECK_EQUAL(out.flags);
1069         CHECK_EQUAL(out._pad);
1070         CHECK_NTTIMES_EQUAL(out.create_time);
1071         CHECK_NTTIMES_EQUAL(out.access_time);
1072         CHECK_NTTIMES_EQUAL(out.write_time);
1073         CHECK_NTTIMES_EQUAL(out.change_time);
1074         CHECK_EQUAL(out.alloc_size);
1075         CHECK_EQUAL(out.size);
1076         CHECK_EQUAL(out.file_attr);
1077
1078         REMOVE_HANDLE(in.file.handle);
1079
1080         return true;
1081 }
1082
1083 /*
1084   generate read operations
1085 */
1086 static bool handler_read(int instance)
1087 {
1088         struct smb2_read parm[NSERVERS];
1089         NTSTATUS status[NSERVERS];
1090
1091         parm[0].in.file.handle.data[0] = gen_fnum(instance);
1092         parm[0].in.reserved    = gen_bits_mask2(0x0, 0xFF);
1093         parm[0].in.length      = gen_io_count();
1094         parm[0].in.offset      = gen_offset();
1095         parm[0].in.min_count   = gen_io_count();
1096         parm[0].in.channel     = gen_bits_mask2(0x0, 0xFFFFFFFF);
1097         parm[0].in.remaining   = gen_bits_mask2(0x0, 0xFFFFFFFF);
1098         parm[0].in.channel_offset = gen_bits_mask2(0x0, 0xFFFF);
1099         parm[0].in.channel_length = gen_bits_mask2(0x0, 0xFFFF);
1100
1101         GEN_COPY_PARM;
1102         GEN_SET_FNUM(in.file.handle);
1103         GEN_CALL(smb2_read(tree, current_op.mem_ctx, &parm[i]));
1104
1105         CHECK_EQUAL(out.remaining);
1106         CHECK_EQUAL(out.reserved);
1107         CHECK_EQUAL(out.data.length);
1108
1109         return true;
1110 }
1111
1112 /*
1113   generate write operations
1114 */
1115 static bool handler_write(int instance)
1116 {
1117         struct smb2_write parm[NSERVERS];
1118         NTSTATUS status[NSERVERS];
1119
1120         parm[0].in.file.handle.data[0] = gen_fnum(instance);
1121         parm[0].in.offset = gen_offset();
1122         parm[0].in.unknown1 = gen_bits_mask2(0, 0xFFFFFFFF);
1123         parm[0].in.unknown2 = gen_bits_mask2(0, 0xFFFFFFFF);
1124         parm[0].in.data = data_blob_talloc(current_op.mem_ctx, NULL,
1125                                             gen_io_count());
1126
1127         GEN_COPY_PARM;
1128         GEN_SET_FNUM(in.file.handle);
1129         GEN_CALL(smb2_write(tree, &parm[i]));
1130
1131         CHECK_EQUAL(out._pad);
1132         CHECK_EQUAL(out.nwritten);
1133         CHECK_EQUAL(out.unknown1);
1134
1135         return true;
1136 }
1137
1138 #if 0
1139 /*
1140   generate lockingx operations
1141 */
1142 static bool handler_lock(int instance)
1143 {
1144         union smb_lock parm[NSERVERS];
1145         NTSTATUS status[NSERVERS];
1146         int n, nlocks;
1147
1148         parm[0].lockx.level = RAW_LOCK_LOCKX;
1149         parm[0].lockx.in.file.fnum = gen_fnum(instance);
1150         parm[0].lockx.in.mode = gen_lock_mode();
1151         parm[0].lockx.in.timeout = gen_timeout();
1152         do {
1153                 /* make sure we don't accidentially generate an oplock
1154                    break ack - otherwise the server can just block forever */
1155                 parm[0].lockx.in.ulock_cnt = gen_lock_count();
1156                 parm[0].lockx.in.lock_cnt = gen_lock_count();
1157                 nlocks = parm[0].lockx.in.ulock_cnt + parm[0].lockx.in.lock_cnt;
1158         } while (nlocks == 0);
1159
1160         if (nlocks > 0) {
1161                 parm[0].lockx.in.locks = talloc_array(current_op.mem_ctx,
1162                                                         struct smb_lock_entry,
1163                                                         nlocks);
1164                 for (n=0;n<nlocks;n++) {
1165                         parm[0].lockx.in.locks[n].pid = gen_pid();
1166                         parm[0].lockx.in.locks[n].offset = gen_offset();
1167                         parm[0].lockx.in.locks[n].count = gen_io_count();
1168                 }
1169         }
1170
1171         GEN_COPY_PARM;
1172         GEN_SET_FNUM(lockx.in.file.fnum);
1173         GEN_CALL(smb_raw_lock(tree, &parm[i]));
1174
1175         return true;
1176 }
1177
1178 /*
1179   generate a fileinfo query structure
1180 */
1181 static void gen_fileinfo(int instance, union smb_fileinfo *info)
1182 {
1183         int i;
1184         #define LVL(v) {RAW_FILEINFO_ ## v, "RAW_FILEINFO_" #v}
1185         struct {
1186                 enum smb_fileinfo_level level;
1187                 const char *name;
1188         }  levels[] = {
1189                 LVL(GETATTR), LVL(GETATTRE), LVL(STANDARD),
1190                 LVL(EA_SIZE), LVL(ALL_EAS), LVL(IS_NAME_VALID),
1191                 LVL(BASIC_INFO), LVL(STANDARD_INFO), LVL(EA_INFO),
1192                 LVL(NAME_INFO), LVL(ALL_INFO), LVL(ALT_NAME_INFO),
1193                 LVL(STREAM_INFO), LVL(COMPRESSION_INFO), LVL(BASIC_INFORMATION),
1194                 LVL(STANDARD_INFORMATION), LVL(INTERNAL_INFORMATION), LVL(EA_INFORMATION),
1195                 LVL(ACCESS_INFORMATION), LVL(NAME_INFORMATION), LVL(POSITION_INFORMATION),
1196                 LVL(MODE_INFORMATION), LVL(ALIGNMENT_INFORMATION), LVL(ALL_INFORMATION),
1197                 LVL(ALT_NAME_INFORMATION), LVL(STREAM_INFORMATION), LVL(COMPRESSION_INFORMATION),
1198                 LVL(NETWORK_OPEN_INFORMATION), LVL(ATTRIBUTE_TAG_INFORMATION)
1199         };
1200         do {
1201                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1202         } while (ignore_pattern(levels[i].name));
1203
1204         info->generic.level = levels[i].level;
1205 }
1206
1207 /*
1208   compare returned fileinfo structures
1209 */
1210 static bool cmp_fileinfo(int instance, 
1211                          union smb_fileinfo parm[NSERVERS],
1212                          NTSTATUS status[NSERVERS])
1213 {
1214         int i;
1215
1216         switch (parm[0].generic.level) {
1217         case RAW_FILEINFO_GENERIC:
1218                 return false;
1219
1220         case RAW_FILEINFO_GETATTR:
1221                 CHECK_EQUAL(getattr.out.attrib);
1222                 CHECK_EQUAL(getattr.out.size);
1223                 CHECK_TIMES_EQUAL(getattr.out.write_time);
1224                 break;
1225
1226         case RAW_FILEINFO_GETATTRE:
1227                 CHECK_TIMES_EQUAL(getattre.out.create_time);
1228                 CHECK_TIMES_EQUAL(getattre.out.access_time);
1229                 CHECK_TIMES_EQUAL(getattre.out.write_time);
1230                 CHECK_EQUAL(getattre.out.size);
1231                 CHECK_EQUAL(getattre.out.alloc_size);
1232                 CHECK_EQUAL(getattre.out.attrib);
1233                 break;
1234
1235         case RAW_FILEINFO_STANDARD:
1236                 CHECK_TIMES_EQUAL(standard.out.create_time);
1237                 CHECK_TIMES_EQUAL(standard.out.access_time);
1238                 CHECK_TIMES_EQUAL(standard.out.write_time);
1239                 CHECK_EQUAL(standard.out.size);
1240                 CHECK_EQUAL(standard.out.alloc_size);
1241                 CHECK_EQUAL(standard.out.attrib);
1242                 break;
1243
1244         case RAW_FILEINFO_EA_SIZE:
1245                 CHECK_TIMES_EQUAL(ea_size.out.create_time);
1246                 CHECK_TIMES_EQUAL(ea_size.out.access_time);
1247                 CHECK_TIMES_EQUAL(ea_size.out.write_time);
1248                 CHECK_EQUAL(ea_size.out.size);
1249                 CHECK_EQUAL(ea_size.out.alloc_size);
1250                 CHECK_EQUAL(ea_size.out.attrib);
1251                 CHECK_EQUAL(ea_size.out.ea_size);
1252                 break;
1253
1254         case RAW_FILEINFO_ALL_EAS:
1255                 CHECK_EQUAL(all_eas.out.num_eas);
1256                 for (i=0;i<parm[0].all_eas.out.num_eas;i++) {
1257                         CHECK_EQUAL(all_eas.out.eas[i].flags);
1258                         CHECK_WSTR_EQUAL(all_eas.out.eas[i].name);
1259                         CHECK_BLOB_EQUAL(all_eas.out.eas[i].value);
1260                 }
1261                 break;
1262
1263         case RAW_FILEINFO_IS_NAME_VALID:
1264                 break;
1265                 
1266         case RAW_FILEINFO_BASIC_INFO:
1267         case RAW_FILEINFO_BASIC_INFORMATION:
1268                 CHECK_NTTIMES_EQUAL(basic_info.out.create_time);
1269                 CHECK_NTTIMES_EQUAL(basic_info.out.access_time);
1270                 CHECK_NTTIMES_EQUAL(basic_info.out.write_time);
1271                 CHECK_NTTIMES_EQUAL(basic_info.out.change_time);
1272                 CHECK_EQUAL(basic_info.out.attrib);
1273                 break;
1274
1275         case RAW_FILEINFO_STANDARD_INFO:
1276         case RAW_FILEINFO_STANDARD_INFORMATION:
1277                 CHECK_EQUAL(standard_info.out.alloc_size);
1278                 CHECK_EQUAL(standard_info.out.size);
1279                 CHECK_EQUAL(standard_info.out.nlink);
1280                 CHECK_EQUAL(standard_info.out.delete_pending);
1281                 CHECK_EQUAL(standard_info.out.directory);
1282                 break;
1283
1284         case RAW_FILEINFO_EA_INFO:
1285         case RAW_FILEINFO_EA_INFORMATION:
1286                 CHECK_EQUAL(ea_info.out.ea_size);
1287                 break;
1288
1289         case RAW_FILEINFO_NAME_INFO:
1290         case RAW_FILEINFO_NAME_INFORMATION:
1291                 CHECK_WSTR_EQUAL(name_info.out.fname);
1292                 break;
1293
1294         case RAW_FILEINFO_ALL_INFO:
1295         case RAW_FILEINFO_ALL_INFORMATION:
1296                 CHECK_NTTIMES_EQUAL(all_info.out.create_time);
1297                 CHECK_NTTIMES_EQUAL(all_info.out.access_time);
1298                 CHECK_NTTIMES_EQUAL(all_info.out.write_time);
1299                 CHECK_NTTIMES_EQUAL(all_info.out.change_time);
1300                 CHECK_EQUAL(all_info.out.attrib);
1301                 CHECK_EQUAL(all_info.out.alloc_size);
1302                 CHECK_EQUAL(all_info.out.size);
1303                 CHECK_EQUAL(all_info.out.nlink);
1304                 CHECK_EQUAL(all_info.out.delete_pending);
1305                 CHECK_EQUAL(all_info.out.directory);
1306                 CHECK_EQUAL(all_info.out.ea_size);
1307                 CHECK_WSTR_EQUAL(all_info.out.fname);
1308                 break;
1309
1310         case RAW_FILEINFO_ALT_NAME_INFO:
1311         case RAW_FILEINFO_ALT_NAME_INFORMATION:
1312                 CHECK_WSTR_EQUAL(alt_name_info.out.fname);
1313                 break;
1314
1315         case RAW_FILEINFO_STREAM_INFO:
1316         case RAW_FILEINFO_STREAM_INFORMATION:
1317                 CHECK_EQUAL(stream_info.out.num_streams);
1318                 for (i=0;i<parm[0].stream_info.out.num_streams;i++) {
1319                         CHECK_EQUAL(stream_info.out.streams[i].size);
1320                         CHECK_EQUAL(stream_info.out.streams[i].alloc_size);
1321                         CHECK_WSTR_EQUAL(stream_info.out.streams[i].stream_name);
1322                 }
1323                 break;
1324
1325         case RAW_FILEINFO_COMPRESSION_INFO:
1326         case RAW_FILEINFO_COMPRESSION_INFORMATION:
1327                 CHECK_EQUAL(compression_info.out.compressed_size);
1328                 CHECK_EQUAL(compression_info.out.format);
1329                 CHECK_EQUAL(compression_info.out.unit_shift);
1330                 CHECK_EQUAL(compression_info.out.chunk_shift);
1331                 CHECK_EQUAL(compression_info.out.cluster_shift);
1332                 break;
1333
1334         case RAW_FILEINFO_INTERNAL_INFORMATION:
1335                 CHECK_EQUAL(internal_information.out.file_id);
1336                 break;
1337
1338         case RAW_FILEINFO_ACCESS_INFORMATION:
1339                 CHECK_EQUAL(access_information.out.access_flags);
1340                 break;
1341
1342         case RAW_FILEINFO_POSITION_INFORMATION:
1343                 CHECK_EQUAL(position_information.out.position);
1344                 break;
1345
1346         case RAW_FILEINFO_MODE_INFORMATION:
1347                 CHECK_EQUAL(mode_information.out.mode);
1348                 break;
1349
1350         case RAW_FILEINFO_ALIGNMENT_INFORMATION:
1351                 CHECK_EQUAL(alignment_information.out.alignment_requirement);
1352                 break;
1353
1354         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1355                 CHECK_NTTIMES_EQUAL(network_open_information.out.create_time);
1356                 CHECK_NTTIMES_EQUAL(network_open_information.out.access_time);
1357                 CHECK_NTTIMES_EQUAL(network_open_information.out.write_time);
1358                 CHECK_NTTIMES_EQUAL(network_open_information.out.change_time);
1359                 CHECK_EQUAL(network_open_information.out.alloc_size);
1360                 CHECK_EQUAL(network_open_information.out.size);
1361                 CHECK_EQUAL(network_open_information.out.attrib);
1362                 break;
1363
1364         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1365                 CHECK_EQUAL(attribute_tag_information.out.attrib);
1366                 CHECK_EQUAL(attribute_tag_information.out.reparse_tag);
1367                 break;
1368
1369                 /* Unhandled levels */
1370
1371         case RAW_FILEINFO_SEC_DESC:
1372         case RAW_FILEINFO_EA_LIST:
1373         case RAW_FILEINFO_UNIX_BASIC:
1374         case RAW_FILEINFO_UNIX_LINK:
1375         case RAW_FILEINFO_SMB2_ALL_EAS:
1376         case RAW_FILEINFO_SMB2_ALL_INFORMATION:
1377         case RAW_FILEINFO_UNIX_INFO2:
1378                 break;
1379         }
1380
1381         return true;
1382 }
1383
1384 /*
1385   generate qfileinfo operations
1386 */
1387 static bool handler_qfileinfo(int instance)
1388 {
1389         union smb_fileinfo parm[NSERVERS];
1390         NTSTATUS status[NSERVERS];
1391
1392         parm[0].generic.in.file.fnum = gen_fnum(instance);
1393
1394         gen_fileinfo(instance, &parm[0]);
1395
1396         GEN_COPY_PARM;
1397         GEN_SET_FNUM(generic.in.file.fnum);
1398         GEN_CALL(smb_raw_fileinfo(tree, current_op.mem_ctx, &parm[i]));
1399
1400         return cmp_fileinfo(instance, parm, status);
1401 }
1402
1403
1404 /*
1405   generate a fileinfo query structure
1406 */
1407 static void gen_setfileinfo(int instance, union smb_setfileinfo *info)
1408 {
1409         int i;
1410         #undef LVL
1411         #define LVL(v) {RAW_SFILEINFO_ ## v, "RAW_SFILEINFO_" #v}
1412         struct {
1413                 enum smb_setfileinfo_level level;
1414                 const char *name;
1415         }  levels[] = {
1416 #if 0
1417                 /* disabled until win2003 can handle them ... */
1418                 LVL(EA_SET), LVL(BASIC_INFO), LVL(DISPOSITION_INFO), 
1419                 LVL(STANDARD), LVL(ALLOCATION_INFO), LVL(END_OF_FILE_INFO), 
1420 #endif
1421                 LVL(SETATTR), LVL(SETATTRE), LVL(BASIC_INFORMATION),
1422                 LVL(RENAME_INFORMATION), LVL(DISPOSITION_INFORMATION), 
1423                 LVL(POSITION_INFORMATION), LVL(MODE_INFORMATION),
1424                 LVL(ALLOCATION_INFORMATION), LVL(END_OF_FILE_INFORMATION), 
1425                 LVL(1023), LVL(1025), LVL(1029), LVL(1032), LVL(1039), LVL(1040)
1426         };
1427         do {
1428                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1429         } while (ignore_pattern(levels[i].name));
1430
1431         info->generic.level = levels[i].level;
1432
1433         switch (info->generic.level) {
1434         case RAW_SFILEINFO_SETATTR:
1435                 info->setattr.in.attrib = gen_attrib();
1436                 info->setattr.in.write_time = gen_timet();
1437                 break;
1438         case RAW_SFILEINFO_SETATTRE:
1439                 info->setattre.in.create_time = gen_timet();
1440                 info->setattre.in.access_time = gen_timet();
1441                 info->setattre.in.write_time = gen_timet();
1442                 break;
1443         case RAW_SFILEINFO_STANDARD:
1444                 info->standard.in.create_time = gen_timet();
1445                 info->standard.in.access_time = gen_timet();
1446                 info->standard.in.write_time = gen_timet();
1447                 break;
1448         case RAW_SFILEINFO_EA_SET: {
1449                 static struct ea_struct ea;
1450                 info->ea_set.in.num_eas = 1;
1451                 info->ea_set.in.eas = &ea;
1452                 info->ea_set.in.eas[0] = gen_ea_struct();
1453         }
1454                 break;
1455         case RAW_SFILEINFO_BASIC_INFO:
1456         case RAW_SFILEINFO_BASIC_INFORMATION:
1457                 info->basic_info.in.create_time = gen_nttime();
1458                 info->basic_info.in.access_time = gen_nttime();
1459                 info->basic_info.in.write_time = gen_nttime();
1460                 info->basic_info.in.change_time = gen_nttime();
1461                 info->basic_info.in.attrib = gen_attrib();
1462                 break;
1463         case RAW_SFILEINFO_DISPOSITION_INFO:
1464         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
1465                 info->disposition_info.in.delete_on_close = gen_bool();
1466                 break;
1467         case RAW_SFILEINFO_ALLOCATION_INFO:
1468         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
1469                 info->allocation_info.in.alloc_size = gen_alloc_size();
1470                 break;
1471         case RAW_SFILEINFO_END_OF_FILE_INFO:
1472         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
1473                 info->end_of_file_info.in.size = gen_offset();
1474                 break;
1475         case RAW_SFILEINFO_RENAME_INFORMATION:
1476         case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
1477                 info->rename_information.in.overwrite = gen_bool();
1478                 info->rename_information.in.root_fid = gen_root_fid(instance);
1479                 info->rename_information.in.new_name = gen_fname_open(instance);
1480                 break;
1481         case RAW_SFILEINFO_POSITION_INFORMATION:
1482                 info->position_information.in.position = gen_offset();
1483                 break;
1484         case RAW_SFILEINFO_MODE_INFORMATION:
1485                 info->mode_information.in.mode = gen_bits_mask(0xFFFFFFFF);
1486                 break;
1487         case RAW_SFILEINFO_GENERIC:
1488         case RAW_SFILEINFO_SEC_DESC:
1489         case RAW_SFILEINFO_UNIX_BASIC:
1490         case RAW_SFILEINFO_UNIX_LINK:
1491         case RAW_SFILEINFO_UNIX_HLINK:
1492         case RAW_SFILEINFO_1023:
1493         case RAW_SFILEINFO_1025:
1494         case RAW_SFILEINFO_1029:
1495         case RAW_SFILEINFO_1032:
1496         case RAW_SFILEINFO_1039:
1497         case RAW_SFILEINFO_1040:
1498         case RAW_SFILEINFO_UNIX_INFO2:
1499                 /* Untested */
1500                 break;
1501         }
1502 }
1503
1504 /*
1505   generate setfileinfo operations
1506 */
1507 static bool handler_sfileinfo(int instance)
1508 {
1509         union smb_setfileinfo parm[NSERVERS];
1510         NTSTATUS status[NSERVERS];
1511
1512         parm[0].generic.in.file.fnum = gen_fnum(instance);
1513
1514         gen_setfileinfo(instance, &parm[0]);
1515
1516         GEN_COPY_PARM;
1517         GEN_SET_FNUM(generic.in.file.fnum);
1518         GEN_CALL(smb_raw_setfileinfo(tree, &parm[i]));
1519
1520         return true;
1521 }
1522
1523 #endif
1524
1525
1526 /*
1527   wipe any relevant files
1528 */
1529 static void wipe_files(void)
1530 {
1531         int i;
1532         NTSTATUS status;
1533
1534         for (i=0;i<NSERVERS;i++) {
1535                 int n = smb2_deltree(servers[i].tree[0], "gentest");
1536                 if (n == -1) {
1537                         printf("Failed to wipe tree on server %d\n", i);
1538                         exit(1);
1539                 }
1540                 status = smb2_util_mkdir(servers[i].tree[0], "gentest");
1541                 if (NT_STATUS_IS_ERR(status)) {
1542                         printf("Failed to create gentest on server %d - %s\n", i, nt_errstr(status));
1543                         exit(1);
1544                 }
1545                 if (n > 0) {
1546                         printf("Deleted %d files on server %d\n", n, i);
1547                 }
1548         }
1549 }
1550
1551 /*
1552   dump the current seeds - useful for continuing a backtrack
1553 */
1554 static void dump_seeds(void)
1555 {
1556         int i;
1557         FILE *f;
1558
1559         if (!options.seeds_file) {
1560                 return;
1561         }
1562         f = fopen("seeds.tmp", "w");
1563         if (!f) return;
1564
1565         for (i=0;i<options.numops;i++) {
1566                 fprintf(f, "%u\n", op_parms[i].seed);
1567         }
1568         fclose(f);
1569         rename("seeds.tmp", options.seeds_file);
1570 }
1571
1572
1573
1574 /*
1575   the list of top-level operations that we will generate
1576 */
1577 static struct {
1578         const char *name;
1579         bool (*handler)(int instance);
1580         int count, success_count;
1581 } gen_ops[] = {
1582         {"NTCREATEX",  handler_ntcreatex},
1583         {"CLOSE",      handler_close},
1584         {"READ",       handler_read},
1585         {"WRITE",      handler_write},
1586 };
1587
1588
1589 /*
1590   run the test with the current set of op_parms parameters
1591   return the number of operations that completed successfully
1592 */
1593 static int run_test(struct event_context *ev, struct loadparm_context *lp_ctx)
1594 {
1595         int op, i;
1596
1597         if (!connect_servers(ev, lp_ctx)) {
1598                 printf("Failed to connect to servers\n");
1599                 exit(1);
1600         }
1601
1602         dump_seeds();
1603
1604         /* wipe any leftover files from old runs */
1605         wipe_files();
1606
1607         /* reset the open handles array */
1608         memset(open_handles, 0, options.max_open_handles * sizeof(open_handles[0]));
1609         num_open_handles = 0;
1610
1611         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1612                 gen_ops[i].count = 0;
1613                 gen_ops[i].success_count = 0;
1614         }
1615
1616         for (op=0; op<options.numops; op++) {
1617                 int instance, which_op;
1618                 bool ret;
1619
1620                 if (op_parms[op].disabled) continue;
1621
1622                 srandom(op_parms[op].seed);
1623
1624                 instance = gen_int_range(0, NINSTANCES-1);
1625
1626                 /* generate a non-ignored operation */
1627                 do {
1628                         which_op = gen_int_range(0, ARRAY_SIZE(gen_ops)-1);
1629                 } while (ignore_pattern(gen_ops[which_op].name));
1630
1631                 DEBUG(3,("Generating op %s on instance %d\n",
1632                          gen_ops[which_op].name, instance));
1633
1634                 current_op.seed = op_parms[op].seed;
1635                 current_op.opnum = op;
1636                 current_op.name = gen_ops[which_op].name;
1637                 current_op.status = NT_STATUS_OK;
1638                 current_op.mem_ctx = talloc_named(NULL, 0, "%s", current_op.name);
1639
1640                 ret = gen_ops[which_op].handler(instance);
1641
1642                 talloc_free(current_op.mem_ctx);
1643
1644                 gen_ops[which_op].count++;
1645                 if (NT_STATUS_IS_OK(current_op.status)) {
1646                         gen_ops[which_op].success_count++;                      
1647                 }
1648
1649                 if (!ret) {
1650                         printf("Failed at operation %d - %s\n",
1651                                op, gen_ops[which_op].name);
1652                         return op;
1653                 }
1654
1655                 if (op % 100 == 0) {
1656                         printf("%d\n", op);
1657                 }
1658         }
1659
1660         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1661                 printf("Op %-10s got %d/%d success\n", 
1662                        gen_ops[i].name,
1663                        gen_ops[i].success_count,
1664                        gen_ops[i].count);
1665         }
1666
1667         return op;
1668 }
1669
1670 /* 
1671    perform a backtracking analysis of the minimal set of operations
1672    to generate an error
1673 */
1674 static void backtrack_analyze(struct event_context *ev,
1675                               struct loadparm_context *lp_ctx)
1676 {
1677         int chunk, ret;
1678
1679         chunk = options.numops / 2;
1680
1681         do {
1682                 int base;
1683                 for (base=0; 
1684                      chunk > 0 && base+chunk < options.numops && options.numops > 1; ) {
1685                         int i, max;
1686
1687                         chunk = MIN(chunk, options.numops / 2);
1688
1689                         /* mark this range as disabled */
1690                         max = MIN(options.numops, base+chunk);
1691                         for (i=base;i<max; i++) {
1692                                 op_parms[i].disabled = true;
1693                         }
1694                         printf("Testing %d ops with %d-%d disabled\n", 
1695                                options.numops, base, max-1);
1696                         ret = run_test(ev, lp_ctx);
1697                         printf("Completed %d of %d ops\n", ret, options.numops);
1698                         for (i=base;i<max; i++) {
1699                                 op_parms[i].disabled = false;
1700                         }
1701                         if (ret == options.numops) {
1702                                 /* this chunk is needed */
1703                                 base += chunk;
1704                         } else if (ret < base) {
1705                                 printf("damn - inconsistent errors! found early error\n");
1706                                 options.numops = ret+1;
1707                                 base = 0;
1708                         } else {
1709                                 /* it failed - this chunk isn't needed for a failure */
1710                                 memmove(&op_parms[base], &op_parms[max], 
1711                                         sizeof(op_parms[0]) * (options.numops - max));
1712                                 options.numops = (ret+1) - (max - base);
1713                         }
1714                 }
1715
1716                 if (chunk == 2) {
1717                         chunk = 1;
1718                 } else {
1719                         chunk *= 0.4;
1720                 }
1721
1722                 if (options.analyze_continuous && chunk == 0 && options.numops != 1) {
1723                         chunk = 1;
1724                 }
1725         } while (chunk > 0);
1726
1727         printf("Reduced to %d ops\n", options.numops);
1728         ret = run_test(ev, lp_ctx);
1729         if (ret != options.numops - 1) {
1730                 printf("Inconsistent result? ret=%d numops=%d\n", ret, options.numops);
1731         }
1732 }
1733
1734 /* 
1735    start the main gentest process
1736 */
1737 static bool start_gentest(struct event_context *ev,
1738                           struct loadparm_context *lp_ctx)
1739 {
1740         int op;
1741         int ret;
1742
1743         /* allocate the open_handles array */
1744         open_handles = calloc(options.max_open_handles, sizeof(open_handles[0]));
1745
1746         srandom(options.seed);
1747         op_parms = calloc(options.numops, sizeof(op_parms[0]));
1748
1749         /* generate the seeds - after this everything is deterministic */
1750         if (options.use_preset_seeds) {
1751                 int numops;
1752                 char **preset = file_lines_load(options.seeds_file, &numops, NULL);
1753                 if (!preset) {
1754                         printf("Failed to load %s - %s\n", options.seeds_file, strerror(errno));
1755                         exit(1);
1756                 }
1757                 if (numops < options.numops) {
1758                         options.numops = numops;
1759                 }
1760                 for (op=0;op<options.numops;op++) {
1761                         if (!preset[op]) {
1762                                 printf("Not enough seeds in %s\n", options.seeds_file);
1763                                 exit(1);
1764                         }
1765                         op_parms[op].seed = atoi(preset[op]);
1766                 }
1767                 printf("Loaded %d seeds from %s\n", options.numops, options.seeds_file);
1768         } else {
1769                 for (op=0; op<options.numops; op++) {
1770                         op_parms[op].seed = random();
1771                 }
1772         }
1773
1774         ret = run_test(ev, lp_ctx);
1775
1776         if (ret != options.numops && options.analyze) {
1777                 options.numops = ret+1;
1778                 backtrack_analyze(ev, lp_ctx);
1779         } else if (options.analyze_always) {
1780                 backtrack_analyze(ev, lp_ctx);
1781         } else if (options.analyze_continuous) {
1782                 while (run_test(ev, lp_ctx) == options.numops) ;
1783         }
1784
1785         return ret == options.numops;
1786 }
1787
1788
1789 static void usage(poptContext pc)
1790 {
1791         printf(
1792 "Usage:\n\
1793   gentest //server1/share1 //server2/share2 [options..]\n\
1794 ");
1795         poptPrintUsage(pc, stdout, 0);
1796 }
1797
1798 /**
1799   split a UNC name into server and share names
1800 */
1801 static bool split_unc_name(const char *unc, char **server, char **share)
1802 {
1803         char *p = strdup(unc);
1804         if (!p) return false;
1805         all_string_sub(p, "\\", "/", 0);
1806         if (strncmp(p, "//", 2) != 0) return false;
1807
1808         (*server) = p+2;
1809         p = strchr(*server, '/');
1810         if (!p) return false;
1811
1812         *p = 0;
1813         (*share) = p+1;
1814         
1815         return true;
1816 }
1817
1818
1819
1820 /****************************************************************************
1821   main program
1822 ****************************************************************************/
1823  int main(int argc, char *argv[])
1824 {
1825         int opt;
1826         int i, username_count=0;
1827         bool ret;
1828         char *ignore_file=NULL;
1829         struct event_context *ev;
1830         struct loadparm_context *lp_ctx;
1831         poptContext pc;
1832         int argc_new;
1833         char **argv_new;
1834         enum {OPT_UNCLIST=1000};
1835         struct poptOption long_options[] = {
1836                 POPT_AUTOHELP
1837                 {"seed",          0, POPT_ARG_INT,  &options.seed,      0,      "Seed to use for randomizer",   NULL},
1838                 {"num-ops",       0, POPT_ARG_INT,  &options.numops,    0,      "num ops",      NULL},
1839                 {"oplocks",       0, POPT_ARG_NONE, &options.use_oplocks,0,      "use oplocks", NULL},
1840                 {"showall",       0, POPT_ARG_NONE, &options.showall,    0,      "display all operations", NULL},
1841                 {"analyse",       0, POPT_ARG_NONE, &options.analyze,    0,      "do backtrack analysis", NULL},
1842                 {"analysealways", 0, POPT_ARG_NONE, &options.analyze_always,    0,      "analysis always", NULL},
1843                 {"analysecontinuous", 0, POPT_ARG_NONE, &options.analyze_continuous,    0,      "analysis continuous", NULL},
1844                 {"ignore",        0, POPT_ARG_STRING, &ignore_file,    0,      "ignore from file", NULL},
1845                 {"preset",        0, POPT_ARG_NONE, &options.use_preset_seeds,    0,      "use preset seeds", NULL},
1846                 {"fast",          0, POPT_ARG_NONE, &options.fast_reconnect,    0,      "use fast reconnect", NULL},
1847                 {"unclist",       0, POPT_ARG_STRING,   NULL,   OPT_UNCLIST,    "unclist",      NULL},
1848                 {"seedsfile",     0, POPT_ARG_STRING,  &options.seeds_file, 0,  "seed file",    NULL},
1849                 { "user", 'U',       POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
1850                 POPT_COMMON_SAMBA
1851                 POPT_COMMON_CONNECTION
1852                 POPT_COMMON_CREDENTIALS
1853                 POPT_COMMON_VERSION
1854                 { NULL }
1855         };
1856
1857         memset(&bad_smb2_handle, 0xFF, sizeof(bad_smb2_handle));
1858
1859         setlinebuf(stdout);
1860         options.seed = time(NULL);
1861         options.numops = 1000;
1862         options.max_open_handles = 20;
1863         options.seeds_file = "gentest_seeds.dat";
1864
1865         pc = poptGetContext("gentest", argc, (const char **) argv, long_options, 
1866                             POPT_CONTEXT_KEEP_FIRST);
1867
1868         poptSetOtherOptionHelp(pc, "<unc1> <unc2>");
1869
1870         lp_ctx = cmdline_lp_ctx;
1871         servers[0].credentials = cli_credentials_init(talloc_autofree_context());
1872         servers[1].credentials = cli_credentials_init(talloc_autofree_context());
1873         cli_credentials_guess(servers[0].credentials, lp_ctx);
1874         cli_credentials_guess(servers[1].credentials, lp_ctx);
1875
1876         while((opt = poptGetNextOpt(pc)) != -1) {
1877                 switch (opt) {
1878                 case OPT_UNCLIST:
1879                         lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
1880                         break;
1881                 case 'U':
1882                         if (username_count == 2) {
1883                                 usage(pc);
1884                                 exit(1);
1885                         }
1886                         cli_credentials_parse_string(servers[username_count].credentials, poptGetOptArg(pc), CRED_SPECIFIED);
1887                         username_count++;
1888                         break;
1889                 }
1890         }
1891
1892         if (ignore_file) {
1893                 options.ignore_patterns = file_lines_load(ignore_file, NULL, NULL);
1894         }
1895
1896         argv_new = discard_const_p(char *, poptGetArgs(pc));
1897         argc_new = argc;
1898         for (i=0; i<argc; i++) {
1899                 if (argv_new[i] == NULL) {
1900                         argc_new = i;
1901                         break;
1902                 }
1903         }
1904
1905         if (!(argc_new >= 3)) {
1906                 usage(pc);
1907                 exit(1);
1908         }
1909
1910         setlinebuf(stdout);
1911
1912         setup_logging("gentest", DEBUG_STDOUT);
1913
1914         if (argc < 3 || argv[1][0] == '-') {
1915                 usage(pc);
1916                 exit(1);
1917         }
1918
1919         setup_logging(argv[0], DEBUG_STDOUT);
1920
1921         for (i=0;i<NSERVERS;i++) {
1922                 const char *share = argv[1+i];
1923                 if (!split_unc_name(share, &servers[i].server_name, &servers[i].share_name)) {
1924                         printf("Invalid share name '%s'\n", share);
1925                         return -1;
1926                 }
1927         }
1928
1929         if (username_count == 0) {
1930                 usage(pc);
1931                 return -1;
1932         }
1933         if (username_count == 1) {
1934                 servers[1].credentials = servers[0].credentials;
1935         }
1936
1937         printf("seed=%u\n", options.seed);
1938
1939         ev = event_context_init(talloc_autofree_context());
1940
1941         gensec_init(lp_ctx);
1942
1943         ret = start_gentest(ev, lp_ctx);
1944
1945         if (ret) {
1946                 printf("gentest completed - no errors\n");
1947         } else {
1948                 printf("gentest failed\n");
1949         }
1950
1951         return ret?0:-1;
1952 }