« blib/lib/Lemonldap/NG/Portal/Plugins/RESTServer.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Lib/SAML.pm »
| 1 | package Lemonldap::NG::Portal::Lib::CrowdSecFilter; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse::Role; | ||
| 5 | use Regexp::Assemble; | ||
| 6 | |||
| 7 | use constant knownCat => (qw(url)); | ||
| 8 | use constant knownSuffixes => (qw(re txt)); | ||
| 9 | |||
| 10 | # Initialization functions for CrowdsecFilter feature | ||
| 11 | |||
| 12 | sub initializeFilters { | ||
| 13 | my ($self) = @_; | ||
| 14 | my $filters = $self->parseFilters( $self->conf->{crowdsecFilters} ); | ||
| 15 | if ( $filters and %$filters ) { | ||
| 16 | foreach my $cat ( keys %$filters ) { | ||
| 17 | my $re = Regexp::Assemble->new; | ||
| 18 | eval { | ||
| 19 | $re->add( map { qr/(?i)$_/ } @{ $filters->{$cat}->{re} } ) | ||
| 20 | if $filters->{$cat}->{re}; | ||
| 21 | $re->add( map { qr/(?i)\Q$_\E/ } @{ $filters->{$cat}->{txt} } ) | ||
| 22 | if @{ $filters->{$cat}->{txt} }; | ||
| 23 | }; | ||
| 24 | if ($@) { | ||
| 25 | $self->logger->error("Unable to parst category $cat: $@"); | ||
| 26 | } | ||
| 27 | else { | ||
| 28 | $self->filters->{$cat} = $re->re; | ||
| 29 | $self->logger->debug("RE $cat: $re"); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | sub parseFilters { | ||
| 36 | my ( $self, $dirname, $res, $cat ) = @_; | ||
| 37 | $self->logger->debug("Crowdsec filters, parsing $dirname"); | ||
| 38 | $res //= {}; | ||
| 39 | my $fh; | ||
| 40 | unless ( opendir $fh, $dirname ) { | ||
| 41 | $self->logger->error("Unable to read directory $dirname: $!"); | ||
| 42 | return $res; | ||
| 43 | } | ||
| 44 | my @files = grep /\w/, readdir $fh; | ||
| 45 | closedir $fh; | ||
| 46 | LOOP: foreach my $file (@files) { | ||
| 47 | |||
| 48 | # Sub-directories fixes the category | ||
| 49 | my $path = "$dirname/$file"; | ||
| 50 | if ( -d $path ) { | ||
| 51 | if ($cat) { | ||
| 52 | $self->parseFilters( $path, $res, $cat ); | ||
| 53 | } | ||
| 54 | elsif ( my ($t) = grep { $file =~ m/^$_/ } knownCat ) { | ||
| 55 | $self->parseFilters( $path, $res, $t ); | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | $self->logger->error("Unknwon category for directory $path"); | ||
| 59 | } | ||
| 60 | next LOOP; | ||
| 61 | } | ||
| 62 | $file =~ s/\.([^\.]+)$//; | ||
| 63 | my $type = $1; | ||
| 64 | unless ( $type and grep { $_ eq $type } knownSuffixes ) { | ||
| 65 | $self->logger->error("Bad suffix for $path, skipping"); | ||
| 66 | next LOOP; | ||
| 67 | } | ||
| 68 | my $lcat = $cat; | ||
| 69 | unless ($lcat) { | ||
| 70 | $file =~ s/\.([^\.]+)$//; | ||
| 71 | $lcat = $1; | ||
| 72 | unless ($lcat) { | ||
| 73 | $self->logger->error("Malformed file $path (missing category)"); | ||
| 74 | next LOOP; | ||
| 75 | } | ||
| 76 | unless ( grep { $_ eq $lcat } knownCat ) { | ||
| 77 | $self->logger->error("Unknown category $lcat for $path"); | ||
| 78 | next LOOP; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | unless ( open $fh, '<', $path ) { | ||
| 82 | $self->logger->error("Unable to read file $path: $!"); | ||
| 83 | next LOOP; | ||
| 84 | } | ||
| 85 | $self->logger->debug( | ||
| 86 | "Crowdsec filters, adding content of $path into category $lcat, type $type" | ||
| 87 | ); | ||
| 88 | my $c = 0; | ||
| 89 | foreach (<$fh>) { | ||
| 90 | next if /^\s*#/; | ||
| 91 | next unless /\w/; | ||
| 92 | s/[\r\n]//g; | ||
| 93 | push @{ $res->{$lcat}->{$type} }, $_; | ||
| 94 | $c++; | ||
| 95 | } | ||
| 96 | $self->logger->debug(" -> $c lines added"); | ||
| 97 | } | ||
| 98 | return $res; | ||
| 99 | } | ||
| 100 | |||
| 101 | 1; |