Configd/Language/ossec.pm

filter: Statementstmt 21.0% 27 / 128 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 28.5% 6 / 21 Podpod 100% 15 / 15 totaltotal 17.7% 48 / 271 CC 50 SCAR 39.7
CC
50
Cov
12%
emit
90.0
hyphens
90.0
parse
72.0
CRAP 53.0
« Configd/Language.pm ↑ Summary Configd/Language/postfix.pm »
1 package Configd::Language::ossec;
2 1 $Configd::Language::ossec::VERSION = '0.007';
3 #ABSTRACT: ossec.conf, which is one XML file that every daemon of OSSEC reads, with no directory beside it.
4  
5 1 ▶use 5.034;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use XML::LibXML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use parent qw{Configd::Language};
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 0 ▶sub files {
Subroutine files: not called CC=1 SCAR=6.9
Pod documented
18 0 return ( { path => '/var/ossec/etc/ossec.conf', mode => 0o660, owner => 'root:ossec' } );
19 }
20  
21 0 ▶sub units {
Subroutine units: not called CC=1 SCAR=6.9
Pod documented
22 0 return ('ossec.service');
23 }
24  
25 0 ▶sub reloads {
Subroutine reloads: not called CC=1 SCAR=6.9
Pod documented
26 0 return 0;
27 }
28  
29 # The sections that appear once, each with the children that are lists. Every
30 # other section is a list of blocks.
31 my %SINGLE = (
32 2 global => { map { $_ => 1 } qw{white_list email_to} },
33 3 rules => { map { $_ => 1 } qw{include list decoder} },
34 5 syscheck => { map { $_ => 1 } qw{directories ignore nodiff registry_ignore windows_registry} },
35 1 rootcheck => { map { $_ => 1 } qw{system_audit windows_audit windows_apps windows_malware} },
36 alerts => {},
37 client => {},
38 );
39  
40  
41 0 ▶sub accumulates {
Subroutine accumulates: not called CC=4 SCAR=30.0
Pod documented
42 0 my ( $self, $key ) = @_;
43  
44 0 my ( $section, $child ) = sections($key);
45 0 ▶ return $SINGLE{$section} ? 0 : 1 unless defined $child;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $child 0 0 0
$SINGLE{$section} ? : 0 0 0
46 0 ▶ return $SINGLE{$section}{$child} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$SINGLE{$section}{$child} ? : 0 0 0
47 }
48  
49  
50 0 ▶sub separator {
Subroutine separator: not called CC=1 SCAR=6.9
Pod documented
51 0 return "\n";
52 }
53  
54  
55 0 ▶sub items {
Subroutine items: not called CC=2 SCAR=17.9
Pod documented
56 0 my ( $self, $key, $value ) = @_;
57 0 ▶ return () unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
58 0 return map { $_->{text} } elements($value);
59 }
60  
61  
62 0 ▶sub parse {
Subroutine parse: not called CC=8 SCAR=42.8
Pod documented
63 0 my ( $self, $text ) = @_;
64  
65 0 my @directives;
66 0 foreach my $root ( elements($text) ) {
67 0 ▶ die "<$root->{name}> is not <ossec_config>, which is the root of ossec.conf.\n" unless $root->{name} eq 'ossec_config';
Branch: 1 branch
BranchTrueFalseTotal
unless $root->{"name"} eq "ossec_config" 0 0 0
68 0 foreach my $section ( children( $root->{node} ) ) {
69 0 ▶ if ( !$SINGLE{ $section->{name} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($SINGLE{$section->{"name"}}) 0 0 0
70 0 ▶ push @directives, { key => $section->{name}, value => $section->{empty} ? q{} : $section->{text} };
Branch: 1 branch
BranchTrueFalseTotal
$section->{'empty'} ? : 0 0 0
71 0 next;
72 }
73 0 foreach my $child ( children( $section->{node} ) ) {
74 0 ▶ push @directives, { key => "$section->{name}/$child->{name}", value => $child->{empty} ? q{} : $child->{text} };
Branch: 1 branch
BranchTrueFalseTotal
$child->{'empty'} ? : 0 0 0
75 }
76 }
77 }
78 0 return \@directives;
79 }
80  
81  
82 0 ▶sub emit {
Subroutine emit: not called CC=9 SCAR=45.0
Pod documented
83 0 my ( $self, $directives ) = @_;
84  
85 0 my ( @order, %items );
86 0 foreach my $directive (@$directives) {
87 0 my ($section) = sections( $directive->{key} );
88 0 ▶ push @order, $section unless $items{$section};
Branch: 1 branch
BranchTrueFalseTotal
unless $items{$section} 0 0 0
89 0 ▶ push @{ $items{$section} }, $self->accumulates( $directive->{key} ) ? $self->items( $directive->{key}, $directive->{value} ) : grep { length } $directive->{value};
Branch: 1 branch
BranchTrueFalseTotal
$self->accumulates($directive->{'key'}) ? : 0 0 0
90 }
91  
92 0 my $doc = XML::LibXML::Document->new( '1.0', 'UTF-8' );
93 0 my $root = $doc->createElement('ossec_config');
94 0 $doc->setDocumentElement($root);
95 0 foreach my $section (@order) {
96 0 my @items = @{ $items{$section} };
97 0 ▶ next unless @items;
Branch: 1 branch
BranchTrueFalseTotal
unless @items 0 0 0
98 0 my $parent = $root;
99 0 ▶ if ( $SINGLE{$section} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SINGLE{$section}) 0 0 0
100 0 $parent = $doc->createElement($section);
101 0 $root->appendChild($parent);
102 }
103 0 foreach my $item (@items) {
104 0 $parent->appendChild( $doc->importNode($_) ) for map { $_->{node} } elements($item);
105 }
106 }
107 0 return $root->toString(1) . "\n";
108 }
109  
110  
111 0 ▶sub header {
Subroutine header: not called CC=1 SCAR=6.9
Pod documented
112 0 my ( $self, $file ) = @_;
113  
114 # An XML comment cannot hold two hyphens in a row.
115 0 my $text = $self->SUPER::header($file) =~ s/^#[ ]?//gmr =~ s/-{2,}/-/gr;
116 0 return "<!--\n$text-->\n";
117 }
118  
119  
120 0 ▶sub sections {
Subroutine sections: not called CC=2 SCAR=17.9
Pod documented
121 0 my ($key) = @_;
122  
123 0 my $slash = index( $key, q{/} );
124 0 ▶ return ($key) if $slash < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $slash < 0 0 0 0
125 0 return ( substr( $key, 0, $slash ), substr( $key, $slash + 1 ) );
126 }
127  
128  
129 # No network, no entities and no DTD: a fragment is configuration, not a
130 # document that may fetch others.
131 1 my $PARSER = XML::LibXML->new( no_network => 1, expand_entities => 0, load_ext_dtd => 0 );
132  
133 # The code of libxml2 for two hyphens inside a comment, and how many of them one
134 # file may have.
135 1 my $HYPHENS_IN_COMMENT = 80;
136 1 my $MOST_HYPHENS = 1000;
137  
138 0 ▶sub elements {
Subroutine elements: not called CC=3 SCAR=24.8
Pod documented
139 0 my ($xml) = @_;
140 0 ▶ return () unless defined $xml && $xml =~ m/\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xml and $xml =~ /\S/aa 0 0 0
Condition0%0/3
defined $xml and $xml =~ /\S/aa
!ll&&!rl&&r
000
MC/DC0%0/2
defined $xml and $xml =~ /\S/aa
defined $xml $xml =~ /\S/aa
Truth table0%0/3
A and B
A= defined $xmlB= $xml =~ /\S/aa
ABresult
0X0
100
111
141 0 return children( document($xml)->documentElement );
142 }
143  
144  
145 0 ▶sub document {
Subroutine document: not called CC=8 SCAR=42.8
Pod documented
146 0 my ($xml) = @_;
147  
148 # One line before the text, so that a line of libxml2 is one past a line of
149 # the text, and a column is a column.
150 0 my @lines = split( "\n", $xml =~ s/\A\s*<\?xml[^>]*\?>//r, -1 );
151 0 foreach ( 0 .. $MOST_HYPHENS ) {
152 0 my $doc = eval { $PARSER->load_xml( string => join( "\n", '<configd>', @lines, '</configd>' ) ) };
153 0 ▶ return $doc if $doc;
Branch: 1 branch
BranchTrueFalseTotal
if $doc 0 0 0
154  
155 0 my @errors;
156 0 ▶ for ( my $error = $@; ref $error; $error = $error->_prev ) { unshift @errors, $error }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $error) 0 0 0
157 0 ▶ die 'Cannot read the XML: ' . ( ref $@ ? $@->message : $@ ) =~ s/\s+\z//r . "\n" if !@errors || grep { $_->code != $HYPHENS_IN_COMMENT } @errors;
Branch: 2 branches
BranchTrueFalseTotal
if not @errors or grep {$_->code != $HYPHENS_IN_COMMENT;} @errors 0 0 0
ref $@ ? : 0 0 0
Condition0%0/3
not @errors or grep {$_->code != $HYPHENS_IN_COMMENT;} @errors
l!l&&r!l&&!r
000
MC/DC0%0/2
not @errors or grep {$_->code != $HYPHENS_IN_COMMENT;} @errors
not @errors grep {$_->code != $HYPHENS_IN_COMMENT;} @errors
Truth table0%0/3
not(A) or B
A= not @errorsB= grep {$_->code != $HYPHENS_IN_COMMENT;} @errors
ABresult
1X1
011
000
158  
159 0 my ($first) = @errors;
160 0 my $hyphens = hyphens( \@lines, $first->line - 2, $first->column );
161 0 ▶ die 'Cannot read the XML: ' . $first->message =~ s/\s+\z//r . "\n" unless defined $hyphens;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hyphens 0 0 0
162 0 substr( $lines[ $first->line - 2 ], $hyphens, 2, '- -' );
163 }
164 0 die "Cannot read the XML: more than $MOST_HYPHENS comments with two hyphens in them\n";
165 }
166  
167  
168 0 ▶sub hyphens {
Subroutine hyphens: not called CC=9 SCAR=45.0
Pod documented
169 0 my ( $lines, $line, $column ) = @_;
170 0 ▶ return if $line < 0 || $line > $#$lines;
Branch: 1 branch
BranchTrueFalseTotal
if $line < 0 or $line > $lines->$#* 0 0 0
Condition0%0/3
$line < 0 or $line > $lines->$#*
l!l&&r!l&&!r
000
MC/DC0%0/2
$line < 0 or $line > $lines->$#*
$line < 0 $line > $lines->$#*
Truth table0%0/3
A or B
A= $line < 0B= $line > $lines->$#*
ABresult
1X1
011
000
171  
172 0 my $text = $lines->[$line];
173 0 ▶ my $at = index( $text, '--', ( $column // 1 ) > 2 ? $column - 2 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
($column // 1) > 2 ? : 0 0 0
Condition0%0/2
$column // 1
l!l
00
MC/DC0%0/1
$column // 1
$column
Truth table0%0/2
A
A= $column
Aresult
11
00
174 0 ▶ while ( $at >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at >= 0) 0 0 0
175 0 ▶ my $opens = $at >= 2 && substr( $text, $at - 2, 2 ) eq '<!';
Condition0%0/3
$at >= 2 && substr($text, $at - 2, 2) eq '<!'
!ll&&!rl&&r
000
MC/DC0%0/2
$at >= 2 && substr($text, $at - 2, 2) eq '<!'
$at >= 2 substr($text, $at - 2, 2) eq '<!'
Truth table0%0/3
A && B
A= $at >= 2B= substr($text, $at - 2, 2) eq '<!'
ABresult
0X0
100
111
176 0 my $closes = substr( $text, $at + 2, 1 ) eq '>';
177 0 ▶ return $at unless $opens || $closes;
Branch: 1 branch
BranchTrueFalseTotal
unless $opens or $closes 0 0 0
Condition0%0/3
$opens or $closes
l!l&&r!l&&!r
000
MC/DC0%0/2
$opens or $closes
$opens $closes
Truth table0%0/3
A or B
A= $opensB= $closes
ABresult
1X1
011
000
178 0 $at = index( $text, '--', $at + 1 );
179 }
180 0 return;
181 }
182  
183  
184 0 ▶sub children {
Subroutine children: not called CC=7 SCAR=40.3
Pod documented
185 0 my ($node) = @_;
186  
187 0 my @elements;
188 0 foreach my $child ( $node->childNodes ) {
189 0 my $type = $child->nodeType;
190 0 ▶ if ( $type == XML::LibXML::XML_TEXT_NODE() || $type == XML::LibXML::XML_CDATA_SECTION_NODE() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 3 or $type == 4) 0 0 0
Condition0%0/3
$type == 3 or $type == 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$type == 3 or $type == 4
$type == 3 $type == 4
Truth table0%0/3
A or B
A= $type == 3B= $type == 4
ABresult
1X1
011
000
191 0 ▶ die "Text outside of an element: '" . substr( $child->data =~ s/\A\s+//r, 0, 40 ) . "'\n" if $child->data =~ m/\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $child->data =~ /\S/aa 0 0 0
192 0 next;
193 }
194 0 ▶ next unless $type == XML::LibXML::XML_ELEMENT_NODE();
Branch: 1 branch
BranchTrueFalseTotal
unless $type == 1 0 0 0
195  
196 0 my $element = tidy( $child->cloneNode(1) );
197 0 ▶ push @elements, {
Condition0%0/3
$element->hasChildNodes || $element->hasAttributes
l!l&&r!l&&!r
000
MC/DC0%0/2
$element->hasChildNodes || $element->hasAttributes
$element->hasChildNodes $element->hasAttributes
Truth table0%0/3
A || B
A= $element->hasChildNodesB= $element->hasAttributes
ABresult
1X1
011
000
198 name => $element->nodeName,
199 node => $element,
200 text => $element->toString(0),
201 empty => !$element->hasChildNodes && !$element->hasAttributes,
202 };
203 }
204 0 return @elements;
205 }
206  
207  
208 0 ▶sub tidy {
Subroutine tidy: not called CC=7 SCAR=40.3
Pod documented
209 0 my ($node) = @_;
210  
211 0 foreach my $child ( $node->childNodes ) {
212 0 my $type = $child->nodeType;
213 0 ▶ if ( $type == XML::LibXML::XML_COMMENT_NODE() || $type == XML::LibXML::XML_PI_NODE() || ( $type == XML::LibXML::XML_TEXT_NODE() && $child->data !~ m/\S/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 8 or $type == 7 or $type == 3 and not $child->data =~ /\S/aa) 0 0 0
Condition0%0/9
$type == 8 or $type == 7 or $type == 3 and not $child->data =~ /\S/aa
l!l&&r!l&&!r
000
$type == 8 or $type == 7
l!l&&r!l&&!r
000
$type == 3 and not $child->data =~ /\S/aa
!ll&&!rl&&r
000
MC/DC0%0/4
$type == 8 or $type == 7 or $type == 3 and not $child->data =~ /\S/aa
$type == 8 $type == 7 $type == 3 not $child->data =~ /\S/aa
Truth table0%0/5
A or B or C and not(D)
A= $type == 8B= $type == 7C= $type == 3D= not $child->data =~ /\S/aa
ABCDresult
1XXX1
01XX1
00111
000X0
00100
214 0 $node->removeChild($child);
215 0 next;
216 }
217 0 ▶ tidy($child) if $type == XML::LibXML::XML_ELEMENT_NODE();
Branch: 1 branch
BranchTrueFalseTotal
if $type == 1 0 0 0
218 }
219 0 return $node;
220 }
221  
222  
223 1 1;
224  
225 __END__
« Configd/Language.pm ↑ Summary Configd/Language/postfix.pm »