Image: Ubuntu and 
            DansGuardian--Great for Libraries!

Ubuntu 12.04 Precise Pangolin and DansGuardian for
Filtering and Public Wireless Access

By John C. Rucker

Originally published on 2 November 2012. Last modified on 2 June 2015.
This guide is a quick step-by-step guide to how
I implement DansGuardian on Ubuntu systems. This 
one is all command-line with no hand-holding. For
something easier, though out of date, try some 
of my earlier tutorials.

Introduction

Below are my local install notes for putting together a DansGuardian filtering system on Ubuntu 12.04 Precise Pangolin, including multiple filtering profiles and statistics. You'll need to be pretty comfortable with Linux and the command line to replicate this. But it's really nothing too complicated. Basic steps needing no explanation for someone experienced in Linux are omitted. Using this guide, it takes me about 2 hours to set up a new machine from starting installation to full working order. These notes are for my benefit, but I hope you find it useful, too.

Install Ubuntu

Tweak a Few Things

$ sudo passwd root
$ su
# mv /etc/securetty /etc/securetty.bak; \
touch /etc/securetty; \
mv /etc/sudoers /etc/sudoers.bak
# apt-get install ntp
*******************************************************************************
                              NOTICE TO USERS

This computer system is the property of the Branch District Library.  It is
for authorized use only.  Users (authorized or unauthorized) have no explicit
or implicit expectation of privacy.

Any or all uses of this system and all files on this system may be intercepted,
monitored, recorded, copied, audited, inspected, and disclosed to authorized
officials of law enforcement and government agencies.

By using this system, the user consents to such interception, monitoring,
recording, auditing, inspection, and disclosure at the discretion of the Branch
District Library or other authorized officials of law enforcement or government
agencies.

Unauthorized or improper use of this system may result in civil and criminal
penalties and administrative or disciplinary action, as appropriate.  By
continuing to use this system you indicate your awareness of and consent to
these terms and conditions of use.  LOG OFF IMMEDIATELY if you do not agree to
the conditions stated in this notice.

*******************************************************************************
auto eth1
iface eth1 inet static
address 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway YOUR-GATEWAY-FOR-ETH0
dns-nameservers YOUR-DNS-SERVER-1 YOUR-DNS-SERVER-1
# /etc/init.d/networking restart

DHCP Server

# apt-get install dhcp3-server; \
mv /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak
authoritative;
default-lease-time 3600;
max-lease-time 3600;
ddns-update-style none;
log-facility local7;
option routers 192.168.1.1;
option domain-name-servers YOUR-DNS-SERVER-1,YOUR-DNS-SERVER-2;

subnet [Subnet of eth0, just ending in .0] netmask 255.255.255.0 
{
}

subnet 192.168.1.0 netmask 255.255.255.0 
{
    range 192.168.1.100 192.168.1.254; # desired public service IP address range
    host HOSTNAME-HERE # host names for static IP assignment
    {
        hardware ethernet MAC-ADDRESS-USING-COLONS;
        fixed-address DESIRED-IP-ADDRESS-FOR-THIS-MACHINE;
    }
}
# /etc/init.d/isc-dhcp-server restart

Shorewall

