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