#!/bin/bash
# 
#  dd-on-port v1.0b by flower/project-deedee.
#
# read the prj-dd.nfo for info BEFORE running :)

INETDPIDFILE="/var/run/inetd.pid"
INETDPATH="/usr/sbin/inetd"
DEFAULTREDIRECT="1998"
BBSUSER="bbs"
INSTALLPORT=$1

echo
echo "[ flo's dd-on-port script v1.0b - use on own risk (works nice though :) ]"
echo
if [ ! "`whoami`" = "root" ]; then
  echo "You must be root to use this shit."
  exit
fi
if [ "$INSTALLPORT" = "" ]; then
  echo "Syntax: dd-on-port <port> | backup | restore"
  echo
  echo "Where port is the port you want to let dd use."
  echo "use 'backup' to backup files that might be changed, and 'restore'"
  echo "to restore backed up files."
  echo
  exit
fi

if [ "$1" = "backup" ]; then
  echo "Backing up files.."
  if [ -e /etc/shadow ]; then
    tar zcvPf /tmp/.backedup_dd.tgz /etc/passwd /etc/shadow /etc/services /etc/inetd.conf
  else
    tar zcvPf /tmp/.backedup_dd.tgz /etc/passwd /etc/services /etc/inetd.conf
  fi
  echo "Done"
  exit
fi

if [ "$1" = "restore" ]; then
  if [ -e /tmp/.backedup_dd.tgz ]; then
    echo "Restoring backed up files.. "
    tar zxvPf /tmp/.backedup_dd.tgz
    echo "Done"
  else
    echo "No backup found, nothing restored.."
  fi
  exit
fi

if [ ! -x in-bbs.shar ]; then
  echo "./in-bbs.shar not found, or not executable!"
  echo "this file must exist and be executable before proceeding!"
  exit 
fi

