Fixed pdfcrendtials preventing login

This commit is contained in:
Spitap
2023-09-01 13:31:30 +02:00
parent 6e35130849
commit 1794ec9feb
3 changed files with 34 additions and 16 deletions
+10 -1
View File
@@ -1,5 +1,6 @@
"""PDF credentials handlers."""
import logging
import os
from django.urls import reverse
@@ -10,6 +11,7 @@ from django.utils.translation import gettext as _
from modoboa.admin import signals as admin_signals
from modoboa.core import models as core_models
from modoboa.core import signals as core_signals
from modoboa.lib.exceptions import InternalError
from modoboa.parameters import tools as param_tools
from .documents import credentials
@@ -25,7 +27,14 @@ def password_updated(sender, account, password, created, **kwargs):
"generate_at_creation")
if (generate_at_creation and not created) or account.is_superuser:
return
init_storage_dir()
try:
init_storage_dir()
except InternalError as e:
logger = logging.getLogger("modoboa.admin")
logger.error(
_("Failed to create PDF_credentials directory. "
"Please check the permissions or the path."),
exc_info=e)
credentials(account, password)
+20 -12
View File
@@ -1,6 +1,7 @@
"""Internal library."""
from io import BytesIO
import logging
import os
import struct
@@ -18,7 +19,7 @@ from modoboa.parameters import tools as param_tools
def init_storage_dir():
"""Create the directory whare documents will be stored."""
"""Create the directory where documents will be stored."""
storage_dir = param_tools.get_global_parameter("storage_dir")
if os.path.exists(storage_dir) and os.access(storage_dir, os.W_OK):
return
@@ -66,17 +67,24 @@ def crypt_and_save_to_file(content, filename, length, chunksize=64*512):
iv = os.urandom(16)
cipher = _get_cipher(iv)
encryptor = cipher.encryptor()
with open(filename, "wb") as fp:
fp.write(struct.pack(b"<Q", length))
fp.write(iv)
while True:
chunk = content.read(chunksize)
if not len(chunk):
break
elif len(chunk) % 16:
chunk += b" " * (16 - len(chunk) % 16)
fp.write(encryptor.update(force_bytes(chunk)))
fp.write(encryptor.finalize())
try:
with open(filename, "wb") as fp:
fp.write(struct.pack(b"<Q", length))
fp.write(iv)
while True:
chunk = content.read(chunksize)
if not len(chunk):
break
elif len(chunk) % 16:
chunk += b" " * (16 - len(chunk) % 16)
fp.write(encryptor.update(force_bytes(chunk)))
fp.write(encryptor.finalize())
except FileNotFoundError as e:
logger = logging.getLogger("modoboa.admin")
logger.error(
_("Failed to create PDF_credentials file. "
"Please check the permissions or the path."),
exc_info=e)
def decrypt_file(filename, chunksize=24*1024):