#!/bin/ash
# an hyper-rustic diald for muLinux
# (C) 1999 by M. Andreoli
# [hack: IP accounting (outgoing) on port 53 (dns) ]
# added to in November 2000 by Andrew Walters - switching of what traffic is
# monitored, and manual switching of the connection, misc changes

# Advanced options

# OVERRIDEPATH
# Manual override is activated here by specifying a directory to poll for the
# files 'dialnow' (starts link) and 'hangnow' (terminates link) - when found,
# they are acted upon and then deleted. For example:
# $ touch /tmp/dialnow
# or
# $ echo 1 > /tmp/dialnow
# will establish a connection. Exporting /tmp via samba means windoze clients
# can control the link too.
# Specify the path to the override files here:

OVERRIDEPATH="/var/run"

# NETMASK - Enter your subnet mask bit value here. If your subnet is 255.255.0.0
# this value should be 16, if it is 255.255.255.0 this value should be 24 etc

NETMASK=${NETMASK:-24}

# script starts here

if [ -r /etc/diald.conf ] ; then
	. /etc/diald.conf
else
# testing value
NAMESERVER=195.223.180.8
NETWORK=192.168.1.0
ACOUSTIC_LOG=TRUE
SLEEP_TIME=1
IDLE_LIMIT=30
TEST_COMMAND="cat /tmp/started"
START_COMMAND="echo start > /tmp/started"
STOP_COMMAND=": > /tmp/started"
> /tmp/started
fi

# parse options

case $1 in
-t)
	verbose=yes
	;;
-v) verbose=yes
	;;
-h)
	echo "Usage: diald [-v|-h]"
	exit
	;;
esac



# functions
LOG()
{
[ "$verbose" ] && echo $@
}

acoustic_log()
{
[ "$ACOUSTIC_LOG" = FALSE ] && return
[ "$ACOUSTIC_LOG" = false ] && return
case $1 in
incoming)
	LOG DNS request incoming
	wave -c 40 2
	;;
started)
	LOG service started
	wave -c 110 1 
	wave -c 220 2 
	;;
stopped)
	LOG service stopped 
	wave -c 40 6 
	;;
esac
}

LOG mu-diald for mu-Linux - by M Andreoli, A Walters
# set IP accounting policy

LOG IP accounting setup: DNS $NAMESERVER
ipfwadm -A -f
ipfwadm -A in -i -P udp -S $NETWORK/$NETMASK -D $NAMESERVER 53 
 
# change policy if connected at startup
if [ "`eval $TEST_COMMAND`" ] ; then
	LOG ppp0 is currently up, setting listen policy to TCP.
	ipfwadm -A -f
	ipfwadm -A in -i -P tcp -S $NETWORK/$NETMASK -D 0.0.0.0/0 21 25 80 110 143 443
$LISTENPORTS
fi

[ "$verbose" ] && ipfwadm -l -A -n 


# main loop

idle=0
prev=0

LOG Starting main loop

while [ 1 ]
do

set -- `cat /proc/net/ip_acct`
shift 7; acc=$4

[ "$acc" -gt  "$prev" ] && condition=true  
[ -f "$OVERRIDEPATH/dialnow" ] && condition=true  
if [ "$condition" = "true" ] ; then
	condition=false  	
	[ -f "$OVERRIDEPATH/dialnow" ] && ( rm $OVERRIDEPATH/*now ; LOG Manual start )
	idle=0
	acoustic_log incoming
	prev=$acc
	conn="`eval ${TEST_COMMAND}`"
	if [ -z "$conn" ] ; then
		LOG -n Starting service...	
		eval $START_COMMAND
		# wait for OK, keep trying if timing out
		timeout=0
		while [ -z "`eval ${TEST_COMMAND}`" ] ; do
		LOG -n "."
	 	sleep 1	
		timeout=`expr $timeout + 1`
		if [ $timeout = 60 ] ; then
			LOG !
			LOG Dial Timeout.
			wave -c 600 5;wave -c 600 5
			LOG Trying again...
			eval $START_COMMAND
			timeout=0
		fi 
		done
		# now monitor SMTP, FTP, HTTP, POP3, IMAP etc traffic
		LOG Connected.
		LOG Now monitoring standard traffic - TCP ports 21 25 80 110 143 443
$LISTENPORTS 
		ipfwadm -A -f
		ipfwadm -A in -i -P tcp -S $NETWORK/$NETMASK -D 0.0.0.0/0 21 25 80 110 143 443 $LISTENPORTS
		[ "$verbose" ] && ipfwadm -l -A -n		
		acoustic_log started	
	fi
else 
	if [ "`eval $TEST_COMMAND`" ] ; then
		LOG -e "idle=$idle     \r\c"
		idle=`expr $idle + 1`
	fi
fi

# check idle time
[ "$idle" -gt "$IDLE_LIMIT" ] && condition=true
[  -f "$OVERRIDEPATH/hangnow" ] && condition=true 
if [ "$condition" = "true" ]  ; then
	condition=false 
	[ -f "$OVERRIDEPATH/hangnow" ] && rm $OVERRIDEPATH/*now
	LOG idle
	LOG Timeout of $IDLE_LIMIT reached or $OVERRIDEPATH/hangup found.
	LOG -n  Stopping service...
	eval $STOP_COMMAND
	acoustic_log service stopped
	ipfwadm -A -f
	LOG Now monitoring DNS traffic
	ipfwadm -A in -i -P udp -S $NETWORK/$NETMASK -D $NAMESERVER 53
	[ "$verbose" ] && ipfwadm -l -A -n
	idle=0
	prev=0	
fi

sleep $SLEEP_TIME 
done

# end

