#!/bin/sh -e

# This script sends an email containing a firewall activity report during
# the last day.
# I choose to have an output type which is text, maybe I can add human, but I
# don't think that it would be a good idea to add something else.

#This script was written by Jean-Michel Kelbert <kelbert@debian.org>, for
#the Debian project (but  may  be used by others).

export LC_ALL="C"

WFLOGS=/usr/bin/wflogs
CONFIG=/etc/default/wflogs

test -x $WFLOGS || exit 0
test -r $CONFIG || exit 0

. $CONFIG

test -r $INPUT_FILE || exit 0

test "$EMAIL_SEND" = "true" || exit 0

if [ -z "$INPUT_TYPE" ]; then
  INPUT_TYPE="netfilter"
fi

INPUT_TYPE=`echo $INPUT_TYPE | tr -d " "`

# wflogs options.
case "$EMAIL_SORT" in
 "Yes default order")
  OPTIONS="--sort"
  ;;
 "Yes custom order")
  OPTIONS="--sort=$EMAIL_SORT_OPTIONS"
  ;;
esac

case "$EMAIL_OUTPUT_WHOIS" in
 "no whois lookups")
  EMAIL_OUTPUT_WHOIS=0
  ;;
 "always do whois lookups")
  EMAIL_OUTPUT_WHOIS=1
  ;;
 "do whois lookups only if no DNS name could be found")
  EMAIL_OUTPUT_WHOIS=2
  ;;
esac

OPTIONS="$OPTIONS --strict-parsing=loose \
-i $INPUT_TYPE -o text --duration=$EMAIL_OUTPUT_DURATION \
--summary=$EMAIL_OUTPUT_SUMMARY --whois_lookup=$EMAIL_OUTPUT_WHOIS \
--src_mac=$EMAIL_OUTPUT_MAC --dst_mac=$EMAIL_OUTPUT_MAC \
--mac_vendor=$EMAIL_OUTPUT_MAC_VENDOR"

if [ "$EMAIL_OBFUSCATE" != "nothing" ]; then
  OPTIONS="--obfuscate=$EMAIL_OBFUSCATE $OPTIONS"
fi

FILTER="\$start_time >= [yesterday]"

# Send the mail.
eval "$WFLOGS -f '$FILTER' $OPTIONS -- $INPUT_FILE | mail $EMAIL_ADDRESS -s 'Wflogs report since `date +"%D" -d "yesterday"`'"
