blib/lib/ACL/Regex.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 126 SCAR 54.8
CC
15
Cov
0%
parse_acl_from_file
30.0
sanitize_acl
20.0
sanitize_action
20.0
CRAP 240.0
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 use strict;
6  
7 use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK);
8 require Exporter;
9  
10 @EXPORT = qw( new parse_acl_from_file match );
11 $VERSION = '0.0001_02';
12  
13 sub new {
14 my $type = shift;
15 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 my ( $acl, $required_file ) = @_;
34  
35 open FD, "<$required_file" or die("Cannot open $required_file: $!\n" );
36 while( <FD> ){
37 next if /^#/;
38 if( /(\S+?)=(\S+)/ ){
39 my @a = split( /,/, $2 );
40 $acl->{req}->{$1} = \@a;
41 }
42 }
43 return ($acl);
44 }
45  
46 sub sanitize_acl ($$) {
47 my ( $self, $acl ) = @_;
48  
49 # Split up the ACL
50 my %hash = $acl =~ /(\S+?)=\[([^\[^\]].+?)\]/g;
51  
52 my @acl_array;
53 my @local_required = sort( keys %hash );
54  
55 my $action = $hash{action};
56  
57 return -1,'ERR','Action not defined'
58 unless defined $hash{action};
59  
60 #return 0,'WARN','Action not defined in required fields'
61 # unless defined $self->{req}->{$action};
62 if( defined $self->{req}->{$action} ){
63 #print "Using pre-defined requirements for $action from file\n";
64 @local_required = @{$self->{req}->{$action}};
65 }
66  
67 # Regenerate the hash
68 for my $key ( sort ( @local_required ) ) {
69 unless ( defined $hash{$key} ) {
70 # Uh-oh, it wasn't specified
71 my $acl_element = "$key=\\\[(.*?)\\\]";
72 push ( @acl_array, $acl_element );
73 } else {
74 my $acl_element = "$key=\\\[$hash{$key}\\\]";
75 push ( @acl_array, $acl_element );
76 }
77 } ## end for my $key ( sort ( @required...
78 return 0,'OK',join ( " ", @acl_array );
79 } ## end sub sanitize_acl ($)
80  
81 sub sanitize_action ($$) {
82 my ( $self, $acl ) = @_;
83  
84 # Split up the ACL
85 my %hash = $acl =~ /(\S+?)=\[([^\[^\]].+?)\]/g;
86  
87 my @acl_array;
88 my @local_required = sort( keys %hash );
89
90 my $action = $hash{action};
91 return -1,'ERR',"Action [$action] not defined"
92 unless defined $hash{action};
93  
94 #return 0,'WARN','Action not defined in required fields'
95 # unless defined $self->{req}->{$action};
96 if( defined $self->{req}->{$action} ){
97 @local_required = @{$self->{req}->{$action}};
98 }
99  
100 my $action = $hash{action};
101  
102 # Regenerate the hash
103 for my $key ( sort ( @local_required ) ) {
104 unless ( defined $hash{$key} ) {
105 # Uh-oh, it wasn't specified
106 my $acl_element = "$key=\[]";
107 push ( @acl_array, $acl_element );
108 } else {
109 my $acl_element = "$key=\[$hash{$key}\]";
110 push ( @acl_array, $acl_element );
111 }
112 } ## end for my $key ( sort ( @required...
113 return 0,'OK',join ( " ", @acl_array );
114 } ## end sub sanitize_action ($)
115  
116 sub parse_acl_from_file( $$ ) {
117 my ( $self, $hash ) = @_;
118  
119 die ( "Please give a filename as an option!\n" )
120 unless defined $hash->{Filename};
121  
122 open FD, "<$hash->{Filename}"
123 or die ( "Cannot open $hash->{Filename}: $!\n" );
124  
125 ENTRY: while ( <FD> ) {
126 chomp;
127 s/^.*?(\s*#.*)//; # Get rid of comments
128 next ENTRY if /^$/;
129 if( /^\/(.+?)\/\s+?(.*)/ ){
130 my ($regex, $comment) = ($1,$2);
131 my ($rc,$rs,$sanitized) = $self->sanitize_acl( $regex );
132 next ENTRY
133 if $rc < 0;
134 $self->{message}->{"$sanitized"} = $comment;
135 push ( @{ $self->{ACL} }, $sanitized );
136 }
137 } ## end while ( <FD> )
138 close( FD );
139 return( $self );
140 } ## end sub parse_acl_from_file( $$ )
141  
142 sub match ($$) {
143 my ( $self, $action ) = @_;
144  
145 my ($rc,$rs,$sanitized) = $self->sanitize_action( $action );
146  
147 return( $rc,$rs,'')
148 if $rc < 0;
149  
150 for my $regex ( @{ $self->{ACL} } ) {
151 return ( 1, $regex, $self->{message}->{"$regex"} ) if ( $sanitized =~ /$regex/i );
152 }
153  
154 return ( 0, '', '' );
155 } ## end sub match ($$)
156  
157 1;
158 # vim: set ai ts=4 nu:
159  
160 __END__