1 |
#!/usr/bin/perl |
2 |
|
3 |
# DrakConnect $Id: drakconnect 239415 2008-03-14 11:06:49Z blino $ |
4 |
|
5 |
# Copyright (C) 1999-2006 Mandriva |
6 |
# Damien "Dam's" Krotkine |
7 |
# Damien "poulpy" Chaumette |
8 |
# Thierry Vignaud <tvignaud@mandriva.com> |
9 |
# |
10 |
# This program is free software; you can redistribute it and/or modify |
11 |
# it under the terms of the GNU General Public License as published by |
12 |
# the Free Software Foundation; either version 2, or (at your option) |
13 |
# any later version. |
14 |
# |
15 |
# This program is distributed in the hope that it will be useful, |
16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
# GNU General Public License for more details. |
19 |
# |
20 |
# You should have received a copy of the GNU General Public License |
21 |
# along with this program; if not, write to the Free Software |
22 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
|
24 |
use strict; |
25 |
use lib qw(/usr/lib/libDrakX); |
26 |
# i18n: IMPORTANT: to get correct namespace (drakx-net instead of libDrakX) |
27 |
BEGIN { unshift @::textdomains, 'drakx-net' } |
28 |
use standalone; #- warning, standalone must be loaded very first, for 'explanations' |
29 |
use common; |
30 |
use interactive; |
31 |
use ugtk2 qw(:create :dialogs :helpers :wrappers); |
32 |
use mygtk2 qw(gtknew); |
33 |
use network::netconnect; |
34 |
use network::connection::ethernet; |
35 |
|
36 |
$ugtk2::wm_icon = "drakconnect"; |
37 |
my $in = 'interactive'->vnew('su'); |
38 |
|
39 |
my $net = {}; |
40 |
network::network::read_net_conf($net); |
41 |
my $modules_conf = modules::any_conf->read; |
42 |
|
43 |
my @all_cards; |
44 |
|
45 |
my $window1 = ugtk2->new(N("Network configuration (%d adapters)", scalar @all_cards)); |
46 |
$window1->{rwindow}->signal_connect(delete_event => sub { ugtk2->exit(0) }); |
47 |
unless ($::isEmbedded) { |
48 |
$window1->{rwindow}->set_position('center'); |
49 |
$window1->{rwindow}->set_size_request(-1, -1); |
50 |
} |
51 |
$window1->{rwindow}->set_border_width(10); |
52 |
|
53 |
my $button_apply; |
54 |
|
55 |
|
56 |
my $hostname = chomp_(`hostname`); |
57 |
|
58 |
my $tree_model = Gtk2::TreeStore->new("Gtk2::Gdk::Pixbuf", map { "Glib::String" } 2..6); |
59 |
my $list = Gtk2::TreeView->new_with_model($tree_model); |
60 |
$list->append_column(Gtk2::TreeViewColumn->new_with_attributes(undef, Gtk2::CellRendererPixbuf->new, 'pixbuf' => 0)); |
61 |
each_index { |
62 |
$list->append_column(my $col = Gtk2::TreeViewColumn->new_with_attributes($_, Gtk2::CellRendererText->new, 'text' => $::i + 1)); |
63 |
$col->set_sort_column_id($::i); |
64 |
} (N("Interface"), N("IP address"), N("Protocol"), N("Driver"), N("State")); |
65 |
|
66 |
$list->signal_connect(button_press_event => sub { |
67 |
my (undef, $event) = @_; |
68 |
my (undef, $iter) = $list->get_selection->get_selected; |
69 |
return unless $iter; |
70 |
configure_lan() if $event->type eq '2button-press'; |
71 |
}); |
72 |
|
73 |
update_list($modules_conf); |
74 |
|
75 |
my $label_host; |
76 |
$window1->{window}->add( |
77 |
gtkpack__(Gtk2::VBox->new(0,10), |
78 |
gtkpack(Gtk2::HBox->new, |
79 |
Gtk2::Label->new(N("Hostname: ")), |
80 |
$label_host = Gtk2::Label->new($hostname), |
81 |
gtksignal_connect(Gtk2::Button->new(N("Configure hostname...")), |
82 |
clicked => sub { |
83 |
local ($::isWizard, $::Wizard_finished) = (1, 1); |
84 |
eval { # For wizcancel |
85 |
network::netconnect::real_main($net, $in, $modules_conf); |
86 |
$button_apply->set_sensitive(1); |
87 |
update(); |
88 |
}; |
89 |
if ($@ =~ /wizcancel/) {} |
90 |
$::WizardWindow->destroy; |
91 |
undef $::WizardWindow; |
92 |
} |
93 |
), |
94 |
), |
95 |
gtkadd(gtkcreate_frame(N("LAN configuration")), |
96 |
gtkpack_(gtkset_border_width(Gtk2::VBox->new(0,0), 5), |
97 |
0, $list, |
98 |
0, Gtk2::HBox->new(0,0), |
99 |
0, gtkpack_(Gtk2::HBox->new(0, 0), |
100 |
0, gtksignal_connect(Gtk2::Button->new(N("Configure Local Area Network...")), |
101 |
clicked => \&configure_lan), |
102 |
), |
103 |
) |
104 |
), |
105 |
gtkpack(Gtk2::HButtonBox->new, |
106 |
gtksignal_connect(Gtk2::Button->new(N("Help")), clicked => sub { |
107 |
run_program::raw({ detach => 1 }, 'drakhelp', '--id', 'internet-connection') }), |
108 |
$button_apply = gtksignal_connect(gtkset_sensitive(Gtk2::Button->new(N("Apply")), 0), |
109 |
clicked => \&apply), |
110 |
gtksignal_connect(Gtk2::Button->new(N("Cancel")), clicked => \&quit_global), |
111 |
gtksignal_connect(Gtk2::Button->new(N("Ok")), clicked => sub { |
112 |
if ($button_apply->get('sensitive')) { |
113 |
my $dialog = _create_dialog(N("Please wait")); |
114 |
gtkpack($dialog->vbox, |
115 |
Gtk2::Label->new(N("Please Wait... Applying the configuration"))); |
116 |
$dialog->show_all; |
117 |
gtkflush(); |
118 |
apply(); |
119 |
$dialog->destroy; |
120 |
} |
121 |
update(); |
122 |
quit_global(); |
123 |
}), |
124 |
), |
125 |
), |
126 |
); |
127 |
|
128 |
|
129 |
|
130 |
$window1->{rwindow}->show_all; |
131 |
gtkflush(); |
132 |
$window1->main; |
133 |
ugtk2->exit(0); |
134 |
|
135 |
sub quit_global() { |
136 |
ugtk2->exit(0); |
137 |
} |
138 |
|
139 |
sub get_intf_status { |
140 |
my ($c) = @_; |
141 |
ethisup($c) ? N("Deactivate now") : N("Activate now"); |
142 |
} |
143 |
|
144 |
my %intf; |
145 |
sub update_list { |
146 |
my ($modules_conf) = @_; |
147 |
@all_cards = network::connection::ethernet::get_eth_cards($modules_conf); |
148 |
my %new_intf = map { @$_ } @all_cards; |
149 |
my @new_intf = sort keys %new_intf; |
150 |
foreach my $interface (difference2(\@new_intf, [ keys %intf ])) { |
151 |
$intf{$interface} = $tree_model->append(undef); |
152 |
} |
153 |
foreach my $interface (@new_intf) { |
154 |
my ($ip, $state) = get_intf_ip($interface); |
155 |
$tree_model->set($intf{$interface}, map_index { $::i => $_ } (gtkcreate_pixbuf("eth_card_mini2"), $interface, $ip , $net->{ifcfg}{$interface}{BOOTPROTO}, $new_intf{$interface}, $state)); |
156 |
} |
157 |
foreach my $i (difference2([ keys %intf ], \@new_intf)) { |
158 |
$tree_model->remove($intf{$i}); |
159 |
delete $intf{$i}; |
160 |
} |
161 |
} |
162 |
|
163 |
sub update() { |
164 |
my $h = chomp_(`hostname`); |
165 |
$label_host->set_label($h); |
166 |
update_list($modules_conf); |
167 |
1; |
168 |
} |
169 |
|
170 |
sub configure_lan() { |
171 |
my $window = _create_dialog(N("LAN configuration")); |
172 |
my @card_tab; |
173 |
|
174 |
if (@all_cards < 1) { |
175 |
$window->vbox->add(Gtk2::Label->new(N("You do not have any configured interface. |
176 |
Configure them first by clicking on 'Configure'"))); |
177 |
gtkpack(gtkset_layout($window->action_area, 'end'), |
178 |
gtksignal_connect(Gtk2::Button->new(N("Ok")), |
179 |
clicked => sub { Gtk2->main_quit }) |
180 |
); |
181 |
$window->show_all; |
182 |
$window->run; |
183 |
$window->destroy; |
184 |
return; |
185 |
} |
186 |
|
187 |
$window->set_border_width(10); |
188 |
gtkpack($window->vbox, |
189 |
Gtk2::Label->new(N("LAN Configuration")), |
190 |
my $notebook = Gtk2::Notebook->new, |
191 |
); |
192 |
|
193 |
foreach (0..$#all_cards) { |
194 |
my @infos; |
195 |
my @conf_data; |
196 |
$card_tab[2*$_] = \@infos; |
197 |
$card_tab[2*$_+1] = \@conf_data; |
198 |
|
199 |
my $vbox_local = Gtk2::VBox->new(0,0); |
200 |
$vbox_local->set_border_width(10); |
201 |
$vbox_local->pack_start(Gtk2::Label->new(N("Adapter %s: %s", $_+1 , $all_cards[$_][0])),1,1,0); |
202 |
# Eth${_}Hostname = $netc->{HOSTNAME} |
203 |
# Eth${_}HostAlias = " . do { $netc->{HOSTNAME} =~ /([^\.]*)\./; $1 } . " |
204 |
# Eth${_}Driver = $all_cards[$_]->[1] |
205 |
my $interface = $all_cards[$_][0]; |
206 |
my ($ip, undef, $mask) = get_intf_ip($interface); |
207 |
$mask ||= $net->{ifcfg}{$interface}{NETMASK}; |
208 |
@conf_data = ([ N("IP address"), \$ip ], |
209 |
[ N("Netmask"), \$mask ], |
210 |
[ N("Boot Protocol"), \$net->{ifcg}{$interface}{BOOTPROTO}, ["static", "dhcp", "bootp"] ], |
211 |
[ N("Started on boot"), \$net->{ifcg}{$interface}{ONBOOT} , ["yes", "no"] ], |
212 |
[ N("DHCP client"), \$net->{ifcfg}{$interface}{DHCP_CLIENT} ] |
213 |
); |
214 |
my $i = 0; |
215 |
my $size_group = Gtk2::SizeGroup->new('horizontal'); |
216 |
|
217 |
foreach my $j (@conf_data) { |
218 |
my $l = Gtk2::Label->new($j->[0]); |
219 |
$l->set_justify('left'); |
220 |
$infos[2*$i] = gtkpack_(Gtk2::HBox->new, |
221 |
1, $l); |
222 |
$vbox_local->pack_start($infos[2*$i], 1, 1, 0); |
223 |
my $c; |
224 |
if (defined $j->[2]) { |
225 |
$c = Gtk2::ComboBox->new_text; |
226 |
$c->set_popdown_strings(@{$j->[2]}); |
227 |
$infos[2*$i+1] = $c->entry; |
228 |
$infos[2*$i]->pack_start($c,0,0,0); |
229 |
} else { |
230 |
$infos[2*$i+1] = ($c = Gtk2::Entry->new); |
231 |
$infos[2*$i]->pack_start($infos[2*$i+1],0,0,0); |
232 |
} |
233 |
$size_group->add_widget($c); |
234 |
$infos[2*$i+1]->set_text(${$j->[1]}); |
235 |
$i++; |
236 |
} |
237 |
|
238 |
my $widget_temp; |
239 |
if (-e "/etc/sysconfig/network-scripts/ifcfg-$interface") { |
240 |
$widget_temp = gtksignal_connect(Gtk2::Button->new(get_intf_status($interface)), |
241 |
clicked => sub { |
242 |
system("/sbin/if" . (ethisup($interface) ? "down" : "up") . " $interface"); |
243 |
$_[0]->set_label(get_intf_status($interface)); |
244 |
update(); |
245 |
}); |
246 |
} else { |
247 |
$widget_temp = N("This interface has not been configured yet.\nRun the \"%s\" assistant from the Mageia Linux Control Center", N("Set up a new network interface (LAN, ISDN, ADSL, ...)")); |
248 |
} |
249 |
$vbox_local->pack_start(gtkpack__(Gtk2::HBox->new(0,0), |
250 |
$widget_temp |
251 |
),0,0,0); |
252 |
# $list->append($_+1, $interface, $intf->{$interface}{IPADDR}, $intf->{$interface}{BOOTPROTO}, $all_cards[$_]->[1]); |
253 |
# $list->set_selectable($_, 0); |
254 |
$notebook->append_page($vbox_local, Gtk2::Label->new($interface)); |
255 |
} |
256 |
|
257 |
my $exit_dialogsub = sub { |
258 |
$window->destroy; |
259 |
Gtk2->main_quit; |
260 |
}; |
261 |
|
262 |
gtkpack($window->action_area, |
263 |
gtksignal_connect(Gtk2::Button->new(N("Cancel")), |
264 |
clicked => $exit_dialogsub), |
265 |
gtksignal_connect(Gtk2::Button->new(N("Ok")), clicked => sub { |
266 |
foreach (0..$#all_cards) { |
267 |
my @infos = @{$card_tab[2*$_]}; |
268 |
each_index { ${$_->[1]} = $infos[2*$::i+1]->get_text } @{$card_tab[2*$_+1]}; |
269 |
my $interface = $all_cards[$_][0]; |
270 |
if ($net->{ifcfg}{$interface}{BOOTPROTO} ne "static") { |
271 |
delete @{$net->{ifcfg}{$interface}}{qw(IPADDR NETWORK NETMASK BROADCAST)}; |
272 |
} else { |
273 |
if ($infos[1]->get_text ne N("No IP")) { |
274 |
$net->{ifcfg}{$interface}{IPADDR} = $infos[1]->get_text; |
275 |
$net->{ifcfg}{$interface}{NETMASK} = $infos[3]->get_text; |
276 |
} |
277 |
} |
278 |
} |
279 |
update(); |
280 |
$button_apply->set_sensitive(1); |
281 |
$exit_dialogsub->(); |
282 |
}), |
283 |
); |
284 |
|
285 |
$window->show_all; |
286 |
foreach (0..$#all_cards) { |
287 |
my @infos = @{$card_tab[2*$_]}; |
288 |
$net->{ifcfg}{$all_cards[$_][0]}{BOOTPROTO} eq "dhcp" or $infos[8]->hide; |
289 |
} |
290 |
$window->run; |
291 |
} |
292 |
|
293 |
sub apply() { |
294 |
network::network::configure_network($net, $in, $modules_conf); |
295 |
} |
296 |
|
297 |
sub ethisup { `LC_ALL=C LANGUAGE=C /sbin/ifconfig $_[0]` =~ /inet/ } |
298 |
|
299 |
# FIXME: duplicated with drakconnect |
300 |
sub get_intf_ip { |
301 |
my ($interface) = @_; |
302 |
my ($ip, $state, $mask); |
303 |
if (-x "/sbin/ifconfig") { |
304 |
local $_ = `LC_ALL=C LANGUAGE=C /sbin/ifconfig $interface`; |
305 |
$ip = /inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/mso ? $1 : N("No IP"); |
306 |
$mask = /Mask:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/mso ? $1 : N("No Mask"); |
307 |
$state = /inet/ ? N("up") : N("down"); |
308 |
} else { |
309 |
$ip = $net->{ifcfg}{$interface}{IPADDR}; |
310 |
$state = "n/a"; |
311 |
} |
312 |
($ip, $state, $mask); |
313 |
} |