mirror of
https://github.com/chatmail/relay.git
synced 2026-05-10 16:04:37 +00:00
streamline: make Config determine uid/gid/maildir of a user
This commit is contained in:
@@ -43,6 +43,13 @@ class Config:
|
|||||||
return res
|
return res
|
||||||
raise ValueError(f"invalid address {addr!r}")
|
raise ValueError(f"invalid address {addr!r}")
|
||||||
|
|
||||||
|
def get_user_dict(self, addr, enc_password=None):
|
||||||
|
home = self.get_user_maildir(addr)
|
||||||
|
res = dict(home=str(home), uid="vmail", gid="vmail")
|
||||||
|
if enc_password is not None:
|
||||||
|
res["password"] = enc_password
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
def write_initial_config(inipath, mail_domain, overrides):
|
def write_initial_config(inipath, mail_domain, overrides):
|
||||||
"""Write out default config file, using the specified config value overrides."""
|
"""Write out default config file, using the specified config value overrides."""
|
||||||
|
|||||||
@@ -62,11 +62,7 @@ def is_allowed_to_create(config: Config, user, cleartext_password) -> bool:
|
|||||||
|
|
||||||
def get_user_data(db, config: Config, user, conn=None):
|
def get_user_data(db, config: Config, user, conn=None):
|
||||||
if user == f"echo@{config.mail_domain}":
|
if user == f"echo@{config.mail_domain}":
|
||||||
return dict(
|
return config.get_user_dict(user)
|
||||||
home=str(config.get_user_maildir(user)),
|
|
||||||
uid="vmail",
|
|
||||||
gid="vmail",
|
|
||||||
)
|
|
||||||
|
|
||||||
if conn is None:
|
if conn is None:
|
||||||
with db.read_connection() as conn:
|
with db.read_connection() as conn:
|
||||||
@@ -75,9 +71,7 @@ def get_user_data(db, config: Config, user, conn=None):
|
|||||||
result = conn.get_user(user)
|
result = conn.get_user(user)
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
result["home"] = str(config.get_user_maildir(user))
|
result.update(config.get_user_dict(user))
|
||||||
result["uid"] = "vmail"
|
|
||||||
result["gid"] = "vmail"
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@@ -94,12 +88,7 @@ def lookup_passdb(db, config: Config, user, cleartext_password):
|
|||||||
logging.exception("Exception when trying to read /run/echobot/password")
|
logging.exception("Exception when trying to read /run/echobot/password")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return dict(
|
return config.get_user_dict(user, enc_password=encrypt_password(password))
|
||||||
home=str(config.get_user_maildir(user)),
|
|
||||||
uid="vmail",
|
|
||||||
gid="vmail",
|
|
||||||
password=encrypt_password(password),
|
|
||||||
)
|
|
||||||
|
|
||||||
userdata = get_user_data(db, config, user)
|
userdata = get_user_data(db, config, user)
|
||||||
if userdata:
|
if userdata:
|
||||||
@@ -114,16 +103,11 @@ def lookup_passdb(db, config: Config, user, cleartext_password):
|
|||||||
if userdata:
|
if userdata:
|
||||||
return userdata
|
return userdata
|
||||||
|
|
||||||
encrypted_password = encrypt_password(cleartext_password)
|
enc_password = encrypt_password(cleartext_password)
|
||||||
q = "INSERT INTO users (addr, password) VALUES (?, ?)"
|
q = "INSERT INTO users (addr, password) VALUES (?, ?)"
|
||||||
conn.execute(q, (user, encrypted_password))
|
conn.execute(q, (user, enc_password))
|
||||||
print(f"Created address: {user}", file=sys.stderr)
|
print(f"Created address: {user}", file=sys.stderr)
|
||||||
return dict(
|
return config.get_user_dict(user, enc_password=enc_password)
|
||||||
home=str(config.get_user_maildir(user)),
|
|
||||||
uid="vmail",
|
|
||||||
gid="vmail",
|
|
||||||
password=encrypted_password,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def iter_userdb(db) -> list:
|
def iter_userdb(db) -> list:
|
||||||
|
|||||||
@@ -59,3 +59,20 @@ def test_config_userstate_paths(make_config, tmp_path):
|
|||||||
|
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
config.get_user_maildir(".")
|
config.get_user_maildir(".")
|
||||||
|
|
||||||
|
|
||||||
|
def test_config_get_user_dict(make_config, tmp_path):
|
||||||
|
config = make_config("something.testrun.org")
|
||||||
|
data = config.get_user_dict("user1@something.org")
|
||||||
|
assert data["home"]
|
||||||
|
assert data["uid"] == "vmail"
|
||||||
|
assert data["gid"] == "vmail"
|
||||||
|
assert "password" not in data
|
||||||
|
|
||||||
|
addr = "user1@something.org"
|
||||||
|
enc_password = "l1k2j31lk2j3l1k23j123"
|
||||||
|
data = config.get_user_dict(addr, enc_password=enc_password)
|
||||||
|
assert addr in str(data["home"])
|
||||||
|
assert data["uid"] == "vmail"
|
||||||
|
assert data["gid"] == "vmail"
|
||||||
|
assert data["password"] == enc_password
|
||||||
|
|||||||
Reference in New Issue
Block a user