filemailer-receiver/classes/config.py

44 lines
1.2 KiB
Python
Raw Normal View History

2022-03-30 11:04:59 +00:00
from configparser import ConfigParser
from socket import gethostname
from pathlib import Path
from hmac import compare_digest
2022-03-30 11:04:59 +00:00
import crypt
import re
2022-03-30 11:04:59 +00:00
class Config:
def __init__(self, path):
self.config = ConfigParser()
self.path = path
self.config.read(path)
self.hash_passwords()
def hash_passwords(self):
for user, password in self.config.items("USERS"):
2022-03-31 05:58:34 +00:00
if not re.match(r"\$\d\$", password):
self.config["USERS"][user] = crypt.crypt(
password, crypt.mksalt())
2022-03-30 11:04:59 +00:00
with open(self.path, "w") as configfile:
self.config.write(configfile)
def verify_password(self, user, password):
hashed = self.config["USERS"][user]
return compare_digest(hashed, crypt.crypt(password, hashed))
2022-03-30 11:04:59 +00:00
@property
def hostname(self):
return self.config.get("SERVER", "hostname", fallback=gethostname())
@property
def port(self):
return self.config.getint("SERVER", "port", fallback=8025)
@property
def maildir(self):
path = self.config.get("SERVER", "maildir", fallback="maildir")
Path(path).mkdir(parents=True, exist_ok=True)
return path