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