summaryrefslogtreecommitdiffstats
path: root/dns_wizard/Bind.pm
blob: e7c3b53459f734ac6ee56615eb95995d5d794613 (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
#!/usr/bin/perl

# Drakwizard

# Copyright (C) 2003 Mandrakesoft
#
# Author: 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::Bind;
use lib qw(/usr/lib/libDrakX);
use strict;

use common;
use services;
use MDK::Wizard::Wizcommon;

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

my $o = { 
    name => N("DNS Configuration Wizard"),
	var => { 
	    ext_dns1 => '',
	    ext_dns2 => ''
	},
	needed_rpm => [ 'bind' ],
	defaultimage => "$ENV{__WIZ_HOME__}dns_wizard/images/DNS.png"
    };

$o->{pages} = { 
		  welcome => {
		      name => N("DNS configuration wizard") . "\n\n" . N("DNS (Domain Name Server) is the service that maps an IP address of a machine with an internet host name.") . "\n\n" . N("This wizard will help you configuring the DNS services of your server. This configuration will provide a local DNS service for local computers names, with non-local requests forwarded to an outside DNS."),
		  no_back => 1,
	          next => 'dns_ip'
	      },
	      dns_ip => {
	          name => N("DNS Server Addresses") . "\n\n" . N("DNS will allow your network to communicate with the Internet using standard internet host names.  In order to configure DNS, you must provide the IP address of primary and secondary DNS server; usually this address are given by your Internet provider.") . N("IP addresses are a dotted list of four numbers smaller than 256"), 
                  pre => sub {
		      $o->{var}{ext_dns1} ||= $dns1;
		      $o->{var}{ext_dns2} ||= $dns2
		  },
                  data => [
		      { label => N("Primary DNS Address"),  val => \$o->{var}{ext_dns1} },
		      { label => N("Secondary DNS Address:"), val => \$o->{var}{ext_dns2} },
		      ],
	          next => 'summary'
	      },
	      ip_error => {
		name => 'Error.' . "\n\n" . N("This is not a valid address... press next to continue"),
		ignore => 1,
	        next => 'dns_ip'
	      },
	      ip_warning => {
		name => 'Warning.' . "\n\n" .  N("You have entered an empty address for the DNS server.") . "\n\n" .
		      N("Your setting could be accepted, but you will not be able to identify machine names outside your local network.") . "\n\n" .
		      N("Press next to leave these values empty, or back to enter a value."),
	        next => 'summary'
	      },
	      summary => {
	        name => N("Configuring the DNS Server") . "\n\n" . N("The wizard collected the following parameters needed to configure your DNS service:") . "\n\n" . N("To accept these values, and configure your server, click the Next button or use the Back button to correct them"),
                data => [
		      { label => N("Primary DNS Address:"), fixed_val => \$o->{var}{ext_dns1} },
		      { label => N("Secondary DNS Address:"), fixed_val => \$o->{var}{ext_dns2} },
		  ],
		post => \&do_it,
	        next => 'end'
	      },
	      end => { 
	        name => N("Congratulations"),
                data => [ { label => N("The wizard successfully configured the DNS services of your server.") } ], 
		end => 1,
	        next => 0
	  },
};

sub up_serial {
    my	($file) = @_;

    my (undef, undef, undef, $mday, $mon, $year) = gmtime(time);
    $year += 1900;
    my	$serial_nbm = sprintf "%4dY%2dm%2d00", $year, $mon, $mday;
    output($file, map { 
	my $line = $_;
	if (/^(\s*)(\d*)(\s*;\s*Serial.*)$/) {
	    my $serial_f = $2;
	    $serial_f++;
	    if ($serial_f <= $serial_nbm) {
		$serial_f = $serial_nbm;
		chomp($serial_f);
		$line = "$1$serial_f$3\n";
	    }
	}
	$line;
    } cat_($file));
}

sub do_it {
    $::testing and return;
    my $wiz_ip_server = $wiz->{net}->itf_get("IPADDR");
    my $wiz_domain_name = $wiz->{net}->network_get("DOMAINNAME"); 
    my $wiz_host_name = $wiz->{net}->network_get("HOSTNAME"); 
    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})/;
    my $host = "$1" if $wiz_host_name =~ /(.*?)\..*/;
    my $reversnet = "$3.$2.$1" if $wiz_ip_server =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
    my $file = "/etc/host.conf";

    -f $file and MDK::Common::cp_af($file, $file.".orig");
    MDK::Common::cp_af("__WIZ_HOME__/dns_wizard/scripts/host.conf.default", $file);

    $file = "/etc/named.conf";
    -f $file and MDK::Common::cp_af($file, $file.".orig");

    my $ispns1 = $o->{var}{ext_dns1} || "// __ISPN1__";
    my $ispns2 = $o->{var}{ext_dns2} || "// __ISPN2__";

    output($file, map { 
	s|__ISPNS1__|$ispns1|g;
	s|__ISPNS2__|$ispns2|g;
	s|__dname__|$wiz_domain_name|g;
	s|__revnet__|$reversnet|g;
	s|__net__|$s_trunc|g;
	$_;
    } cat_("__WIZ_HOME__/dns_wizard/scripts/named.conf.default"));

    standalone::explanations("$file : DNS1: $ispns1 DNS2: $ispns2");
