| 1 | package Ereshkigal::App::Command::add; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Ereshkigal::App -command; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Ereshkigal::App::Command::add - Define and start a new kur instance at runtime. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | ereshkigal add sshd --backend ipfw --ports 22 --protocols tcp | ||||||||||||||||||||||||||||
| 23 | ereshkigal add baphomet --fan-out sshd,smtp | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Defines and starts a new kur instance in the running manager. Does not | ||||||||||||||||||||||||||||
| 28 | rewrite the config file... to make it permanent, add it to the config. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | With C<--fan-out> in place of C<--backend> the new kur is a manager side | ||||||||||||||||||||||||||||
| 31 | fan out kur... no process of its own, with commands targeted at it | ||||||||||||||||||||||||||||
| 32 | fanning out to the listed member kurs. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 METHODS | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | The App::Cmd hooks this subcommand supplies... abstract, description, | ||||||||||||||||||||||||||||
| 37 | usage_desc, opt_spec, validate_args, and execute. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | sub abstract { return 'define and start a new kur instance at runtime' } | ||||||||||||||||||||||||||
|
Subroutine
abstract: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | sub description { | ||||||||||||||||||||||||||
|
Subroutine
description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 4 | return <<'DESCRIPTION'; | |||||||||||||||||||||||||||
| 45 | Raise a new underworld in the running manager, now. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Defines a kur and starts it, building its firewall setup immediately. The | ||||||||||||||||||||||||||||
| 48 | options mirror what the kur bin takes and are handed straight through. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | With --fan-out in place of --backend the new kur is a gate instead: no | ||||||||||||||||||||||||||||
| 51 | process and no firewall of its own, just a name that commands fan out | ||||||||||||||||||||||||||||
| 52 | from to the kurs it lists. Handy as a single point of contact for an | ||||||||||||||||||||||||||||
| 53 | integration, which can then be authorized for the gate alone rather than | ||||||||||||||||||||||||||||
| 54 | each member. Gates cannot nest. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | The config file is NOT rewritten. A kur added this way is gone at the next | ||||||||||||||||||||||||||||
| 57 | manager start unless you also add it to the config yourself, which is | ||||||||||||||||||||||||||||
| 58 | easy to forget after an incident. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Backend specific settings go via --option, repeated per setting. The one | ||||||||||||||||||||||||||||
| 61 | exception is --interfaces, which some backends want as a list rather than | ||||||||||||||||||||||||||||
| 62 | a single value. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | ereshkigal add dns --backend pf --ports 53 --protocols tcp,udp --option kill=1 | ||||||||||||||||||||||||||||
| 65 | ereshkigal add edge --backend xdp --interfaces eth0,eth1 --enable-cidr 1 | ||||||||||||||||||||||||||||
| 66 | ereshkigal add gate --fan-out sshd,smtp | ||||||||||||||||||||||||||||
| 67 | DESCRIPTION | ||||||||||||||||||||||||||||
| 68 | } ## end sub description | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 12 | ▶ | sub usage_desc { return '%c add %o <kur>'; } | ||||||||||||||||||||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 12 | ▶ | sub opt_spec { | ||||||||||||||||||||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | return ( | ||||||||||||||||||||||||||||
| 74 | 12 | [ 'backend=s', 'the Net::Firewall::BlockerHelper backend to use' ], | |||||||||||||||||||||||||||
| 75 | [ 'fan-out=s', 'comma separated list of kurs to fan out to, in place of --backend' ], | ||||||||||||||||||||||||||||
| 76 | [ 'ports=s', 'comma separated list of ports to block' ], | ||||||||||||||||||||||||||||
| 77 | [ 'protocols=s', 'comma separated list of protocols to block' ], | ||||||||||||||||||||||||||||
| 78 | [ 'prefix=s', 'the prefix to use' ], | ||||||||||||||||||||||||||||
| 79 | [ 'option=s@', 'a backend specific option, key=value, may be given multiple times' ], | ||||||||||||||||||||||||||||
| 80 | [ 'interfaces=s', 'comma separated list of interfaces, handed over as the interfaces option array' ], | ||||||||||||||||||||||||||||
| 81 | [ 'self-heal=i', 'if the firewall setup should be checked and re-inited before each ban/unban' ], | ||||||||||||||||||||||||||||
| 82 | [ 'ban-time=i', 'seconds bans should last for this kur, 0 meaning never time out' ], | ||||||||||||||||||||||||||||
| 83 | [ 'checkpoint=i', 'seconds between ban state CSV rewrites for this kur' ], | ||||||||||||||||||||||||||||
| 84 | [ 'enable-cidr=s', 'if CIDR banning is enabled for this kur' ], | ||||||||||||||||||||||||||||
| 85 | [ 'cidr-silent-drop=s', 'if unavailable CIDR commands are silently dropped rather than errored' ], | ||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||
| 87 | } ## end sub opt_spec | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 8 | ▶ | sub validate_args { | ||||||||||||||||||||||||||
|
Subroutine
validate_args: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 8 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 8 | ▶ | if ( @{$args} != 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 2 | $self->usage_error('a single kur instance name must be specified'); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 6 | ▶ | if ( !defined( $opt->backend ) && !defined( $opt->fan_out ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opt->backend or defined $opt->fan_out
MC/DC100%2/2
defined $opt->backend or defined $opt->fan_out
defined $opt->backend
defined $opt->fan_out
Truth table100%3/3
A or B
A= defined $opt->backendB= defined $opt->fan_out
| |||||||||||||||||||||||||||||
| 96 | 1 | $self->usage_error('either --backend or --fan-out must be specified'); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 5 | ▶ | if ( defined( $opt->backend ) && defined( $opt->fan_out ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opt->backend and defined $opt->fan_out
MC/DC100%2/2
defined $opt->backend and defined $opt->fan_out
defined $opt->backend
defined $opt->fan_out
Truth table100%3/3
A and B
A= defined $opt->backendB= defined $opt->fan_out
| |||||||||||||||||||||||||||||
| 99 | 1 | $self->usage_error('--backend and --fan-out may not be used together'); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | return; | |||||||||||||||||||||||||||
| 103 | } ## end sub validate_args | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 4 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=15 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 4 | my ( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 4 | my $opts = {}; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 4 | ▶ | if ( defined( $opt->backend ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 3 | $opts->{backend} = $opt->backend; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 4 | ▶ | if ( defined( $opt->fan_out ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 1 | $opts->{fan_out} = $self->_split_comma_list( 'fan-out', $opt->fan_out ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 4 | ▶ | if ( defined( $opt->ports ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1 | $opts->{ports} = $self->_split_comma_list( 'ports', $opt->ports ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 4 | ▶ | if ( defined( $opt->protocols ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 1 | $opts->{protocols} = $self->_split_comma_list( 'protocols', $opt->protocols ); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 4 | ▶ | if ( defined( $opt->prefix ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 1 | $opts->{prefix} = $opt->prefix; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 4 | ▶ | if ( defined( $opt->self_heal ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 1 | $opts->{self_heal} = $opt->self_heal; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 4 | ▶ | if ( defined( $opt->ban_time ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 1 | $opts->{ban_time} = $opt->ban_time; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 4 | ▶ | if ( defined( $opt->checkpoint ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 1 | $opts->{checkpoint} = $opt->checkpoint; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 4 | ▶ | if ( defined( $opt->option ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 2 | my %backend_options; | |||||||||||||||||||||||||||
| 136 | 2 | foreach my $key_value ( @{ $opt->option } ) { | |||||||||||||||||||||||||||
| 137 | 3 | my ( $key, $value ) = split( /=/, $key_value, 2 ); | |||||||||||||||||||||||||||
| 138 | 3 | ▶ | if ( !defined($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 1 | $self->usage_error( '--option "' . $key_value . '" is not in the form key=value' ); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 2 | $backend_options{$key} = $value; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 1 | $opts->{options} = \%backend_options; | |||||||||||||||||||||||||||
| 144 | } ## end if ( defined( $opt->option ) ) | ||||||||||||||||||||||||||||
| 145 | 3 | ▶ | if ( defined( $opt->interfaces ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | $opts->{options}{interfaces} = $self->_split_comma_list( 'interfaces', $opt->interfaces ); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 3 | ▶ | if ( defined( $opt->enable_cidr ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | $opts->{enable_cidr} = $opt->enable_cidr; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 3 | ▶ | if ( defined( $opt->cidr_silent_drop ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | $opts->{cidr_silent_drop} = $opt->cidr_silent_drop; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 3 | $self->run_command( 'add_kur', { 'name' => $args->[0], 'opts' => $opts } ); | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2 | return; | |||||||||||||||||||||||||||
| 158 | } ## end sub execute | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # Turns one of this command's comma separated option values into the array | ||||||||||||||||||||||||||||
| 161 | # ref the manager expects for it. The kur bin takes --ports 22,80 and the | ||||||||||||||||||||||||||||
| 162 | # like as a single string, but add_kur is a JSON request where ports is a | ||||||||||||||||||||||||||||
| 163 | # array, so the splitting has to happen somewhere and it happens here. | ||||||||||||||||||||||||||||
| 164 | # Shared by --fan-out, --ports, --protocols and --interfaces rather than | ||||||||||||||||||||||||||||
| 165 | # each doing its own, so all four treat sloppy input the same way. | ||||||||||||||||||||||||||||
| 166 | # | ||||||||||||||||||||||||||||
| 167 | # The value is trimmed at both ends, split on commas with any surrounding | ||||||||||||||||||||||||||||
| 168 | # whitespace eaten, and empty elements dropped, so ",22", "22, 23," and | ||||||||||||||||||||||||||||
| 169 | # "22 , 23" all come out as clean lists. Nothing else is checked... whether | ||||||||||||||||||||||||||||
| 170 | # 22 is a plausible port or eth0 a real interface is for the manager and the | ||||||||||||||||||||||||||||
| 171 | # backend to say, not this. | ||||||||||||||||||||||||||||
| 172 | # | ||||||||||||||||||||||||||||
| 173 | # Args, both positional and required... | ||||||||||||||||||||||||||||
| 174 | # | ||||||||||||||||||||||||||||
| 175 | # $option_name :: The option's name without its leading dashes, as in | ||||||||||||||||||||||||||||
| 176 | # 'ports' or 'fan-out'. Used only to build the usage | ||||||||||||||||||||||||||||
| 177 | # error, so it wants to be the dashed spelling the user | ||||||||||||||||||||||||||||
| 178 | # actually typed rather than the underscored one the | ||||||||||||||||||||||||||||
| 179 | # opt object carries. | ||||||||||||||||||||||||||||
| 180 | # $option_value :: The raw string as given on the command line. Must be | ||||||||||||||||||||||||||||
| 181 | # defined... callers guard with a defined check on the | ||||||||||||||||||||||||||||
| 182 | # opt accessor, and an undef reaching here warns and | ||||||||||||||||||||||||||||
| 183 | # then behaves as the empty string, which is a usage | ||||||||||||||||||||||||||||
| 184 | # error anyway. | ||||||||||||||||||||||||||||
| 185 | # | ||||||||||||||||||||||||||||
| 186 | # Returns an array ref of the elements, in the order given and with | ||||||||||||||||||||||||||||
| 187 | # duplicates left alone... a caller wanting them deduped does that its | ||||||||||||||||||||||||||||
| 188 | # self, and for ports the manager does it anyway. | ||||||||||||||||||||||||||||
| 189 | # | ||||||||||||||||||||||||||||
| 190 | # Does not return if the value yields nothing, as "" or "," would. That | ||||||||||||||||||||||||||||
| 191 | # calls usage_error, which dies with the message and the usage screen, so | ||||||||||||||||||||||||||||
| 192 | # --ports with an empty value is refused rather than quietly becoming a | ||||||||||||||||||||||||||||
| 193 | # ports list of none. | ||||||||||||||||||||||||||||
| 194 | # | ||||||||||||||||||||||||||||
| 195 | # my $ports = $self->_split_comma_list( 'ports', '22, 80,443' ); | ||||||||||||||||||||||||||||
| 196 | # # [ '22', '80', '443' ] | ||||||||||||||||||||||||||||
| 197 | # | ||||||||||||||||||||||||||||
| 198 | # my $members = $self->_split_comma_list( 'fan-out', ',sshd,,smtp,' ); | ||||||||||||||||||||||||||||
| 199 | # # [ 'sshd', 'smtp' ] | ||||||||||||||||||||||||||||
| 200 | 3 | ▶ | sub _split_comma_list { | ||||||||||||||||||||||||||
|
Subroutine
_split_comma_list: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 201 | 3 | my ( $self, $option_name, $option_value ) = @_; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 3 | $option_value =~ s/^\s+//; | |||||||||||||||||||||||||||
| 204 | 3 | $option_value =~ s/\s+$//; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 3 | my @elements = grep { length } split( /\s*,\s*/, $option_value ); | |||||||||||||||||||||||||||
| 207 | 3 | ▶ | if ( !@elements ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | $self->usage_error( '--' . $option_name . ' must contain at least one item' ); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 3 | return \@elements; | |||||||||||||||||||||||||||
| 212 | } ## end sub _split_comma_list | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 2 | 1; | |||||||||||||||||||||||||||