File Coverage

lib/ACL/Regex.pm
Criterion Covered Total %
statement 6 72 8.3
branch 0 32 0.0
condition n/a
subroutine 2 8 25.0
pod 5 6 83.3
total 13 118 11.0


line stmt bran cond sub pod time code
1             package ACL::Regex;
2             # This is a little package used to handle ACLs in a friendly,
3             # sysadmin like regex enabled manner.
4              
5 1     1   2351 use strict;
  1         2  
  1         35  
6              
7 1     1   4 use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK);
  1         2  
  1         1275  
8             require Exporter;
9              
10             @EXPORT = qw( new parse_acl_from_file match );
11             $VERSION = '0.0001_02';
12              
13             sub new {
14 0     0 0   my $type = shift;
15 0           bless {}, $type;
16             }
17              
18             # This variable stores all of the required fields
19             # for the ACL. If a required field is not in a
20             # given ACL or action, then it is autogenerated
21             # with the defaults (enabled).
22             my @required = qw(
23             account
24             action
25             ip
26             group
27             dow
28             time
29             );
30              
31             sub generate_required( $$ ){
32              
33 0     0 1   my ( $acl, $required_file ) = @_;
34              
35 0 0         open FD, "<$required_file" or die("Cannot open $required_file: $!\n" );
36 0           while( ){
37 0 0         next if /^#/;
38 0 0         if( /(\S+?)=(\S+)/ ){
39 0           my @a = split( /,/, $2 );
40 0           $acl->{req}->{$1} = \@a;
41             }
42             }
43 0           return ($acl);
44             }
45              
46             sub sanitize_acl ($$) {
47 0     0 1   my ( $self, $acl ) = @_;
48              
49             # Split up the ACL
50 0           my %hash = $acl =~ /(\S+?)=\[([^\[^\]].+?)\]/g;
51              
52 0           my @acl_array;
53 0           my @local_required = sort( keys %hash );
54              
55 0           my $action = $hash{action};
56              
57             return -1,'ERR','Action not defined'
58 0 0         unless defined $hash{action};
59              
60             #return 0,'WARN','Action not defined in required fields'
61             # unless defined $self->{req}->{$action};
62 0 0         if( defined $self->{req}->{$action} ){
63             #print "Using pre-defined requirements for $action from file\n";
64 0           @local_required = @{$self->{req}->{$action}};
  0            
65             }
66              
67             # Regenerate the hash
68 0           for my $key ( sort ( @local_required ) ) {
69 0 0         unless ( defined $hash{$key} ) {
70             # Uh-oh, it wasn't specified
71 0           my $acl_element = "$key=\\\[(.*?)\\\]";
72 0           push ( @acl_array, $acl_element );
73             } else {
74 0           my $acl_element = "$key=\\\[$hash{$key}\\\]";
75 0           push ( @acl_array, $acl_element );
76             }
77             } ## end for my $key ( sort ( @required...
78 0           return 0,'OK',join ( " ", @acl_array );
79             } ## end sub sanitize_acl ($)
80              
81             sub sanitize_action ($$) {
82 0     0 1   my ( $self, $acl ) = @_;
83              
84             # Split up the ACL
85 0           my %hash = $acl =~ /(\S+?)=\[([^\[^\]].+?)\]/g;
86              
87 0           my @acl_array;
88 0           my @local_required = sort( keys %hash );
89            
90 0           my $action = $hash{action};
91             return -1,'ERR',"Action [$action] not defined"
92 0 0         unless defined $hash{action};
93              
94             #return 0,'WARN','Action not defined in required fields'
95             # unless defined $self->{req}->{$action};
96 0 0         if( defined $self->{req}->{$action} ){
97 0           @local_required = @{$self->{req}->{$action}};
  0            
98             }
99              
100 0           my $action = $hash{action};
101              
102             # Regenerate the hash
103 0           for my $key ( sort ( @local_required ) ) {
104 0 0         unless ( defined $hash{$key} ) {
105             # Uh-oh, it wasn't specified
106 0           my $acl_element = "$key=\[]";
107 0           push ( @acl_array, $acl_element );
108             } else {
109 0           my $acl_element = "$key=\[$hash{$key}\]";
110 0           push ( @acl_array, $acl_element );
111             }
112             } ## end for my $key ( sort ( @required...
113 0           return 0,'OK',join ( " ", @acl_array );
114             } ## end sub sanitize_action ($)
115              
116             sub parse_acl_from_file( $$ ) {
117 0     0 1   my ( $self, $hash ) = @_;
118              
119             die ( "Please give a filename as an option!\n" )
120 0 0         unless defined $hash->{Filename};
121              
122 0 0         open FD, "<$hash->{Filename}"
123             or die ( "Cannot open $hash->{Filename}: $!\n" );
124              
125 0           ENTRY: while ( ) {
126 0           chomp;
127 0           s/^.*?(\s*#.*)//; # Get rid of comments
128 0 0         next ENTRY if /^$/;
129 0 0         if( /^\/(.+?)\/\s+?(.*)/ ){
130 0           my ($regex, $comment) = ($1,$2);
131 0           my ($rc,$rs,$sanitized) = $self->sanitize_acl( $regex );
132             next ENTRY
133 0 0         if $rc < 0;
134 0           $self->{message}->{"$sanitized"} = $comment;
135 0           push ( @{ $self->{ACL} }, $sanitized );
  0            
136             }
137             } ## end while ( )
138 0           close( FD );
139 0           return( $self );
140             } ## end sub parse_acl_from_file( $$ )
141              
142             sub match ($$) {
143 0     0 1   my ( $self, $action ) = @_;
144              
145 0           my ($rc,$rs,$sanitized) = $self->sanitize_action( $action );
146              
147 0 0         return( $rc,$rs,'')
148             if $rc < 0;
149              
150 0           for my $regex ( @{ $self->{ACL} } ) {
  0            
151 0 0         return ( 1, $regex, $self->{message}->{"$regex"} ) if ( $sanitized =~ /$regex/i );
152             }
153              
154 0           return ( 0, '', '' );
155             } ## end sub match ($$)
156              
157             1;
158             # vim: set ai ts=4 nu:
159              
160             __END__