source4/torture/basic: Fix prototypes for all functions.
[ira/wip.git] / source4 / torture / gentest.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    generic testing tool - version with both SMB and 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 "librpc/gen_ndr/ndr_security.h"
34 #include "auth/credentials/credentials.h"
35 #include "libcli/resolve/resolve.h"
36 #include "auth/gensec/gensec.h"
37 #include "param/param.h"
38 #include "dynconfig/dynconfig.h"
39 #include "libcli/security/security.h"
40 #include "libcli/raw/raw_proto.h"
41
42 #define NSERVERS 2
43 #define NINSTANCES 2
44
45 /* global options */
46 static struct gentest_options {
47         int showall;
48         int analyze;
49         int analyze_always;
50         int analyze_continuous;
51         unsigned int max_open_handles;
52         unsigned int seed;
53         unsigned int numops;
54         int use_oplocks;
55         char **ignore_patterns;
56         const char *seeds_file;
57         int use_preset_seeds;
58         int fast_reconnect;
59         int mask_indexing;
60         int no_eas;
61         int no_acls;
62         int skip_cleanup;
63         int valid;
64         int smb2;
65 } options;
66
67 /* mapping between open handles on the server and local handles */
68 static struct {
69         bool active;
70         unsigned int instance;
71         struct smb2_handle smb2_handle[NSERVERS]; /* SMB2 */
72         uint16_t smb_handle[NSERVERS];            /* SMB */
73         const char *name;
74 } *open_handles;
75 static unsigned int num_open_handles;
76
77 /* state information for the servers. We open NINSTANCES connections to
78    each server */
79 static struct {
80         struct smb2_tree *smb2_tree[NINSTANCES];
81         struct smbcli_tree *smb_tree[NINSTANCES];
82         char *server_name;
83         char *share_name;
84         struct cli_credentials *credentials;
85 } servers[NSERVERS];
86
87 /* the seeds and flags for each operation */
88 static struct {
89         unsigned int seed;
90         bool disabled;
91 } *op_parms;
92
93
94 /* oplock break info */
95 static struct {
96         bool got_break;
97         struct smb2_handle smb2_handle;
98         uint16_t smb_handle;
99         uint16_t handle;
100         uint8_t level;
101         bool do_close;
102 } oplocks[NSERVERS][NINSTANCES];
103
104 /* change notify reply info */
105 static struct {
106         int notify_count;
107         NTSTATUS status;
108         union smb_notify notify;
109 } notifies[NSERVERS][NINSTANCES];
110
111 /* info relevant to the current operation */
112 static struct {
113         const char *name;
114         unsigned int seed;
115         NTSTATUS status;
116         unsigned int opnum;
117         TALLOC_CTX *mem_ctx;
118         const char *mismatch;
119 } current_op;
120
121 static struct smb2_handle bad_smb2_handle;
122
123
124 #define BAD_HANDLE 0xFFFE
125
126 static bool oplock_handler_smb2(struct smb2_transport *transport, const struct smb2_handle *handle,
127                                 uint8_t level, void *private_data);
128 static void idle_func_smb2(struct smb2_transport *transport, void *private_data);
129 static bool oplock_handler_smb(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private_data);
130 static void idle_func_smb(struct smbcli_transport *transport, void *private_data);
131
132 /*
133   check if a string should be ignored. This is used as the basis
134   for all error ignore settings
135 */
136 static bool ignore_pattern(const char *str)
137 {
138         int i;
139         if (!options.ignore_patterns) return false;
140
141         for (i=0;options.ignore_patterns[i];i++) {
142                 if (strcmp(options.ignore_patterns[i], str) == 0 ||
143                     gen_fnmatch(options.ignore_patterns[i], str) == 0) {
144                         DEBUG(2,("Ignoring '%s'\n", str));
145                         return true;
146                 }
147         }
148         return false;
149 }
150
151 /***************************************************** 
152 connect to the servers
153 *******************************************************/
154 static bool connect_servers_fast(void)
155 {
156         int h, i;
157
158         /* close all open files */
159         for (h=0;h<options.max_open_handles;h++) {
160                 if (!open_handles[h].active) continue;
161                 for (i=0;i<NSERVERS;i++) {
162                         NTSTATUS status;
163                         if (options.smb2) {
164                                 status = smb2_util_close(servers[i].smb2_tree[open_handles[h].instance],
165                                                          open_handles[h].smb2_handle[i]);
166                         } else {
167                                 status = smbcli_close(servers[i].smb_tree[open_handles[h].instance],
168                                                       open_handles[h].smb_handle[i]);
169                         }
170                         if (NT_STATUS_IS_ERR(status)) {
171                                 return false;
172                         }
173                         open_handles[h].active = false;
174                 }
175         }
176
177         return true;
178 }
179
180
181
182
183 /***************************************************** 
184 connect to the servers
185 *******************************************************/
186 static bool connect_servers(struct tevent_context *ev,
187                             struct loadparm_context *lp_ctx)
188 {
189         int i, j;
190
191         if (options.fast_reconnect && servers[0].smb2_tree[0]) {
192                 if (connect_servers_fast()) {
193                         return true;
194                 }
195         }
196
197         /* close any existing connections */
198         for (i=0;i<NSERVERS;i++) {
199                 for (j=0;j<NINSTANCES;j++) {
200                         if (servers[i].smb2_tree[j]) {
201                                 smb2_tdis(servers[i].smb2_tree[j]);
202                                 talloc_free(servers[i].smb2_tree[j]);
203                                 servers[i].smb2_tree[j] = NULL;
204                         }
205                         if (servers[i].smb_tree[j]) {
206                                 smb_tree_disconnect(servers[i].smb_tree[j]);
207                                 talloc_free(servers[i].smb_tree[j]);
208                                 servers[i].smb_tree[j] = NULL;
209                         }
210                 }
211         }
212
213         for (i=0;i<NSERVERS;i++) {
214                 for (j=0;j<NINSTANCES;j++) {
215                         NTSTATUS status;
216                         struct smbcli_options smb_options;
217                         struct smbcli_session_options smb_session_options;
218                         lpcfg_smbcli_options(lp_ctx, &smb_options);
219                         lpcfg_smbcli_session_options(lp_ctx, &smb_session_options);
220
221                         printf("Connecting to \\\\%s\\%s as %s - instance %d\n",
222                                servers[i].server_name, servers[i].share_name, 
223                                servers[i].credentials->username, j);
224
225                         cli_credentials_set_workstation(servers[i].credentials, 
226                                                         "gentest", CRED_SPECIFIED);
227
228                         if (options.smb2) {
229                                 status = smb2_connect(NULL, servers[i].server_name, 
230                                                                           lpcfg_smb_ports(lp_ctx),
231                                                       servers[i].share_name,
232                                                       lpcfg_resolve_context(lp_ctx),
233                                                       servers[i].credentials,
234                                                       &servers[i].smb2_tree[j],
235                                                       ev, &smb_options,
236                                                           lpcfg_socket_options(lp_ctx),
237                                                           lpcfg_gensec_settings(lp_ctx, lp_ctx)
238                                                           );
239                         } else {
240                                 status = smbcli_tree_full_connection(NULL,
241                                                                      &servers[i].smb_tree[j], 
242                                                                      servers[i].server_name, 
243                                                                      lpcfg_smb_ports(lp_ctx),
244                                                                      servers[i].share_name, "A:",
245                                                                          lpcfg_socket_options(lp_ctx),
246                                                                      servers[i].credentials,
247                                                                      lpcfg_resolve_context(lp_ctx), ev,
248                                                                      &smb_options,
249                                                                      &smb_session_options,
250                                                                          lpcfg_gensec_settings(lp_ctx, lp_ctx));
251                         }
252                         if (!NT_STATUS_IS_OK(status)) {
253                                 printf("Failed to connect to \\\\%s\\%s - %s\n",
254                                        servers[i].server_name, servers[i].share_name,
255                                        nt_errstr(status));
256                                 return false;
257                         }
258
259                         if (options.smb2) {
260                                 servers[i].smb2_tree[j]->session->transport->oplock.handler = oplock_handler_smb2;
261                                 servers[i].smb2_tree[j]->session->transport->oplock.private_data = (void *)(uintptr_t)((i<<8)|j);
262                                 smb2_transport_idle_handler(servers[i].smb2_tree[j]->session->transport, 
263                                                             idle_func_smb2, 50000, NULL);
264                         } else {
265                                 smbcli_oplock_handler(servers[i].smb_tree[j]->session->transport, oplock_handler_smb, 
266                                                       (void *)(uintptr_t)((i<<8)|j));
267                                 smbcli_transport_idle_handler(servers[i].smb_tree[j]->session->transport, idle_func_smb, 
268                                                               50000, (void *)(uintptr_t)((i<<8)|j));
269                         }
270                 }
271         }
272
273         return true;
274 }
275
276 /*
277   work out the time skew between the servers - be conservative
278 */
279 static unsigned int time_skew(void)
280 {
281         unsigned int ret;
282         if (options.smb2) {
283                 ret = labs(servers[0].smb2_tree[0]->session->transport->negotiate.system_time -
284                            servers[1].smb2_tree[0]->session->transport->negotiate.system_time);
285         } else {
286                 ret = labs(servers[0].smb_tree[0]->session->transport->negotiate.server_time -
287                            servers[1].smb_tree[0]->session->transport->negotiate.server_time);
288         }
289         return ret + 300;
290 }
291
292
293 static bool smb2_handle_equal(const struct smb2_handle *h1, const struct smb2_handle *h2)
294 {
295         return memcmp(h1, h2, sizeof(struct smb2_handle)) == 0;
296 }
297
298 /*
299   turn a server handle into a local handle
300 */
301 static unsigned int fnum_to_handle_smb2(int server, int instance, struct smb2_handle server_handle)
302 {
303         unsigned int i;
304         for (i=0;i<options.max_open_handles;i++) {
305                 if (!open_handles[i].active ||
306                     instance != open_handles[i].instance) continue;
307                 if (smb2_handle_equal(&open_handles[i].smb2_handle[server], &server_handle)) {
308                         return i;
309                 }
310         }
311         printf("Invalid server handle in fnum_to_handle on server %d instance %d\n", 
312                server, instance);
313         return BAD_HANDLE;
314 }
315
316 /*
317   turn a server handle into a local handle
318 */
319 static unsigned int fnum_to_handle_smb(int server, int instance, uint16_t server_handle)
320 {
321         unsigned int i;
322         for (i=0;i<options.max_open_handles;i++) {
323                 if (!open_handles[i].active ||
324                     instance != open_handles[i].instance) continue;
325                 if (open_handles[i].smb_handle[server] == server_handle) {
326                         return i;
327                 }
328         }
329         printf("Invalid server handle in fnum_to_handle on server %d instance %d\n", 
330                server, instance);
331         return BAD_HANDLE;
332 }
333
334 /*
335   add some newly opened handles
336 */
337 static void gen_add_handle_smb2(int instance, const char *name, struct smb2_handle handles[NSERVERS])
338 {
339         int i, h;
340         for (h=0;h<options.max_open_handles;h++) {
341                 if (!open_handles[h].active) break;
342         }
343         if (h == options.max_open_handles) {
344                 /* we have to force close a random handle */
345                 h = random() % options.max_open_handles;
346                 for (i=0;i<NSERVERS;i++) {
347                         NTSTATUS status;
348                         status = smb2_util_close(servers[i].smb2_tree[open_handles[h].instance], 
349                                                  open_handles[h].smb2_handle[i]);
350                         if (NT_STATUS_IS_ERR(status)) {
351                                 printf("INTERNAL ERROR: Close failed when recovering handle! - %s\n",
352                                        nt_errstr(status));
353                         }
354                 }
355                 printf("Recovered handle %d\n", h);
356                 num_open_handles--;
357         }
358         for (i=0;i<NSERVERS;i++) {
359                 open_handles[h].smb2_handle[i] = handles[i];
360                 open_handles[h].instance = instance;
361                 open_handles[h].active = true;
362                 open_handles[h].name = name;
363         }
364         num_open_handles++;
365
366         printf("OPEN num_open_handles=%d h=%d (%s)\n", 
367                num_open_handles, h, name);
368 }
369
370 /*
371   add some newly opened handles
372 */
373 static void gen_add_handle_smb(int instance, const char *name, uint16_t handles[NSERVERS])
374 {
375         int i, h;
376         for (h=0;h<options.max_open_handles;h++) {
377                 if (!open_handles[h].active) break;
378         }
379         if (h == options.max_open_handles) {
380                 /* we have to force close a random handle */
381                 h = random() % options.max_open_handles;
382                 for (i=0;i<NSERVERS;i++) {
383                         NTSTATUS status;
384                         status = smbcli_close(servers[i].smb_tree[open_handles[h].instance], 
385                                               open_handles[h].smb_handle[i]);
386                         if (NT_STATUS_IS_ERR(status)) {
387                                 printf("INTERNAL ERROR: Close failed when recovering handle! - %s\n",
388                                        nt_errstr(status));
389                         }
390                 }
391                 printf("Recovered handle %d\n", h);
392                 num_open_handles--;
393         }
394         for (i=0;i<NSERVERS;i++) {
395                 open_handles[h].smb_handle[i] = handles[i];
396                 open_handles[h].instance = instance;
397                 open_handles[h].active = true;
398                 open_handles[h].name = name;
399         }
400         num_open_handles++;
401
402         printf("OPEN num_open_handles=%d h=%d (%s)\n", 
403                num_open_handles, h, name);
404 }
405
406
407 /*
408   remove a closed handle
409 */
410 static void gen_remove_handle_smb2(int instance, struct smb2_handle handles[NSERVERS])
411 {
412         int h;
413         for (h=0;h<options.max_open_handles;h++) {
414                 if (instance == open_handles[h].instance &&
415                     smb2_handle_equal(&open_handles[h].smb2_handle[0], &handles[0])) {
416                         open_handles[h].active = false;                 
417                         num_open_handles--;
418                         printf("CLOSE num_open_handles=%d h=%d (%s)\n", 
419                                num_open_handles, h, 
420                                open_handles[h].name);
421                         return;
422                 }
423         }
424         printf("Removing invalid handle!?\n");
425         exit(1);
426 }
427
428 /*
429   remove a closed handle
430 */
431 static void gen_remove_handle_smb(int instance, uint16_t handles[NSERVERS])
432 {
433         int h;
434         for (h=0;h<options.max_open_handles;h++) {
435                 if (instance == open_handles[h].instance &&
436                     open_handles[h].smb_handle[0] == handles[0]) {
437                         open_handles[h].active = false;                 
438                         num_open_handles--;
439                         printf("CLOSE num_open_handles=%d h=%d (%s)\n", 
440                                num_open_handles, h, 
441                                open_handles[h].name);
442                         return;
443                 }
444         }
445         printf("Removing invalid handle!?\n");
446         exit(1);
447 }
448
449 /*
450   return true with 'chance' probability as a percentage
451 */
452 static bool gen_chance(unsigned int chance)
453 {
454         return ((random() % 100) <= chance);
455 }
456
457 /*
458   map an internal handle number to a server handle
459 */
460 static struct smb2_handle gen_lookup_handle_smb2(int server, uint16_t handle)
461 {
462         if (handle == BAD_HANDLE) return bad_smb2_handle;
463         return open_handles[handle].smb2_handle[server];
464 }
465
466 /*
467   map an internal handle number to a server handle
468 */
469 static uint16_t gen_lookup_handle_smb(int server, uint16_t handle)
470 {
471         if (handle == BAD_HANDLE) return BAD_HANDLE;
472         return open_handles[handle].smb_handle[server];
473 }
474
475 /*
476   return a file handle
477 */
478 static uint16_t gen_fnum(int instance)
479 {
480         uint16_t h;
481         int count = 0;
482
483         if (gen_chance(20)) return BAD_HANDLE;
484
485         while (num_open_handles > 0 && count++ < 10*options.max_open_handles) {
486                 h = random() % options.max_open_handles;
487                 if (open_handles[h].active && 
488                     open_handles[h].instance == instance) {
489                         return h;
490                 }
491         }
492         return BAD_HANDLE;
493 }
494
495 /*
496   return a file handle, but skewed so we don't close the last
497   couple of handles too readily
498 */
499 static uint16_t gen_fnum_close(int instance)
500 {
501         if (num_open_handles < 5) {
502                 if (gen_chance(90)) return BAD_HANDLE;
503         }
504
505         return gen_fnum(instance);
506 }
507
508 /*
509   generate an integer in a specified range
510 */
511 static int gen_int_range(uint64_t min, uint64_t max)
512 {
513         unsigned int r = random();
514         return min + (r % (1+max-min));
515 }
516
517 /*
518   return a fnum for use as a root fid
519   be careful to call GEN_SET_FNUM() when you use this!
520 */
521 static uint16_t gen_root_fid(int instance)
522 {
523         if (gen_chance(5)) return gen_fnum(instance);
524         return 0;
525 }
526
527 /*
528   generate a file offset
529 */
530 static int gen_offset(void)
531 {
532         if (gen_chance(20)) return 0;
533 //      if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF);
534         return gen_int_range(0, 1024*1024);
535 }
536
537 /*
538   generate a io count
539 */
540 static int gen_io_count(void)
541 {
542         if (gen_chance(20)) return 0;
543 //      if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF);
544         return gen_int_range(0, 4096);
545 }
546
547 /*
548   generate a filename
549 */
550 static const char *gen_fname(void)
551 {
552         const char *names[] = {"gentest\\gentest.dat", 
553                                "gentest\\foo", 
554                                "gentest\\foo2.sym", 
555                                "gentest\\foo3.dll", 
556                                "gentest\\foo4", 
557                                "gentest\\foo4:teststream1", 
558                                "gentest\\foo4:teststream2", 
559                                "gentest\\foo5.exe", 
560                                "gentest\\foo5.exe:teststream3", 
561                                "gentest\\foo5.exe:teststream4", 
562                                "gentest\\foo6.com", 
563                                "gentest\\blah", 
564                                "gentest\\blah\\blergh.txt", 
565                                "gentest\\blah\\blergh2", 
566                                "gentest\\blah\\blergh3.txt", 
567                                "gentest\\blah\\blergh4", 
568                                "gentest\\blah\\blergh5.txt", 
569                                "gentest\\blah\\blergh5", 
570                                "gentest\\blah\\.", 
571                                "gentest\\blah\\..", 
572                                "gentest\\a_very_long_name.bin", 
573                                "gentest\\x.y", 
574                                "gentest\\blah"};
575         int i;
576
577         do {
578                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
579         } while (ignore_pattern(names[i]));
580
581         return names[i];
582 }
583
584 /*
585   generate a filename with a higher chance of choosing an already 
586   open file
587 */
588 static const char *gen_fname_open(int instance)
589 {
590         uint16_t h;
591         h = gen_fnum(instance);
592         if (h == BAD_HANDLE) {
593                 return gen_fname();
594         }
595         return open_handles[h].name;
596 }
597
598 /*
599   generate a wildcard pattern
600 */
601 static const char *gen_pattern(void)
602 {
603         int i;
604         const char *names[] = {"gentest\\*.dat", 
605                                "gentest\\*", 
606                                "gentest\\*.*", 
607                                "gentest\\blah\\*.*", 
608                                "gentest\\blah\\*", 
609                                "gentest\\?"};
610
611         if (gen_chance(50)) return gen_fname();
612
613         do {
614                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
615         } while (ignore_pattern(names[i]));
616
617         return names[i];
618 }
619
620 static uint32_t gen_bits_levels(int nlevels, ...)
621 {
622         va_list ap;
623         uint32_t pct;
624         uint32_t mask;
625         int i;
626         va_start(ap, nlevels);
627         for (i=0;i<nlevels;i++) {
628                 pct = va_arg(ap, uint32_t);
629                 mask = va_arg(ap, uint32_t);
630                 if (pct == 100 || gen_chance(pct)) {
631                         va_end(ap);
632                         return mask & random();
633                 }
634         }
635         va_end(ap);
636         return 0;
637 }
638
639 /*
640   generate a bitmask
641 */
642 static uint32_t gen_bits_mask(unsigned int mask)
643 {
644         unsigned int ret = random();
645         return ret & mask;
646 }
647
648 /*
649   generate a bitmask with high probability of the first mask
650   and low of the second
651 */
652 static uint32_t gen_bits_mask2(uint32_t mask1, uint32_t mask2)
653 {
654         if (!options.valid && gen_chance(10)) return gen_bits_mask(mask2);
655         return gen_bits_mask(mask1);
656 }
657
658 /*
659   generate reserved values
660  */
661 static uint64_t gen_reserved8(void)
662 {
663         if (options.valid) return 0;
664         return gen_bits_mask(0xFF);
665 }
666
667 static uint64_t gen_reserved16(void)
668 {
669         if (options.valid) return 0;
670         return gen_bits_mask(0xFFFF);
671 }
672
673 static uint64_t gen_reserved32(void)
674 {
675         if (options.valid) return 0;
676         return gen_bits_mask(0xFFFFFFFF);
677 }
678
679 static uint64_t gen_reserved64(void)
680 {
681         if (options.valid) return 0;
682         return gen_bits_mask(0xFFFFFFFF) | (((uint64_t)gen_bits_mask(0xFFFFFFFF))<<32);
683 }
684
685
686
687 /*
688   generate a boolean
689 */
690 static bool gen_bool(void)
691 {
692         return gen_bits_mask2(0x1, 0xFF);
693 }
694
695 /*
696   generate ntrename flags
697 */
698 static uint16_t gen_rename_flags(void)
699 {
700         if (gen_chance(30)) return RENAME_FLAG_RENAME;
701         if (gen_chance(30)) return RENAME_FLAG_HARD_LINK;
702         if (gen_chance(30)) return RENAME_FLAG_COPY;
703         return gen_bits_mask(0xFFFF);
704 }
705
706 /*
707   generate a pid 
708 */
709 static uint16_t gen_pid(void)
710 {
711         if (gen_chance(10)) return gen_bits_mask(0xFFFF);
712         return getpid();
713 }
714
715 /*
716   return a set of lock flags
717 */
718 static uint16_t gen_lock_flags_smb2(void)
719 {
720         if (!options.valid && gen_chance(5))  return gen_bits_mask(0xFFFF);
721         if (gen_chance(20)) return gen_bits_mask(0x1F);
722         if (gen_chance(50)) return SMB2_LOCK_FLAG_UNLOCK;
723         return gen_bits_mask(SMB2_LOCK_FLAG_SHARED | 
724                              SMB2_LOCK_FLAG_EXCLUSIVE | 
725                              SMB2_LOCK_FLAG_FAIL_IMMEDIATELY);
726 }
727
728 /*
729   generate a lock count
730 */
731 static off_t gen_lock_count(void)
732 {
733         return gen_int_range(0, 3);
734 }
735
736 /*
737   generate a NT access mask
738 */
739 static uint32_t gen_access_mask(void)
740 {
741         uint32_t ret;
742         if (gen_chance(70)) return SEC_FLAG_MAXIMUM_ALLOWED;
743         if (gen_chance(70)) return SEC_FILE_ALL;
744         ret = gen_bits_mask(0xFFFFFFFF);
745         if (options.valid) ret &= ~SEC_MASK_INVALID;
746         return ret;
747 }
748
749 /*
750   return a lockingx lock mode
751 */
752 static uint16_t gen_lock_mode(void)
753 {
754         if (!options.valid && gen_chance(5))  return gen_bits_mask(0xFFFF);
755         if (gen_chance(20)) return gen_bits_mask(0x1F);
756         return gen_bits_mask(LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES);
757 }
758
759 /*
760   generate a ntcreatex flags field
761 */
762 static uint32_t gen_ntcreatex_flags(void)
763 {
764         if (gen_chance(70)) return NTCREATEX_FLAGS_EXTENDED;
765         return gen_bits_mask2(0x1F, 0xFFFFFFFF);
766 }
767
768 /*
769   generate a ntcreatex create options bitfield
770 */
771 static uint32_t gen_create_options(void)
772 {
773         if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
774         if (gen_chance(50)) return 0;
775         return gen_bits_mask(NTCREATEX_OPTIONS_DELETE_ON_CLOSE | NTCREATEX_OPTIONS_DIRECTORY);
776 }
777
778 /*
779   generate a ntcreatex open disposition
780 */
781 static uint32_t gen_open_disp(void)
782 {
783         if (gen_chance(50)) return NTCREATEX_DISP_OPEN_IF;
784         if (!options.valid && gen_chance(10)) return gen_bits_mask(0xFFFFFFFF);
785         return gen_int_range(0, 5);
786 }
787
788 /*
789   generate an openx open mode
790 */
791 static uint16_t gen_openx_mode(void)
792 {
793         if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFF);
794         if (gen_chance(20)) return gen_bits_mask(0xFF);
795         return OPENX_MODE_DENY_NONE | gen_bits_mask(0x3);
796 }
797
798 /*
799   generate an openx flags field
800 */
801 static uint16_t gen_openx_flags(void)
802 {
803         if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFF);
804         return gen_bits_mask(0x7);
805 }
806
807 /*
808   generate an openx open function
809 */
810 static uint16_t gen_openx_func(void)
811 {
812         if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFF);
813         return gen_bits_mask(0x13);
814 }
815
816 /*
817   generate a file attrib combination
818 */
819 static uint32_t gen_attrib(void)
820 {
821         uint32_t ret;
822         if (gen_chance(20)) {
823                 ret = gen_bits_mask(0xFFFFFFFF);
824                 if (options.valid) ret &= FILE_ATTRIBUTE_ALL_MASK;
825                 return ret;
826         }
827         return gen_bits_mask(FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
828 }
829
830 /*
831   generate a unix timestamp
832 */
833 static time_t gen_timet(void)
834 {
835         if (gen_chance(30)) return 0;
836         return (time_t)random();
837 }
838
839 /*
840   generate a milliseconds protocol timeout
841 */
842 static uint32_t gen_timeout(void)
843 {
844         if (gen_chance(98)) return 0;
845         return random() % 50;
846 }
847
848 /*
849   generate a timestamp
850 */
851 static NTTIME gen_nttime(void)
852 {
853         NTTIME ret;
854         unix_to_nt_time(&ret, gen_timet());
855         return ret;
856 }
857
858 /*
859   generate a timewarp value
860 */
861 static NTTIME gen_timewarp(void)
862 {
863         NTTIME ret = gen_nttime();
864         if (gen_chance(98)) ret = 0;
865         return ret;
866 }
867
868 /*
869   generate a file allocation size
870 */
871 static unsigned int gen_alloc_size(void)
872 {
873         unsigned int ret;
874
875         if (gen_chance(30)) return 0;
876
877         ret = random() % 4*1024*1024;
878         /* give a high chance of a round number */
879         if (gen_chance(60)) {
880                 ret &= ~(1024*1024 - 1);
881         }
882         return ret;
883 }
884
885 /*
886   generate an ea_struct
887 */
888 static struct ea_struct gen_ea_struct(void)
889 {
890         struct ea_struct ea;
891         const char *names[] = {"EAONE", 
892                                "", 
893                                "FOO!", 
894                                " WITH SPACES ", 
895                                ".", 
896                                "AVERYLONGATTRIBUTENAME"};
897         const char *values[] = {"VALUE1", 
898                                "", 
899                                "NOT MUCH FOO", 
900                                " LEADING SPACES ", 
901                                ":", 
902                                "ASOMEWHATLONGERATTRIBUTEVALUE"};
903         int i;
904
905         ZERO_STRUCT(ea);
906
907         do {
908                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
909         } while (ignore_pattern(names[i]));
910
911         ea.name.s = names[i];
912
913         do {
914                 i = gen_int_range(0, ARRAY_SIZE(values)-1);
915         } while (ignore_pattern(values[i]));
916
917         ea.value = data_blob(values[i], strlen(values[i]));
918
919         if (gen_chance(10)) ea.flags = gen_bits_mask(0xFF);
920         ea.flags = 0;
921
922         return ea;
923 }
924
925 /*
926   generate an ea_struct
927 */
928 static struct smb_ea_list gen_ea_list(void)
929 {
930         struct smb_ea_list eas;
931         int i;
932         if (options.no_eas) {
933                 ZERO_STRUCT(eas);
934                 return eas;
935         }
936         eas.num_eas = gen_int_range(0, 3);
937         eas.eas = talloc_array(current_op.mem_ctx, struct ea_struct, eas.num_eas);
938         for (i=0;i<eas.num_eas;i++) {
939                 eas.eas[i] = gen_ea_struct();
940         }
941         return eas;
942 }
943
944 /* generate a security descriptor */
945 static struct security_descriptor *gen_sec_desc(void)
946 {
947         struct security_descriptor *sd;
948         if (options.no_acls || gen_chance(90)) return NULL;
949
950         sd = security_descriptor_dacl_create(current_op.mem_ctx,
951                                              0, NULL, NULL,
952                                              NULL,
953                                              SEC_ACE_TYPE_ACCESS_ALLOWED,
954                                              SEC_FILE_WRITE_DATA | SEC_STD_WRITE_DAC,
955                                              SEC_ACE_FLAG_OBJECT_INHERIT,
956                                              SID_WORLD,
957                                              SEC_ACE_TYPE_ACCESS_ALLOWED,
958                                              SEC_FILE_ALL | SEC_STD_ALL,
959                                              0,
960                                              NULL);
961         return sd;
962 }
963
964
965 static void oplock_handler_close_recv_smb(struct smbcli_request *req)
966 {
967         NTSTATUS status;
968         status = smbcli_request_simple_recv(req);
969         if (!NT_STATUS_IS_OK(status)) {
970                 printf("close failed in oplock_handler\n");
971                 smb_panic("close failed in oplock_handler");
972         }
973 }
974
975 /*
976   the oplock handler will either ack the break or close the file
977 */
978 static bool oplock_handler_smb(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private_data)
979 {
980         union smb_close io;
981         int i, j;
982         bool do_close;
983         struct smbcli_tree *tree = NULL;
984         struct smbcli_request *req;
985
986         srandom(current_op.seed);
987         do_close = gen_chance(50);
988
989         for (i=0;i<NSERVERS;i++) {
990                 for (j=0;j<NINSTANCES;j++) {
991                         if (transport == servers[i].smb_tree[j]->session->transport &&
992                             tid == servers[i].smb_tree[j]->tid) {
993                                 oplocks[i][j].got_break = true;
994                                 oplocks[i][j].smb_handle = fnum;
995                                 oplocks[i][j].handle = fnum_to_handle_smb(i, j, fnum);
996                                 oplocks[i][j].level = level;
997                                 oplocks[i][j].do_close = do_close;
998                                 tree = servers[i].smb_tree[j];
999                         }
1000                 }
1001         }
1002
1003         if (!tree) {
1004                 printf("Oplock break not for one of our trees!?\n");
1005                 return false;
1006         }
1007
1008         if (!do_close) {
1009                 printf("oplock ack fnum=%d\n", fnum);
1010                 return smbcli_oplock_ack(tree, fnum, level);
1011         }
1012
1013         printf("oplock close fnum=%d\n", fnum);
1014
1015         io.close.level = RAW_CLOSE_CLOSE;
1016         io.close.in.file.fnum = fnum;
1017         io.close.in.write_time = 0;
1018         req = smb_raw_close_send(tree, &io);
1019
1020         if (req == NULL) {
1021                 printf("WARNING: close failed in oplock_handler_close\n");
1022                 return false;
1023         }
1024
1025         req->async.fn = oplock_handler_close_recv_smb;
1026         req->async.private_data = NULL;
1027
1028         return true;
1029 }
1030
1031
1032 /*
1033   the idle function tries to cope with getting an oplock break on a connection, and
1034   an operation on another connection blocking until that break is acked
1035   we check for operations on all transports in the idle function
1036 */
1037 static void idle_func_smb(struct smbcli_transport *transport, void *private_data)
1038 {
1039         int i, j;
1040         for (i=0;i<NSERVERS;i++) {
1041                 for (j=0;j<NINSTANCES;j++) {
1042                         if (servers[i].smb_tree[j] &&
1043                             transport != servers[i].smb_tree[j]->session->transport) {
1044                                 smbcli_transport_process(servers[i].smb_tree[j]->session->transport);
1045                         }
1046                 }
1047         }
1048
1049 }
1050
1051 static void oplock_handler_close_recv_smb2(struct smb2_request *req)
1052 {
1053         NTSTATUS status;
1054         struct smb2_close io;
1055         status = smb2_close_recv(req, &io);
1056         if (!NT_STATUS_IS_OK(status)) {
1057                 printf("close failed in oplock_handler\n");
1058                 smb_panic("close failed in oplock_handler");
1059         }
1060 }
1061
1062 static void oplock_handler_ack_callback_smb2(struct smb2_request *req)
1063 {
1064         NTSTATUS status;
1065         struct smb2_break br;
1066
1067         status = smb2_break_recv(req, &br);
1068         if (!NT_STATUS_IS_OK(status)) {
1069                 printf("oplock break ack failed in oplock_handler\n");
1070                 smb_panic("oplock break ack failed in oplock_handler");
1071         }
1072 }
1073
1074 static bool send_oplock_ack_smb2(struct smb2_tree *tree, struct smb2_handle handle, 
1075                                  uint8_t level)
1076 {
1077         struct smb2_break br;
1078         struct smb2_request *req;
1079
1080         ZERO_STRUCT(br);
1081         br.in.file.handle       = handle;
1082         br.in.oplock_level      = level;
1083         br.in.reserved          = gen_reserved8();
1084         br.in.reserved2         = gen_reserved32();
1085
1086         req = smb2_break_send(tree, &br);
1087         if (req == NULL) return false;
1088         req->async.fn = oplock_handler_ack_callback_smb2;
1089         req->async.private_data = NULL;
1090         return true;
1091 }
1092
1093 /*
1094   the oplock handler will either ack the break or close the file
1095 */
1096 static bool oplock_handler_smb2(struct smb2_transport *transport, const struct smb2_handle *handle, 
1097                                 uint8_t level, void *private_data)
1098 {
1099         struct smb2_close io;
1100         unsigned i, j;
1101         bool do_close;
1102         struct smb2_tree *tree = NULL;
1103         struct smb2_request *req;
1104
1105         srandom(current_op.seed);
1106         do_close = gen_chance(50);
1107
1108         i = ((uintptr_t)private_data) >> 8;
1109         j = ((uintptr_t)private_data) & 0xFF;
1110
1111         if (i >= NSERVERS || j >= NINSTANCES) {
1112                 printf("Bad private_data in oplock_handler\n");
1113                 return false;
1114         }
1115
1116         oplocks[i][j].got_break = true;
1117         oplocks[i][j].smb2_handle = *handle;
1118         oplocks[i][j].handle = fnum_to_handle_smb2(i, j, *handle);
1119         oplocks[i][j].level = level;
1120         oplocks[i][j].do_close = do_close;
1121         tree = talloc_get_type(servers[i].smb2_tree[j], struct smb2_tree);
1122
1123         if (!tree) {
1124                 printf("Oplock break not for one of our trees!?\n");
1125                 return false;
1126         }
1127
1128         if (!do_close) {
1129                 printf("oplock ack handle=%d\n", oplocks[i][j].handle);
1130                 return send_oplock_ack_smb2(tree, *handle, level);
1131         }
1132
1133         printf("oplock close fnum=%d\n", oplocks[i][j].handle);
1134
1135         ZERO_STRUCT(io);
1136         io.in.file.handle = *handle;
1137         io.in.flags = 0;
1138         req = smb2_close_send(tree, &io);
1139
1140         if (req == NULL) {
1141                 printf("WARNING: close failed in oplock_handler_close\n");
1142                 return false;
1143         }
1144
1145         req->async.fn = oplock_handler_close_recv_smb2;
1146         req->async.private_data = NULL;
1147
1148         return true;
1149 }
1150
1151
1152 /*
1153   the idle function tries to cope with getting an oplock break on a connection, and
1154   an operation on another connection blocking until that break is acked
1155   we check for operations on all transports in the idle function
1156 */
1157 static void idle_func_smb2(struct smb2_transport *transport, void *private_data)
1158 {
1159         int i, j;
1160         for (i=0;i<NSERVERS;i++) {
1161                 for (j=0;j<NINSTANCES;j++) {
1162                         if (servers[i].smb2_tree[j] &&
1163                             transport != servers[i].smb2_tree[j]->session->transport) {
1164                                 // smb2_transport_process(servers[i].smb2_tree[j]->session->transport);
1165                         }
1166                 }
1167         }
1168
1169 }
1170
1171
1172 /*
1173   compare NTSTATUS, using checking ignored patterns
1174 */
1175 static bool compare_status(NTSTATUS status1, NTSTATUS status2)
1176 {
1177         char *s;
1178
1179         if (NT_STATUS_EQUAL(status1, status2)) return true;
1180
1181         /* one code being an error and the other OK is always an error */
1182         if (NT_STATUS_IS_OK(status1) || NT_STATUS_IS_OK(status2)) {
1183                 current_op.mismatch = nt_errstr(status1);
1184                 return false;
1185         }
1186
1187         /* if we are ignoring one of the status codes then consider this a match */
1188         if (ignore_pattern(nt_errstr(status1)) ||
1189             ignore_pattern(nt_errstr(status2))) {
1190                 return true;
1191         }
1192
1193         /* also support ignore patterns of the form NT_STATUS_XX:NT_STATUS_YY
1194            meaning that the first server returns NT_STATUS_XX and the 2nd
1195            returns NT_STATUS_YY */
1196         s = talloc_asprintf(current_op.mem_ctx, "%s:%s", 
1197                             nt_errstr(status1), 
1198                             nt_errstr(status2));
1199         if (ignore_pattern(s)) {
1200                 return true;
1201         }
1202
1203         current_op.mismatch = nt_errstr(status1);
1204         return false;
1205 }
1206
1207 /*
1208   check for pending packets on all connections
1209 */
1210 static void check_pending(void)
1211 {
1212         int i, j;
1213
1214         smb_msleep(20);
1215
1216         for (j=0;j<NINSTANCES;j++) {
1217                 for (i=0;i<NSERVERS;i++) {
1218                         // smb2_transport_process(servers[i].smb2_tree[j]->session->transport);
1219                 }
1220         }       
1221 }
1222
1223 /*
1224   check that the same oplock breaks have been received by all instances
1225 */
1226 static bool check_oplocks(const char *call)
1227 {
1228         int i, j;
1229         int tries = 0;
1230
1231         if (!options.use_oplocks || options.smb2) {
1232                 /* no smb2 oplocks in gentest yet */
1233                 return true;
1234         }
1235
1236 again:
1237         check_pending();
1238
1239         for (j=0;j<NINSTANCES;j++) {
1240                 for (i=1;i<NSERVERS;i++) {
1241                         if (oplocks[0][j].got_break != oplocks[i][j].got_break ||
1242                             oplocks[0][j].handle != oplocks[i][j].handle ||
1243                             oplocks[0][j].level != oplocks[i][j].level) {
1244                                 if (tries++ < 10) goto again;
1245                                 printf("oplock break inconsistent - %d/%d/%d vs %d/%d/%d\n",
1246                                        oplocks[0][j].got_break, 
1247                                        oplocks[0][j].handle, 
1248                                        oplocks[0][j].level, 
1249                                        oplocks[i][j].got_break, 
1250                                        oplocks[i][j].handle, 
1251                                        oplocks[i][j].level);
1252                                 current_op.mismatch = "oplock break";
1253                                 return false;
1254                         }
1255                 }
1256         }
1257
1258         /* if we got a break and closed then remove the handle */
1259         for (j=0;j<NINSTANCES;j++) {
1260                 if (oplocks[0][j].got_break &&
1261                     oplocks[0][j].do_close) {
1262                         uint16_t fnums[NSERVERS];
1263                         for (i=0;i<NSERVERS;i++) {
1264                                 fnums[i] = oplocks[i][j].smb_handle;
1265                         }
1266                         gen_remove_handle_smb(j, fnums);
1267                         break;
1268                 }
1269         }       
1270         return true;
1271 }
1272
1273
1274 /*
1275   check that the same change notify info has been received by all instances
1276 */
1277 static bool check_notifies(const char *call)
1278 {
1279         int i, j;
1280         int tries = 0;
1281
1282         if (options.smb2) {
1283                 /* no smb2 notifies in gentest yet */
1284                 return true;
1285         }
1286
1287 again:
1288         check_pending();
1289
1290         for (j=0;j<NINSTANCES;j++) {
1291                 for (i=1;i<NSERVERS;i++) {
1292                         int n;
1293                         union smb_notify not1, not2;
1294
1295                         if (notifies[0][j].notify_count != notifies[i][j].notify_count) {
1296                                 if (tries++ < 10) goto again;
1297                                 printf("Notify count inconsistent %d %d\n",
1298                                        notifies[0][j].notify_count,
1299                                        notifies[i][j].notify_count);
1300                                 current_op.mismatch = "notify count";
1301                                 return false;
1302                         }
1303
1304                         if (notifies[0][j].notify_count == 0) continue;
1305
1306                         if (!NT_STATUS_EQUAL(notifies[0][j].status,
1307                                              notifies[i][j].status)) {
1308                                 printf("Notify status mismatch - %s - %s\n",
1309                                        nt_errstr(notifies[0][j].status),
1310                                        nt_errstr(notifies[i][j].status));
1311                                 current_op.mismatch = "Notify status";
1312                                 return false;
1313                         }
1314
1315                         if (!NT_STATUS_IS_OK(notifies[0][j].status)) {
1316                                 continue;
1317                         }
1318
1319                         not1 = notifies[0][j].notify;
1320                         not2 = notifies[i][j].notify;
1321
1322                         for (n=0;n<not1.nttrans.out.num_changes;n++) {
1323                                 if (not1.nttrans.out.changes[n].action != 
1324                                     not2.nttrans.out.changes[n].action) {
1325                                         printf("Notify action %d inconsistent %d %d\n", n,
1326                                                not1.nttrans.out.changes[n].action,
1327                                                not2.nttrans.out.changes[n].action);
1328                                         current_op.mismatch = "notify action";
1329                                         return false;
1330                                 }
1331                                 if (strcmp(not1.nttrans.out.changes[n].name.s,
1332                                            not2.nttrans.out.changes[n].name.s)) {
1333                                         printf("Notify name %d inconsistent %s %s\n", n,
1334                                                not1.nttrans.out.changes[n].name.s,
1335                                                not2.nttrans.out.changes[n].name.s);
1336                                         current_op.mismatch = "notify name";
1337                                         return false;
1338                                 }
1339                                 if (not1.nttrans.out.changes[n].name.private_length !=
1340                                     not2.nttrans.out.changes[n].name.private_length) {
1341                                         printf("Notify name length %d inconsistent %d %d\n", n,
1342                                                not1.nttrans.out.changes[n].name.private_length,
1343                                                not2.nttrans.out.changes[n].name.private_length);
1344                                         current_op.mismatch = "notify name length";
1345                                         return false;
1346                                 }
1347                         }
1348                 }
1349         }
1350
1351         ZERO_STRUCT(notifies);
1352
1353         return true;
1354 }
1355
1356 #define GEN_COPY_PARM do { \
1357         int i; \
1358         for (i=1;i<NSERVERS;i++) { \
1359                 parm[i] = parm[0]; \
1360         } \
1361 } while (0)
1362
1363 #define GEN_CALL(call, treetype, treefield) do {                \
1364         int i; \
1365         ZERO_STRUCT(oplocks); \
1366         ZERO_STRUCT(notifies); \
1367         for (i=0;i<NSERVERS;i++) { \
1368                 struct treetype *tree = servers[i].treefield[instance]; \
1369                 status[i] = call; \
1370         } \
1371         current_op.status = status[0]; \
1372         for (i=1;i<NSERVERS;i++) { \
1373                 if (!compare_status(status[0], status[1])) { \
1374                         printf("status different in %s - %s %s\n", #call, \
1375                                nt_errstr(status[0]), nt_errstr(status[i])); \
1376                         current_op.mismatch = nt_errstr(status[0]); \
1377                         return false; \
1378                 } \
1379         } \
1380         if (!check_oplocks(#call)) return false;        \
1381         if (!check_notifies(#call)) return false;       \
1382         if (!NT_STATUS_IS_OK(status[0])) { \
1383                 return true; \
1384         } \
1385 } while(0)
1386
1387 #define GEN_CALL_SMB(call) GEN_CALL(call, smbcli_tree, smb_tree)
1388 #define GEN_CALL_SMB2(call) GEN_CALL(call, smb2_tree, smb2_tree)
1389
1390 #define ADD_HANDLE_SMB2(name, field) do { \
1391         struct smb2_handle handles[NSERVERS]; \
1392         int i; \
1393         for (i=0;i<NSERVERS;i++) { \
1394                 handles[i] = parm[i].field; \
1395         } \
1396         gen_add_handle_smb2(instance, name, handles); \
1397 } while(0)
1398
1399 #define REMOVE_HANDLE_SMB2(field) do { \
1400         struct smb2_handle handles[NSERVERS]; \
1401         int i; \
1402         for (i=0;i<NSERVERS;i++) { \
1403                 handles[i] = parm[i].field; \
1404         } \
1405         gen_remove_handle_smb2(instance, handles); \
1406 } while(0)
1407
1408 #define ADD_HANDLE_SMB(name, field) do { \
1409         uint16_t handles[NSERVERS]; \
1410         int i; \
1411         for (i=0;i<NSERVERS;i++) { \
1412                 handles[i] = parm[i].field; \
1413         } \
1414         gen_add_handle_smb(instance, name, handles); \
1415 } while(0)
1416
1417 #define REMOVE_HANDLE_SMB(field) do { \
1418         uint16_t handles[NSERVERS]; \
1419         int i; \
1420         for (i=0;i<NSERVERS;i++) { \
1421                 handles[i] = parm[i].field; \
1422         } \
1423         gen_remove_handle_smb(instance, handles); \
1424 } while(0)
1425
1426 #define GEN_SET_FNUM_SMB2(field) do { \
1427         int i; \
1428         for (i=0;i<NSERVERS;i++) { \
1429                 parm[i].field = gen_lookup_handle_smb2(i, parm[i].field.data[0]); \
1430         } \
1431 } while(0)
1432
1433 #define GEN_SET_FNUM_SMB(field) do { \
1434         int i; \
1435         for (i=0;i<NSERVERS;i++) { \
1436                 parm[i].field = gen_lookup_handle_smb(i, parm[i].field); \
1437         } \
1438 } while(0)
1439
1440 #define CHECK_EQUAL(field) do { \
1441         if (parm[0].field != parm[1].field && !ignore_pattern(#field)) { \
1442                 current_op.mismatch = #field; \
1443                 printf("Mismatch in %s - 0x%llx 0x%llx\n", #field, \
1444                        (unsigned long long)parm[0].field, (unsigned long long)parm[1].field); \
1445                 return false; \
1446         } \
1447 } while(0)
1448
1449 #define CHECK_SECDESC(field) do { \
1450         if (!security_acl_equal(parm[0].field->dacl, parm[1].field->dacl) && !ignore_pattern(#field)) { \
1451                 current_op.mismatch = #field; \
1452                 printf("Mismatch in %s\n", #field); \
1453                 return false;                       \
1454         } \
1455 } while(0)
1456
1457 #define CHECK_ATTRIB(field) do { \
1458                 if (!options.mask_indexing) { \
1459                 CHECK_EQUAL(field); \
1460         } else if ((~FILE_ATTRIBUTE_NONINDEXED & parm[0].field) != (~FILE_ATTRIBUTE_NONINDEXED & parm[1].field) && !ignore_pattern(#field)) { \
1461                 current_op.mismatch = #field; \
1462                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
1463                        (int)parm[0].field, (int)parm[1].field); \
1464                 return false; \
1465         } \
1466 } while(0)
1467
1468 #define CHECK_WSTR_EQUAL(field) do { \
1469         if ((!parm[0].field.s && parm[1].field.s) || (parm[0].field.s && !parm[1].field.s)) { \
1470                 current_op.mismatch = #field; \
1471                 printf("%s is NULL!\n", #field); \
1472                 return false; \
1473         } \
1474         if (parm[0].field.s && strcmp(parm[0].field.s, parm[1].field.s) != 0 && !ignore_pattern(#field)) { \
1475                 current_op.mismatch = #field; \
1476                 printf("Mismatch in %s - %s %s\n", #field, \
1477                        parm[0].field.s, parm[1].field.s); \
1478                 return false; \
1479         } \
1480         CHECK_EQUAL(field.private_length); \
1481 } while(0)
1482
1483 #define CHECK_BLOB_EQUAL(field) do { \
1484         if (((parm[0].field.data == NULL && parm[1].field.data != NULL) || \
1485             (parm[1].field.data == NULL && parm[0].field.data != NULL) || \
1486             (memcmp(parm[0].field.data, parm[1].field.data, parm[0].field.length) != 0)) && !ignore_pattern(#field)) { \
1487                 current_op.mismatch = #field; \
1488                 printf("Mismatch in %s\n", #field); \
1489                 return false; \
1490         } \
1491         CHECK_EQUAL(field.length); \
1492 } while(0)
1493
1494 #define CHECK_TIMES_EQUAL(field) do { \
1495         if (labs(parm[0].field - parm[1].field) > time_skew() && \
1496             !ignore_pattern(#field)) { \
1497                 current_op.mismatch = #field; \
1498                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
1499                        (int)parm[0].field, (int)parm[1].field); \
1500                 return false; \
1501         } \
1502 } while(0)
1503
1504 #define CHECK_NTTIMES_EQUAL(field) do { \
1505         if (labs(nt_time_to_unix(parm[0].field) - \
1506                 nt_time_to_unix(parm[1].field)) > time_skew() && \
1507             !ignore_pattern(#field)) { \
1508                 current_op.mismatch = #field; \
1509                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
1510                        (int)nt_time_to_unix(parm[0].field), \
1511                        (int)nt_time_to_unix(parm[1].field)); \
1512                 return false; \
1513         } \
1514 } while(0)
1515
1516
1517 /*
1518   compare returned fileinfo structures
1519 */
1520 static bool cmp_fileinfo(int instance, 
1521                          union smb_fileinfo parm[NSERVERS],
1522                          NTSTATUS status[NSERVERS])
1523 {
1524         int i;
1525         enum smb_fileinfo_level level = parm[0].generic.level;
1526
1527         if (level == RAW_FILEINFO_ALL_INFORMATION &&
1528             options.smb2) {
1529                 level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
1530         }
1531
1532         switch (level) {
1533         case RAW_FILEINFO_GENERIC:
1534                 return false;
1535
1536         case RAW_FILEINFO_GETATTR:
1537                 CHECK_ATTRIB(getattr.out.attrib);
1538                 CHECK_EQUAL(getattr.out.size);
1539                 CHECK_TIMES_EQUAL(getattr.out.write_time);
1540                 break;
1541
1542         case RAW_FILEINFO_GETATTRE:
1543                 CHECK_TIMES_EQUAL(getattre.out.create_time);
1544                 CHECK_TIMES_EQUAL(getattre.out.access_time);
1545                 CHECK_TIMES_EQUAL(getattre.out.write_time);
1546                 CHECK_EQUAL(getattre.out.size);
1547                 CHECK_EQUAL(getattre.out.alloc_size);
1548                 CHECK_ATTRIB(getattre.out.attrib);
1549                 break;
1550
1551         case RAW_FILEINFO_STANDARD:
1552                 CHECK_TIMES_EQUAL(standard.out.create_time);
1553                 CHECK_TIMES_EQUAL(standard.out.access_time);
1554                 CHECK_TIMES_EQUAL(standard.out.write_time);
1555                 CHECK_EQUAL(standard.out.size);
1556                 CHECK_EQUAL(standard.out.alloc_size);
1557                 CHECK_ATTRIB(standard.out.attrib);
1558                 break;
1559
1560         case RAW_FILEINFO_EA_SIZE:
1561                 CHECK_TIMES_EQUAL(ea_size.out.create_time);
1562                 CHECK_TIMES_EQUAL(ea_size.out.access_time);
1563                 CHECK_TIMES_EQUAL(ea_size.out.write_time);
1564                 CHECK_EQUAL(ea_size.out.size);
1565                 CHECK_EQUAL(ea_size.out.alloc_size);
1566                 CHECK_ATTRIB(ea_size.out.attrib);
1567                 CHECK_EQUAL(ea_size.out.ea_size);
1568                 break;
1569
1570         case RAW_FILEINFO_ALL_EAS:
1571                 CHECK_EQUAL(all_eas.out.num_eas);
1572                 for (i=0;i<parm[0].all_eas.out.num_eas;i++) {
1573                         CHECK_EQUAL(all_eas.out.eas[i].flags);
1574                         CHECK_WSTR_EQUAL(all_eas.out.eas[i].name);
1575                         CHECK_BLOB_EQUAL(all_eas.out.eas[i].value);
1576                 }
1577                 break;
1578
1579         case RAW_FILEINFO_IS_NAME_VALID:
1580                 break;
1581                 
1582         case RAW_FILEINFO_BASIC_INFO:
1583         case RAW_FILEINFO_BASIC_INFORMATION:
1584                 CHECK_NTTIMES_EQUAL(basic_info.out.create_time);
1585                 CHECK_NTTIMES_EQUAL(basic_info.out.access_time);
1586                 CHECK_NTTIMES_EQUAL(basic_info.out.write_time);
1587                 CHECK_NTTIMES_EQUAL(basic_info.out.change_time);
1588                 CHECK_ATTRIB(basic_info.out.attrib);
1589                 break;
1590
1591         case RAW_FILEINFO_STANDARD_INFO:
1592         case RAW_FILEINFO_STANDARD_INFORMATION:
1593                 CHECK_EQUAL(standard_info.out.alloc_size);
1594                 CHECK_EQUAL(standard_info.out.size);
1595                 CHECK_EQUAL(standard_info.out.nlink);
1596                 CHECK_EQUAL(standard_info.out.delete_pending);
1597                 CHECK_EQUAL(standard_info.out.directory);
1598                 break;
1599
1600         case RAW_FILEINFO_EA_INFO:
1601         case RAW_FILEINFO_EA_INFORMATION:
1602                 CHECK_EQUAL(ea_info.out.ea_size);
1603                 break;
1604
1605         case RAW_FILEINFO_NAME_INFO:
1606         case RAW_FILEINFO_NAME_INFORMATION:
1607                 CHECK_WSTR_EQUAL(name_info.out.fname);
1608                 break;
1609
1610         case RAW_FILEINFO_ALL_INFO:
1611         case RAW_FILEINFO_ALL_INFORMATION:
1612                 CHECK_NTTIMES_EQUAL(all_info.out.create_time);
1613                 CHECK_NTTIMES_EQUAL(all_info.out.access_time);
1614                 CHECK_NTTIMES_EQUAL(all_info.out.write_time);
1615                 CHECK_NTTIMES_EQUAL(all_info.out.change_time);
1616                 CHECK_ATTRIB(all_info.out.attrib);
1617                 CHECK_EQUAL(all_info.out.alloc_size);
1618                 CHECK_EQUAL(all_info.out.size);
1619                 CHECK_EQUAL(all_info.out.nlink);
1620                 CHECK_EQUAL(all_info.out.delete_pending);
1621                 CHECK_EQUAL(all_info.out.directory);
1622                 CHECK_EQUAL(all_info.out.ea_size);
1623                 CHECK_WSTR_EQUAL(all_info.out.fname);
1624                 break;
1625
1626         case RAW_FILEINFO_ALT_NAME_INFO:
1627         case RAW_FILEINFO_ALT_NAME_INFORMATION:
1628                 CHECK_WSTR_EQUAL(alt_name_info.out.fname);
1629                 break;
1630
1631         case RAW_FILEINFO_STREAM_INFO:
1632         case RAW_FILEINFO_STREAM_INFORMATION:
1633                 CHECK_EQUAL(stream_info.out.num_streams);
1634                 for (i=0;i<parm[0].stream_info.out.num_streams;i++) {
1635                         CHECK_EQUAL(stream_info.out.streams[i].size);
1636                         CHECK_EQUAL(stream_info.out.streams[i].alloc_size);
1637                         CHECK_WSTR_EQUAL(stream_info.out.streams[i].stream_name);
1638                 }
1639                 break;
1640
1641         case RAW_FILEINFO_COMPRESSION_INFO:
1642         case RAW_FILEINFO_COMPRESSION_INFORMATION:
1643                 CHECK_EQUAL(compression_info.out.compressed_size);
1644                 CHECK_EQUAL(compression_info.out.format);
1645                 CHECK_EQUAL(compression_info.out.unit_shift);
1646                 CHECK_EQUAL(compression_info.out.chunk_shift);
1647                 CHECK_EQUAL(compression_info.out.cluster_shift);
1648                 break;
1649
1650         case RAW_FILEINFO_INTERNAL_INFORMATION:
1651                 CHECK_EQUAL(internal_information.out.file_id);
1652                 break;
1653
1654         case RAW_FILEINFO_ACCESS_INFORMATION:
1655                 CHECK_EQUAL(access_information.out.access_flags);
1656                 break;
1657
1658         case RAW_FILEINFO_POSITION_INFORMATION:
1659                 CHECK_EQUAL(position_information.out.position);
1660                 break;
1661
1662         case RAW_FILEINFO_MODE_INFORMATION:
1663                 CHECK_EQUAL(mode_information.out.mode);
1664                 break;
1665
1666         case RAW_FILEINFO_ALIGNMENT_INFORMATION:
1667                 CHECK_EQUAL(alignment_information.out.alignment_requirement);
1668                 break;
1669
1670         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1671                 CHECK_NTTIMES_EQUAL(network_open_information.out.create_time);
1672                 CHECK_NTTIMES_EQUAL(network_open_information.out.access_time);
1673                 CHECK_NTTIMES_EQUAL(network_open_information.out.write_time);
1674                 CHECK_NTTIMES_EQUAL(network_open_information.out.change_time);
1675                 CHECK_EQUAL(network_open_information.out.alloc_size);
1676                 CHECK_EQUAL(network_open_information.out.size);
1677                 CHECK_ATTRIB(network_open_information.out.attrib);
1678                 break;
1679
1680         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1681                 CHECK_ATTRIB(attribute_tag_information.out.attrib);
1682                 CHECK_EQUAL(attribute_tag_information.out.reparse_tag);
1683                 break;
1684
1685         case RAW_FILEINFO_SMB2_ALL_INFORMATION:
1686                 CHECK_NTTIMES_EQUAL(all_info2.out.create_time);
1687                 CHECK_NTTIMES_EQUAL(all_info2.out.access_time);
1688                 CHECK_NTTIMES_EQUAL(all_info2.out.write_time);
1689                 CHECK_NTTIMES_EQUAL(all_info2.out.change_time);
1690                 CHECK_ATTRIB(all_info2.out.attrib);
1691                 CHECK_EQUAL(all_info2.out.unknown1);
1692                 CHECK_EQUAL(all_info2.out.alloc_size);
1693                 CHECK_EQUAL(all_info2.out.size);
1694                 CHECK_EQUAL(all_info2.out.nlink);
1695                 CHECK_EQUAL(all_info2.out.delete_pending);
1696                 CHECK_EQUAL(all_info2.out.directory);
1697                 CHECK_EQUAL(all_info2.out.file_id);
1698                 CHECK_EQUAL(all_info2.out.ea_size);
1699                 CHECK_EQUAL(all_info2.out.access_mask);
1700                 CHECK_EQUAL(all_info2.out.position);
1701                 CHECK_EQUAL(all_info2.out.mode);
1702                 CHECK_EQUAL(all_info2.out.alignment_requirement);
1703                 CHECK_WSTR_EQUAL(all_info2.out.fname);
1704                 break;
1705
1706         case RAW_FILEINFO_SMB2_ALL_EAS:
1707                 CHECK_EQUAL(all_eas.out.num_eas);
1708                 for (i=0;i<parm[0].all_eas.out.num_eas;i++) {
1709                         CHECK_EQUAL(all_eas.out.eas[i].flags);
1710                         CHECK_WSTR_EQUAL(all_eas.out.eas[i].name);
1711                         CHECK_BLOB_EQUAL(all_eas.out.eas[i].value);
1712                 }
1713                 break;
1714
1715         case RAW_FILEINFO_SEC_DESC:
1716                 CHECK_SECDESC(query_secdesc.out.sd);
1717                 break;
1718
1719                 /* Unhandled levels */
1720         case RAW_FILEINFO_EA_LIST:
1721         case RAW_FILEINFO_UNIX_BASIC:
1722         case RAW_FILEINFO_UNIX_LINK:
1723         case RAW_FILEINFO_UNIX_INFO2:
1724                 break;
1725         }
1726
1727         return true;
1728 }
1729
1730
1731
1732 /*
1733   generate openx operations
1734 */
1735 static bool handler_smb_openx(int instance)
1736 {
1737         union smb_open parm[NSERVERS];
1738         NTSTATUS status[NSERVERS];
1739
1740         parm[0].openx.level = RAW_OPEN_OPENX;
1741         parm[0].openx.in.flags = gen_openx_flags();
1742         parm[0].openx.in.open_mode = gen_openx_mode();
1743         parm[0].openx.in.search_attrs = gen_attrib();
1744         parm[0].openx.in.file_attrs = gen_attrib();
1745         parm[0].openx.in.write_time = gen_timet();
1746         parm[0].openx.in.open_func = gen_openx_func();
1747         parm[0].openx.in.size = gen_io_count();
1748         parm[0].openx.in.timeout = gen_timeout();
1749         parm[0].openx.in.fname = gen_fname_open(instance);
1750
1751         if (!options.use_oplocks) {
1752                 /* mask out oplocks */
1753                 parm[0].openx.in.flags &= ~(OPENX_FLAGS_REQUEST_OPLOCK|
1754                                             OPENX_FLAGS_REQUEST_BATCH_OPLOCK);
1755         }
1756         
1757         GEN_COPY_PARM;
1758         GEN_CALL_SMB(smb_raw_open(tree, current_op.mem_ctx, &parm[i]));
1759
1760         CHECK_ATTRIB(openx.out.attrib);
1761         CHECK_EQUAL(openx.out.size);
1762         CHECK_EQUAL(openx.out.access);
1763         CHECK_EQUAL(openx.out.ftype);
1764         CHECK_EQUAL(openx.out.devstate);
1765         CHECK_EQUAL(openx.out.action);
1766         CHECK_EQUAL(openx.out.access_mask);
1767         CHECK_EQUAL(openx.out.unknown);
1768         CHECK_TIMES_EQUAL(openx.out.write_time);
1769
1770         /* open creates a new file handle */
1771         ADD_HANDLE_SMB(parm[0].openx.in.fname, openx.out.file.fnum);
1772
1773         return true;
1774 }
1775
1776
1777 /*
1778   generate open operations
1779 */
1780 static bool handler_smb_open(int instance)
1781 {
1782         union smb_open parm[NSERVERS];
1783         NTSTATUS status[NSERVERS];
1784
1785         parm[0].openold.level = RAW_OPEN_OPEN;
1786         parm[0].openold.in.open_mode = gen_bits_mask2(0xF, 0xFFFF);
1787         parm[0].openold.in.search_attrs = gen_attrib();
1788         parm[0].openold.in.fname = gen_fname_open(instance);
1789
1790         if (!options.use_oplocks) {
1791                 /* mask out oplocks */
1792                 parm[0].openold.in.open_mode &= ~(OPENX_FLAGS_REQUEST_OPLOCK|
1793                                                   OPENX_FLAGS_REQUEST_BATCH_OPLOCK);
1794         }
1795         
1796         GEN_COPY_PARM;
1797         GEN_CALL_SMB(smb_raw_open(tree, current_op.mem_ctx, &parm[i]));
1798
1799         CHECK_ATTRIB(openold.out.attrib);
1800         CHECK_TIMES_EQUAL(openold.out.write_time);
1801         CHECK_EQUAL(openold.out.size);
1802         CHECK_EQUAL(openold.out.rmode);
1803
1804         /* open creates a new file handle */
1805         ADD_HANDLE_SMB(parm[0].openold.in.fname, openold.out.file.fnum);
1806
1807         return true;
1808 }
1809
1810
1811 /*
1812   generate ntcreatex operations
1813 */
1814 static bool handler_smb_ntcreatex(int instance)
1815 {
1816         union smb_open parm[NSERVERS];
1817         NTSTATUS status[NSERVERS];
1818
1819         parm[0].ntcreatex.level = RAW_OPEN_NTCREATEX;
1820         parm[0].ntcreatex.in.flags = gen_ntcreatex_flags();
1821         parm[0].ntcreatex.in.root_fid.fnum = gen_root_fid(instance);
1822         parm[0].ntcreatex.in.access_mask = gen_access_mask();
1823         parm[0].ntcreatex.in.alloc_size = gen_alloc_size();
1824         parm[0].ntcreatex.in.file_attr = gen_attrib();
1825         parm[0].ntcreatex.in.share_access = gen_bits_mask2(0x7, 0xFFFFFFFF);
1826         parm[0].ntcreatex.in.open_disposition = gen_open_disp();
1827         parm[0].ntcreatex.in.create_options = gen_create_options();
1828         parm[0].ntcreatex.in.impersonation = gen_bits_mask2(0, 0xFFFFFFFF);
1829         parm[0].ntcreatex.in.security_flags = gen_bits_mask2(0, 0xFF);
1830         parm[0].ntcreatex.in.fname = gen_fname_open(instance);
1831
1832         if (!options.use_oplocks) {
1833                 /* mask out oplocks */
1834                 parm[0].ntcreatex.in.flags &= ~(NTCREATEX_FLAGS_REQUEST_OPLOCK|
1835                                                 NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK);
1836         }
1837         
1838         GEN_COPY_PARM;
1839         if (parm[0].ntcreatex.in.root_fid.fnum != 0) {
1840                 GEN_SET_FNUM_SMB(ntcreatex.in.root_fid.fnum);
1841         }
1842         GEN_CALL_SMB(smb_raw_open(tree, current_op.mem_ctx, &parm[i]));
1843
1844         CHECK_EQUAL(ntcreatex.out.oplock_level);
1845         CHECK_EQUAL(ntcreatex.out.create_action);
1846         CHECK_NTTIMES_EQUAL(ntcreatex.out.create_time);
1847         CHECK_NTTIMES_EQUAL(ntcreatex.out.access_time);
1848         CHECK_NTTIMES_EQUAL(ntcreatex.out.write_time);
1849         CHECK_NTTIMES_EQUAL(ntcreatex.out.change_time);
1850         CHECK_ATTRIB(ntcreatex.out.attrib);
1851         CHECK_EQUAL(ntcreatex.out.alloc_size);
1852         CHECK_EQUAL(ntcreatex.out.size);
1853         CHECK_EQUAL(ntcreatex.out.file_type);
1854         CHECK_EQUAL(ntcreatex.out.ipc_state);
1855         CHECK_EQUAL(ntcreatex.out.is_directory);
1856
1857         /* ntcreatex creates a new file handle */
1858         ADD_HANDLE_SMB(parm[0].ntcreatex.in.fname, ntcreatex.out.file.fnum);
1859
1860         return true;
1861 }
1862
1863 /*
1864   generate close operations
1865 */
1866 static bool handler_smb_close(int instance)
1867 {
1868         union smb_close parm[NSERVERS];
1869         NTSTATUS status[NSERVERS];
1870
1871         parm[0].close.level = RAW_CLOSE_CLOSE;
1872         parm[0].close.in.file.fnum = gen_fnum_close(instance);
1873         parm[0].close.in.write_time = gen_timet();
1874
1875         GEN_COPY_PARM;
1876         GEN_SET_FNUM_SMB(close.in.file.fnum);
1877         GEN_CALL_SMB(smb_raw_close(tree, &parm[i]));
1878
1879         REMOVE_HANDLE_SMB(close.in.file.fnum);
1880
1881         return true;
1882 }
1883
1884 /*
1885   generate unlink operations
1886 */
1887 static bool handler_smb_unlink(int instance)
1888 {
1889         union smb_unlink parm[NSERVERS];
1890         NTSTATUS status[NSERVERS];
1891
1892         parm[0].unlink.in.pattern = gen_pattern();
1893         parm[0].unlink.in.attrib = gen_attrib();
1894
1895         GEN_COPY_PARM;
1896         GEN_CALL_SMB(smb_raw_unlink(tree, &parm[i]));
1897
1898         return true;
1899 }
1900
1901 /*
1902   generate chkpath operations
1903 */
1904 static bool handler_smb_chkpath(int instance)
1905 {
1906         union smb_chkpath parm[NSERVERS];
1907         NTSTATUS status[NSERVERS];
1908
1909         parm[0].chkpath.in.path = gen_fname_open(instance);
1910
1911         GEN_COPY_PARM;
1912         GEN_CALL_SMB(smb_raw_chkpath(tree, &parm[i]));
1913
1914         return true;
1915 }
1916
1917 /*
1918   generate mkdir operations
1919 */
1920 static bool handler_smb_mkdir(int instance)
1921 {
1922         union smb_mkdir parm[NSERVERS];
1923         NTSTATUS status[NSERVERS];
1924
1925         parm[0].mkdir.level = RAW_MKDIR_MKDIR;
1926         parm[0].mkdir.in.path = gen_fname_open(instance);
1927
1928         GEN_COPY_PARM;
1929         GEN_CALL_SMB(smb_raw_mkdir(tree, &parm[i]));
1930
1931         return true;
1932 }
1933
1934 /*
1935   generate rmdir operations
1936 */
1937 static bool handler_smb_rmdir(int instance)
1938 {
1939         struct smb_rmdir parm[NSERVERS];
1940         NTSTATUS status[NSERVERS];
1941
1942         parm[0].in.path = gen_fname_open(instance);
1943
1944         GEN_COPY_PARM;
1945         GEN_CALL_SMB(smb_raw_rmdir(tree, &parm[i]));
1946
1947         return true;
1948 }
1949
1950 /*
1951   generate rename operations
1952 */
1953 static bool handler_smb_rename(int instance)
1954 {
1955         union smb_rename parm[NSERVERS];
1956         NTSTATUS status[NSERVERS];
1957
1958         parm[0].generic.level = RAW_RENAME_RENAME;
1959         parm[0].rename.in.pattern1 = gen_pattern();
1960         parm[0].rename.in.pattern2 = gen_pattern();
1961         parm[0].rename.in.attrib = gen_attrib();
1962
1963         GEN_COPY_PARM;
1964         GEN_CALL_SMB(smb_raw_rename(tree, &parm[i]));
1965
1966         return true;
1967 }
1968
1969 /*
1970   generate ntrename operations
1971 */
1972 static bool handler_smb_ntrename(int instance)
1973 {
1974         union smb_rename parm[NSERVERS];
1975         NTSTATUS status[NSERVERS];
1976
1977         parm[0].generic.level = RAW_RENAME_NTRENAME;
1978         parm[0].ntrename.in.old_name = gen_fname();
1979         parm[0].ntrename.in.new_name = gen_fname();
1980         parm[0].ntrename.in.attrib = gen_attrib();
1981         parm[0].ntrename.in.cluster_size = gen_bits_mask2(0, 0xFFFFFFF);
1982         parm[0].ntrename.in.flags = gen_rename_flags();
1983
1984         GEN_COPY_PARM;
1985         GEN_CALL_SMB(smb_raw_rename(tree, &parm[i]));
1986
1987         return true;
1988 }
1989
1990
1991 /*
1992   generate seek operations
1993 */
1994 static bool handler_smb_seek(int instance)
1995 {
1996         union smb_seek parm[NSERVERS];
1997         NTSTATUS status[NSERVERS];
1998
1999         parm[0].lseek.in.file.fnum = gen_fnum(instance);
2000         parm[0].lseek.in.mode = gen_bits_mask2(0x3, 0xFFFF);
2001         parm[0].lseek.in.offset = gen_offset();
2002
2003         GEN_COPY_PARM;
2004         GEN_SET_FNUM_SMB(lseek.in.file.fnum);
2005         GEN_CALL_SMB(smb_raw_seek(tree, &parm[i]));
2006
2007         CHECK_EQUAL(lseek.out.offset);
2008
2009         return true;
2010 }
2011
2012
2013 /*
2014   generate readx operations
2015 */
2016 static bool handler_smb_readx(int instance)
2017 {
2018         union smb_read parm[NSERVERS];
2019         NTSTATUS status[NSERVERS];
2020
2021         parm[0].readx.level = RAW_READ_READX;
2022         parm[0].readx.in.file.fnum = gen_fnum(instance);
2023         parm[0].readx.in.offset = gen_offset();
2024         parm[0].readx.in.mincnt = gen_io_count();
2025         parm[0].readx.in.maxcnt = gen_io_count();
2026         parm[0].readx.in.remaining = gen_io_count();
2027         parm[0].readx.in.read_for_execute = gen_bool();
2028         parm[0].readx.out.data = talloc_array(current_op.mem_ctx, uint8_t,
2029                                              MAX(parm[0].readx.in.mincnt, parm[0].readx.in.maxcnt));
2030
2031         GEN_COPY_PARM;
2032         GEN_SET_FNUM_SMB(readx.in.file.fnum);
2033         GEN_CALL_SMB(smb_raw_read(tree, &parm[i]));
2034
2035         CHECK_EQUAL(readx.out.remaining);
2036         CHECK_EQUAL(readx.out.compaction_mode);
2037         CHECK_EQUAL(readx.out.nread);
2038
2039         return true;
2040 }
2041
2042 /*
2043   generate writex operations
2044 */
2045 static bool handler_smb_writex(int instance)
2046 {
2047         union smb_write parm[NSERVERS];
2048         NTSTATUS status[NSERVERS];
2049
2050         parm[0].writex.level = RAW_WRITE_WRITEX;
2051         parm[0].writex.in.file.fnum = gen_fnum(instance);
2052         parm[0].writex.in.offset = gen_offset();
2053         parm[0].writex.in.wmode = gen_bits_mask(0xFFFF);
2054         parm[0].writex.in.remaining = gen_io_count();
2055         parm[0].writex.in.count = gen_io_count();
2056         parm[0].writex.in.data = talloc_zero_array(current_op.mem_ctx, uint8_t, parm[0].writex.in.count);
2057
2058         GEN_COPY_PARM;
2059         GEN_SET_FNUM_SMB(writex.in.file.fnum);
2060         GEN_CALL_SMB(smb_raw_write(tree, &parm[i]));
2061
2062         CHECK_EQUAL(writex.out.nwritten);
2063         CHECK_EQUAL(writex.out.remaining);
2064
2065         return true;
2066 }
2067
2068 /*
2069   generate lockingx operations
2070 */
2071 static bool handler_smb_lockingx(int instance)
2072 {
2073         union smb_lock parm[NSERVERS];
2074         NTSTATUS status[NSERVERS];
2075         int n, nlocks;
2076
2077         parm[0].lockx.level = RAW_LOCK_LOCKX;
2078         parm[0].lockx.in.file.fnum = gen_fnum(instance);
2079         parm[0].lockx.in.mode = gen_lock_mode();
2080         parm[0].lockx.in.timeout = gen_timeout();
2081         do {
2082                 /* make sure we don't accidentially generate an oplock
2083                    break ack - otherwise the server can just block forever */
2084                 parm[0].lockx.in.ulock_cnt = gen_lock_count();
2085                 parm[0].lockx.in.lock_cnt = gen_lock_count();
2086                 nlocks = parm[0].lockx.in.ulock_cnt + parm[0].lockx.in.lock_cnt;
2087         } while (nlocks == 0);
2088
2089         if (nlocks > 0) {
2090                 parm[0].lockx.in.locks = talloc_array(current_op.mem_ctx,
2091                                                         struct smb_lock_entry,
2092                                                         nlocks);
2093                 for (n=0;n<nlocks;n++) {
2094                         parm[0].lockx.in.locks[n].pid = gen_pid();
2095                         parm[0].lockx.in.locks[n].offset = gen_offset();
2096                         parm[0].lockx.in.locks[n].count = gen_io_count();
2097                 }
2098         }
2099
2100         GEN_COPY_PARM;
2101         GEN_SET_FNUM_SMB(lockx.in.file.fnum);
2102         GEN_CALL_SMB(smb_raw_lock(tree, &parm[i]));
2103
2104         return true;
2105 }
2106
2107 #if 0
2108 /*
2109   generate a fileinfo query structure
2110 */
2111 static void gen_setfileinfo(int instance, union smb_setfileinfo *info)
2112 {
2113         int i;
2114         #undef LVL
2115         #define LVL(v) {RAW_SFILEINFO_ ## v, "RAW_SFILEINFO_" #v}
2116         struct {
2117                 enum smb_setfileinfo_level level;
2118                 const char *name;
2119         }  levels[] = {
2120 #if 0
2121                 /* disabled until win2003 can handle them ... */
2122                 LVL(EA_SET), LVL(BASIC_INFO), LVL(DISPOSITION_INFO), 
2123                 LVL(STANDARD), LVL(ALLOCATION_INFO), LVL(END_OF_FILE_INFO), 
2124 #endif
2125                 LVL(SETATTR), LVL(SETATTRE), LVL(BASIC_INFORMATION),
2126                 LVL(RENAME_INFORMATION), LVL(DISPOSITION_INFORMATION), 
2127                 LVL(POSITION_INFORMATION), LVL(MODE_INFORMATION),
2128                 LVL(ALLOCATION_INFORMATION), LVL(END_OF_FILE_INFORMATION), 
2129                 LVL(1023), LVL(1025), LVL(1029), LVL(1032), LVL(1039), LVL(1040)
2130         };
2131         do {
2132                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
2133         } while (ignore_pattern(levels[i].name));
2134
2135         info->generic.level = levels[i].level;
2136
2137         switch (info->generic.level) {
2138         case RAW_SFILEINFO_SETATTR:
2139                 info->setattr.in.attrib = gen_attrib();
2140                 info->setattr.in.write_time = gen_timet();
2141                 break;
2142         case RAW_SFILEINFO_SETATTRE:
2143                 info->setattre.in.create_time = gen_timet();
2144                 info->setattre.in.access_time = gen_timet();
2145                 info->setattre.in.write_time = gen_timet();
2146                 break;
2147         case RAW_SFILEINFO_STANDARD:
2148                 info->standard.in.create_time = gen_timet();
2149                 info->standard.in.access_time = gen_timet();
2150                 info->standard.in.write_time = gen_timet();
2151                 break;
2152         case RAW_SFILEINFO_EA_SET: {
2153                 static struct ea_struct ea;
2154                 info->ea_set.in.num_eas = 1;
2155                 info->ea_set.in.eas = &ea;
2156                 info->ea_set.in.eas[0] = gen_ea_struct();
2157         }
2158                 break;
2159         case RAW_SFILEINFO_BASIC_INFO:
2160         case RAW_SFILEINFO_BASIC_INFORMATION:
2161                 info->basic_info.in.create_time = gen_nttime();
2162                 info->basic_info.in.access_time = gen_nttime();
2163                 info->basic_info.in.write_time = gen_nttime();
2164                 info->basic_info.in.change_time = gen_nttime();
2165                 info->basic_info.in.attrib = gen_attrib();
2166                 break;
2167         case RAW_SFILEINFO_DISPOSITION_INFO:
2168         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
2169                 info->disposition_info.in.delete_on_close = gen_bool();
2170                 break;
2171         case RAW_SFILEINFO_ALLOCATION_INFO:
2172         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
2173                 info->allocation_info.in.alloc_size = gen_alloc_size();
2174                 break;
2175         case RAW_SFILEINFO_END_OF_FILE_INFO:
2176         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
2177                 info->end_of_file_info.in.size = gen_offset();
2178                 break;
2179         case RAW_SFILEINFO_RENAME_INFORMATION:
2180         case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
2181                 info->rename_information.in.overwrite = gen_bool();
2182                 info->rename_information.in.root_fid = gen_root_fid(instance);
2183                 info->rename_information.in.new_name = gen_fname_open(instance);
2184                 break;
2185         case RAW_SFILEINFO_POSITION_INFORMATION:
2186                 info->position_information.in.position = gen_offset();
2187                 break;
2188         case RAW_SFILEINFO_MODE_INFORMATION:
2189                 info->mode_information.in.mode = gen_bits_mask(0xFFFFFFFF);
2190                 break;
2191         case RAW_SFILEINFO_FULL_EA_INFORMATION:
2192                 info->full_ea_information.in.eas = gen_ea_list();
2193                 break;
2194         case RAW_SFILEINFO_GENERIC:
2195         case RAW_SFILEINFO_SEC_DESC:
2196         case RAW_SFILEINFO_UNIX_BASIC:
2197         case RAW_SFILEINFO_UNIX_LINK:
2198         case RAW_SFILEINFO_UNIX_HLINK:
2199         case RAW_SFILEINFO_1023:
2200         case RAW_SFILEINFO_1025:
2201         case RAW_SFILEINFO_1029:
2202         case RAW_SFILEINFO_1032:
2203         case RAW_SFILEINFO_1039:
2204         case RAW_SFILEINFO_1040:
2205         case RAW_SFILEINFO_UNIX_INFO2:
2206                 /* Untested */
2207                 break;
2208         }
2209 }
2210 #endif
2211
2212 /*
2213   generate a fileinfo query structure
2214 */
2215 static void gen_setfileinfo(int instance, union smb_setfileinfo *info)
2216 {
2217         int i;
2218         #undef LVL
2219         #define LVL(v) {RAW_SFILEINFO_ ## v, "RAW_SFILEINFO_" #v}
2220         struct levels {
2221                 enum smb_setfileinfo_level level;
2222                 const char *name;
2223         };
2224         struct levels smb_levels[] = {
2225                 LVL(EA_SET), LVL(BASIC_INFO), LVL(DISPOSITION_INFO), 
2226                 LVL(STANDARD), LVL(ALLOCATION_INFO), LVL(END_OF_FILE_INFO), 
2227                 LVL(SETATTR), LVL(SETATTRE), LVL(BASIC_INFORMATION),
2228                 LVL(RENAME_INFORMATION), LVL(DISPOSITION_INFORMATION), 
2229                 LVL(POSITION_INFORMATION), LVL(FULL_EA_INFORMATION), LVL(MODE_INFORMATION),
2230                 LVL(ALLOCATION_INFORMATION), LVL(END_OF_FILE_INFORMATION), 
2231                 LVL(PIPE_INFORMATION), LVL(VALID_DATA_INFORMATION), LVL(SHORT_NAME_INFORMATION), 
2232                 LVL(1025), LVL(1027), LVL(1029), LVL(1030), LVL(1031), LVL(1032), LVL(1036),
2233                 LVL(1041), LVL(1042), LVL(1043), LVL(1044),
2234         };
2235         struct levels smb2_levels[] = {
2236                 LVL(BASIC_INFORMATION),
2237                 LVL(RENAME_INFORMATION), LVL(DISPOSITION_INFORMATION), 
2238                 LVL(POSITION_INFORMATION), LVL(FULL_EA_INFORMATION), LVL(MODE_INFORMATION),
2239                 LVL(ALLOCATION_INFORMATION), LVL(END_OF_FILE_INFORMATION), 
2240                 LVL(PIPE_INFORMATION), LVL(VALID_DATA_INFORMATION), LVL(SHORT_NAME_INFORMATION), 
2241                 LVL(1025), LVL(1027), LVL(1029), LVL(1030), LVL(1031), LVL(1032), LVL(1036),
2242                 LVL(1041), LVL(1042), LVL(1043), LVL(1044),
2243         };
2244         struct levels *levels = options.smb2?smb2_levels:smb_levels;
2245         uint32_t num_levels = options.smb2?ARRAY_SIZE(smb2_levels):ARRAY_SIZE(smb_levels);
2246
2247         do {
2248                 i = gen_int_range(0, num_levels-1);
2249         } while (ignore_pattern(levels[i].name));
2250
2251         ZERO_STRUCTP(info);
2252         info->generic.level = levels[i].level;
2253
2254         switch (info->generic.level) {
2255         case RAW_SFILEINFO_SETATTR:
2256                 info->setattr.in.attrib = gen_attrib();
2257                 info->setattr.in.write_time = gen_timet();
2258                 break;
2259         case RAW_SFILEINFO_SETATTRE:
2260                 info->setattre.in.create_time = gen_timet();
2261                 info->setattre.in.access_time = gen_timet();
2262                 info->setattre.in.write_time = gen_timet();
2263                 break;
2264         case RAW_SFILEINFO_STANDARD:
2265                 info->standard.in.create_time = gen_timet();
2266                 info->standard.in.access_time = gen_timet();
2267                 info->standard.in.write_time = gen_timet();
2268                 break;
2269         case RAW_SFILEINFO_EA_SET: {
2270                 static struct ea_struct ea;
2271                 info->ea_set.in.num_eas = 1;
2272                 info->ea_set.in.eas = &ea;
2273                 info->ea_set.in.eas[0] = gen_ea_struct();
2274                 break;
2275         }
2276         case RAW_SFILEINFO_BASIC_INFO:
2277         case RAW_SFILEINFO_BASIC_INFORMATION:
2278                 info->basic_info.in.create_time = gen_nttime();
2279                 info->basic_info.in.access_time = gen_nttime();
2280                 info->basic_info.in.write_time = gen_nttime();
2281                 info->basic_info.in.change_time = gen_nttime();
2282                 info->basic_info.in.attrib = gen_attrib();
2283                 break;
2284         case RAW_SFILEINFO_DISPOSITION_INFO:
2285         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
2286                 info->disposition_info.in.delete_on_close = gen_bool();
2287                 break;
2288         case RAW_SFILEINFO_ALLOCATION_INFO:
2289         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
2290                 info->allocation_info.in.alloc_size = gen_alloc_size();
2291                 break;
2292         case RAW_SFILEINFO_END_OF_FILE_INFO:
2293         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
2294                 info->end_of_file_info.in.size = gen_offset();
2295                 break;
2296         case RAW_SFILEINFO_RENAME_INFORMATION:
2297         case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
2298                 info->rename_information.in.overwrite = gen_bool();
2299                 info->rename_information.in.root_fid = gen_root_fid(instance);
2300                 info->rename_information.in.new_name = gen_fname_open(instance);
2301                 break;
2302         case RAW_SFILEINFO_POSITION_INFORMATION:
2303                 info->position_information.in.position = gen_offset();
2304                 break;
2305         case RAW_SFILEINFO_MODE_INFORMATION:
2306                 info->mode_information.in.mode = gen_bits_mask(0xFFFFFFFF);
2307                 break;
2308         case RAW_SFILEINFO_FULL_EA_INFORMATION:
2309                 info->full_ea_information.in.eas = gen_ea_list();
2310                 break;
2311
2312         case RAW_SFILEINFO_GENERIC:
2313         case RAW_SFILEINFO_SEC_DESC:
2314         case RAW_SFILEINFO_1025:
2315         case RAW_SFILEINFO_1029:
2316         case RAW_SFILEINFO_1032:
2317         case RAW_SFILEINFO_UNIX_BASIC:
2318         case RAW_SFILEINFO_UNIX_INFO2:
2319         case RAW_SFILEINFO_UNIX_LINK:
2320         case RAW_SFILEINFO_UNIX_HLINK:
2321                 /* Untested */
2322                 break;
2323         }
2324 }
2325
2326
2327
2328 /*
2329   generate a fileinfo query structure
2330 */
2331 static void gen_fileinfo_smb(int instance, union smb_fileinfo *info)
2332 {
2333         int i;
2334         #undef LVL
2335         #define LVL(v) {RAW_FILEINFO_ ## v, "RAW_FILEINFO_" #v}
2336         struct {
2337                 enum smb_fileinfo_level level;
2338                 const char *name;
2339         }  levels[] = {
2340                 LVL(GETATTR), LVL(GETATTRE), LVL(STANDARD),
2341                 LVL(EA_SIZE), LVL(ALL_EAS), LVL(IS_NAME_VALID),
2342                 LVL(BASIC_INFO), LVL(STANDARD_INFO), LVL(EA_INFO),
2343                 LVL(NAME_INFO), LVL(ALL_INFO), LVL(ALT_NAME_INFO),
2344                 LVL(STREAM_INFO), LVL(COMPRESSION_INFO), LVL(BASIC_INFORMATION),
2345                 LVL(STANDARD_INFORMATION), LVL(INTERNAL_INFORMATION), LVL(EA_INFORMATION),
2346                 LVL(ACCESS_INFORMATION), LVL(NAME_INFORMATION), LVL(POSITION_INFORMATION),
2347                 LVL(MODE_INFORMATION), LVL(ALIGNMENT_INFORMATION), LVL(ALL_INFORMATION),
2348                 LVL(ALT_NAME_INFORMATION), LVL(STREAM_INFORMATION), LVL(COMPRESSION_INFORMATION),
2349                 LVL(NETWORK_OPEN_INFORMATION), LVL(ATTRIBUTE_TAG_INFORMATION)
2350         };
2351         do {
2352                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
2353         } while (ignore_pattern(levels[i].name));
2354
2355         info->generic.level = levels[i].level;
2356 }
2357
2358 /*
2359   generate qpathinfo operations
2360 */
2361 static bool handler_smb_qpathinfo(int instance)
2362 {
2363         union smb_fileinfo parm[NSERVERS];
2364         NTSTATUS status[NSERVERS];
2365
2366         parm[0].generic.in.file.path = gen_fname_open(instance);
2367
2368         gen_fileinfo_smb(instance, &parm[0]);
2369
2370         GEN_COPY_PARM;
2371         GEN_CALL_SMB(smb_raw_pathinfo(tree, current_op.mem_ctx, &parm[i]));
2372
2373         return cmp_fileinfo(instance, parm, status);
2374 }
2375
2376 /*
2377   generate qfileinfo operations
2378 */
2379 static bool handler_smb_qfileinfo(int instance)
2380 {
2381         union smb_fileinfo parm[NSERVERS];
2382         NTSTATUS status[NSERVERS];
2383
2384         parm[0].generic.in.file.fnum = gen_fnum(instance);
2385
2386         gen_fileinfo_smb(instance, &parm[0]);
2387
2388         GEN_COPY_PARM;
2389         GEN_SET_FNUM_SMB(generic.in.file.fnum);
2390         GEN_CALL_SMB(smb_raw_fileinfo(tree, current_op.mem_ctx, &parm[i]));
2391
2392         return cmp_fileinfo(instance, parm, status);
2393 }
2394
2395
2396 /*
2397   generate setpathinfo operations
2398 */
2399 static bool handler_smb_spathinfo(int instance)
2400 {
2401         union smb_setfileinfo parm[NSERVERS];
2402         NTSTATUS status[NSERVERS];
2403
2404         gen_setfileinfo(instance, &parm[0]);
2405         parm[0].generic.in.file.path = gen_fname_open(instance);
2406
2407         GEN_COPY_PARM;
2408
2409         /* a special case for the fid in a RENAME */
2410         if (parm[0].generic.level == RAW_SFILEINFO_RENAME_INFORMATION &&
2411             parm[0].rename_information.in.root_fid != 0) {
2412                 GEN_SET_FNUM_SMB(rename_information.in.root_fid);
2413         }
2414
2415         GEN_CALL_SMB(smb_raw_setpathinfo(tree, &parm[i]));
2416
2417         return true;
2418 }
2419
2420
2421 /*
2422   generate setfileinfo operations
2423 */
2424 static bool handler_smb_sfileinfo(int instance)
2425 {
2426         union smb_setfileinfo parm[NSERVERS];
2427         NTSTATUS status[NSERVERS];
2428
2429         parm[0].generic.in.file.fnum = gen_fnum(instance);
2430
2431         gen_setfileinfo(instance, &parm[0]);
2432
2433         GEN_COPY_PARM;
2434         GEN_SET_FNUM_SMB(generic.in.file.fnum);
2435         GEN_CALL_SMB(smb_raw_setfileinfo(tree, &parm[i]));
2436
2437         return true;
2438 }
2439
2440
2441 /*
2442   this is called when a change notify reply comes in
2443 */
2444 static void async_notify_smb(struct smbcli_request *req)
2445 {
2446         union smb_notify notify;
2447         NTSTATUS status;
2448         int i, j;
2449         uint16_t tid;
2450         struct smbcli_transport *transport = req->transport;
2451
2452         tid = SVAL(req->in.hdr, HDR_TID);
2453
2454         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
2455         status = smb_raw_changenotify_recv(req, current_op.mem_ctx, &notify);
2456         if (NT_STATUS_IS_OK(status) && notify.nttrans.out.num_changes > 0) {
2457                 printf("notify tid=%d num_changes=%d action=%d name=%s\n", 
2458                        tid, 
2459                        notify.nttrans.out.num_changes,
2460                        notify.nttrans.out.changes[0].action,
2461                        notify.nttrans.out.changes[0].name.s);
2462         }
2463
2464         for (i=0;i<NSERVERS;i++) {
2465                 for (j=0;j<NINSTANCES;j++) {
2466                         if (transport == servers[i].smb_tree[j]->session->transport &&
2467                             tid == servers[i].smb_tree[j]->tid) {
2468                                 notifies[i][j].notify_count++;
2469                                 notifies[i][j].status = status;
2470                                 notifies[i][j].notify = notify;
2471                         }
2472                 }
2473         }
2474 }
2475
2476 /*
2477   generate change notify operations
2478 */
2479 static bool handler_smb_notify(int instance)
2480 {
2481         union smb_notify parm[NSERVERS];
2482         int n;
2483
2484         ZERO_STRUCT(parm[0]);
2485         parm[0].nttrans.level                   = RAW_NOTIFY_NTTRANS;
2486         parm[0].nttrans.in.buffer_size          = gen_io_count();
2487         parm[0].nttrans.in.completion_filter    = gen_bits_mask(0xFF);
2488         parm[0].nttrans.in.file.fnum            = gen_fnum(instance);
2489         parm[0].nttrans.in.recursive            = gen_bool();
2490
2491         GEN_COPY_PARM;
2492         GEN_SET_FNUM_SMB(nttrans.in.file.fnum);
2493
2494         for (n=0;n<NSERVERS;n++) {
2495                 struct smbcli_request *req;
2496                 req = smb_raw_changenotify_send(servers[n].smb_tree[instance], &parm[n]);
2497                 req->async.fn = async_notify_smb;
2498         }
2499
2500         return true;
2501 }
2502
2503
2504 /*
2505   generate ntcreatex operations
2506 */
2507 static bool handler_smb2_create(int instance)
2508 {
2509         struct smb2_create parm[NSERVERS];
2510         NTSTATUS status[NSERVERS];
2511
2512         ZERO_STRUCT(parm[0]);
2513         parm[0].in.security_flags             = gen_bits_levels(3, 90, 0x0, 70, 0x3, 100, 0xFF);
2514         parm[0].in.oplock_level               = gen_bits_levels(3, 90, 0x0, 70, 0x9, 100, 0xFF);
2515         parm[0].in.impersonation_level        = gen_bits_levels(3, 90, 0x0, 70, 0x3, 100, 0xFFFFFFFF);
2516         parm[0].in.create_flags               = gen_reserved64();
2517         parm[0].in.reserved                   = gen_reserved64();
2518         parm[0].in.desired_access             = gen_access_mask();
2519         parm[0].in.file_attributes            = gen_attrib();
2520         parm[0].in.share_access               = gen_bits_mask2(0x7, 0xFFFFFFFF);
2521         parm[0].in.create_disposition         = gen_open_disp();
2522         parm[0].in.create_options             = gen_create_options();
2523         parm[0].in.fname                      = gen_fname_open(instance);
2524         parm[0].in.eas                        = gen_ea_list();
2525         parm[0].in.alloc_size                 = gen_alloc_size();
2526         parm[0].in.durable_open               = gen_bool();
2527         parm[0].in.query_maximal_access       = gen_bool();
2528         parm[0].in.timewarp                   = gen_timewarp();
2529         parm[0].in.query_on_disk_id           = gen_bool();
2530         parm[0].in.sec_desc                   = gen_sec_desc();
2531
2532         if (!options.use_oplocks) {
2533                 /* mask out oplocks */
2534                 parm[0].in.oplock_level = 0;
2535         }
2536
2537         if (options.valid) {
2538                 parm[0].in.security_flags   &= 3;
2539                 parm[0].in.oplock_level     &= 9;
2540                 parm[0].in.impersonation_level &= 3;
2541         }
2542
2543         GEN_COPY_PARM;
2544         GEN_CALL_SMB2(smb2_create(tree, current_op.mem_ctx, &parm[i]));
2545
2546         CHECK_EQUAL(out.oplock_level);
2547         CHECK_EQUAL(out.reserved);
2548         CHECK_EQUAL(out.create_action);
2549         CHECK_NTTIMES_EQUAL(out.create_time);
2550         CHECK_NTTIMES_EQUAL(out.access_time);
2551         CHECK_NTTIMES_EQUAL(out.write_time);
2552         CHECK_NTTIMES_EQUAL(out.change_time);
2553         CHECK_EQUAL(out.alloc_size);
2554         CHECK_EQUAL(out.size);
2555         CHECK_ATTRIB(out.file_attr);
2556         CHECK_EQUAL(out.reserved2);
2557         CHECK_EQUAL(out.maximal_access);
2558
2559         /* ntcreatex creates a new file handle */
2560         ADD_HANDLE_SMB2(parm[0].in.fname, out.file.handle);
2561
2562         return true;
2563 }
2564
2565 /*
2566   generate close operations
2567 */
2568 static bool handler_smb2_close(int instance)
2569 {
2570         struct smb2_close parm[NSERVERS];
2571         NTSTATUS status[NSERVERS];
2572
2573         ZERO_STRUCT(parm[0]);
2574         parm[0].in.file.handle.data[0] = gen_fnum_close(instance);
2575         parm[0].in.flags               = gen_bits_mask2(0x1, 0xFFFF);
2576
2577         GEN_COPY_PARM;
2578         GEN_SET_FNUM_SMB2(in.file.handle);
2579         GEN_CALL_SMB2(smb2_close(tree, &parm[i]));
2580
2581         CHECK_EQUAL(out.flags);
2582         CHECK_EQUAL(out._pad);
2583         CHECK_NTTIMES_EQUAL(out.create_time);
2584         CHECK_NTTIMES_EQUAL(out.access_time);
2585         CHECK_NTTIMES_EQUAL(out.write_time);
2586         CHECK_NTTIMES_EQUAL(out.change_time);
2587         CHECK_EQUAL(out.alloc_size);
2588         CHECK_EQUAL(out.size);
2589         CHECK_ATTRIB(out.file_attr);
2590
2591         REMOVE_HANDLE_SMB2(in.file.handle);
2592
2593         return true;
2594 }
2595
2596 /*
2597   generate read operations
2598 */
2599 static bool handler_smb2_read(int instance)
2600 {
2601         struct smb2_read parm[NSERVERS];
2602         NTSTATUS status[NSERVERS];
2603
2604         parm[0].in.file.handle.data[0] = gen_fnum(instance);
2605         parm[0].in.reserved    = gen_reserved8();
2606         parm[0].in.length      = gen_io_count();
2607         parm[0].in.offset      = gen_offset();
2608         parm[0].in.min_count   = gen_io_count();
2609         parm[0].in.channel     = gen_bits_mask2(0x0, 0xFFFFFFFF);
2610         parm[0].in.remaining   = gen_bits_mask2(0x0, 0xFFFFFFFF);
2611         parm[0].in.channel_offset = gen_bits_mask2(0x0, 0xFFFF);
2612         parm[0].in.channel_length = gen_bits_mask2(0x0, 0xFFFF);
2613
2614         GEN_COPY_PARM;
2615         GEN_SET_FNUM_SMB2(in.file.handle);
2616         GEN_CALL_SMB2(smb2_read(tree, current_op.mem_ctx, &parm[i]));
2617
2618         CHECK_EQUAL(out.remaining);
2619         CHECK_EQUAL(out.reserved);
2620         CHECK_EQUAL(out.data.length);
2621
2622         return true;
2623 }
2624
2625 /*
2626   generate write operations
2627 */
2628 static bool handler_smb2_write(int instance)
2629 {
2630         struct smb2_write parm[NSERVERS];
2631         NTSTATUS status[NSERVERS];
2632
2633         parm[0].in.file.handle.data[0] = gen_fnum(instance);
2634         parm[0].in.offset = gen_offset();
2635         parm[0].in.unknown1 = gen_bits_mask2(0, 0xFFFFFFFF);
2636         parm[0].in.unknown2 = gen_bits_mask2(0, 0xFFFFFFFF);
2637         parm[0].in.data = data_blob_talloc(current_op.mem_ctx, NULL,
2638                                             gen_io_count());
2639
2640         GEN_COPY_PARM;
2641         GEN_SET_FNUM_SMB2(in.file.handle);
2642         GEN_CALL_SMB2(smb2_write(tree, &parm[i]));
2643
2644         CHECK_EQUAL(out._pad);
2645         CHECK_EQUAL(out.nwritten);
2646         CHECK_EQUAL(out.unknown1);
2647
2648         return true;
2649 }
2650
2651 /*
2652   generate lockingx operations
2653 */
2654 static bool handler_smb2_lock(int instance)
2655 {
2656         struct smb2_lock parm[NSERVERS];
2657         NTSTATUS status[NSERVERS];
2658         int n;
2659
2660         parm[0].level = RAW_LOCK_LOCKX;
2661         parm[0].in.file.handle.data[0] = gen_fnum(instance);
2662         parm[0].in.lock_count = gen_lock_count();
2663         parm[0].in.lock_sequence = gen_reserved32();
2664         
2665         parm[0].in.locks = talloc_array(current_op.mem_ctx,
2666                                         struct smb2_lock_element,
2667                                         parm[0].in.lock_count);
2668         for (n=0;n<parm[0].in.lock_count;n++) {
2669                 parm[0].in.locks[n].offset = gen_offset();
2670                 parm[0].in.locks[n].length = gen_io_count();
2671                 /* don't yet cope with async replies */
2672                 parm[0].in.locks[n].flags  = gen_lock_flags_smb2() | 
2673                         SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2674                 parm[0].in.locks[n].reserved = gen_bits_mask2(0x0, 0xFFFFFFFF);
2675         }
2676
2677         GEN_COPY_PARM;
2678         GEN_SET_FNUM_SMB2(in.file.handle);
2679         GEN_CALL_SMB2(smb2_lock(tree, &parm[i]));
2680
2681         return true;
2682 }
2683
2684 /*
2685   generate flush operations
2686 */
2687 static bool handler_smb2_flush(int instance)
2688 {
2689         struct smb2_flush parm[NSERVERS];
2690         NTSTATUS status[NSERVERS];
2691
2692         ZERO_STRUCT(parm[0]);
2693         parm[0].in.file.handle.data[0] = gen_fnum(instance);
2694         parm[0].in.reserved1  = gen_reserved16();
2695         parm[0].in.reserved2  = gen_reserved32();
2696
2697         GEN_COPY_PARM;
2698         GEN_SET_FNUM_SMB2(in.file.handle);
2699         GEN_CALL_SMB2(smb2_flush(tree, &parm[i]));
2700
2701         CHECK_EQUAL(out.reserved);
2702
2703         return true;
2704 }
2705
2706 /*
2707   generate echo operations
2708 */
2709 static bool handler_smb2_echo(int instance)
2710 {
2711         NTSTATUS status[NSERVERS];
2712
2713         GEN_CALL_SMB2(smb2_keepalive(tree->session->transport));
2714
2715         return true;
2716 }
2717
2718
2719
2720 /*
2721   generate a fileinfo query structure
2722 */
2723 static void gen_fileinfo_smb2(int instance, union smb_fileinfo *info)
2724 {
2725         int i;
2726         #define LVL(v) {RAW_FILEINFO_ ## v, "RAW_FILEINFO_" #v}
2727         struct {
2728                 enum smb_fileinfo_level level;
2729                 const char *name;
2730         }  levels[] = {
2731                 LVL(BASIC_INFORMATION),
2732                 LVL(STANDARD_INFORMATION), LVL(INTERNAL_INFORMATION), LVL(EA_INFORMATION),
2733                 LVL(ACCESS_INFORMATION), LVL(NAME_INFORMATION), LVL(POSITION_INFORMATION),
2734                 LVL(MODE_INFORMATION), LVL(ALIGNMENT_INFORMATION), LVL(SMB2_ALL_INFORMATION),
2735                 LVL(ALT_NAME_INFORMATION), LVL(STREAM_INFORMATION), LVL(COMPRESSION_INFORMATION),
2736                 LVL(NETWORK_OPEN_INFORMATION), LVL(ATTRIBUTE_TAG_INFORMATION),
2737                 LVL(SMB2_ALL_EAS), LVL(SMB2_ALL_INFORMATION), LVL(SEC_DESC),
2738         };
2739         do {
2740                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
2741         } while (ignore_pattern(levels[i].name));
2742
2743         info->generic.level = levels[i].level;
2744 }
2745
2746 /*
2747   generate qfileinfo operations
2748 */
2749 static bool handler_smb2_qfileinfo(int instance)
2750 {
2751         union smb_fileinfo parm[NSERVERS];
2752         NTSTATUS status[NSERVERS];
2753
2754         parm[0].generic.in.file.handle.data[0] = gen_fnum(instance);
2755
2756         gen_fileinfo_smb2(instance, &parm[0]);
2757
2758         GEN_COPY_PARM;
2759         GEN_SET_FNUM_SMB2(generic.in.file.handle);
2760         GEN_CALL_SMB2(smb2_getinfo_file(tree, current_op.mem_ctx, &parm[i]));
2761
2762         return cmp_fileinfo(instance, parm, status);
2763 }
2764
2765
2766 /*
2767   generate setfileinfo operations
2768 */
2769 static bool handler_smb2_sfileinfo(int instance)
2770 {
2771         union smb_setfileinfo parm[NSERVERS];
2772         NTSTATUS status[NSERVERS];
2773
2774         gen_setfileinfo(instance, &parm[0]);
2775         parm[0].generic.in.file.fnum = gen_fnum(instance);
2776
2777         GEN_COPY_PARM;
2778         GEN_SET_FNUM_SMB2(generic.in.file.handle);
2779         GEN_CALL_SMB2(smb2_setinfo_file(tree, &parm[i]));
2780
2781         return true;
2782 }
2783
2784 /*
2785   wipe any relevant files
2786 */
2787 static void wipe_files(void)
2788 {
2789         int i;
2790         NTSTATUS status;
2791
2792         if (options.skip_cleanup) {
2793                 return;
2794         }
2795
2796         for (i=0;i<NSERVERS;i++) {
2797                 int n;
2798                 if (options.smb2) {
2799                         n = smb2_deltree(servers[i].smb2_tree[0], "gentest");
2800                 } else {
2801                         n = smbcli_deltree(servers[i].smb_tree[0], "gentest");
2802                 }
2803                 if (n == -1) {
2804                         printf("Failed to wipe tree on server %d\n", i);
2805                         exit(1);
2806                 }
2807                 if (options.smb2) {
2808                         status = smb2_util_mkdir(servers[i].smb2_tree[0], "gentest");
2809                 } else {
2810                         status = smbcli_mkdir(servers[i].smb_tree[0], "gentest");
2811                 }
2812                 if (NT_STATUS_IS_ERR(status)) {
2813                         printf("Failed to create gentest on server %d - %s\n", i, nt_errstr(status));
2814                         exit(1);
2815                 }
2816                 if (n > 0) {
2817                         printf("Deleted %d files on server %d\n", n, i);
2818                 }
2819         }
2820 }
2821
2822 /*
2823   dump the current seeds - useful for continuing a backtrack
2824 */
2825 static void dump_seeds(void)
2826 {
2827         int i;
2828         FILE *f;
2829
2830         if (!options.seeds_file) {
2831                 return;
2832         }
2833         f = fopen("seeds.tmp", "w");
2834         if (!f) return;
2835
2836         for (i=0;i<options.numops;i++) {
2837                 fprintf(f, "%u\n", op_parms[i].seed);
2838         }
2839         fclose(f);
2840         rename("seeds.tmp", options.seeds_file);
2841 }
2842
2843
2844
2845 /*
2846   the list of top-level operations that we will generate
2847 */
2848 static struct {
2849         const char *name;
2850         bool (*handler)(int instance);
2851         bool smb2;
2852         int count, success_count;
2853 } gen_ops[] = {
2854         {"CREATE",     handler_smb2_create,     true},
2855         {"CLOSE",      handler_smb2_close,      true},
2856         {"READ",       handler_smb2_read,       true},
2857         {"WRITE",      handler_smb2_write,      true},
2858         {"LOCK",       handler_smb2_lock,       true},
2859         {"FLUSH",      handler_smb2_flush,      true},
2860         {"ECHO",       handler_smb2_echo,       true},
2861         {"QFILEINFO",  handler_smb2_qfileinfo,  true},
2862         {"SFILEINFO",  handler_smb2_sfileinfo,  true},
2863
2864         {"OPEN",       handler_smb_open,        false},
2865         {"OPENX",      handler_smb_openx,       false},
2866         {"NTCREATEX",  handler_smb_ntcreatex,   false},
2867         {"CLOSE",      handler_smb_close,       false},
2868         {"UNLINK",     handler_smb_unlink,      false},
2869         {"MKDIR",      handler_smb_mkdir,       false},
2870         {"RMDIR",      handler_smb_rmdir,       false},
2871         {"RENAME",     handler_smb_rename,      false},
2872         {"NTRENAME",   handler_smb_ntrename,    false},
2873         {"READX",      handler_smb_readx,       false},
2874         {"WRITEX",     handler_smb_writex,      false},
2875         {"CHKPATH",    handler_smb_chkpath,     false},
2876         {"SEEK",       handler_smb_seek,        false},
2877         {"LOCKINGX",   handler_smb_lockingx,    false},
2878         {"QPATHINFO",  handler_smb_qpathinfo,   false},
2879         {"QFILEINFO",  handler_smb_qfileinfo,   false},
2880         {"SPATHINFO",  handler_smb_spathinfo,   false},
2881         {"SFILEINFO",  handler_smb_sfileinfo,   false},
2882         {"NOTIFY",     handler_smb_notify,      false},
2883         {"SEEK",       handler_smb_seek,        false},
2884 };
2885
2886
2887 /*
2888   run the test with the current set of op_parms parameters
2889   return the number of operations that completed successfully
2890 */
2891 static int run_test(struct tevent_context *ev, struct loadparm_context *lp_ctx)
2892 {
2893         int op, i;
2894
2895         if (!connect_servers(ev, lp_ctx)) {
2896                 printf("Failed to connect to servers\n");
2897                 exit(1);
2898         }
2899
2900         dump_seeds();
2901
2902         /* wipe any leftover files from old runs */
2903         wipe_files();
2904
2905         /* reset the open handles array */
2906         memset(open_handles, 0, options.max_open_handles * sizeof(open_handles[0]));
2907         num_open_handles = 0;
2908
2909         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
2910                 gen_ops[i].count = 0;
2911                 gen_ops[i].success_count = 0;
2912         }
2913
2914         for (op=0; op<options.numops; op++) {
2915                 int instance, which_op;
2916                 bool ret;
2917
2918                 if (op_parms[op].disabled) continue;
2919
2920                 srandom(op_parms[op].seed);
2921
2922                 instance = gen_int_range(0, NINSTANCES-1);
2923
2924                 /* generate a non-ignored operation */
2925                 do {
2926                         which_op = gen_int_range(0, ARRAY_SIZE(gen_ops)-1);
2927                 } while (ignore_pattern(gen_ops[which_op].name) ||
2928                          gen_ops[which_op].smb2 != options.smb2);
2929
2930                 DEBUG(3,("Generating op %s on instance %d\n",
2931                          gen_ops[which_op].name, instance));
2932
2933                 current_op.seed = op_parms[op].seed;
2934                 current_op.opnum = op;
2935                 current_op.name = gen_ops[which_op].name;
2936                 current_op.status = NT_STATUS_OK;
2937                 talloc_free(current_op.mem_ctx);
2938                 current_op.mem_ctx = talloc_named(NULL, 0, "%s", current_op.name);
2939
2940                 ret = gen_ops[which_op].handler(instance);
2941
2942                 gen_ops[which_op].count++;
2943                 if (NT_STATUS_IS_OK(current_op.status)) {
2944                         gen_ops[which_op].success_count++;                      
2945                 }
2946
2947                 if (!ret) {
2948                         printf("Failed at operation %d - %s\n",
2949                                op, gen_ops[which_op].name);
2950                         return op;
2951                 }
2952
2953                 if (op % 100 == 0) {
2954                         printf("%d\n", op);
2955                 }
2956         }
2957
2958         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
2959                 printf("Op %-10s got %d/%d success\n", 
2960                        gen_ops[i].name,
2961                        gen_ops[i].success_count,
2962                        gen_ops[i].count);
2963         }
2964
2965         return op;
2966 }
2967
2968 /* 
2969    perform a backtracking analysis of the minimal set of operations
2970    to generate an error
2971 */
2972 static void backtrack_analyze(struct tevent_context *ev,
2973                               struct loadparm_context *lp_ctx)
2974 {
2975         int chunk, ret;
2976         const char *mismatch = current_op.mismatch;
2977
2978         chunk = options.numops / 2;
2979
2980         do {
2981                 int base;
2982                 for (base=0; 
2983                      chunk > 0 && base+chunk < options.numops && options.numops > 1; ) {
2984                         int i, max;
2985
2986                         chunk = MIN(chunk, options.numops / 2);
2987
2988                         /* mark this range as disabled */
2989                         max = MIN(options.numops, base+chunk);
2990                         for (i=base;i<max; i++) {
2991                                 op_parms[i].disabled = true;
2992                         }
2993                         printf("Testing %d ops with %d-%d disabled\n", 
2994                                options.numops, base, max-1);
2995                         ret = run_test(ev, lp_ctx);
2996                         printf("Completed %d of %d ops\n", ret, options.numops);
2997                         for (i=base;i<max; i++) {
2998                                 op_parms[i].disabled = false;
2999                         }
3000                         if (ret == options.numops) {
3001                                 /* this chunk is needed */
3002                                 base += chunk;
3003                         } else if (mismatch != current_op.mismatch &&
3004                                    strcmp(mismatch, current_op.mismatch)) {
3005                                 base += chunk;
3006                                 printf("Different error in backtracking\n");
3007                         } else if (ret < base) {
3008                                 printf("damn - inconsistent errors! found early error\n");
3009                                 options.numops = ret+1;
3010                                 base = 0;
3011                         } else {
3012                                 /* it failed - this chunk isn't needed for a failure */
3013                                 memmove(&op_parms[base], &op_parms[max], 
3014                                         sizeof(op_parms[0]) * (options.numops - max));
3015                                 options.numops = (ret+1) - (max - base);
3016                         }
3017                 }
3018
3019                 if (chunk == 2) {
3020                         chunk = 1;
3021                 } else {
3022                         chunk *= 0.4;
3023                 }
3024
3025                 if (options.analyze_continuous && chunk == 0 && options.numops != 1) {
3026                         chunk = 1;
3027                 }
3028         } while (chunk > 0);
3029
3030         printf("Reduced to %d ops\n", options.numops);
3031         ret = run_test(ev, lp_ctx);
3032         if (ret != options.numops - 1) {
3033                 printf("Inconsistent result? ret=%d numops=%d\n", ret, options.numops);
3034         }
3035 }
3036
3037 /* 
3038    start the main gentest process
3039 */
3040 static bool start_gentest(struct tevent_context *ev,
3041                           struct loadparm_context *lp_ctx)
3042 {
3043         int op;
3044         int ret;
3045
3046         /* allocate the open_handles array */
3047         open_handles = calloc(options.max_open_handles, sizeof(open_handles[0]));
3048
3049         srandom(options.seed);
3050         op_parms = calloc(options.numops, sizeof(op_parms[0]));
3051
3052         /* generate the seeds - after this everything is deterministic */
3053         if (options.use_preset_seeds) {
3054                 int numops;
3055                 char **preset = file_lines_load(options.seeds_file, &numops, 0, NULL);
3056                 if (!preset) {
3057                         printf("Failed to load %s - %s\n", options.seeds_file, strerror(errno));
3058                         exit(1);
3059                 }
3060                 if (numops < options.numops) {
3061                         options.numops = numops;
3062                 }
3063                 for (op=0;op<options.numops;op++) {
3064                         if (!preset[op]) {
3065                                 printf("Not enough seeds in %s\n", options.seeds_file);
3066                                 exit(1);
3067                         }
3068                         op_parms[op].seed = atoi(preset[op]);
3069                 }
3070                 printf("Loaded %d seeds from %s\n", options.numops, options.seeds_file);
3071         } else {
3072                 for (op=0; op<options.numops; op++) {
3073                         op_parms[op].seed = random();
3074                 }
3075         }
3076
3077         ret = run_test(ev, lp_ctx);
3078
3079         if (ret != options.numops && options.analyze) {
3080                 options.numops = ret+1;
3081                 backtrack_analyze(ev, lp_ctx);
3082         } else if (options.analyze_always) {
3083                 backtrack_analyze(ev, lp_ctx);
3084         } else if (options.analyze_continuous) {
3085                 while (run_test(ev, lp_ctx) == options.numops) ;
3086         }
3087
3088         return ret == options.numops;
3089 }
3090
3091
3092 static void usage(poptContext pc)
3093 {
3094         printf(
3095 "Usage:\n\
3096   gentest //server1/share1 //server2/share2 [options..]\n\
3097 ");
3098         poptPrintUsage(pc, stdout, 0);
3099 }
3100
3101 /**
3102   split a UNC name into server and share names
3103 */
3104 static bool split_unc_name(const char *unc, char **server, char **share)
3105 {
3106         char *p = strdup(unc);
3107         if (!p) return false;
3108         all_string_sub(p, "\\", "/", 0);
3109         if (strncmp(p, "//", 2) != 0) return false;
3110
3111         (*server) = p+2;
3112         p = strchr(*server, '/');
3113         if (!p) return false;
3114
3115         *p = 0;
3116         (*share) = p+1;
3117         
3118         return true;
3119 }
3120
3121
3122
3123 /****************************************************************************
3124   main program
3125 ****************************************************************************/
3126  int main(int argc, char *argv[])
3127 {
3128         int opt;
3129         int i, username_count=0;
3130         bool ret;
3131         char *ignore_file=NULL;
3132         struct tevent_context *ev;
3133         struct loadparm_context *lp_ctx;
3134         poptContext pc;
3135         int argc_new;
3136         char **argv_new;
3137         enum {OPT_UNCLIST=1000};
3138         struct poptOption long_options[] = {
3139                 POPT_AUTOHELP
3140                 {"smb2",          0, POPT_ARG_NONE, &options.smb2, 0,   "use SMB2 protocol",    NULL},
3141                 {"seed",          0, POPT_ARG_INT,  &options.seed,      0,      "Seed to use for randomizer",   NULL},
3142                 {"num-ops",       0, POPT_ARG_INT,  &options.numops,    0,      "num ops",      NULL},
3143                 {"oplocks",       0, POPT_ARG_NONE, &options.use_oplocks,0,      "use oplocks", NULL},
3144                 {"showall",       0, POPT_ARG_NONE, &options.showall,    0,      "display all operations", NULL},
3145                 {"analyse",       0, POPT_ARG_NONE, &options.analyze,    0,      "do backtrack analysis", NULL},
3146                 {"analysealways", 0, POPT_ARG_NONE, &options.analyze_always,    0,      "analysis always", NULL},
3147                 {"analysecontinuous", 0, POPT_ARG_NONE, &options.analyze_continuous,    0,      "analysis continuous", NULL},
3148                 {"ignore",        0, POPT_ARG_STRING, &ignore_file,    0,      "ignore from file", NULL},
3149                 {"preset",        0, POPT_ARG_NONE, &options.use_preset_seeds,    0,      "use preset seeds", NULL},
3150                 {"fast",          0, POPT_ARG_NONE, &options.fast_reconnect,    0,      "use fast reconnect", NULL},
3151                 {"unclist",       0, POPT_ARG_STRING,   NULL,   OPT_UNCLIST,    "unclist",      NULL},
3152                 {"seedsfile",     0, POPT_ARG_STRING,  &options.seeds_file, 0,  "seed file",    NULL},
3153                 { "user", 'U',       POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
3154                 {"maskindexing",  0, POPT_ARG_NONE,  &options.mask_indexing, 0, "mask out the indexed file attrib",     NULL},
3155                 {"noeas",  0, POPT_ARG_NONE,  &options.no_eas, 0,       "don't use extended attributes",        NULL},
3156                 {"noacls",  0, POPT_ARG_NONE,  &options.no_acls, 0,     "don't use ACLs",       NULL},
3157                 {"skip-cleanup",  0, POPT_ARG_NONE,  &options.skip_cleanup, 0,  "don't delete files at start",  NULL},
3158                 {"valid",  0, POPT_ARG_NONE,  &options.valid, 0,        "generate only valid fields",   NULL},
3159                 POPT_COMMON_SAMBA
3160                 POPT_COMMON_CONNECTION
3161                 POPT_COMMON_CREDENTIALS
3162                 POPT_COMMON_VERSION
3163                 { NULL }
3164         };
3165
3166         memset(&bad_smb2_handle, 0xFF, sizeof(bad_smb2_handle));
3167
3168         setlinebuf(stdout);
3169         options.seed = time(NULL);
3170         options.numops = 1000;
3171         options.max_open_handles = 20;
3172         options.seeds_file = "gentest_seeds.dat";
3173
3174         pc = poptGetContext("gentest", argc, (const char **) argv, long_options, 
3175                             POPT_CONTEXT_KEEP_FIRST);
3176
3177         poptSetOtherOptionHelp(pc, "<unc1> <unc2>");
3178
3179         lp_ctx = cmdline_lp_ctx;
3180         servers[0].credentials = cli_credentials_init(talloc_autofree_context());
3181         servers[1].credentials = cli_credentials_init(talloc_autofree_context());
3182         cli_credentials_guess(servers[0].credentials, lp_ctx);
3183         cli_credentials_guess(servers[1].credentials, lp_ctx);
3184
3185         while((opt = poptGetNextOpt(pc)) != -1) {
3186                 switch (opt) {
3187                 case OPT_UNCLIST:
3188                         lpcfg_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
3189                         break;
3190                 case 'U':
3191                         if (username_count == 2) {
3192                                 usage(pc);
3193                                 exit(1);
3194                         }
3195                         cli_credentials_parse_string(servers[username_count].credentials, poptGetOptArg(pc), CRED_SPECIFIED);
3196                         username_count++;
3197                         break;
3198                 }
3199         }
3200
3201         if (ignore_file) {
3202                 options.ignore_patterns = file_lines_load(ignore_file, NULL, 0, NULL);
3203         }
3204
3205         argv_new = discard_const_p(char *, poptGetArgs(pc));
3206         argc_new = argc;
3207         for (i=0; i<argc; i++) {
3208                 if (argv_new[i] == NULL) {
3209                         argc_new = i;
3210                         break;
3211                 }
3212         }
3213
3214         if (!(argc_new >= 3)) {
3215                 usage(pc);
3216                 exit(1);
3217         }
3218
3219         setlinebuf(stdout);
3220
3221         setup_logging("gentest", DEBUG_STDOUT);
3222
3223         if (argc < 3 || argv[1][0] == '-') {
3224                 usage(pc);
3225                 exit(1);
3226         }
3227
3228         setup_logging(argv[0], DEBUG_STDOUT);
3229
3230         for (i=0;i<NSERVERS;i++) {
3231                 const char *share = argv[1+i];
3232                 if (!split_unc_name(share, &servers[i].server_name, &servers[i].share_name)) {
3233                         printf("Invalid share name '%s'\n", share);
3234                         return -1;
3235                 }
3236         }
3237
3238         if (username_count == 0) {
3239                 usage(pc);
3240                 return -1;
3241         }
3242         if (username_count == 1) {
3243                 servers[1].credentials = servers[0].credentials;
3244         }
3245
3246         printf("seed=%u\n", options.seed);
3247
3248         ev = s4_event_context_init(talloc_autofree_context());
3249
3250         gensec_init(lp_ctx);
3251
3252         ret = start_gentest(ev, lp_ctx);
3253
3254         if (ret) {
3255                 printf("gentest completed - no errors\n");
3256         } else {
3257                 printf("gentest failed\n");
3258         }
3259
3260         return ret?0:-1;
3261 }