#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import utils def setup(interactive=True): dir_path = utils.get_dir(__file__) pwd_path = '/etc/pure-ftpd/pureftpd.passwd' # Get passwords ftpincoming_users = utils.get_conf('FTP_INCOMING_USERS', '').strip(',').split(',') if not ftpincoming_users: raise Exception('No users specified in the configuration FTP_INCOMING_USERS variable.') for user in ftpincoming_users: if ':' not in user or user.count(':') > 1: raise Exception('Invalid user/pass definition, separator not present or too many detected') # Run commands cmds = [ 'apt-get install --yes pure-ftpd python3-unidecode', dict(line='adduser --disabled-login --gecos "" --shell /bin/false ftp', cond='id ftp', cond_neg=True, cond_skip=True), 'cp "%s/create_ftp_account.sh" /usr/local/bin' % (dir_path), 'mkdir -p /home/ftp/storage', 'mkdir -p /home/ftp/storage/incoming', 'mkdir -p /home/ftp/storage/hotfolder', 'chmod -R 775 /home/ftp/storage/incoming', 'chmod -R 775 /home/ftp/storage/hotfolder', # Config 'echo "no" > /etc/pure-ftpd/conf/AllowDotFiles', 'echo "yes" > /etc/pure-ftpd/conf/CallUploadScript', 'echo "yes" > /etc/pure-ftpd/conf/ChrootEveryone', 'echo "yes" > /etc/pure-ftpd/conf/DontResolve', 'echo "no" > /etc/pure-ftpd/conf/PAMAuthentication', # Post upload script 'cp "%s/on_ftp_upload.py" /home/ftp/on_ftp_upload.py' % dir_path, 'chown ftp:ftp /home/ftp/on_ftp_upload.py', 'chmod +x /home/ftp/on_ftp_upload.py', 'pure-uploadscript -p /home/ftp/.on_upload.pid -B -g $(id -g ftp) -r /home/ftp/on_ftp_upload.py -u $(id -u ftp)', 'cp "%s/pure-ftpd-common" /etc/default/pure-ftpd-common.tmp' % dir_path, 'sed "s/UPLOADUID=UID/UPLOADUID=$(id -u ftp)/g" /etc/default/pure-ftpd-common.tmp > /etc/default/pure-ftpd-common.tmp2', 'mv -f /etc/default/pure-ftpd-common.tmp2 /etc/default/pure-ftpd-common.tmp', 'sed "s/UPLOADGID=GID/UPLOADGID=$(id -g ftp)/g" /etc/default/pure-ftpd-common.tmp > /etc/default/pure-ftpd-common.tmp2', 'mv -f /etc/default/pure-ftpd-common.tmp2 /etc/default/pure-ftpd-common', 'rm -f /etc/default/pure-ftpd-common.tmp', 'cp "%s/remove_empty_dirs.py" /etc/cron.hourly/remove_empty_dirs.py' % dir_path, # Create FTP accounts '([ -f "%s" ] || [ -f "%s" ] && cp "%s" "%s") || true' % (pwd_path + '.back', pwd_path, pwd_path, pwd_path + '.back'), '([ -f "%s" ] && mv -f "%s" pureftpd.passwd.tmp) || true' % (pwd_path, pwd_path), ] for ftpuser in ftpincoming_users: login, password = ftpuser.split(':') cmds.extend([ 'mkdir -p /home/ftp/storage/incoming/%s' % login, '"%s/create_ftp_account.sh" %s "%s" /home/ftp/storage/incoming/%s' % (dir_path, login, password, login), ]) cmds.extend([ 'chmod -R 775 /home/ftp/storage/incoming', 'chown -R ftp:ftp /home/ftp/storage', 'rm -f pureftpd.passwd.tmp', 'pure-pw mkdb', 'ln -sfn /etc/pure-ftpd/conf/PureDB /etc/pure-ftpd/auth/50puredb', '/etc/init.d/pure-ftpd force-reload', ]) try: utils.run_commands(cmds) except Exception: raise finally: # Restore password conf if required if os.path.exists('pureftpd.passwd.tmp'): os.rename('pureftpd.passwd.tmp', pwd_path)