#!/bin/sh
#
#
# Listing 20-2: Firewall-primary.sh

IPTABLES=/sbin/iptables

DMZ_LAN="192.168.1.0/24"

DMZ_INTERFACE="eth1"
DMZ_INTERFACE_ADDR="192.168.1.254"

WORLD_INTERFACE="eth0"
WORLD_INTERFACE_ADDR="207.177.175.66"

INTERNAL_LAN="192.168.2.0/24"

DMZ_FTP_SERVER_ADDR="192.168.1.10"
DMZ_APACHE_SERVER_ADDR="192.168.1.20"
DMZ_MAIL_SERVER_ADDR="192.168.1.30"
DMZ_DNS_SERVER_ADDR="192.168.1.40"

# Drop all rules in the filter table
$IPTABLES -F FORWARD
$IPTABLES -F INPUT
$IPTABLES -F OUTPUT

# Create default drop and reject everything policy
$IPTABLES -P FORWARD DROP
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P INPUT DROP

# Enable loopback
$IPTABLES -A INPUT -i lo -p all -j ACCEPT

# Enable the Internet Control Message Protocol (ICMP)
# packets that are used by ping and others
$IPTABLES -A INPUT -p icmp -j ACCEPT

# Enable DMZ LAN access to the firewall's
# internal interface
$IPTABLES -A input -i $DMZ_LAN_INTERFACE \
          -s $DMZ_LAN -j ACCEPT

# Enable firewall generated packets destined for
# the DMZ LAN
$IPTABLES -A output -i $DMZ_LAN_INTERFACE \
          -d $DMZ_LAN -j ACCEPT

# Setup masquerading for packets generated by the
# internal private LAN that is not meant for itself
$IPTABLES -t nat -A POSTROUTING -o $EXTERNAL_LAN_INTERFACE \
          -s $INTERNAL_LAN \
          -d ! $INTERNAL_LAN \
          -j MASQUERADE

# Forward packets from DMZ LAN
$IPTABLES -A FORWARD -s $DMZ_LAN -j ACCEPT
$IPTABLES -A FORWARD -d $DMZ_LAN -j ACCEPT
