#!/usr/bin/env python3 import sys import os import shutil # import ssl import re import logging import sqlite3 from hashlib import md5 from pathlib import Path from datetime import datetime from utils.time import timestamp_fmt as ts from utils.time import date_refmt as df from utils.config import set_config as setting from pyftpdlib.handlers import FTPHandler, TLS_FTPHandler from pyftpdlib.servers import FTPServer from pyftpdlib.authorizers import DummyAuthorizer, AuthenticationFailed class DummyMD5Authorizer(DummyAuthorizer): def __init__(self, cfg): # Initialize the DummyAuthorizer and add the admin user super().__init__() self.add_user( cfg.adminuser[0], cfg.adminuser[1], cfg.adminuser[2], perm=cfg.adminuser[3]) # Connect to the SQLite database for virtual users con = sqlite3.connect(cfg.virtusersdb) cur = con.cursor() # Create the virtusers table if it doesn't exist cur.execute( '''CREATE TABLE IF NOT EXISTS virtusers (user text, hash text, virtpath text, perm text)''') # Create an index on the user column for faster lookups cur.execute( '''CREATE INDEX IF NOT EXISTS user_idx on virtusers(user)''') # Load existing virtual users from the database for row in cur.execute('SELECT * FROM virtusers'): self.add_user(row[0], row[1], row[2], perm=row[3]) con.close() def validate_authentication(self, username, password, handler): # Validate the user's password against the stored hash hash = md5(password.encode("UTF-8")).hexdigest() try: if self.user_table[username]["pwd"] != hash: raise KeyError except KeyError: raise AuthenticationFailed class ASEHandler(FTPHandler): def __init__(self, conn, server, ioloop=None): # Initialize the FTPHandler and add custom commands super().__init__(conn, server, ioloop) self.proto_cmds = FTPHandler.proto_cmds.copy() # Add custom FTP commands for managing virtual users - command in lowercase self.proto_cmds.update( {'SITE ADDU': dict(perm='M', auth=True, arg=True, help='Syntax: SITE ADDU USERNAME PASSWORD (add virtual user).')} ) self.proto_cmds.update( {'SITE DELU': dict(perm='M', auth=True, arg=True, help='Syntax: SITE DELU USERNAME (remove virtual user).')} ) self.proto_cmds.update( {'SITE LSTU': dict(perm='M', auth=True, arg=None, help='Syntax: SITE LSTU (list virtual users).')} ) def on_file_received(self, file): # Handle the event when a file is received unitType = "" unitName = "" toolName = "" toolType = "" fileDate = "" fileTime = "" queue = "" # Check if the file is empty and remove it if so if not os.stat(file).st_size: os.remove(file) logging.info( "File {} was empty: removed.".format(file)) else: cfg = self.cfg path, filenameExt = os.path.split(file) filename, fileExtension = os.path.splitext(filenameExt) # Process the file if it has a valid extension if (fileExtension.upper() in (cfg.fileext)): # Match the filename against a specific pattern if m := re.match( r"^(G\d\d\d|GFLOW)_(ID\d\d\d\d)_(DT\d\d\d\d)_(\d\d)(\d\d)(\d\d\d\d|\d\d)(\d\d)(\d\d)(\d\d)$", filename, re.I, ): unitType = m.group(1).upper() unitName = m.group(2).upper() toolName = m.group(3).upper() toolType = "N/A" fileDate = m.group(6) + "/" + m.group(5) + "/" + m.group(4) fileTime = m.group(7) + ":" + m.group(8) + ":" + m.group(9) # Match against another pattern for different file formats elif re.match( r"^(\d\d_\d\d\d\d|)(DT\d\d\d\d|LOC\d\d\d\d|GD\d\d\d\d)$", filename, re.I ): with open(file, "r") as fileCsv: try: # Read the CSV file line by line for i, line in enumerate(fileCsv.readlines(4096), 1): # Extract creation date and time if m1 := re.match( r"^(File Creation Date:\s)?(\d*\/\d*\/\d*)\s(\d*:\d*:\d*)\;*\n?$", line, re.I, ): fileDate = m1.group(2) fileTime = m1.group(3) # Extract unit type and name elif m2 := re.match( r"^(\w+\d+)\s(\w+\d+)\;*\n?$", line, re.I, ): unitType = m2.group(1).upper() unitName = m2.group(2).upper() # Extract tool type and name from the path elif m3 := re.match( r"^SD path: a:\/\w+\/(\w+)(?:\.\w+)?\/*(\w*)(?:\.\w+)?\;*\n?$", line, re.I, ): if m3.group(2): toolType = m3.group(1).upper() toolName = m3.group(2).upper() else: toolType = "".join( re.findall( "^[a-zA-Z]+", m3.group(1)) ).upper() toolName = m3.group(1).upper() break except: logging.error( "Error: {}.".format(sys.exc_info()[1])) fileCsv.close # Log the extracted information logging.info( "{} - {} - {} - {} - {} {}.".format( unitType, unitName, toolName, toolType, df.dateFmt(fileDate), fileTime, ) ) # Prepare the new path for the received file newPath = cfg.csvfs + "/" + self.username + "/received/" + \ unitName.upper() + "/" newFilename = ( newPath + filename + "_" + str(ts.timestamp("tms") + fileExtension) ) fileRenamed = file + "_" + str(ts.timestamp("tms")) # Rename the original file to avoid conflicts os.rename(file, fileRenamed) try: # Create the new directory for the user if it doesn't exist os.makedirs(newPath) logging.info("Path {} created.".format(newPath)) except FileExistsError: logging.info("Path {} already exists.".format(newPath)) try: # Move the renamed file to the new location shutil.move(fileRenamed, newFilename) logging.info("{} moved into {}.".format( filenameExt, newFilename)) except OSError: logging.error("Error to move {} into {}.".format( filenameExt, newFilename)) now = datetime.now() def on_incomplete_file_received(self, file): # Remove partially uploaded files 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. """ cfg = self.cfg parms = line.split() user = os.path.basename(parms[0]) # Extract the username password = parms[1] # Get the password hash = md5(password.encode("UTF-8")).hexdigest() # Hash the password 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.') else: try: # Add the user to the authorizer self.authorizer.add_user(str(user), hash, cfg.virtpath + "/" + user, perm="lmw") # Save the user to the SQLite database con = sqlite3.connect(cfg.virtusersdb) cur = con.cursor() cur.execute("INSERT INTO virtusers VALUES (?,?,?,?)", (user, hash, cfg.virtpath + user, 'elmw')) con.commit() con.close() logging.info("User {} created.".format(user)) self.respond('200 SITE ADDU successful.') except: self.respond('501 SITE ADDU failed.') def ftp_SITE_DELU(self, line): """ Remove a virtual user and save the virtuser configuration file. """ cfg = self.cfg parms = line.split() user = os.path.basename(parms[0]) # Extract the username try: # Remove the user from the authorizer self.authorizer.remove_user(str(user)) # Delete the user from the SQLite database con = sqlite3.connect(cfg.virtusersdb) cur = con.cursor() cur.execute("DELETE FROM virtusers WHERE user = ?", (user,)) con.commit() con.close() logging.info("User {} deleted.".format(user)) self.respond('200 SITE DELU successful.') except: self.respond('501 SITE DELU failed.') def ftp_SITE_LSTU(self, line): """ List all virtual users. """ cfg = self.cfg users_list = [] try: # Connect to the SQLite database to fetch users con = sqlite3.connect(cfg.virtusersdb) cur = con.cursor() self.push("214-The following virtual users are defined:\r\n") # Fetch and list all virtual users for row in cur.execute("SELECT * FROM virtusers").fetchall(): users_list.append( " Username: " + row[0] + "\tPerms: " + row[3] + "\r\n") con.close() self.push(''.join(users_list)) self.respond("214 LSTU SITE command successful.") except: self.respond('501 list users failed.') def main(): # Load the configuration settings cfg = setting.config() try: # Initialize the authorizer and handler authorizer = DummyMD5Authorizer(cfg) handler = ASEHandler handler.cfg = cfg handler.authorizer = authorizer handler.masquerade_address = cfg.proxyaddr # Set the range of passive ports for the FTP server _range = list(range(cfg.firstport, cfg.firstport + cfg.portrangewidth)) handler.passive_ports = _range # Configure logging logging.basicConfig( format="%(asctime)s %(message)s", filename=cfg.logfilename, level=logging.INFO, ) # Create and start the FTP server server = FTPServer(("0.0.0.0", 2121), handler) server.serve_forever() except KeyboardInterrupt: logging.info( "Info: {}.".format("Shutdown requested...exiting") ) except Exception: print( "{} - PID {:>5} >> Error: {}.".format( ts.timestamp("log"), os.getpid(), sys.exc_info()[1] ) ) if __name__ == "__main__": main()