#!/bin/sh

#
#### Add user virtual ftp
# NOTE: The entry will be placed in the file ftpd.passwd by default
# ftpasswd --uid UserID --gid GroupID --gecos "Full Name" --name UserName --shell /bin/false --home Path --passwd

#### Delete an entry from the file ftpd.passwd:
# ftpasswd --uid UserID --gid GroupID --gecos "Full Name" --name UserName --shell /bin/false --home Path --passwd --delete-user

#### Adding Groups and Group Membership to ftpd.group ####
# If used with --group, ftpasswd creates a file in the group(5) format,
# suitable for use with proftpd's AuthGroupFile configuration directive.

# NOTE: Using --group Default will write output to "./ftpd.group".

# --gid
# Primary group ID for this user (optional, will default to given --uid value if absent).

# --name
# Name of the user account (required). If the name does not exist in the specified output-file, an entry will be created
# for her. Otherwise, the given fields will be updated.

# --member
# User name to be a member of the group. This argument may be
# used multiple times to specify the full list of users to be members of this group.

# --delete-group
#  Remove the entry for the given group name from the file.

#### Add a group entry use the following command:
#  ftpasswd --gid GroupID --name GroupName --group

# NOTE: The entry will be placed in the file ftpd.group by default.

#### Delete an entry from the file ftpd.group:
#  ftpasswd --gid GroupID --name GroupName --group --delete-group

#### Add a member to a group:
#  ftpasswd --gid GroupID --name GroupName --member MemberName --group

#### Setting Permissions ####
# Set permissions on the ftpd.passwd and ftpd.group files:
# chmod 400 /etc/proftpd/ftpd.passwd /etc/proftpd/ftpd.group
# chown proftpd.nogroup /etc/proftpd/ftpd.passwd /etc/proftpd/ftpd.group
#
# NOTE: You can also add or remove members from groups or add and remove groups
# themselves by editing the file ftpd.group directly.

#### Start script ftpasswd

DEFHOME=/mnt/areaftp
REAL_FTPUSER=ftpuser
REAL_FTPGROUP=ftpuser
REAL_UID=1000
REAL_GID=1000


FTPCONF=/etc/proftpd   #ftpd.passwd ftpd.group position

function MENU {
  cd $FTPCONF
  clear
  echo "Virtual FTP user for ProFtpd - v0.3"
  echo "Please choose the letter and enter"
  echo "------------------------------------------------------"
  echo "1. Add new virtual user / password"
  echo "2. Delete virual user / password"
  echo "3. Change user password"
  echo ""
  echo "4. Add new virtual group ftp"
  echo "5. Delete virtual group ftp"
  echo "6. Add member in a group" 
  echo "------------------------------------------------------"
  echo "7. View minimal Config file"
  echo "8. View virtual file passwd and group"
  echo "9. List directory in areaftp"
  echo "10. View realtime proftpd log file"
  echo "0. Exit"
  echo "choice?: " 
  read choice

case $choice in
0)
        exit
;;
1)
        ADDUSER
;;
2)
        DELUSER
;;
3)
        CHANGEPWD
;;
4)
        ADDGROUP
;;
5)
        DELGROUP
;;
6)
        MEMBERGROUP
;;
7)
  echo "List of default config variable script"
  echo "Default ftparea home = $DEFHOME"
  echo "Default real system user for ftp = $REAL_FTPUSER"
  echo "Default real system group for ftp = $REAL_FTPGROUP"
  echo "Default real uid = $REAL_UID"
  echo "Default real gid = $REAL_GID"
  echo ""
  read tasto
        MENU
;;
8)
        echo "Virtual User file ftp.passwd"
        cat $FTPCONF/ftpd.passwd
        echo ""
        echo "Virtual Group file ftp.group"
        cat $FTPCONF/ftpd.group
        echo "Press return to menu"
        echo ""
        read tasto
        MENU
;;
9)  
  ls -l $DEFHOME
  read tasto
  MENU
;;
10)
  LOG
;;
esac  

  }


function ADDUSER {
  echo "Add new user function"
  echo "Input new user name:"
  read NAME

  echo "User uid:"
  read USERID

  echo "User gid:"
  read GROUPID

  echo "Usare il path impostato di default? ( $DEFHOME/$NAME ) y/n "
  read MSG1
    if [ $MSG1 = "y" ];  then
    # Add user def path
    ftpasswd --uid $USERID --gid $GROUPID --gecos "$NAME" --name $NAME --shell /bin/false --home $DEFHOME/$NAME --passwd
      
      echo "Creare la cartella di default e relativi permessi? ( $DEFHOME/$NAME ) y/n "
            read MSG2
                  if [ $MSG2 = "y" ];  then
                  # mkdir user def path area
                  mkdir -p $DEFHOME/$NAME
                  chown $REAL_FTPUSER:$REAL_FTPGROUP $DEFHOME/$NAME
      fi
    else  
    
  
    echo "Create custom Virtual ftp_home user share:"
    echo "Creare manualmente la home ftp e relativi permessi!!!"
    read FTPHOME

    # Add user
    ftpasswd --uid $USERID --gid $GROUPID --gecos "$NAME" --name $NAME --shell /bin/false --home $FTPHOME --passwd
    
    fi
  MENU
  }
  
function DELUSER {
  echo "Delete user function"
  echo "Input user name:"
  read NAME

  echo "User uid:"
  read USERID

  echo "User gid:"
  read GROUPID

  echo "Usare il path impostato di default? ( $DEFHOME/$NAME ) y/n "
  echo "La directory e i dati dovranno essere cancellati manualmente!!!"
        read MSG1
    if [ $MSG1 = "y" ];  then
    # delete user def path
    ftpasswd --uid $USERID --gid $GROUPID --gecos "$NAME" --name $NAME --shell /bin/false --home $DEFHOME/$NAME --passwd --delete-user
  else

  echo "Delete custom Virtual ftp_home user share"
  read FTPHOME

  # Delete user
  ftpasswd --uid $USERID --gid $GROUPID --gecos "$NAME" --name $NAME --shell /bin/false --home $FTPHOME --passwd --delete-user
  fi
  MENU
  }

function CHANGEPWD {
        echo "Change user password"
        echo "Input user name:"
        read NAME
  
  # Change password
  ftpasswd --passwd --name=$NAME --change-password
  
  MENU
        }

function ADDGROUP {
  echo "Add new group function"
        echo "Input new group name:"
        read GROUPNAME

        echo "Group gid:"
        read GROUPGID

  #Add group  
  ftpasswd --gid $GROUPGID --name $GROUPNAME --group
  MENU
  }

function DELGROUP {
        echo "Delete group function"
        echo "Input group name:"
        read GROUPNAME

        echo "Group gid:"
        read GROUPGID

        #Del group
  ftpasswd --gid $GROUPGID --name $GROUPNAME --group --delete-group
  MENU
        }


function MEMBERGROUP {
  echo "Add member in a group function"
        echo "input member of a group"
  read MEMNAME

  echo "Input group name:"
        read GROUPNAME

        echo "Group gid:"
        read GROUPGID
  # Add a member to a group:
  ftpasswd --gid $GROUPGID --name $GROUPNAME --member $MEMNAME --group
  MENU
  }


function LOG {
   echo "ctrl+c per terminare"
   echo ""
   tail -f /var/log/proftpd/proftpd.log  
   }

MENU
