#!/bin/sh
#
# $Id: ppp_server,v 1.3 2002/10/09 15:26:12 kds Exp $
#
# Script to start a PPP server on a serial connection. The script is called
# with the serial device file open on the standard input and output.
# The following options can be specified for this script:
#  notty      : the serial device file is not a tty (i.e. a socket)
#  masquerade : setup masquerading
#  proxyarp   : setup proxyarp

logerror()
{
	echo $1 >&2
	logger -p daemon.err -t $0 "$@"
}

usage()
{
	echo "Usage: $0 [line <id>] [connid <id>] [notty] [proxyarp|masquerade]" >&2
}

CONNID=0
DEV=""
LOCALIP=192.168.0.1
REMOTEIP=192.168.0.2

# Get the DNS server's from resolv.conf. They will be offered to the PPP client.
if [ -f /etc/resolv.conf ] ; then
	DNS=`sed -n 's/^nameserver \+\([0-9.]\+\)$/ms-dns \1/p' < /etc/resolv.conf | head -2`
fi

while [ $# != 0 ] ; do
	case "$1" in
	# Determine if we must masquerade or setup proxyarp
	proxyarp)
		PROXYARP=proxyarp
		;;
	masquerade)
		if [ -n "$PROXYARP" ] ; then
			logerror "Only one of proxyarp and masquerade can be specified."
			exit -1
		fi
		# Be sure that  masquerading is supported in your system!!!
		IPPARAM="ipparam MASQUERADE"
		;;
	notty)
		NOTTY=notty
		;;
	connid)
		CONNID=`echo $[$2] 2> /dev/null`
		if [ "$CONNID" != "$2" ] ; then
			logerror "Invalid connection id"
			usage
			exit -1
		fi
		REMOTEIP=192.168.0.$[$CONNID + 2]
		shift
		;;
	line)
		LINE=`echo $[$2] 2> /dev/null`
		if [ "$LINE" != "$2" ] ; then
			logerror "Invalid line id"
			usage
			exit -1
		fi
		DEV="/dev/bty$2"
		shift
		;;
	?*)
		usage
		exit -1
		;;
	esac
	shift
done

PPP_OPTS="passive persist $DNS $IPPARAM $PROXYARP $NOTTY $LOCALIP:$REMOTEIP"

exec /usr/sbin/pppd $DEV $PPP_OPTS

