forked from samba-team/samba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgentest.c
3311 lines (2840 loc) · 85.2 KB
/
gentest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Unix SMB/CIFS implementation.
generic testing tool - version with both SMB and SMB2 support
Copyright (C) Andrew Tridgell 2003-2008
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "lib/cmdline/popt_common.h"
#include "lib/events/events.h"
#include "system/time.h"
#include "system/filesys.h"
#include "libcli/raw/request.h"
#include "libcli/libcli.h"
#include "libcli/raw/libcliraw.h"
#include "libcli/smb2/smb2.h"
#include "libcli/smb2/smb2_calls.h"
#include "librpc/gen_ndr/security.h"
#include "librpc/gen_ndr/ndr_security.h"
#include "auth/credentials/credentials.h"
#include "libcli/resolve/resolve.h"
#include "auth/gensec/gensec.h"
#include "param/param.h"
#include "dynconfig/dynconfig.h"
#include "libcli/security/security.h"
#include "libcli/raw/raw_proto.h"
#include "../libcli/smb/smbXcli_base.h"
#define NSERVERS 2
#define NINSTANCES 2
/* global options */
static struct gentest_options {
int showall;
int analyze;
int analyze_always;
int analyze_continuous;
unsigned int max_open_handles;
unsigned int seed;
unsigned int numops;
int use_oplocks;
char **ignore_patterns;
const char *seeds_file;
int use_preset_seeds;
int fast_reconnect;
int mask_indexing;
int no_eas;
int no_acls;
int skip_cleanup;
int valid;
int smb2;
} options;
/* mapping between open handles on the server and local handles */
static struct {
bool active;
unsigned int instance;
struct smb2_handle smb2_handle[NSERVERS]; /* SMB2 */
uint16_t smb_handle[NSERVERS]; /* SMB */
const char *name;
} *open_handles;
static unsigned int num_open_handles;
/* state information for the servers. We open NINSTANCES connections to
each server */
static struct {
struct smb2_tree *smb2_tree[NINSTANCES];
struct smbcli_tree *smb_tree[NINSTANCES];
char *server_name;
char *share_name;
struct cli_credentials *credentials;
} servers[NSERVERS];
/* the seeds and flags for each operation */
static struct {
unsigned int seed;
bool disabled;
} *op_parms;
/* oplock break info */
static struct {
bool got_break;
struct smb2_handle smb2_handle;
uint16_t smb_handle;
uint16_t handle;
uint8_t level;
bool do_close;
} oplocks[NSERVERS][NINSTANCES];
/* change notify reply info */
static struct {
int notify_count;
NTSTATUS status;
union smb_notify notify;
} notifies[NSERVERS][NINSTANCES];
/* info relevant to the current operation */
static struct {
const char *name;
unsigned int seed;
NTSTATUS status;
unsigned int opnum;
TALLOC_CTX *mem_ctx;
const char *mismatch;
} current_op;
static struct smb2_handle bad_smb2_handle;
#define BAD_HANDLE 0xFFFE
static bool oplock_handler_smb2(struct smb2_transport *transport, const struct smb2_handle *handle,
uint8_t level, void *private_data);
static void idle_func_smb2(struct smb2_transport *transport, void *private_data);
static bool oplock_handler_smb(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private_data);
static void idle_func_smb(struct smbcli_transport *transport, void *private_data);
/*
check if a string should be ignored. This is used as the basis
for all error ignore settings
*/
static bool ignore_pattern(const char *str)
{
int i;
if (!options.ignore_patterns) return false;
for (i=0;options.ignore_patterns[i];i++) {
if (strcmp(options.ignore_patterns[i], str) == 0 ||
gen_fnmatch(options.ignore_patterns[i], str) == 0) {
DEBUG(2,("Ignoring '%s'\n", str));
return true;
}
}
return false;
}
/*****************************************************
connect to the servers
*******************************************************/
static bool connect_servers_fast(void)
{
int h, i;
/* close all open files */
for (h=0;h<options.max_open_handles;h++) {
if (!open_handles[h].active) continue;
for (i=0;i<NSERVERS;i++) {
NTSTATUS status;
if (options.smb2) {
status = smb2_util_close(servers[i].smb2_tree[open_handles[h].instance],
open_handles[h].smb2_handle[i]);
} else {
status = smbcli_close(servers[i].smb_tree[open_handles[h].instance],
open_handles[h].smb_handle[i]);
}
if (NT_STATUS_IS_ERR(status)) {
return false;
}
open_handles[h].active = false;
}
}
return true;
}
/*****************************************************
connect to the servers
*******************************************************/
static bool connect_servers(struct tevent_context *ev,
struct loadparm_context *lp_ctx)
{
int i, j;
if (options.fast_reconnect && servers[0].smb2_tree[0]) {
if (connect_servers_fast()) {
return true;
}
}
/* close any existing connections */
for (i=0;i<NSERVERS;i++) {
for (j=0;j<NINSTANCES;j++) {
if (servers[i].smb2_tree[j]) {
smb2_tdis(servers[i].smb2_tree[j]);
talloc_free(servers[i].smb2_tree[j]);
servers[i].smb2_tree[j] = NULL;
}
if (servers[i].smb_tree[j]) {
smb_tree_disconnect(servers[i].smb_tree[j]);
talloc_free(servers[i].smb_tree[j]);
servers[i].smb_tree[j] = NULL;
}
}
}
for (i=0;i<NSERVERS;i++) {
for (j=0;j<NINSTANCES;j++) {
NTSTATUS status;
struct smbcli_options smb_options;
struct smbcli_session_options smb_session_options;
lpcfg_smbcli_options(lp_ctx, &smb_options);
lpcfg_smbcli_session_options(lp_ctx, &smb_session_options);
printf("Connecting to \\\\%s\\%s as %s - instance %d\n",
servers[i].server_name, servers[i].share_name,
cli_credentials_get_username(servers[i].credentials),
j);
cli_credentials_set_workstation(servers[i].credentials,
"gentest", CRED_SPECIFIED);
if (options.smb2) {
status = smb2_connect(NULL, servers[i].server_name,
lpcfg_smb_ports(lp_ctx),
servers[i].share_name,
lpcfg_resolve_context(lp_ctx),
servers[i].credentials,
&servers[i].smb2_tree[j],
ev, &smb_options,
lpcfg_socket_options(lp_ctx),
lpcfg_gensec_settings(lp_ctx, lp_ctx)
);
} else {
status = smbcli_tree_full_connection(NULL,
&servers[i].smb_tree[j],
servers[i].server_name,
lpcfg_smb_ports(lp_ctx),
servers[i].share_name, "A:",
lpcfg_socket_options(lp_ctx),
servers[i].credentials,
lpcfg_resolve_context(lp_ctx), ev,
&smb_options,
&smb_session_options,
lpcfg_gensec_settings(lp_ctx, lp_ctx));
}
if (!NT_STATUS_IS_OK(status)) {
printf("Failed to connect to \\\\%s\\%s - %s\n",
servers[i].server_name, servers[i].share_name,
nt_errstr(status));
return false;
}
if (options.smb2) {
servers[i].smb2_tree[j]->session->transport->oplock.handler = oplock_handler_smb2;
servers[i].smb2_tree[j]->session->transport->oplock.private_data = (void *)(uintptr_t)((i<<8)|j);
smb2_transport_idle_handler(servers[i].smb2_tree[j]->session->transport,
idle_func_smb2, 50000, NULL);
} else {
smbcli_oplock_handler(servers[i].smb_tree[j]->session->transport, oplock_handler_smb,
(void *)(uintptr_t)((i<<8)|j));
smbcli_transport_idle_handler(servers[i].smb_tree[j]->session->transport, idle_func_smb,
50000, (void *)(uintptr_t)((i<<8)|j));
}
}
}
return true;
}
/*
work out the time skew between the servers - be conservative
*/
static unsigned int time_skew(void)
{
unsigned int ret;
NTTIME nt0, nt1;
if (options.smb2) {
struct smbXcli_conn *c0, *c1;
c0 = servers[0].smb2_tree[0]->session->transport->conn;
c1 = servers[1].smb2_tree[0]->session->transport->conn;
nt0 = smbXcli_conn_server_system_time(c0);
nt1 = smbXcli_conn_server_system_time(c1);
} else {
nt0 = servers[0].smb_tree[0]->session->transport->negotiate.server_time;
nt1 = servers[1].smb_tree[0]->session->transport->negotiate.server_time;
}
/* Samba's NTTIME is unsigned, abs() won't work! */
if (nt0 > nt1){
ret = nt0 - nt1;
} else {
ret = nt1 - nt0;
}
return ret + 300;
}
static bool smb2_handle_equal(const struct smb2_handle *h1, const struct smb2_handle *h2)
{
return memcmp(h1, h2, sizeof(struct smb2_handle)) == 0;
}
/*
turn a server handle into a local handle
*/
static unsigned int fnum_to_handle_smb2(int server, int instance, struct smb2_handle server_handle)
{
unsigned int i;
for (i=0;i<options.max_open_handles;i++) {
if (!open_handles[i].active ||
instance != open_handles[i].instance) continue;
if (smb2_handle_equal(&open_handles[i].smb2_handle[server], &server_handle)) {
return i;
}
}
printf("Invalid server handle in fnum_to_handle on server %d instance %d\n",
server, instance);
return BAD_HANDLE;
}
/*
turn a server handle into a local handle
*/
static unsigned int fnum_to_handle_smb(int server, int instance, uint16_t server_handle)
{
unsigned int i;
for (i=0;i<options.max_open_handles;i++) {
if (!open_handles[i].active ||
instance != open_handles[i].instance) continue;
if (open_handles[i].smb_handle[server] == server_handle) {
return i;
}
}
printf("Invalid server handle in fnum_to_handle on server %d instance %d\n",
server, instance);
return BAD_HANDLE;
}
/*
add some newly opened handles
*/
static void gen_add_handle_smb2(int instance, const char *name, struct smb2_handle handles[NSERVERS])
{
int i, h;
for (h=0;h<options.max_open_handles;h++) {
if (!open_handles[h].active) break;
}
if (h == options.max_open_handles) {
/* we have to force close a random handle */
h = random() % options.max_open_handles;
for (i=0;i<NSERVERS;i++) {
NTSTATUS status;
status = smb2_util_close(servers[i].smb2_tree[open_handles[h].instance],
open_handles[h].smb2_handle[i]);
if (NT_STATUS_IS_ERR(status)) {
printf("INTERNAL ERROR: Close failed when recovering handle! - %s\n",
nt_errstr(status));
}
}
printf("Recovered handle %d\n", h);
num_open_handles--;
}
for (i=0;i<NSERVERS;i++) {
open_handles[h].smb2_handle[i] = handles[i];
open_handles[h].instance = instance;
open_handles[h].active = true;
open_handles[h].name = name;
}
num_open_handles++;
printf("OPEN num_open_handles=%d h=%d (%s)\n",
num_open_handles, h, name);
}
/*
add some newly opened handles
*/
static void gen_add_handle_smb(int instance, const char *name, uint16_t handles[NSERVERS])
{
int i, h;
for (h=0;h<options.max_open_handles;h++) {
if (!open_handles[h].active) break;
}
if (h == options.max_open_handles) {
/* we have to force close a random handle */
h = random() % options.max_open_handles;
for (i=0;i<NSERVERS;i++) {
NTSTATUS status;
status = smbcli_close(servers[i].smb_tree[open_handles[h].instance],
open_handles[h].smb_handle[i]);
if (NT_STATUS_IS_ERR(status)) {
printf("INTERNAL ERROR: Close failed when recovering handle! - %s\n",
nt_errstr(status));
}
}
printf("Recovered handle %d\n", h);
num_open_handles--;
}
for (i=0;i<NSERVERS;i++) {
open_handles[h].smb_handle[i] = handles[i];
open_handles[h].instance = instance;
open_handles[h].active = true;
open_handles[h].name = name;
}
num_open_handles++;
printf("OPEN num_open_handles=%d h=%d (%s)\n",
num_open_handles, h, name);
}
/*
remove a closed handle
*/
static void gen_remove_handle_smb2(int instance, struct smb2_handle handles[NSERVERS])
{
int h;
for (h=0;h<options.max_open_handles;h++) {
if (instance == open_handles[h].instance &&
smb2_handle_equal(&open_handles[h].smb2_handle[0], &handles[0])) {
open_handles[h].active = false;
num_open_handles--;
printf("CLOSE num_open_handles=%d h=%d (%s)\n",
num_open_handles, h,
open_handles[h].name);
return;
}
}
printf("Removing invalid handle!?\n");
exit(1);
}
/*
remove a closed handle
*/
static void gen_remove_handle_smb(int instance, uint16_t handles[NSERVERS])
{
int h;
for (h=0;h<options.max_open_handles;h++) {
if (instance == open_handles[h].instance &&
open_handles[h].smb_handle[0] == handles[0]) {
open_handles[h].active = false;
num_open_handles--;
printf("CLOSE num_open_handles=%d h=%d (%s)\n",
num_open_handles, h,
open_handles[h].name);
return;
}
}
printf("Removing invalid handle!?\n");
exit(1);
}
/*
return true with 'chance' probability as a percentage
*/
static bool gen_chance(unsigned int chance)
{
return ((random() % 100) <= chance);
}
/*
map an internal handle number to a server handle
*/
static struct smb2_handle gen_lookup_handle_smb2(int server, uint16_t handle)
{
if (handle == BAD_HANDLE) return bad_smb2_handle;
return open_handles[handle].smb2_handle[server];
}
/*
map an internal handle number to a server handle
*/
static uint16_t gen_lookup_handle_smb(int server, uint16_t handle)
{
if (handle == BAD_HANDLE) return BAD_HANDLE;
return open_handles[handle].smb_handle[server];
}
/*
return a file handle
*/
static uint16_t gen_fnum(int instance)
{
uint16_t h;
int count = 0;
if (gen_chance(20)) return BAD_HANDLE;
while (num_open_handles > 0 && count++ < 10*options.max_open_handles) {
h = random() % options.max_open_handles;
if (open_handles[h].active &&
open_handles[h].instance == instance) {
return h;
}
}
return BAD_HANDLE;
}
/*
return a file handle, but skewed so we don't close the last
couple of handles too readily
*/
static uint16_t gen_fnum_close(int instance)
{
if (num_open_handles < 5) {
if (gen_chance(90)) return BAD_HANDLE;
}
return gen_fnum(instance);
}
/*
generate an integer in a specified range
*/
static int gen_int_range(uint64_t min, uint64_t max)
{
unsigned int r = random();
return min + (r % (1+max-min));
}
/*
return a fnum for use as a root fid
be careful to call GEN_SET_FNUM() when you use this!
*/
static uint16_t gen_root_fid(int instance)
{
if (gen_chance(5)) return gen_fnum(instance);
return 0;
}
/*
generate a file offset
*/
static int gen_offset(void)
{
if (gen_chance(20)) return 0;
// if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF);
return gen_int_range(0, 1024*1024);
}
/*
generate a io count
*/
static int gen_io_count(void)
{
if (gen_chance(20)) return 0;
// if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF);
return gen_int_range(0, 4096);
}
/*
generate a filename
*/
static const char *gen_fname(void)
{
const char *names[] = {"gentest\\gentest.dat",
"gentest\\foo",
"gentest\\foo2.sym",
"gentest\\foo3.dll",
"gentest\\foo4",
"gentest\\foo4:teststream1",
"gentest\\foo4:teststream2",
"gentest\\foo5.exe",
"gentest\\foo5.exe:teststream3",
"gentest\\foo5.exe:teststream4",
"gentest\\foo6.com",
"gentest\\blah",
"gentest\\blah\\blergh.txt",
"gentest\\blah\\blergh2",
"gentest\\blah\\blergh3.txt",
"gentest\\blah\\blergh4",
"gentest\\blah\\blergh5.txt",
"gentest\\blah\\blergh5",
"gentest\\blah\\.",
"gentest\\blah\\..",
"gentest\\a_very_long_name.bin",
"gentest\\x.y",
"gentest\\blah"};
int i;
do {
i = gen_int_range(0, ARRAY_SIZE(names)-1);
} while (ignore_pattern(names[i]));
return names[i];
}
/*
generate a filename with a higher chance of choosing an already
open file
*/
static const char *gen_fname_open(int instance)
{
uint16_t h;
h = gen_fnum(instance);
if (h == BAD_HANDLE) {
return gen_fname();
}
return open_handles[h].name;
}
/*
generate a wildcard pattern
*/
static const char *gen_pattern(void)
{
int i;
const char *names[] = {"gentest\\*.dat",
"gentest\\*",
"gentest\\*.*",
"gentest\\blah\\*.*",
"gentest\\blah\\*",
"gentest\\?"};
if (gen_chance(50)) return gen_fname();
do {
i = gen_int_range(0, ARRAY_SIZE(names)-1);
} while (ignore_pattern(names[i]));
return names[i];
}
static uint32_t gen_bits_levels(int nlevels, ...)
{
va_list ap;
uint32_t pct;
uint32_t mask;
int i;
va_start(ap, nlevels);
for (i=0;i<nlevels;i++) {
pct = va_arg(ap, uint32_t);
mask = va_arg(ap, uint32_t);
if (pct == 100 || gen_chance(pct)) {
va_end(ap);
return mask & random();
}
}
va_end(ap);
return 0;
}
/*
generate a bitmask
*/
static uint32_t gen_bits_mask(unsigned int mask)
{
unsigned int ret = random();
return ret & mask;
}
/*
generate a bitmask with high probability of the first mask
and low of the second
*/
static uint32_t gen_bits_mask2(uint32_t mask1, uint32_t mask2)
{
if (!options.valid && gen_chance(10)) return gen_bits_mask(mask2);
return gen_bits_mask(mask1);
}
/*
generate reserved values
*/
static uint64_t gen_reserved8(void)
{
if (options.valid) return 0;
return gen_bits_mask(0xFF);
}
static uint64_t gen_reserved16(void)
{
if (options.valid) return 0;
return gen_bits_mask(0xFFFF);
}
static uint64_t gen_reserved32(void)
{
if (options.valid) return 0;
return gen_bits_mask(0xFFFFFFFF);
}
static uint64_t gen_reserved64(void)
{
if (options.valid) return 0;
return gen_bits_mask(0xFFFFFFFF) | (((uint64_t)gen_bits_mask(0xFFFFFFFF))<<32);
}
/*
generate a boolean
*/
static bool gen_bool(void)
{
return gen_bits_mask2(0x1, 0xFF);
}
/*
generate ntrename flags
*/
static uint16_t gen_rename_flags(void)
{
if (gen_chance(30)) return RENAME_FLAG_RENAME;
if (gen_chance(30)) return RENAME_FLAG_HARD_LINK;
if (gen_chance(30)) return RENAME_FLAG_COPY;
return gen_bits_mask(0xFFFF);
}
/*
generate a pid
*/
static uint16_t gen_pid(void)
{
if (gen_chance(10)) return gen_bits_mask(0xFFFF);
return getpid();
}
/*
return a set of lock flags
*/
static uint16_t gen_lock_flags_smb2(void)
{
if (!options.valid && gen_chance(5)) return gen_bits_mask(0xFFFF);
if (gen_chance(20)) return gen_bits_mask(0x1F);
if (gen_chance(50)) return SMB2_LOCK_FLAG_UNLOCK;
return gen_bits_mask(SMB2_LOCK_FLAG_SHARED |
SMB2_LOCK_FLAG_EXCLUSIVE |
SMB2_LOCK_FLAG_FAIL_IMMEDIATELY);
}
/*
generate a lock count
*/
static off_t gen_lock_count(void)
{
return gen_int_range(0, 3);
}
/*
generate a NT access mask
*/
static uint32_t gen_access_mask(void)
{
uint32_t ret;
if (gen_chance(70)) return SEC_FLAG_MAXIMUM_ALLOWED;
if (gen_chance(70)) return SEC_FILE_ALL;
ret = gen_bits_mask(0xFFFFFFFF);
if (options.valid) ret &= ~SEC_MASK_INVALID;
return ret;
}
/*
return a lockingx lock mode
*/
static uint16_t gen_lock_mode(void)
{
if (!options.valid && gen_chance(5)) return gen_bits_mask(0xFFFF);
if (gen_chance(20)) return gen_bits_mask(0x1F);
return gen_bits_mask(LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES);
}
/*
generate a ntcreatex flags field
*/
static uint32_t gen_ntcreatex_flags(void)
{
if (gen_chance(70)) return NTCREATEX_FLAGS_EXTENDED;
return gen_bits_mask2(0x1F, 0xFFFFFFFF);
}
/*
generate a ntcreatex create options bitfield
*/
static uint32_t gen_create_options(void)
{
if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
if (gen_chance(50)) return 0;
return gen_bits_mask(NTCREATEX_OPTIONS_DELETE_ON_CLOSE | NTCREATEX_OPTIONS_DIRECTORY);
}
/*
generate a ntcreatex open disposition
*/
static uint32_t gen_open_disp(void)
{
if (gen_chance(50)) return NTCREATEX_DISP_OPEN_IF;
if (!options.valid && gen_chance(10)) return gen_bits_mask(0xFFFFFFFF);
return gen_int_range(0, 5);
}
/*
generate an openx open mode
*/
static uint16_t gen_openx_mode(void)
{
if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFF);
if (gen_chance(20)) return gen_bits_mask(0xFF);
return OPENX_MODE_DENY_NONE | gen_bits_mask(0x3);
}
/*
generate an openx flags field
*/
static uint16_t gen_openx_flags(void)
{
if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFF);
return gen_bits_mask(0x7);
}
/*
generate an openx open function
*/
static uint16_t gen_openx_func(void)
{
if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFF);
return gen_bits_mask(0x13);
}
/*
generate a file attrib combination
*/
static uint32_t gen_attrib(void)
{
uint32_t ret;
if (gen_chance(20)) {
ret = gen_bits_mask(0xFFFFFFFF);
if (options.valid) ret &= FILE_ATTRIBUTE_ALL_MASK;
return ret;
}
return gen_bits_mask(FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
}
/*
generate a unix timestamp
*/
static time_t gen_timet(void)
{
if (gen_chance(30)) return 0;
return (time_t)random();
}
/*
generate a milliseconds protocol timeout
*/
static uint32_t gen_timeout(void)
{
if (gen_chance(98)) return 0;
return random() % 50;
}
/*
generate a timestamp
*/
static NTTIME gen_nttime(void)
{
NTTIME ret;
unix_to_nt_time(&ret, gen_timet());
return ret;
}
/*
generate a timewarp value
*/
static NTTIME gen_timewarp(void)
{
NTTIME ret = gen_nttime();
if (gen_chance(98)) ret = 0;
return ret;
}
/*
generate a file allocation size
*/
static unsigned int gen_alloc_size(void)
{
unsigned int ret;
if (gen_chance(30)) return 0;
ret = random() % 4*1024*1024;
/* give a high chance of a round number */
if (gen_chance(60)) {
ret &= ~(1024*1024 - 1);
}
return ret;
}
/*
generate an ea_struct
*/
static struct ea_struct gen_ea_struct(void)
{
struct ea_struct ea;
const char *names[] = {"EAONE",
"",
"FOO!",
" WITH SPACES ",
".",
"AVERYLONGATTRIBUTENAME"};
const char *values[] = {"VALUE1",
"",
"NOT MUCH FOO",
" LEADING SPACES ",
":",
"ASOMEWHATLONGERATTRIBUTEVALUE"};
int i;
ZERO_STRUCT(ea);
do {
i = gen_int_range(0, ARRAY_SIZE(names)-1);
} while (ignore_pattern(names[i]));
ea.name.s = names[i];
do {
i = gen_int_range(0, ARRAY_SIZE(values)-1);
} while (ignore_pattern(values[i]));
ea.value = data_blob(values[i], strlen(values[i]));
if (gen_chance(10)) ea.flags = gen_bits_mask(0xFF);
ea.flags = 0;
return ea;
}
/*
generate an ea_struct
*/
static struct smb_ea_list gen_ea_list(void)
{
struct smb_ea_list eas;
int i;
if (options.no_eas) {
ZERO_STRUCT(eas);
return eas;
}
eas.num_eas = gen_int_range(0, 3);
eas.eas = talloc_array(current_op.mem_ctx, struct ea_struct, eas.num_eas);
for (i=0;i<eas.num_eas;i++) {
eas.eas[i] = gen_ea_struct();
}
return eas;
}
/* generate a security descriptor */
static struct security_descriptor *gen_sec_desc(void)
{
struct security_descriptor *sd;
if (options.no_acls || gen_chance(90)) return NULL;
sd = security_descriptor_dacl_create(current_op.mem_ctx,
0, NULL, NULL,
NULL,
SEC_ACE_TYPE_ACCESS_ALLOWED,
SEC_FILE_WRITE_DATA | SEC_STD_WRITE_DAC,
SEC_ACE_FLAG_OBJECT_INHERIT,
SID_WORLD,
SEC_ACE_TYPE_ACCESS_ALLOWED,
SEC_FILE_ALL | SEC_STD_ALL,
0,
NULL);
return sd;
}
static void oplock_handler_close_recv_smb(struct smbcli_request *req)
{
NTSTATUS status;
status = smbcli_request_simple_recv(req);
if (!NT_STATUS_IS_OK(status)) {
printf("close failed in oplock_handler\n");
smb_panic("close failed in oplock_handler");
}
}
/*
the oplock handler will either ack the break or close the file
*/
static bool oplock_handler_smb(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private_data)
{
union smb_close io;
int i, j;
bool do_close;
struct smbcli_tree *tree = NULL;
struct smbcli_request *req;