package network::ipsec; use detect_devices; use network::netconnect; use run_program; use common; use log; use Data::Dumper; #- debugg functions ---------- sub recreate_ipsec_conf { my ($ipsec, $kernel_version) = @_; if ($kernel_version < 2.5) { #- kernel 2.4 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { print "$ipsec->{$key1}\n" if ! $ipsec->{$key1}{1}; foreach my $key2 (ikeys %{$ipsec->{$key1}}) { if ($ipsec->{$key1}{$key2}[0] =~ m/^#/) { print "\t$ipsec->{$key1}{$key2}[0]\n"; } elsif ($ipsec->{$key1}{$key2}[0] =~ m/(conn|config|version)/) { print "$ipsec->{$key1}{$key2}[0] $ipsec->{$key1}{$key2}[1]\n"; } else { print "\t$ipsec->{$key1}{$key2}[0]=$ipsec->{$key1}{$key2}[1]\n"; }; } } } else { #- kernel 2.6 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { if (! $ipsec->{$key1}{command}) { print "$ipsec->{$key1}\n"; } else { print $ipsec->{$key1}{command} . " " . $ipsec->{$key1}{src_range} . " " . $ipsec->{$key1}{dst_range} . " " . $ipsec->{$key1}{upperspec} . " " . $ipsec->{$key1}{flag} . " " . $ipsec->{$key1}{direction} . " " . $ipsec->{$key1}{ipsec} . "\n\t" . $ipsec->{$key1}{protocol} . "/" . $ipsec->{$key1}{mode} . "/" . $ipsec->{$key1}{src_dest} . "/" . $ipsec->{$key1}{level} . ";\n" }; } } } sub recreate_racoon_conf { my ($racoon) = @_; my $in_a_section = "n"; my $in_a_proposal_section = "n"; foreach my $key1 (ikeys %$racoon) { if ($in_a_proposal_section eq "y") { print "\t}\n}\n$racoon->{$key1}\n" if ! $racoon->{$key1}{1}; } elsif ($in_a_section eq "y") { print "}\n$racoon->{$key1}\n" if ! $racoon->{$key1}{1}; } else { print "$racoon->{$key1}\n" if ! $racoon->{$key1}{1}; }; $in_a_section = "n"; $in_a_proposal_section = "n"; foreach my $key2 (ikeys %{$racoon->{$key1}}) { if ($racoon->{$key1}{$key2}[0] =~ /^path/) { print "$racoon->{$key1}{$key2}[0] $racoon->{$key1}{$key2}[1] $racoon->{$key1}{$key2}[2];\n"; } elsif ($racoon->{$key1}{$key2}[0] =~ /^remote/) { $in_a_section = "y"; $in_a_proposal_section = "n"; print "$racoon->{$key1}{$key2}[0] $racoon->{$key1}{$key2}[1] {\n"; } elsif ($racoon->{$key1}{$key2}[0] =~ /^sainfo/) { $in_a_section = "y"; $in_a_proposal_section = "n"; if ($racoon->{$key1}{$key2}[2] && $racoon->{$key1}{$key2}[5]) { print "$racoon->{$key1}{$key2}[0] $racoon->{$key1}{$key2}[1] $racoon->{$key1}{$key2}[2] $racoon->{$key1}{$key2}[3] $racoon->{$key1}{$key2}[4] $racoon->{$key1}{$key2}[5] $racoon->{$key1}{$key2}[6] {\n"; } else { print "$racoon->{$key1}{$key2}[0] anonymous {\n"; } } elsif ($racoon->{$key1}{$key2}[0] =~ /^proposal /) { $in_a_proposal_section = "y"; print "\t$racoon->{$key1}{$key2}[0] {\n"; } elsif ($in_a_section eq "y" && $racoon->{$key1}{$key2}[0] =~ /^certificate_type/) { print "\t$racoon->{$key1}{$key2}[0] $racoon->{$key1}{$key2}[1] $racoon->{$key1}{$key2}[2] $racoon->{$key1}{$key2}[3];\n"; } elsif ($in_a_section eq "y" && $racoon->{$key1}{$key2}[0] =~ /^#/) { print "\t$racoon->{$key1}{$key2}[0] $racoon->{$key1}{$key2}[1]\n"; } elsif ($in_a_section eq "y") { print "\t$racoon->{$key1}{$key2}[0] $racoon->{$key1}{$key2}[1];\n"; } elsif ($in_a_proposal_section eq "y" && $racoon->{$key1}{$key2}[0] =~ /^#/) { print "\t\t$racoon->{$key1}{$key2}[0] $racoon->{$key1}{$key2}[1]\n"; } elsif ($in_a_proposal_section eq "y") { print "\t\t$racoon->{$key1}{$key2}[0] $racoon->{$key1}{$key2}[1];\n"; } } } print "}\n"; } sub recreate_ipsec_conf1_k24 { my ($ipsec) = @_; foreach my $key1 (ikeys %$ipsec) { print "$key1-->$ipsec->{$key1}\n" if ! $ipsec->{$key1}{1}; foreach my $key2 (ikeys %{$ipsec->{$key1}}) { if ($ipsec->{$key1}{$key2}[0] =~ m/^#/) { print "\t$key2-->$ipsec->{$key1}{$key2}[0]\n"; } elsif ($ipsec->{$key1}{$key2}[0] =~ m/(conn|config|version)/) { print "$key1-->$key2-->$ipsec->{$key1}{$key2}[0] $ipsec->{$key1}{$key2}[1]\n"; } else { print "\t$key2-->$ipsec->{$key1}{$key2}[0]=$ipsec->{$key1}{$key2}[1]\n"; }; } } } #- end of debug functions -------- sub sys { system(@_) == 0 or log::l("[drakvpn] Warning, sys failed for $_[0]") } sub start_daemons () { return if $::testing; log::explanations("Starting daemons"); if (-e "/etc/rc.d/init.d/ipsec") { system("/etc/rc.d/init.d/ipsec status >/dev/null") == 0 and sys("/etc/rc.d/init.d/ipsec stop"); sys("/etc/rc.d/init.d/$_ start >/dev/null"), sys("/sbin/chkconfig --level 345 $_ on") foreach 'ipsec'; } else { }; sys("/etc/rc.d/init.d/$_ start >/dev/null"), sys("/sbin/chkconfig --level 345 $_ on") foreach 'shorewall'; } sub stop_daemons () { return if $::testing; log::explanations("Stopping daemons"); if (-e "/etc/rc.d/init.d/ipsec") { foreach (qw(ipsec)) { system("/etc/rc.d/init.d/$_ status >/dev/null 2>/dev/null") == 0 and sys("/etc/rc.d/init.d/$_ stop"); }; sys("/sbin/chkconfig --level 345 $_ off") && -e "/etc/rc.d/init.d/$_" foreach 'ipsec'; }; system("/etc/rc.d/init.d/shorewall status >/dev/null 2>/dev/null") == 0 and sys("/etc/rc.d/init.d/shorewall stop >/dev/null"); } sub set_config_file { my ($file, @l) = @_; my $done; substInFile { if (!$done && (/^#LAST LINE/ || eof)) { $_ = join('', map { join("\t", @$_) . "\n" } @l) . $_; $done = 1; } else { $_ = '' if /^[^#]/; } } "$::prefix/$file"; } sub get_config_file { my ($file) = @_; map { [ split ' ' ] } grep { !/^#/ } cat_("$::prefix/$file"); } #------------------------------------------------------------------- #---------------------- configure racoon_conf ----------------------- #------------------------------------------------------------------- sub read_racoon_conf { my ($racoon_conf) = @_; my %conf; my $nb = 0; #total number my $i = 0; #nb within a section my $in_a_section = "n"; my @line1; my $line = ""; local $_; open(my $LIST, "< $racoon_conf"); while (<$LIST>) { chomp($_); $line = $_; $in_a_section = "n" if $line =~ /}/ && $line !~ /^#/; $line =~ s/^\s+|\s*;|\s*{//g if $line !~ /^#/; $line =~ /(.*)#(.*)/ if $line !~ /^#/; #- define before and after comment # print "--line-->$line\n"; my $data_part = $1; my $comment_part = "#".$2; if ($data_part) { $data_part =~ s/,//g; # print "@@".$data_part."->".$comment_part."\n"; @line1 = split /\s+/,$data_part; @line1 = (@line1, $comment_part) if $comment_part; } else { @line1 = split /\s+/,$line; } if (!$line && $in_a_section eq "n") { $nb++; put_in_hash(\%conf, { $nb => $line }); $in_a_section = "n"; } elsif (!$line && $in_a_section eq "y") { put_in_hash($conf{$nb} ||= {}, { $i => [ '' ] }); $i++; } elsif ($line =~ /^path/) { $i=1; $nb++; put_in_hash($conf{$nb} ||= {}, { $i => [@line1] }); $in_a_section = "n"; $i++; } elsif ($line =~ /^#|^{|^}/) { if ($in_a_section eq "y") { put_in_hash($conf{$nb} ||= {}, { $i => [$line] }); $i++; } else { $nb++; put_in_hash(\%conf, { $nb => $line }); $in_a_section = "n"; }; } elsif ($line =~ /^sainfo|^remote|^listen|^timer|^padding/ && $in_a_section eq "n") { $i=1; $nb++; put_in_hash($conf{$nb} ||= {}, { $i => [@line1] }); $in_a_section = "y"; $i++; } elsif ($line eq "proposal" && $in_a_section eq "y") { $i=1; $nb++; put_in_hash($conf{$nb} ||= {}, { $i => [@line1] }); $in_a_section = "y"; $i++; } else { put_in_hash($conf{$nb} ||= {}, { $i => [@line1] }); $i++; }; }; \%conf; } sub display_racoon_conf { my ($racoon) = @_; my $display = ""; my $prefix_to_simple_line = ""; my $pt; foreach my $key1 (ikeys %$racoon) { if (!$racoon->{$key1}{1}) { $display .= $prefix_to_simple_line . $racoon->{$key1} . "\n"; $prefix_to_simple_line = ""; } else { foreach my $key2 (ikeys %{$racoon->{$key1}}) { if ($key2 > 1) { $pt = $racoon->{$key1}{$key2-1}[0]; } else { $pt = $racoon->{$key1}{1}[0]; }; my $t = $racoon->{$key1}{1}[0]; my $f = $racoon->{$key1}{$key2}[0]; my $list_length = scalar @{$racoon->{$key1}{$key2}}; my $already_read = 0; my $line = ""; if ($racoon->{$key1}{$key2}[0] eq "sainfo" && !$racoon->{$key1}{$key2}[2]) { $line = "sainfo anonymous"; } else { for (my $i = 0; $i <= $list_length-1; $i++) { my $c = $racoon->{$key1}{$key2}[$i]; my $n = $racoon->{$key1}{$key2}[$i+1]; if ($c =~ /^path|^log|^timer|^listen|^padding|^remote|^proposal|^sainfo/) { $line .= "$c "; } elsif ($i == $list_length-2 && $n =~ /^#/) { $line .= "$c; "; } elsif ($i == $list_length-1) { if ($f =~ /^#|^$|^timer|^listen|^padding|^remote|^proposal\s+|^sainfo/) { $line .= $c; } elsif ($c =~ /^#/) { $line .= "\t$c"; } else { $line .= "$c;"; } } else { $line .= "$c "; } $already_read = 1; } } if ($f =~ /^timer|^listen|^padding|^remote|^sainfo/) { $line .= " {"; $prefix_to_simple_line = ""; } elsif ($f eq "proposal") { $line = "\t" . $line . " {"; } elsif ($t eq "proposal") { $line = "\t\t" . $line if $line ne "proposal"; $prefix_to_simple_line = "\t"; } else { $line = "\t" . $line if $t !~ /^path|^log/; $prefix_to_simple_line = ""; } $display .= "$line\n"; } } } $display; } sub write_racoon_conf { my ($racoon_conf, $racoon) = @_; my $display = ""; my $prefix_to_simple_line = ""; my $pt; foreach my $key1 (ikeys %$racoon) { if (!$racoon->{$key1}{1}) { $display .= $prefix_to_simple_line . $racoon->{$key1} . "\n"; $prefix_to_simple_line = ""; } else { foreach my $key2 (ikeys %{$racoon->{$key1}}) { if ($key2 > 1) { $pt = $racoon->{$key1}{$key2-1}[0]; } else { $pt = $racoon->{$key1}{1}[0]; }; my $t = $racoon->{$key1}{1}[0]; my $f = $racoon->{$key1}{$key2}[0]; my $list_length = scalar @{$racoon->{$key1}{$key2}}; my $already_read = 0; my $line = ""; if ($racoon->{$key1}{$key2}[0] eq "sainfo" && !$racoon->{$key1}{$key2}[2]) { $line = "sainfo anonymous"; } else { for (my $i = 0; $i <= $list_length-1; $i++) { my $c = $racoon->{$key1}{$key2}[$i]; my $n = $racoon->{$key1}{$key2}[$i+1]; if ($c =~ /^path|^log|^timer|^listen|^padding|^remote|^proposal|^sainfo/) { $line .= "$c "; } elsif ($i == $list_length-2 && $n =~ /^#/) { $line .= "$c; "; } elsif ($i == $list_length-1) { if ($f =~ /^#|^$|^timer|^listen|^padding|^remote|^proposal\s+|^sainfo/) { $line .= $c; } elsif ($c =~ /^#/) { $line .= "\t$c"; } else { $line .= "$c;"; } } else { $line .= "$c "; } $already_read = 1; } } if ($f =~ /^timer|^listen|^padding|^remote|^sainfo/) { $line .= " {"; $prefix_to_simple_line = ""; } elsif ($f eq "proposal") { $line = "\t" . $line . " {"; } elsif ($t eq "proposal") { $line = "\t\t" . $line if $line ne "proposal"; $prefix_to_simple_line = "\t"; } else { $line = "\t" . $line if $t !~ /^path|^log/; $prefix_to_simple_line = ""; } $display .= "$line\n"; } } } open(my $ADD, "> $racoon_conf") or die "Can not open the $racoon_conf file for writing"; print $ADD "$display\n"; } sub get_section_names_racoon_conf { my ($racoon) = @_; my @section_names; foreach my $key1 (ikeys %$racoon) { if (!$racoon->{$key1}{1}) { next; } else { my $list_length = scalar @{$racoon->{$key1}{1}}; my $section_title = ""; my $separator = ""; for (my $i = 0; $i <= $list_length-1; $i++) { my $s = $racoon->{$key1}{1}[$i]; if ($s !~ /^#|^proposal/) { $section_title .= $separator . $s; $separator = " "; }; } push(@section_names, $section_title) if $section_title ne ""; } } @section_names; } sub add_section_racoon_conf { my ($new_section, $racoon) = @_; put_in_hash($racoon, { max(keys %$racoon) + 1 => '' }); put_in_hash($racoon, { max(keys %$racoon) + 1 => $new_section }); put_in_hash($racoon, { max(keys %$racoon) + 1 => '}' }) if $new_section->{1}[0] !~ /^path|^remote/; put_in_hash($racoon, { max(keys %$racoon) + 1 => '' }) if $new_section->{1}[0] =~ /^proposal/; put_in_hash($racoon, { max(keys %$racoon) + 1 => '}' }) if $new_section->{1}[0] =~ /^proposal/; } sub matched_section_key_number_racoon_conf { my ($section_name, $racoon) = @_; foreach my $key1 (ikeys %$racoon) { if (!$racoon->{$key1}{1}) { next; } else { my $list_length = scalar @{$racoon->{$key1}{1}}; my $section_title = ""; my $separator = ""; for (my $i = 0; $i <= $list_length-1; $i++) { my $s = $racoon->{$key1}{1}[$i]; if ($s !~ /^#|^proposal/) { $section_title .= $separator . $s; $separator = " "; }; }; if ($section_title eq $section_name) { return $key1; }; } } } sub already_existing_section_racoon_conf { my ($section_name, $racoon, $racoon_conf) = @_; if (-e $racoon_conf) { foreach my $key1 (ikeys %$racoon) { if (!$racoon->{$key1}{1}) { next; } elsif (find { my $list_length = scalar @{$racoon->{$key1}{1}}; my $section_title = ""; my $separator = ""; for (my $i = 0; $i <= $list_length-1; $i++) { my $s = $racoon->{$key1}{1}[$i]; if ($s !~ /^#|^proposal/) { $section_title .= $separator . $s; $separator = " "; }; } $section_title eq $section_name; } ikeys %{$racoon->{$key1}}) { return "already existing"; } } } } sub remove_section_racoon_conf { my ($section_name, $racoon, $k) = @_; if ($section_name =~ /^remote/) { delete $racoon->{$k} if $k > 1 && !$racoon->{$k-1}; my $closing_curly_bracket = 0; while ($closing_curly_bracket < 2) { print "-->$k\n"; $closing_curly_bracket++ if $racoon->{$k} eq "}"; delete $racoon->{$k}; $k++; } } elsif ($section_name =~ /^path/) { delete $racoon->{$k}; delete $racoon->{$k+1} if $racoon->{$k+1}{1} eq ""; } else { delete $racoon->{$k}; delete $racoon->{$k+1} if $racoon->{$k+1}{1} eq ""; delete $racoon->{$k+2} if $racoon->{$k+2}{1} eq ""; #- remove assoc } } } #------------------------------------------------------------------- #---------------------- configure ipsec_conf ----------------------- #------------------------------------------------------------------- sub read_ipsec_conf { my ($ipsec_conf, $kernel_version) = @_; my %conf; my $nb = 0; #total number my $i = 0; #nb within a connexion my $in_a_conn = "n"; my $line = ""; my @line1; local $_; if ($kernel_version < 2.5) { #- kernel 2.4 part ------------------------------- open(my $LIST, "< $ipsec_conf"); #or die "Can not open the $ipsec_conf file for reading"; while (<$LIST>) { chomp($_); $line = $_; $line =~ s/^\s+//; if (!$line) { $nb++; put_in_hash(\%conf, { $nb => $line }); $in_a_conn = "n"; } elsif ($line =~ /^#/) { if ($in_a_conn eq "y") { put_in_hash($conf{$nb} ||= {}, { $i => [$line] }); $i++; } else { $nb++; put_in_hash(\%conf, { $nb => $line }); $in_a_conn = "n"; }; } elsif ($line =~ /^conn|^config|^version/ && $in_a_conn eq "n") { @line1 = split /\s+/,$line; $i=1; $nb++; put_in_hash($conf{$nb} ||= {}, { $i => [$line1[0], $line1[1]] }); $in_a_conn = "y" if $line !~ /^version/; $i++; } elsif ($line =~ /^conn|^config|^version/ && $in_a_conn eq "y") { @line1 = split /\s+/,$line; $i=1; $nb++; put_in_hash($conf{$nb} ||= {}, { $i => [$line1[0], $line1[1]] }); $i++; } else { @line1 = split /=/,$line; put_in_hash($conf{$nb} ||= {}, { $i => [$line1[0], $line1[1]] }); $i++; }; }; } else { #- kernel 2.6 part ------------------------------- my @mylist; my $myline = ""; open(my $LIST, "< $ipsec_conf"); #or die "Can not open the $ipsec_conf file for reading"; while (<$LIST>) { chomp($_); $myline = $_; $myline =~ s/^\s+//; $myline =~ s/;$//; if ($myline =~ /^spdadd/) { @mylist = split /\s+/,$myline; $in_a_conn = "y"; $nb++; next; } elsif ($in_a_conn eq "y") { @mylist = (@mylist, split '\s+|/',$myline); put_in_hash(\%conf, { $nb => { command => $mylist[0], src_range => $mylist[1], dst_range => $mylist[2], upperspec => $mylist[3], flag => $mylist[4], direction => $mylist[5], ipsec => $mylist[6], protocol => $mylist[7], mode => $mylist[8], src_dest => $mylist[9], level => $mylist[10] } }); $in_a_conn = "n"; } else { $nb++; put_in_hash(\%conf, { $nb => $myline }); }; }; }; \%conf; } sub write_ipsec_conf { my ($ipsec_conf, $ipsec, $kernel_version) = @_; if ($kernel_version < 2.5) { #- kernel 2.4 part ------------------------------- open(my $ADD, "> $ipsec_conf") or die "Can not open the $ipsec_conf file for writing"; foreach my $key1 (ikeys %$ipsec) { print $ADD "$ipsec->{$key1}\n" if ! $ipsec->{$key1}{1}; foreach my $key2 (ikeys %{$ipsec->{$key1}}) { if ($ipsec->{$key1}{$key2}[0] =~ m/^#/) { print $ADD "\t$ipsec->{$key1}{$key2}[0]\n"; } elsif ($ipsec->{$key1}{$key2}[0] =~ m/(^conn|^config|^version)/) { print $ADD "$ipsec->{$key1}{$key2}[0] $ipsec->{$key1}{$key2}[1]\n"; } else { print $ADD "\t$ipsec->{$key1}{$key2}[0]=$ipsec->{$key1}{$key2}[1]\n" if $ipsec->{$key1}{$key2}[0] && $ipsec->{$key1}{$key2}[1]; }; } } } else { #- kernel 2.6 part ------------------------------- my $display = ""; foreach my $key1 (ikeys %$ipsec) { if (! $ipsec->{$key1}{command}) { $display .= "$ipsec->{$key1}\n"; } else { $display .= $ipsec->{$key1}{command} . " " . $ipsec->{$key1}{src_range} . " " . $ipsec->{$key1}{dst_range} . " " . $ipsec->{$key1}{upperspec} . " " . $ipsec->{$key1}{flag} . " " . $ipsec->{$key1}{direction} . " " . $ipsec->{$key1}{ipsec} . "\n\t" . $ipsec->{$key1}{protocol} . "/" . $ipsec->{$key1}{mode} . "/" . $ipsec->{$key1}{src_dest} . "/" . $ipsec->{$key1}{level} . ";\n" }; } open(my $ADD, "> $ipsec_conf") or die "Can not open the $ipsec_conf file for writing"; print $ADD $display; } } sub display_ipsec_conf { my ($ipsec, $kernel_version) = @_; my $display = ""; if ($kernel_version < 2.5) { #- kernel 2.4 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { $display .= "$ipsec->{$key1}\n" if ! $ipsec->{$key1}{1}; foreach my $key2 (ikeys %{$ipsec->{$key1}}) { if ($ipsec->{$key1}{$key2}[0] =~ m/^#/) { $display .= "\t$ipsec->{$key1}{$key2}[0]\n"; } elsif ($ipsec->{$key1}{$key2}[0] =~ m/(^conn|^config|^version)/) { $display .= "$ipsec->{$key1}{$key2}[0] $ipsec->{$key1}{$key2}[1]\n"; } else { $display .= "\t$ipsec->{$key1}{$key2}[0]=$ipsec->{$key1}{$key2}[1]\n"; }; } } } else { #- kernel 2.6 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { if (! $ipsec->{$key1}{command}) { $display .= "$ipsec->{$key1}\n"; } else { $display .= $ipsec->{$key1}{command} . " " . $ipsec->{$key1}{src_range} . " " . $ipsec->{$key1}{dst_range} . " " . $ipsec->{$key1}{upperspec} . " " . $ipsec->{$key1}{flag} . " " . $ipsec->{$key1}{direction} . " " . $ipsec->{$key1}{ipsec} . "\n\t" . $ipsec->{$key1}{protocol} . "/" . $ipsec->{$key1}{mode} . "/" . $ipsec->{$key1}{src_dest} . "/" . $ipsec->{$key1}{level} . ";\n"; } } } $display; } sub get_section_names_ipsec_conf { my ($ipsec, $kernel_version) = @_; my @section_names; if ($kernel_version < 2.5) { #- kernel 2.4 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { foreach my $key2 (ikeys %{$ipsec->{$key1}}) { if ($ipsec->{$key1}{$key2}[0] =~ m/(^conn|^config|^version)/) { push(@section_names, "$ipsec->{$key1}{$key2}[0] $ipsec->{$key1}{$key2}[1]"); }; } } } else { #- kernel 2.6 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { if ($ipsec->{$key1}{command} =~ m/(^spdadd)/) { push(@section_names, "$ipsec->{$key1}{src_range} $ipsec->{$key1}{dst_range}"); }; } } @section_names; } sub remove_section_ipsec_conf { my ($section_name, $ipsec, $kernel_version) = @_; if ($kernel_version < 2.5) { #- kernel 2.4 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { if (find { my $s = $ipsec->{$key1}{$_}[0]; $s !~ /^#/ && $s =~ m/(^conn|^config|^version)/ && $section_name eq "$s $ipsec->{$key1}{$_}[1]"; } ikeys %{$ipsec->{$key1}}) { delete $ipsec->{$key1}; } } } else { #- kernel 2.6 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { if (find { my $s = "$ipsec->{$key1}{src_range} $ipsec->{$key1}{dst_range}"; $s !~ /^#/ && $ipsec->{$key1}{src_range} && $section_name eq $s; } ikeys %{$ipsec->{$key1}}) { delete $ipsec->{$key1-1}; delete $ipsec->{$key1}; } } } } sub add_section_ipsec_conf { my ($new_section, $ipsec) = @_; put_in_hash($ipsec, { max(keys %$ipsec) + 1 => '' }); put_in_hash($ipsec, { max(keys %$ipsec) + 1 => $new_section }); } sub already_existing_section_ipsec_conf { my ($section_name, $ipsec, $kernel_version) = @_; if ($kernel_version < 2.5) { #- kernel 2.4 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { if (find { my $s = $ipsec->{$key1}{$_}[0]; $s !~ /^#/ && $s =~ m/(^conn|^config|^version)/ && $section_name eq "$s $ipsec->{$key1}{$_}[1]"; } ikeys %{$ipsec->{$key1}}) { return "already existing"; } } } else { #- kernel 2.6 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { if (find { my $s = "$ipsec->{$key1}{src_range} $ipsec->{$key1}{dst_range}"; $s !~ /^#/ && $ipsec->{$key1}{src_range} && $section_name eq $s; } ikeys %{$ipsec->{$key1}}) { return "already existing"; } } }; return "no"; } #- returns the reference to the dynamical list for editing sub dynamic_list { my ($number, $ipsec) = @_; my @list = map { { label => $ipsec->{$number}{$_}[0] . "=", val => \$ipsec->{$number}{$_}[1] } } ikeys %{$ipsec->{$number}}; @list; } #- returns the hash key number of $section_name sub matched_section_key_number_ipsec_conf { my ($section_name, $ipsec, $kernel_version) = @_; if ($kernel_version < 2.5) { #- kernel 2.4 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { if (find { my $s = $ipsec->{$key1}{$_}[0]; $s !~ /^#/ && $s =~ m/(^conn|^config|^version)/ && $section_name eq "$s $ipsec->{$key1}{$_}[1]"; } ikeys %{$ipsec->{$key1}}) { return $key1; } } } else { #- kernel 2.6 part ------------------------------- foreach my $key1 (ikeys %$ipsec) { if (find { my $s = "$ipsec->{$key1}{src_range} $ipsec->{$key1}{dst_range}"; $s !~ /^#/ && $ipsec->{$key1}{src_range} && $section_name eq $s; } ikeys %{$ipsec->{$key1}}) { return $key1; } } } } 1 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
package install_steps_gtk; # $Id$

