1#!/bin/sh
2# @configure_input@
3#***********************************************************************
4#
5# pppoe-connect
6#
7# Shell script to connect to a PPPoE provider
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-connect [config_file]
19#        pppoe-connect 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@
28localstatedir=/var
29
30# Paths to programs
31IFCONFIG=/sbin/ifconfig
32PPPD=@PPPD@
33SETSID=@SETSID@
34PPPOE=@sbindir@/pppoe
35LOGGER="/usr/bin/logger -t `basename $0`"
36
37# Set to "C" locale so we can parse messages from commands
38LANG=C
39export LANG
40
41# Must be root
42if test "`@ID@ -u`" != 0 ; then
43    echo "$0: You must be root to run this script" >& 2
44    exit 1
45fi
46
47if test "$SETSID" != "" -a ! -x "$SETSID"; then
48    SETSID=""
49fi
50
51CONFIG=/etc//ppp/pppoe.conf
52USER=""
53ETH=""
54
55# Sort out command-line arguments
56case "$#" in
57    1)
58    CONFIG="$1"
59    ;;
60    3)
61    CONFIG="$3"
62    ;;
63esac
64
65if test ! -f "$CONFIG" -o ! -r "$CONFIG" ; then
66    echo "$0: Cannot read configuration file '$CONFIG'" >& 2
67    exit 1
68fi
69export CONFIG
70. $CONFIG
71
72PPPOE_PIDFILE="$PIDFILE.pppoe"
73PPPD_PIDFILE="$PIDFILE.pppd"
74
75# Check for command-line overriding of ETH and USER
76case "$#" in
77    2|3)
78    ETH="$1"
79    USER="$2"
80    ;;
81esac
82
83# Check that config file is sane
84if test "$USER" = "" ; then
85    echo "$0: Check '$CONFIG' -- no setting for USER" >& 2
86    exit 1
87fi
88if test "$ETH" = "" ; then
89    echo "$0: Check '$CONFIG' -- no setting for ETH" >& 2
90    exit 1
91fi
92
93PPPD_PID=0
94
95# Catch common error
96if test "$DEBUG" = "1" ; then
97    echo "*** If you want to use DEBUG, invoke pppoe-start, not pppoe-connect."
98    exit 1
99fi
100
101if test "$DEBUG" != "" ; then
102    if test "$LINUX_PLUGIN" != "" ; then
103	echo "Cannot use DEBUG mode and LINUX_PLUGIN at the same time."
104	echo "Kernel-mode PPPoE is experimental and unsupported."
105	exit 1
106    fi
107    echo "* The following section identifies your Ethernet interface" >> $DEBUG
108    echo "* and user name.  Some ISP's need 'username'; others" >> $DEBUG
109    echo "* need 'username@isp.com'.  Try both" >> $DEBUG
110    echo "ETH=$ETH; USER=$USER" >> $DEBUG
111    echo "---------------------------------------------" >> $DEBUG
112fi
113
114# MTU of Ethernet card attached to modem MUST be 1500.  This apparently
115# fails on some *BSD's, so we'll only do it under Linux
116
117if test `uname -s` = Linux ; then
118    $IFCONFIG $ETH up
119    EXISTING_MTU=`$IFCONFIG $ETH 2> /dev/null | grep MTU: | sed -e 's/.*MTU://' | sed -e 's/[^0-9].*//'`
120    if test "$EXISTING_MTU" = "" -o "$EXISTING_MTU" -lt 1500 ; then
121	$IFCONFIG $ETH mtu 1500
122    fi
123
124    # For 2.4 kernels.  Will fail on 2.2.x, but who cares?
125    modprobe ppp_generic > /dev/null 2>&1
126    modprobe ppp_async > /dev/null 2>&1
127    modprobe ppp_synctty > /dev/null 2>&1
128    if test -n "$LINUX_PLUGIN" ; then
129	modprobe pppox > /dev/null 2>&1
130	modprobe pppoe > /dev/null 2>&1
131    fi
132fi
133
134if test "$SYNCHRONOUS" = "yes" ; then
135    PPPOE_SYNC=-s
136    PPPD_SYNC=sync
137	# Increase the chances of it working on Linux...
138    if test `uname -s` = Linux ; then
139	modprobe n_hdlc > /dev/null 2>&1
140    fi
141else
142    PPPOE_SYNC=""
143    PPPD_SYNC=""
144fi
145
146if test -n "$ACNAME" ; then
147    ACNAME="-C $ACNAME"
148fi
149
150if test -n "$SERVICENAME" ; then
151    SERVICENAMEOPT="-S $SERVICENAME"
152else
153    SERVICENAMEOPT=""
154fi
155
156if test "$CLAMPMSS" = "no" ; then
157    CLAMPMSS=""
158else
159    CLAMPMSS="-m $CLAMPMSS"
160fi
161
162# If DNSTYPE is SERVER, we must use "usepeerdns" option to pppd.
163if test "$DNSTYPE" = "SERVER" ; then
164    PEERDNS=yes
165    USEPEERDNS=yes
166fi
167
168if test "$PEERDNS" = "yes" ; then
169    PEERDNS="usepeerdns"
170else
171    PEERDNS=""
172fi
173
174# Backward config file compatibility -- PEERDNS used to be USEPEERDNS
175if test "$USEPEERDNS" = "yes" ; then
176    PEERDNS="usepeerdns"
177fi
178if test "$USEPEERDNS" = "no" ; then
179    PEERDNS=""
180fi
181
182
183# Backward config file compatibility
184if test "$DEMAND" = "" ; then
185    DEMAND=no
186fi
187
188if test "$DEMAND" = "no" ; then
189    DEMAND=""
190else
191    DEMAND="demand persist idle $DEMAND 10.112.112.112:10.112.112.113 ipcp-accept-remote ipcp-accept-local connect true noipdefault ktune"
192fi
193
194case "$FIREWALL" in
195    STANDALONE)
196    . /etc/ppp/firewall-standalone
197    ;;
198    MASQUERADE)
199    . /etc/ppp/firewall-masq
200    ;;
201esac
202
203# If we're using kernel-mode PPPoE on Linux...
204if test "$LINUX_PLUGIN" != "" ; then
205    PLUGIN_OPTS="plugin $LINUX_PLUGIN nic-$ETH"
206    if test -n "$SERVICENAME" ; then
207	PLUGIN_OPTS="$PLUGIN_OPTS rp_pppoe_service $SERVICENAME"
208    fi
209    modprobe pppoe > /dev/null 2>&1
210fi
211
212if test "$DEFAULTROUTE" != "no" ; then
213    DEFAULTROUTE="defaultroute"
214else
215    DEFAULTROUTE=""
216fi
217
218if test -n "$UNIT" ; then
219    DOUNIT="unit $UNIT"
220else
221    DOUNIT=""
222fi
223
224if test "$MTU" = "" ; then
225    MTU=1492
226fi
227if test "$MRU" = "" ; then
228    MRU=1492
229fi
230
231# Standard PPP options we always use
232PPP_STD_OPTIONS="$PLUGIN_OPTS $DOUNIT noipdefault noauth default-asyncmap $DEFAULTROUTE hide-password nodetach $PEERDNS mtu $MTU mru $MRU noaccomp nodeflate nopcomp novj novjccomp user $USER lcp-echo-interval $LCP_INTERVAL lcp-echo-failure $LCP_FAILURE $PPPD_EXTRA"
233
234# Jigger DNS if required...
235if test "$DNSTYPE" = "SERVER" ; then
236    # Sorry, dude...
237    rm -f /etc/resolv.conf
238    ln -s /etc/ppp/resolv.conf /etc/resolv.conf
239elif test "$DNSTYPE" = "SPECIFY" ; then
240    # Sorry, dude...
241    rm -f /etc/resolv.conf
242    echo "nameserver $DNS1" > /etc/resolv.conf
243    if test -n "$DNS2" ; then
244	echo "nameserver $DNS2" >> /etc/resolv.conf
245    fi
246fi
247
248# PPPoE invocation
249PPPOE_CMD="$PPPOE -p $PPPOE_PIDFILE -I $ETH -T $PPPOE_TIMEOUT -U $PPPOE_SYNC $CLAMPMSS $ACNAME $SERVICENAMEOPT $PPPOE_EXTRA"
250if test "$DEBUG" != "" ; then
251    if test "$DEMAND" != "" ; then
252	echo "(Turning off DEMAND for debugging purposes)"
253	DEMAND=""
254    fi
255    echo "* The following section shows the pppd command we will invoke" >> $DEBUG
256    echo "pppd invocation" >> $DEBUG
257    echo "$SETSID $PPPD pty '$PPPOE_CMD' $PPP_STD_OPTIONS $PPPD_SYNC debug" >> $DEBUG
258    echo "---------------------------------------------" >> $DEBUG
259    $SETSID $PPPD pty "$PPPOE_CMD -D $DEBUG-0" \
260	$PPP_STD_OPTIONS \
261	$PPPD_SYNC \
262	debug >> $DEBUG 2>&1
263    echo "---------------------------------------------" >> $DEBUG
264    echo "* The following section is an extract from your log." >> $DEBUG
265    echo "* Look for error messages from pppd, such as" >> $DEBUG
266    echo "* a lack of kernel support for PPP, authentication failure" >> $DEBUG
267    echo "* etc." >> $DEBUG
268    if test -f "/var/log/messages" ; then
269	echo "Extract from /var/log/messages" >> $DEBUG
270	grep 'ppp' /var/log/messages | tail -150 >> $DEBUG
271    elif test -f "/var/adm/messages"; then
272	echo "Extract from /var/adm/messages" >> $DEBUG
273	grep 'ppp' /var/adm/messages | tail -150 >> $DEBUG
274    else
275        echo "Can't find messages file (looked for /var/{log,adm}/messages" >> $DEBUG
276    fi
277    date >> $DEBUG
278    echo "---------------------------------------------" >> $DEBUG
279    echo "* The following section is a dump of the packets" >> $DEBUG
280    echo "* sent and received by rp-pppoe.  If you don't see" >> $DEBUG
281    echo "* any output, it's an Ethernet driver problem.  If you only" >> $DEBUG
282    echo "* see three PADI packets and nothing else, check your cables" >> $DEBUG
283    echo "* and modem.  Make sure the modem lights flash when you try" >> $DEBUG
284    echo "* to connect.  Check that your Ethernet card is in" >> $DEBUG
285    echo "* half-duplex, 10Mb/s mode.  If all else fails," >> $DEBUG
286    echo "* try using pppoe-sniff." >> $DEBUG
287    echo "rp-pppoe debugging dump" >> $DEBUG
288    cat $DEBUG-0 >> $DEBUG
289    rm -f $DEBUG-0
290    for i in 1 2 3 4 5 6 7 8 9 10 ; do
291	echo ""
292	echo ""
293	echo ""
294    done
295    echo "*** Finished debugging run.  Please review the file"
296    echo "*** '$DEBUG' and try to"
297    echo "*** figure out what is going on."
298    echo "***"
299    echo "*** Unfortunately, we can NO LONGER accept debugging"
300    echo "*** output for analysis.  Please do not send this to"
301    echo "*** Roaring Penguin; it is too time-consuming for"
302    echo "*** us to deal with all the analyses we have been sent."
303    exit 0
304fi
305
306echo $$ > $PIDFILE
307
308while [ true ] ; do
309    if test "$OVERRIDE_PPPD_COMMAND" != "" ; then
310	$SETSID $OVERRIDE_PPPD_COMMAND &
311	echo "$!" > $PPPD_PIDFILE
312    elif test "$LINUX_PLUGIN" != "" ; then
313	$SETSID $PPPD $PPP_STD_OPTIONS $DEMAND &
314	echo "$!" > $PPPD_PIDFILE
315    else
316	$SETSID $PPPD pty "$PPPOE_CMD" \
317	    $PPP_STD_OPTIONS \
318	    $DEMAND \
319	    $PPPD_SYNC &
320	echo "$!" > $PPPD_PIDFILE
321    fi
322    wait
323
324    if test "$RETRY_ON_FAILURE" = "no" ; then
325	exit
326    fi
327
328    # Run /etc/ppp/pppoe-lost if it exists
329    test -x /etc/ppp/pppoe-lost && /etc/ppp/pppoe-lost
330
331    # Re-establish the connection
332    $LOGGER -p daemon.notice \
333        "PPPoE connection lost; attempting re-connection."
334
335    # Wait a bit in case a problem causes tons of log messages :-)
336    sleep 5
337done
338