1 |
wakaba |
1.1 |
|
2 |
|
|
=head1 NAME
|
3 |
|
|
|
4 |
|
|
Message::Util::HostPermit --- manakai: Simple host permission checker
|
5 |
|
|
|
6 |
|
|
=head1 DESCRIPTION
|
7 |
|
|
|
8 |
|
|
This module is part of manakai.
|
9 |
|
|
|
10 |
|
|
=cut
|
11 |
|
|
|
12 |
|
|
package Message::Util::HostPermit;
|
13 |
|
|
use strict;
|
14 |
wakaba |
1.3 |
our $VERSION = do{my @r=(q$Revision: 1.2 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
|
15 |
wakaba |
1.1 |
|
16 |
|
|
=head1 METHODS
|
17 |
|
|
|
18 |
|
|
=over 4
|
19 |
|
|
|
20 |
|
|
=item $err = Message::Util::HostPermit->new ()
|
21 |
|
|
|
22 |
|
|
Constructs new instance
|
23 |
|
|
|
24 |
|
|
=cut
|
25 |
|
|
|
26 |
|
|
sub new ($) {
|
27 |
|
|
my $class = shift;
|
28 |
|
|
my $self = bless {rule => []}, $class;
|
29 |
|
|
$self;
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
sub add_rule ($$) {
|
33 |
|
|
my ($self, $s) = @_;
|
34 |
|
|
for (split /[\x0D\x0A]+/, $s) {
|
35 |
|
|
s/\#.*$//g;
|
36 |
|
|
if (/^(Allow|Deny) (.+)$/) {
|
37 |
|
|
my $rule = {type => $1};
|
38 |
|
|
for (split /\s+/, $2) {
|
39 |
|
|
my ($name, $val) = split /=/, $_, 2;
|
40 |
|
|
$rule->{'-'.$name} = $val;
|
41 |
|
|
}
|
42 |
|
|
push @{$self->{rule}}, $rule;
|
43 |
|
|
}
|
44 |
|
|
}
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
sub check ($$;$) {
|
48 |
|
|
my ($self, $name, $port) = @_;
|
49 |
wakaba |
1.3 |
return 0 unless defined $name;
|
50 |
|
|
my $addr;
|
51 |
|
|
($name, undef, undef, undef, $addr) = gethostbyname ($name);
|
52 |
wakaba |
1.1 |
return 0 if !$name && !$addr;
|
53 |
|
|
for my $rule (@{$self->{rule}}) {
|
54 |
|
|
if ($rule->{-host}) {
|
55 |
|
|
if ($self->match_host ($rule->{-host}, $name)) {
|
56 |
|
|
if (!$rule->{-port} || ($rule->{-port} == $port)) {
|
57 |
|
|
return ($rule->{type} eq 'Allow') ? 1 : 0;
|
58 |
|
|
}
|
59 |
|
|
}
|
60 |
|
|
} elsif ($rule->{-ipv4}) {
|
61 |
|
|
if ($self->match_ipv4 ($rule->{-ipv4}, $addr)) {
|
62 |
|
|
if (!$rule->{-port} || ($rule->{-port} == $port)) {
|
63 |
|
|
return ($rule->{type} eq 'Allow') ? 1 : 0;
|
64 |
|
|
}
|
65 |
|
|
}
|
66 |
|
|
} elsif ($rule->{-ipv6}) {
|
67 |
|
|
if ($self->match_ipv6 ($rule->{-ipv6}, $addr)) {
|
68 |
|
|
if (!$rule->{-port} || ($rule->{-port} == $port)) {
|
69 |
|
|
return ($rule->{type} eq 'Allow') ? 1 : 0;
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
wakaba |
1.2 |
return 0;
|
75 |
wakaba |
1.1 |
}
|
76 |
|
|
|
77 |
|
|
sub match_host ($$$) {
|
78 |
|
|
my ($self, $pattern, $host) = @_;
|
79 |
|
|
if (index ($pattern, '*') > -1) {
|
80 |
|
|
my @host = reverse split /\./, $host;
|
81 |
|
|
my @pattern = reverse split /\./, $pattern;
|
82 |
|
|
return 0 if $#host < $#pattern;
|
83 |
|
|
for (0..$#pattern) {
|
84 |
|
|
if ($pattern[$_] eq '*') {
|
85 |
|
|
return 1;
|
86 |
|
|
} elsif ($host[$_] ne $pattern[$_]) {
|
87 |
|
|
return 0;
|
88 |
|
|
}
|
89 |
|
|
}
|
90 |
|
|
return 0;
|
91 |
|
|
} else {
|
92 |
|
|
return $pattern eq $host ? 1 : 0;
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
sub match_ipv4 ($$$) {
|
97 |
|
|
my ($self, $pattern, $addr) = @_;
|
98 |
|
|
if (length ($addr) != 4) {
|
99 |
wakaba |
1.3 |
$addr =~ /([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/ or return 0;
|
100 |
wakaba |
1.1 |
$addr = pack 'C4', $1, $2, $3, $4;
|
101 |
|
|
}
|
102 |
wakaba |
1.2 |
my $mask = pack 'C4', 255, 255, 255, 255;
|
103 |
wakaba |
1.1 |
if (length ($pattern) != 4) {
|
104 |
wakaba |
1.3 |
$pattern =~ m!([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)(?:/([0-9]+))?!
|
105 |
|
|
or return 0;
|
106 |
wakaba |
1.1 |
$pattern = pack 'C4', $1, $2, $3, $4;
|
107 |
wakaba |
1.3 |
my $m = $5;
|
108 |
wakaba |
1.2 |
if (defined $m) {
|
109 |
wakaba |
1.3 |
$m = $m > 32 ? 32 : $m + 0;
|
110 |
wakaba |
1.2 |
$mask = pack 'C4', (($m > 24) ? (255, 255, 255, (2**($m-24)-1) << (32-$m)) :
|
111 |
|
|
($m > 16) ? (255, 255, (2**($m-16)-1) << (24-$m), 0) :
|
112 |
|
|
($m > 8) ? (255, (2**($m-8)-1) << (16-$m), 0, 0) :
|
113 |
|
|
((2**$m-1) << (8-$m), 0, 0, 0));
|
114 |
|
|
}
|
115 |
|
|
$pattern &= $mask;
|
116 |
|
|
#printf '[%vd] %vd (%s) %vd (%vd) %d', $mask, $pattern, $_[1], ($addr & $mask), $addr, (($addr & $mask) eq $pattern);
|
117 |
wakaba |
1.1 |
}
|
118 |
wakaba |
1.2 |
return (($addr & $mask) eq $pattern) ? 1 : 0;
|
119 |
wakaba |
1.1 |
}
|
120 |
|
|
|
121 |
|
|
## TODO: IPv6 support
|
122 |
|
|
sub match_ipv6 {
|
123 |
|
|
return 0;
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
=head1 LICENSE
|
127 |
|
|
|
128 |
|
|
Copyright 2003 Wakaba <w@suika.fam.cx>
|
129 |
|
|
|
130 |
|
|
This program is free software; you can redistribute it and/or
|
131 |
|
|
modify it under the same terms as Perl itself.
|
132 |
|
|
|
133 |
|
|
=cut
|
134 |
|
|
|
135 |
wakaba |
1.3 |
1; # $Date: 2003/09/27 07:59:11 $
|