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