summaryrefslogtreecommitdiffstats
path: root/perl-install/network/activefw.pm
blob: f152c581f868b375900554f9ba518097c8648074 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package activefw;

use Net::DBus;
use Net::DBus::Binding::Watch;
use Gtk2::Helper;
use POSIX qw(strftime);
use Socket;

sub new {
    my ($type, $filter) = @_;

    my $bus = Net::DBus->system;
    my $con = $bus->{connection};

    $con->add_filter($filter);
    $con->add_match("type='signal',interface='com.mandrakesoft.activefirewall'");

    set_DBus_watch($con);
    $con->dispatch;

    my $o = bless {
        bus => $bus,
        daemon => $daemon
    }, $type;

    $o->find_daemon;

    $o;
}

sub find_daemon {
    my ($o) = @_;
    my $service = $o->{bus}->get_service("com.mandrakesoft.activefirewall.daemon");
    $o->{daemon} = $service->get_object("/com/mandrakesoft/activefirewall", "com.mandrakesoft.activefirewall.daemon");
}

sub set_DBus_watch {
    my ($con) = @_;
    $con->set_watch_callbacks(sub {
        my ($con, $watch) = @_;
        my $flags = $watch->get_flags;
        if ($flags & &Net::DBus::Binding::Watch::READABLE) {
            Gtk2::Helper->add_watch($watch->get_fileno, 'in', sub {
                $watch->handle(&Net::DBus::Binding::Watch::READABLE);
                $con->dispatch;
                1;
            });
        }
        #- do nothing for WRITABLE watch, we dispatch when needed
    }, undef, undef); #- do nothing when watch is disabled or toggled yet
}

sub dispatch {
    my ($o) = @_;
    $o->{bus}{connection}->dispatch;
}

sub get_mode {
    my ($o) = @_;
    my $mode;
    eval {
        $mode = $o->{daemon}->GetMode;
    };
    if ($@) {
        print "(GetMode) exception: $@\n";
        $o->dispatch;
        return;
    }
    $mode;
}

sub blacklist {
    my ($o, $seq, $blacklist) = @_;
    eval {
        $o->{daemon}->Blacklist(Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_UINT32, $seq),
                                Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_UINT32, $blacklist));
    };
    if ($@) {
        print "(Blacklist) exception: $@\n";
        $o->dispatch;
    }
}

sub unblacklist {
    my ($o, $addr) = @_;
    eval {
        $o->{daemon}->UnBlacklist(Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_UINT32, $addr));
    };
    if ($@) {
        print "(UnBlacklist) exception: $@\n";
        $o->dispatch;
    }
}

sub whitelist {
    my ($o, $addr) = @_;
    eval {
        $o->{daemon}->Whitelist(Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_UINT32, $addr));
    };
    if ($@) {
        print "(Whitelist) exception: $@\n";
        $o->dispatch;
    }
}

sub unwhitelist {
    my ($o, $addr) = @_;
    eval {
        $o->{daemon}->UnWhitelist(Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_UINT32, $addr));
    };
    if ($@) {
        print "(UnWhitelist) exception: $@\n";
        $o->dispatch;
    }
}

sub set_interactive {
    my ($o, $mode) = @_;
    print "setting new IDS mode: $mode\n";
    eval {
        $o->{daemon}->SetMode(Net::DBus::Binding::Value->new(&Net::DBus::Binding::Message::TYPE_UINT32, $mode));
    };
    if ($@) {
        print "(SetMode) exception: $@\n";
        $o->dispatch;
    }
}

sub get_blacklist {
    my ($o) = @_;
    my @blacklist;
    eval {
        @blacklist = $o->{daemon}->GetBlacklist;
    };
    if ($@) {
        print "(GetBlacklist) exception: $@\n";
        $o->dispatch;
        return;
    }
    @blacklist;
}

sub get_whitelist {
    my ($o) = @_;
    my @whitelist;
    eval {
        @whitelist = $o->{daemon}->GetWhitelist;
    };
    if ($@) {
        print "(GetWhitelist) exception: $@\n";
        $o->dispatch;
        return;
    }
    @whitelist;
}

sub format_date {
    my ($timestamp) = @_;
    strftime("%c", localtime($timestamp));
}

sub get_service {
    my ($port) = @_;
    getservbyport($port, undef) || $port;
}

sub get_ip_address {
    my ($addr) = @_;
    inet_ntoa(pack('N', $addr));
}

sub resolve_address {
    my ($ip_addr) = @_;
    #- try to resolve address, timeout after 2 seconds
    my $hostname;
    eval {
        local $SIG{ALRM} = sub { die "ALARM" };
        alarm 2;
        $hostname = gethostbyaddr(inet_aton($ip_addr), AF_INET);
        alarm 0;
    };
    $hostname || $ip_addr;
}

1;