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.2 |
our $VERSION = do{my @r=(q$Revision: 1.1 $=~/\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 |
|
|
my ($name, undef, undef, undef, $addr) = gethostbyname ($name);
|
50 |
|
|
return 0 if !$name && !$addr;
|
51 |
|
|
for my $rule (@{$self->{rule}}) {
|
52 |
|
|
if ($rule->{-host}) {
|
53 |
|
|
if ($self->match_host ($rule->{-host}, $name)) {
|
54 |
|
|
if (!$rule->{-port} || ($rule->{-port} == $port)) {
|
55 |
|
|
return ($rule->{type} eq 'Allow') ? 1 : 0;
|
56 |
|
|
}
|
57 |
|
|
}
|
58 |
|
|
} elsif ($rule->{-ipv4}) {
|
59 |
|
|
if ($self->match_ipv4 ($rule->{-ipv4}, $addr)) {
|
60 |
|
|
if (!$rule->{-port} || ($rule->{-port} == $port)) {
|
61 |
|
|
return ($rule->{type} eq 'Allow') ? 1 : 0;
|
62 |
|
|
}
|
63 |
|
|
}
|
64 |
|
|
} elsif ($rule->{-ipv6}) {
|
65 |
|
|
if ($self->match_ipv6 ($rule->{-ipv6}, $addr)) {
|
66 |
|
|
if (!$rule->{-port} || ($rule->{-port} == $port)) {
|
67 |
|
|
return ($rule->{type} eq 'Allow') ? 1 : 0;
|
68 |
|
|
}
|
69 |
|
|
}
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
wakaba |
1.2 |
return 0;
|
73 |
wakaba |
1.1 |
}
|
74 |
|
|
|
75 |
|
|
sub match_host ($$$) {
|
76 |
|
|
my ($self, $pattern, $host) = @_;
|
77 |
|
|
if (index ($pattern, '*') > -1) {
|
78 |
|
|
my @host = reverse split /\./, $host;
|
79 |
|
|
my @pattern = reverse split /\./, $pattern;
|
80 |
|
|
return 0 if $#host < $#pattern;
|
81 |
|
|
for (0..$#pattern) {
|
82 |
|
|
if ($pattern[$_] eq '*') {
|
83 |
|
|
return 1;
|
84 |
|
|
} elsif ($host[$_] ne $pattern[$_]) {
|
85 |
|
|
return 0;
|
86 |
|
|
}
|
87 |
|
|
}
|
88 |
|
|
return 0;
|
89 |
|
|
} else {
|
90 |
|
|
return $pattern eq $host ? 1 : 0;
|
91 |
|
|
}
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
sub match_ipv4 ($$$) {
|
95 |
|
|
my ($self, $pattern, $addr) = @_;
|
96 |
|
|
if (length ($addr) != 4) {
|
97 |
|
|
$addr =~ /([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/;
|
98 |
|
|
$addr = pack 'C4', $1, $2, $3, $4;
|
99 |
|
|
}
|
100 |
wakaba |
1.2 |
my $mask = pack 'C4', 255, 255, 255, 255;
|
101 |
wakaba |
1.1 |
if (length ($pattern) != 4) {
|
102 |
|
|
$pattern =~ m!([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)(?:/([0-9]+))?!;
|
103 |
|
|
$pattern = pack 'C4', $1, $2, $3, $4;
|
104 |
wakaba |
1.2 |
my $m = $5; $m = 32 if $m > 32;
|
105 |
|
|
if (defined $m) {
|
106 |
|
|
$mask = pack 'C4', (($m > 24) ? (255, 255, 255, (2**($m-24)-1) << (32-$m)) :
|
107 |
|
|
($m > 16) ? (255, 255, (2**($m-16)-1) << (24-$m), 0) :
|
108 |
|
|
($m > 8) ? (255, (2**($m-8)-1) << (16-$m), 0, 0) :
|
109 |
|
|
((2**$m-1) << (8-$m), 0, 0, 0));
|
110 |
|
|
}
|
111 |
|
|
$pattern &= $mask;
|
112 |
|
|
#printf '[%vd] %vd (%s) %vd (%vd) %d', $mask, $pattern, $_[1], ($addr & $mask), $addr, (($addr & $mask) eq $pattern);
|
113 |
wakaba |
1.1 |
}
|
114 |
wakaba |
1.2 |
return (($addr & $mask) eq $pattern) ? 1 : 0;
|
115 |
wakaba |
1.1 |
}
|
116 |
|
|
|
117 |
|
|
## TODO: IPv6 support
|
118 |
|
|
sub match_ipv6 {
|
119 |
|
|
return 0;
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
=head1 LICENSE
|
123 |
|
|
|
124 |
|
|
Copyright 2003 Wakaba <w@suika.fam.cx>
|
125 |
|
|
|
126 |
|
|
This program is free software; you can redistribute it and/or
|
127 |
|
|
modify it under the same terms as Perl itself.
|
128 |
|
|
|
129 |
|
|
=cut
|
130 |
|
|
|
131 |
wakaba |
1.2 |
1; # $Date: 2003/09/17 02:34:32 $
|