# apt-get install shorewall; \
cp /usr/share/doc/shorewall/default-config/* /etc/shorewall/
#ZONE   INTERFACE   BROADCAST   OPTIONS

net     eth0        detect
loc     eth1        detect      dhcp
eth0          192.168.1.0/24
eth0          YOUR-SUBNET-FOR-ETH0/24
loc         all         REJECT
fw          all         REJECT
net         all         DROP
all         all         REJECT
# Accept SSH connections for administration, outgoing ssh from fw
ACCEPT      loc                     fw      tcp     22
ACCEPT      net                     fw      tcp     22
ACCEPT      fw                      net     tcp     22

# Accept HTTP for the wifi stats
ACCEPT      loc                     fw      tcp     80
ACCEPT      net                     fw      tcp     80

# allow web traffic out of the firewall
ACCEPT      fw      net                     tcp     80,8000,8001,8080

# All pinging the web filter
ACCEPT      loc        net        icmp
        
# Allow DNS requests
ACCEPT      loc        net        tcp     53
ACCEPT      loc        net        udp     53 
ACCEPT      fw         net        tcp     53
ACCEPT      fw         net        udp     53

# Allow POP3 email
ACCEPT      loc        net        tcp     110

# Allow NTP
ACCEPT      loc        net        udp     123

# Allow IMAP email
ACCEPT      loc        net        tcp     143   
        
# Allow secure web sites
ACCEPT      loc        net        tcp     443

# Secure SMTP
ACCEPT      loc                     net     tcp     465

# Allow DHCP requests
ACCEPT      loc        fw         tcp     546,547
ACCEPT      loc        fw         udp     67,68
ACCEPT      fw         net        tcp     546,547
ACCEPT      fw         net        udp     67,68

# smtp submission
ACCEPT      loc                     net     tcp     587

# Allow Secure IMAP email
ACCEPT      loc        net        tcp    993

# Allow Secure POP3 email
ACCEPT      loc        net        tcp    995

# Allow both sides of the firewall to access the filter
# (as in your staff side can use the filter, too)
ACCEPT      net       fw          tcp    8080

# Route all HTTP traffic from library kids computers to the filter
# make the IP address range match your needs
REDIRECT    loc:192.168.1.11-192.168.1.20 8081    tcp    80,8000,8001,8080   -

# Route all HTTP traffic from library adult computers to the filter
# make the IP address range match your needs
REDIRECT    loc:192.168.1.21-192.168.1.30 8082    tcp    80,8000,8001,8080   -

# Route all HTTP traffic from patron devices to the filter
# make the IP address range match your needs
REDIRECT    loc:192.168.1.100-192.168.1.255 8083    tcp    80,8000,8001,8080   -

#LAST LINE -- ADD YOUR ENTRIES BEFORE THIS ONE -- DO NOT REMOVE
fw      firewall
net     ipv4
loc     ipv4
# shorewall restart

Squid

# apt-get install squid
########Squid Delay Pools#########
# a simple global throttle, users sharing 5 Mbit/s
delay_pools 1
delay_class 1 1
# 5 Mbit/s fill rate, 6 Mbit/s reserve, units in bytes
delay_parameters 1 655360/786432
acl All src 0/0
delay_access 1 allow All
# /etc/init.d/squid3 restart

DansGuardian

# apt-get install dansguardian
# ln -s /usr/sbin/dansguardian /usr/sbin/dansguardian1; \
ln -s /usr/sbin/dansguardian /usr/sbin/dansguardian2; \
ln -s /usr/sbin/dansguardian /usr/sbin/dansguardian3; \
mkdir /etc/dansguardian1; \
mkdir /etc/dansguardian2; \
mkdir /etc/dansguardian3; \
mkdir /var/log/dansguardian1; \
mkdir /var/log/dansguardian2; \
mkdir /var/log/dansguardian3; \
chown dansguardian:dansguardian /etc/dansguardian1; \
chown dansguardian:dansguardian /etc/dansguardian2; \
chown dansguardian:dansguardian /etc/dansguardian3; \
chown dansguardian:dansguardian /var/log/dansguardian1; \
chown dansguardian:dansguardian /var/log/dansguardian2; \
chown dansguardian:dansguardian /var/log/dansguardian3; \
cp /etc/dansguardian/dansguardian.conf /etc/dansguardian1/; \
cp /etc/dansguardian/dansguardian.conf /etc/dansguardian2/; \
cp /etc/dansguardian/dansguardian.conf /etc/dansguardian3/; \
cp /etc/dansguardian/dansguardianf1.conf /etc/dansguardian1/; \
cp /etc/dansguardian/dansguardianf1.conf /etc/dansguardian2/; \
cp /etc/dansguardian/dansguardianf1.conf /etc/dansguardian3/; \
ln -s /etc/dansguardian/authplugins /etc/dansguardian1/authplugins; \
ln -s /etc/dansguardian/authplugins /etc/dansguardian2/authplugins; \
ln -s /etc/dansguardian/authplugins /etc/dansguardian3/authplugins; \
ln -s /etc/dansguardian/contentscanners /etc/dansguardian1/contentscanners; \
ln -s /etc/dansguardian/contentscanners /etc/dansguardian2/contentscanners; \
ln -s /etc/dansguardian/contentscanners /etc/dansguardian3/contentscanners; \
ln -s /etc/dansguardian/downloadmanagers /etc/dansguardian1/downloadmanagers; \
ln -s /etc/dansguardian/downloadmanagers /etc/dansguardian2/downloadmanagers; \
ln -s /etc/dansguardian/downloadmanagers /etc/dansguardian3/downloadmanagers; \
ln -s /etc/dansguardian/languages /etc/dansguardian1/languages; \
ln -s /etc/dansguardian/languages /etc/dansguardian2/languages; \
ln -s /etc/dansguardian/languages /etc/dansguardian3/languages; \
ln -s /etc/dansguardian/lists /etc/dansguardian1/lists; \
ln -s /etc/dansguardian/lists /etc/dansguardian2/lists; \
ln -s /etc/dansguardian/lists /etc/dansguardian3/lists
# for Google
#we add &safe=vss to the end of every search
"(^http[s]?://[0-9a-z]+\.google\.[a-z]+[-/%.0-9a-z]*/search\?.*)"->"\1&safe=vss"

