summaryrefslogtreecommitdiffstats
path: root/perl-install
diff options
context:
space:
mode:
authorThierry Vignaud <tvignaud@mandriva.org>2003-01-13 09:01:31 +0000
committerThierry Vignaud <tvignaud@mandriva.org>2003-01-13 09:01:31 +0000
commit07bdf9c75e71c1ac3530894ba9490309f642086c (patch)
treeec5c59aacd44e7cf705a829c2263ae18847de2f5 /perl-install
parent8ace139532ce4f5d8cba9e1533aeb2db2180a6cb (diff)
downloaddrakx-07bdf9c75e71c1ac3530894ba9490309f642086c.tar
drakx-07bdf9c75e71c1ac3530894ba9490309f642086c.tar.gz
drakx-07bdf9c75e71c1ac3530894ba9490309f642086c.tar.bz2
drakx-07bdf9c75e71c1ac3530894ba9490309f642086c.tar.xz
drakx-07bdf9c75e71c1ac3530894ba9490309f642086c.zip
print_testpages() : factorize and simplify options managment
by using an options hash
Diffstat (limited to 'perl-install')
-rw-r--r--perl-install/printer/printerdrake.pm92
1 files changed, 26 insertions, 66 deletions
diff --git a/perl-install/printer/printerdrake.pm b/perl-install/printer/printerdrake.pm
index bca7ae3ff..af8ed1c76 100644
--- a/perl-install/printer/printerdrake.pm
+++ b/perl-install/printer/printerdrake.pm
@@ -2189,17 +2189,9 @@ sub print_testpages {
my ($printer, $in, $upNetwork) = @_;
$in->set_help('printTestPages') if $::isInstall;
# print test pages
- my $standard = 1;
- my $altletter = 0;
- my $alta4 = 0;
- my $photo = 0;
- my $ascii = 0;
my $res2 = 0;
- my $oldstandard = 1;
- my $oldaltletter = 0;
- my $oldalta4 = 0;
- my $oldphoto = 0;
- my $oldascii = 0;
+ my %options = (alta4 => 0, altletter => 0, ascii => 0, photo => 0, standard => 1);
+ my %old_options = (alta4 => 0, altletter => 0, ascii => 0, photo => 0, standard => 1);
my $oldres2 = 0;
my $res1 = $in->ask_from_(
{ title => N("Test pages"),
@@ -2213,74 +2205,42 @@ Note: the photo test page can take a rather long time to get printed and on lase
changed => sub {
if ($oldres2 ne $res2) {
if ($res2) {
- $standard = 0;
- $altletter = 0;
- $alta4 = 0;
- $photo = 0;
- $ascii = 0;
- $oldstandard = 0;
- $oldaltletter = 0;
- $oldalta4 = 0;
- $oldphoto = 0;
- $oldascii = 0;
+ foreach my $opt (keys %options) {
+ $options{$opt} = 0;
+ $old_options{$opt} = 0;
+ }
}
$oldres2 = $res2;
}
- if ($oldstandard ne $standard) {
- if ($standard) {
- $res2 = 0;
- $oldres2 = 0;
- }
- $oldstandard = $standard;
- }
- if ($oldaltletter ne $altletter) {
- if ($altletter) {
- $res2 = 0;
- $oldres2 = 0;
- }
- $oldaltletter = $altletter;
- }
- if ($oldalta4 ne $alta4) {
- if ($alta4) {
- $res2 = 0;
- $oldres2 = 0;
- }
- $oldalta4 = $alta4;
- }
- if ($oldphoto ne $photo) {
- if ($photo) {
- $res2 = 0;
- $oldres2 = 0;
- }
- $oldphoto = $photo;
- }
- if ($oldascii ne $ascii) {
- if ($ascii) {
- $res2 = 0;
- $oldres2 = 0;
- }
- $oldascii = $ascii;
+ foreach my $opt (keys %options) {
+ if ($old_options{$opt} ne $options{$opt}) {
+ if ($options{$opt}) {
+ $res2 = 0;
+ $oldres2 = 0;
+ }
+ $old_options{standard} = $options{standard};
+ }
}
return 0;
}
} },
[
{ text => N("Standard test page"), type => 'bool',
- val => \$standard },
+ val => \$options{standard} },
if_($::expert,
{ text => N("Alternative test page (Letter)"), type => 'bool',
- val => \$altletter }),
+ val => \$options{altletter} }),
if_($::expert,
{ text => N("Alternative test page (A4)"), type => 'bool',
- val => \$alta4 }),
- { text => N("Photo test page"), type => 'bool', val => \$photo },
+ val => \$options{alta4} }),
+ { text => N("Photo test page"), type => 'bool', val => \$options{photo} },
#{ text => N("Plain text test page"), type => 'bool',
- # val => \$ascii }
+ # val => \$options{ascii} }
if_($::isWizard,
{ text => N("Do not print any test page"), type => 'bool',
val => \$res2 })
]);
- $res2 = 1 if !($standard || $altletter || $alta4 || $photo || $ascii);
+ $res2 = 1 if !($options{standard} || $options{altletter} || $options{alta4} || $options{photo} || $options{ascii});
if ($res1 && !$res2) {
my @lpq_output;
{
@@ -2295,16 +2255,16 @@ Note: the photo test page can take a rather long time to get printed and on lase
my $asciitestpage = "/usr/share/printer-testpages/testpage.asc";
my @testpages;
# Install the filter to convert the photo test page to PS
- if ($printer->{SPOOLER} ne "cups" && $photo && !$::testing &&
+ if ($printer->{SPOOLER} ne "cups" && $options{photo} && !$::testing &&
!files_exist('/usr/bin/convert')) {
$in->do_pkgs->install('ImageMagick');
}
# set up list of pages to print
- $standard && push @testpages, $stdtestpage;
- $altletter && push @testpages, $altlttestpage;
- $alta4 && push @testpages, $alta4testpage;
- $photo && push @testpages, $phototestpage;
- $ascii && push @testpages, $asciitestpage;
+ $options{standard} && push @testpages, $stdtestpage;
+ $options{altletter} && push @testpages, $altlttestpage;
+ $options{alta4} && push @testpages, $alta4testpage;
+ $options{photo} && push @testpages, $phototestpage;
+ $options{ascii} && push @testpages, $asciitestpage;
# print the stuff
@lpq_output = printer::main::print_pages($printer, @testpages);
}
4 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 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
package network::network; # $Id$wir

#-######################################################################################
#- misc imports
#-######################################################################################

use strict;

use Socket;
use common;
use detect_devices;
use run_program;
use network::tools;
use vars qw(@ISA @EXPORT);
use log;

our $network_file = "/etc/sysconfig/network";
my $resolv_file = "/etc/resolv.conf";
my $tmdns_file = "/etc/tmdns.conf";
our $wireless_d = "/etc/sysconfig/network-scripts/wireless.d";


@ISA = qw(Exporter);
@EXPORT = qw(addDefaultRoute dns dnsServers gateway guessHostname is_ip is_ip_forbidden masked_ip netmask resolv);

#- $net hash structure
#-   autodetect
#-   type
#-   net_interface
#-   PROFILE: selected netprofile
#-   network (/etc/sysconfig/network) : NETWORKING FORWARD_IPV4 NETWORKING_IPV6 HOSTNAME GATEWAY GATEWAYDEV NISDOMAIN
#-     NETWORKING : networking flag : string : "yes" by default
#-     FORWARD_IPV4 : forward IP flag : string : "false" by default
#-     HOSTNAME : hostname : string : "localhost.localdomain" by default
#-     GATEWAY : gateway
#-     GATEWAYDEV : gateway interface
#-     NISDOMAIN : nis domain
#-     NETWORKING_IPV6 : use IPv6, "yes" or "no"
#-     IPV6_DEFAULTDEV
#-   resolv (/etc/resolv.conf): dnsServer, dnsServer2, dnsServer3, DOMAINNAME, DOMAINNAME2, DOMAINNAME3
#-     dnsServer : dns server 1
#-     dnsServer2 : dns server 2
#-     dnsServer3 : dns server 3 : note that we uses the dns1 for the LAN, and the 2 others for the internet conx
#-     DOMAINNAME : domainname : string : $net->{network}{HOSTNAME} =~ /\.(.*)/ by default
#-     DOMAINNAME2 : well it's another domainname : have to look further why we used 2
#-   adsl: bus, Encapsulation, vpi, vci provider_id, method, login, passwd, ethernet_device, capi_card
#-   cable: bpalogin, login, passwd
#-   zeroconf: hostname
#-   auth: LDAPDOMAIN WINDOMAIN
#-   ifcfg (/etc/sysconfig/network-scripts/ifcfg-*):
#-     key : device name
#-     value : hash containing ifcfg file values, see write_interface_conf() for an exhaustive list
#-       DHCP_HOSTNAME : If you have a dhcp and want to set the hostname
#-       IPADDR : IP address
#-       NETMASK : netmask
#-       DEVICE : device name
#-       BOOTPROTO : boot prototype : "bootp" or "dhcp" or "pump" or ...
#-       IPV6INIT
#-       IPV6TO4INIT
#-       MS_DNS1
#-       MS_DNS2
#-       DOMAIN

sub read_conf {
    my ($file) = @_;
    +{ getVarsFromSh($file) };
}

sub read_resolv_conf_raw {
    my ($o_file) = @_;
    my $s = cat_($o_file || $::prefix . $resolv_file);
    { nameserver => [ $s =~ /^\s*nameserver\s+(\S+)/mg ],
      search => [ if_($s =~ /^\s*search\s+(.*)/m, split(' ', $1)) ] };
}

sub read_resolv_conf {
    my ($o_file) = @_;
    my $resolv_conf = read_resolv_conf_raw($o_file);
    +{
      (mapn { $_[0] => $_[1] } [ qw(dnsServer dnsServer2 dnsServer3) ], $resolv_conf->{nameserver}),
      (mapn { $_[0] => $_[1] } [ qw(DOMAINNAME DOMAINNAME2 DOMAINNAME3) ], $resolv_conf->{search}),
     };
}

sub read_interface_conf {
    my ($file) = @_;
    my %intf = getVarsFromSh($file);

    $intf{BOOTPROTO} ||= 'static';
    $intf{isPtp} = $intf{NETWORK} eq '255.255.255.255';
    $intf{isUp} = 1;
    \%intf;
}

sub read_zeroconf() {
    cat_($::prefix . $tmdns_file) =~ /^\s*hostname\s*=\s*(\w+)/m && { ZEROCONF_HOSTNAME => $1 };
}

sub write_network_conf {
    my ($net) = @_;

    if ($net->{network}{HOSTNAME} && $net->{network}{HOSTNAME} =~ /\.(.+\..+)$/) {
	$net->{resolv}{DOMAINNAME} ||= $1;
    }
    $net->{network}{NETWORKING} = 'yes';

    setVarsInSh($::prefix . $network_file, $net->{network}, qw(HOSTNAME NETWORKING GATEWAY GATEWAYDEV NISDOMAIN FORWARD_IPV4 NETWORKING_IPV6 IPV6_DEFAULTDEV));
}

sub write_zeroconf {
    my ($net, $in) = @_;
    my $zhostname = $net->{zeroconf}{hostname};
    my $file = $::prefix . $tmdns_file;

    if ($zhostname) {
	$in->do_pkgs->ensure_binary_is_installed('tmdns', 'tmdns', 'auto') if !$in->do_pkgs->is_installed('bind');
	$in->do_pkgs->ensure_binary_is_installed('zcip', 'zcip', 'auto');
    }

    #- write blank hostname even if disabled so that drakconnect does not assume zeroconf is enabled
    eval { substInFile { s/^\s*(hostname)\s*=.*/$1 = $zhostname/ } $file } if $zhostname || -f $file;

    require services;
    services::set_status('tmdns', $net->{zeroconf}{hostname}, $::isInstall);
}

sub write_resolv_conf {
    my ($net) = @_;
    my $resolv = $net->{resolv};
    my $file = $::prefix . $resolv_file;

    my %new = (
        search => [ grep { $_ } uniq(@$resolv{'DOMAINNAME', 'DOMAINNAME2', 'DOMAINNAME3'}) ],
        nameserver => [ grep { $_ } uniq(@$resolv{'dnsServer', 'dnsServer2', 'dnsServer3'}) ],
    );

    my (%prev, @unknown);
    foreach (cat_($file)) {
	s/\s+$//;
	s/^[#\s]*//;

	if (my ($key, $val) = /^(search|nameserver)\s+(.*)$/) {
	    push @{$prev{$key}}, $val;
	} elsif (/^ppp temp entry$/) {
	} elsif (/\S/) {
	    push @unknown, $_;
	}
    }
    unlink $file if -l $file;  #- workaround situation when /etc/resolv.conf is an absolute link to /etc/ppp/resolv.conf or whatever

    if (@{$new{search}} || @{$new{nameserver}}) {
	$prev{$_} = [ difference2($prev{$_} || [], $new{$_}) ] foreach keys %new;

	my @search = do {
	    my @new = if_(@{$new{search}}, "search " . join(' ', @{$new{search}}) . "\n");
	    my @old = if_(@{$prev{search}}, "# search " . join(' ', @{$prev{search}}) . "\n");
	    @new, @old;
	};
	my @nameserver = do {
	    my @new = map { "nameserver $_\n" } @{$new{nameserver}};
	    my @old = map { "# nameserver $_\n" } @{$prev{nameserver}};
	    @new, @old;
	};
	output_with_perm($file, 0644, @search, @nameserver, (map { "# $_\n" } @unknown), "\n# ppp temp entry\n");

	#-res_init();		# reinit the resolver so DNS changes take affect
	1;
    } else {
	log::explanations("neither domain name nor dns server are configured");
	0;
    }
}

sub update_broadcast_and_network {
    my ($intf) = @_;
    my @ip = split '\.', $intf->{IPADDR};
    my @mask = split '\.', $intf->{NETMASK};
    #- FIXME: NETWORK and BROADCAST are deprecated, see sysconfig.txt
    $intf->{BROADCAST} = join('.', mapn { int($_[0]) | ((~int($_[1])) & 255) } \@ip, \@mask);
    $intf->{NETWORK} = join('.', mapn { int($_[0]) &        $_[1]          } \@ip, \@mask);
}

sub write_interface_settings {
    my ($intf, $file) = @_;
    setVarsInSh($file, $intf, qw(DEVICE BOOTPROTO IPADDR NETMASK NETWORK BROADCAST GATEWAY ONBOOT HWADDR METRIC MII_NOT_SUPPORTED TYPE USERCTL ATM_ADDR ETHTOOL_OPTS VLAN MTU DNS1 DNS2 DOMAIN RESOLV_MODS LINK_DETECTION_DELAY),
                qw(WIRELESS_MODE WIRELESS_ESSID WIRELESS_NWID WIRELESS_FREQ WIRELESS_SENS WIRELESS_RATE WIRELESS_ENC_KEY WIRELESS_ENC_MODE WIRELESS_RTS WIRELESS_FRAG WIRELESS_IWCONFIG WIRELESS_IWSPY WIRELESS_IWPRIV WIRELESS_WPA_DRIVER),
                qw(DVB_ADAPTER_ID DVB_NETWORK_DEMUX DVB_NETWORK_PID),
                qw(IPV6INIT IPV6TO4INIT),
                qw(MRU REMIP PPPOPTIONS HARDFLOWCTL DEFABORT RETRYTIMEOUT PAPNAME LINESPEED MODEMPORT DEBUG ESCAPECHARS INITSTRING),
                qw(DISCONNECTTIMEOUT PERSIST DEFROUTE),
                qw(VPN_NAME VPN_TYPE),
                if_($intf->{BOOTPROTO} eq "dhcp", qw(DHCP_CLIENT DHCP_HOSTNAME NEEDHOSTNAME PEERDNS PEERYP PEERNTPD DHCP_TIMEOUT)),
                if_($intf->{DEVICE} =~ /^ippp\d+$/, qw(DIAL_ON_IFUP))
               );
    substInFile { s/^DEVICE='(`.*`)'/DEVICE=$1/g } $file; #- remove quotes if DEVICE is the result of a command
    chmod $intf->{WIRELESS_ENC_KEY} ? 0700 : 0755, $file; #- hide WEP key for non-root users
    log::explanations("written $intf->{DEVICE} interface configuration in $file");
}

sub get_ifcfg_file {
    my ($name) = @_;
    "$::prefix/etc/sysconfig/network-scripts/ifcfg-$name";
}

sub write_interface_conf {
    my ($net, $name) = @_;

    my $file = get_ifcfg_file($name);
    #- prefer ifcfg-XXX files
    unlink("$::prefix/etc/sysconfig/network-scripts/$name");

    my $intf = $net->{ifcfg}{$name};

    require network::connection::ethernet;
    my (undef, $mac_address) = network::connection::ethernet::get_eth_card_mac_address($intf->{DEVICE});
    $intf->{HWADDR} &&= $mac_address; #- set HWADDR to MAC address if required

    update_broadcast_and_network($intf);
    $intf->{ONBOOT} ||= bool2yesno(!member($intf->{DEVICE}, map { $_->{device} } detect_devices::pcmcia_probe()));

    defined($intf->{METRIC}) or $intf->{METRIC} = network::tools::get_default_metric(network::tools::get_interface_type($intf)),
    $intf->{BOOTPROTO} =~ s/dhcp.*/dhcp/;

    write_interface_settings($intf, $file);
}

sub write_wireless_conf {
    my ($ssid, $ifcfg) = @_;
    my $wireless_file = $::prefix . $wireless_d . '/' . $ssid;
    my %wireless_ifcfg = %$ifcfg;
    # FIXME: be smarter to keep only DHCP/IP settings here
    delete $wireless_ifcfg{$_}
      foreach qw(DEVICE MII_NOT_SUPPORTED);
    write_interface_settings(\%wireless_ifcfg, $wireless_file);
}

sub add2hosts {
    my ($hostname, @ips) = @_;
    my ($sub_hostname) = $hostname =~ /(.*?)\./;

    my $file = "$::prefix/etc/hosts";

    my %l;
    foreach (cat_($file)) {
        my ($ip, $aliases) = /^\s*(\S+)\s+(\S+.*)$/ or next;
        push @{$l{$ip}}, difference2([ split /\s+/, $aliases ], [ $hostname, $sub_hostname ]);
    } cat_($file);

    unshift @{$l{$_}}, $hostname, if_($sub_hostname, $sub_hostname) foreach grep { $_ } @ips;

    log::explanations("writing host information to $file");
    output($file, map { "$_\t\t" . join(" ", @{$l{$_}}) . "\n" } keys %l);
}

# The interface/gateway needs to be configured before this will work!
sub guessHostname {
    my ($net, $intf_name) = @_;

    $net->{ifcfg}{$intf_name}{isUp} && dnsServers($net) or return 0;
    $net->{network}{HOSTNAME} && $net->{resolv}{DOMAINNAME} and return 1;

    write_resolv_conf($net);

    my $name = gethostbyaddr(Socket::inet_aton($net->{ifcfg}{$intf_name}{IPADDR}), Socket::AF_INET()) or log::explanations("reverse name lookup failed"), return 0;

    log::explanations("reverse name lookup worked");

    $net->{network}{HOSTNAME} ||= $name;
    1;
}

sub addDefaultRoute {
    my ($net) = @_;
    c::addDefaultRoute($net->{network}{GATEWAY}) if $net->{network}{GATEWAY};
}

sub write_hostname {
    my ($hostname) = @_;

    addVarsInSh($::prefix . $network_file, { HOSTNAME => $hostname }, qw(HOSTNAME));

    add2hosts("localhost", "127.0.0.1");
    add2hosts($hostname, "127.0.0.1") if $hostname;

    unless ($::isInstall) {
        my $rc = syscall_("sethostname", $hostname, length $hostname);
        log::explanations($rc ? "set sethostname to $hostname" : "sethostname failed: $!");
        run_program::run("/usr/bin/run-parts", "--arg", $hostname, "/etc/sysconfig/network-scripts/hostname.d");
    }
}

sub resolv($) {
    my ($name) = @_;
    is_ip($name) and return $name;
    my $a = join(".", unpack "C4", (gethostbyname $name)[4]);
    #-log::explanations("resolved $name in $a");
    $a;
}

sub dnsServers {
    my ($net) = @_;
    #- FIXME: that's weird
    my %used_dns; @used_dns{$net->{network}{dnsServer}, $net->{network}{dnsServer2}, $net->{network}{dnsServer3}} = (1, 2, 3);
    sort { $used_dns{$a} <=> $used_dns{$b} } grep { $_ } keys %used_dns;
}

sub findIntf {
    my ($net, $device) = @_;
    $net->{ifcfg}{$device}{DEVICE} = undef;
    $net->{ifcfg}{$device};
}

my $ip_regexp = qr/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;

sub is_ip {
    my ($ip) = @_;
    my @fields = $ip =~ $ip_regexp or return;
    every { 0 <= $_ && $_ <= 255 } @fields or return;
    @fields;
}

sub ip_compare {
    my ($ip1, $ip2) = @_;
    my (@ip1_fields) = $ip1 =~ $ip_regexp;
    my (@ip2_fields) = $ip2 =~ $ip_regexp;
    
    every { $ip1_fields[$_] eq $ip2_fields[$_] } (0 .. 3);
}

sub is_ip_forbidden {
    my ($ip) = @_;
    my @forbidden = ('127.0.0.1', '255.255.255.255');
    
    any { ip_compare($ip, $_) } @forbidden;
}

sub is_domain_name {
    my ($name) = @_;
    my @fields = split /\./, $name;
    $name !~ /\.$/ && @fields > 0 && @fields == grep { /^[[:alnum:]](?:[\-[:alnum:]]{0,61}[[:alnum:]])?$/ } @fields;
}

sub netmask {
    my ($ip) = @_;
    return "255.255.255.0" unless is_ip($ip);
    $ip =~ $ip_regexp or warn "IP_regexp failed\n" and return "255.255.255.0";
    if ($1 >= 1 && $1 < 127) {
	"255.0.0.0";    #-1.0.0.0 to 127.0.0.0
    } elsif ($1  >= 128 && $1 <= 191) {
	"255.255.0.0";  #-128.0.0.0 to 191.255.0.0
    } elsif ($1 >= 192 && $1 <= 223) {
	"255.255.255.0";
    } else {
	"255.255.255.255"; #-experimental classes
    }
}

sub netmask_to_vlsm {
    my ($netmask) = @_;
    #- based on Network::IPv4Addr::ipv4_msk2cidr
    my @bytes = split /\./, $netmask;
    my $prefix = 0;
    foreach (@bytes) {
        my $bits = unpack("B*", pack("C", $_));
        $prefix += $bits =~ tr/1/1/;
    }
    return $prefix;
}

sub masked_ip {
    my ($ip) = @_;
    my @ip = is_ip($ip) or return '';
    my @mask = netmask($ip) =~ $ip_regexp;
    for (my $i = 0; $i < @ip; $i++) {
	$ip[$i] &= int $mask[$i];
    }
    join(".", @ip);
}

sub dns {
    my ($ip) = @_;
    my @masked = masked_ip($ip) =~ $ip_regexp;
    $masked[3]  = 2;
    join(".", @masked);

}

sub gateway {
    my ($ip) = @_;
    my @masked = masked_ip($ip) =~ $ip_regexp;
    $masked[3]  = 1;
    join(".", @masked);
}


sub netprofile_set {
    my ($net, $profile) = @_;
    $net->{PROFILE} = $profile;
    system('/sbin/set-netprofile', $net->{PROFILE});
    log::explanations(qq(Switching to "$net->{PROFILE}" profile));
}

sub netprofile_save {
    my ($net) = @_;
    system('/sbin/save-netprofile', $net->{PROFILE});
    log::explanations(qq(Saving "$net->{PROFILE}" profile));
}

sub netprofile_delete {
    my ($profile) = @_;
    return if !$profile || $profile eq "default";
    rm_rf("$::prefix/etc/netprofile/profiles/$profile");
    log::explanations(qq(Deleting "$profile" profile));
}

sub netprofile_clone {
    my ($source_profile, $dest_profile) = @_;
    return if !$dest_profile || $dest_profile eq "default" || member($dest_profile, netprofile_list());
    system('/sbin/clone-netprofile', $source_profile, $dest_profile);
    log::explanations(qq("Creating "$dest_profile" profile));
}

sub netprofile_add {
    my ($net, $profile) = @_;
    netprofile_clone($net->{PROFILE}, $profile);
}

sub netprofile_list() {
    map { if_(m!([^/]*)/$!, $1) } glob("$::prefix/etc/netprofile/profiles/*/");
}

sub netprofile_read {
    my ($net) = @_;
    my $config = { getVarsFromSh("$::prefix/etc/netprofile/current") };
    $net->{PROFILE} = $config->{PROFILE} || 'default';
}


sub miscellaneous_choose {
    my ($in, $u) = @_;

    my $use_http_for_https = $u->{https_proxy} eq $u->{http_proxy};
    $in->ask_from(N("Proxies configuration"),
       N("Here you can set up your proxies configuration (eg: http://my_caching_server:8080)"),
       [ { label => N("HTTP proxy"), val => \$u->{http_proxy} },
         { text => N("Use HTTP proxy for HTTPS connections"), val => \$use_http_for_https, type => "bool" },
         { label => N("HTTPS proxy"), val => \$u->{https_proxy}, disabled => sub { $use_http_for_https } },
         { label => N("FTP proxy"),  val => \$u->{ftp_proxy} },
         { label => N("No proxy for (comma separated list):"),  val => \$u->{no_proxy} },
       ],
       complete => sub {
           $use_http_for_https and $u->{https_proxy} = $u->{http_proxy};
           $u->{no_proxy} =~ s/\s//g;
	   $u->{http_proxy} =~ m,^($|http://), or $in->ask_warn('', N("Proxy should be http://...")), return 1,0;
	   $u->{https_proxy} =~ m,^($|https?://), or $in->ask_warn('', N("Proxy should be http://... or https://...")), return 1,2;
	   $u->{ftp_proxy} =~ m,^($|ftp://|http://), or $in->ask_warn('', N("URL should begin with 'ftp:' or 'http:'")), return 1,3;
	   0;
       }
    ) or return;
    1;
}

sub proxy_configure_shell {
    my ($proxy) = @_;
    my $sh_file = "$::prefix/etc/profile.d/proxy.sh";
    setExportedVarsInSh($sh_file, $proxy, qw(http_proxy https_proxy ftp_proxy no_proxy));
    chmod 0755, $sh_file;
    my $csh_file = "$::prefix/etc/profile.d/proxy.csh";
    setExportedVarsInCsh($csh_file, $proxy, qw(http_proxy https_proxy ftp_proxy no_proxy));
    chmod 0755, $csh_file;
}

sub proxy_configure_kde {
    my ($proxy) = @_;

    my $kde_config_dir = "$::prefix/usr/share/config";
    -d $kde_config_dir or return;

    my $kde_config_file = "$kde_config_dir/kioslaverc";
    update_gnomekderc($kde_config_file,
                      undef,
                      PersistentProxyConnection => "false"
                  );
    update_gnomekderc($kde_config_file,
                      "Proxy Settings",
                      AuthMode => 0,
                      ProxyType => $proxy->{http_proxy} || $proxy->{https_proxy} || $proxy->{ftp_proxy} ? 4 : 0,
                      ftpProxy => "ftp_proxy",
                      httpProxy => "http_proxy",
                      httpsProxy => "https_proxy",
                      NoProxyFor => "no_proxy",
                  );
}

#- (protocol, user, password, host, port)
my $http_proxy_match = qr,^(http)://(?:([^:\@]+)(?::([^:\@]+))?\@)?([^\:]+)(?::(\d+))?$,;
#- (protocol, host, port)
my $https_proxy_match = qr,^(https?)://(?:[^:\@]+(?::[^:\@]+)?\@)?([^\:]+)(?::(\d+))?$,;
#- (protocol, protocol, host, port)
my $ftp_proxy_match = qr,^(http|ftp)://(?:[^:\@]+(?::[^:\@]+)?\@)?([^\:]+)(?::(\d+))?$,;
my %proxy_default_port = (
    http => 80,
    https => 443,
    ftp => 21,
);

sub proxy_configure_gnome {
    my ($proxy) = @_;

    -d "$::prefix/etc/gconf/2/" or return;

    my $defaults_dir = "/etc/gconf/gconf.xml.local-defaults";
    my $p_defaults_dir = "$::prefix$defaults_dir";

    my $use_alternate_proxy;
    my $gconf_set = sub {
        my ($key, $type, $value) = @_;
        #- gconftool-2 is available since /etc/gconf/2/ exists
        run_program::rooted($::prefix, 'gconftool-2', '>', '/dev/null', "--config-source=xml::$p_defaults_dir", "--direct", "--set", "--type=$type", if_($type eq "list", '--list-type', 'string'), $key, $value);
    };

    #- http proxy
    if (my ($protocol, $user, $password, $host, $port) = $proxy->{http_proxy} =~ $http_proxy_match) {
        $port ||= $proxy_default_port{$protocol} || $proxy_default_port{http};
        $gconf_set->("/system/http_proxy/use_http_proxy", "bool", 1);
        $gconf_set->("/system/http_proxy/host", "string", $host);
        $gconf_set->("/system/http_proxy/port", "int", $port);
        $gconf_set->("/system/http_proxy/use_authentication", "bool", to_bool($user));
        $user and $gconf_set->("/system/http_proxy/authentication_user", "string", $user);
        $password and $gconf_set->("/system/http_proxy/authentication_password", "string", $password);
    } else {
        $gconf_set->("/system/http_proxy/use_http_proxy", "bool", 0);
    }

    #- https proxy
    if (my ($protocol, $host, $port) = $proxy->{https_proxy} =~ $https_proxy_match) {
        $port ||= $proxy_default_port{$protocol} || $proxy_default_port{https};
        $gconf_set->("/system/proxy/secure_host", "string", $host);
        $gconf_set->("/system/proxy/secure_port",  "int", $port);
        $use_alternate_proxy = 1;
    } else {
        #- clear the ssl host so that it isn't used if the manual proxy is activated for ftp
        $gconf_set->("/system/proxy/secure_host", "string", "");
    }

    #- ftp proxy
    if (my ($protocol, $host, $port) = $proxy->{ftp_proxy} =~ $ftp_proxy_match) {
        $port ||= $proxy_default_port{$protocol} || $proxy_default_port{ftp};
        $gconf_set->("/system/proxy/ftp_host", "string", $host);
        $gconf_set->("/system/proxy/ftp_port", "int", $port);
        $use_alternate_proxy = 1;
    } else {
        #- clear the ftp host so that it isn't used if the manual proxy is activated for ssl
        $gconf_set->("/system/proxy/ftp_host", "string", "");
    }

    my $ignore_hosts = join(',', uniq(qw(localhost 127.0.0.0/8)), split(',', $proxy->{no_proxy}));
    $gconf_set->("/system/http_proxy/ignore_hosts", "list", "[$ignore_hosts]");

    #- set proxy mode to manual if either https or ftp is used
    $gconf_set->("/system/proxy/mode", "string", $use_alternate_proxy ? "manual" : "none");

    #- make gconf daemons reload their settings
    system("killall -s HUP gconfd-2");
}

sub proxy_configure_mozilla_firefox {
    my ($proxy) = @_;

    my $firefox_config_file = "$::prefix/etc/firefox.cfg";
    -f $firefox_config_file or return;

    my %prefs;
    foreach (qw(http ssl ftp)) {
        undef $prefs{"network.proxy.${_}"};
        undef $prefs{"network.proxy.${_}_port"};
    }
    if (my ($protocol, undef, undef, $host, $port) = $proxy->{http_proxy} =~ $http_proxy_match) {
        $prefs{"network.proxy.http"} = qq("$host");
        $prefs{"network.proxy.http_port"} = $port || $proxy_default_port{$protocol} || $proxy_default_port{http};
    }
    if (my ($protocol, $host, $port) = $proxy->{https_proxy} =~ $https_proxy_match) {
        $prefs{"network.proxy.ssl"} =  qq("$host");
        $prefs{"network.proxy.ssl_port"} = $port || $proxy_default_port{$protocol} || $proxy_default_port{https};
    }
    if (my ($protocol, $host, $port) = $proxy->{ftp_proxy} =~ $ftp_proxy_match) {
        $prefs{"network.proxy.ftp"} =  qq("$host");
        $prefs{"network.proxy.ftp_port"} = $port || $proxy_default_port{$protocol} || $proxy_default_port{ftp};
    }
    if ($proxy->{no_proxy}) {
        $prefs{"network.proxy.no_proxies_on"} = qq("$proxy->{no_proxy}");
    }
    $prefs{"network.proxy.type"} = any { defined $prefs{"network.proxy.${_}_port"} } qw(http ssl ftp);

    substInFile {
        while (my ($key, $value) = each(%prefs)) {
            if (/^defaultPref\("$key",/) {
                $_ = defined $value && qq(defaultPref("$key", $value);\n);
                delete $prefs{$key};
            }
        }
        $_ .= join('', map { if_(defined $prefs{$_}, qq(defaultPref("$_", $prefs{$_});\n)) } sort(keys %prefs)) if eof;
    } $firefox_config_file;
}

sub proxy_configure {
    my ($proxy) = @_;
    proxy_configure_shell($proxy);
    proxy_configure_kde($proxy);
    proxy_configure_gnome($proxy);
    proxy_configure_mozilla_firefox($proxy);
}

sub read_net_conf {
    my ($net) = @_;
    add2hash($net->{network} ||= {}, read_conf($::prefix . $network_file));
    add2hash($net->{resolv} ||= {}, read_resolv_conf());
    add2hash($net->{zeroconf} ||= {}, read_zeroconf());

    foreach (all("$::prefix/etc/sysconfig/network-scripts")) {
	my ($device) = /^ifcfg-([A-Za-z0-9.:_-]+)$/;
	next if $device =~ /.rpmnew$|.rpmsave$/;
	if ($device && $device ne 'lo') {
	    my $intf = findIntf($net, $device);
	    add2hash($intf, { getVarsFromSh("$::prefix/etc/sysconfig/network-scripts/$_") });
	    $intf->{DEVICE} ||= $device;
	}
    }
    $net->{wireless} ||= {};
    foreach (all($::prefix . $wireless_d)) {
        $net->{wireless}{$_} = { getVarsFromSh($::prefix . $wireless_d . '/' . $_) };
    }
    netprofile_read($net);
    if (my $default_intf = network::tools::get_default_gateway_interface($net)) {
	$net->{net_interface} = $default_intf;
	$net->{type} = network::tools::get_interface_type($net->{ifcfg}{$default_intf});
    }
}

#- FIXME: this is buggy, use network::tools::get_default_gateway_interface
sub probe_netcnx_type {
    my ($net) = @_;
    #- try to probe $netcnx->{type} which is used almost everywhere.
    unless ($net->{type}) {
	#- ugly hack to determine network type (avoid saying not configured in summary).
	-e "$::prefix/etc/ppp/peers/adsl" and $net->{type} ||= 'adsl'; # enough ?
	-e "$::prefix/etc/ppp/ioptions1B" || -e "$::prefix/etc/ppp/ioptions2B" and $net->{type} ||= 'isdn'; # enough ?
	$net->{ifcfg}{ppp0} and $net->{type} ||= 'modem';
	$net->{ifcfg}{eth0} and $net->{type} ||= 'lan';
    }
}

sub easy_dhcp {
    my ($net, $modules_conf) = @_;

    return if text2bool($net->{network}{NETWORKING});

    require modules;
    require network::connection::ethernet;
    modules::load_category($modules_conf, list_modules::ethernet_categories());
    my @all_dev = sort map { $_->[0] } network::connection::ethernet::get_eth_cards($modules_conf);

    #- only for a single ethernet network card