#!/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=/tmp/ftpuser
REAL_FTPUSER=mnemonic
REAL_FTPGROUP=mnemonic
REAL_UID=1000
REAL_GID=1000

SCRIPT_NAME=virtual_userftp4.sh
FTPCONF=/etc/proftpd   #ftpd.passwd ftpd.group position

function MENU {
  cd $FTPCONF
  clear
  echo "Virtual FTP user manager for ProFtpd - v0.4"
  echo "Please choose the letter and enter"
  echo "------------------------------------------------------"
  echo "1. Add new default virtual user / password"
  echo "2. Add new custom virtual user / password"
  echo "3. Delete virtual user / password"
  echo "4. Change user password"
  echo ""
  echo "5. Add new virtual group ftp"
  echo "6. Delete virtual group ftp"
  echo "7. Add member in a group" 
  echo ""
  echo "8. View minimal Config file"
  echo "9. View virtual file passwd and group"
  echo "10. List directory in area ftp ($DEFHOME) "
  echo ""
  echo "11. View realtime proftpd log file"
  echo "12. View realtime proftpd transfer log"
  echo ""
  echo "20. Manual Edit ftpd.passwd"
  echo "21. Manual Edit ftpd.group"
  echo "0. Exit"
  echo "choice?: " 
  read choice

case $choice in
0)
        exit
;;
1)
        ADDUSER
;;
2)
        ADDUSER_CUSTOM
;;
3)
        DELUSER
;;
4)
        CHANGEPWD
;;
5)
        ADDGROUP
;;
6)
        DELGROUP
;;
7)
        MEMBERGROUP
;;
8)
	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
;;
9)
        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
;;
10)  
  ls -l $DEFHOME
  read tasto
  MENU
;;
11)
  LOG
;;
12)
  TRANSFER_LOG
;;



20)
  EDIT_PASSWDFILE
;;
21)
  EDIT_GROUPFILE
;;
esac  

  }


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

	echo "Create default directory path with default permission? [ y/n ]"
	echo "( $DEFHOME/$NAME - $REAL_FTPUSER:$REAL_FTPGROUP - $REAL_UID:$REAL_GID )"
	read MSG1
  if [ $MSG1 = "y" ];  then
	# mkdir user default path area
        mkdir -p $DEFHOME/$NAME
        chown $REAL_FTPUSER:$REAL_FTPGROUP $DEFHOME/$NAME
  else
	echo "If the directory does not exist you will need to create it"
        echo "with the correct name ( $DEFHOME/$NAME ) and permissions!!!"
        echo "ftp virtual user row will be added in any case !!!"
        echo ""
fi
	# Add ftp user default path
        echo "Add User...."
        ftpasswd --uid $REAL_UID --gid $REAL_GID --gecos "$NAME" --name $NAME --shell /bin/false --home $DEFHOME/$NAME --passwd
	
        echo "Press return to menu"
        read tasto
  MENU
  }
  
function ADDUSER_CUSTOM {
	echo "Add new custom user function"
	echo "Input new user name:"
	read NAME

	echo "Mapping on a real User name:"
	read USER_MAPPING
  
	echo "Mapping on a real Group name:"
	read GROUP_MAPPING
  
	echo "User uid:"
	read USERID

	echo "User gid:"
	read GROUPID

	echo "Custom ftp directory path (absolute)"
	read CUSTOM_HOME
  
	echo "Create custom directory path with custom permission? [ y/n ]"
	echo "( $CUSTOM_HOME - $USER_MAPPING:$GROUP_MAPPING - $USERID:$GROUPID )"
	read MSG1
  if [ $MSG1 = "y" ];  then
        # mkdir user custom path area
        mkdir -p $CUSTOM_HOME
        chown $USER_MAPPING:$GROUP_MAPPING  $CUSTOM_HOME
  else
        echo "If the directory does not exist you will need to create it"
        echo "with the correct name ( $CUSTOM_HOME ) and permissions!!!"
	echo "ftp virtual custom user row will be added in any case !!!"
	echo ""
  fi
        # Add ftp user default path
        echo "Add User...."
        ftpasswd --uid $USERID --gid $GROUPID --gecos "$NAME" --name $NAME --shell /bin/false --home $CUSTOM_HOME --passwd
        
        echo "Press return to menu"
        read tasto
  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 default path
  	 ftpasswd --uid $USERID --gid $GROUPID --gecos "$NAME" --name $NAME --shell /bin/false --home $DEFHOME/$NAME --passwd --delete-user
    
  	 echo "Press return to menu"
  	 read tasto
  else
  	 # delete user custom path
  	 echo "Insert Custom ftp directory path (absolute) to Delete:"
  	 read CUSTOM_HOME
  	 # Delete user custom
  	 ftpasswd --uid $USERID --gid $GROUPID --gecos "$NAME" --name $NAME --shell /bin/false --home $CUSTOM_HOME --passwd --delete-user
    
  	 echo "Press return to menu"
  	 read tasto
  fi
  MENU
  }

function CHANGEPWD {
        echo "Change user password"
        echo "Input user name:"
        read NAME
  
        # Change password
        ftpasswd --passwd --name=$NAME --change-password
  
        echo "Press return to menu"
  	read tasto
  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
  
        echo "Press return to menu"
        read tasto
  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
  
        echo "Press return to menu"
        read tasto
  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
   
        echo "Press return to menu"
        read tasto     
  MENU
  }

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

function TRANSFER_LOG {
	echo "ctrl+c per terminare"
	echo ""
	tail -f /var/log/proftpd/xferlog
  }
   
function TRANSFER_REPORT {
	echo "ctrl+c per terminare"
	echo ""
	tail -f /var/log/proftpd/xferreport
  }   

function EDIT_PASSWDFILE {
  chmod 644 $FTPCONF/ftpd.passwd
  vi $FTPCONF/ftpd.passwd
  chmod 444 $FTPCONF/ftpd.passwd
  $SCRIPT_NAME
  }

function EDIT_GROUPFILE {
  chmod 644 $FTPCONF/ftpd.group
  vi $FTPCONF/ftpd.group
  chmod 444 $FTPCONF/ftpd.group
  $SCRIPT_NAME
  }  
  
MENU
