Last active
February 17, 2017 18:16
-
-
Save devthejo/de2986504f8165fba212e1e2655e2760 to your computer and use it in GitHub Desktop.
vsftpd virtual users management made easy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
$rootDir = '/var/www/html/'; | |
$file = '/etc/vsftpd/ftpd.passwd'; | |
$username = getenv('USERNAME') ?: getenv('USER'); | |
if($username!='root'){ | |
echo "You must run this script as root: sudo vsftpd-user ...\n"; | |
exit; | |
} | |
$map = []; | |
if(file_exists($file)){ | |
foreach(file($file) as $line){ | |
$line = trim($line); | |
if(empty($line)) continue; | |
$sep = strpos($line,':'); | |
$user = substr($line,0,$sep); | |
$password = substr($line,$sep+1); | |
$map[$user] = $password; | |
} | |
} | |
$action = isset($argv[1])?$argv[1]:null; | |
$user = isset($argv[2])?$argv[2]:null; | |
$pass = isset($argv[3])?$argv[3]:null; | |
switch($action){ | |
case 'update'; | |
if(!isset($map[$user])){ | |
echo "User $user allready exists, use add or set to create it\n"; | |
exit; | |
} | |
break; | |
case 'add'; | |
if(isset($map[$user])){ | |
echo "User $user allready exists, use update or set to update it\n"; | |
exit; | |
} | |
break; | |
} | |
switch($action){ | |
case 'update'; | |
case 'add'; | |
case 'set'; | |
if(isset($map[$user])){ | |
echo "User $user updated\n"; | |
} | |
else{ | |
echo "User $user created\n"; | |
} | |
if(!is_dir($rootDir.$user)){ | |
mkdir($rootDir.$user,0777,true); | |
} | |
chmod($rootDir.$user,0777); | |
$map[$user] = trim( shell_exec('openssl passwd -1 '.escapeshellarg($pass)) ); | |
break; | |
case 'remove'; | |
if(!$user){ | |
echo "Missing user parameter\n"; | |
exit; | |
} | |
if(isset($map[$user])){ | |
unset($map[$user]); | |
echo "User $user removed\n"; | |
} | |
else{ | |
echo "User $user not found\n"; | |
} | |
break; | |
case 'list'; | |
echo implode("\n",array_keys($map))."\n"; | |
break; | |
default: | |
echo "Available commands are 'set', 'remove', 'add', 'update', 'list'\n"; | |
exit; | |
break; | |
} | |
$content = ''; | |
foreach($map as $u=>$p){ | |
$content .= "$u:$p\n"; | |
} | |
if(!file_exists($dir=dirname($file))) mkdir($dir,0777,true); | |
file_put_contents($file,$content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#see http://askubuntu.com/questions/575523/how-to-setup-virtual-users-for-vsftpd-with-access-to-a-specific-sub-directory | |
#and for ssl: https://doc.ubuntu-fr.org/vsftpd | |
#/etc/vsftpd.conf | |
listen=YES | |
#listen_port=21 | |
anonymous_enable=NO | |
utf8_filesystem=YES | |
local_enable=YES | |
write_enable=YES | |
local_umask=000 | |
chroot_local_user=YES | |
allow_writeable_chroot=YES | |
hide_ids=YES | |
#virtual user settings | |
guest_enable=YES | |
virtual_use_local_privs=YES | |
pam_service_name=vsftpd | |
nopriv_user=vsftpd | |
guest_username=vsftpd | |
user_sub_token=$USER | |
local_root=/var/www/html/$USER | |
chmod_enable=no | |
#SSL settings | |
ssl_enable=YES | |
allow_anon_ssl=NO | |
force_local_data_ssl=NO | |
force_local_logins_ssl=NO | |
ssl_tlsv1=YES | |
ssl_sslv2=YES | |
ssl_sslv3=YES | |
rsa_cert_file=/etc/ssl/private/vsftpd.cert.pem | |
rsa_private_key_file=/etc/ssl/private/vsftpd.key.pem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment