#! /usr/bin/perl
#
# blacklist2zone -- convert a blacklist to a DNS zone file for use by dnsbl
#
# (c) 2004 Dr. Andreas Mueller, Beratung und Entwicklung
#
# This file is part of the mod_dnsbl distribution, and is distributed unter
# the GNU General Public License GPL.
# Please see http://software.othello.ch/mod_dnsbl for a full copy of the
# license.
#
# $Id: blacklist2zone,v 1.2 2004/03/08 23:41:34 afm Exp $
#
$hostname = "timon.othello.ch";
$hostmaster = "afm.othello.ch";
@nameservers = ("timon.othello.ch", "cesario.othello.ch");

# read a file of URLs and construct a zone file from this
sub	handlefile {
	local ($filename, $ip) = @_;

	# open the file
	open URLFILE, "<".$filename || die "cannot open file: $!\n";

	# go throught file file, analysing every URL in it
	while ($line = <URLFILE>) {
		next if ($line =~ m/^\s*#/);
		next if ($line =~ m/^\s*$/);
		chop $line;
		$name = $line;
		if ($line =~ m/\//) {
			$name = &handleurl($name);
		}
		printf("%s	IN	A	%s\n", $name, $ip);
		printf("*.%s	IN	A	%s\n", $name, $ip);
	}

	# close the file
	close(URLFILE)
}

# process an URL
sub	handleurl {
	my $name = $_[0];
	@components = split('/', $name);
	$name = shift @components;
	printf("%s\tIN\tA\t127.255.255.255\n", $name);
	while ($#components >= 0) {
		$name = &escapechars(shift @components) . "." . $name;
	}
	return $name;
}

# convert illegal DNS characters to dashes
sub	escapechars {
	my $component = $_[0];
	# replace every illegal character by a dash
	$component =~ s/[^a-zA-Z0-9\-]/-/g;
	# a name component may not begin or end with a dash
	$component =~ s/^-/X/;
	$component =~ s/-$/X/;
	return $component;
}

# convert URL files according to the following IP assignments
printf("\$TTL 86400\n");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$serial = sprintf("%04d%02d%02d1", $year + 1900, $mon, $mday);
printf("@\tIN\tSOA\t%s.\t%s. ( %s 10800 3600 604800 3600 )\n",
	$hostname, $hostmaster, $serial);
foreach $ns (@nameservers) {
	printf("@\tIN\tNS\t%s.\n", $ns);
}
&handlefile("blacklists/ads/domains",		"127.0.0.1");
&handlefile("blacklists/ads/urls",		"127.0.0.1");
&handlefile("blacklists/aggressive/domains",	"127.0.0.2");
&handlefile("blacklists/aggressive/urls",	"127.0.0.2");
&handlefile("blacklists/audio-video/domains",	"127.0.0.3");
&handlefile("blacklists/audio-video/urls",	"127.0.0.3");
&handlefile("blacklists/drugs/domains",		"127.0.0.4");
&handlefile("blacklists/drugs/urls",		"127.0.0.4");
&handlefile("blacklists/gambling/domains",	"127.0.0.5");
&handlefile("blacklists/gambling/urls",		"127.0.0.5");
&handlefile("blacklists/hacking/domains",	"127.0.0.6");
&handlefile("blacklists/hacking/urls",		"127.0.0.6");
&handlefile("blacklists/proxy/domains",		"127.0.0.7");
&handlefile("blacklists/proxy/urls",		"127.0.0.7");
&handlefile("blacklists/violence/domains",	"127.0.0.8");
&handlefile("blacklists/violence/urls",		"127.0.0.8");
&handlefile("blacklists/warez/domains",		"127.0.0.9");
&handlefile("blacklists/warez/urls",		"127.0.0.9");
&handlefile("blacklists/mail/domains",		"127.0.0.10");
&handlefile("blacklists/porn/domains",		"127.0.0.11");
&handlefile("blacklists/porn/urls",		"127.0.0.11");
&handlefile("adult/domains",			"127.0.0.12");
&handlefile("adult/urls",			"127.0.0.12");

exit 0
