#!/bin/sh
# (c) papaschtroumpf 2005 for puppy linux
# this "control center" interacts with the BetaFTPD FTP server program
# which runs in the background
#
# This script is designed to run with a version of betaftpd that I
# modified to accept a command line argument "-restrict" to restrict
# upload to the anonymous account.
# Note that there is no way to determine the restricted state of the 
# server at run time, so if the server was started manually, the state
# of the semaphore file (/ect/betaftpd.restricted) may be wrong!
# If in doubt click the button to force the desired state
#

VERSIONNUMBER="0.0.8pre17-pup-4"
INSTALLDIR="/usr/sbin"
RUNOPTIONS="-contain"
SEMAPHOREFILE="/etc/betaftpd.restricted"
HELPDIR="/usr/share/doc/"
ANONUSER="ftp"
BGCOLOR="#e0ffe0"
ERRORCOLOR="#ffe0e0"

# The BetaFTPD package for puppy provides a deny_sh "fake shell"
# that can be used to prevent users created for FTP purpose only to
# be able to have shell access
# We will use this for the "ftp" user
DENYSHELL="/usr/bin/deny_sh"


while true
do 

  ANONSTATUS="Anonymous FTP access is disabled"
  ANONBUTTON="Enable Anon:15,"
  ANONDIR=""
  RESTRICTEDSTATUS=""
  RESTRICTEDBUTTON=""
  grep "$ANONUSER:" /etc/passwd
  if [ $? -eq 0 ] ; then
    if [ -d /root/ftp ] ; then
      ANONDIR=`grep "$ANONUSER:" /etc/passwd | cut -f 6 -d":"`
      ANONSTATUS="Anonymous FTP access is available for $ANONDIR"
      ANONBUTTON="Disable Anon:14,"
      if [ -f $SEMAPHOREFILE ] ; then
        RESTRICTEDSTATUS="Download-only"
        RESTRICTEDBUTTON="Full Access:16,"
      else
        RESTRICTEDSTATUS="FULL"
        RESTRICTEDBUTTON="Download Only:17,"
      fi
    fi
  fi


  if [ -f /var/log/xferlog ]
  then
    LOGSTATUS="ENABLED"
    LOGBUTTON="Disable Logging:12,"
    VIEWBUTTON="View Xfer Log:13,"
  else
    LOGSTATUS="DISABLED"
    LOGBUTTON="Enable Logging:12,"
    VIEWBUTTON=""
  fi

  
  ps | egrep "[SR] +$INSTALLDIR/betaftpd" >/dev/null
  if [ $? -eq "0" ]
  then
    RUNSTATUS="RUNNING"
    RUNBUTTON="Stop FTP Server:11,"
  else
    RUNSTATUS="STOPPED"
    RUNBUTTON="Start FTP Server:10,"
  fi

BUTTONS="${LOADSAVEBUTTONS}$RUNBUTTON$ANONBUTTON$RESTRICTEDBUTTON$LOGBUTTON$VIEWBUTTON Help:21,EXIT:19"

  xmessage -bg "$BGCOLOR" -center -name "BetaFTPD" -title "BetaFTPD Control Center" -buttons "$BUTTONS" -file -<<MSG1
--- This is the BetaFTPD Control Center ---
BetaFTPD is a single threaded fast and efficient FTP server

* This is betaftpd-$VERSIONNUMBER
* BetaFTPD is currently $RUNSTATUS
* BetaFTPD log (/var/log/xferlog) is currently $LOGSTATUS
* $RESTRICTEDSTATUS $ANONSTATUS
* Users with a password on their account have full access 
  to their home directory by logging in as themselves.

