This commit is contained in:
2025-04-26 16:39:39 +02:00
parent ddd45d7276
commit dc6ccc1744
9 changed files with 802 additions and 106 deletions

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python3
#!.venv/bin/python
"""This module implements an FTP server with custom commands for managing virtual users and handling CSV file uploads."""
import sys
import os
@@ -7,7 +8,7 @@ import os
import re
import logging
import psycopg2
import mysql.connector
from hashlib import sha256
from pathlib import Path
@@ -15,16 +16,25 @@ from pathlib import Path
from utils.time import timestamp_fmt as ts
from utils.config import set_config as setting
from pyftpdlib.handlers import FTPHandler, TLS_FTPHandler
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from pyftpdlib.authorizers import DummyAuthorizer, AuthenticationFailed
def conn_db(cfg):
return psycopg2.connect(dbname=cfg.dbname, user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport )
"""Establishes a connection to the MySQL database.
Args:
cfg: The configuration object containing database connection details.
Returns:
A MySQL database connection object.
"""
return mysql.connector.connect(user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport)
def extract_value(patterns, primary_source, secondary_source, default='Not Defined'):
"""
Extracts the first match for a list of patterns from the primary source.
"""Extracts the first match for a list of patterns from the primary source.
Falls back to the secondary source if no match is found.
"""
for source in (primary_source, secondary_source):
@@ -35,26 +45,36 @@ def extract_value(patterns, primary_source, secondary_source, default='Not Defin
return default # Return default if no matches are found
class DummySha256Authorizer(DummyAuthorizer):
"""Custom authorizer that uses SHA256 for password hashing and manages users from a database."""
def __init__(self, cfg):
# Initialize the DummyAuthorizer and add the admin user
"""Initializes the authorizer, adds the admin user, and loads users from the database.
Args:
cfg: The configuration object.
"""
super().__init__()
self.add_user(
cfg.adminuser[0], cfg.adminuser[1], cfg.adminuser[2], perm=cfg.adminuser[3])
# Definisci la connessione al database
# Define the database connection
conn = conn_db(cfg)
# Crea un cursore
# Create a cursor
cur = conn.cursor()
cur.execute(f'SELECT ftpuser, hash, virtpath, perm FROM {cfg.dbschema}.{cfg.dbusertable} WHERE deleted_at IS NULL')
cur.execute(f'SELECT ftpuser, hash, virtpath, perm FROM {cfg.dbname}.{cfg.dbusertable} WHERE disabled_at IS NULL')
for ftpuser, hash, virtpath, perm in cur.fetchall():
self.add_user(ftpuser, hash, virtpath, perm)
"""
Create the user's directory if it does not exist.
"""
try:
Path(cfg.virtpath + ftpuser).mkdir(parents=True, exist_ok=True)
except:
self.responde('551 Error in create virtual user path.')
except Exception as e:
self.responde(f'551 Error in create virtual user path: {e}')
def validate_authentication(self, username, password, handler):
# Validate the user's password against the stored hash
@@ -66,9 +86,16 @@ class DummySha256Authorizer(DummyAuthorizer):
raise AuthenticationFailed
class ASEHandler(FTPHandler):
"""Custom FTP handler that extends FTPHandler with custom commands and file handling."""
def __init__(self, conn, server, ioloop=None):
# Initialize the FTPHandler and add custom commands
"""Initializes the handler, adds custom commands, and sets up command permissions.
Args:
conn: The connection object.
server: The FTP server object.
ioloop: The I/O loop object.
"""
super().__init__(conn, server, ioloop)
self.proto_cmds = FTPHandler.proto_cmds.copy()
# Add custom FTP commands for managing virtual users - command in lowercase
@@ -77,12 +104,12 @@ class ASEHandler(FTPHandler):
help='Syntax: SITE <SP> ADDU USERNAME PASSWORD (add virtual user).')}
)
self.proto_cmds.update(
{'SITE DELU': dict(perm='M', auth=True, arg=True,
help='Syntax: SITE <SP> DELU USERNAME (remove virtual user).')}
{'SITE DISU': dict(perm='M', auth=True, arg=True,
help='Syntax: SITE <SP> DISU USERNAME (disable virtual user).')}
)
self.proto_cmds.update(
{'SITE RESU': dict(perm='M', auth=True, arg=True,
help='Syntax: SITE <SP> RESU USERNAME (restore virtual user).')}
{'SITE ENAU': dict(perm='M', auth=True, arg=True,
help='Syntax: SITE <SP> ENAU USERNAME (enable virtual user).')}
)
self.proto_cmds.update(
{'SITE LSTU': dict(perm='M', auth=True, arg=None,
@@ -90,6 +117,11 @@ class ASEHandler(FTPHandler):
)
def on_file_received(self, file):
"""Handles the event when a file is successfully received.
Args:
file: The path to the received file.
"""
if not os.stat(file).st_size:
os.remove(file)
logging.info(f'File {file} was empty: removed.')
@@ -108,14 +140,14 @@ class ASEHandler(FTPHandler):
conn = conn_db(cfg)
# Crea un cursore
# Create a cursor
cur = conn.cursor()
try:
cur.execute(f"INSERT INTO {cfg.dbschema}.{cfg.dbrectable } (filename, unit_name, unit_type, tool_name, tool_type, tool_data) VALUES (%s, %s, %s, %s, %s, %s)", (filename, unit_name.upper(), unit_type.upper(), tool_name.upper(), tool_type.upper(), lines))
cur.execute(f"INSERT INTO {cfg.dbname}.{cfg.dbrectable} (filename, unit_name, unit_type, tool_name, tool_type, tool_data) VALUES (%s, %s, %s, %s, %s, %s)", (filename, unit_name.upper(), unit_type.upper(), tool_name.upper(), tool_type.upper(), ''.join(lines)))
conn.commit()
conn.close()
except psycopg2.Error as e:
except Exception as e:
logging.error(f'File {file} not loaded. Held in user path.')
logging.error(f'{e}')
else:
@@ -123,13 +155,14 @@ class ASEHandler(FTPHandler):
logging.info(f'File {file} loaded: removed.')
def on_incomplete_file_received(self, file):
# Remove partially uploaded files
"""Removes partially uploaded files.
Args:
file: The path to the incomplete file.
"""
os.remove(file)
def ftp_SITE_ADDU(self, line):
"""
Add a virtual user and save the virtuser configuration file.
Create a directory for the virtual user in the specified virtpath.
"""Adds a virtual user, creates their directory, and saves their details to the database.
"""
cfg = self.cfg
try:
@@ -137,38 +170,36 @@ class ASEHandler(FTPHandler):
user = os.path.basename(parms[0]) # Extract the username
password = parms[1] # Get the password
hash = sha256(password.encode("UTF-8")).hexdigest() # Hash the password
except:
except IndexError:
self.respond('501 SITE ADDU failed. Command needs 2 arguments')
else:
try:
# Create the user's directory
Path(cfg.virtpath + user).mkdir(parents=True, exist_ok=True)
except:
self.respond('551 Error in create virtual user path.')
except Exception as e:
self.respond(f'551 Error in create virtual user path: {e}')
else:
try:
# Add the user to the authorizer
self.authorizer.add_user(str(user),
hash, cfg.virtpath + "/" + user, perm=cfg.defperm)
# Save the user to the database
# Definisci la connessione al database
# Define the database connection
conn = conn_db(cfg)
# Crea un cursore
# Create a cursor
cur = conn.cursor()
cur.execute(f"INSERT INTO {cfg.dbschema}.{cfg.dbusertable} (ftpuser, hash, virtpath, perm) VALUES ('{user}', '{hash}', '{cfg.virtpath + user}', '{cfg.defperm}')")
cur.execute(f"INSERT INTO {cfg.dbname}.{cfg.dbusertable} (ftpuser, hash, virtpath, perm) VALUES ('{user}', '{hash}', '{cfg.virtpath + user}', '{cfg.defperm}')")
conn.commit()
conn.close()
logging.info("User {} created.".format(user))
logging.info(f"User {user} created.")
self.respond('200 SITE ADDU successful.')
except psycopg2.Error as e:
self.respond('501 SITE ADDU failed.')
except Exception as e:
self.respond(f'501 SITE ADDU failed: {e}.')
print(e)
def ftp_SITE_DELU(self, line):
"""
Remove a virtual user and save the virtuser configuration file.
"""
def ftp_SITE_DISU(self, line):
"""Removes a virtual user from the authorizer and marks them as deleted in the database."""
cfg = self.cfg
parms = line.split()
user = os.path.basename(parms[0]) # Extract the username
@@ -180,20 +211,18 @@ class ASEHandler(FTPHandler):
# Crea un cursore
cur = conn.cursor()
cur.execute(f"UPDATE {cfg.dbschema}.{cfg.dbusertable} SET deleted_at = now() WHERE ftpuser = '{user}'")
cur.execute(f"UPDATE {cfg.dbname}.{cfg.dbusertable} SET disabled_at = now() WHERE ftpuser = '{user}'")
conn.commit()
conn.close()
logging.info("User {} deleted.".format(user))
self.respond('200 SITE DELU successful.')
except psycopg2.Error as e:
self.respond('501 SITE DELU failed.')
logging.info(f"User {user} deleted.")
self.respond('200 SITE DISU successful.')
except Exception as e:
self.respond('501 SITE DISU failed.')
print(e)
def ftp_SITE_RESU(self, line):
"""
Restore a virtual user and save the virtuser configuration file.
"""
def ftp_SITE_ENAU(self, line):
"""Restores a virtual user by updating their status in the database and adding them back to the authorizer."""
cfg = self.cfg
parms = line.split()
user = os.path.basename(parms[0]) # Extract the username
@@ -204,33 +233,31 @@ class ASEHandler(FTPHandler):
# Crea un cursore
cur = conn.cursor()
try:
cur.execute(f"UPDATE {cfg.dbschema}.{cfg.dbusertable} SET deleted_at = null WHERE ftpuser = '{user}'")
cur.execute(f"UPDATE {cfg.dbname}.{cfg.dbusertable} SET disabled_at = null WHERE ftpuser = '{user}'")
conn.commit()
except psycopg2.Error as e:
logging.error("Update DB failed: {}".format(e))
except Exception as e:
logging.error(f"Update DB failed: {e}")
cur.execute(f"SELECT ftpuser, hash, virtpath, perm FROM {cfg.dbschema}.{cfg.dbusertable} WHERE ftpuser = '{user}'")
cur.execute(f"SELECT ftpuser, hash, virtpath, perm FROM {cfg.dbname}.{cfg.dbusertable} WHERE ftpuser = '{user}'")
ftpuser, hash, virtpath, perm = cur.fetchone()
self.authorizer.add_user(ftpuser, hash, virtpath, perm)
try:
Path(cfg.virtpath + ftpuser).mkdir(parents=True, exist_ok=True)
except:
self.responde('551 Error in create virtual user path.')
except Exception as e:
self.responde(f'551 Error in create virtual user path: {e}')
conn.close()
logging.info("User {} restored.".format(user))
self.respond('200 SITE RESU successful.')
logging.info(f"User {user} restored.")
self.respond('200 SITE ENAU successful.')
except Exception as e:
self.respond('501 SITE RESU failed.')
self.respond('501 SITE ENAU failed.')
print(e)
def ftp_SITE_LSTU(self, line):
"""
List all virtual users.
"""
"""Lists all virtual users from the database."""
cfg = self.cfg
users_list = []
try:
@@ -240,15 +267,16 @@ class ASEHandler(FTPHandler):
# Crea un cursore
cur = conn.cursor()
self.push("214-The following virtual users are defined:\r\n")
cur.execute(f'SELECT ftpuser, perm FROM {cfg.dbschema}.{cfg.dbusertable} WHERE deleted_at IS NULL ')
cur.execute(f'SELECT ftpuser, perm FROM {cfg.dbname}.{cfg.dbusertable} WHERE disabled_at IS NULL ')
[users_list.append(f'Username: {ftpuser}\tPerms: {perm}\r\n') for ftpuser, perm in cur.fetchall()]
self.push(''.join(users_list))
self.respond("214 LSTU SITE command successful.")
except:
self.respond('501 list users failed.')
except Exception as e:
self.respond(f'501 list users failed: {e}')
def main():
"""Main function to start the FTP server."""
# Load the configuration settings
cfg = setting.config()
@@ -275,15 +303,14 @@ def main():
server.serve_forever()
except KeyboardInterrupt:
logging.info(
"Info: {}.".format("Shutdown requested...exiting")
"Info: Shutdown requested...exiting"
)
except Exception:
print(
"{} - PID {:>5} >> Error: {}.".format(
ts.timestamp("log"), os.getpid(), sys.exc_info()[1]
)
f"{ts.timestamp("log")} - PID {os.getpid():>5} >> Error: {sys.exc_info()[1]}."
)
if __name__ == "__main__":
main()