postgres fix
This commit is contained in:
@@ -2,27 +2,27 @@
|
||||
|
||||
import sys
|
||||
import os
|
||||
import shutil
|
||||
# import ssl
|
||||
|
||||
import re
|
||||
import logging
|
||||
|
||||
import psycopg2
|
||||
from psycopg2 import sql
|
||||
|
||||
from hashlib import md5
|
||||
from hashlib import sha256
|
||||
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 conn_db(cfg):
|
||||
return psycopg2.connect(dbname=cfg.dbname, user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport )
|
||||
class DummySha256Authorizer(DummyAuthorizer):
|
||||
def __init__(self, cfg):
|
||||
# Initialize the DummyAuthorizer and add the admin user
|
||||
super().__init__()
|
||||
@@ -30,17 +30,11 @@ class DummyMD5Authorizer(DummyAuthorizer):
|
||||
cfg.adminuser[0], cfg.adminuser[1], cfg.adminuser[2], perm=cfg.adminuser[3])
|
||||
|
||||
# Definisci la connessione al database
|
||||
conn = psycopg2.connect(
|
||||
dbname=cfg.dbname,
|
||||
user=cfg.dbuser,
|
||||
password=cfg.dbpass,
|
||||
host=cfg.dbhost,
|
||||
port=cfg.dbport
|
||||
)
|
||||
conn = conn_db(cfg)
|
||||
|
||||
# Crea un cursore
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT ftpuser, hash, virtpath, perm FROM virtusers")
|
||||
cur.execute(f'SELECT ftpuser, hash, virtpath, perm FROM {cfg.dbschema}.{cfg.dbtable}')
|
||||
|
||||
for ftpuser, hash, virtpath, perm in cur.fetchall():
|
||||
self.add_user(ftpuser, hash, virtpath, perm)
|
||||
@@ -51,7 +45,7 @@ class DummyMD5Authorizer(DummyAuthorizer):
|
||||
|
||||
def validate_authentication(self, username, password, handler):
|
||||
# Validate the user's password against the stored hash
|
||||
hash = md5(password.encode("UTF-8")).hexdigest()
|
||||
hash = sha256(password.encode("UTF-8")).hexdigest()
|
||||
try:
|
||||
if self.user_table[username]["pwd"] != hash:
|
||||
raise KeyError
|
||||
@@ -90,16 +84,9 @@ class ASEHandler(FTPHandler):
|
||||
if (fileExtension.upper() in (cfg.fileext)):
|
||||
with open(file, 'r') as csvfile:
|
||||
lines = csvfile.readlines()
|
||||
conn = psycopg2.connect(
|
||||
dbname=cfg.dbname,
|
||||
user=cfg.dbuser,
|
||||
password=cfg.dbpass,
|
||||
host=cfg.dbhost,
|
||||
port=cfg.dbport
|
||||
)
|
||||
conn = conn_db(cfg)
|
||||
|
||||
# Crea un cursore
|
||||
print(file, lines)
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.execute("INSERT INTO received (filename, content) VALUES (%s,%s)" , (filename, lines))
|
||||
@@ -125,7 +112,7 @@ class ASEHandler(FTPHandler):
|
||||
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
|
||||
hash = sha256(password.encode("UTF-8")).hexdigest() # Hash the password
|
||||
except:
|
||||
self.respond('501 SITE ADDU failed. Command needs 2 arguments')
|
||||
else:
|
||||
@@ -138,20 +125,14 @@ class ASEHandler(FTPHandler):
|
||||
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
|
||||
hash, cfg.virtpath + "/" + user, perm=cfg.defperm)
|
||||
# Save the user to the database
|
||||
# Definisci la connessione al database
|
||||
conn = psycopg2.connect(
|
||||
dbname=cfg.dbname,
|
||||
user=cfg.dbuser,
|
||||
password=cfg.dbpass,
|
||||
host=cfg.dbhost,
|
||||
port=cfg.dbport
|
||||
)
|
||||
conn = conn_db(cfg)
|
||||
|
||||
# Crea un cursore
|
||||
cur = conn.cursor()
|
||||
cur.execute("INSERT INTO virtusers (ftpuser, hash, virtpath, perm) VALUES (%s,%s,%s,%s)" , (user, hash, cfg.virtpath + user, 'elmw'))
|
||||
cur.execute(f"INSERT INTO {cfg.dbschema}.{cfg.dbtable} (ftpuser, hash, virtpath, perm) VALUES ('{user}', '{hash}', '{cfg.virtpath + user}', '{cfg.defperm}')")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
logging.info("User {} created.".format(user))
|
||||
@@ -169,26 +150,19 @@ class ASEHandler(FTPHandler):
|
||||
try:
|
||||
# Remove the user from the authorizer
|
||||
self.authorizer.remove_user(str(user))
|
||||
# Delete the user from the SQLite database
|
||||
|
||||
conn = psycopg2.connect(
|
||||
dbname=cfg.dbname,
|
||||
user=cfg.dbuser,
|
||||
password=cfg.dbpass,
|
||||
host=cfg.dbhost,
|
||||
port=cfg.dbport
|
||||
)
|
||||
# Delete the user from database
|
||||
conn = conn_db(cfg)
|
||||
|
||||
# Crea un cursore
|
||||
cur = conn.cursor()
|
||||
cur.execute("DELETE FROM virtusers WHERE ftpuser = %s", (user, ))
|
||||
cur.execute(f"DELETE FROM {cfg.dbschema}.{cfg.dbtable} WHERE ftpuser = '{user}'")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
logging.info("User {} deleted.".format(user))
|
||||
self.respond('200 SITE DELU successful.')
|
||||
|
||||
except:
|
||||
except Exception as e:
|
||||
self.respond('501 SITE DELU failed.')
|
||||
|
||||
def ftp_SITE_LSTU(self, line):
|
||||
@@ -199,18 +173,12 @@ class ASEHandler(FTPHandler):
|
||||
users_list = []
|
||||
try:
|
||||
# Connect to the SQLite database to fetch users
|
||||
conn = psycopg2.connect(
|
||||
dbname=cfg.dbname,
|
||||
user=cfg.dbuser,
|
||||
password=cfg.dbpass,
|
||||
host=cfg.dbhost,
|
||||
port=cfg.dbport
|
||||
)
|
||||
conn = conn_db(cfg)
|
||||
|
||||
# Crea un cursore
|
||||
cur = conn.cursor()
|
||||
self.push("214-The following virtual users are defined:\r\n")
|
||||
cur.execute("SELECT ftpuser, perm FROM virtusers")
|
||||
cur.execute(f'SELECT ftpuser, perm FROM {cfg.dbschema}.{cfg.dbtable}')
|
||||
[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.")
|
||||
@@ -224,7 +192,7 @@ def main():
|
||||
|
||||
try:
|
||||
# Initialize the authorizer and handler
|
||||
authorizer = DummyMD5Authorizer(cfg)
|
||||
authorizer = DummySha256Authorizer(cfg)
|
||||
handler = ASEHandler
|
||||
handler.cfg = cfg
|
||||
handler.authorizer = authorizer
|
||||
@@ -247,6 +215,7 @@ def main():
|
||||
logging.info(
|
||||
"Info: {}.".format("Shutdown requested...exiting")
|
||||
)
|
||||
|
||||
except Exception:
|
||||
print(
|
||||
"{} - PID {:>5} >> Error: {}.".format(
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
proxyAddr = 0.0.0.0
|
||||
portRangeWidth = 500
|
||||
virtpath = /home/alex/aseftp/
|
||||
adminuser = admin|c8cf955bd8b8a78419013b831e627eb2|/home/alex/aseftp/|elradfmwMT
|
||||
adminuser = admin|83e61ecb0e9871aff37a12491aa848f884f5657ddbfd46454878e28afbecfc20|/home/alex/aseftp/|elradfmwMT
|
||||
servertype = FTPHandler
|
||||
certfile = /home/alex/aseftp/keycert.pem
|
||||
fileext = .CSV|.TXT
|
||||
defaultUserPerm = elmw
|
||||
#servertype = FTPHandler/TLS_FTPHandler
|
||||
|
||||
[csvfs]
|
||||
@@ -24,4 +25,6 @@
|
||||
user = asepg
|
||||
password = batt1l0
|
||||
dbName = asedb
|
||||
dbSchema = public
|
||||
tableName = virtusers
|
||||
defaultPerm = elmw
|
||||
@@ -19,6 +19,7 @@ class config:
|
||||
self.servertype = c.get("ftpserver", "servertype")
|
||||
self.certfile = c.get("ftpserver", "certfile")
|
||||
self.fileext = c.get("ftpserver", "fileext").upper().split("|")
|
||||
self.defperm = c.get("ftpserver", "defaultUserPerm")
|
||||
|
||||
# CSV FILE setting
|
||||
self.csvfs = c.get("csvfs", "path")
|
||||
@@ -32,4 +33,5 @@ class config:
|
||||
self.dbuser = c.get("db", "user")
|
||||
self.dbpass = c.get("db", "password")
|
||||
self.dbname = c.get("db", "dbName")
|
||||
self.dbschema = c.get("db", "dbSchema")
|
||||
self.dbtable = c.get("db", "tableName")
|
||||
|
||||
Reference in New Issue
Block a user