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

package Dhcpconf;
require "__WIZ_HOME__/common/scripts/Vareqval.pm";
use MDK::Common;
use strict;

sub	do_it {
    my %mdk = Vareqval->get("/etc/sysconfig/mdk_serv");
    my $wiz_domain_name = $mdk{wiz_domain_name}	if defined $mdk{wiz_domain_name} or
	die "wiz_domain_name not in /etc/sysconfig/mdk_serv";
    my $wiz_host_name = $mdk{wiz_host_name}	if defined $mdk{wiz_host_name} or 
	die "wiz_host_name not in /etc/sysconfig/mdk_serv";
    
    defined $ENV{wiz_ip_range1} or die "wiz_ip_range1 not defined : $!";
    defined $ENV{wiz_ip_range2} or die "wiz_ip_range2 not defined : $!";
    my $wiz_ip_range1 = $ENV{wiz_ip_range1};
    my $wiz_ip_range2 = $ENV{wiz_ip_range2};
    $mdk{wiz_ip_range1} = $wiz_ip_range1;
    $mdk{wiz_ip_range2} = $wiz_ip_range2;
    
    my $wiz_device = $mdk{wiz_device}		if defined $mdk{wiz_device} or
	die "wiz_device not in /etc/sysconfig/mdk_serv";
    Vareqval->commit("/etc/sysconfig/mdk_serv", \%mdk);
    my %mdk = Vareqval->get("/etc/sysconfig/network-scripts/ifcfg-".$wiz_device);
    my $wiz_ip_net = $mdk{NETWORK}		if defined $mdk{NETWORK} or 
	die "NETWORK not in /etc/sysconfig/network-scripts/ifcfg-".$wiz_device;
    my $wiz_ip_server = $mdk{IPADDR}		if defined $mdk{IPADDR} or 
	die "IPADDR not in /etc/sysconfig/network-scripts/ifcfg-".$wiz_device;
    my $wiz_ip_netmask = $mdk{NETMASK}		if defined $mdk{NETMASK} or 
	die "NETMASK not in /etc/sysconfig/network-scripts/ifcfg-".$wiz_device;    
# 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";
    MDK::Common::cp_af($file, $file.".orig");
    open(NEW, "> $file")				or die "can not open $file: $!";
    print NEW "INTERFACES=$wiz_device\n";
    close($file)					or die "can not close $file: $!";
    $file = "/etc/rc.d/init.d/dhcpd";
# now patching etc/rc.d/init.d/dhcpd
    if (!`grep INTERFACES $file`){
	MDK::Common::cp_af($file, $file . ".orig");
	my  $tmp = `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);
	MDK::Common::cp_af($tmp, $file);
    }
    $file = "/etc/dhcpd.conf";
    MDK::Common::cp_af($file, $file.".orig");
    my  $tmp = `mktemp /tmp/Dhcpdconf.XXXXXX` or
	die "can't make a temp file: $!";
    open(NEW, "> $tmp")	or
	die "can't open $tmp: $!";
    open(OLD, "< __WIZ_HOME__/dhcp_wizard/scripts/dhcpd.conf.default") or
	die "can't open default: $!";
    while (<OLD>) {
	s|__hname__|$wiz_host_name|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;
	print NEW $_;
    }
    close(OLD);
    close(NEW);
    MDK::Common::cp_af($tmp, $file);
    system("touch /var/dhcpd/dhcpd.leases")	or die "can not touch /var/dhcpd/dhcd.leases: $!";
# modifying webmin config
    $file="/etc/webmin/dhcpd/config";
    if (-f $file) {
	%mdk = Vareqval->get($file);
	$mdk{lease_file} = "/var/dhcpd/dhcpd.leases";
	$mdk{interfaces} = $wiz_device;
    }
    system("/etc/rc.d/init.d/dhcpd restart");
}