2022-08-25 17:03:56 +00:00
|
|
|
"""Class representing Dovecot and Postfix services"""
|
|
|
|
|
|
|
|
import base64
|
|
|
|
import subprocess
|
2024-03-01 00:21:31 +00:00
|
|
|
from typing import Optional, List
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2023-07-27 23:18:05 +00:00
|
|
|
from selfprivacy_api.jobs import Job, Jobs
|
2022-08-25 17:03:56 +00:00
|
|
|
from selfprivacy_api.services.generic_service_mover import FolderMoveNames, move_service
|
|
|
|
from selfprivacy_api.services.generic_status_getter import (
|
|
|
|
get_service_status_from_several_units,
|
|
|
|
)
|
|
|
|
from selfprivacy_api.services.service import Service, ServiceDnsRecord, ServiceStatus
|
2023-07-27 23:18:05 +00:00
|
|
|
from selfprivacy_api import utils
|
2022-08-25 17:03:56 +00:00
|
|
|
from selfprivacy_api.utils.block_devices import BlockDevice
|
|
|
|
from selfprivacy_api.services.mailserver.icon import MAILSERVER_ICON
|
|
|
|
|
|
|
|
|
|
|
|
class MailServer(Service):
|
|
|
|
"""Class representing mail service"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_id() -> str:
|
2024-01-09 18:58:09 +00:00
|
|
|
return "simple-nixos-mailserver"
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
@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:
|
|
|
|
return base64.b64encode(MAILSERVER_ICON.encode("utf-8")).decode("utf-8")
|
|
|
|
|
2023-04-19 12:43:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_user() -> str:
|
|
|
|
return "virtualMail"
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2024-03-01 00:21:31 +00:00
|
|
|
def get_url() -> Optional[str]:
|
2022-08-25 17:03:56 +00:00
|
|
|
"""Return service url."""
|
|
|
|
return None
|
|
|
|
|
2024-03-01 00:21:31 +00:00
|
|
|
@staticmethod
|
2024-03-01 11:58:28 +00:00
|
|
|
def get_subdomain() -> Optional[str]:
|
2024-03-01 00:21:31 +00:00
|
|
|
return None
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def is_movable() -> bool:
|
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_required() -> bool:
|
|
|
|
return True
|
|
|
|
|
2023-06-29 11:27:08 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_backup_description() -> str:
|
|
|
|
return "Mail boxes and filters."
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def is_enabled() -> bool:
|
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_status() -> ServiceStatus:
|
|
|
|
return get_service_status_from_several_units(
|
|
|
|
["dovecot2.service", "postfix.service"]
|
|
|
|
)
|
|
|
|
|
|
|
|
@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():
|
2023-07-27 23:18:05 +00:00
|
|
|
subprocess.run(["systemctl", "stop", "dovecot2.service"], check=False)
|
|
|
|
subprocess.run(["systemctl", "stop", "postfix.service"], check=False)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def start():
|
2023-07-27 23:18:05 +00:00
|
|
|
subprocess.run(["systemctl", "start", "dovecot2.service"], check=False)
|
|
|
|
subprocess.run(["systemctl", "start", "postfix.service"], check=False)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def restart():
|
2023-07-27 23:18:05 +00:00
|
|
|
subprocess.run(["systemctl", "restart", "dovecot2.service"], check=False)
|
|
|
|
subprocess.run(["systemctl", "restart", "postfix.service"], check=False)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_configuration():
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def set_configuration(config_items):
|
|
|
|
return super().set_configuration(config_items)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_logs():
|
|
|
|
return ""
|
|
|
|
|
2023-04-17 13:47:49 +00:00
|
|
|
@staticmethod
|
2024-03-01 00:21:31 +00:00
|
|
|
def get_folders() -> List[str]:
|
2023-04-17 13:47:49 +00:00
|
|
|
return ["/var/vmail", "/var/sieve"]
|
|
|
|
|
2024-03-01 00:21:31 +00:00
|
|
|
@classmethod
|
|
|
|
def get_dns_records(cls, ip4: str, ip6: Optional[str]) -> List[ServiceDnsRecord]:
|
2022-08-25 17:03:56 +00:00
|
|
|
domain = utils.get_domain()
|
|
|
|
dkim_record = utils.get_dkim_key(domain)
|
|
|
|
|
|
|
|
if dkim_record is None:
|
|
|
|
return []
|
|
|
|
|
2024-03-01 00:21:31 +00:00
|
|
|
dns_records = [
|
2022-08-25 17:03:56 +00:00
|
|
|
ServiceDnsRecord(
|
|
|
|
type="A",
|
|
|
|
name=domain,
|
|
|
|
content=ip4,
|
|
|
|
ttl=3600,
|
2023-11-24 10:57:52 +00:00
|
|
|
display_name="Root Domain",
|
2022-08-25 17:03:56 +00:00
|
|
|
),
|
|
|
|
ServiceDnsRecord(
|
2023-11-24 10:57:52 +00:00
|
|
|
type="MX",
|
|
|
|
name=domain,
|
|
|
|
content=domain,
|
|
|
|
ttl=3600,
|
|
|
|
priority=10,
|
|
|
|
display_name="Mail server record",
|
2022-08-25 17:03:56 +00:00
|
|
|
),
|
|
|
|
ServiceDnsRecord(
|
2023-11-24 10:57:52 +00:00
|
|
|
type="TXT",
|
|
|
|
name="_dmarc",
|
|
|
|
content="v=DMARC1; p=none",
|
|
|
|
ttl=18000,
|
|
|
|
display_name="DMARC record",
|
2022-08-25 17:03:56 +00:00
|
|
|
),
|
|
|
|
ServiceDnsRecord(
|
|
|
|
type="TXT",
|
|
|
|
name=domain,
|
|
|
|
content=f"v=spf1 a mx ip4:{ip4} -all",
|
|
|
|
ttl=18000,
|
2023-11-24 10:57:52 +00:00
|
|
|
display_name="SPF record",
|
2022-08-25 17:03:56 +00:00
|
|
|
),
|
|
|
|
ServiceDnsRecord(
|
2023-11-24 10:57:52 +00:00
|
|
|
type="TXT",
|
|
|
|
name="selector._domainkey",
|
|
|
|
content=dkim_record,
|
|
|
|
ttl=18000,
|
|
|
|
display_name="DKIM key",
|
2022-08-25 17:03:56 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
2024-03-01 00:21:31 +00:00
|
|
|
if ip6 is not None:
|
|
|
|
dns_records.append(
|
|
|
|
ServiceDnsRecord(
|
|
|
|
type="AAAA",
|
|
|
|
name=domain,
|
|
|
|
content=ip6,
|
|
|
|
ttl=3600,
|
|
|
|
display_name="Root Domain (IPv6)",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return dns_records
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
def move_to_volume(self, volume: BlockDevice) -> Job:
|
2022-10-27 14:01:11 +00:00
|
|
|
job = Jobs.add(
|
2023-08-02 04:41:55 +00:00
|
|
|
type_id="services.email.move",
|
2022-08-25 17:03:56 +00:00
|
|
|
name="Move Mail Server",
|
|
|
|
description=f"Moving mailserver data to {volume.name}",
|
|
|
|
)
|
|
|
|
|
|
|
|
move_service(
|
|
|
|
self,
|
|
|
|
volume,
|
|
|
|
job,
|
2023-04-19 12:43:47 +00:00
|
|
|
FolderMoveNames.default_foldermoves(self),
|
2024-01-09 18:58:09 +00:00
|
|
|
"simple-nixos-mailserver",
|
2022-08-25 17:03:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return job
|