package ovhmlib;

do '../web-lib.pl';

# obtenir l'adresse ip de la machine
sub get_ip {
	$ip=`cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep IPADDR | sed s/IPADDR=//`;
	chop $ip;
	return $ip;
}

# ajouter une zone primaire dans bind
sub bind_ajouter {
	($nom, $verbeux) = @_;
	$ip = get_ip ();
	$serveur = &get_system_hostname();
	print "<br>Création de la zone dns dans Bind : /etc/named.conf /var/named/$nom.hosts" if ($verbeux eq "oui");

	($sec,$min,$heure,$mjour,$mois,$annee) = localtime(time);
	$serie = $annee + 1900 . ((($mois + 1) < 10) ? "0" : "") . $mois + 1 . ((($mjour) < 10) ? "0" : "") . $mjour . "01";

	open VARNAMED, "> /var/named/$nom.hosts";
	print VARNAMED <<EOF;
\$ttl 86400
$nom.   IN      SOA     $serveur. webmaster.$nom. (
                        $serie
                        21600
                        3600
                        604800
                        86400 )
                IN      NS      $serveur.
                IN      NS      ns.ovh.net.
                IN      MX      10 mail.$nom.
                IN      A       $ip
www             IN      A       $ip
mail            IN      A       $ip
smtp            IN      A       $ip
pop             IN      A       $ip
pop3            IN      A       $ip
imap            IN      A       $ip
sql             IN      A       $ip
mysql           IN      A       $ip
EOF
close VARNAMED;

open NAMEDCONF, ">>/etc/named.conf";
print NAMEDCONF <<EOF;

zone "$nom" {
        type master; 
        file "/var/named/$nom.hosts";
        };

EOF
}

# effacer une zone primaire dans bind
sub bind_effacer {
	my ($sav, $date, $domaine) = @_;
        system "cp /etc/named.conf $sav/named.conf.$date";
        system "rm -f /var/named/$domaine.hosts";

        my $conf = &foreign_call("bind8","get_config");
        my @zones = &foreign_call("bind8","find","zone", $conf);
        foreach $z (@zones) {
                my $v = $z->{'value'};
                if ($v eq "$domaine") {
                        $idb = $z->{'index'};
                        last;
                }
                $tttt .= $v;
        }
        my $zconf = $conf->[$idb];
        &lock_file("/etc/named.conf");
        my $lref = &read_file_lines("/etc/named.conf");
        splice(@$lref, $zconf->{'line'},$zconf->{'eline'} - $zconf->{'line'} + 1
);
        &flush_file_lines();
        &unlock_all_files();
}

1;
