summaryrefslogtreecommitdiffstats
path: root/dhcp_wizard/Dhcp.pm
blob: 72a413f1705629dab31abf6d5f21ef8848052854 (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
#!/usr/bin/perl

# Drakwizard

# Copyright (C) 2002, 2003 Mandrakesoft
#
# Authors: Arnaud Desmons <adesmons@mandrakesoft.com>
#          Florent Villard <warly@mandrakesoft.com>
#
# 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 services;
use MDK::Wizard::Varspaceval;
use MDK::Wizard::Wizcommon;

my $wiz = new MDK::Wizard::Wizcommon;

my $wiz_ip_server = $wiz->{net}->itf_get("IPADDR");
my $wiz_tftpserverip = $wiz_ip_server;

my $o = {
	 name => N("DHCP Wizard"),
	 var => {
		 ip1 => '',
		 ip2 => '',
		 pxe => '1',
		 gateway => '',
		 wiz_authoritative => 0,
		 interface => $wiz->{net}->default_itf
		},
	 needed_rpm => [ 'dhcp-server' ],
	 defaultimage => "/usr/share/mcc/themes/default/dhcp_server-mdk.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."),
			   no_back => 1,
			   next => 'interface', no_back => 1,
			  },
	       interface => {
			     name => N("Interface the dhcp server must listen to"),
			     pre => sub {
			       if (!$wiz_ip_server) {
				 my $interface = 'eth0';
				 ($wiz_ip_server) = `/sbin/ip addr show dev $interface` =~ /^\s*inet\s+(\d+\.\d+\.\d+\.\d+)/m;
			       }
			       $o->{var}{interface} |= $wiz->{net}->default_itf;
			     },
			     data => [
				      { list => [ keys %{$wiz->{net}{itf}} ],  val => \$o->{var}{interface} },
				     ],
			     next => 'ip_range',
			     no_back => 1,
			    },
	       ip_range => {
			    name => N("Range of addresses used by DHCP") . "\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. (ie: 192.168.100.20 192.168.100.40)") . "\n\n" . N("If you want to enable PXE in your dhcp server please check the box (Pre-boot eXecution Environment, a protocol that allows computers to boot through the network)."),
			    pre => sub {
			      #($wiz_ip_server) = `/sbin/ip addr show dev $o->{var}{interface}` =~ /^\s*inet\s+(\d+\.\d+\.\d+\.\d+)/m;
			      ($wiz_ip_server) = $wiz->{net}{itf}{$o->{var}{interface}}{IPADDR};
			      $wiz_tftpserverip = $wiz_ip_server;
			      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})/;
			      $o->{var}{ip1} = compute_range($d, $s);
			      $o->{var}{ip2} = compute_range2($d, $s);
			    },
			    data => [
				     { label => N("Lowest IP address:"),  val => \$o->{var}{ip1} },
				     { label => N("Highest IP address:"), val => \$o->{var}{ip2} },
				     { label => N("Gateway IP address:"), val => \$o->{var}{gateway} },
				     { label => N("Enable PXE:"), type => 'bool', val => \$o->{var}{pxe} },
				    ],
			    next => 'summary',
			    no_back => 1,
			   },
	       dhcp_warning => {
		name => N("Warning") . "\n\n" . N("You are in dhcp, server may not work with your configuration."),
		ignore => 1,
	        next => 'ip_range',
	      },
	      ip_range_error => {
		name => N("Error") . "\n\n" . N("The IP range specified is not correct."),
		ignore => 1,
	        next => 'ip_range',
	      },
	      ip_range_warning => {
		name => N("Warning") . "\n\n" . N("The IP range specified is not in server address range."),
		ignore => 1,
	        next => 'summary',
	      },
	      server_in_range => {
		name => N("Error") . "\n\n" . N("The IP of the server must not be in range."),
		ignore => 1,
	        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:"),
			  pre => sub {
			    $o->{var}{pxeornot} = $o->{var}{pxe} ? N("enabled") : N("disabled");
			  },
			  data => [
				   { label => N("Lowest IP address:"), val_ref => \$o->{var}{ip1} },
				   { label => N("Highest IP address:"), val_ref => \$o->{var}{ip2} },
				   { label => N("Gateway IP address:"), val_ref => \$o->{var}{gateway} },
				   { label => N("Interface:"), val_ref => \$o->{var}{interface} },
				   { label => N("Enable PXE:"), val_ref => \$o->{var}{pxeornot} },
				  ],
			  post => \&do_it,
			  next => 'end',
			 },
	       end => {
		       name => N("Congratulations") . "\n\n" . N("The wizard successfully configured the DHCP services."),
		       end => 1,
		       no_back => 1,
		      },
	       error_end => {
			     name => N("Failed"),
                             data => [ { label => N("Relaunch drakwizard, and try to change some parameters.") } ],
                             no_back => 1,
                             end => 1,
                            },
	      };



