#!/bin/sh -e # /etc/ppp ip-up-diald -ci "-vD" -s "ip-up ip-goingdown ip-down" -- \ # ip-up-diald -- This script handles ip-(up|goingdown|down) for ppp|diald # Copyright (C) 2003 by Richard Hawes # under GNU General Public License version 2 # see www.dma.org/~rhawes/site/COPYING # $Revision: 1.36 $ # latest version: www.dma.org/~rhawes/programs/ip-up-diald exec 3<>/dev/null; # USAGE: make symbolic links to this program # in /etc/ppp/ for ip-up ip-down ip-goingdown # # ip-goingdown can be called from diald or a script such as poff # the first parameter has to be the interface name p_init() { SCRIPTNAME="$1"; VARNAME="$2"; eval export PARAM_$2=\"$3\"; # source ip-conf _if_test -f . "$_ETC/ip-conf"||:; TIME_STAMP="$TMPDIR/${IFNAME}.goingdown"; PPP_EXPORTS="$TMPDIR/${IFNAME}.exports"; # set DIALD_PID _if_test -f "read DIALD_PID <" "$_DIALDRUN"&&kill -0 $DIALD_PID 2>&3|| DIALD_PID=; } p_log() { logger $_LOG_OPT -t "$SCRIPTNAME[$$]";} p_param() { export PPP_IPPARAM="$6" PPP_TTYNAME=`basename "$DEVICE"`; #the other parameters are in the environment # $1 == IFNAME # $2 == DEVICE # $3 == SPEED # $4 == IPLOCAL # $5 == IPREMOTE # but because scripts use these variables . . . export PPP_IFACE="$1" PPP_TTY="$2" PPP_SPEED="$3" export PPP_LOCAL="$4" PPP_REMOTE="$5" } # because run-parts version 1.15 does not work with suidperl # unless all parent directories have permission o+x run_parts() { local FILE; cd "$1"||return; for FILE in `ls | grep '^[-_[:alnum:]]\+$'`; do if [ ! -d "$FILE" ]&&[ -x "$FILE" ];then "./$FILE" $2|| echo "run_parts: $1/$FILE returned $?" >&2 fi done cd - >&3; } r_pts() { export SCRIPTNAME VARNAME DIALD_PID; eval "$1"; eval "$2"; _if_exec "$3"||:; _if_exec "${3}.${PPP_TTYNAME}"||:; _if_test -d run_parts "${4}.${PPP_TTYNAME}" "$PPP_IPPARAM"|| _if_test -d run_parts "${4}" "$PPP_IPPARAM" _if_exec "$5"||:; _if_exec "${5}.${PPP_TTYNAME}"||:; eval "$6"; eval "$7"; } r_parts() { eval renice \$_NICE_$1 $$; eval r_pts \"\$E_PRE$1\" \"\$E_PRE$1_${PPP_TTYNAME}\" \"\$S_PRE$1\" \ \"\$D_$1\" \"\$S_POST$1\" \"\$E_POST$1\" \"\$E_POST$1_${PPP_TTYNAME}\" } r_pts_down() { [ -f "$TIME_STAMP" ]&&[ "$PPP_EXPORTS" -ot "$TIME_STAMP" ]|| { touch "$TIME_STAMP"; r_parts DOWN; } } r_pts_up() { # give diald time to make new device the default route _if_test -f "mv -vfb" "$TMPDIR" "${TMPDIR}.orig.$$"||:; _if_test '! -d' "mkdir -vp" "$TMPDIR"||:; # save exports for goingdown export -p >"$PPP_EXPORTS"; if [ "$DIALD_PID" ];then if [ "$DIALD_DEVICE" != "$DEVICE" ];then sleep $_DELAY; r_parts UP; fi else r_parts UP; fi } # functions that could be used in ip-conf _if_test() { [ $1 "$3" ]&&{ shift; eval "$@";};} _if_exec() { [ -x "$1" ]&&"$@";} _ETC=/etc/ppp; # variables that could be changed in ip-conf _DELAY=2; _DIALDRUN="/var/run/diald.pid"; _LOG_OPT="-p user.debug"; _NICE_DOWN="+0"; _NICE_UP="+19"; D_DOWN="$_ETC/ip-down.d"; D_UP="$_ETC/ip-up.d"; E_PREDOWN=":"; E_PREUP=":"; E_POSTDOWN=":"; E_POSTUP=":"; S_PREDOWN="$_ETC/ip-predown"; S_PREUP="$_ETC/ip-preup"; S_POSTDOWN="$_ETC/ip-postdown"; S_POSTUP="$_ETC/ip-postup"; DIALD_DEVICE=""; PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin; TMPDIR=/var/tmp/ppp; export PATH TMPDIR DIALD_DEVICE; #base names of symbolic links to this program P_UP="ip-up"; P_UP_DIALD="ip-up-diald"; P_DOWN="ip-down"; P_GOINGDOWN="ip-goingdown"; case "$0" in *"$P_UP") p_param "$@"; p_init "$P_UP" IP_UP "$*"; r_pts_up 2>&1 | p_log;; *"$P_UP_DIALD") IFNAME="$1"; p_init "$P_UP_DIALD" IP_UP_DIALD "$*"; { . "$PPP_EXPORTS"; [ "$DIALD_DEVICE" != "$DEVICE" ]|| r_parts UP; } 2>&1 | p_log;; *"$P_GOINGDOWN") IFNAME="$1"; p_init "$P_GOINGDOWN" IP_GOINGDOWN "$*"; { . "$PPP_EXPORTS"; r_pts_down;} 2>&1 | p_log;; *"$P_DOWN") p_param "$@"; p_init "$P_DOWN" IP_DOWN "$*"; r_pts_down 2>&1 | p_log;; *) echo "called with invalid name: $0" >&2; exit 1;; esac