#!/usr/bin/perl #use strict; use DBI(); use Config::IniFiles; use Getopt::Long; use vars qw($configfile $quiet $help $version $iplist); $ENV{'PATH'}='/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/usr/local/bin'; if(!$configfile) { $configfile = "/etc/lms/lms.ini"; } my $_version = '1.00 LMS Bind by H_C_K'; if($version) { print STDERR < $configfile; print @Config::IniFiles::errors; my $dbtype = $ini->val('database', 'type') || 'mysql'; my $dbhost = $ini->val('database', 'host') || 'localhost'; my $dbuser = $ini->val('database', 'user') || 'root'; my $dbpasswd = $ini->val('database', 'password') || ''; my $dbname = $ini->val('database', 'database') || 'lms'; my $named_file = $ini->val('dns','file') || '/etc/bind/named.conf.lms'; my $named_dir = $ini->val('dns','dir') || '/etc/bind/lms/'; my $dbase; my $utsfmt; if($dbtype =~ /mysql/) { $dbase = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 }); $utsfmt = "UNIX_TIMESTAMP()"; } elsif($dbtype eq "postgres") { $dbase = DBI->connect("DBI:Pg:dbname=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 }); $utsfmt = "EXTRACT(EPOCH FROM CURRENT_TIMESTAMP(0))"; } else { print STDERR "Fatal error: unsupported database type: $dbtype, exiting.\n"; exit 1; } unlink $named_file; # usuwamy stary plik konfiguracyjny my $main = $dbase->prepare("SELECT name,type FROM `domains`"); #nasze zapytanie $main->execute(); while (my $ref = $main->fetchrow_hashref()) { my $domena = $ref->{'name'}; my $type = $ref->{'type'}; open(PLIK, ">>$named_file"); #tworzymy plik konfiguracyjny print PLIK "zone \"$domena\" IN {\n"; print PLIK " type $type;\n"; print PLIK " file \"$named_dir$domena\";\n"; print PLIK " allow-query { any; };\n"; print PLIK " allow-transfer { any; };\n"; print PLIK "};\n\n"; close PLIK; } my $record = $dbase->prepare("SELECT lms.domains.name AS domain_name,lms.domains.id AS dom_id, lms.domains.type AS dom_type, lms.records.type AS type,content,ttl,prio,lms.records.name AS name_record FROM `lms`.`domains` LEFT JOIN `lms`.`records` ON (`lms`.`domains`.`id` = `lms`.`records`.`domain_id`)"); $record->execute(); while (my $rec = $record->fetchrow_hashref()) { my $domena = $rec->{'domain_name'}; my $czas = time(); my $TTL = $rec->{'ttl'}; my $wpis = $rec->{'name_record'}; my $ip = $rec->{'content'}; my $type = $rec->{'type'}; my $prio = $rec->{'prio'}; unlink ($named_dir.$domena); open(PLIK, ">>$named_dir$domena"); print PLIK "\$TTL $TTL\n"; print PLIK "\$ORIGIN $domena.\n"; print PLIK "@ IN SOA $domena. admin.$domena. ( \n"; print PLIK " $czas;; SERIAL \n"; print PLIK " 1200;; REFRESH \n"; print PLIK " 1200;; RETRY \n"; print PLIK " 2419200;; EXPIRE \n"; print PLIK " $TTL;; TTL \n\n"; close PLIK; } $record->execute(); while (my $ref = $record->fetchrow_hashref()) { my $domena = $ref->{'domain_name'}; my $wpis = $ref->{'name_record'}; my $ip = $ref->{'content'}; my $type = $ref->{'type'}; my $prio = $ref->{'prio'}; open(PLIK, ">>$named_dir$domena"); print PLIK "$wpis IN $type $ip\n"; close PLIK; } $dbase->disconnect();