sub compute_range {
  my ($d, $s) = @_;
    my $n;
    if ($d <= 64) { $n = "65" }
    elsif ($d <= 128) { $n = "129" }
    else { $n = "1" }
    "$s.$n";
}

sub compute_range2 {
  my ($d, $s) = @_;
    my $n;
    if ($d <= 128) { $n = "254" }
    elsif ($d > 192) { $n = "192" }
    else { $n = "128" }
    "$s.$n";
}

sub check {
    my $check_ip = sub { return $_[0] < 0 || $_[0] > 255 ? 0 : 1 };
    my $r1_trunc = "$1.$2.$3" if $o->{var}{ip1} =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
    foreach ($1, $2, $3, $4) { $check_ip->($_) or return 'ip_range_error' }
    my $r2_trunc = "$1.$2.$3" if $o->{var}{ip2} =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
    foreach ($1, $2, $3, $4) { $check_ip->($_) or return 'ip_range_error' }
    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 $in = 'interactive'->vnew('su', 'DHCP');
    check_starts_on_boot($in, 'dhcpd');
    my $wiz_domain_name = $wiz->{net}->network_get("DOMAINNAME");
    my $wiz_host_name = $wiz->{net}->network_get("HOSTNAME");
    my $wiz_dns = $wiz->{net}->network_get("dnsServer");
    if ($wiz_dns eq '127.0.0.1') {
    	$wiz_dns = $wiz_ip_server;
    }
    if (!$o->{var}{gateway}) {
        my $t = `LC_ALL=C /sbin/ip route list scope global`;
	($o->{var}{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");
    if (!$wiz_ip_netmask) { $wiz_ip_netmask = "255.255.255.0" }
    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: $!";
	chomp($tmp);
	output($tmp,
	       (map {
		   if (m|INTERFACE|) {
		       "# $_";
		   }
		   # FIXME: we probably lack a "else":
		   $_;
		} cat_or_die($file)),
	       "\n# Added by drakwizard\nINTERFACES=$wiz_device");
	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__|$o->{var}{gateway}|g;
	s|__tftpserverip__|$wiz_tftpserverip|g;
	s|__dhcpd_interface__|$wiz_device|g;
	$_;
    } cat_("/usr/share/wizards//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);
	!$o->{var}{wiz_authoritative} and output($file, map {
	    s|^\s*not\s*authoritative.*|#$&|i;
	    $_;
        } cat_("/etc/dhcpd.conf"));
    }

    if ($o->{var}{pxe} eq '0') {
      substInFile {
	s/#\s+deny members of "PXE"/deny members of "PXE"/;
      } "/etc/dhcpd.conf";
    }

    $o->{var}{gateway} or substInFile {
      s/#\s+doption routers.*//;
    } "/etc/dhcpd.conf";

    services::restart_or_start('dhcpd');
    check_started('dhcpd');
}

sub new {
    my ($class) = @_;
    bless $o, $class;
}

1;