forked from cgustave/fgtconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfg_rules.pm
More file actions
1988 lines (1520 loc) · 64.8 KB
/
Copy pathcfg_rules.pm
File metadata and controls
1988 lines (1520 loc) · 64.8 KB
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
# ****************************************************
# * *
# * C F G F E A T U R E *
# * *
# * XML rules based feature check *
# * *
# * Author : Cedric Gustave cgustave@fortinet.com *
# * *
# ****************************************************
#
# This object provides generic tools to check feature
# from rules defined in XML files
# This is a virtual object
# use for both global and vdom
package cfg_rules ;
my $obj = cfg_rules ;
use Moose ;
use Data::Dumper ;
use XML::LibXML ;
# record the current processed vdom (undef if global section)
has 'vdomCurrent' => (isa => 'Str', is => 'rw') ;
# ---
sub BUILD {
my $self = shift ;
# Load rules_global.xml and rules_vdom.xml
$self->{XMLDOC} = undef ; # LIBXML DOC reference
# Init tag list
$self->{TAGS} = undef ;
# Init scope memory
$self->{SCOPE_MEMORY} = undef ;
# Load the XML rule files
$self->_load_XML_rules() ;
# Init scope
$self->{SCOPE} = (undef, undef) ; # means scope on all config
}
# ---
sub initScope {
my $subn = "initScope" ;
# Initialize object scopes before processing global and each vdom
# scope is set depending on vdom or global
my $self = shift ;
warn "\n* Entering $obj:$subn vdomCurrent=" . $self->vdomCurrent . " (nothing means global)" if $self->debug ;
# Init scope pointer depending on who we are
if (ref($self) eq "cfg_global") {
warn "$obj:$subn init for global" if $self->debug ;
$self->{SCOPE}[0] = undef ;
$self->{SCOPE}[1] = undef ; # means scope on all config
}
elsif (ref($self) eq "cfg_vdoms") {
my $vdom = $self->vdomCurrent ;
warn "$obj:$subn init for vdom=$vdom" if $self->debug ;
# sanity
die "undefined vdom in vdom processing" if (not(defined($vdom))) ;
# Get vdom boundaries (defined in cfg_vdoms
my $startIndex = $self->cfg->vdom_index(action => 'get', vdom => $vdom, type => 'startindex') ;
my $endIndex = $self->cfg->vdom_index(action => 'get', vdom => $vdom, type => 'endindex') ;
warn "$obj:$subn init scope for vdom=$vdom => startIndex=$startIndex endIndex=$endIndex" if $self->debug ;
$self->{SCOPE}[0] = $startIndex ;
$self->{SCOPE}[1] = $endIndex ;
}
else {
die "$obj:$subn unexpected object" ;
}
}
# --
sub resetScopeSets {
my $subn = "resetScopeSets" ;
# Resets all recorded scope sets
# It is important to reset them before processing a new vdom or a scop recall from previous vdom
# could be used in new vdom and cause unexpected results in case of mistakes in rules definitions
# This gave me a hard time in Nov 2017...
my $self = shift ;
warn "\n* Entering $obj:$subn" if $self->debug ;
$self->{SCOPE_MEMORY} = {} ;
}
# ---
sub dataDefined {
my $subn = "dataDefined" ;
# Check if rule is defined (if no key is given)
# Check if key on rule is defined if both ruleId and key are defined
# This is generic function for global and vdoms.
# If vdom is not defined, it is considered global
my $self = shift ;
my %options = @_ ;
my $vdom = $options{'vdom'} ;
my $ruleId = $options{'ruleId'} ;
my $key = $options{'key'} ;
my $value = undef ;
warn "\n* Entering $obj:$subn with vdom=$vdom ruleId=$ruleId key=$key" if $self->debug ;
# Sanity
die "ruleId is required" if (not(defined($ruleId))) ;
# GLobal
if (not(defined($vdom))) {
warn "$obj:$subn global - ruleId=$ruleId Key=$key" if $self->debug ;
if (defined($key)) {
if ($self->{RULEDB_GLOBAL}->{$ruleId}->{$key}) {
return 1 ;
}
else {
return 0 ;
}
}
else {
if ($self->{RULEDB_GLOBAL}->{$ruleId}) {
return 1 ;
}
else {
return 0 ;
}
}
}
else {
warn "$obj:$subn: vdom=$vdom - ruleId=$ruleId key=$key" if $self->debug ;
if (defined($key)) {
if ($self->{RULEDB_VD}->{$vdom}->{$ruleId}->{$key}) {
return 1 ;
}
else {
return 0 ;
}
}
else {
if ($self->{RULEDB_VD}->{$vdom}->{$ruleId}) {
return 1 ;
}
else {
return 0 ;
}
}
}
}
# ---
sub dataGet {
my $subn = "dataGet" ;
# Get data into rule DB
# This is generic function for global and vdoms.
# If vdom is not defined, it is considered global
my $self = shift ;
my %options = @_ ;
my $vdom = $options{'vdom'} ;
my $ruleId = $options{'ruleId'} ;
my $key = $options{'key'} ;
my $value = undef ;
warn "\n* Entering $obj:$subn with vdom=$vdom ruleId=$ruleId key=$key" if $self->debug ;
# Sanity
die "ruleId is required" if (not(defined($ruleId))) ;
die "key is required" if (not(defined($key))) ;
# GLobal
if (ref($self) eq 'cfg_global') {
$value = $self->{RULEDB_GLOBAL}->{$ruleId}->{$key} ;
warn "$obj:$subn global - ruleId=$ruleId Key=$key, get value=$value" if $self->debug ;
$value = "" if (not(defined($value))) ;
return $value ;
}
elsif (ref($self) eq 'cfg_vdoms') {
die "vdom required" if (not(defined($vdom))) ;
$value = $self->{RULEDB_VD}->{$vdom}->{$ruleId}->{$key} ;
warn "$obj:$subn: vdom=$vdom - ruleId=$ruleId key=$key, get value=$value" if $self->debug ;
$value = "" if (not(defined($value))) ;
return $value ;
}
else {
die "called from non expected object" ;
}
}
# ---
sub dataSet {
my $subn = "dataSet" ;
# Set data into rule DB
# Rhis is generic function for global and vdoms.
# If vdom is not defined, it is considered global
my $self = shift ;
my %options = @_ ;
my $vdom = $options{'vdom'} ;
my $ruleId = $options{'ruleId'} ;
my $key = $options{'key'} ;
my $value = $options{'value'} ;
warn "\n* Entering $obj:$subn with vdom=$vdom ruleId=$ruleId key=$key value=$value" if $self->debug ;
# Sanity
die "ruleId is required" if (not(defined($ruleId))) ;
die "key is required" if (not(defined($key))) ;
# Global
if (not(defined($vdom))) {
warn "$obj:$subn global: ruleId=$ruleId Key=$key, Set value=$value" if $self->debug ;
$self->{RULEDB_GLOBAL}->{$ruleId}->{$key} = $value ;
}
# vdom
else {
warn "$obj:$subn: vdom=$vdom : ruleId=$ruleId - key=$key, Set value=$value" if $self->debug ;
$self->{RULEDB_VD}->{$vdom}->{$ruleId}->{$key} = $value ;
}
}
# ---
sub warnAdd {
my $subn = "warnAdd" ;
# Adds a new warning for both global and vdom
my $self = shift ;
my %options = @_ ;
my $warn = $options{'warn'} ;
my $toolTip = $options{'toolTip'} ;
my $severity = defined($options{'severity'}) ? $options{'severity'} : 'medium' ;
my $base = undef ;
my $baseDebug = undef ;
my $vdom = defined($self->vdomCurrent) ? $self->vdomCurrent : 'global' ;
warn "\* Entering $obj:$subn warn=$warn severity=$severity toolTip=$toolTip (rule=" . $self->{RULE_ID} . " vdom=$vdom)" if $self->debug ;
# Sanity
die "severity can only be low medium or high" if ($severity !~ /low|medium|high/) ;
die "warn is require" if (not(defined($warn))) ;
# Set base warn hash_ref
if (ref($self) eq 'cfg_global') {
$base = $self->{WARN_GLOBAL} ;
$baseDebug = "global" ;
}
elsif (ref($self) eq 'cfg_vdoms') {
if (defined($vdom) and ($vdom ne "")) {
# Must create the reference before getting it !
$self->{WARN_VD}->{$vdom} = {} if (not($self->{WARN_VD}->{$vdom})) ;
$base = $self->{WARN_VD}->{$vdom} ;
$baseDebug = "vdom=$vdom" ;
}
else {
die "no vdom defined, don't know on which vdom we are" ;
}
}
else {
die "Call from unexpected object" ;
}
# Don't do anything if warning is already set
if ($base->{$warn}) {
warn "$obj:$subn warning warn=$warn is already in our list, skeeping" if $self->debug ;
return ;
}
# Add new warn
warn "$obj:$subn Adding warn on baseDebug=\"$baseDebug\" warn=$warn vdom=$vdom" if $self->debug ;
$base->{$warn}->{'toolTip'} = $toolTip ;
$base->{$warn}->{'severity'} = $severity ;
}
# ---
sub warnTable {
my $subn = "warnTable" ;
# Retrun a hash table either global, either per vdom
# if no vdom is provided, global is asked
# if no warning for a vdom, undef is returned
my $self = shift ;
my %options = @_ ;
my $vdom = $options{'vdom'} ;
warn "\* Entering $obj:$subn with vdom=$vdom" if $self->debug ;
if (ref($self) eq 'cfg_global') {
warn "$obj:$subn returning global vdom warn table" if $self->debug ;
return $self->{WARN_GLOBAL} ;
}
elsif (ref($self) eq 'cfg_vdoms') {
die "vdom missing" if (not(defined($vdom))) ;
if ($self->{WARN_VD}->{$vdom}) {
warn "$obj:$subn returning vdom=$vdom warn table" if $self->debug ;
return $self->{WARN_VD}->{$vdom} ;
}
else {
warn "$obj:$subn no warnings for vdom=$vdom" if $self->debug ;
return ;
}
}
else {
die "Call from unexpected object" ;
}
}
# ---
sub processRules {
my $subn = "processRules" ;
# Start rules processing for both global and vdoms
# if not defined ($self->vdomCurrent) then this is for global
my $self = shift ;
warn "\n* Entering $obj:$subn (vdomCurrent=" . $self->vdomCurrent . ")" if $self->debug ;
# Init global vdom warnings
$self->{WARN_GLOBAL} = {} ;
# do not init $self->{WARN_VD} ! or it is cleared for all vdom
# each time a new vdom is processed.
# Start processing our groups
$self->{GRP_NAME} = undef ;
$self->{GRP_DESC} = "" ;
$self->{GRP_NODE} = undef ;
$self->_process_groups() ;
# For debuging after all rules processing print the $self->{RULE} hash table
if ($self->debug) {
if (not(defined($self->vdomCurrent))) {
warn "Global DB" ;
warn "RuleTable: " . Dumper $self->{RULEDB_GLOBAL} ;
warn "Warnings : " . Dumper $self->{WARN_GLOBAL} ;
}
else {
warn "VDOM DB vdom=" . $self->vdomCurrent ;
warn "RuleTable (" . $self->vdomCurrent . "): " . Dumper $self->{RULEDB_VD}->{$self->vdomCurrent} ;
warn "Warnings (" . $self->vdomCurrent . "): " . Dumper $self->{WARN_VD}->{$self->vdomCurrent} ;
}
}
}
# ---
sub hasMatched {
my $subn = "hasMatched" ;
# Returns 1 if the rule has matched or 0 if not
# function for both global and vdom
my $self = shift ;
my %options = @_ ;
my $ruleId = $options{'ruleId'} ;
my $vdom = $options{'vdom'} ; # if not provided, we rely on $self->vdomCurrent
$vdom = defined($vdom) ? $vdom : $self->vdomCurrent ;
warn "\n* Entering $obj:$subn with ruleId=$ruleId vdom=$vdom" if $self->debug ;
# Sanity
die "-ruleId is required" if (not(defined($ruleId))) ;
die "unknow rule id=$ruleId" if (not($self->dataDefined(vdom => $vdom, ruleId => $ruleId))) ;
# not necessary if no match on the rule
return 0 if (not($self->dataDefined(vdom => $vdom, ruleId => $ruleId, key => 'hasMatched'))) ;
if ($self->dataGet(vdom => $vdom, ruleId => $ruleId, key => 'hasMatched')) {
return 1 ;
}
else {
return 0 ;
}
}
# ---
sub expandVariables {
my $subn = "expandVariables" ;
# replace the variables inside the message with their values
# takes a string reference as input and modifies the string
my $self = shift ;
my %options = @_ ;
my $ref_message = $options{'messageRef'} ;
my $round = $options{'round'} ? $options{'round'} : 0 ;
my ($myrule, $myvar, $value) = undef ;
warn "\n* Entering $obj:$subn with message=" . $$ref_message . " round=$round" if $self->debug ;
my $variable = undef ;
if (($variable) = $$ref_message =~ /(?:\$)(\S+?)(?:\$)/) { # Note \S+? where ? is the necessary non-greedy modifier
warn "$obj:$subn rule=" . $self->{'RULE_ID'} . " found variable $variable" if $self->debug ;
# looking for variable in the same rule (variable name is related to rule)
if ($self->dataGet(vdom => $self->vdomCurrent, ruleId => $self->{'RULE_ID'}, key => $variable)) {
warn "$obj:$subn variable relative to rule exists value="
. $self->dataGet(vdom => $self->vdomCurrent, ruleId => $self->{'RULE_ID'}, key => $variable)
if $self->debug ;
$value = $self->dataGet(vdom => $self->vdomCurrent, ruleId => $self->{'RULE_ID'}, key => $variable) ;
# replacing
$$ref_message =~ s/\$$variable\$/$value/ ;
}
# see if the variable has absolute name (including rule id)
# in case variable comes from another rule
if (($myrule, $myvar) = $variable =~ /(\S+)\.(\S+)/) { # Here we use greedy modifier, the rule is more likely to have . in its name
warn "$obj:$subn check if variable has an absolute name rule=$myrule var=$myvar" if $self->debug ;
if ($self->dataGet(vdom => $self->vdomCurrent, ruleId => $myrule, key => $myvar)) {
warn "$obj:$subn found variable with obsolute name in format <rule>.<var> rule=$myrule var=$myvar with value="
. $self->dataGet(vdom => $self->vdomCurent, ruleId => $myrule, key => $myvar)
if $self->debug ;
$value = $self->dataGet(vdom => $self->vdomCurrent, ruleId => $myrule, key => $myvar) ;
# replacing
$$ref_message =~ s/\$$variable\$/$value/ ;
}
else {
warn "$obj:$subn can't find absolute name variable $variable with rule=$myrule variable=$myvar in the rule file" if $self->debug ;
}
}
else {
warn "$obj:$subn variable $variable could not be found in the rule file" if $self->debug ;
}
}
warn "$obj:$subn processed message=$$ref_message" if $self->debug ;
# If more processing is needed, go for another iteration
# We need to limit the number of loops in case no resolution is done
if ($round <= 3) {
if ($$ref_message =~ /\$\S+\$/) {
warn "$obj:$subn round=$round : more variables to process, go for another iteration" if $self->debug ;
$round++ ;
$self->expandVariables(messageRef => $ref_message, round => $round) if (defined($ref_message) and ($ref_message =~ /\$/)) ;
}
}
else {
warn "$obj:$subn ruleId="
. $self->{'RULE_ID'}
. " message=$$ref_message - some variable resolution still not done after 3 rounds. cancel resoltion"
if $self->debug ;
}
}
# ---
sub expandLoopVariables {
my $subn = "expandLoopVariables" ;
# replace the variables inside the message with their values
# takes a string reference as input and modifies the string
my $self = shift ;
my %options = @_ ;
my $ref_message = $options{'messageRef'} ;
warn "\n* Entering $obj:$subn with message=" . $$ref_message if $self->debug ;
return if (not(defined($$ref_message))) ;
# Nothing to do if no variables are set
return if (not(defined($self->{VARIABLES}))) ;
# We do have variables, look for $1, $2 ... and expand with variable index (remove 1 because table starts at 0)
while ($$ref_message =~ /\$(\d+)/g) {
my $tabIndex = $1 - 1 ;
warn "$obj:$subn index=$1 found, expanding message=$$ref_message with " . @{$self->{VARIABLES}}[$tabIndex] if $self->debug ;
$$ref_message =~ s/\$$1/@{$self->{VARIABLES}}[$tabIndex]/g ;
warn "$obj:$subn expanded string=$$ref_message" if $self->debug ;
}
}
# ---
sub _process_groups {
my $subn = "_process_groups" ;
my $self = shift ;
warn "\n* Entering $obj:$subn" if $self->debug ;
# Sanity
die "XML doc not loaded" if (not(defined($self->{XMLDOC}))) ;
my $XQuery = "/groups/group" ;
my $nodeSet = $self->{XMLDOC}->find($XQuery) ;
foreach my $node ($nodeSet->get_nodelist()) {
$self->{GRP_NAME} = $node->getAttribute('name') ;
$self->{GRP_DESC} = $node->getAttribute('description') ;
$self->{GRP_NODE} = $node ; # Recording group node for further processing
warn "$obj:$subn found group name=" . $self->{GRP_NAME} . " with description=" . $self->{GRP_DESC} if $self->debug ;
# Reset rules pointers
$self->{RULE_NODE} = undef ;
# Process all loop statements if any in the group first
my $saveNode = $node ;
$self->_process_loop() ;
# Restore the groupe node at that top of the group
# and then process rules (not in loop) if any restart
$self->{GRP_NODE} = $saveNode ;
$self->_process_rules() ;
}
}
# ---
sub _process_loop {
my $subn = "_process_loop" ;
# Process all loops from the group
my $self = shift ;
warn "\n* Entering $obj:$subn" if $self->debug ;
# Sanity
die "XML doc not loaded" if (not(defined($self->{XMLDOC}))) ;
my $XQuery = "./loop" ;
my $nodeSet = $self->{GRP_NODE}->find($XQuery) ;
foreach my $node ($nodeSet->get_nodelist()) {
warn "$obj:$subn found a loop statement, extracting elements" if $self->debug ;
my $elements = $node->getAttribute('elements') ;
warn "$obj:$subn elements=$elements" if $self->debug ;
# Extract elements in a non greedy way
while ($elements =~ /(\[\S+?\])/g) {
my $element = $1 ;
warn "$obj:$subn element=$element" if $self->debug ;
# process_rules using the extracted variables
# Give the loop node as the group node
# Original group node will be later restored when processing the rules from the group that are not part of a loop
$self->{GRP_NODE} = $node ;
$self->_process_rules(variables => $element) ;
}
}
}
# ---
sub _process_rules {
my $subn = "_process_rules" ;
# We are in a group and going through rules one by one
my $self = shift ;
my %options = @_ ;
my $variables = defined($options{'variables'}) ? $options{'variables'} : "" ;
warn "\n* Entering $obj:$subn (vdomCurrent=" . $self->vdomCurrent . " group=" . $self->{GRP_NAME} if $self->debug ;
# Sanity
die "no group node" if (not(defined($self->{GRP_NODE}))) ;
# loop statement : get variables if any and record variables in global variable for the duration of the rule processing
my @vars ;
if ($variables ne "") {
while ($variables =~ /(\d+|\w+),?/g) {
warn "$obj:$subn variables $1 " if $self->debug ;
push @vars, $1 ;
}
#foreach my $var (@vars) {
# print "var=$var " ;
# }
#print "\n" ;
# Store variables
$self->{VARIABLES} = \@vars ;
}
else {
$self->{VARIABLES} = undef ;
}
# processing rules
my $XQuery = "./rule" ;
my $nodeSet = $self->{GRP_NODE}->find($XQuery) ;
foreach my $node ($nodeSet->get_nodelist()) {
# Set all pointers needed when processing a new rule
$self->{RULE_ID} = $node->getAttribute('id') ;
if ($self->{RULE_ID} =~ /\$\d/) {
$self->expandLoopVariables(messageRef => \$self->{RULE_ID}) ;
}
$self->{RULE_DESC} = $node->getAttribute('description') ;
if (defined($self->{RULE_DESC}) and ($self->{RULE_DESC} =~ /\$\d/)) {
$self->expandLoopVariables(messageRef => \$self->{RULE_DESC}) ;
}
$self->{RULE_DEBUG} = $node->getAttribute('debug') ;
$self->{RULE_NODE} = $node ;
# init non explicit value
$self->{RULE_DEBUG} = 'disable' if ((not(defined($self->{RULE_DEBUG}))) or ($self->{RULE_DEBUG} ne 'enable')) ;
warn "$obj:$subn found rule=" . $self->{RULE_ID} . " with description=" . $self->{RULE_DESC} . " debug=" . $self->{RULE_DEBUG} if $self->debug ;
# Reset all rules flags
$self->{RULE_FLAG_MATCHED} = 0 ; # The rule has not matched
# Go through all processing of the found rule
$self->_process_this_rule() ;
}
}
# ---
sub _process_this_rule {
my $subn = "_process_this_rule" ;
my $self = shift ;
warn "\n* Entering $obj:$subn (group=" . $self->{GRP_NAME} . " rule=" . $self->{RULE_ID} if $self->debug ;
# Sanity
die "no rule node" if (not(defined($self->{RULE_NODE}))) ;
# Turn rule debugging on if asked by -ruledebug <rule_id>
my $rd = $self->ruledebug() ;
if (defined($rd)) {
if ($rd eq $self->{RULE_ID}) {
warn "$obj:$subn - Turning debug on for rule=" . $self->{RULE_ID} . " as per -ruledebug" ;
$self->debug(255) ;
}
else {
$self->debug(0) ;
}
}
# Turn rule debugging on if debug='enable' set in rule file
if ($self->{RULE_DEBUG} eq 'enable') {
warn "$obj:$subn rule debug set by rule config for rule id=" . $self->{RULE_ID} ;
$self->debug(255) ;
}
else {
$self->debug(0) ;
}
# Init flags
$self->dataSet(vdom => $self->vdomCurrent, ruleId => $self->{RULE_ID}, key => 'hasMatched', value => '0') ;
# Init scope for the new rule
$self->initScope() ;
# Process match blocks
$self->_process_match_blocks() ;
# Process rules statements
$self->_process_rules_statements(node => $self->{RULE_NODE}) ;
}
# ---
sub _process_match_blocks {
my $subn = "_process_match_blocks" ;
# Go through the sequences of <match> blocks with a stop on match logic
# So if a match block condition is ok, procesing of next block depends on 'logic' and if the rule has matched
# A match block without any condition set is a catchall
# If no match block are defined, there is no pre-condition so go to <scope> processing
my $self = shift ;
warn "\n* Entering $obj:$subn (group=" . $self->{GRP_NAME} . " rule=" . $self->{RULE_ID} if $self->debug ;
my $XQuery = "./match" ;
my $nodeSet = $self->{RULE_NODE}->find($XQuery) ;
foreach my $node ($nodeSet->get_nodelist()) {
my $MatchRelease = $node->getAttribute('release') ;
my $MatchBuildMin = $node->getAttribute('buildMin') ;
my $MatchBuildMax = $node->getAttribute('buildMax') ;
my $MatchTag = $node->getAttribute('tag') ;
my $MatchNoTag = $node->getAttribute('noTag') ;
my $MatchRules = $node->getAttribute('rules') ;
my $MatchLogic = $node->getAttribute('logic') ;
# Default and Sanity
$MatchLogic = 'stopOnMatch' if (not(defined($MatchLogic))) ;
die "MatchLogic can only be stopOnMatch*|nextIfRuleNoMatch for rule" . $self->{RULE_ID}
if ($MatchLogic !~ /stopOnMatch|nextIfRuleNoMatch/) ;
warn
"$obj:$subn found <match> with MatchRelease=$MatchRelease MatchBuildMin=$MatchBuildMin MatchBuildMax=$MatchBuildMax MatchTag=$MatchTag MatchNoTag=$MatchNoTag MatchRules=$MatchRules MatchLogic=$MatchLogic"
if $self->debug ;
my $hasMatched = $self->_process_this_match(
node => $node,
MatchRelease => $MatchRelease,
MatchBuildMin => $MatchBuildMin,
MatchBuildMax => $MatchBuildMax,
MatchTag => $MatchTag,
MatchNoTag => $MatchNoTag,
MatchRules => $MatchRules
) ;
if ($hasMatched) {
# if (stopOnMatch), we stop here whatever the status of the rule
# Stop further match processing as one has already matched
my $ruleStatus = $self->dataGet(vdom => $self->vdomCurrent, ruleId => $self->{RULE_ID}, key => 'hasMatched') ;
if ($MatchLogic eq 'stopOnMatch') {
last ;
warn "$obj:$subn: MatchLogic=$MatchLogic : match block has matched, stop here (rule status=$ruleStatus)" if $self->debug ;
}
elsif ($MatchLogic eq 'nextIfRuleNoMatch') {
if ($ruleStatus) {
last ;
warn "$obj:$subn: MatchLogic=$MatchLogic : match block has matched, rule has matched, stop here" if $self->debug ;
}
else {
warn "$obj:$subn: MatchLogic=$MatchLogic : match block has matched, rule has NOT matched, continue next block" if $self->debug ;
}
}
}
}
}
# ---
sub _process_this_match {
my $subn = "_process_this_match" ;
# Eval the match condition one by one with AND logic
# Return 1 if all the condition given match otherwise 0
my $self = shift ;
my %options = @_ ;
my $node = $options{'node'} ;
my $MatchRelease = $options{'MatchRelease'} ;
my $MatchBuildMin = $options{'MatchBuildMin'} ;
my $MatchBuildMax = $options{'MatchBuildMax'} ;
my $MatchTag = $options{'MatchTag'} ;
my $MatchNoTag = $options{'MatchNoTag'} ;
my $MatchRules = $options{'MatchRules'} ;
# Default return is 1 but will be set to 0 if a match condition fails
my $return = 1 ;
warn
"\n* Entering $obj:$subn with MatchRelease=$MatchRelease MatchBuildMin=$MatchBuildMin MatchBuildMax=$MatchBuildMax MatchTag=$MatchTag MatchNoTag=$MatchNoTag MatchRules=$MatchRules"
if $self->debug ;
# Sanity
die "node is required" if not(defined($node)) ;
# Checking release condition
if (defined($MatchRelease)) {
my $cfg_version = $self->cfg->config_version() ;
warn "$obj:$subn verifying match release condition cfg_version=$cfg_version against given MatchRelease=$MatchRelease" if $self->debug ;
if ($cfg_version =~ /$MatchRelease/) {
warn "$obj:$subn pass release test $MatchRelease" if $self->debug ;
}
else {
warn "$obj:$subn failed release test. Stopping here" if $self->debug ;
return (0) ;
}
}
# Checking mininum build condition
if (defined($MatchBuildMin)) {
warn "$obj:$subn verifying match minimum build condition cfg_build=" . $self->cfg->build() . " against given MatchBuildMin=$MatchBuildMin"
if $self->debug ;
if ($self->cfg->build() >= $MatchBuildMin) {
warn "$obj:$subn pass minimum build test" if $self->debug ;
}
else {
warn "$obj:$subn failed minimum build test. Stopping here" if $self->debug ;
return (0) ;
}
}
# Checking maximum build condition
if (defined($MatchBuildMax)) {
warn "$obj:$subn verifying match maximum build condition cfg_build=" . $self->cfg->build() . " against given MatchBuildMax=$MatchBuildMax"
if $self->debug ;
if ($self->cfg->build() <= $MatchBuildMax) {
warn "$obj:$subn pass maximum build test" if $self->debug ;
}
else {
warn "$obj:$subn failed maximum build test. Stopping here" if $self->debug ;
return (0) ;
}
}
# Checking if the given rule test logic matches
if (defined($MatchRules)) {
warn "$obj:$subn evaluating matching for rules binary logic with MatchRules=$MatchRules" if $self->debug ;
if ($self->_match_rules_check_pass(MatchRules => $MatchRules)) {
warn "$obj:$subn pass match rules test" if $self->debug ;
# report the policy has matched
warn "$obj:$subn rule=" . $self->{RULE_ID} . " match rule check statement has matched" if $self->debug ;
$self->dataSet(vdom => $self->vdomCurrent, ruleId => $self->{RULE_ID}, key => 'hasMatched', value => '1') ;
}
else {
warn "$obj:$subn failed match rules test" if $self->debug ;
return (0) ;
}
}
# Init TAGS for vdom
my $vdom = defined($self->vdomCurrent) ? $self->vdomCurrent : 'global' ;
$self->{TAGS}->{$vdom} = "" if (not(defined($self->{TAGS}->{$vdom}))) ;
# Checking if the given tag is in our list of tags (allow loop variable expansion)
if (defined($MatchTag)) {
$self->expandLoopVariables(messageRef => \$MatchTag) ;
warn "$obj:$subn verifying match MatchTag against vdom=$vdom given MatchTag=$MatchTag against TAGS list=" . $self->{TAGS}->{$vdom}
if $self->debug ;
if ($self->{TAGS}->{$vdom} =~ /$MatchTag/) {
warn "$obj:$subn vdom=$vdom pass match tag $MatchTag" if $self->debug ;
}
else {
warn "$obj:$subn vdom=$vdom failed match tag $MatchTag" if $self->debug ;
return (0) ;
}
}
# Checking if the given tag is not in our list of tags
if (defined($MatchNoTag)) {
$self->expandLoopVariables(messageRef => \$MatchTag) ;
warn "$obj:$subn verifying match MatchTag against vdom=$vdom given MatchTag=$MatchTag" if $self->debug ;
if ($MatchNoTag !~ /$self->{TAGS}->{$vdom}/) {
warn "$obj:$subn vdom=$vdom pass match noTag $MatchNoTag" if $self->debug ;
}
else {
warn "$obj:$subn vdom=$vdom failed match noTag $MatchNoTag" if $self->debug ;
return (0) ;
}
}
# At this point, if we have failed one of the match statement, we would have returned already
# Go through rules statements within the <match> block
$self->_process_rules_statements(node => $node) ;
# report if the block has matched
return ($return) ;
}
# ---
sub _match_rules_check_pass {
my $subn = "_match_rules_check_pass" ;
# Parse the <matche rules=''> expression, extract rules and see if they match
# combine with OR/AND logical operators with rules ID and eval the resulting expression
my $self = shift ;
my %options = @_ ;
my $MatchRules = $options{'MatchRules'} ;
my ($ruleId, $ruleMatch) ;
my ($result, $op) ; # must be undef to start
warn "\n * Entering $obj:$subn with MatchRules=$MatchRules" if $self->debug ;
# Extract tokens
my @tokens ;
if (
(@tokens) =
$MatchRules =~ /(\$\S+\$) # rule : 2 possible format with or wihout group : $group.rule$ or $rule$
(?:\s*) # ignore spaces
(OR|AND)? # op
(?:\s*) # ignore spaces
/gx
)
{
warn "$obj:$subn some tokens were extracted" if $self->debug ;
# Process each tokens
foreach my $token (@tokens) {
next if (not(defined($token))) ;
warn "$obj:$subn current result=$result, new token=$token" if $self->debug ;
# the token is a rule
if (($ruleId) = $token =~ /\$(\S+)\$/) {
die "rule=" . $self->{RULE_ID} . "rules statement references an undefined rule id" if (not(defined($ruleId))) ;
$ruleMatch = $self->hasMatched(vdom => $self->vdomCurrent, ruleId => $ruleId) ;
warn "$obj:$subn token is a valid rule reference ruleId=$ruleId with ruleMatch=$ruleMatch" if $self->debug ;
# This is the initial result if no operator has been seen yet
if (not(defined($result)) and (not(defined($op)))) {
warn "$obj:$subn initial result set with $ruleMatch" if $self->debug ;
$result = $ruleMatch ;
}
# compute result from the last seen operator
elsif ((defined($result)) and (defined($op))) {
my $logic ;
$logic = '|' if ($op eq 'OR') ;
$logic = '&' if ($op eq 'AND') ;
die "undefined logic" if (not(defined($logic))) ;
my $evalString = $result . ' ' . $logic . ' ' . $ruleMatch ;
$result = eval($evalString) ;
warn "$obj:$subn evalString=$evalString => eval result=$result" if $self->debug ;
die "incorrect rule format for ruleid=" . $self->{RULE_ID} if (not(defined($result))) ;
}
# incorrect logic format
else {
die "rule " . $self->{RULE_ID} . " format is wrong" ;
}
}
# Token is an operator
elsif (($op) = $token =~ /(AND|OR)/) {
warn "$obj:$subn token is an operator $op" if $self->debug ;
}
}
}
# Could not see one token from the rules
else {
die "Failed to extract <match rules=>tokens for rule=" . $self->{RULE_ID} ;
}
# return the last state or computed result
warn "$obj:$subn overall expression result=$result" if $self->debug ;
return ($result) ;
}
# ---
sub _process_rules_statements {
my $subn = "_process_rules_statements" ;
my $self = shift ;
my %options = @_ ;
my $node = $options{'node'} ;
warn "\n* Entering $obj:$subn" if $self->debug ;
# Sanity
die "no node given" if not(defined($node)) ;
# Process <scope> statements