#for Bing
#similar, but &adlt=strict is the keyword
"(http[s]?://[0-9a-z]+.bing.com/images/search\?.*)"->"\1&adlt=strict"
# cd /etc/init.d/; \
mv dansguardian dansguardian.orig; \
update-rc.d dansguardian remove
# cd /etc/init.d/; \
chmod +x dansguardian*; \
update-rc.d dansguardian1 defaults 50 19; \
update-rc.d dansguardian2 defaults 50 19; \
update-rc.d dansguardian3 defaults 50 19
# /etc/init.d/dansguardian1 start; \
/etc/init.d/dansguardian2 start; \
/etc/init.d/dansguardian3 start

Public Computer Lab Session Timer

# chgrp -R www-data /var/www/signup; \
chmod 664 /var/www/signup/laptops_available; \
chmod 664 /var/www/signup/time_override
# /etc/init.d/mysql restart

Statistics

# apt-get install phpmyadmin
# mysql -u root -p
mysql> CREATE DATABASE IF NOT EXISTS `wifi_stats` DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;quit;
# mysql -u root -p wifi_stats < old_exported_wifi_stats.sql
# mysql -u root -p wifi_stats < wifi_stats.sql
1 *  *   *   *     wget http://localhost/DHCPd-parse.php -O /dev/null > /dev/null 2>&1
1 *  *   *   *     /etc/init.d/dansguardian1 restart
1 *  *   *   *     /etc/init.d/dansguardian2 restart
1 *  *   *   *     /etc/init.d/dansguardian3 restart

Print Release Station

# apt-get install kubuntu-desktop
# apt-get install samba
[Color_Printer]
   path = /var/spool/samba
   browseable = yes
   printable = yes
   printer name = CirculationPrinter
   cups options = job-hold-until=indefinite

[Black_and_White_Printer]
   path = /var/spool/samba
   browseable = yes
   printable = yes
   printer name = CirculationPrinter
   cups options = job-hold-until=indefinite
4 21 *   *   *     /usr/bin/lprm -P CirculationPrinter -
5 21 *   *   *     /bin/rm /var/cache/cups/job.cache*
6 21 *   *   *     /bin/rm /var/spool/cups/c*
7 21 *   *   *     /bin/rm /var/spool/cups/d*

Battery and File Backups

# apt-get install apcupsd; \
mv /etc/apcupsd/apcupsd.conf /etc/apcupsd/apcupsd.conf.bak
# ssh-keygen -t rsa
# ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_host
5 0 * * * mysqldump -u root --password=YOUR-PASSWORD wifi_stats > /home/username/wifi_stats_dump.sql; scp -i /home/username/.ssh/id_rsa /home/username/wifi_stats_dump.sql username@remote_host:/path/to/backup/directory/

Conclusion

And that is all! Reboot and make sure everything comes back up the way it should.


Originally published on 2 November 2012. Last modified on 2 June 2015.
Copyright © John C. Rucker
Verbatim copying and distribution of this entire article is permitted in any medium without royalty provided this notice is preserved.