#!/bin/bash
# (c) Eduard Bloch <blade@debian.org>
# LICENSE: GPL
# Purpose: initial PPPoE configuration on Debian
# Depends: bash, pppd, pppoe, whiptail, my usepeerdns script

export TEXTDOMAINDIR="/usr/share/locale"
export TEXTDOMAIN=pppoeconf
export OPTSFILE="/etc/ppp/peers/dsl-provider"

# IMPORTANT: Do not use gdialog unless it has been fixed!
DIALOG=whiptail

# SUID wrapper for KNOPPIX added by Klaus Knopper <knoppix@knopper.net>
# mod. by EB
PATH="/bin:/sbin:/usr/bin:/usr/sbin"
export PATH

# for non-root, try to reexec with sudo, warn otherwise
if [ "`id -u`" != "0" ]; then
  if which sudo >/dev/null; then
    exec sudo "$0" "$@"  || exit 1
  else
    echo $"Please become root before running pppoeconf!"
    echo $"Press return to continue..."
    read
    exit 1
  fi
fi

# EOF SUID wrapper

# make a secure directory
umask 177
export TMP="/etc/ppp/tmp$RANDOM"
mkdir "$TMP"
#sectempfile=`tempfile -d $TMP 2>/dev/null` || exit 1
sectempfile="$TMP/$RANDOM"
trap "rm -rf '$TMP'" 0 1 2 5 15

namehelp=$"
Most providers send the needed login information per mail. Some describe it in odd ways, assuming the user to input the data in their \"user-friendly\" setup programs. But in fact, this applications generate usuall PPP user names and passwords from the data. You can do this too and input the correct data in the dialog box.\n\nFor example, this is the method used by the german Telekom:

Anschlusskennung: 11111111111
T-Onlinenummer: 222222222222 
Mitbenutzer: 0001
Vollstndiger Benuztername: 111111111111222222222222#0001@t-online.de
"

list=$( LANG=C /sbin/ifconfig -a | grep "Ethernet" | cut -f1 -d" " )

