Skip to content

Instantly share code, notes, and snippets.

@leVirve
Last active November 23, 2021 09:34
Show Gist options
  • Save leVirve/9e87173fd5167b2ff8feb754673de59a to your computer and use it in GitHub Desktop.
Save leVirve/9e87173fd5167b2ff8feb754673de59a to your computer and use it in GitHub Desktop.
A Python script to add/remove users for FileZilla Server.
import hashlib
import os
import subprocess
import xml.etree.ElementTree
import click
user_xml_fmt = '''
<User Name="ddd_{username}">
<Option Name="Pass">{md5_pwd}</Option>
<Option Name="Group" />
<Option Name="Bypass server userlimit">0</Option>
<Option Name="User Limit">0</Option>
<Option Name="IP Limit">0</Option>
<Option Name="Enabled">1</Option>
<Option Name="Comments" />
<Option Name="ForceSsl">0</Option>
<Option Name="8plus3">0</Option>
<IpFilter>
<Disallowed />
<Allowed />
</IpFilter>
<Permissions>
<Permission Dir="H:\Drowsiness Dataset">
<Option Name="FileRead">1</Option>
<Option Name="FileWrite">0</Option>
<Option Name="FileDelete">0</Option>
<Option Name="FileAppend">0</Option>
<Option Name="DirCreate">0</Option>
<Option Name="DirDelete">0</Option>
<Option Name="DirList">1</Option>
<Option Name="DirSubdirs">1</Option>
<Option Name="IsHome">1</Option>
<Option Name="AutoCreate">0</Option>
</Permission>
</Permissions>
<SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="0" UlType="0" UlLimit="10" ServerUlLimitBypass="0">
<Download />
<Upload />
</SpeedLimits>
</User>
'''
folder = 'C:/Program Files (x86)/FileZilla Server'
xml_path = os.path.join(folder, 'FileZilla Server.xml')
exe_path = os.path.join(folder, 'FileZilla Server.exe')
class DDDManager():
def __init__(self, filename):
self.filename = filename
def setup(self):
self.tree = xml.etree.ElementTree.parse(self.filename)
self.root = self.tree.getroot()
self.user_tree = self.root.findall('Users')[0]
return self
def new_user(self, username, pwd=None):
pwd = pwd or username
md5_pwd = hashlib.md5(pwd.encode('utf-8')).hexdigest()
xml_str = user_xml_fmt.format(username=username, md5_pwd=md5_pwd)
return xml.etree.ElementTree.fromstring(xml_str)
def add_user(self, *arg):
user = self.new_user(*arg)
self.user_tree.append(user)
def remove_user(self, username):
for user in self.user_tree:
if user.get('Name') == username:
self.user_tree.remove(user)
def dump(self):
self.tree.write(self.filename)
@click.command()
@click.argument('mode')
@click.argument('username')
def main(mode, username):
manager = DDDManager(xml_path).setup()
if mode == 'add':
user_info = (username,)
manager.add_user(*user_info)
elif mode == 'rm':
manager.remove_user(username)
manager.dump()
subprocess.run([exe_path, '/reload-config'], shell=True)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment