| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | File::OSS::Scan::Ruleset - initialize the scan rules | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 VERSION | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | version 0.04 | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | use File::OSS::Scan::Ruleset; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | File::OSS::Scan::Ruleset->init($config_file); | ||||||||||||||||||||||||||||
| 14 | my $ruleset = File::OSS::Scan::Ruleset->get_ruleset(); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | This is an internal module used by L<File::OSS::Scan> to initialise scan rules from | ||||||||||||||||||||||||||||
| 19 | the configuration file, and should not be called directly. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =over 4 | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =item * L<File::OSS::Scan> | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =back | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | Harry Wang <harry.wang@outlook.com> | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | This software is Copyright (c) 2014 by Harry Wang. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Artistic License 1.0 | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | package File::OSS::Scan::Ruleset; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | ▶ | use Fatal qw( open close ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use Data::Dumper; # for debug | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | use File::OSS::Scan::Constant qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | my $cfg_default = $ENV{OSSSCAN_CONFIG} || ".ossscan.rc"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{'OSSSCAN_CONFIG'} || '.ossscan.rc'
MC/DC0%0/1
$ENV{'OSSSCAN_CONFIG'} || '.ossscan.rc'
$ENV{'OSSSCAN_CONFIG'}
Truth table50%1/2
A
A= $ENV{'OSSSCAN_CONFIG'}
| |||||||||||||||||||||||||||||
| 58 | 1 | my @valid_sections = qw/GLOBAL FILE DIRECTORY LINE/; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # global var ... | ||||||||||||||||||||||||||||
| 61 | 1 | our $ruleset = undef; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=17 SCAR=57.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | my $config_file = shift || $cfg_default; | ||||||||||||||||||||||||||
|
Condition0%0/3
shift() || $cfg_default
MC/DC0%0/2
shift() || $cfg_default
shift()
$cfg_default
Truth table0%0/3
A || B
A= shift()B= $cfg_default
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | local *CONFIG; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( ! -f $config_file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | carp "config file $config_file doesn't exist, using the embedded ruleset."; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # read from __DATA__ section | ||||||||||||||||||||||||||||
| 73 | 0 | *CONFIG = *DATA; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | croak "config file $config_file is not readable." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | if ( ! -r $config_file ); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | open( CONFIG, $config_file ) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | croak "Can't open $config_file, $!."; | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # clear previously set config $ruleset | ||||||||||||||||||||||||||||
| 84 | 0 | undef $ruleset; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | my ( $section, $rule ) = ( undef, undef ); | |||||||||||||||||||||||||||
| 87 | 0 | my $invalid_section_flag = UNI_FALSE; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | while(<CONFIG>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | chomp; # remove newline | |||||||||||||||||||||||||||
| 91 | 0 | s/#.*//; # remove comments | |||||||||||||||||||||||||||
| 92 | 0 | s/^\s+//; # remove leading spaces | |||||||||||||||||||||||||||
| 93 | 0 | s/\s+$//; # remove trailing spaces | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # anything left ? | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | next unless length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # parse sections | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ( /^\[(\w+)\].*/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $section = uc $1; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # skip invalid sections | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( ! grep {/^$section$/} @valid_sections ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | carp "Invalid section name $section, skipping ..."; | |||||||||||||||||||||||||||
| 105 | 0 | $invalid_section_flag = UNI_TRUE; | |||||||||||||||||||||||||||
| 106 | 0 | next; | |||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||
| 108 | 0 | $invalid_section_flag = UNI_FALSE; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | $ruleset->{$section} = undef | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ( not exists $ruleset->{$section} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # parse settings | ||||||||||||||||||||||||||||
| 116 | # put them all under 'GLOBAL' section | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ( /^(\w+)\s*\:\s*(.*)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | my ( $key, $val ) = ( $1, $2 ); | |||||||||||||||||||||||||||
| 119 | 0 | $ruleset->{'GLOBAL'}->{$key} = [ split /\s+/, $val ]; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # parse rules | ||||||||||||||||||||||||||||
| 123 | # if ( /^([\w\%\-\s]+)$/ ) { | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if ( /^(\d+\%?.*)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $rule = $1; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # skip rules under invalid sections | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ( $invalid_section_flag ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | carp "also skipping rules $rule under invalid section $section ..."; | |||||||||||||||||||||||||||
| 130 | 0 | next; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | my $cur_section = $section || 'GLOBAL'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$section || 'GLOBAL'
MC/DC0%0/1
$section || 'GLOBAL'
$section
Truth table0%0/2
A
A= $section
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | my ( $certain, $func, @args ) | |||||||||||||||||||||||||||
| 136 | = split(' ', $rule); | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # valid level of certainty: 0 - 100 | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if ( $certain =~ /^(\d+)\%?$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | $certain = $1; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ( $certain > 100 or $certain < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$certain > 100 or $certain < 0
MC/DC0%0/2
$certain > 100 or $certain < 0
$certain > 100
$certain < 0
Truth table0%0/3
A or B
A= $certain > 100B= $certain < 0
| |||||||||||||||||||||||||||||
| 143 | 0 | carp "certainty $certain is not in the range of 0 to 100, skipping rules $rule ..."; | |||||||||||||||||||||||||||
| 144 | 0 | next; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | else { | ||||||||||||||||||||||||||||
| 148 | 0 | carp "invalid level of certainty $certain, skipping rules $rule ..."; | |||||||||||||||||||||||||||
| 149 | 0 | next; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | my $hash = { | |||||||||||||||||||||||||||
| 153 | 'cert' => $certain, | ||||||||||||||||||||||||||||
| 154 | 'func' => $func, | ||||||||||||||||||||||||||||
| 155 | 'args' => \@args | ||||||||||||||||||||||||||||
| 156 | }; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | push @{$ruleset->{$cur_section}}, $hash; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | close( CONFIG ); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # sort rulesets by certainty level | ||||||||||||||||||||||||||||
| 166 | 0 | sort_ruleset(); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | return SUCCESS; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | sub get_ruleset { | ||||||||||||||||||||||||||
|
Subroutine
get_ruleset: not called CC=1 SCAR=0.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 172 | 0 | my $var = $_[0] . "::ruleset"; | |||||||||||||||||||||||||||
| 173 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 174 | 0 | return $$var; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub sort_ruleset { | ||||||||||||||||||||||||||
|
Subroutine
sort_ruleset: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # sort every sections of ruleset according to | ||||||||||||||||||||||||||||
| 180 | # its level of certainty. | ||||||||||||||||||||||||||||
| 181 | 0 | foreach my $sec ( @valid_sections ) { | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # don't sort global setting | ||||||||||||||||||||||||||||
| 184 | next | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ( $sec eq 'GLOBAL' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | if ( exists $ruleset->{$sec} and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $File::OSS::Scan::Ruleset::ruleset->{$sec} and defined $File::OSS::Scan::Ruleset::ruleset->{$sec}
MC/DC0%0/2
exists $File::OSS::Scan::Ruleset::ruleset->{$sec} and defined $File::OSS::Scan::Ruleset::ruleset->{$sec}
exists $File::OSS::Scan::Ruleset::ruleset->{$sec}
defined $File::OSS::Scan::Ruleset::ruleset->{$sec}
Truth table0%0/3
A and B
A= exists $File::OSS::Scan::Ruleset::ruleset->{$sec}B= defined $File::OSS::Scan::Ruleset::ruleset->{$sec}
| |||||||||||||||||||||||||||||
| 188 | defined $ruleset->{$sec} ) { | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | my @sorted = (); | |||||||||||||||||||||||||||
| 191 | 0 | my %raw_hash = (); | |||||||||||||||||||||||||||
| 192 | 0 | my $rules = $ruleset->{$sec}; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | foreach my $rule ( @$rules ) { | |||||||||||||||||||||||||||
| 195 | 0 | my $cert = $rule->{'cert'}; | |||||||||||||||||||||||||||
| 196 | 0 | push @{$raw_hash{$cert}}, $rule; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | foreach my $s_cert ( sort { $b <=> $a } keys %raw_hash ) { | |||||||||||||||||||||||||||
| 200 | 0 | push @sorted, @{$raw_hash{$s_cert}}; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 0 | $ruleset->{$sec} = \@sorted; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | return SUCCESS; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 1 | 1; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | __DATA__ | ||||||||||||||||||||||||||||