#!/usr/bin/perl # Drakwizard # Copyright (C) 2002 Arnaud Desmons # 2003 Florent Villard # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. package MDK::Wizard::Dhcp; use strict; use common; use MDK::Wizard::Varspaceval; use services; use MDK::Wizard::Wizcommon; my $wiz = new MDK::Wizard::Wizcommon; my $wiz_ip_server = $wiz->{net}->itf_get("IPADDR"); my $d = "$4" if $wiz_ip_server =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/; my $s = "$1.$2.$3" if $wiz_ip_server =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/; my $o = { name => N('DHCP Wizard'), var => { ip1 => '', ip2 => '', interface => $wiz->{net}->default_itf() }, needed_rpm => [ 'dhcp-server' ], defaultimage => "$ENV{__WIZ_HOME__}dhcp_wizard/images/DHCP.png" }; $o->{pages} = { welcome => { name => N('DHCP Wizard') . "\n\n\n" . N('DHCP is a service that automatically assigns networking addresses to your workstations.') . "\n\n\n" . N('This wizard will help you configuring the DHCP services of your server.'), post => sub { $wiz->check_dhcp }, no_back => 1, next => 'ip_range' }, ip_range => { name => N('Range of addresses used by dhcp') . "\n\n\n" . N('Select the range of addresses assigned to the workstations by the DHCP service; unless you have special needs, you can safely accept the proposed values.'), pre => sub { $o->{var}{ip1} ||= compute_range(); $o->{var}{ip2} ||= compute_range2() }, post => \&check, data => [ { label => N('Lowest IP Address:'), val => \$o->{var}{ip1} }, { label => N('Highest IP Address:'), val => \$o->{var}{ip2} }, ], next => 'summary' }, interface => { name => N('Interface the dhcp server must listen to'), pre => sub { $o->{var}{interface} ||= $wiz->{net}->default_itf() }, data => [ { list => [ keys %{$wiz->{net}{itf}} ], val => \$o->{var}{interface} }, ], next => 'summary' }, dhcp_warning => { name => N('Warning'), data => [ { label => N('You are in dhcp, server may not work with your configuration.') } ], next => 'ip_range' }, ip_range_error => { name => N('Error'), data => [ { label => N('The IP range specified is not correct.') } ], next => 'ip_range' }, ip_range_warning => { name => N('Warning'), data => [ { label => N('The IP range specified is not in server address range.') } ], next => 'summary' }, server_in_range => { name => N('Error'), data => [ { label => N('The IP of the server must not be in range.') } ], next => 'ip_range' }, summary => { name => N('Configuring the DHCP Server') . "\n\n" . N('The wizard collected the following parameters needed to configure your DHCP service:'), data => [ { label => N('Lowest IP Address:'), fixed_val => \$o->{var}{ip1} }, { label => N('Highest IP Address:'), fixed_val => \$o->{var}{ip2} }, { label => N('Interface:'), fixed_val => \$o->{var}{interface} }, ], post => \&do_it, next => 'end' }, end => { name => N('Congratulations'), data => [ { label => N('The wizard successfully configured the DHCP services of your server.') } ], end => 1, next => 0 }, }; sub compute_range { my $n; if ($d <= 64) { $n = "65" } elsif ($d <= 128) { $n = "129" } else { $n = "1"} "$s.$n"; } sub compute_range2 { my $n; if ($d <= 128) { $n = "254" } elsif ($d > 192) { $n = "192" } else { $n = "128"} "$s.$n"; } sub check { my $r1_trunc = "$1.$2.$3" if $o->{var}{ip1} =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/; my $r2_trunc = "$1.$2.$3" if $o->{var}{ip2} =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/; my $d1 = "$4" if $o->{var}{ip1} =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/; my $d2 = "$4" if $o->{var}{ip2} =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/; my $s_trunc = "$1.$2.$3" if $wiz_ip_server =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/; my $ds = "$4" if $wiz_ip_server =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/; if (!$r1_trunc) { standalone::explanations("DHCP wizard : incorrect adress range 1"); return 'ip_range_error'} if (!$r2_trunc) { standalone::explanations("DHCP wizard : incorrect adress range 2"); return 'ip_range_error'} if ($r1_trunc ne $s_trunc || $r2_trunc ne $s_trunc) { standalone::explanations("DHCP wizard : range not in network"); return 'ip_range_warning'; } if (!$d1 || !$d2 || $d1 > $d2) { standalone::explanations("DHCP wizard : bad range"); return 'ip_range_error'} if ($ds >= $d1 && $ds <= $d2) { standalone::explanations("DHCP wizard : server in range"); return 'server_in_range'} return 'interface' if keys %{$wiz->{net}{itf}} > 1; 0 } sub do_it { $::testing and return; my $wiz_domain_name = $wiz->{net}->network_get("DOMAINNAME"); my $wiz_host_name = $wiz->{net}->network_get("HOSTNAME"); my $wiz_gateway = $wiz->{net}->network_get("GATEWAY"); my $wiz_dns = $wiz->{net}->network_get("dnsServer"); if ($wiz_dns eq '127.0.0.1') { $wiz_dns = $wiz_ip_server } if (!$wiz_gateway) { my $t = `LC_ALL=C /sbin/ip route list scope global`; ($wiz_gateway) = $t =~ /default via (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) dev/; } my $wiz_ip_net = "$1.$2.$3.0" if $wiz_ip_server =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/; my $err = check(); return $err if $err != 0; my $wiz_ip_range1 = $o->{var}{ip1}; my $wiz_ip_range2 = $o->{var}{ip2}; my $wiz_ip_netmask = $wiz->{net}->itf_get("NETMASK"); my $wiz_device = $o->{var}{interface}; # patch to rewrite when got new file about dhcp with INTERFACES value # currently, I put the device to configure as dhcp server # in /etc/sysconfig/dhcpd #[ -f /etc/sysconfig/dhcpd ] && cp -f /etc/sysconfig/dhcpd /var/tmp/wiz_bck/orig/dhcpd my $file = "/etc/sysconfig/dhcpd"; -f $file and MDK::Common::cp_af($file, $file.".orig"); standalone::explanations("now patching etc/sysconfig/dhcpd"); if (!`grep INTERFACES $file`){ my $tmp = `/bin/mktemp /tmp/Dhcpconf.XXXXXX` or die "can't make a temp file: $!"; open(NEW, "> $tmp") or die "can't open $tmp: $!"; open(OLD, "< $file") or die "can't open default: $!"; while () { if (m|INTERFACE|) { print NEW "# $_"; } print NEW $_; } MDK::Common::append_to_file($tmp, "\n# Added by drakwizard\nINTERFACES=$wiz_device"); close(OLD); close(NEW); chomp($tmp); system("mv $tmp $file"); } $file = "/etc/dhcpd.conf"; -f $file and MDK::Common::cp_af($file, $file.".orig"); output($file, map { s|__hname__|$wiz_host_name|g; s|__dns__|$wiz_dns|g; s|__net__|$wiz_ip_net|g; s|__ip__|$wiz_ip_server|g; s|__mask__|$wiz_ip_netmask|g; s|__rng1__|$wiz_ip_range1|g; s|__rng2__|$wiz_ip_range2|g; s|__dname__|$wiz_domain_name|g; s|__gateway__|$wiz_gateway|g; $_; } cat_ ("__WIZ_HOME__/dhcp_wizard/scripts/dhcpd.conf.default")); standalone::explanations("$file: hname = $wiz_host_name, net = $wiz_ip_net, ip = $wiz_ip_server, mask = $wiz_ip_netmask, rng1 = $wiz_ip_range1, rng2 = $wiz_ip_range2, dname = $wiz_domain_name"); MDK::Common::touch("/var/dhcpd/dhcpd.leases"); # modifying webmin config $file="/etc/webmin/dhcpd/config"; if (-f $file) { my %mdk = MDK::Wizard::Varspaceval->get($file); $mdk{lease_file} = "/var/dhcpd/dhcpd.leases"; $mdk{interfaces} = $wiz_device; standalone::explanations("$file: lease_file = $mdk{lease_file}, interfaces = $mdk{interfaces}"); MDK::Wizard::Varspaceval->commit($file, \%mdk); !$ENV{wiz_authoritative} and output($file, map { s|^\s*not\s*authoritative.*|\#$&|i; $_ } cat_ ("/etc/dhcpd.conf")); } if (services::is_service_running('dhcpd')) { services::restart('dhcpd') } else { services::start('dhcpd') } 10; } sub new { my ($class, $conf) = @_; bless { o => $o, }, $class; } 1;