Changing options may restart the server.
MSG1

  RETVALUE=$?

  ##################################################################
  #  Start Server
  #
  if [ $RETVALUE -eq 10 ];then
    if [ -f $SEMAPHOREFILE ] ; then
      $INSTALLDIR/betaftpd -restrict $RUNOPTIONS
    else
      $INSTALLDIR/betaftpd $RUNOPTIONS
    fi
    continue
  fi


  ##################################################################
  #  Stop Server
  #
  if [ $RETVALUE -eq 11 ];then 
    THEPID=`ps | egrep -e"[SR] +/usr/sbin/betaftpd" | sed -e "s/^ \+//" | cut -f1 -d" "`
    kill $THEPID
    continue
  fi



  ##################################################################
  #  Toggle logging
  #
  if [ $RETVALUE -eq 12 ];then
    # not sure what the server will do if the logging file disappears 
    # from under it, and it will not discover the file has been created
    # until a restart, so always restart it if it was started
    if [ $RUNSTATUS = "RUNNING" ] ; then
      THEPID=`ps | egrep -e"[SR] +/usr/sbin/betaftpd" | sed -e "s/^ \+//" | cut -f1 -d" "`
      kill $THEPID
      sleep 2
    fi
    if [ "$LOGSTATUS" = "DISABLED" ];then
      # creating the log file enables logging
      echo "# This is the BetaFTPD transfer log" >/var/log/xferlog
      echo "# Format: <date> <xfer time> <IP address> <xfer size> <filename> b _ <iNPUT/oUTPUT> a <owner> ftp 0 *" >>/var/log/xferlog
    else
      rm -f /var/log/xferlog
      xmessage -bg "$BGCOLOR" -center -name "BetaFTPD" -title "BetaFTPD Control Center" "The log file has been reset"
    fi
    sync
    if [ $RUNSTATUS = "RUNNING" ] ; then
      if [ -f $SEMAPHOREFILE ] ; then
        $INSTALLDIR/betaftpd -restrict $RUNOPTIONS
      else
        $INSTALLDIR/betaftpd $RUNOPTIONS
      fi
    fi
    continue
  fi


  ##################################################################
  #  View Transfer Log
  #
  if [ $RETVALUE -eq 13 ];then
    if [ -f /var/log/xferlog ] ; then
      sync
      leafpad /var/log/xferlog &
    else
      xmessage -bg "$ERRORCOLOR" -center -name "BetaFTPD" -title "BetaFTPD Control Center" "BetaFTPD logging is not enabled"
    fi
    continue
  fi


  ##################################################################
  #  Disable Anonymous
  #
  if [ $RETVALUE -eq 14 ];then
    if [ $RUNSTATUS = "RUNNING" ] ; then
      THEPID=`ps | egrep -e"[SR] +/usr/sbin/betaftpd" | sed -e "s/^ \+//" | cut -f1 -d" "`
      kill $THEPID
      sleep 1
    fi
    deluser $ANONUSER >/dev/null
    # I think this is safe: clean up the group file since it fills in a new entry whenever we
    # create a new instance of the "ftp" user
    sed -i.bak '/^[0-9]\+:x:[0-9]\+:ftp *$/ d' /etc/group
    grep "$ANONUSER:" /etc/passwd >/dev/null
    if [ $? -eq 0 ] ; then
      xmessage -bg "$ERRCOLOR" -center -name "BetaFTPD" -title "BetaFTPD Control Center" -file -<<MSG3
Could not remove the "$ANONUSER" user!
You should delete the user manually (try the deluser command)
or at least rename/remove the $ANONDIR directory.
Until you do this, Anonymous FTP is STILL ENABLED!
The server has been stopped.
MSG3
    else
      xmessage -bg "$BGCOLOR" -center -name "BetaFTPD" -title "BetaFTPD Control Center" -file -<<MSG4