use diagnostics;
use strict;
use vars qw(@ISA);

@ISA = qw(install_steps_interactive interactive::gtk);

#-######################################################################################
#- misc imports
#-######################################################################################
use pkgs;
use install_steps_interactive;
use interactive::gtk;
use xf86misc::main;
use common;
use mygtk2;
use ugtk2 qw(:helpers :wrappers :create);
use devices;
use modules;
use install_gtk;
use install_any;
use mouse;
use help;
use log;

#-######################################################################################
#- In/Out Steps Functions
#-######################################################################################
sub new($$) {
    my ($type, $o) = @_;

    $ENV{DISPLAY} ||= $o->{display} || ":0";
    my $wanted_DISPLAY = $::testing && -x '/usr/X11R6/bin/Xnest' ? ':9' : $ENV{DISPLAY};

    if ($ENV{DISPLAY} =~ /^:\d/ && !$::testing || $ENV{DISPLAY} ne $wanted_DISPLAY) { #- is the display local or distant?
	my $f = "/tmp/Xconf";
	if (!$::testing) {
	    devices::make("/dev/kbd");
	}
	my $launchX = sub {
	    my ($server, $Driver) = @_;

	    mkdir '/var/log' if !-d '/var/log';

	    my @options = $wanted_DISPLAY;
	    if ($server eq 'Xnest') {
		push @options, '-ac', '-geometry', $o->{vga} || ($o->{vga16} ? '640x480' : '800x600');
	    } elsif ($::globetrotter || !$::move) {
                #- use alternate mouse in install if mouse is unsafe or needs some specific module (e.g. synaptics)
                my $mouse = is_empty_hash_ref($o->{mouse}{alternate_install}) ? $o->{mouse} : $o->{mouse}{alternate_install};
		install_gtk::createXconf($f, @$mouse{"XMOUSETYPE", "device"}, $o->{mouse}{wacom}[0], $Driver);

		push @options, if_(!$::globetrotter, '-kb'), '-allowMouseOpenFail', '-xf86config', $f if arch() !~ /^sparc/;
		push @options, 'tty7', '-dpms', '-s', '240';

		#- old weird servers: Xsun
		push @options, '-fp', '/usr/X11R6/lib/X11/fonts:unscaled' if $server =~ /Xsun/;
	    }

	    if (!fork()) {
		c::setsid();
		exec $server, @options or c::_exit(1);
	    }

	    #- wait for the server to start
	    foreach (1..5) {
		sleep 1;
		last if fuzzy_pidofs(qr/\b$server\b/);
		log::l("$server still not running, trying again");
	    }
	    my $nb;
	    foreach (1..60) {
		log::l("waiting for the server to start ($_ $nb)");
		log::l("Server died"), return 0 if !fuzzy_pidofs(qr/\b$server\b/);
		$nb++ if xf86misc::main::Xtest($wanted_DISPLAY);
		if ($nb > 2) { #- one succeeded test is not enough :-(
		    log::l("AFAIK X server is up");
		    return 1;
		}
		sleep 1;
	    }
	    log::l("Timeout!!");
	    0;
	};
	my @servers = qw(Driver:fbdev); #-)
	if ($::testing) {
	    @servers = 'Xnest';
	} elsif (arch() eq "alpha") {
	    require Xconfig::card;
	    my ($card) = Xconfig::card::probe();
	    Xconfig::card::add_to_card__using_Cards($card, $card->{type}) if $card && $card->{type};
	    @servers = $card->{server} || "TGA";
	    #-@servers = qw(SVGA 3DLabs TGA) 
	} elsif (arch() =~ /^sparc/) {
	    local $_ = cat_("/proc/fb");
	    if (/Mach64/) {
		@servers = qw(Mach64);
	    } elsif (/Permedia2/) {
		@servers = qw(3DLabs);
	    } else {
		@servers = qw(Xsun24);
	    }
	} elsif (arch() =~ /ia64|x86_64/) {
	    require Xconfig::card;
	    my ($card) = Xconfig::card::probe();
	    @servers = map { if_($_, "Driver:$_") } $card && $card->{Driver}, 'fbdev';
        }

        if (($::move || $::globetrotter) && !$::testing) {
            require move;
            require run_program;
            move::automatic_xconf($o);
            run_program::run('/sbin/service', 'xfs', 'start');
            @servers = $::globetrotter ? qw(Driver:fbdev) : qw(X_move);
	}

	foreach (@servers) {
	    log::l("Trying with server $_");
	    my ($prog, $Driver) = /Driver:(.*)/ ? ('Xorg', $1) : /Xsun|Xnest|^X_move$/ ? $_ : "XF86_$_";
	    if (/FB/i) {
		!$o->{vga16} && $o->{allowFB} or next;

		$o->{allowFB} = &$launchX($prog, $Driver) #- keep in mind FB is used.
		  and goto OK;
	    } else {
		$o->{vga16} = 1 if /VGA16/;
		&$launchX($prog, $Driver) and goto OK;
	    }
            $::move and print("can not launch graphical mode :(\n"), c::_exit(1);
	}
	return undef;
    }
  OK:
    $ENV{DISPLAY} = $wanted_DISPLAY;
    install_gtk::init_gtk($o);
    install_gtk::init_sizes();
    install_gtk::install_theme($o);
    install_gtk::create_logo_window($o);
    install_gtk::create_steps_window($o);

    $ugtk2::grab = 1;

    $o = (bless {}, ref($type) || $type)->SUPER::new($o);
    $o->interactive::gtk::new;
    $o;
}

