mirror of
https://github.com/chatmail/relay.git
synced 2026-05-21 21:38:03 +00:00
Encrypt the passwords in the database
There is also no need to compare the passwords manually, dovecot does it for us.
This commit is contained in:
@@ -7,10 +7,25 @@ from socketserver import (
|
|||||||
ThreadingMixIn,
|
ThreadingMixIn,
|
||||||
)
|
)
|
||||||
import pwd
|
import pwd
|
||||||
|
import subprocess
|
||||||
|
|
||||||
from .database import Database
|
from .database import Database
|
||||||
|
|
||||||
|
|
||||||
|
def encrypt_password(password: str):
|
||||||
|
password = password.encode("ascii")
|
||||||
|
# https://doc.dovecot.org/configuration_manual/authentication/password_schemes/
|
||||||
|
process = subprocess.Popen(
|
||||||
|
["doveadm", "pw", "-s", "BLF-CRYPT"],
|
||||||
|
stdin=subprocess.PIPE,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
)
|
||||||
|
stdout_data, _stderr_data = process.communicate(
|
||||||
|
input=password + b"\n" + password + b"\n"
|
||||||
|
)
|
||||||
|
return stdout_data.decode("ascii").strip()
|
||||||
|
|
||||||
|
|
||||||
def create_user(db, user, password):
|
def create_user(db, user, password):
|
||||||
with db.write_transaction() as conn:
|
with db.write_transaction() as conn:
|
||||||
conn.create_user(user, password)
|
conn.create_user(user, password)
|
||||||
@@ -33,11 +48,9 @@ def lookup_userdb(db, user):
|
|||||||
def lookup_passdb(db, user, password):
|
def lookup_passdb(db, user, password):
|
||||||
userdata = get_user_data(db, user)
|
userdata = get_user_data(db, user)
|
||||||
if not userdata:
|
if not userdata:
|
||||||
return create_user(db, user, password)
|
return create_user(db, user, encrypt_password(password))
|
||||||
if userdata.get("password") == password:
|
userdata["password"] = userdata["password"].strip()
|
||||||
return userdata
|
return userdata
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def handle_dovecot_request(msg, db):
|
def handle_dovecot_request(msg, db):
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import subprocess
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from .dictproxy import get_user_data
|
from .dictproxy import get_user_data
|
||||||
|
|||||||
Reference in New Issue
Block a user