summaryrefslogtreecommitdiffstats
path: root/perl-install/standalone/drakroam
blob: a748a0c5c70a28943f1fddd0e8daba0172ddee98 (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
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
#!/usr/bin/perl

# drakroam: wireless network roaming GUI
# beta version uses wlandetect as backend
# Austin Acton, 2004
# <austin@mandrake.org>
# Licensed under the GPL

# problems
# - deletes comments in config file
# - expects an ifcfg file for static IP configurations (not uncommon)
# - roaming status fails (no idea why)
#   maybe same reason bash-completion killall can not see wlandetect?

# todo (wlandetect version)
# - make known and available lists have more rows by default (why so small?)
# - refresh status every x seconds
# - find a good way to drop the access point and resume roaming
# - make 'key' column wider by default
# todo (waproamd version)
# - listen to dbus for pings from waproamd; update all on receiving a dbus ping
# - setup static network configurations
# - handle keys (can key file be named after ESSID?)
# - should files be named as MAC or as essid:ESSID ?

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

use standalone;
use common;
use run_program;

use Glib qw(TRUE FALSE);
use ugtk2 qw(:create :helpers :wrappers);
use Gtk2::SimpleList;
use Socket;

require_root_capability();

# global settings
my $route = '/sbin/route -n';
my $IWList = '/sbin/iwlist';
my $IWConfig = '/sbin/iwconfig';
my $IFConfig = '/sbin/ifconfig';
my $IFUp = '/sbin/ifup';
my $IFDown = '/sbin/ifdown';
my $DHClient = '/sbin/dhclient';
my $enable_roaming = member('--roaming', @ARGV); #- not tested yet ...

# initialize variables
my $ScanInterval = 30; # tell deamon to search for new nets every x seconds

my ($KnownList, $AvailableList);
my $device;

my %available_roaming_daemons = (
		       wlandetect => {
				      config_location => '/etc/wlandetect.conf',
				      binary => '/usr/sbin/wlandetect',
				      start_options => sub {
					  my ($interval, undef) = @_;
					  "-d -t $interval";
				      },
				      read_config => sub {
					  my ($config) = @_;
					  each_index {
					      /^#/ || /^\n/ and next; #ignore comments and blank lines
					      if (/^(\S+)\s+(.*)$/) {
						  my ($essid, $mode, $channel, $dhcp, $key);
						  my $command = $2;
						  # setup new network entry
						  $essid = $1;
						  ($mode) = $command =~ /mode\s([^\s;]+)/;
						  ($channel) = $command =~ /channel\s([^\s;]+)/;
						  ($key) = $command =~ /key\s([^\s;]+)/;
						  $dhcp = $command =~ /dhclient/;
						  &AddNet($essid, $mode, $channel, $dhcp, $key);
					      }
					      else { die "Line $::i of configuration file is not parseable.\n" }
					  } cat_($config);
				      },
				      write_config => sub {
					  my ($config) = @_;
					  my @contents = (
							  "#wlandetect configuration file\n",
							  "#format: essid<tab><tab>commands\n",
							  "#use \@DEV\@ for device name\n"
							 );
					  foreach my $row (@{$KnownList->{data}}) { # again, lame
					      my $essid = $row->[0];
					      my $iwc = join(' ', $IWConfig, "essid $essid",
							     if_($row->[1], "mode $row->[1]"),
							     if_($row->[2], "channel $row->[2]"),
							     if_($row->[4], "key $row->[4]"));
					      my $ifc = $row->[3] ? "$IFConfig \@DEV\@ up; $DHClient \@DEV\@" : "$IFUp \@DEV\@";
					      push @contents, "$essid\t\t$iwc; $ifc\n";
					  }
					  output_p($config, @contents);
				      },
				      add_net => sub {},
				      remove_net => sub {},
				     },
		       waproamd => {
				      config_location => '/etc/waproamd/scripts',
				      binary => '/usr/sbin/waproamd',
				      start_options => sub {
					  my ($interval, $device) = @_;
					  "-i $device -t $interval";
				      },
				      read_config => sub {
					  my ($config) = @_;
					  foreach my $net (all($config)) {
					      $net eq "default" or next;
					      print "Adding net $net\n" if $::testing;
					      push @{$KnownList->{data}}, [$net];
					  }
				      },
				      write_config => sub {},
				      add_net => sub {
					  my ($config, $essid) = @_;
					  output_p("$config/$essid", qq(# essid specific config file));
				      },
				      remove_net => sub {
					  my ($config, $essid) = @_;
					  system("rm -f $config/$essid");
				      },
				     },
);

my $roaming_daemon = $available_roaming_daemons{wlandetect};

my $ScanEntry = Gtk2::Entry->new;
$ScanEntry->set_width_chars(4);
$ScanEntry->set_text($ScanInterval);

$KnownList = Gtk2::SimpleList->new(
                                      N("ESSID") => "text",
                                      N("Mode") => "text",
                                      N("Channel") => "text",
                                      N("DHCP") => "bool",
                                      N("Key") => "text"
                                     );
$KnownList->get_selection->set_mode('single');
$KnownList->set_reorderable(1);
$KnownList->set_column_editable(1, TRUE); # allow to change mode
$KnownList->set_column_editable(2, TRUE); # allow to change channel
$KnownList->set_column_editable(4, TRUE); # allow to change key

$AvailableList = Gtk2::SimpleList->new(
                                          "ESSID" => "text",
                                          "Type" => "text",
                                          "Encryption" => "text",
                                          "Signal (%)" => "int"
                                         );
$AvailableList->get_selection->set_mode('single');

my $NetLabel = Gtk2::WrappedLabel->new(N("Network:"));
my $IpLabel = Gtk2::WrappedLabel->new(N("IP:"));
my $GwLabel = Gtk2::WrappedLabel->new(N("Gateway:"));
my $ModeLabel = Gtk2::WrappedLabel->new(N("Mode:"));
my $WepLabel = Gtk2::WrappedLabel->new(N("Encryption:"));
my $SignalLabel = Gtk2::WrappedLabel->new(N("Signal:"));

my $net_field   = Gtk2::WrappedLabel->new;
my $mode_field  = Gtk2::WrappedLabel->new;
my $ip_field    = Gtk2::WrappedLabel->new;
my $wep_field   = Gtk2::WrappedLabel->new;
my $gw_field    = Gtk2::WrappedLabel->new;
my $sig_field   = Gtk2::WrappedLabel->new;


my $w = ugtk2->new('Drakroam');
gtkadd(gtkset_border_width($w->{window}, 2),
       gtkpack_(Gtk2::VBox->new,
                if_($enable_roaming,
                    0, gtkadd(gtkset_border_width(Gtk2::Frame->new(N("Roaming")), 2),
                          gtkpack(create_hbox(),
                                  gtkpack(Gtk2::VBox->new,
                                          my $RoamStatus = Gtk2::Label->new(N("Roaming: %s", N("off"))),
                                          gtkpack(create_hbox(),
						  gtksignal_connect(Gtk2::Button->new(N("Start")), clicked => sub { &StartRoam }),
						  gtksignal_connect(Gtk2::Button->new(N("Stop")), clicked => sub { &StopRoam })
                                                 )
                                         ),
                                  gtkpack(Gtk2::VBox->new,
                                          Gtk2::Label->new(N("Scan interval (sec): ")),
                                          gtkpack(Gtk2::HBox->new,
                                                  $ScanEntry,
                                                  gtksignal_connect(Gtk2::Button->new(N("Set")), clicked => sub { &SetInterval })
                                                 )
                                         )
                                 )
                         )),
                1, gtkadd(gtkset_border_width(Gtk2::Frame->new(N("Known Networks (Drag up/down or edit)")), 2),
                          gtkpack_(Gtk2::VBox->new,
                                   1, create_scrolled_window($KnownList),
                                   0, gtkpack(create_hbox(),
					      gtksignal_connect(Gtk2::Button->new(N("Remove")), clicked => sub {
								    my ($selected) = $KnownList->get_selected_indices;
								    &RemoveNet($selected);
								}),
					      gtksignal_connect(Gtk2::Button->new(N("Connect")), clicked => sub {
								    my ($selected) = $KnownList->get_selected_indices;
								    &ConnectNow($selected);
								}),
					      gtksignal_connect(Gtk2::Button->new(N("Save")), clicked => sub { &WriteConfig })
                                             )
                                  )
                         ),
                1, gtkadd(gtkset_border_width(Gtk2::Frame->new(N("Available Networks")), 2),
                          gtkpack_(Gtk2::VBox->new,
                                   1, create_scrolled_window($AvailableList),
                                   0, gtkpack(create_hbox(),
					      gtksignal_connect(Gtk2::Button->new(N("Add")), clicked => sub {
								    my @selected = $AvailableList->get_selected_indices or return;
								    my ($mode, $channel, $key);
								    my $essid = $AvailableList->{data}["@selected"][0];
								    my $dhcp = 1; # assume dhcp for new networks
								    &AddNet($essid, $mode, $channel, $dhcp, $key);
								}),
					      gtksignal_connect(Gtk2::Button->new(N("Rescan")), clicked => sub { &UpdateAvailable })
                                             )
                                  )
                         ),
                0, gtkadd(gtkset_border_width(Gtk2::Frame->new(N("Status")), 2),
                          gtkpack(Gtk2::VBox->new,
                                  create_packtable({ col_spacings => 5, row_spacings => 5, homogenous => 1 },
                                                   [ $NetLabel,  $net_field,  $IpLabel,  $ip_field,  $GwLabel,     $gw_field ],
                                                   [ $ModeLabel, $mode_field, $WepLabel, $wep_field, $SignalLabel, $sig_field ],
                                                  ),
                                  gtkpack(create_hbox(),
                                          gtksignal_connect(Gtk2::Button->new(N("Disconnect")), clicked => sub { &Disconnect }),
                                          gtksignal_connect(Gtk2::Button->new(N("Refresh")), clicked => sub { &UpdateStatus }),
                                          gtksignal_connect(Gtk2::Button->new(N("Close")), clicked => sub { Gtk2->main_quit })
                                         )
                                 )
                         ),
               )
      );

# fill the GUI
&ReadConfig;
&UpdateAll;

sub UpdateAll {
	&UpdateAvailable; #must go first as it defines the device name
	&UpdateStatus;
	&UpdateRoaming;
}

sub isRoamingRunning() {
	my $name = basename($roaming_daemon->{binary});
	any { /\Q$name\E$/ } run_program::get_stdout("ps", "-A");
}

sub UpdateRoaming() {
	my $status = isRoamingRunning() ? N("on") : N("off");
	$RoamStatus->set_text(N("Roaming: %s", $status));
	return FALSE; #- do not update again if launched on timeout
}

sub UpdateStatus() {
	my $CurrentNet = "-";
	my $CurrentIP = "---.---.---.---";
	my $CurrentGW = "---.---.---.---";
	my $CurrentMode = "";
	my $CurrentWEP = "";
	my $CurrentSignal = "-";
	print "Updating\n" if $::testing;
	foreach (run_program::get_stdout($IWConfig, $device)) {
		/ESSID:"(.*?)"/ and $CurrentNet = $1;
		/Mode:(\S*)\s/ and $CurrentMode = $1;
		/key:(\S*)\s/ and $CurrentWEP = $1;
		m!Quality[:=](\S*)/! and $CurrentSignal = $1;
	}
	foreach (run_program::get_stdout($IFConfig, $device)) {
		if (/inet addr:(\S*)\s/) { $CurrentIP = $1 }
	}
	foreach (run_program::get_stdout($route)) {
		#- FIXME: use timeout for DNS resolution, factorize with activefw.pm
		if (/^0.0.0.0\s*(\S*)\s/) { $CurrentGW = gethostbyaddr(inet_aton($1), AF_INET) }
		else { $CurrentGW = "---.---.---.---" }
	}
	$net_field->set_text($CurrentNet);
	$mode_field->set_text($CurrentMode);
	$ip_field->set_text($CurrentIP);
	$wep_field->set_text($CurrentWEP);
	$gw_field->set_text($CurrentGW);
	$sig_field->set_text($CurrentSignal);
}

sub UpdateAvailable() {
	my ($essid, $mode, $wep, $signal);
	print "Running iwlist\n" if $::testing;
	@{$AvailableList->{data}} = ();
	foreach (`$IWList scan 2>/dev/null`) {
		/([^ ]+)([ \t]+)Scan completed :/ and $device = $1;
		/([^ ]+)([ \t]+)No scan results/ and  $device = $1;
		/ESSID:"(.*?)"/ and $essid = $1;
		/Mode:(\S*)/ and $mode = $1;
		m!Quality[:=](\S*)/! and $signal = $1;
		if (/key:(\S*)\s/) {
			$wep = $1;
			print "ESSID: $essid, Mode: $mode, WEP: $wep, Signal: $signal\n" if $::testing;
			push @{$AvailableList->{data}}, [$essid, $mode, $wep, $signal];
		}
	}
}

sub AddNet {
	my ($essid, $mode, $channel, $dhcp, $key) = @_;
	print "Adding net $essid\n" if $::testing;
	push @{$KnownList->{data}}, [ $essid, $mode, $channel, $dhcp, $key ];
	$_->{add_net}($_->{config_location}, $essid) foreach values %available_roaming_daemons;
}

sub RemoveNet {
	my ($selected) = @_;
	my $essid = $KnownList->{data}[$selected][0];
	print "Removing net $essid\n" if $::testing;
	splice @{$KnownList->{data}}, $selected, 1;
	$_->{remove_net}($_->{config_location}, $essid) foreach values %available_roaming_daemons;
}

sub ReadConfig() {
	$_->{read_config}($_->{config_location}) foreach values %available_roaming_daemons;
}

sub WriteConfig() {
	$_->{write_config}($_->{config_location}) foreach values %available_roaming_daemons;
}

sub StartRoam() {
	my $options = $roaming_daemon->{start_options}($ScanInterval, $device);
	my $name = basename($roaming_daemon->{binary});
	system("killall $name; $roaming_daemon->{binary} $options &");
	Glib::Timeout->add(1000, \&UpdateRoaming);
}

sub StopRoam() {
	my $name = basename($roaming_daemon->{binary});
	system("killall $name");
	Glib::Timeout->add(1000, \&UpdateRoaming);
}

sub SetInterval() {
	$ScanInterval = $ScanEntry->get_text;
	if (isRoamingRunning()) {
	    StopRoam();
	    StartRoam();
	}
}

sub ConnectNow {
	my ($row) = @_;
	my @command = "";
	push @command, "$IWConfig $device essid $KnownList->{data}[$row][0] ";
     my %commands = (1 => 'mode', 2 => 'channel', 4 => 'key');
     push @command, map { "$commands{$_} $KnownList->{data}[$row][$_] " } grep { $KnownList->{data}[$row][$_] } keys %commands;
	push @command, "; ";
	if ($KnownList->{data}[$row][3]) {
		push @command, "$IFConfig $device up; $DHClient $device";
	}
	else {
		push @command, "$IFUp $device";
	}
	my $ToBash = join("", @command);
	print "Sending $ToBash\n" if $::testing;
	system($ToBash);
	&UpdateStatus;
}

sub Disconnect {
	print "Dropping $device\n" if $::testing;
	system("$IFDown $device");
	&UpdateStatus;
}

sub Dialog {
    my ($FilePointer) = @_;
    my $content = join('', cat_($FilePointer));
    # dump into a dialog
    my $AboutWindow = Gtk2::Dialog->new(N("Information"), $w->{real_window},
                                        'destroy-with-parent',
                                        N("Ok") => 'none');
    $AboutWindow->vbox->add(create_scrolled_window(Gtk2::Label->new($content)));
    $AboutWindow->signal_connect(response => sub { $_[0]->destroy });
    $AboutWindow->show_all;
}

$w->main;