if test "$list" ; then
  number=`echo $list | wc -w| tr -d " "`
  $DIALOG --title $"ALL DEVICES FOUND?" --clear --yesno $"I found $number ethernet device(s):\n$list\n\nAre all your ethernet interfaces listed above?\n(If No, modconf will be started so you can load the card drivers manually).\n\nOr press Ctrl-C to abort here." 15 60
  case $? in
    1)
    # configure and restart
      modconf
      $0
      exit $?
      ;;
    255)
      rm -rf "$TMP"
      exit 1
      ;;
  esac
  # now, execute an AC lookup on each interface
  for iface in $list; do
    touch $TMP/pppoe.scan
    (pppoe -A -I $iface > $TMP/$iface.pppoe ; rm $TMP/pppoe.scan) &
    ( time=0 ; while test -f $TMP/pppoe.scan ; do time=`expr $time + 6`; echo $time; sleep 1; done ) | $DIALOG --title $"SCANNING DEVICE" --gauge $"Looking for PPPoE Access Concentrator on $iface..." 10 40 0
  done
  cd "$TMP"
  iface=`grep -l AC eth*.pppoe| cut -f1 -d"." | head -n1`
  if test -z "$iface" ; then
    $DIALOG --title $"NOT CONNECTED" --clear --msgbox $"Sorry, I scanned $number interface(s), but the Access Concentrator of your provider did not respond. Please check your network and modem cables. Another reason for the scan failure may also be another running pppoe process which controls the modem." 15 60
    rm -rf "$TMP"
    exit 1;
  fi
  $DIALOG --title $"DSL CONNECTION FOUND" --clear --yesno $"I found an Access Concentrator on $iface. Should I setup PPPOE for this connection?" 15 60
  if test "$?" = "0"; then
    # sanity check
     # install the default line
    grep '^.*pty.*pppoe.*-I.*eth' $OPTSFILE || echo 'pty "pppoe -I eth0 -T 80"' >> $OPTSFILE
    # install alternative lines
    grep '^.*pty.*pppoe.*-m.*1452' $OPTSFILE || echo '#pty "pppoe -I eth0 -T 80 -m 1452"' >> $OPTSFILE
    grep '^.*pty.*pppoe.*-m.*1412' $OPTSFILE || echo '#pty "pppoe -I eth0 -T 80 -m 1412"' >> $OPTSFILE
    # at least one must work
    grep '^pty' $OPTSFILE || echo 'pty "pppoe -I eth0 -T 80"' >> $OPTSFILE
    
    sed -e "s/eth[:0-9:]* /$iface /" $OPTSFILE > "$sectempfile"
    mv "$sectempfile" $OPTSFILE
  else
    rm -rf "$TMP"
    exit 1
  fi
  # STATUS: interface is $iface, we can continue
  
  $DIALOG --title $"OKAY TO MODIFY" --clear --yesno $"If you continue with this program, the configuration file $OPTSFILE will be modified. Please make sure that you have a backup copy before saying Yes.\n\nContinue with configuration?" 22 70
  if test "$?" != "0" ; then
    rm -rf "$TMP"
    exit 0
  fi

  # ask about sane options
  $DIALOG --title $"POPULAR OPTIONS" --clear --yesno $"Most people using popular dialup providers prefer the options 'noauth' and 'defaultroute' in their configuration and remove the 'nodetach' option. Further, for busy providers the lcp-echo-interval could be increased. Should I check your configuration file and change these settings where neccessary?" 22 70
  if test "$?" = "0" ; then
    grep '^noauth' $OPTSFILE || echo 'noauth' >> $OPTSFILE
    grep '^defaultroute' $OPTSFILE  || echo 'defaultroute' >> $OPTSFILE
    sed -e "s/^nodetach.*//" $OPTSFILE > "$sectempfile"
    mv "$sectempfile" $OPTSFILE
    sed -e "s/^lcp-echo-interval 20$/lcp-echo-interval 60/" $OPTSFILE > "$sectempfile"
    mv "$sectempfile" $OPTSFILE
  fi
  
  user=`grep ^user $OPTSFILE|cut -f2 -d" " | tr -d '"'`
  test -z "$user" && user="username"
  while test "$goahead" != "yes" ; do
    $DIALOG --nocancel --title $"ENTER USERNAME" --clear --inputbox $"Please enter the username which you usually need for the PPP login to your provider in the input box below. If you wish to see the help screen, delete the username and press OK." 15 60 $user 2> "$sectempfile"
    user=`cat "$sectempfile"`
    case $? in
    0)
    if test -z "$user" ; then
      $DIALOG --scrolltext --msgbox "$namehelp" 17 75
    else
      sed -e 's/^user .*//' $OPTSFILE > "$sectempfile"
      mv "$sectempfile" $OPTSFILE
      echo  "user \"$user\"" >> $OPTSFILE
      export goahead="yes"
    fi
      ;;
    *)
      exit 1
      rm -rf "$TMP"
      ;;
    esac
  done

  $DIALOG --nocancel --title $"ENTER PASSWORD" --clear --inputbox $"Please enter the password which you usually need for the PPP login to your provider in the input box below.\n\nNOTE: you can see the password in plain text while typing." 15 60 2> "$sectempfile"
  pass=`cat "$sectempfile"`
  case $? in
  0)
    sed -e "s/^\"*$user\"* .*//" /etc/ppp/pap-secrets > "$sectempfile"
    mv "$sectempfile" /etc/ppp/pap-secrets
    echo "\"$user\" * \"$pass\"" >> /etc/ppp/pap-secrets
    ;;
  *)
    rm -rf "$TMP"
    exit 1
    ;;
  esac

  # ask about DNS
  $DIALOG --title $"USE PEER DNS" --clear --yesno $"You need at least one DNS IP addresses to resolve the normal host names. Normally your provider sends you addresses of useable servers when the connection is established. Would you like to add these addresses automaticaly to the list on nameservers in your local /etc/resolv.conf file? (recommended)" 15 60
  case $? in
  0)
    sed -e 's/usepeerdns//' $OPTSFILE > "$sectempfile"
    mv "$sectempfile" $OPTSFILE
    echo "usepeerdns" >> $OPTSFILE
  ;;
  esac
  
  # ask about MSS limitation
  $DIALOG --title $"LIMITED MSS PROBLEM" --clear --yesno $"Many providers have routers that do not support IP packets with MSS more than 1500. Normal packets outgoing from this machine have this MSS size. Unfortunately if you are forwarding packets from another hosts (ie. doing masquerading) the MSS may become increased depending on the paket size and the route to the client host. So your client machines won't be able to connect to some sites. There is a solution: the maximum MSS can be limited by pppoe. You can find more details about this issue in pppoe documentation.\n\nShould pppoe clamp MSS at 1452 bytes?\n\nIf unsure, say yes.\n\n(If you still get problems described above, try setting to 1412 in the dsl-provider file.)" 22 70
  case $? in
  0)
  # disable the old line
    sed -e 's/^pty/#&/' $OPTSFILE > "$sectempfile"
    mv "$sectempfile" $OPTSFILE
  # enable the one with our mss size
    sed -e 's/^#\(pty.*-m 1452.*\)/\1/' $OPTSFILE > "$sectempfile"
    mv "$sectempfile" $OPTSFILE
  ;;
  esac
  rm -f "$sectempfile"

  $DIALOG --title $"DONE" --clear --yesno $"Your PPPD is configured now. Would you like to start the connection at boot time?" 15 60
  if test "$?" = "0"; then
    if [ ! -f /etc/ppp/ppp_on_boot ] ; then
      ln -s ppp_on_boot.dsl /etc/ppp/ppp_on_boot || true
    fi
  fi

  cd /
  
  # end of story
  rm -rf "$TMP"
  
  $DIALOG --title $"ESTABLISH A CONNECTION" --clear --yesno $"Now, you can make an DSL connection with \"pon dsl-provider\" and terminate it with \"poff\". Would you like to start the connection now?" 15 60
  case $? in
  0)
    cd /
    pon dsl-provider
    watch /sbin/ifconfig ppp0
    ;;
  *)
    exit 0
    ;;
  esac

else
  $DIALOG --title $"NO INTERFACE FOUND" --clear --yesno $"Sorry, no working ethernet card could be found. If you do have an interface card which was not autodetected so far, you probably wish to load the driver manually using the modconf utility. Run modconf now?" 20 70
  case $? in
  0)
    rm -rf "$TMP"
    modconf
    exec $0
    exit $?
    ;;
  *)
    rm -rf "$TMP"
    exit 1
    ;;
  esac
fi
