summaryrefslogtreecommitdiffstats
path: root/perl-install/standalone/drakids
blob: 71c7a133950c0c12b354227537dc89076de35b50 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/perl

use strict;
use lib qw(/usr/lib/libDrakX);
use common;
use standalone;

use Socket;
use mygtk2 qw(gtknew);
use ugtk2 qw(:dialogs);
use POSIX qw(strftime);
use dbus_object;
use network::activefw;

use Gtk2::SimpleList;

use ugtk2 qw(:create :helpers :wrappers);

my $blacklist = Gtk2::SimpleList->new(addr => 'hidden',
                                      timestamp => 'hidden',
                                      N("Date") => 'text',
                                      N("Attacker") => 'text',
                                      N("Attack type") => 'text',
                                      N("Service") => 'text',
                                      N("Network interface") => 'text',
                                      N("Protocol") => 'text',
                                  );
$blacklist->get_selection->set_mode('multiple');
$blacklist->set_headers_clickable(1);
foreach (0, 1, 2) {
    $blacklist->get_column($_)->signal_connect('clicked', \&sort_by_column, $blacklist->get_model);
    #- sort on timestamp if Date column is clicked
    #- sort columns include hidden columns while list columns don't
    $blacklist->get_column($_)->set_sort_column_id($_ == 0 ? 1 : $_ + 2);
}

my $whitelist = Gtk2::SimpleList->new(addr => 'hidden',
                                      N("Attacker") => 'text',
                                  );
$whitelist->get_selection->set_mode('multiple');
$whitelist->set_headers_clickable(1);
$whitelist->get_column(0)->signal_connect('clicked', \&sort_by_column, $whitelist->get_model);
$whitelist->get_column(0)->set_sort_column_id(0);

my $activefw = network::activefw->new(dbus_object::system_bus(), sub {
    my ($_con, $msg) = @_;
    my $member = $msg->get_member;
    if ($member eq 'Blacklist') {
	handle_blacklist($msg->get_args_list);
    } elsif ($member eq 'Whitelist') {
	handle_whitelist($msg->get_args_list);
    } elsif ($member eq 'Clear') {
	clear_lists();
    } elsif ($member eq 'Init') {
	handle_init();
    }
});
init_lists();

$ugtk2::wm_icon = "/usr/lib/libDrakX/icons/drakfirewall.png";
my $w = ugtk2->new(N("Active Firewall"));
gtkpack($w->{window},
        gtknew('Notebook', children => [
            gtknew('Label', text => N("Blacklist")),
            gtknew('VBox', spacing => 5, children => [
                1, gtknew('ScrolledWindow', width => 600, height => 400, child => $blacklist),
                0, gtknew('HButtonBox', layout => 'edge', children_loose => [
                    gtknew('Button', text => N("Remove from blacklist"), clicked => sub { unblacklist(get_selected_blacklist()) }),
                    gtknew('Button', text => N("Move to whitelist"), clicked => sub { whitelist(get_selected_blacklist()) }),
                    gtknew('Button', text => N("Quit"), clicked => sub { Gtk2->main_quit })
                ]),
            ]),
            gtknew('Label', text => N("Whitelist")),
            gtknew('VBox', spacing => 5, children => [
                1, gtknew('ScrolledWindow', width => 600, height => 400, child => $whitelist),
                0, gtknew('HButtonBox', layout => 'edge', children_loose => [
                    gtknew('Button', text => N("Remove from whitelist"), clicked => sub { unwhitelist(get_selected_whitelist()) }),
                    gtknew('Button', text => N("Quit"), clicked => sub { Gtk2->main_quit })
                ]),
            ]),
        ]),
);
$w->show;
Gtk2->main;

ugtk2::exit(0);

sub sort_by_column {
    my ($column, $model) = @_;
    my $col_id = $column->get_sort_column_id;
    my ($old_id, $old_order) = $model->get_sort_column_id;
    $model->set_sort_column_id($col_id, $old_id == $col_id && $old_order ne 'descending' ? 'ascending' : 'descending');
}

sub handle_init() {
    $activefw->attach_object;
    init_lists();
}

sub list_remove_addr {
    my ($list, @addr) = @_;
    #- workaround buggy Gtk2::SimpleList array abstraction, it corrupts references
    foreach (0 .. $#{$list}) {
        member($list->[$_][0], @addr) and splice @$list, $_, 1;
    }
}

#- may throw an exception
sub init_blacklist() {
    my @packets = $activefw->get_blacklist;
    while (my @blacklist = splice(@packets, 0, 8)) {
        handle_blacklist(@blacklist);
    }
}

sub clear_blacklist() {
    @{$blacklist->{data}} = ();
}

sub handle_blacklist {
    my ($timestamp, $indev, $prefix, $_sensor, $protocol, $addr, $port, $_icmp_type) = @_;
    push @{$blacklist->{data}}, [
        $addr,
        $timestamp,
        network::activefw::format_date($timestamp),
        network::activefw::resolve_address(network::activefw::get_ip_address($addr)),
        $prefix eq 'SCAN' ? N("Port scanning") :
        $prefix eq 'SERV' ? N("Service attack") :
        $prefix eq 'PASS' ? N("Password cracking") :
        '',
        network::activefw::get_service($port) || '',
        $indev,
        $protocol || '',
    ];
}

sub get_selected_blacklist() {
    uniq(map { $blacklist->{data}[$_][0] } $blacklist->get_selected_indices);
}

sub unblacklist {
    my @addr = @_;
    eval { $activefw->unblacklist($_) foreach @addr };
    if ($@) {
        my $error = $@;
        err_dialog(N("Error"), N("Unable to remove from blacklist: %s", $error));
        return;
    }
    list_remove_addr($blacklist->{data}, @addr);
}

#- may throw an exception
sub init_whitelist() {
    handle_whitelist($_) foreach $activefw->get_whitelist;
}

sub clear_whitelist() {
    @{$whitelist->{data}} = ();
}

sub handle_whitelist {
    my ($addr) = @_;
    push @{$whitelist->{data}}, [ $addr, network::activefw::resolve_address(network::activefw::get_ip_address($addr)) ];
}

sub get_selected_whitelist() {
    uniq(map { $whitelist->{data}[$_][0] } $whitelist->get_selected_indices);
}

sub whitelist {
    my @addr = @_;
    unblacklist(@addr);
    eval { $activefw->whitelist($_) foreach @addr };
    if ($@) {
        my $error = $@;
        err_dialog(N("Error"), N("Unable to move in whitelist: %s", $error));
    }
}

sub unwhitelist {
    my @addr = @_;
    eval { $activefw->unwhitelist($_) foreach @addr };
    if ($@) {
        my $error = $@;
        err_dialog(N("Error"), N("Unable to remove from whitelist: %s", $error));
    }
    list_remove_addr($whitelist->{data}, @addr);
}

sub init_lists() {
    eval {
        init_blacklist();
        init_whitelist();
    };
    if ($@) {
        my $error = $@;
        err_dialog(N("Error"), N("Unable to contact the Active Firewall daemon: %s", $error));
    }
}

sub clear_lists() {
    clear_blacklist();
    clear_whitelist();
}