lib/FP/Iptables/Rules.pm

filter: Statementstmt 98.8% 168 / 170 Branchbran 62.5% 5 / 8 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 96.4% 54 / 56 Podpod n/a totaltotal 93.5% 230 / 246 CC 8 SCAR 3.5
CC
8
Cov
95%
is_port
4.1
xml
3.7
__ANON__
2.1
CRAP 1.4
« lib/FP/Combinators2.pm ↑ Summary lib/FP/Docstring/t.pm »
1 #
2 # Copyright (c) 2022 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9 #
10  
11 =head1 NAME
12  
13 FP::Iptables::Rules
14  
15 =head1 SYNOPSIS
16  
17 =head1 DESCRIPTION
18  
19 Very much unfinished, just a subset of iptables functionality is
20 implemented yet.
21  
22 =head1 SEE ALSO
23  
24 =head1 NOTE
25  
26 This is alpha software! Read the status section in the package README
27 or on the L<website|http://functional-perl.org/>.
28  
29 =cut
30  
31  
32 package FP::Iptables::Rules;
33 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # Excluding: Base ACondition AnAction
44 1 our @EXPORT=qw(
45 Rule Protocol Match Multiport Return Call Actions Rules
46 Rule_ Protocol_ Match_ Multiport_ Return_ Call_ Actions_ Rules_
47 );
48 1 our @EXPORT_OK=qw();
49 1 our %EXPORT_TAGS=(default => \@EXPORT, all=>[@EXPORT,@EXPORT_OK]);
50  
51  
52 1 package FP::Iptables::Rules::Base {
53 1 ▶ use FP::Struct []
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 => qw(FP::Struct::Equal FP::Struct::Show);
55 1 _END_ "FP::Iptables::Rules";
56 }
57  
58 # ---- rules -----------------------------------------------------------
59  
60 2 ▶my $perhaps_xml= sub($maybe_v) {
Subroutine __ANON__: called CC=2 SCAR=7.6
61 2 ▶ defined ($maybe_v) ? $maybe_v->xml : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $maybe_v ? : 2 0 2
62 1 };
63  
64 1 package FP::Iptables::Rules::Rule {
65 1 ▶ use FP::Ops qw(the_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶ use FP::List qw(list_of nonempty_list_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶ use FP::Predicates qw(instance_of maybe);
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶ use PXML::Tags qw(rule conditions actions);
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 1 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 [ list_of(instance_of("FP::Iptables::Rules::ACondition")),
72 "conditions" ],
73 [ maybe(instance_of("FP::Iptables::Rules::AnAction")),
74 # The XML element is called "actions" just because it could
75 # be 0 or 1, right?
76 "maybe_action" ],
77 ]
78 1 => qw(FP::Iptables::Rules::Base);
79  
80 2 ▶ sub xml($self) {
Subroutine xml: called CC=1 SCAR=0.0
81 2 RULE(
82 CONDITIONS(
83 $self->conditions->map(the_method("xml"))
84 ),
85 ACTIONS(
86 $perhaps_xml->($self->maybe_action)
87 ),
88 )
89 }
90 1 _END_ "FP::Iptables::Rules";
91 }
92  
93 1 package FP::Iptables::Rules::ACondition {
94 1 ▶ use FP::Struct []
Subroutine BEGIN: called CC=1 SCAR=0.0
95 1 => qw(FP::Iptables::Rules::Base);
96 1 _END_
97 }
98  
99 1 package FP::Iptables::Rules::Protocol {
100 1 ▶ use PXML::Tags qw(p);
Subroutine BEGIN: called CC=1 SCAR=0.0
101 2 ▶ sub is_protocol_string($v) {
Subroutine is_protocol_string: called CC=2 SCAR=7.6
102 2 ▶ $v eq "tcp" or $v eq "udp" # XX and?..
Branch: 1 branch
BranchTrueFalseTotal
$v eq "tcp" or $v eq "udp" 2 0 2
103 }
104 1 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
105 1 [ \&is_protocol_string, "protocol_string" ]
106 ]
107 1 => qw(FP::Iptables::Rules::Base);
108 2 ▶ sub xml($self) {
Subroutine xml: called CC=1 SCAR=0.0
109 2 P($self->protocol_string)
110 }
111 1 _END_ "FP::Iptables::Rules";
112 }
113  
114 1 package FP::Iptables::Rules::Match {
115 1 ▶ use FP::Predicates qw(instance_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
116 1 ▶ use PXML::Tags qw(match);
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 1 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
119 1 [ instance_of("FP::Iptables::Rules::Protocol"),
120 "specifier" ] # ?
121 ]
122 1 => qw(FP::Iptables::Rules::ACondition);
123 2 ▶ sub xml ($self) {
Subroutine xml: called CC=1 SCAR=0.0
124 2 MATCH($self->specifier->xml)
125 }
126 1 _END_ "FP::Iptables::Rules";
127 }
128  
129 1 package FP::Iptables::Rules::Multiport {
130 1 ▶ use FP::List qw(list_of nonempty_list_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
131 1 ▶ use FP::Predicates qw(is_natural0 is_natural maybe);
Subroutine BEGIN: called CC=1 SCAR=0.0
132 1 ▶ use PXML::Tags qw(multiport sports dports);
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 2 ▶ sub is_port($v) {
Subroutine is_port: called CC=3 SCAR=14.2
135 # XX would port 0 be valid?
136 2 ▶ not ref($v) and is_natural($v) and $v <= 65535
Branch: 1 branch
BranchTrueFalseTotal
not ref $v and is_natural($v) and $v <= 65535 2 0 2
Condition33%1/3
not ref $v and is_natural($v)
!ll&&!rl&&r
002
MC/DC0%0/2
not ref $v and is_natural($v)
not ref $v is_natural($v)
Truth table33%1/3
not(A) and B
A= not ref $vB= is_natural($v)
ABresult
0X0
100
111
137 }
138  
139 4 ▶ sub perhaps_tag($tagger, $maybe_v) {
Subroutine perhaps_tag: called CC=2 SCAR=6.9
140 4 ▶ defined($maybe_v) ? $tagger->($maybe_v) : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $maybe_v ? : 2 2 4
141 }
142  
143 1 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
144 1 [ maybe(nonempty_list_of(\&is_port)), "maybe_sports" ],
145 [ maybe(nonempty_list_of(\&is_port)), "maybe_dports" ],
146 ]
147 1 => qw(FP::Iptables::Rules::ACondition);
148 2 ▶ sub xml($self) {
Subroutine xml: called CC=3 SCAR=13.1
149 # XX bad, declare for check at creation time, please....!
150 2 ▶ $self->maybe_sports // $self->maybe_dports
Condition40%2/5
($self->maybe_sports // $self->maybe_dports) // die("Multiport requires at least one of sports or dports")
l!l
20
$self->maybe_sports // $self->maybe_dports
l!l&&r!l&&!r
020
MC/DC0%0/2
($self->maybe_sports // $self->maybe_dports) // die("Multiport requires at least one of sports or dports")
$self->maybe_sports $self->maybe_dports
Truth table33%1/3
A // B
A= $self->maybe_sportsB= $self->maybe_dports
ABresult
1X1
011
000
151 // die "Multiport requires at least one of sports or dports";
152 2 MULTIPORT(
153 perhaps_tag(\&SPORTS, $self->maybe_sports),
154 perhaps_tag(\&DPORTS, $self->maybe_dports)
155 )
156 }
157 1 _END_ "FP::Iptables::Rules";
158 }
159  
160  
161 # package FP::Iptables::Rules:: {
162 # use PXML::Tags qw();
163 # use FP::Struct []
164 # => qw(FP::Iptables::Rules::Base);
165 # _END_ "FP::Iptables::Rules";
166 # }
167  
168 # package FP::Iptables::Rules:: {
169 # use PXML::Tags qw();
170 # use FP::Struct []
171 # => qw(FP::Iptables::Rules::Base);
172 # _END_ "FP::Iptables::Rules";
173 # }
174  
175  
176 # ----
177  
178 1 package FP::Iptables::Rules::AnAction {
179 # and I don't mean Rust "Some", but "Interface"--ok use "An"
180 1 ▶ use FP::Struct []
Subroutine BEGIN: called CC=1 SCAR=0.0
181 1 => qw(FP::Iptables::Rules::Base);
182 1 _END_
183 }
184  
185 1 package FP::Iptables::Rules::Return {
186 1 ▶ use FP::Ops qw(the_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
187 1 ▶ use PXML::Tags qw(RETURN);
Subroutine BEGIN: called CC=1 SCAR=0.0
188 1 ▶ use FP::Struct []
Subroutine BEGIN: called CC=1 SCAR=0.0
189 1 => qw(FP::Iptables::Rules::AnAction);
190 1 ▶ sub xml($self) {
Subroutine xml: called CC=1 SCAR=0.0
191 1 RETURN()
192 }
193 1 _END_ "FP::Iptables::Rules";
194 }
195  
196 1 package FP::Iptables::Rules::Call {
197 1 ▶ use FP::Ops qw(the_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
198 1 ▶ use FP::Predicates qw(is_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
199 1 ▶ use PXML::Tags qw(call);
Subroutine BEGIN: called CC=1 SCAR=0.0
200  
201 1 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
202 1 [ \&is_string, "chain_name" ]
203 ]
204 1 => qw(FP::Iptables::Rules::AnAction);
205 1 ▶ sub xml($self) {
Subroutine xml: called CC=1 SCAR=0.0
206 1 CALL(PXML::Element->new($self->chain_name, {}, []))
207 }
208 1 _END_ "FP::Iptables::Rules";
209 }
210  
211 1 package FP::Iptables::Rules::Actions {
212 1 ▶ use FP::Ops qw(the_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
213 1 ▶ use FP::List qw(list_of nonempty_list_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
214 1 ▶ use FP::Predicates qw(instance_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
215 1 ▶ use PXML::Tags qw(actions);
Subroutine BEGIN: called CC=1 SCAR=0.0
216  
217 1 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
218 1 [ list_of(instance_of("FP::Iptables::Rules::AnAction")),
219 "items" ],
220 ]
221 1 => qw(FP::Iptables::Rules::Base);
222 0 ▶ sub xml($self) {
Subroutine xml: not called CC=1 SCAR=6.9
223 0 ACTIONS($self->actions->map(the_method("xml")))
224 }
225 1 _END_ "FP::Iptables::Rules";
226 }
227  
228  
229 # Chain
230  
231 # Table
232  
233  
234 # ---- main -----------------------------------------------------------
235  
236 1 package FP::Iptables::Rules::Rules {
237 1 ▶ use FP::Ops qw(the_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
238 1 ▶ use FP::List qw(list_of nonempty_list_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
239 1 ▶ use FP::Predicates qw(instance_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
240 1 ▶ use PXML::Tags qw(iptables-rules);
Subroutine BEGIN: called CC=1 SCAR=0.0
241  
242 1 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
243 1 [ list_of(instance_of("FP::Iptables::Rules::Table")),
244 "tables" ]
245 ]
246 1 => qw(FP::Iptables::Rules::Base);
247 0 ▶ sub xml ($self) {
Subroutine xml: not called CC=1 SCAR=6.9
248 0 IPTABLES_RULES($self->tables->map(the_method("xml")))
249 }
250 1 _END_ "FP::Iptables::Rules";
251 }
252  
253  
254  
255 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
256 1 Rule(
257 list(Match(Protocol("tcp")),
258 Multiport(undef, list(22))),
259 Call("f2b-sshd"))
260 ->xml->string
261 }
262 1 '<rule><conditions><match><p>tcp</p></match><multiport><dports>22</dports></multiport></conditions><actions><call><f2b-sshd/></call></actions></rule>';
263  
264 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
265 1 Rule(
266 list(Match(Protocol("tcp")),
267 Multiport(undef, list(22))),
268 Return())
269 ->xml->string
270 }
271 1 '<rule><conditions><match><p>tcp</p></match><multiport><dports>22</dports></multiport></conditions><actions><RETURN/></actions></rule>';
272  
273  
274 1 1
« lib/FP/Combinators2.pm ↑ Summary lib/FP/Docstring/t.pm »