/[suikacvs]/messaging/manakai/lib/Message/Util/HostPermit.pm
Suika

Contents of /messaging/manakai/lib/Message/Util/HostPermit.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Wed Sep 17 02:34:32 2003 UTC (21 years, 2 months ago) by wakaba
Branch: MAIN
New

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     our $VERSION = do{my @r=(q$Revision: 1.2 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
15    
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     return 1;
73     }
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     if (length ($pattern) != 4) {
101     $pattern =~ m!([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)(?:/([0-9]+))?!;
102     $pattern = pack 'C4', $1, $2, $3, $4;
103     my $m = $5; $m %= 33;
104     my $mask = pack 'C4', (($m > 24) ? (2**($m-24)-1, 255, 255, 255) :
105     ($m > 16) ? (0, 2**($m-16)-1, 255, 255) :
106     ($m > 8) ? (0, 0, 2**($m-8)-1, 255) :
107     (0, 0, 0, 2**$m));
108     $pattern |= $mask;
109     }
110     return (($addr & $pattern) eq $addr) ? 1 : 0;
111     }
112    
113     ## TODO: IPv6 support
114     sub match_ipv6 {
115     return 0;
116     }
117    
118     =head1 LICENSE
119    
120     Copyright 2003 Wakaba <w@suika.fam.cx>
121    
122     This program is free software; you can redistribute it and/or
123     modify it under the same terms as Perl itself.
124    
125     =cut
126    
127     1; # $Date: 2003/08/05 07:30:14 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24