#!/usr/bin/perl =head1 NAME Bind_config.pl - 12.6.08 terminalfool@yahoo.com =head1 DESCRIPTION Formats csv domain lists into master zone definitions for inclusion in a BIND config file. It merges multiple lists, processes an exclusion list, and removes any duplications that would cause BIND startup to fail. Intended to run periodically within a cron process. Ban list is loaded from [https://pgl.yoyo.org]. =over 4 =item * Outputs zone definitions of the form: zone "googlesyndication.com" { type master; notify no; file "null.zone.file"; }; where null.zone.file is a localhost definition such as: $TTL 24h @ IN SOA example.homeip.net. unused-email.example.homeip.net. ( 2003052800 86400 300 604800 3600 ) @ IN NS ns1.example.homeip.net. @ IN A 127.0.0.1 * IN A 127.0.0.1 =cut #!/usr/bin/perl use strict; use LWP::Simple; use Text::CSV::Simple; use Proc::Simple; use Proc::Killall; use Time::localtime; #pgl.yoyo.org feed my $url = 'https://pgl.yoyo.org/adservers/serverlist.php?hostformat=one-line&showintro=0&mimetype=plaintext'; #bind config files /var/named/... my $adlist = '/var/named/adslist.csv'; #pgl.yoyo exclusion list my $appendlist = '/var/named/adslist_append.csv'; #additonal list my $removelist = '/var/named/adslist_remove.csv'; #ads you simply must have my $outfile = '/var/named/adslist.txt'; #include file for named.conf #used with Proc::Killall for stopping and starting named my $proc = Proc::Simple->new(); my $file; my %list; my $csv = Text::CSV::Simple->new; $csv->add_trigger(before_parse => sub { my ($self, $line) = @_; die if $line =~ /#|^",|\s|^\n/; }); $csv->add_trigger(after_parse => sub { my ($self, $data) = @_; foreach (@$data) { $self->_file eq $removelist ? delete $list{$_} : $list{$_}++; } }); if (is_success(getstore($url, $adlist))) { foreach $file ($adlist,$appendlist,$removelist) { $csv->read_file($file); } open (OUT, ">$outfile") or die "Couldn't open output file: $!"; print OUT '// bind config generated ',ctime(),' using ',$adlist,', ',$appendlist,', ',$removelist,"\n\n"; foreach my $key (sort(keys %list)) { print OUT 'zone "',$key,'" { type master; notify no; file "null.zone.file"; };',"\n"; } close OUT; print "BIND ad zones updated.\n"; print "Halting BIND.\n" if killall('KILL','/usr/sbin/named'); print "Restarting BIND.\n" if $proc->start('/usr/sbin/named -4'); }