#!/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/500gb/ftpuser

FTPCONF=/etc/proftpd

function MENU {
	cd $FTPCONF
	clear
	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 "8. View virtual file passwd and group"
	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
;;
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
;;
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
		else	
		
		echo "virtual ftp_home user share"
		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 "
        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 "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