The user "$ANONUSER" was removed to prevent Anonymous FTP
The shared directory $ANONDIR was not touched however.
The server will be restarted if it was running.
MSG4
      if [ $RUNSTATUS = "RUNNING" ] ; then
        $INSTALLDIR/betaftpd -restrict $RUNOPTIONS 
      fi
    fi
    continue
  fi


  ##################################################################
  #  Enable Anonymous
  #
  if [ $RETVALUE -eq 15 ];then
    if [ $RUNSTATUS = "RUNNING" ] ; then
      THEPID=`ps | egrep -e"[SR] +/usr/sbin/betaftpd" | sed -e "s/^ \+//" | cut -f1 -d" "`
      kill $THEPID
      sleep 1
    fi
    echo "echo --- Creating the \"$ANONUSER\" user for Anonymous FTP access" > /tmp/betaftd-cc.tmp
    echo "echo For security reasons, the user \"ftp\" will not have shell access" >> /tmp/betaftd-cc.tmp
    echo "echo however sure the password you enter below in NOT BLANK" >> /tmp/betaftd-cc.tmp

    echo "echo \" \"" >> /tmp/betaftd-cc.tmp
    echo "adduser -h /root/$ANONUSER -s $DENYSHELL $ANONUSER " >> /tmp/betaftd-cc.tmp
    chmod +x /tmp/betaftd-cc.tmp
    rxvt -bg Yellow3 -e "/tmp/betaftd-cc.tmp"
    rm -f /tmp/betaftd-cc.tmp
    if [ -f $SEMAPHOREFILE ] ; then
      RESTRICTEDSTATUS="Download-only"
    else
      RESTRICTEDSTATUS="FULL upload/delete"
    fi
    grep "$ANONUSER:" /etc/passwd >/dev/null
    if [ $? -eq 0 ] ; then
      echo "Anonymous FTP directory" >/root/$ANONUSER/betaftpd.anon.readme
      xmessage -bg "$BGCOLOR" -center -name "BetaFTPD" -button "Show Dir:10,Okay:11" -title "BetaFTPD Control Center" -file -<<MSG6
The user "$ANONUSER" was properly created.
Anonymous FTP is now available for directory: /root/$ANONUSER
Anonymous users have $RESTRICTEDSTATUS access to that directory.
If the directory already existed, all files that were in it are now shared!
You may want to check the contents of the directory (click "Show Dir").
The server will be restarted if it was running.
MSG6
      if [ $? -eq 10 ] ; then
        rox -d /root/$ANONUSER
      fi
    else
      xmessage -bg "$ERRCOLOR" -center -name "BetaFTPD" -title "BetaFTPD Control Center" -file -<<MSG5
Could not create the "$ANONUSER" user!
You can try and create the user manually (try the adduser command)
Until you do this, Anonymous FTP is NOT ENABLED!
MSG5
    fi
    if [ $RUNSTATUS = "RUNNING" ] ; then
      if [ -f $SEMAPHOREFILE ] ; then
        $INSTALLDIR/betaftpd -restrict $RUNOPTIONS
      else
        $INSTALLDIR/betaftpd $RUNOPTIONS
      fi
    fi
    continue
  fi



  ##################################################################
  #  Enable full access
  #
  if [ $RETVALUE -eq 16 ];then
    if [ $RUNSTATUS = "RUNNING" ] ; then
      THEPID=`ps | egrep -e"[SR] +/usr/sbin/betaftpd" | sed -e "s/^ \+//" | cut -f1 -d" "`
      kill $THEPID
      sleep 1
    fi
    # remove the semaphore file
    rm -f $SEMAPHOREFILE
    sync
    if [ $RUNSTATUS = "RUNNING" ] ; then
      $INSTALLDIR/betaftpd $RUNOPTIONS
    fi
    xmessage -bg "$BGCOLOR" -center -name "BetaFTPD" -title "BetaFTPD Control Center" "Everyone can now upload or delete files in $ANONDIR!"
    continue
  fi



  ##################################################################
  #  Enable download only access
  #
  if [ $RETVALUE -eq 17 ];then
    if [ $RUNSTATUS = "RUNNING" ] ; then
      THEPID=`ps | egrep -e"[SR] +/usr/sbin/betaftpd" | sed -e "s/^ \+//" | cut -f1 -d" "`
      kill $THEPID
      sleep 1
    fi
    # create the semaphore file
    echo "BetaFTPD semaphore file - DO NOT REMOVE!"> $SEMAPHOREFILE
    sync
    if [ $RUNSTATUS = "RUNNING" ] ; then
      $INSTALLDIR/betaftpd -restrict $RUNOPTIONS
    fi
    continue
  fi



  ##################################################################
  #  Help
  #
  if [ $RETVALUE -eq 21 ];then 
    dillo $HELPDIR/betaftpd.htm &
    continue
  fi


  ##################################################################
  #  Exit
  #
  if [ $RETVALUE -eq 19 ];then 
    exit
  fi
done