sub enteringStep {
    my ($o, $step) = @_;

    printf "Entering step `%s'\n", $o->{steps}{$step}{text};
    $o->SUPER::enteringStep($step);
    install_gtk::update_steps_position($o);
}
sub leavingStep {
    my ($o, $step) = @_;
    $o->SUPER::leavingStep($step);
}


sub charsetChanged {
    my ($o) = @_;
    Gtk2->set_locale;
    install_gtk::load_font($o);
    install_gtk::create_steps_window($o);
}

#-######################################################################################
#- Steps Functions
#-######################################################################################
sub selectLanguage {
    my ($o, $first_time) = @_;
    $o->SUPER::selectLanguage;
  
    $o->ask_warn('',
formatAlaTeX(N("Your system is low on resources. You may have some problem installing
Mandrakelinux. If that occurs, you can try a text install instead. For this,
press `F1' when booting on CDROM, then enter `text'."))) if $first_time && availableRamMB() < 70; # 70MB

}

#------------------------------------------------------------------------------
sub selectMouse {
    my ($o, $force) = @_;
    my %old = %{$o->{mouse}};
    $o->SUPER::selectMouse($force) or return;
    my $mouse = $o->{mouse};
    $mouse->{type} eq 'none' ||
      $old{type}   eq $mouse->{type}   && 
      $old{name}   eq $mouse->{name}   &&
      $old{device} eq $mouse->{device} && !$force and return;

    while (1) {
	my $x_protocol_changed = mouse::change_mouse_live($mouse, \%old);
	mouse::test_mouse_install($mouse, $x_protocol_changed) and return;

	%old = %$mouse;
	$o->SUPER::selectMouse(1);
	$mouse = $o->{mouse};
    } 
}

sub reallyChooseGroups {
    my ($o, $size_to_display, $individual, $_compssUsers) = @_;

    my $w = ugtk2->new('');
    my $w_size = gtknew('Label', text => &$size_to_display);

    my $entry = sub {
	my ($e) = @_;

	gtknew('CheckButton', 
	       text => translate($e->{label}), 
	       tip => translate($e->{descr}),
	       active_ref => \$e->{selected},
	       toggled => sub { 
		   gtkset($w_size, text => &$size_to_display);
	       });
    };
    #- when restarting this step, it might be necessary to reload the compssUsers.pl (bug 11558). kludgy.
    if (!ref $o->{gtk_display_compssUsers}) { install_any::load_rate_files($o) }
    ugtk2::gtkadd($w->{window},
	   gtkpack_($w->create_box_with_title(N("Package Group Selection")),
		    1, $o->{gtk_display_compssUsers}->($entry),
		    1, '',
		    0, gtknew('HBox', children_loose => [
			  gtknew('Button', text => N("Help"), clicked => $o->interactive_help_sub_display_id('choosePackages')),
			  $w_size,
			  if_($individual,
			      gtknew('CheckButton', text => N("Individual package selection"), active_ref => $individual),
			  ),
			  gtknew('Button', text => N("Next"), clicked => sub { Gtk2->main_quit }),
			 ]),
		  ),
	  );
    $w->main;
    1;
}

sub choosePackagesTree {
    my ($o, $packages, $o_limit_medium) = @_;

    my $available = install_any::getAvailableSpace($o);
    my $availableCorrected = pkgs::invCorrectSize($available / sqr(1024)) * sqr(1024);

    my $common;
    $common = {             get_status => sub {
				my $size = pkgs::selectedSize($packages);
				N("Total size: %d / %d MB", pkgs::correctSize($size / sqr(1024)), $available / sqr(1024));
			    },
			    node_state => sub {
				my $p = pkgs::packageByName($packages, $_[0]) or return;
				pkgs::packageMedium($packages, $p)->{selected} or return;
				$p->arch eq 'src'                       and return;
				$p->flag_base                           and return 'base';
				$p->flag_installed && !$p->flag_upgrade and return 'installed';
				$p->flag_selected                       and return 'selected';
				return 'unselected';
			    },
			    build_tree => sub {
				my ($add_node, $flat) = @_;
				if ($flat) {
				    foreach (sort map { $_->name }
					     grep { !$o_limit_medium || pkgs::packageMedium($packages, $_) == $o_limit_medium }
					     grep { $_ && $_->arch ne 'src' }
					     @{$packages->{depslist}}) {
					$add_node->($_, undef);
				    }
				} else {
				    foreach my $root (@{$o->{compssUsers}}) {
					my (@firstchoice, @others);
					my %fl = map { ("CAT_$_" => 1) } @{$root->{flags}};
					foreach my $p (@{$packages->{depslist}}) {
					    !$o_limit_medium || pkgs::packageMedium($packages, $p) == $o_limit_medium or next;
					    my @flags = $p->rflags;
					    next if !($p->rate && any { any { !/^!/ && $fl{$_} } split('\|\|') } @flags);
					    $p->rate >= 3 ?
					      push(@firstchoice, $p->name) :
						push(@others,    $p->name);
					}
					my $root2 = translate($root->{path}) . '|' . translate($root->{label});
					$add_node->($_, $root2)                    foreach sort @firstchoice;