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