Monday, December 15, 2008
Adding FTP Users to a Mediatemple (dv)
After going to some pains to set up additional FTP users on my (dv) for different domains, I finally came up with a script that I think will help you all out (in the case you need to grant someone access to a single folder underneath a website directory).
To use this script, you must have root access enabled and be logged in as root (or su’d). I do not know if sudo will work for this.
The script should be fairly straightfoward… if you have any problems, please post here…. although there shouldn’t be any as I extensively tested this.
And without further ado…
You need to create two files. ftpuser.sh and passwd.exp
ftpuser.sh
#!/bin/sh
#
# (mt) DV 3.0+ FTP User Creation Shell Script
# Author: Brendan Corcoran
# Site: www.brendancorcoran.com
#
vhostsdir=/var/www/vhosts/
echo "-----------------------------------------------"
echo "- (mt) DV 3.0+ FTP User Creation Shell Script -"
echo "- This script must be run as root user! -"
echo "-----------------------------------------------"
echo " "
echo -n "Username: "
read usern
echo -n "Password: "
read userp
echo -n "Domain: "
read userd
echo -n "Subdomain: "
read usersd
echo -n "Access Path: "
read userap
echo -n "Parent User: "
read userparent
echo -n "Parent User ID: "
grep "$userparent:" /etc/passwd | cut -d: -f3
echo -n "Verify Parent UID: "
read useruid
while :
do
echo " "
echo "-----------------------------------------------------------"
echo "Please review the above info, are you sure this is correct?"
echo "UID's MUST MATCH OR FTP USER WILL NOT HAVE ANY PERMISSIONS "
echo "-----------------------------------------------------------"
echo " "
echo -n "Please answer 'y' for Yes, 'n' for No: "
read choice
case $choice in
"y") echo "Adding User $usern with uid $useruid and gid psacln..." ;
/usr/sbin/useradd -u $useruid -o -d $vhostsdir$userd$userap -g psacln -s /bin/false $usern ;
echo "Setting user's password to $userp..." ;
./passwd.exp $usern $userp ;
echo "Adding user to psacln group..." ;
/usr/sbin/usermod -g psacln $usern ;
if test $usersd = "" ; then
echo "Making sure $vhostsdir$userd$userap is chmod 0755..." ;
chmod 0755 $vhostsdir$userd$userap ;
elif test $usersd != "" ; then
echo "Making sure $vhostsdir$userd/subdomains/$usersd$userap is chmod 0755"
chmod 0755 $vhostsdir$userd/subdomains/$usersd$userap ;
fi
echo "Granting user permissions to $vhostsdir$userd$userap..."
chown $usern:psacln $vhostsdir$userd$userap ;
echo "Exiting..."
exit 0 ;;
"n") echo "Good thing you checked! Ending program." ; exit 0 ;;
*) echo -n "You didn't enter 'y' or 'n', press any key and try again." ; read ;;
esac
done
passwd.exp
#!/usr/bin/expect -f
#
# Small expect script to automate setting passwd
#
spawn passwd [lindex $argv 0]
set password [lindex $argv 1]
expect "password:"
send "$password\r"
expect "password:"
send "$password\r"
expect eof
Place these two files inside any non web accessible folder, preferably one that is also only accessible as root.
I placed mine in my /var/www/vhosts/domain.com/private folder… you could place them in /root if you’d like. Wherever you prefer, to be honest.
Once you have them in the correct place, make sure you are logged in as root and run the ftpuser.sh file like so:
[root@domain folder]# ./ftpuser.sh
And you will have to follow the instructions from there.
Here is an example:
Let’s say my domain name is sample.com and my domain user is sampleuser. I want to create an FTP user whose username is testuser with the password testpass in the /httpdocs/testuser directory. At the prompts I would enter:
Username: testuser
Password: testpass
Domain: sample.com
Access Path: /httpdocs/testuser
Parent User: sampleuser
Parent User ID: 10000 (THIS IS AUTOGENERATED)
Please Verify User ID: 10000
If you answer ‘y’ at the prompt you will see some messages on the screen such as:
Adding User testuser with uid 10000 and gid psacln...
Setting users password to testpass...
spawn passwd testuser
Changing password for user testuser.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
Adding user to psacln group...
Making sure /var/www/vhosts/sample.com/httpdocs/testuser is chmod 0755...
Granting user permissions to /var/www/vhosts/sample.com/httpdocs/testuser...
Exiting...
That’s it. You’re done. You should be able to log in with that username/password to that domain and the user will have access to that folder only, but with the same permissions as the primary domain user.
Hope this helps ease the pain of typing in all those commands to set up something so simple.
For Mediatemple users, please see this thread for any discussion on the topic. This could obviously be adapted to any webhost, but I tailored this to my Mediatemple (dv) 3.5