BootstrapPostfix.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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("/opt/postfix/conf/config.json")
  37. # Create SNI Config
  38. self.run_command(["postmap", "-F", "hash:/opt/postfix/conf/sni.map"])
  39. # Fix Postfix permissions
  40. self.set_owner("/opt/postfix/conf/sql", user="root", group="postfix", recursive=True)
  41. self.set_owner("/opt/postfix/conf/custom_transport.pcre", user="root", group="postfix")
  42. for cf_file in Path("/opt/postfix/conf/sql").glob("*.cf"):
  43. self.set_permissions(cf_file, 0o640)
  44. self.set_permissions("/opt/postfix/conf/custom_transport.pcre", 0o640)
  45. self.set_owner("/var/spool/postfix/public", user="root", group="postdrop", recursive=True)
  46. self.set_owner("/var/spool/postfix/maildrop", user="root", group="postdrop", recursive=True)
  47. self.run_command(["postfix", "set-permissions"], check=False)
  48. # Checking if there is a leftover of a crashed postfix container before starting a new one
  49. pid_file = Path("/var/spool/postfix/pid/master.pid")
  50. if pid_file.exists():
  51. print(f"Removing stale Postfix PID file: {pid_file}")
  52. pid_file.unlink()
  53. def get_valid_cert_dirs(self):
  54. certs = {}
  55. base_path = Path("/etc/ssl/mail")
  56. if not base_path.exists():
  57. return certs
  58. for cert_dir in base_path.iterdir():
  59. if not cert_dir.is_dir():
  60. continue
  61. domains_file = cert_dir / "domains"
  62. cert_file = cert_dir / "cert.pem"
  63. key_file = cert_dir / "key.pem"
  64. if not (domains_file.exists() and cert_file.exists() and key_file.exists()):
  65. continue
  66. with open(domains_file, "r") as f:
  67. domains = [line.strip() for line in f if line.strip()]
  68. if domains:
  69. certs[str(cert_dir)] = domains
  70. return certs