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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
#!/usr/bin/perl
use strict;
use lib qw(/usr/lib/libDrakX);
use c;
use common;
use standalone;
use network::network;
use network::tools;
use run_program;
use mygtk2 qw(gtknew);
use dbus_object;
use network::ifw;
use network::monitor;
use detect_devices;
use Gtk2::TrayIcon;
use ugtk2 qw(:create :helpers :wrappers :dialogs);
my $onstartupfile = "$ENV{HOME}/.net_applet";
shouldStart() or die "$onstartupfile should be set to TRUE or use net_applet --force";
# Allow multiple instances, but only one per user:
is_running('net_applet') and die "net_applet already running\n";
my ($eventbox, $img, $balloon);
my ($current_state, $current_interface, $menu, $wireless_menu, $timeout, $update_timeout);
add_icon_path("/usr/share/libDrakX/pixmaps/");
my $net = {};
my $watched_interface;
my $dbus = dbus_object::system_bus();
my $monitor = network::monitor->new($dbus);
my ($ifw, $interactive_cb, @attacks_queue);
my %pixbufs =
(
state => { map { $_ => gtkcreate_pixbuf($_) } qw(connected disconnected) },
link_level => { map {
my $f = "./wifi-$_.png";
my $pixbuf = gtkcreate_pixbuf(-r $f ? $f : 'default')->scale_simple(24, 24, 'hyper');
$_ => $pixbuf;
} qw(20 40 60 80 100) },
keyring => gtkcreate_pixbuf("/usr/share/pixmaps/keyring-small.png")->scale_simple(24, 24, 'hyper'), #- provided by usermode, required by drakxtools
);
my %wireless_networks;
my %tooltips =
(
connected => N_("Network is up on interface %s"),
disconnected =>
#-PO: keep the "Configure Network" substring synced with the "Configure Network" message below
N_("Network is down on interface %s. Click on \"Configure Network\""),
notconfigured => N_("You do not have any configured Internet connection.
Run the \"%s\" assistant from the Mandriva Linux Control Center", N("Set up a new network interface (LAN, ISDN, ADSL, ...)")),
);
my %actions = (
'upNetwork' => { name => sub { N("Connect %s", $_[0]) }, launch => sub { network::tools::start_interface($_[0], 1) } },
'downNetwork' => { name => sub { N("Disconnect %s", $_[0]) }, launch => sub { network::tools::stop_interface($_[0], 1) } },
'monitorNetwork' => { name => N("Monitor Network"), launch => \&run_net_monitor },
'monitorAFW' => { name => N("Interactive Firewall"), launch => \&run_drakids },
'wireless' => { name => N("Manage wireless networks"), launch => \&run_drakroam },
'confNetwork' => { name => N("Configure Network"), launch => sub { system("/usr/sbin/drakconnect --skip-wizard &") } },
'chooseInterface' => {
name => N("Watched interface"),
choices => sub { N("Auto-detect"), sort keys %{$net->{ifcfg}} },
choice_selected => sub { $watched_interface ? $_[0] eq $watched_interface : $_[0] eq N("Auto-detect") },
launch => sub {
$watched_interface = $_[0] eq N("Auto-detect") ? undef : $_[0];
checkNetworkForce();
}
},
'chooseProfile' => {
name => N("Profiles"),
choices => sub { network::network::netprofile_list() },
choice_selected => sub { $_[0] eq $net->{PROFILE} },
launch => sub {
require run_program;
$net->{PROFILE} = $_[0];
run_program::raw({ detach => 1 }, network::tools::wrap_command_for_root('/sbin/set-netprofile', $net->{PROFILE}));
}
},
'help' => { name => N("Get Online Help"), launch => sub { system("drakhelp --id internet-connection &") } },
'quit' => { name => N("Quit"), launch => \&mainQuit },
);
gtkadd(my $icon = Gtk2::TrayIcon->new("Net_Applet"),
gtkadd($eventbox = Gtk2::EventBox->new,
gtkpack($img = Gtk2::Image->new)
)
);
$eventbox->signal_connect(button_press_event => sub {
$_[1]->button == 1 and run_net_monitor();
$_[1]->button == 3 && $menu and $menu->popup(undef, undef, undef, undef, $_[1]->button, $_[1]->time);
});
$icon->show_all;
$balloon = Gtk2::Balloon->new_from_window($icon->window);
$balloon->add_events('button-press-mask');
$balloon->signal_connect(hide => sub {
#- on timeout, apply default policy
exists $attacks_queue[0]{handled} or set_blacklist_verdict($attacks_queue[0]{seq}, undef);
});
$balloon->signal_connect(button_press_event => sub {
$attacks_queue[0]{handled} = 1;
Gtk2::Balloon::hide_text($balloon);
ask_attack_verdict($attacks_queue[0]);
});
$ifw = network::ifw->new($dbus, sub {
my ($_con, $msg) = @_;
my $member = $msg->get_member;
if ($member eq 'Attack') {
handle_attack($msg->get_args_list);
} elsif ($member eq 'Init') {
$ifw->attach_object;
checkNetworkForce();
}
});
checkNetworkForce();
cronNetwork();
cronUpdate();
get_unprocessed_attacks();
$SIG{HUP} = sub {
print "received SIGHUP, reloading network configuration\n";
checkNetworkForce();
};
Gtk2->main;
ugtk2::exit(0);
sub is_running {
my ($name) = @_;
any {
my ($ppid, $pid, $n) = /^\s*(\d+)\s+(\d+)\s+(.*)/;
$pid != $$ && $n eq $name;
} `ps -o '%P %p %c' -u $ENV{USER}`;
}
sub shouldStart() {
my ($opt) = @ARGV;
if ($opt eq '--force' || $opt eq '-f') {
return 1;
}
return getAutoStart();
}
sub run_net_monitor() {
run_program::raw({ detach => 1 }, '/usr/sbin/net_monitor', '--defaultintf', $current_interface) unless is_running('net_monitor');
}
sub run_drakroam() {
run_program::raw({ detach => 1 }, '/usr/sbin/drakroam') unless is_running('drakroam');
}
sub run_drakids() {
run_program::raw({ detach => 1 }, '/usr/sbin/drakids') unless is_running('drakids');
}
sub generate_wireless_menuitem {
my ($net) = @_;
$net->{menuitem} = Gtk2::CheckMenuItem->new;
$net->{menuitem}->set_draw_as_radio(1);
$net->{menuitem}->add(gtkpack_(gtkshow(Gtk2::HBox->new),
1, gtkset_alignment($net->{ssid_label} = Gtk2::Label->new, 0, 0),
0, $net->{keyring_image} = Gtk2::Image->new_from_pixbuf($pixbufs{keyring}),
0, $net->{level_image} = Gtk2::Image->new));
$net->{activate} = $net->{menuitem}->signal_connect('activate' => sub {
if (exists $net->{id}) {
eval { $monitor->select_network($net->{id}) };
$@ and err_dialog(N("Interactive Firewall"), N("Unable to contact daemon"));
} else {
run_drakroam();
}
checkNetworkForce();
});
undef $current_state; #- force menu redraw
}
sub update_wireless_item {
my ($net, $ap_address) = @_;
$net->{ssid_label}->set_text($net->{ssid} || "[$ap_address]");
$net->{keyring_image}->visible(defined $net->{flags});
$net->{level_image}->set_from_pixbuf($pixbufs{link_level}{$net->{approx_level}});
$net->{menuitem}->signal_handler_block($net->{activate});
$net->{menuitem}->set_active($net->{current});
$net->{menuitem}->signal_handler_unblock($net->{activate});
}
sub checkWireless() {
my $networks = $monitor->list_wireless;
foreach (keys %$networks) {
my $net = $wireless_networks{$_} ||= {};
put_in_hash($net, $networks->{$_});
exists $net->{menuitem} or generate_wireless_menuitem($net);
update_wireless_item($net, $_);
}
$wireless_networks{$_}{menuitem}->visible(exists $networks->{$_}) foreach keys %wireless_networks;
}
sub checkNetwork() {
checkWireless();
my ($gw_intf, $_is_up, $gw_address) = $watched_interface ?
($watched_interface, network::tools::get_interface_status($watched_interface)) :
network::tools::get_internet_connection($net);
go2State($gw_address ? 'connected' : $gw_intf ? 'disconnected' : 'notconfigured', $gw_intf);
}
sub checkNetworkForce() {
$net = {};
network::network::read_net_conf($net);
undef $current_state;
checkNetwork();
}
sub cronNetwork() {
$timeout = Glib::Timeout->add(2000, sub {
checkNetwork();
1;
});
}
sub cronUpdate() {
my $current_md5 = common::md5file($0);
$update_timeout = Glib::Timeout->add(60000, sub {
common::md5file($0) ne $current_md5 and exec($0);
});
}
sub go2State {
my ($state_type, $interface) = @_;
if ($current_state ne $state_type || $current_interface ne $interface) {
$current_state = $state_type;
$current_interface = $interface;
if ($menu) {
if (my $m = $wireless_menu && $wireless_menu->get_submenu) {
$_->{menuitem}->get_parent and $m->remove($_->{menuitem}) foreach values %wireless_networks;
}
$menu->destroy;
}
$menu = generate_menu($state_type, $interface);
}
}
sub generate_menu {
my ($state_type, $interface) = @_;
$img->set_from_pixbuf($pixbufs{state}{$state_type eq 'connected' ? 'connected' : 'disconnected'});
gtkset_tip(Gtk2::Tooltips->new, $eventbox, formatAlaTeX(common::sprintf_fixutf8(translate($tooltips{$state_type}), $interface)));
my $menu = Gtk2::Menu->new;
my $create_item = sub {
my ($action) = @_;
my $name = ref($actions{$action}{name}) eq 'CODE' ? $actions{$action}{name}->($interface) : $actions{$action}{name};
my $launch = $actions{$action}{launch};
my @choices = exists $actions{$action}{choices} ? $actions{$action}{choices}->() : ();
my $w;
if (@choices == 0) {
$w = gtksignal_connect(gtkshow(Gtk2::MenuItem->new_with_label($name)), activate => sub { $launch->($interface) });
} elsif (@choices > 1) {
my $selected = $actions{$action}{choice_selected};
$w = gtkshow(create_menu($name, map {
my $choice = $_;
my $w = gtkshow(gtkset_active(Gtk2::CheckMenuItem->new_with_label($choice), $selected->($choice)));
gtksignal_connect($w, activate => sub { $launch->($choice) });
$w->set_draw_as_radio(1);
$w;
} $actions{$action}{choices}->()));
}
#- don't add submenu if only one choice exists
$w;
};
my (@settings);
my $has_wireless = detect_devices::has_wireless();
if ($state_type eq 'connected') {
$menu->append($create_item->($_)) foreach qw(downNetwork monitorNetwork monitorAFW);
} elsif ($state_type eq 'disconnected') {
$menu->append($create_item->('upNetwork'));
}
$menu->append($create_item->('confNetwork'));
if ($state_type ne 'notconfigured') {
$menu->append($create_item->('wireless')) if $has_wireless;
push @settings, $create_item->('chooseInterface');
}
push @settings, $create_item->('chooseProfile');
my $interactive;
eval { $interactive = $ifw->get_interactive };
if (defined $interactive) {
$interactive_cb = gtkshow(gtksignal_connect(gtkset_active(Gtk2::CheckMenuItem->new_with_label(N("Interactive intrusion detection")),
$interactive),
toggled => sub { eval { $ifw->set_interactive(to_bool($_[0]->get_active)) } }));
push @settings, $interactive_cb;
}
push @settings, gtkshow(gtksignal_connect(gtkset_active(Gtk2::CheckMenuItem->new_with_label(N("Always launch on startup")), getAutoStart()),
toggled => sub { setAutoStart(uc(bool2text($_[0]->get_active))) }));
$menu->append(gtkshow(Gtk2::SeparatorMenuItem->new));
$has_wireless and $menu->append(gtkshow($wireless_menu = create_menu(N("Wireless networks"),
map { $_->{menuitem} } values %wireless_networks)));
$menu->append(gtkshow(create_menu(N("Settings"), grep { $_ } @settings)));
$menu->append(gtkshow(Gtk2::SeparatorMenuItem->new));
$menu->append($create_item->('help'));
$menu->append($create_item->('quit'));
$menu;
}
sub mainQuit() {
Glib::Source->remove($timeout) if $timeout;
Glib::Source->remove($update_timeout) if $update_timeout;
Gtk2->main_quit;
}
sub getAutoStart() {
my %p = getVarsFromSh($onstartupfile);
return to_bool($p{AUTOSTART} ne 'FALSE');
}
sub setAutoStart {
my $state = shift;
output_p $onstartupfile,
qq(AUTOSTART=$state
);
}
sub get_unprocessed_attacks() {
my @packets = eval { $ifw->get_reports };
while (my @attack = splice(@packets, 0, 9)) {
handle_attack(@attack);
}
}
sub handle_attack {
my $attack = { mapn { $_[0] => $_[1] } [ 'seq', 'timestamp', 'indev', 'prefix', 'sensor', 'protocol', 'addr', 'port', 'icmp_type' ], \@_ };
$attack->{ip_addr} = network::ifw::get_ip_address($attack->{addr});
$attack->{hostname} = network::ifw::resolve_address($attack->{ip_addr});
$attack->{protocol} = network::ifw::get_protocol($attack->{protocol});
$attack->{service} = network::ifw::get_service($attack->{port});
$attack->{msg} = $attack->{prefix} eq "SCAN" ? N("A port scanning attack has been attempted by %s.", $attack->{hostname})
: $attack->{prefix} eq "SERV" ? N("The %s service has been attacked by %s.", $attack->{service}, $attack->{hostname})
: $attack->{prefix} eq "PASS" ? N("A password cracking attack has been attempted by %s.", $attack->{hostname})
: undef;
push @attacks_queue, $attack;
@attacks_queue == 1 and notify_attack($attacks_queue[0]);
}
sub set_blacklist_verdict {
my ($seq, $verdict) = @_;
#- default is to blacklist
defined $verdict or $verdict = 1;
eval { $ifw->blacklist($seq, $verdict) };
$@ and err_dialog(N("Interactive Firewall"), N("Unable to contact daemon"));
shift @attacks_queue;
#- wait for some time so that the new balloon is noticeable
@attacks_queue and Glib::Timeout->add(500, sub { notify_attack($attacks_queue[0]); 0 });
}
sub notify_attack {
my ($attack) = @_;
unless ($attack->{msg}) {
print "unhandled attack type, skipping\n";
return;
}
Gtk2::Balloon::show_text($balloon, $attack->{msg}, 5000);
}
sub ask_attack_verdict {
my ($attack) = @_;
my $w = ugtk2->new(N("Interactive Firewall: intrusion detected"),
icon => "/usr/lib/libDrakX/icons/drakfirewall.png");
my ($yes, $no, $auto);
gtkadd($w->{window},
gtknew('VBox', spacing => 5, children_loose => [
gtknew('HBox', children => [
0, Gtk2::Image->new_from_stock('gtk-dialog-warning', 'dialog'),
0, gtknew('Label', text => " "),
1, gtknew('VBox', children => [
0, $attack->{msg},
0, N("Do you want to blacklist the attacker?")
])
]),
gtksignal_connect(gtkadd(Gtk2::Expander->new(N("Attack details")),
gtknew('HBox', children => [
0, gtknew('Label', text => " "),
1, gtknew('VBox', children_loose => [
N("Attack time: %s", network::ifw::format_date($attack->{timestamp})),
N("Network interface: %s", $attack->{indev}),
N("Attack type: %s", $attack->{prefix}),
if_($attack->{protocol}, N("Protocol: %s", $attack->{protocol})),
N("Attacker IP address: %s", $attack->{ip_addr}),
if_($attack->{hostname} ne $attack->{ip_addr}, N("Attacker hostname: %s", $attack->{hostname})),
(
$attack->{service} ne $attack->{port} ?
N("Service attacked: %s", $attack->{service}) :
N("Port attacked: %s", $attack->{port}),
),
if_($attack->{icmp_type}, N("Type of ICMP attack: %s", $attack->{icmp_type}))
]),
])),
activate => sub { $_[0]->get_expanded and $w->shrink_topwindow }
),
$auto = gtknew('CheckButton', text => N("Always blacklist (do not ask again)"), toggled => sub {
$no->set_sensitive(!$_[0]->get_active);
}),
gtknew('HButtonBox', layout => 'edge', children_loose => [
$no = gtknew('Button', text => N("No"), clicked => sub {
$w->destroy;
set_blacklist_verdict($attack->{seq}, 0);
}),
$yes = gtknew('Button', text => N("Yes"), clicked => sub {
$auto->get_active and $interactive_cb->set_active(0);
$w->destroy;
set_blacklist_verdict($attack->{seq}, 1);
})
]),
]));
$yes->grab_focus;
gtksignal_connect($w->{window}, delete_event => sub {
$auto->get_active and $interactive_cb->set_active(0);
#- apply default policy
set_blacklist_verdict($attack->{seq}, undef);
});
$w->{window}->show_all;
}
package Gtk2::Balloon;
use ugtk2 qw(:wrappers);
sub new_from_window {
my ($_class, $window) = @_;
my $w = Gtk2::Window->new('GTK_WINDOW_POPUP');
$w->{ref_window} = $window;
$w->set_name("gtk-tooltips");
$w->set_app_paintable(1);
$w->set_resizable(0);
$w->set_border_width(4);
$w->signal_connect("expose_event" => sub {
my $req = $w->size_request;
$w->get_style->paint_flat_box($w->window, 'normal', 'out', undef, $w, "tooltip",
0, 0, $req->width, $req->height);
});
ugtk2::gtkadd($w, $w->{label} = ugtk2::gtkset_alignment(ugtk2::gtkset_line_wrap(Gtk2::Label->new, 1), 0.5, 0.5));
}
sub show_text {
my ($balloon, $text, $timeout) = @_;
$balloon->{label}->set_text($text);
$balloon->{label}->show;
my ($ref_x, $ref_y) = $balloon->{ref_window}->get_origin;
my (undef, undef, $ref_w, $ref_h) = $balloon->{ref_window}->get_geometry;
my ($x, $y) = ($ref_x, $ref_y);
my $req = $balloon->size_request;
my ($w, $h) = ($req->width, $req->height);
#- code mostly from gtktooltips.c
$x += $ref_w / 2 - $w / 2 - $balloon->get_border_width;
my $screen = Gtk2::Gdk::Screen->get_default;
my $monitor_num = $screen->get_monitor_at_window($balloon->{ref_window});
my ($monitor) = $screen->get_monitor_geometry($monitor_num);
if ($x + $w > $monitor->x + $monitor->width) {
$x = $monitor->x + $monitor->width - $w - $balloon->get_border_width;
} elsif ($x < $monitor->x) {
$x = $monitor->x + $balloon->get_border_width;
}
if ($y + $h + $ref_h + $balloon->get_border_width > $monitor->y + $monitor->height) {
$y = $y - $h - $balloon->get_border_width;
} else {
$y += $ref_h + $balloon->get_border_width;
}
$balloon->move($x, $y);
$balloon->show;
$balloon->{timeout} = Glib::Timeout->add($timeout, sub { $balloon->hide; 0 });
}
sub hide_text {
my ($balloon) = @_;
Glib::Source->remove($balloon->{timeout});
$balloon->hide;
}
1;
|