BootstrapPostfix.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from jinja2 import Environment, FileSystemLoader
  2. from modules.BootstrapBase import BootstrapBase
  3. from pathlib import Path
  4. import os
  5. import sys
  6. import time
  7. class Bootstrap(BootstrapBase):
  8. def bootstrap(self):
  9. # Connect to MySQL
  10. self.connect_mysql()
  11. # Wait for DNS
  12. self.wait_for_dns("mailcow.email")
  13. self.create_dir("/opt/postfix/conf/sql/")
  14. # Setup Jinja2 Environment and load vars
  15. self.env = Environment(
  16. loader=FileSystemLoader([
  17. '/opt/postfix/conf/custom_templates',
  18. '/opt/postfix/conf/config_templates'
  19. ]),
  20. keep_trailing_newline=True,
  21. lstrip_blocks=True,
  22. trim_blocks=True
  23. )
  24. with open("/opt/postfix/conf/extra.cf", "r") as f:
  25. extra_config = f.read()
  26. extra_vars = {
  27. "VALID_CERT_DIRS": self.get_valid_cert_dirs(),
  28. "EXTRA_CF": extra_config
  29. }
  30. self.env_vars = self.prepare_template_vars('/overwrites.json', extra_vars)
  31. print("Set Timezone")
  32. self.set_timezone()
  33. print("Set Syslog redis")
  34. self.set_syslog_redis()
  35. print("Render config")
  36. self.render_config("aliases.j2", "/etc/aliases")
  37. self.render_config("mysql_relay_ne.cf.j2", "/opt/postfix/conf/sql/mysql_relay_ne.cf")
  38. self.render_config("mysql_relay_recipient_maps.cf.j2", "/opt/postfix/conf/sql/mysql_relay_recipient_maps.cf")
  39. self.render_config("mysql_tls_policy_override_maps.cf.j2", "/opt/postfix/conf/sql/mysql_tls_policy_override_maps.cf")
  40. self.render_config("mysql_tls_enforce_in_policy.cf.j2", "/opt/postfix/conf/sql/mysql_tls_enforce_in_policy.cf")
  41. self.render_config("mysql_sender_dependent_default_transport_maps.cf.j2", "/opt/postfix/conf/sql/mysql_sender_dependent_default_transport_maps.cf")
  42. self.render_config("mysql_transport_maps.cf.j2", "/opt/postfix/conf/sql/mysql_transport_maps.cf")
  43. self.render_config("mysql_virtual_resource_maps.cf.j2", "/opt/postfix/conf/sql/mysql_virtual_resource_maps.cf")
  44. self.render_config("mysql_sasl_passwd_maps_sender_dependent.cf.j2", "/opt/postfix/conf/sql/mysql_sasl_passwd_maps_sender_dependent.cf")
  45. self.render_config("mysql_sasl_passwd_maps_transport_maps.cf.j2", "/opt/postfix/conf/sql/mysql_sasl_passwd_maps_transport_maps.cf")
  46. self.render_config("mysql_virtual_alias_domain_maps.cf.j2", "/opt/postfix/conf/sql/mysql_virtual_alias_domain_maps.cf")
  47. self.render_config("mysql_virtual_alias_maps.cf.j2", "/opt/postfix/conf/sql/mysql_virtual_alias_maps.cf")
  48. self.render_config("mysql_recipient_bcc_maps.cf.j2", "/opt/postfix/conf/sql/mysql_recipient_bcc_maps.cf")
  49. self.render_config("mysql_sender_bcc_maps.cf.j2", "/opt/postfix/conf/sql/mysql_sender_bcc_maps.cf")
  50. self.render_config("mysql_recipient_canonical_maps.cf.j2", "/opt/postfix/conf/sql/mysql_recipient_canonical_maps.cf")
  51. self.render_config("mysql_virtual_domains_maps.cf.j2", "/opt/postfix/conf/sql/mysql_virtual_domains_maps.cf")
  52. self.render_config("mysql_virtual_mailbox_maps.cf.j2", "/opt/postfix/conf/sql/mysql_virtual_mailbox_maps.cf")
  53. self.render_config("mysql_virtual_relay_domain_maps.cf.j2", "/opt/postfix/conf/sql/mysql_virtual_relay_domain_maps.cf")
  54. self.render_config("mysql_virtual_sender_acl.cf.j2", "/opt/postfix/conf/sql/mysql_virtual_sender_acl.cf")
  55. self.render_config("mysql_mbr_access_maps.cf.j2", "/opt/postfix/conf/sql/mysql_mbr_access_maps.cf")
  56. self.render_config("mysql_virtual_spamalias_maps.cf.j2", "/opt/postfix/conf/sql/mysql_virtual_spamalias_maps.cf")
  57. self.render_config("sni.map.j2", "/opt/postfix/conf/sni.map")
  58. self.render_config("main.cf.j2", "/opt/postfix/conf/main.cf")
  59. # Conditional render
  60. if not Path("/opt/postfix/conf/dns_blocklists.cf").exists():
  61. self.render_config("dns_blocklists.cf.j2", "/opt/postfix/conf/dns_blocklists.cf")
  62. if not Path("/opt/postfix/conf/dns_reply.map").exists():
  63. self.render_config("dns_reply.map.j2", "/opt/postfix/conf/dns_reply.map")
  64. if not Path("/opt/postfix/conf/custom_postscreen_whitelist.cidr").exists():
  65. self.render_config("custom_postscreen_whitelist.cidr.j2", "/opt/postfix/conf/custom_postscreen_whitelist.cidr")
  66. if not Path("/opt/postfix/conf/custom_transport.pcre").exists():
  67. self.render_config("custom_transport.pcre.j2", "/opt/postfix/conf/custom_transport.pcre")
  68. # Create SNI Config
  69. self.run_command(["postmap", "-F", "hash:/opt/postfix/conf/sni.map"])
  70. # Fix Postfix permissions
  71. self.set_owner("/opt/postfix/conf/sql", user="root", group="postfix", recursive=True)
  72. self.set_owner("/opt/postfix/conf/custom_transport.pcre", user="root", group="postfix")
  73. for cf_file in Path("/opt/postfix/conf/sql").glob("*.cf"):
  74. self.set_permissions(cf_file, 0o640)
  75. self.set_permissions("/opt/postfix/conf/custom_transport.pcre", 0o640)
  76. self.set_owner("/var/spool/postfix/public", user="root", group="postdrop", recursive=True)
  77. self.set_owner("/var/spool/postfix/maildrop", user="root", group="postdrop", recursive=True)
  78. self.run_command(["postfix", "set-permissions"], check=False)
  79. # Checking if there is a leftover of a crashed postfix container before starting a new one
  80. pid_file = Path("/var/spool/postfix/pid/master.pid")
  81. if pid_file.exists():
  82. print(f"Removing stale Postfix PID file: {pid_file}")
  83. pid_file.unlink()
  84. def get_valid_cert_dirs(self):
  85. certs = {}
  86. base_path = Path("/etc/ssl/mail")
  87. if not base_path.exists():
  88. return certs
  89. for cert_dir in base_path.iterdir():
  90. if not cert_dir.is_dir():
  91. continue
  92. domains_file = cert_dir / "domains"
  93. cert_file = cert_dir / "cert.pem"
  94. key_file = cert_dir / "key.pem"
  95. if not (domains_file.exists() and cert_file.exists() and key_file.exists()):
  96. continue
  97. with open(domains_file, "r") as f:
  98. domains = [line.strip() for line in f if line.strip()]
  99. if domains:
  100. certs[str(cert_dir)] = domains
  101. return certs