src_bin/kur

filter: Statementstmt 76.4% 52 / 68 Branchbran 50.0% 22 / 44 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 60.7% 82 / 135 CC 1 SCAR 0.0
CC
1
Cov
61%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Ereshkigal/App/Command/cidr_unban.pm ↑ Summary blib/lib/Ereshkigal/App.pm »
1 #!perl
2  
3 =head1 NAME
4  
5 kur - Firewall ban manager worker for Ereshkigal.
6  
7 =head1 SYNOPSIS
8  
9 kur B<--name>=<name> B<--backend>=<backend> [B<--ports>=<ports>]
10 [B<--protocols>=<protocols>] [B<--prefix>=<prefix>] [B<--option> <key>=<value>]
11 [B<--interfaces>=<interfaces>] [B<--self-heal> <0/1>]
12 [B<--ban-time>=<seconds>] [B<--checkpoint>=<seconds>]
13 [B<--enable-cidr> <0/1>] [B<--cidr-silent-drop> <0/1>]
14 [B<--cache>=<cache dir>] [B<--run>=<run dir>] [B<-f>|B<--foreground>]
15  
16 kur -v|--version
17  
18 kur -h|--help
19  
20 =head1 DESCRIPTION
21  
22 Runs a single L<Net::Firewall::BlockerHelper> instance via L<Ereshkigal::Kur>,
23 serving it up on a unix socket speaking the newline delimited JSON protocol of
24 L<POE::Component::Server::JSONUnix>.
25  
26 The socket and PID file go under C<$run_dir/kur/>, named for the instance...
27 C<< <name>.sock >> and C<< <name>.pid >>.
28  
29 Normally spawned and supervised by C<ereshkigal>, but usable standalone.
30  
31 =head1 FLAGS
32  
33 =head2 -v|--version
34  
35 Show version info.
36  
37 =head2 -h|--help
38  
39 Show help info.
40  
41 =head2 --name=<name>
42  
43 The name to use for this instance. Must match /^[a-zA-Z0-9\-]+$/.
44  
45 This must be specified.
46  
47 =head2 --backend=<backend>
48  
49 The Net::Firewall::BlockerHelper backend to use.
50  
51 This must be specified.
52  
53 =head2 --ports=<ports>
54  
55 A comma separated list of ports to block. Each must be a positive integer
56 or a name resolvable via getservbyname.
57  
58 If not specified, all are blocked.
59  
60 =head2 --protocols=<protocols>
61  
62 A comma separated list of protocols to block. Each is checked against
63 /etc/protocols via the function getprotobyname.
64  
65 If not specified, all are blocked.
66  
67 =head2 --prefix=<prefix>
68  
69 The prefix to use. Must match /^[a-zA-Z0-9]+$/.
70  
71 Default is kur.
72  
73 =head2 --option <key>=<value>
74  
75 A backend specific option. May be specified multiple times.
76  
77 =head2 --interfaces=<interfaces>
78  
79 A comma separated list of interfaces, handed to the backend as the
80 C<interfaces> option as an array. Wanted by backends taking an array of
81 interfaces there, such as xdp.
82  
83 =head2 --self-heal <0/1>
84  
85 If the firewall setup should be verified and re-inited if needed before each
86 ban or unban.
87  
88 Default is 1.
89  
90 =head2 --ban-time <seconds>
91  
92 How long bans should last in seconds. 0 means bans never time out. May be
93 overridden per ban request.
94  
95 Default is 600.
96  
97 =head2 --checkpoint <seconds>
98  
99 Seconds between periodic rewrites of the ban state CSV under the cache dir.
100 0 disables the periodic rewrite... ban/unban, stop, and on demand
101 checkpoints still happen.
102  
103 Default is 60.
104  
105 =head2 --enable-cidr <0/1>
106  
107 Whether CIDR banning is enabled. Even when set, CIDR commands only work if
108 the backend supports CIDR bans.
109  
110 Default is 0.
111  
112 =head2 --cidr-silent-drop <0/1>
113  
114 When set, CIDR commands are silently dropped rather than erroring when CIDR
115 banning is not available for this instance.
116  
117 Default is 0.
118  
119 =head2 --run=<run dir>
120  
121 The base dir for run files.
122  
123 Default is /var/run/ereshkigal
124  
125 =head2 --cache=<cache dir>
126  
127 The base dir for cache files.
128  
129 Default is /var/cache/ereshkigal
130  
131 =head2 -f|--foreground
132  
133 Do not daemonize. This is used by ereshkigal when spawning kur instances so
134 they can be supervised.
135  
136 =cut
137  
138 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
139 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 8 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 8 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
142 8 ▶use Net::Server::Daemonize qw(daemonize);
Subroutine BEGIN: called CC=1 SCAR=0.0
143 8 ▶use Ereshkigal::Kur;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 # Prints the version and exits. Named and placed in main:: because that is
146 # where Getopt::Long looks for it, so a --version it handles itself finds it,
147 # but the option handling below also calls it outright rather than relying on
148 # that, which is why both spellings of the flag work the same.
149 #
150 # Takes no args. Getopt::Long passes a getopt object when it is the caller,
151 # and it is deliberately ignored... there is nothing here worth varying on
152 # it.
153 #
154 # Returns nothing, ever, as it exits 255 rather than returning. 255 is used
155 # throughout this bin for "asked and answered, did not run", the same code
156 # pod2usage is given below.
157 #
158 # main::VERSION_MESSAGE; # prints "kur v. 0.0.1" and exits 255
159 1 ▶sub main::VERSION_MESSAGE {
Subroutine VERSION_MESSAGE: called CC=1 SCAR=0.0
160 1 print 'kur v. ' . $Ereshkigal::Kur::VERSION . "\n";
161 1 exit 255;
162 }
163  
164 # Prints the full help, that being this bin's own POD rendered by pod2usage,
165 # and exits. In main:: for the same reason VERSION_MESSAGE is... Getopt::Long
166 # looks there, and the option handling below calls it outright as well.
167 #
168 # Takes no args, ignoring anything Getopt::Long hands it.
169 #
170 # Returns nothing, ever. pod2usage is given -exitval 255 so it exits rather
171 # than returning, and -verbose 2 so the whole POD is shown rather than just
172 # the usage line. It goes to STDOUT rather than STDERR, help having been
173 # asked for rather than provoked by a mistake.
174 #
175 # main::HELP_MESSAGE; # prints the POD to stdout and exits 255
176 1 ▶sub main::HELP_MESSAGE {
Subroutine HELP_MESSAGE: called CC=1 SCAR=0.0
177 1 pod2usage( -exitval => 255, -verbose => 2, -output => \*STDOUT, );
178 }
179  
180 8 my $name;
181 my $backend;
182 8 my $prefix;
183 8 my $ports;
184 8 my $protocols;
185 8 my @options;
186 8 my $interfaces;
187 8 my $self_heal;
188 8 my $ban_time;
189 8 my $checkpoint;
190 8 my $enable_cidr;
191 8 my $cidr_silent_drop;
192 8 my $cache_dir = '/var/cache/ereshkigal';
193 8 my $run_dir = '/var/run/ereshkigal';
194 8 my $foreground;
195 my $version;
196 8 my $help;
197 8 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("version|v", \$version, "help|h", \$help, "name=s", \$name, "backend=s", \$backend, "ports=s", \$ports, "protocols=s", \$protocols, "prefix=s", \$prefix, "option=s", \@options, "interfaces=s", \$interfaces, "self-heal=i", \$self_heal, "ban-time=s", \$ban_time, "checkpoint=s", \$checkpoint, "enable-cidr=s", \$enable_cidr, "cidr-silent-drop=s", \$cidr_silent_drop, "cache=s", \$cache_dir, "run=s", \$run_dir, "foreground|f", \$foreground) or pod2usage("-exitval", 255, "-verbose", 1, "-output", \*STDERR) 8 0 8
198 'version|v' => \$version,
199 'help|h' => \$help,
200 'name=s' => \$name,
201 'backend=s' => \$backend,
202 'ports=s' => \$ports,
203 'protocols=s' => \$protocols,
204 'prefix=s' => \$prefix,
205 'option=s' => \@options,
206 'interfaces=s' => \$interfaces,
207 'self-heal=i' => \$self_heal,
208 'ban-time=s' => \$ban_time,
209 'checkpoint=s' => \$checkpoint,
210 'enable-cidr=s' => \$enable_cidr,
211 'cidr-silent-drop=s' => \$cidr_silent_drop,
212 'cache=s' => \$cache_dir,
213 'run=s' => \$run_dir,
214 'foreground|f' => \$foreground,
215 ) || pod2usage( -exitval => 255, -verbose => 1, -output => \*STDERR, );
216  
217 # print version or help if requested
218 8 ▶if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 1 7 8
219 1 main::HELP_MESSAGE;
220 }
221 7 ▶if ($version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version) 1 6 7
222 1 main::VERSION_MESSAGE;
223 }
224  
225 # both are documented as required, so bounce their absence as a usage error
226 # here rather than as internal errors from the layers below
227 6 ▶if ( !defined($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 1 5 6
228 1 pod2usage( -exitval => 255, -verbose => 1, -output => \*STDERR, -message => 'kur: --name is required' );
229 }
230 5 ▶if ( !defined($backend) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $backend) 1 4 5
231 1 pod2usage( -exitval => 255, -verbose => 1, -output => \*STDERR, -message => 'kur: --backend is required' );
232 }
233  
234 4 my %backend_options;
235 4 foreach my $key_value (@options) {
236 1 my ( $key, $value ) = split( /=/, $key_value, 2 );
237 1 ▶ if ( !defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 0 1
238 1 die( '--option "' . $key_value . '" is not in the form key=value' );
239 }
240 0 $backend_options{$key} = $value;
241 }
242 # interfaces is array valued, so it rides its own flag rather than --option
243 3 ▶if ( defined($interfaces) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $interfaces) 0 3 3
244 0 $backend_options{interfaces} = [ split( /\s*,\s*/, $interfaces ) ];
245 }
246  
247 3 ▶my $kur = Ereshkigal::Kur->new(
Branch: 9 branches
BranchTrueFalseTotal
defined $ports ? : 1 2 3
defined $protocols ? : 1 2 3
defined $prefix ? : 0 3 3
%backend_options ? : 0 3 3
defined $self_heal ? : 0 3 3
defined $ban_time ? : 0 3 3
defined $checkpoint ? : 0 3 3
defined $enable_cidr ? : 0 3 3
defined $cidr_silent_drop ? : 0 3 3
248 'name' => $name,
249 'backend' => $backend,
250 'ports' => defined($ports) ? [ split( /\s*,\s*/, $ports ) ] : [],
251 'protocols' => defined($protocols) ? [ split( /\s*,\s*/, $protocols ) ] : [],
252 defined($prefix) ? ( 'prefix' => $prefix ) : (),
253 %backend_options ? ( 'options' => \%backend_options ) : (),
254 defined($self_heal) ? ( 'self_heal' => $self_heal ) : (),
255 defined($ban_time) ? ( 'ban_time' => $ban_time ) : (),
256 defined($checkpoint) ? ( 'checkpoint' => $checkpoint ) : (),
257 defined($enable_cidr) ? ( 'enable_cidr' => $enable_cidr ) : (),
258 defined($cidr_silent_drop) ? ( 'cidr_silent_drop' => $cidr_silent_drop ) : (),
259 'run_base_dir' => $run_dir,
260 'cache_base_dir' => $cache_dir,
261 );
262  
263 0 ▶if ($foreground) {
Branch: 1 branch
BranchTrueFalseTotal
if ($foreground) { } 0 0 0
264 # supervised by ereshkigal or ran by hand, so the PID file is ours to
265 # write... the daemonize branch gets that plus a liveness check from
266 # daemonize() itself, so do the same check here rather than clobbering
267 # the PID file of a live kur and unlinking it again on the way out
268 0 ▶ if ( -e $kur->pid_path && open( my $old_pid_fh, '<', $kur->pid_path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $kur->pid_path and open my $old_pid_fh, "<", $kur->pid_path) 0 0 0
Condition0%0/3
-e $kur->pid_path and open my $old_pid_fh, "<", $kur->pid_path
!ll&&!rl&&r
000
MC/DC0%0/2
-e $kur->pid_path and open my $old_pid_fh, "<", $kur->pid_path
-e $kur->pid_path open my $old_pid_fh, "<", $kur->pid_path
Truth table0%0/3
A and B
A= -e $kur->pid_pathB= open my $old_pid_fh, "<", $kur->pid_path
ABresult
0X0
100
111
269 0 my $old_pid = <$old_pid_fh>;
270 0 close($old_pid_fh);
271 0 ▶ if ( defined($old_pid) && $old_pid =~ /^\s*([0-9]+)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_pid and $old_pid =~ /^\s*([0-9]+)\s*$/) 0 0 0
Condition0%0/3
defined $old_pid and $old_pid =~ /^\s*([0-9]+)\s*$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $old_pid and $old_pid =~ /^\s*([0-9]+)\s*$/
defined $old_pid $old_pid =~ /^\s*([0-9]+)\s*$/
Truth table0%0/3
A and B
A= defined $old_pidB= $old_pid =~ /^\s*([0-9]+)\s*$/
ABresult
0X0
100
111
272 0 $old_pid = $1;
273 # kill 0 is true while the process exists, and EPERM means it
274 # exists but is not ours to signal
275 0 ▶ if ( kill( 0, $old_pid ) || $!{EPERM} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $old_pid or $!{"EPERM"}) 0 0 0
Condition0%0/3
kill 0, $old_pid or $!{"EPERM"}
l!l&&r!l&&!r
000
MC/DC0%0/2
kill 0, $old_pid or $!{"EPERM"}
kill 0, $old_pid $!{"EPERM"}
Truth table0%0/3
A or B
A= kill 0, $old_pidB= $!{"EPERM"}
ABresult
1X1
011
000
276 0 die( 'A kur named "'
277 . $name
278 . '" already appears to be running with PID '
279 . $old_pid
280 . ' per the PID file "'
281 . $kur->pid_path
282 . '"' );
283 }
284 } ## end if ( defined($old_pid) && $old_pid =~ /^\s*([0-9]+)\s*$/)
285 } ## end if ( -e $kur->pid_path && open( my $old_pid_fh...))
286  
287 0 ▶ open( my $pid_fh, '>', $kur->pid_path ) || die( 'Failed to open the PID file "' . $kur->pid_path . '"... ' . $! );
Branch: 1 branch
BranchTrueFalseTotal
open my $pid_fh, ">", $kur->pid_path or die 'Failed to open the PID file "' . $kur->pid_path . '"... ' . $! 0 0 0
288 0 print $pid_fh $$;
289 0 close($pid_fh);
290 } else {
291 0 daemonize( $>, ( split( /\s+/, $) ) )[0], $kur->pid_path );
292 }
293  
294 0 $kur->start_server;
295  
296 0 ▶unlink( $kur->pid_path ) if -e $kur->pid_path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $kur->pid_path 0 0 0
« blib/lib/Ereshkigal/App/Command/cidr_unban.pm ↑ Summary blib/lib/Ereshkigal/App.pm »