#!/bin/sh
#
#        +-----------------------------------------------------------------------------+
#        | Endian Firewall                                                             |
#        +-----------------------------------------------------------------------------+
#        | Copyright (c) 2005-2006 Endian                                              |
#        |         Endian GmbH/Srl                                                     |
#        |         Bergweg 41 Via Monte                                                |
#        |         39057 Eppan/Appiano                                                 |
#        |         ITALIEN/ITALIA                                                      |
#        |         info@endian.it                                                      |
#        |                                                                             |
#        | This program is free software; you can redistribute it and/or               |
#        | modify it under the terms of the GNU General Public License                 |
#        | as published by the Free Software Foundation; either version 2              |
#        | of the License, or (at your option) any later version.                      |
#        |                                                                             |
#        | This program is distributed in the hope that it will be useful,             |
#        | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
#        | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
#        | GNU General Public License for more details.                                |
#        |                                                                             |
#        | You should have received a copy of the GNU General Public License           |
#        | along with this program; if not, write to the Free Software                 |
#        | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
#        | http://www.fsf.org/                                                         |
#        +-----------------------------------------------------------------------------+
#

#
# This file is triggered when a pppd link goes up and it will
# activate the uplink associated with it
#

if [ -z "$LINKNAME" ]; then
    logger "pppd: ip-up not triggered, no linkname set"
    exit 1
fi

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
. /etc/rc.d/efw_lib.sh

if [ ! -d "${UPLINK_VAR}/$LINKNAME" ]; then
    # this ppp connection is not an uplink, so do not do anything here
    exit 0
fi

. ${UPLINK_SCRIPTS}/generic/route.sh
. ${UPLINK_SCRIPTS}/generic/static.sh
. ${UPLINK_SCRIPTS}/generic/hookery.sh

umask 0022

PPP_DNS1=$DNS1
PPP_DNS2=$DNS2

init_variables $LINKNAME

if is_active $LINKNAME; then
    loadSettings "${UPLINK_VAR}/${LINKNAME}/data"
    if [ -n "$ip_address" ] && [ "$IPLOCAL" != "$ip_address" ]; then
        kill -TERM $PPID
        exit 0
    fi
fi

if [ "$DNS" == "Automatic" ]; then
    export new_domain_name_servers="$PPP_DNS1 $PPP_DNS2"
else
    export new_domain_name_servers="$DNS1 $DNS2"
fi

export interface=$IFNAME
export new_ip_address=$IPLOCAL
export new_gateway=$IPREMOTE
export new_network_number=$IPLOCAL
export new_broadcast_address=$IPLOCAL
export new_cidr=32
export new_interface_mtu=$MTU
export new_ips="$new_ip_address/$new_cidr $new_ips"

static_addr "append" "$IPLOCAL/32"
if [ $? -ne 0 ]; then
    failure "Could not configure ip addresses"
fi
route_start
if [ $? -ne 0 ]; then
    route_stop
    failure "Could not set up routing"
fi

sleep 3
set_active "$LINKNAME"

exit 0
