selfprivacy-rest-api/selfprivacy_api/services/mailserver/__init__.py

205 lines
6.1 KiB
Python
Raw Normal View History

2022-08-10 23:36:36 +00:00
"""Class representing Dovecot and Postfix services"""
import base64
import subprocess
import typing
from selfprivacy_api.jobs import Job, JobStatus, Jobs
from selfprivacy_api.services.generic_service_mover import FolderMoveNames, move_service
from selfprivacy_api.services.generic_size_counter import get_storage_usage
from selfprivacy_api.services.generic_status_getter import get_service_status
from selfprivacy_api.services.service import Service, ServiceDnsRecord, ServiceStatus
2022-08-20 18:49:51 +00:00
import selfprivacy_api.utils as utils
2022-08-10 23:36:36 +00:00
from selfprivacy_api.utils.block_devices import BlockDevice
2022-08-12 21:29:18 +00:00
from selfprivacy_api.utils.huey import huey
2022-08-20 18:49:51 +00:00
import selfprivacy_api.utils.network as network_utils
2022-08-12 21:42:11 +00:00
from selfprivacy_api.services.mailserver.icon import MAILSERVER_ICON
2022-08-10 23:36:36 +00:00
class MailServer(Service):
"""Class representing mail service"""
@staticmethod
def get_id() -> str:
return "mailserver"
@staticmethod
def get_display_name() -> str:
return "Mail Server"
@staticmethod
def get_description() -> str:
return "E-Mail for company and family."
@staticmethod
def get_svg_icon() -> str:
2022-08-12 21:42:11 +00:00
return base64.b64encode(MAILSERVER_ICON.encode("utf-8")).decode("utf-8")
2022-08-10 23:36:36 +00:00
2022-08-12 21:29:18 +00:00
@staticmethod
def get_url() -> typing.Optional[str]:
"""Return service url."""
return None
2022-08-10 23:36:36 +00:00
@staticmethod
def is_movable() -> bool:
return True
@staticmethod
def is_required() -> bool:
return True
@staticmethod
def is_enabled() -> bool:
return True
@staticmethod
def get_status() -> ServiceStatus:
imap_status = get_service_status("dovecot2.service")
smtp_status = get_service_status("postfix.service")
2022-08-20 18:49:51 +00:00
if imap_status == ServiceStatus.ACTIVE and smtp_status == ServiceStatus.ACTIVE:
return ServiceStatus.ACTIVE
elif imap_status == ServiceStatus.FAILED or smtp_status == ServiceStatus.FAILED:
return ServiceStatus.FAILED
elif (
imap_status == ServiceStatus.RELOADING
or smtp_status == ServiceStatus.RELOADING
):
return ServiceStatus.RELOADING
elif (
imap_status == ServiceStatus.ACTIVATING
or smtp_status == ServiceStatus.ACTIVATING
):
return ServiceStatus.ACTIVATING
elif (
imap_status == ServiceStatus.DEACTIVATING
or smtp_status == ServiceStatus.DEACTIVATING
2022-08-10 23:36:36 +00:00
):
2022-08-20 18:49:51 +00:00
return ServiceStatus.DEACTIVATING
2022-08-10 23:36:36 +00:00
elif (
2022-08-20 18:49:51 +00:00
imap_status == ServiceStatus.INACTIVE
or smtp_status == ServiceStatus.INACTIVE
2022-08-10 23:36:36 +00:00
):
2022-08-20 18:49:51 +00:00
return ServiceStatus.INACTIVE
2022-08-10 23:36:36 +00:00
elif imap_status == ServiceStatus.OFF or smtp_status == ServiceStatus.OFF:
return ServiceStatus.OFF
else:
2022-08-20 18:49:51 +00:00
return ServiceStatus.FAILED
2022-08-10 23:36:36 +00:00
@staticmethod
def enable():
raise NotImplementedError("enable is not implemented for MailServer")
@staticmethod
def disable():
raise NotImplementedError("disable is not implemented for MailServer")
@staticmethod
def stop():
subprocess.run(["systemctl", "stop", "dovecot2.service"])
subprocess.run(["systemctl", "stop", "postfix.service"])
@staticmethod
def start():
subprocess.run(["systemctl", "start", "dovecot2.service"])
subprocess.run(["systemctl", "start", "postfix.service"])
@staticmethod
def restart():
subprocess.run(["systemctl", "restart", "dovecot2.service"])
subprocess.run(["systemctl", "restart", "postfix.service"])
@staticmethod
def get_configuration():
return {}
@staticmethod
def set_configuration(config_items):
return super().set_configuration(config_items)
@staticmethod
def get_logs():
return ""
@staticmethod
def get_storage_usage() -> int:
return get_storage_usage("/var/vmail")
@staticmethod
def get_location() -> str:
2022-08-20 18:49:51 +00:00
with utils.ReadUserData() as user_data:
2022-08-10 23:36:36 +00:00
if user_data.get("useBinds", False):
return user_data.get("mailserver", {}).get("location", "sda1")
else:
return "sda1"
@staticmethod
def get_dns_records() -> typing.List[ServiceDnsRecord]:
2022-08-20 18:49:51 +00:00
domain = utils.get_domain()
dkim_record = utils.get_dkim_key(domain)
ip4 = network_utils.get_ip4()
ip6 = network_utils.get_ip6()
2022-08-10 23:36:36 +00:00
if dkim_record is None:
return []
return [
2022-08-20 18:49:51 +00:00
ServiceDnsRecord(
type="A",
name=domain,
content=ip4,
ttl=3600,
),
ServiceDnsRecord(
type="AAAA",
name=domain,
content=ip6,
ttl=3600,
),
2022-08-10 23:36:36 +00:00
ServiceDnsRecord(
2022-08-11 19:11:00 +00:00
type="MX", name=domain, content=domain, ttl=3600, priority=10
2022-08-10 23:36:36 +00:00
),
ServiceDnsRecord(
2022-08-20 18:49:51 +00:00
type="TXT", name="_dmarc", content=f"v=DMARC1; p=none", ttl=18000
2022-08-10 23:36:36 +00:00
),
ServiceDnsRecord(
2022-08-22 17:45:00 +00:00
type="TXT",
name=domain,
content=f"v=spf1 a mx ip4:{ip4} -all",
ttl=18000,
2022-08-10 23:36:36 +00:00
),
ServiceDnsRecord(
2022-08-20 18:49:51 +00:00
type="TXT", name="selector._domainkey", content=dkim_record, ttl=18000
2022-08-10 23:36:36 +00:00
),
]
2022-08-17 20:58:56 +00:00
def move_to_volume(self, volume: BlockDevice) -> Job:
2022-08-10 23:36:36 +00:00
job = Jobs.get_instance().add(
2022-08-17 20:58:56 +00:00
type_id="services.mailserver.move",
name="Move Mail Server",
2022-08-10 23:36:36 +00:00
description=f"Moving mailserver data to {volume.name}",
)
move_service(
self,
volume,
job,
[
FolderMoveNames(
name="vmail",
bind_location="/var/vmail",
group="virtualMail",
owner="virtualMail",
),
FolderMoveNames(
name="sieve",
bind_location="/var/sieve",
group="virtualMail",
owner="virtualMail",
),
],
"mailserver",
)
return job