#! /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 2008/01/05 15:55:24 afm Exp $
#
$hostname = "proxy.othello.ch";
$hostmaster = "afm.othello.ch";
@nameservers = ("proxy.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);
		} else {
			$name = &removeunderscore($name);
		}
		if ($name ne "") {
			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) {
		$c = shift @components;
		if (length($c) > 32) {
			return "";
		}
		$name = &escapechars($c) . "." . $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;
}

sub	removeunderscore {
	my $component = $_[0];
	# replace every illegal character by a dash
	$component =~ s/_/-/g;
	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/redirector/domains",	"127.0.0.8");
&handlefile("blacklists/redirector/urls",	"127.0.0.8");
&handlefile("blacklists/violence/domains",	"127.0.0.9");
&handlefile("blacklists/violence/urls",		"127.0.0.9");
&handlefile("blacklists/warez/domains",		"127.0.0.10");
&handlefile("blacklists/warez/urls",		"127.0.0.10");
&handlefile("blacklists/mail/domains",		"127.0.0.11");
&handlefile("blacklists/mail/urls",		"127.0.0.11");
&handlefile("blacklists/porn/domains",		"127.0.0.12");
&handlefile("blacklists/porn/urls",		"127.0.0.12");
&handlefile("ut1/adult/domains",		"127.0.1.1");
&handlefile("ut1/adult/urls", 			"127.0.1.1");
&handlefile("ut1/agressif/domains", 		"127.0.1.2");
&handlefile("ut1/agressif/urls", 		"127.0.1.2");
&handlefile("ut1/astrology/domains", 		"127.0.1.3");
&handlefile("ut1/astrology/urls", 		"127.0.1.3");
&handlefile("ut1/audio-video/domains", 		"127.0.1.4");
&handlefile("ut1/audio-video/urls", 		"127.0.1.4");
&handlefile("ut1/blog/domains", 		"127.0.1.5");
&handlefile("ut1/blog/urls", 			"127.0.1.5");
&handlefile("ut1/cleaning/domains", 		"127.0.1.6");
&handlefile("ut1/cleaning/urls", 		"127.0.1.6");
&handlefile("ut1/dangerous_material/domains", 	"127.0.1.7");
&handlefile("ut1/dangerous_material/urls", 	"127.0.1.7");
&handlefile("ut1/dating/domains", 		"127.0.1.8");
&handlefile("ut1/dating/urls", 			"127.0.1.8");
&handlefile("ut1/drogue/domains", 		"127.0.1.9");
&handlefile("ut1/drogue/urls", 			"127.0.1.9");
&handlefile("ut1/filehosting/domains", 		"127.0.1.10");
&handlefile("ut1/filehosting/urls", 		"127.0.1.10");
&handlefile("ut1/financial/domains", 		"127.0.1.11");
&handlefile("ut1/forums/domains", 		"127.0.1.12");
&handlefile("ut1/forums/urls", 			"127.0.1.12");
&handlefile("ut1/gambling/domains", 		"127.0.1.13");
&handlefile("ut1/gambling/urls", 		"127.0.1.13");
&handlefile("ut1/games/domains", 		"127.0.1.14");
&handlefile("ut1/games/urls", 			"127.0.1.14");
&handlefile("ut1/hacking/domains", 		"127.0.1.15");
&handlefile("ut1/hacking/urls", 		"127.0.1.15");
&handlefile("ut1/liste_bu/domains", 		"127.0.1.16");
&handlefile("ut1/liste_bu/urls", 		"127.0.1.16");
&handlefile("ut1/marketingware/domains", 	"127.0.1.17");
&handlefile("ut1/marketingware/urls", 		"127.0.1.17");
&handlefile("ut1/mixed_adult/domains", 		"127.0.1.18");
&handlefile("ut1/mixed_adult/urls", 		"127.0.1.18");
&handlefile("ut1/mobile-phone/domains", 	"127.0.1.19");
&handlefile("ut1/mobile-phone/urls", 		"127.0.1.19");
&handlefile("ut1/phishing/domains", 		"127.0.1.20");
&handlefile("ut1/phishing/urls", 		"127.0.1.20");
&handlefile("ut1/publicite/domains", 		"127.0.1.21");
&handlefile("ut1/publicite/urls", 		"127.0.1.21");
&handlefile("ut1/radio/domains", 		"127.0.1.22");
&handlefile("ut1/radio/urls", 			"127.0.1.22");
&handlefile("ut1/reaffected/domains", 		"127.0.1.23");
&handlefile("ut1/reaffected/urls", 		"127.0.1.23");
&handlefile("ut1/redirector/domains", 		"127.0.1.24");
&handlefile("ut1/redirector/urls", 		"127.0.1.24");
&handlefile("ut1/sect/domains", 		"127.0.1.25");
&handlefile("ut1/sexual_education/domains", 	"127.0.1.26");
&handlefile("ut1/sexual_education/urls", 	"127.0.1.26");
&handlefile("ut1/shopping/domains", 		"127.0.1.27");
&handlefile("ut1/shopping/urls", 		"127.0.1.27");
&handlefile("ut1/strict_redirector/domains", 	"127.0.1.28");
&handlefile("ut1/strict_redirector/urls", 	"127.0.1.28");
&handlefile("ut1/strong_redirector/domains", 	"127.0.1.29");
&handlefile("ut1/strong_redirector/urls", 	"127.0.1.29");
&handlefile("ut1/tricheur/domains", 		"127.0.1.30");
&handlefile("ut1/tricheur/urls", 		"127.0.1.30");
&handlefile("ut1/warez/domains", 		"127.0.1.31");
&handlefile("ut1/warez/urls", 			"127.0.1.31");
&handlefile("ut1/webmail/domains", 		"127.0.1.32");
&handlefile("ut1/webmail/urls", 		"127.0.1.32");

exit 0
