This commit is contained in:
2025-04-27 17:21:36 +02:00
parent dc6ccc1744
commit 40ef173694
15 changed files with 309 additions and 80 deletions

View File

@@ -13,8 +13,9 @@ import mysql.connector
from hashlib import sha256
from pathlib import Path
from utils.time import timestamp_fmt as ts
from utils.config import set_config as setting
from utils.datefmt import date_check as date_check
from utils.db import connect_db as connect_db
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
@@ -30,7 +31,17 @@ def conn_db(cfg):
Returns:
A MySQL database connection object.
"""
return mysql.connector.connect(user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport)
try:
conn = mysql.connector.connect(user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport)
conn.autocommit = True
return conn
except mysql.connector.Error as e:
print(f"Error: {e} - Error: {e.errno}")
print (f"Error code: {e.errno}") # error number
print (f"SQLSTATE value: {e.sqlstate}") # SQLSTATE value
print (f"Error message: {e.msg}")
logging.error(f'{e}')
exit(e.errno)
def extract_value(patterns, primary_source, secondary_source, default='Not Defined'):
@@ -62,6 +73,7 @@ class DummySha256Authorizer(DummyAuthorizer):
# Define the database connection
conn = conn_db(cfg)
# Create a cursor
cur = conn.cursor()
cur.execute(f'SELECT ftpuser, hash, virtpath, perm FROM {cfg.dbname}.{cfg.dbusertable} WHERE disabled_at IS NULL')
@@ -138,7 +150,11 @@ class ASEHandler(FTPHandler):
tool_name = extract_value(cfg.tools_name, filename, str(lines[0:9]))
tool_type = extract_value(cfg.tools_type, filename, str(lines[0:9]))
conn = conn_db(cfg)
try:
conn = conn_db(cfg)
except mysql.connector.Error as e:
print(f"Error: {e}")
logging.error(f'{e}')
# Create a cursor
cur = conn.cursor()
@@ -185,7 +201,11 @@ class ASEHandler(FTPHandler):
hash, cfg.virtpath + "/" + user, perm=cfg.defperm)
# Save the user to the database
# Define the database connection
conn = conn_db(cfg)
try:
conn = conn_db(cfg)
except mysql.connector.Error as e:
print(f"Error: {e}")
logging.error(f'{e}')
# Create a cursor
cur = conn.cursor()
@@ -207,7 +227,11 @@ class ASEHandler(FTPHandler):
# Remove the user from the authorizer
self.authorizer.remove_user(str(user))
# Delete the user from database
conn = conn_db(cfg)
try:
conn = conn_db(cfg)
except mysql.connector.Error as e:
print(f"Error: {e}")
logging.error(f'{e}')
# Crea un cursore
cur = conn.cursor()
@@ -228,7 +252,11 @@ class ASEHandler(FTPHandler):
user = os.path.basename(parms[0]) # Extract the username
try:
# Restore the user into database
conn = conn_db(cfg)
try:
conn = conn_db(cfg)
except mysql.connector.Error as e:
print(f"Error: {e}")
logging.error(f'{e}')
# Crea un cursore
cur = conn.cursor()
@@ -262,7 +290,11 @@ class ASEHandler(FTPHandler):
users_list = []
try:
# Connect to the SQLite database to fetch users
conn = conn_db(cfg)
try:
conn = conn_db(cfg)
except mysql.connector.Error as e:
print(f"Error: {e}")
logging.error(f'{e}')
# Crea un cursore
cur = conn.cursor()