-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal_proj.pl
106 lines (91 loc) · 1.43 KB
/
final_proj.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/perl
use strict;
use warnings;
my $directive;
my %allow;
my %deny;
while( <> ) {
chomp;
if( /\Aorder (allow\,deny|deny\,allow)\z/ )
{
if( $directive )
{
warn "Multiple ORDER directives\n";
}
$directive = $1;
}
elsif( /\A(allow|deny) from (.*)\z/ )
{
if( $1 eq 'allow' ) {$allow{$2} = 1;}
elsif ( $1 eq 'deny' ) { $deny{$2} = 1;}
}
}
die "No order directive in input" unless( $directive );
print "Input address to test: ";
while( <STDIN> )
{
chomp;
exit if /\Aquit\z/;
if ($directive eq "allow,deny")
{
if( match( \%allow, $_ ) )
{
if( match( \%deny, $_ ) )
{
print "REJECTED\n";
}
else
{
print "ALLOWED\n";
}
}
else
{
print "REJECTED\n";
}
}
elsif ($directive eq "deny,allow")
{
if ( match( \%deny, $_ ) )
{
if ( match( \%allow, $_ ) )
{
print "ALLOWED\n";
}
else
{
print "REJECTED\n";
}
}
elsif ( match( \%allow, $_ ) )
{
print "ALLOWED\n";
}
else
{
print "ALLOWED\n";
}
}
print "Input address to test: ";
}
sub match
{
my %hash = %{ shift @_ };
my $target = shift @_;
if( $target =~ /\d+/ ){
foreach my $h( keys %hash ) {
if( $target =~ /\A$h/ ) {
return 1;
}
}
return 0;
} elsif ( $target =~ /[a-z]+/ ) {
foreach my $h( keys %hash ) {
if( $target =~ /$h\z/ ) {
return 1;
}
}
return 0;
}
return 0;
}