Giter Club home page Giter Club logo

mailsrv's Introduction

GitHub Workflow Status (branch) GitHub License

Python style: black Shell style: shellcheck

Milestone Crawl Milestone Walk Milestone Run Milestone Fly

mailsrv

This is a mail server setup using Postfix as Mail Transport Agent (MTA) and Dovecot as Mail Delivery Agent (MDA) and IMAP/POP3 server.

Disclaimer

This is my personal setup, which is based on Christoph Haas' ISPmail tutorial, though it deviates in some aspects.

You may freely use the content of this repository (it's MIT licensed), but be aware that you will have to adjust some aspects. I highly recommend working through the tutorial to get a deeper understanding of what is going on.

mailsrv's People

Contributors

mischback avatar dependabot[bot] avatar

Watchers

 avatar  avatar

mailsrv's Issues

``journald`` logging

Verify and ensure, that Postfix and Dovecot are using journald for their logging.

Prepare default workflow for ``GitHub Actions``

The default workflow will perform various things, but at this point just perform a linting job.

  • run black
  • run isort
  • run mypy
  • run flake8
  • OPTIONAL streamline this as make ci/linting ?
  • include workflow badge in README

Dovecot Configuration

Relevant Dovecot settings

  • 10-auth.conf
    • auth_mechanism: Outlook requires login, plain is the default
    • review the different authentication backends with Dovecot's documentation
  • 10-mail.conf
    • mail_location
      • guide suggests maildir:~/Maildir, but as we are (mainly) concerned about virtual users, how does this work?
    • mail_plugins: add quota to manage the size of users' mailboxes
  • 10-master.conf
    • provide the authentication socket required by Postfix:
      # Postfix smtp-auth
      unix_listener /var/spool/postfix/private/auth {
        mode = 0660
        user = postfix
        group = postfix
      }
      
    • create the lmtp socket for Postfix (might already be there, configure accordingly)
      service lmtp {
        unix_listener /var/spool/postfix/private/dovecot-lmtp {
          group = postfix
          mode = 0600
          user = postfix
        }
      }
      
    • review other settings here!
  • 10-ssl.conf
    • ssl_cert and ssl_key: Point to the respective files
      • IMPORTANT syntax is like ssl_cert = </etc/letsencrypt/live/webmail.example.org/fullchain.pem, making the content of the certificate available!
  • 20-lmtp.conf
    • if server side sieve should be applied, activate the plugin here (needs packet installation)
    • tracked in #21
  • 90-quota.conf
    • Provide the actual mailbox quota
      plugin {
        quota = maildir:User quota
      
        quota_status_success = DUNNO
        quota_status_nouser = DUNNO
        quota_status_overquota = "452 4.2.2 Mailbox is full and cannot receive any more emails"
      }
      
    • provide the endpoint (socket) for Postfix
      service quota-status {
        executable = /usr/lib/dovecot/quota-status -p postfix
        unix_listener /var/spool/postfix/private/quota-status {
          user = postfix
        }
      }
      
      • Why is the definition of unix_listener lacking mode and group settings, as they are present in the other socket definitions?
    • the actual quota must be provided by the user_db; needs investigation!
    • Enable warnings about quotas:
      plugin {
         quota_warning = storage=95%% quota-warning 95 %u
         quota_warning2 = storage=80%% quota-warning 80 %u
      }
      service quota-warning {
         executable = script /usr/local/bin/quota-warning.sh  # Adjust script path!
         unix_listener quota-warning {
           user = vmail
           group = vmail
           mode = 0660
         }
      }
      
    • The referenced script to send the users emails about their quotas:
      #!/bin/sh
      PERCENT=$1
      USER=$2
      cat << EOF | /usr/lib/dovecot/dovecot-lda -d $USER -o "plugin/quota=maildir:User quota:noenforcing"
      From: [email protected]
      Subject: Quota warning - $PERCENT% reached
      
      Your mailbox can only store a limited amount of emails.
      Currently it is $PERCENT% full. If you reach 100% then
      new emails cannot be stored. Thanks for your understanding.
      EOF
      
  • Important somewhere is the user_db setting. This needs careful attention, because my setup will not rely on SQL as backend (deviating from the tutorial!); see https://doc.dovecot.org/configuration_manual/authentication/user_databases_userdb/ and https://doc.dovecot.org/configuration_manual/authentication/passwd_file/#authentication-passwd-file

CI Test Suite

Prerequisites

  • Is the test suite working with the current, manual setup?
    • basically the test suite is working!
    • there are still some hard-coded assumptions in the test suite (mailsrv_aux.test_suite.smtp) and some configuration variables that are currently not exposed in test_runner.py; this have to be fixed!
  • can a working Docker Container be created from our installation process?

Remove test-venv-related code from ``Makefile``

Makefile includes code for a dedicated virtual environment for the test suite, but as of now, the test suite only relies on Python's standard library.

  • remove code from Makefile
  • remove requirements/test_suite.txt (see #4)
  • remove requirements/test_suite.txt from tox configuration in pyproject.toml (this leaves a blank mypy for typechecking. However, the infrastructure to perform typechecking with the actual dependencies should be left in place. Document this somewhere!)

``Sphinx`` / ``readthedocs``-compatible Documentation

Provide a setup for sphinx, including a general structure for the documentation.

Pages

  • Getting started
  • Mail in a nutshell
  • Configuration References
  • Cookbook
    • Keep configuration in a dedicated directory or repository
      • Should be highly recommended!
    • In-depth setup of IP and DNS and how these things are working together
      • Including the guide on How to ensure Postfix uses the right IP
      • See #19
  • Developer Documentation
    • Development Setup
    • Coding Style
    • Git Branching Model
  • References
    • Official Documentation
    • Guides
    • Other Projects

Rename Configuration Templates

As of now, the configuration templates have a .sample suffix. .template would be more fitting.

Requires adjustment of:

  • Makefile
  • .gitignore

Dovecot's ``auth_worker_max_count``

Dovecot is used to authenticate (mail) users for itsself (IMAP/POP3) and for Postfix (SMTP submission).

In 10-auth.conf the setting auth_worker_max_count is heavily modified compared to Dovecot's default value of 30 (currently set to 5).

Assuming the number of possible virtual users / mailboxes, this might be sufficient for my personal setup, but probably this should be made configurable in settings.env or at least clearly documented.

Configuration Reference

While working on the configuration reference (as tracked in #23) it feels like duplicating too much.

configs/postfix/main.cf does already include lots of documentation, it feels unnecessary to duplicate this again for the Sphinx-based documentation.

Instead, let's parse the actual configuration files, extract the comments and generate the Sphinx documentation automatically.

For reference: 9f08744 has rST compatible documentation of some Postfix settings.

``mypy`` in GitHub Actions

mypy fails in GitHub Actions workflows sporadically. It can not be reproduced deterministically, so for now this is fixed by disabling the mypy pre-commit hook during CI.

This does introduce additional complexity to the Makefile.

Postfix's ``master.cf``

There are quite a lot of (sub) services in Postfix's default master.cf. Probably some of them are not actively used in this setup.

Deactivating them seems desirable (resource usage, minimizing attack surface, ...)

Django-based Admin backend

A web-based, Django-based backend to manage virtual domains, mailboxes and aliases.

Just like my own, very old and outdated PHP-based project.

Once this issue will be worked on, it might be placed in a dedicated repository.

Automate the Installation (CRAWL)

The steps to complete the CRAWL milestone (setting up Postfix and Dovecot) should be wrapped in a script for convenience.

  • Use a combination of (small) bash scripts with Makefile as the controlling instance

Separate list of (OS) packages from installation script

As of now, util/scripts/install-packages.sh provides the command to install the packages, including the actual list of packages.

This should be split into

  • a script that includes the installation command (apt-get install)
  • a list of required packages (might be placed in configs/ or requirements/

Test Suite

The Python-based test suite should verify, that the general setup of the different components is working.

Prerequisites

  • reference the actual mailserver (bare-metal or container setup) as mail.sut.test providing (virtual) mail addresses @sut.test

Test Cases

  • SMTP (:25)
  • POP3 (:110) / POP3s (:995)
    • Verify, that logins can only be performed over a secure connection (STARTTLS)
    • check the mailbox for expected messages
      • pick up and parse the protocol of the SMTP test suites, check all local mailboxes
  • SMTP (submission :587)
    • Any "sending address" (might be determined from the LHS in util/test_configs/postfix_sender-login-map) should be able to send mail to local / internal accounts and external accounts
    • Sending of mails does require a valid login (RHS in util/test_configs/postfix_sender-login-map)
      • sending addresses may be usable by different login accounts (RHS of util/test_configs/postfix_sender-login-map might be a list); this has to be cross-checked!
      • actual logins have to be performed via a secure connection (TLS/STARTTLS)

Required Infrastructure

Change Purpose of this Repository

The purpose of this repository changed!

The original idea was the development of a containerized mail setup, that was easy to manage and easy to deploy.

However, while testing and researching various stuff, I could not find easy solutions to all kind of problems. Furthermore, there are already several projects that do provide similar functionalities.

New Focus of the Repository

The ISPmail guide was the original blueprint for the desired mail setup. However, quite early during development, some things were changed, e.g. ditching the use of a SQL database for alias, mailbox and domain management.

However: As the need for containerization ceased (mostly inside of myself), going bare metal (or whatever you will call it on a VPS) brings database topic back into question, as the overall server will require a database server in any case.

Having said this: This repository is now intended to provide a ready to go configuration template, so that the mail service (inlcuding Postfix, Dovecot, ...) can be implemented with very little effort.

Everything HTTP-related, including the webmail interface and the generation/renewal of SSL certificates will be skipped aswell.

Postfix Configuration

This issue just documents the configuration of Postfix with some additional notes.

Please note that Postfix and Dovecot work closely together and can not be handled independently!

Postfix Configuration

  • mydestination for Postfix's canonical domains, that is domains that are directly related to the server. Might just be localhost and the actual mail setup relies on virtual domains
  • mynetworks: Should most likely be empty or just containing my very own subnet
  • Use Dovecot for SASL authentication: smtpd_sasl_type=dovecot
  • Use the existing authentication socket: smtpd_sasl_path=private/auth
  • smtpd_sasl_auth_enable=yes Verify this setting
  • smtpd_tls_security_level=may Verfiy this setting (document what it is doing!)
  • smtpd_tls_auth_only=yes Verfiy this setting (document what it is doing!)
  • smtpd_tls_cert_file=/etc/letsencrypt/live/webmail.example.org/fullchain.pem
    • provide the actual path to the certificate file
  • smtpd_tls_key_file=/etc/letsencrypt/live/webmail.example.org/privkey.pem (see above)
  • smtp_tls_security_level=may Verify this setting (document what it is doing!)
  • virtual mailboxes: virtual_mailbox_maps
  • virtual domains: virtual_mailbox_domains
  • (virtual) aliases: virtual_alias_maps
  • Verify: smtpd_recipient_restrictions=reject_sender_login_mismatch
  • use Dovecot's lmtp service: virtual_transport=lmtp:unix:private/dovecot-lmtp
  • use Dovecot's quota service: smtpd_recipient_restrictions=reject_unauth_destination,"check_policy_service unix:private/quota-status"
  • smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination Verify this setting (document what it is doing!)
  • enable submission service in /etc/postfix/master.cf
    submission inet n       -       y       -       -       smtpd
     -o syslog_name=postfix/submission
     -o smtpd_tls_security_level=encrypt
     -o smtpd_sasl_auth_enable=yes
     -o smtpd_tls_auth_only=yes
     -o smtpd_reject_unlisted_recipient=no
     -o smtpd_recipient_restrictions=
     -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
     -o milter_macro_daemon_name=ORIGINATING
    
  • review smtpd_sender_login_maps and make that work
    • The tutorial allow (by default) only sending from your own account. The setup should support sending from alias addresses aswell
    • Has to be included in the submission service like -o smtpd_sender_restrictions=reject_sender_login_mismatch,permit_sasl_authenticated,reject
    • needs mirror rule: [email protected] [email protected]
  • inet_interfaces=all Verify this setting! Details in #19

``redis`` basic installation

rspamd relies on redis as a fast caching backend.

This issue is directly related to #15.

Requirements

  • install redis from Debian/stable
  • provide a basic configuration
    • only available locally (bind to localhost)
    • limit RAM usage (needs investigation)
    • write to disk frequently (needs investigation)
  • Can redis be used for other required caching?
    • Postfix?
    • nginx
    • Django

``Postfix`` IP address settings

Research, evaluate and then document IP address-related settings of Postfix.

Relevant Settings

  • inet_interfaces
  • smtp_bind_address
    • not tested
    • assumed to be working like smtp_bind_address6
  • smtp_bind_address6
  • smtp_bind_address_enforce
    • available from Postfix > 3.7
    • not tested, as Debian/stable (bullseye) only includes 3.5
    • left in main.cf.sample with a clarifying comment
  • inet_protocols
    • not tested
  • smtp_address_preference
    • not tested

Required Test Cases

  • inet_interfaces set to a specific IP
    • Is that IP used for all inbound and outbound connections?
    • This would be the most obvious and easy configuration
  • inet_interfaces set to a specific IP + localhost
    • Is the IP used for all inbound and outbound connections? At least the external ones?
    • This would be the most desirable configuration (easy, obvious, functional)
  • inet_interfaces set to all in combination with smtp_bind_address6
    • Is smtp_bind_address6 used for all outbound connections?!
  • inet_interfaces set to all in combination with smtp_bind_address6 and multiple available IPv6 interfaces.
    • Verify smtp_bind_address_enforce. I feel like this should be set to yes.

Notes

  • if inet_interfaces is set to an actual IP address (v4 or v6), Postfix binds itsself only to that interface (see ss -lntp output)
  • inet_intonly to the specified interfaceserfaces might be a list, e.g. 192.168.100.5, 127.0.0.1, [::1]
  • if inet_interfaces is set to all and inet_protocols is set to ipv6, Postfix binds itsself to IPv6 interfaces only (see ss -lntp output)

Original Issue Statement

Postfix might be tied to a dedicated IPv4/IPv6 address.

This will make SPF/DMARC/... possible without too much interdependent settings with other services.

Highly relevant for an IPv6 setup, that maintains scalability.

See https://serverfault.com/questions/92181/how-to-make-postfix-use-another-ip-address#92207

Originally posted by @Mischback in #13 (comment)

Test Suite - Quota Tests

Follow up to #1

Create Test Suite to verify the quota setup

The configuration applies a general quota to all mailboxes. User-specific quotas may be applied using dovecot's userdb (passwd-like file).

The server automatically notifies the user about quota limits (as of now at 80% and 95% of the quota).

The test suite should check for these notifications.

Implementation Idea

  • use a dedicated user / mailbox for the test
  • limit capacity to a known (and easily reachable) value (e.g. 1kB)
  • manually reach a limit of 80% of the capacity
  • manually reach 95% of the capacity
  • add notification mails to expected_messages that will be checked during the (POP3-based) testing stage

Dependabot setup

Provide a minimal dependabot setup

  • Dependency Management of Python dependencies
  • protect the default branch (see one of my NodeJS repositories)
  • automatic approval of PR for MINOR/PATCH updates (see one of my NodeJS repositories)

Configuration Validation

  • Configuration Verification
    • read relevant configuration files
      • Dovecot's userdb: vmail_users
      • Postfix's virtual_mailboxes
      • Postfix's virtual_aliases
      • ...
    • perform parsing and tests
      • all entries in Postfix's virtual_mailboxes must have a corresponding entry in Dovecot's userdb (vmail_users)
      • all of Postfix's addresses (virtual_mailboxesand the left-hand side of virtual_aliases) must have a domain part in virtual_domains
      • all entries in Postfix's virtual_aliases must resolve to an entry in virtual_mailboxes (or an external address, which raises another warning!)
      • all entries in Postfix's virtual_aliases should not resolve to an external address
      • all entries in Postfix's virtual_mailboxes combined with virtual_aliases should have a corresponding entry in sender_login_map
      • all entries in Postfix's sender_login_map must resolve to an entry in Dovecot's userdb
      • all entries in Dovecot's userdb should have one of the following:
        • a (left hand side) entry in virtual_mailboxes
        • a (right hand side) entry in sender_login_map
      • all domains in virtual_domains should have postmaster@domain and abuse@domain addresses (mailbox or alias)
      • Dovecot's userdb should not contain plain text passwords ({plain}foobar)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.