if [ -e $INETDPIDFILE ]; then
  INSTALLEDALREADY=`cat /etc/services | grep bbs`
  if [ ! "$INSTALLEDALREADY" = "" ]; then
    echo
    echo "Hmm.. Seems like you've used this script already.."
    echo 
    echo "Extract from /etc/services: "
    echo $INSTALLEDALREADY
    echo
    echo "Do you want to remove old setup?"
    echo "ONLY do this if you've used this script to install, or you might fuckup"
    echo "/etc/services and /etc/inetd.conf"
    echo
    echo "Hit enter to clean up old setup (ctrl-c to abort)"
    read removeold
    echo 
    echo -n "Removing old config from /etc/services and /etc/inetd.conf.. "
    cat /etc/services | grep -v "bbs	" > ./.tmp_services
    mv ./.tmp_services /etc/services
    cat /etc/inetd.conf | grep -v "bbs	stream	tcp" > ./.tmp_inetdconf
    mv ./.tmp_inetdconf /etc/inetd.conf
    echo "Done"
    echo 
  fi
  echo "You have chosen to install DD on port $INSTALLPORT."
  echo
  echo "Extracts from /etc/services for port $INSTALLPORT.."
  TEMP=`cat /etc/services | grep $INSTALLPORT/tcp | head -1`
  SUREPORT=`echo $TEMP | cut -f 2 -d " " | cut -f 1 -d "/"`
  if [ ! "$SUREPORT" = "$INSTALLPORT" ]; then
    echo "(no protocol assigned)"
  else
    echo $TEMP
    PROT1=`echo $TEMP | cut -f 1 -d " "`
    PROT2=`echo $TEMP | cut -f 2 -d " "`
    echo 
    echo "Seems like this port is already in use. Please install on another port,"
    echo "or enter new port for protocol $PROT1."
    echo
    echo -n "Enter new port for protocol $PROT1 (DEFAULT=$DEFAULTREDIRECT), or ctrl-c to abort: "
    read newport
    if [ "$newport" = "" ]; then
      newport=$DEFAULTREDIRECT
    fi
    INSTALLEDPORT=`cat /etc/services | grep $newport | head -1`
    SUREPORT=`echo $INSTALLED | cut -f 2 -d " " | cut -f 1 -d "/"`
    if [ "$newport" = "$SUREPORT" ]; then
      echo "Hmm.. The port you selected is in use already.. "
      echo
      echo $INSTALLEDPORT
      echo
      echo "Run the script again, and select another port to move $PROT1 to.."
      exit 
    fi
    cat /etc/services | grep $PROT1 | grep $PROT2 > ./.services.cutline
    cat /etc/services | grep -v "`cat ./.services.cutline`" > ./.services.new
    echo "$PROT1	$newport/tcp" >> ./.services.new
    NEWPORTSETUP=`cat ./.services.new | grep $PROT1 | tail -1`
    echo
    echo "New port for protocol $PROT1 is gonne be:"
    echo $NEWPORTSETUP
    echo
    echo "Hit (Enter) to accept new setup, or ctrl-c to abort."
    read acceptnewport
    mv ./.services.new /etc/services
    rm ./.services.cutline
  fi
  echo
  echo "Hit (Enter) to continue to install bbs on port $INSTALLPORT or ctrl-c to abort."
  read sure

  echo -n "Modifying system config: /etc/services "
  echo "bbs		$INSTALLPORT/tcp" >> /etc/services
  echo -n "/etc/inetd.conf "
  echo "bbs	stream	tcp	nowait	root	/usr/sbin/tcpd	in.bbs	-L /usr/sbin/bbslogin" >> /etc/inetd.conf
  echo ".. Done"

  echo -n "Extracting in.bbs daemon to /usr/sbin/in.bbs.. "
  ./in-bbs.shar m0nk3yw4sh3r3
  chmod +x /usr/sbin/in.bbs
  :> /etc/issue.bbs
  echo "Done"
  echo -n "Creating loginscript /usr/sbin/bbslogin.. "
  echo "#!/bin/bash" > /usr/sbin/bbslogin
  echo "exec login bbs" >> /usr/sbin/bbslogin
  chmod +x /usr/sbin/bbslogin
  echo "Done"
  echo -n "Restarting inetd.. "
  kill `cat /var/run/inetd.pid`
  echo -n "Killed.. Sleeping.. "
  sleep 5
  $INETDPATH
  echo "Restarted"
  echo
  echo -n "Checking if bbs-user ($BBSUSER) has a password set.. "
  if [ -e /etc/shadow ]; then
    SHADOW="YEH"
    echo "found Shadow pwd file"
    BBSLINE=`cat /etc/shadow | grep $BBSUSER`
  else
    SHADOW="NAH"
    echo "using passwd file"
    BBSLINE=`cat /etc/passwd | grep $BBSUSER | grep daydream`
  fi 
  if [ "$BBSLINE" = "" ]; then
    echo "HMM! No bbs user found.. Not good.. "
    echo
    echo "Try editing the dd-on-port file, and change the BBSUSER definition in"
    echo "the top to the name of your bbsuser, then run the script again."
    echo
    exit
  else
    BBSPWD=`echo $BBSLINE | cut -f 2 -d ":"`
    if [ "$BBSPWD" = "" ]; then
      echo "Found no password for user $BBSUSER.. No changes made.. "
    else
      echo "Found password for user $BBSUSER.. "
      echo
      echo -n "Removing password for user $BBSUSER.. "
      if [ "$SHADOW" = "YEH" ]; then
        cat /etc/shadow | grep $BBSUSER > ./.sh.cl
        cat /etc/shadow | grep -v "`cat ./.sh.cl`" > ./.newpwd
        echo "`cat ./.sh.cl | cut -f 1 -d ":"`::`cat ./.sh.cl | cut -f 3-100 -d ":"`" >> ./.newpwd
      else
        cat /etc/passwd | grep $BBSUSER | grep daydream > ./.sh.cl
        cat /etc/passwd | grep -v "`cat ./.sh.cl`" > ./.newpwd
        echo "`cat ./.sh.cl | cut -f 1 -d ":"`::`cat ./.sh.cl | cut -f 3-100 -d ":"`" >> ./.newpwd
      fi
      echo "Done"
      echo
      echo "New updated password entry for user $BBSUSER.."
      cat ./.newpwd | grep $BBSUSER
      echo 
      echo "Hit (Enter) to accept, or ctrl-c to abort."
      read makechanges
      echo
      if [ "$SHADOW" = "YEH" ]; then
        echo -n "Updating /etc/shadow.. "
        mv ./.newpwd /etc/shadow
      else 
        echo -n "Updating /etc/passwd.. "
        mv ./.newpwd /etc/passwd
      fi
      rm ./.sh.cl
      echo "Done"
    fi
  fi
  echo
  echo "So, everything should be fixed now, try telnetting to port $INSTALLPORT to"
  echo "check if everything works as it should."
  echo 
  echo "Have a nice day, Flo."
else
  echo
  echo "You dont seem to be running inetd. In order to use this script you must"
  echo "use inetd. If you are running inetd, but we just cant detect it, please"
  echo "take a look at the top of this script, and maybe edit it yourself."
  echo
fi

