summaryrefslogtreecommitdiffstats
path: root/dhcp_wizard/scripts/Dhcpconf.pm
blob: e75cd855f47ce5e519a2e78ad7002e7e78c97349 (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
#!/usr/bin/perl

# DHCP Conf Parser

# Copyright (C) 2002 MandrakeSoft Arnaud Desmons <adesmons@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 Dhcpconf;
require "__WIZ_HOME__/common/scripts/IFCFG.pm";
#require "__WIZ_HOME__/common/scripts/DrakconnectConf.pm";
use MDK::Common;
use strict;
use standalone;

my $o = IFCFG->new();
my $wiz_ip_server = $o->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})/;

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 {
# FIXME : see check_range.sh
    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 $ENV{wiz_ip_range1} =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
    my $d2 = "$4" if $ENV{wiz_ip_range2} =~ /(\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 1}
    if (!$r2_trunc) { standalone::explanations("DHCP wizard : incorrect adress range 2"); return 1}
    if ($r1_trunc ne $s_trunc || $r2_trunc ne $s_trunc) { 
	standalone::explanations("DHCP wizard : range not in network");
	return 2;
    }
    if (!$d1 || !$d2 || $d1 > $d2) { standalone::explanations("DHCP wizard : bad range"); return 1}
    if ($ds >= $d1 && $ds <= $d2) { standalone::explanations("DHCP wizard : server in range"); return 3}
    10;
}

sub	check_dhcp {
    $o->is_dhcp() and return 1;
    0;
}

sub	do_it {
    my $wiz_domain_name = $o->network_get("DOMAINNAME"); 
    my $wiz_host_name = $o->network_get("HOSTNAME"); 
    my $wiz_gateway = $o->network_get("GATEWAY"); 
    my $wiz_dns = $o->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 != 10;
    my $wiz_ip_range1 = $ENV{wiz_ip_range1};
    my $wiz_ip_range2 = $ENV{wiz_ip_range2};
    my $wiz_ip_netmask = $o->itf_get("NETMASK");
    my $wiz_device = $o->default_itf();
# 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");

    $file = "/etc/rc.d/init.d/dhcpd";
# now patching etc/rc.d/init.d/dhcpd
    standalone::explanations("now patching etc/rc.d/init.d/dhcpd");
    if (!`grep INTERFACES $file`){
	MDK::Common::append_to_file($file, "\nINTERFACES=$wiz_device");
	MDK::Common::cp_af($file, $file . ".orig");
	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 (<OLD>) {
	    if (m|daemon\s*/usr/sbin/dhcp|) {
		print NEW "\tif [ -f /etc/sysconfig/dhcpd ]; then
\t\t. /etc/sysconfig/dhcpd
\t\tDEV=\$INTERFACES
\tfi\n";
	    }
	    print NEW $_;
	}
	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 = Vareqval->get($file);
	$mdk{lease_file} = "/var/dhcpd/dhcpd.leases";
	$mdk{interfaces} = $wiz_device;
	standalone::explanations("$file: lease_file = $mdk{lease_file}, interfaces = $mdk{interfaces}");
	Vareqval->commit($file, \%mdk);
	!$ENV{wiz_authoritative} and output($file, map { 
	    s|^\s*not\s*authoritative.*|\#$&|i;
	    $_
        } cat_ ("/etc/dhcpd.conf"));
    }

    system("/etc/rc.d/init.d/dhcpd restart");
    10;
}
1;