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