# Bug fix for bind 9:
    if (! -f "/etc/rndc.key") { system("touch /etc/rndc.key") or die "can not touch /etc/rndc.key"};

# root.hints
    $file="/var/named/root.hints";
    -f $file and MDK::Common::cp_af($file, $file . ".orig");
	MDK::Common::cp_af("__WIZ_HOME__/dns_wizard/scripts/root.hints.default", $file);
	standalone::explanations("$file clobbered");
# 127.0.0.rev
    $file="/var/named/127.0.0.rev";
    -f $file and MDK::Common::cp_af($file, $file . ".orig");
    output($file, map {
	s|__hname__|$wiz_host_name|g;
	$_;
    } cat_("__WIZ_HOME__/dns_wizard/scripts/127.0.0.rev.default"));
    standalone::explanations("$file : hostname: $wiz_host_name");
    up_serial($file);

# $ipnet.rev
    $file = "/var/named/$s_trunc.rev";
    -f $file and MDK::Common::cp_af($file, $file.".orig");
    output($file, map {
	s|__dname__|$wiz_domain_name|g;
	s|__hname__|$wiz_host_name|g;
	s|__revnet__|$reversnet|g;
	s|__nb__|$ds|g;
	$_;
    } cat_("__WIZ_HOME__/dns_wizard/scripts/ipnet.rev.default"));
    up_serial($file);
    standalone::explanations("$file : domain: $wiz_domain_name hostname: $wiz_host_name revnet: $reversnet
nb: $ds");
# $domain.db
    $file = "/var/named/$wiz_domain_name.db";
    -f $file and MDK::Common::cp_af($file, $file.".orig");
    output($file, map {
	s|__dname__|$wiz_domain_name|g;
	s|__hname__|$wiz_host_name|g;
	s|__ip__|$wiz_ip_server|g;
	s|__host__|$host|g;
	s|__nb__|$ds|g;
	$_;
    } cat_("__WIZ_HOME__/dns_wizard/scripts/domain.db.default"));
    up_serial($file);
    standalone::explanations("$file : domain: $wiz_domain_name hostname: $wiz_host_name revnet: $reversnet
nb: $ds");
# resolv.conf
    $file = "/etc/resolv.conf";
    -f $file and MDK::Common::cp_af($file, $file.".orig");
    open(NEW, "> $file");
    print NEW "domain $wiz_domain_name\n";
    print NEW "nameserver $wiz_ip_server\n";
    standalone::explanations("$file : domain: $wiz_domain_name nameserver: $wiz_ip_server");
    services::start_service_on_boot('named');
    if (services::is_service_running('named')) {
	services::restart('named') 
    } else {
        services::start('named')
    }
}

sub new {
    my ($class, $conf) = @_;
    my $file = "/etc/named.conf";
    if (-f $file) {
	open my $NEW, $file;
	DNS : while (<$NEW>) {
	    $dns2 = "";
	    if (m/^\s*options\s*\{/s...m/^\s*\}\;/s ) {
		if (m/^\s*forwarders\s*\{/s...m/^\s*\}\;/s ) {
		    if (/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/) {
			$dns2 = "$1.$2.$3.$4";
			$dns1 and next DNS;
			$dns1 = $dns2;
		    }
		}
	    }
	}
	close $NEW;
    }
    if (!$dns1) {
	$dns1 = $wiz->{net}->network_get("dnsServer");
	if ($dns1 eq '127.0.0.1') { 
	    $dns1 = $wiz->{net}->network_get("dnsServer2");
	    $dns2 = $wiz->{net}->network_get("dnsServer3") || '127.0.0.1'
	} else {
	    $dns2 = $wiz->{net}->network_get("dnsServer2")
	}
    }

    bless {
	    o   => $o,
       }, $class;
}

1;