1#!/bin/sh
2# @configure_input@
3#***********************************************************************
4#
5# pppoe-start
6#
7# Shell script to bring up a PPPoE connection
8#
9# Copyright (C) 2000 Roaring Penguin Software Inc.
10#
11# $Id$
12#
13# This file may be distributed under the terms of the GNU General
14# Public License.
15#
16# LIC: GPL
17#
18# Usage: pppoe-start [config_file]
19#        pppoe-start interface user [config_file]
20# Second form overrides USER and ETH from config file.
21# If config_file is omitted, defaults to /etc/ppp/pppoe.conf
22#
23#***********************************************************************
24
25# From AUTOCONF
26prefix=@prefix@
27exec_prefix=@exec_prefix@
28
29# Paths to programs
30CONNECT=@sbindir@/pppoe-connect
31ECHO=@ECHO@
32IFCONFIG=/sbin/ifconfig
33
34# Set to "C" locale so we can parse messages from commands
35LANG=C
36export LANG
37
38# Defaults
39CONFIG=/etc/ppp/pppoe.conf
40USER=""
41ETH=""
42ME=`basename $0`
43# Must be root
44if [ "`@ID@ -u`" != 0 ] ; then
45    $ECHO "$ME: You must be root to run this script" >& 2
46    exit 1
47fi
48
49# Debugging
50if [ "$DEBUG" = "1" ] ; then
51    $ECHO "*** Running in debug mode... please be patient..."
52    DEBUG=/tmp/pppoe-debug-$$
53    export DEBUG
54    mkdir $DEBUG
55    if [ "$?" != 0 ] ; then
56	$ECHO "Could not create directory $DEBUG... exiting"
57	exit 1
58    fi
59    DEBUG=$DEBUG/pppoe-debug.txt
60
61    # Initial debug output
62    $ECHO "---------------------------------------------" > $DEBUG
63    $ECHO "* The following section contains information about your system" >> $DEBUG
64    date >> $DEBUG
65    $ECHO "Output of uname -a" >> $DEBUG
66    uname -a >> $DEBUG
67    $ECHO "---------------------------------------------" >> $DEBUG
68    $ECHO "* The following section contains information about your network" >> $DEBUG
69    $ECHO "* interfaces.  The one you chose for PPPoE should contain the words:" >> $DEBUG
70    $ECHO "* 'UP' and 'RUNNING'.  If it does not, you probably have an Ethernet" >> $DEBUG
71    $ECHO "* driver problem." >> $DEBUG
72    $ECHO "Output of ifconfig -a" >> $DEBUG
73    $IFCONFIG -a >> $DEBUG
74    $ECHO "---------------------------------------------" >> $DEBUG
75    if [ "`uname -s`" = "Linux" ] ; then
76        $ECHO "* The following section contains information about kernel modules" >> $DEBUG
77	$ECHO "* If the module for your Ethernet card is 'tulip', you might" >> $DEBUG
78	$ECHO "* want to look for an updated version at http://www.scyld.com" >> $DEBUG
79	$ECHO "Output of lsmod" >> $DEBUG
80	lsmod >> $DEBUG
81	$ECHO "---------------------------------------------" >> $DEBUG
82    fi
83    $ECHO "* The following section lists your routing table." >> $DEBUG
84    $ECHO "* If you have an entry which starts with '0.0.0.0', you probably" >> $DEBUG
85    $ECHO "* have defined a default route and gateway, and pppd will" >> $DEBUG
86    $ECHO "* not create a default route using your ISP.  Try getting" >> $DEBUG
87    $ECHO "* rid of this route." >> $DEBUG
88    $ECHO "Output of netstat -n -r" >> $DEBUG
89    netstat -n -r >> $DEBUG
90    $ECHO "---------------------------------------------" >> $DEBUG
91    $ECHO "Contents of /etc/resolv.conf" >> $DEBUG
92    $ECHO "* The following section lists DNS setup." >> $DEBUG
93    $ECHO "* If you can browse by IP address, but not name, suspect" >> $DEBUG
94    $ECHO "* a DNS problem." >> $DEBUG
95    cat /etc/resolv.conf >> $DEBUG
96    $ECHO "---------------------------------------------" >> $DEBUG
97    $ECHO "* The following section lists /etc/ppp/options." >> $DEBUG
98    $ECHO "* You should have NOTHING in that file." >> $DEBUG
99    $ECHO "Contents of /etc/ppp/options" >> $DEBUG
100    cat /etc/ppp/options >> $DEBUG 2>/dev/null
101    $ECHO "---------------------------------------------" >> $DEBUG
102else
103    DEBUG=""
104fi
105
106# Sort out command-line arguments
107case "$#" in
108    1)
109	CONFIG="$1"
110	;;
111    3)
112	CONFIG="$3"
113	;;
114esac
115
116if [ ! -f "$CONFIG" -o ! -r "$CONFIG" ] ; then
117    $ECHO "$ME: Cannot read configuration file '$CONFIG'" >& 2
118    exit 1
119fi
120export CONFIG
121. $CONFIG
122
123# Check for command-line overriding of ETH and USER
124case "$#" in
125    2|3)
126	ETH="$1"
127	USER="$2"
128	;;
129esac
130
131# Check for pidfile
132if [ -r "$PIDFILE" ] ; then
133    PID=`cat "$PIDFILE"`
134    # Check if still running
135    kill -0 $PID > /dev/null 2>&1
136    if [ $? = 0 ] ; then
137	$ECHO "$ME: There already seems to be a PPPoE connection up (PID $PID)" >& 2
138	exit 1
139    fi
140    # Delete bogus PIDFILE
141    rm -f "$PIDFILE" "$PIDFILE.pppd" "$PIDFILE.pppoe" "$PIDFILE.start"
142fi
143
144echo $$ > $PIDFILE.start
145
146# Start the connection in the background unless we're debugging
147if [ "$DEBUG" != "" ] ; then
148    $CONNECT "$@"
149    exit 0
150fi
151
152$CONNECT "$@" > /dev/null 2>&1 &
153CONNECT_PID=$!
154
155if [ "$CONNECT_TIMEOUT" = "" -o "$CONNECT_TIMEOUT" = 0 ] ; then
156    exit 0
157fi
158
159# Don't monitor connection if dial-on-demand
160if [ "$DEMAND" != "" -a "$DEMAND" != "no" ] ; then
161    exit 0
162fi
163
164# Monitor connection
165TIME=0
166while [ true ] ; do
167    @sbindir@/pppoe-status $CONFIG > /dev/null 2>&1
168
169    # Looks like the interface came up
170    if [ $? = 0 ] ; then
171	# Print newline if standard input is a TTY
172	tty -s && $ECHO " Connected!"
173	exit 0
174    fi
175
176    if test -n "$FORCEPING" ; then
177	printf "%s" "$FORCEPING"
178    else
179	tty -s && printf "%s" "$PING"
180    fi
181    sleep $CONNECT_POLL
182    TIME=`expr $TIME + $CONNECT_POLL`
183    if [ $TIME -gt $CONNECT_TIMEOUT ] ; then
184	break
185    fi
186done
187
188$ECHO "TIMED OUT" >& 2
189# Timed out!  Kill the pppoe-connect process and quit
190kill $CONNECT_PID > /dev/null 2>&1
191
192# Clean up PIDFILE(s)
193rm -f "$PIDFILE" "$PIDFILE.pppd" "$PIDFILE.pppoe" "$PIDFILE.start"
194
195exit 1
196
197