#!/usr/bin/perl


#    Anonymiser 0.9 - Quick and convenient system-wide anonymiser using Tor transparently
#    Copyright (C) 2011 Andy Dixon
#
#
#   ****************************************************************
#	** FOR USE ONLY WITH UBUNTU MAVERICK AT THE MOMENT            **
#   ****************************************************************
#   ** Remove the if block starting with if ($UBUNTU_VERSION..... **
#   ** and make sure Tor is installed for it to work on other     **
#	** Linux Operating Systems. IPTables rules *may* be different **
#   ****************************************************************
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


# the config file /etc/anonymiser.conf needs to have any internal (LAN) network
# subnet masks and also the user in which Tor runs as. 
# An example of the config file is as follows:

# @mynets = ("192.168.0.0/24","192.168.1.0/24","10.0.0.0/8");
# $TOR_UID = `id -u debian-tor`;
# 1;
#
# The 1 at the end is required. Without it the anonymiser will fail.
#

print "anonymiser Copyright (C) 2011 Andy Dixon
    This program comes with ABSOLUTELY NO WARRANTY; for details view the GPL license at http://www.gnu.org/licenses/gpl.html.
    This is free software, and you are welcome to redistribute it
    under certain conditions; for details, refer to the GPL license above.\n";

require "/etc/anonymiser.conf" or die "Error: Missing configuration file.\n";

$TRANS_PORT="9040";
$UBUNTU_VERSION=`lsb_release -c -s`;
chomp $TOR_UID;
chomp $UBUNTU_VERSION;


if ( $< == 0 ) {
	if ($UBUNUTU_VERSION == "maverick") {

		system("which tor >/dev/null");
		print $?;
		if ($? == 256 ) {
			print "Prerequisites not met. Installing....\n";
			open IN, '<', "/etc/apt/sources.list" or die;
			my @contents = <IN>;
			close IN;
	
			@contents = grep !/^$deb\ http\:\/\/deb.torproject.org\/torproject.org\ maverick\ main/, @contents;

			open OUT, '>', "/etc/apt/sources.list" or die;
			print OUT @contents;
			close OUT;
			system("echo \"deb http://deb.torproject.org/torproject.org maverick main\" >> /etc/apt/sources.list");
			system("gpg --keyserver keys.gnupg.net --recv 886DDD89");
			system("gpg --export A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89 | apt-key add -");
			system("apt-get update >/dev/null 2>&1");
			system("apt-get -y install tor >/dev/null 2>&1");
		}

		system("iptables -F");
		system("iptables -t nat -F");

		system("iptables -t nat -A OUTPUT -m owner --uid-owner $TOR_UID -j RETURN");
		system("iptables -t nat -A OUTPUT -d 127.0.0.0/9 -j RETURN");
		system("iptables -t nat -A OUTPUT -d 127.128.0.0/10 -j RETURN");
		foreach (@mynets) {
		 	system("iptables -t nat -A OUTPUT -d $_	-j RETURN");
			system("iptables -A OUTPUT -d $_ -j ACCEPT");
		 } 
		system("iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 53");
		system("iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $TRANS_PORT");
		system("iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT");
		system("iptables -A OUTPUT -d 127.0.0.0/8 -j ACCEPT");
		system("iptables -A OUTPUT -m owner --uid-owner $TOR_UID -j ACCEPT");
		system("iptables -A OUTPUT -j REJECT");

		open IN, '<', "/etc/tor/torrc" or die "Cant open transport config (+R). Epic Fail.\n";
		my @contents = <IN>;
		close IN;

		@contents = grep !/^$AutomapHostsOnResolve\ 1/, @contents;
		@contents = grep !/^$TransPort\ 9040/, @contents;
		@contents = grep !/^$DNSPort\ 53/, @contents;

		open OUT, '>', "/etc/tor/torrc" or die "Cant open transport config (W+). Epic Fail.\n";;
		print OUT @contents;
		close OUT;

		system("echo \"AutomapHostsOnResolve 1\" >>/etc/tor/torrc");
		system("echo \"TransPort 9040\" >>/etc/tor/torrc");
		system("echo \"DNSPort 53\" >>/etc/tor/torrc");
		system("/etc/init.d/tor restart");
		system("echo \"nameserver 127.0.0.1\" > /etc/resolv.conf");

	} else {
		print "Unsupported version: ".$UBUNTU_VERSION;
	}
} else {
	print "You must be running as root or with sudo privileges.\n";
}
