system-configuration/modules/config/mail.scm

mail.scm

1
;;; Tyreunom's system administration and configuration tools.
2
;;;
3
;;; Copyright © 2019 Julien Lepiller <julien@lepiller.eu>
4
;;;
5
;;; This program is free software: you can redistribute it and/or modify
6
;;; it under the terms of the GNU General Public License as published by
7
;;; the Free Software Foundation, either version 3 of the License, or
8
;;; (at your option) any later version.
9
;;;
10
;;; This program is distributed in the hope that it will be useful,
11
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
;;; GNU General Public License for more details.
14
;;;
15
;;; You should have received a copy of the GNU General Public License
16
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
;;
19
;; Email configuration
20
;;
21
22
(define-module (config mail)
23
  #:use-module (data dns)
24
  #:use-module (gnu packages mail)
25
  #:use-module (gnu services)
26
  #:use-module (gnu services mail)
27
  #:use-module (guix gexp)
28
  #:use-module (services mail)
29
  #:export (lepiller-mail-services))
30
31
(define aliases-file
32
  (plain-file "aliases" "postmaster root
33
34
@ tyreunom
35
"))
36
37
(define relays-file
38
  (plain-file "other-relays"
39
    (string-append ene-rennes-ip4 "\n" ene-toulouse-ip4 "\n" ene-toulouse-ip6 "\n"
40
                   hermes-ip4 "\n" hermes-ip6 "\n" )))
41
42
(define blacklist-file
43
  (plain-file "blacklist" "
44
@yahoo.com.cn
45
@qq.com
46
@just-aero.us
47
@elitetorrent1.com
48
@officedepot.com
49
@isaemailmarketing.co
50
@email.etsy.com
51
@online-discounter.net
52
@turing.com
53
@napptive.com
54
opendmarc@box.euandre.org
55
opendmark@mail.arctype.co"))
56
57
(define (opensmtpd-conf interface domain)
58
  (mixed-text-file "smtpd.conf" "
59
# This is the smtpd server system-wide configuration file.
60
# See smtpd.conf(5) for more information.
61
62
# My TLS certificate and key
63
pki lepiller.eu cert \"/etc/letsencrypt/live/" domain "/fullchain.pem\"
64
pki lepiller.eu key \"/etc/letsencrypt/live/" domain "/privkey.pem\"
65
66
# Edit this file to add more virtual users (passwords are read in that file
67
# instead of /etc/passwd.
68
table passwd file:/etc/mail/passwd
69
70
table other-relays file:" relays-file "
71
table blacklist file:" blacklist-file "
72
73
# A simple spam filter
74
filter check-rdns phase connect match !rdns disconnect \"550 no rDNS\"
75
filter spam-filter phase mail-from match mail-from <blacklist> reject \"555 Your spam level is over NINE THOUSAND!\"
76
filter rspamd proc-exec \"" opensmtpd-filter-rspamd "/libexec/opensmtpd/filter-rspamd\"
77
78
# port 25 is used only for receiving from external servers, and they may start a
79
# TLS session if the want.
80
listen on " interface " port 25 tls pki lepiller.eu filter { check-rdns, spam-filter, rspamd }
81
# For sending messages from outside of this server, you need to authenticate and
82
# use TLS.
83
listen on " interface " port 587 tls-require pki lepiller.eu mask-src auth <passwd>
84
# Localhost is used by the .onion, so we use the same configuration for
85
# local connections.
86
listen on lo port 25 tls pki lepiller.eu filter { check-rdns, spam-filter, rspamd }
87
# Since incoming connection uses tor, we don't need tls, but still require
88
# authentication; we're not a relay
89
listen on lo port 587 tls pki lepiller.eu mask-src auth <passwd>
90
91
# DKIMproxy
92
listen on lo port 10028 tag DKIM_OUT
93
94
# The socket is considered an internal connection
95
listen on socket mask-src
96
97
# Maybe it'll work better if we connect to gmail only with v4?
98
#limit mta for domain gmail.com inet4
99
100
# TODO: manage these files directly in the configuration?
101
# If you edit the file, you have to run \"smtpctl update table aliases\"
102
table aliases file:" aliases-file "
103
104
# We define some actions
105
action receive maildir virtual <aliases>
106
action outbound relay
107
action godkim relay host smtp://127.0.0.1:10027
108
109
# We accept to relay any mail from authenticated users
110
match for any from any auth action godkim
111
match tag DKIM_OUT for any action outbound
112
113
# Then, we reject on some other conditions:
114
115
# If the mail tries to impersonate us
116
match !from src <other-relays> mail-from \"@lepiller.eu\" for any reject
117
# If it comes from someone on the blacklist
118
match from any mail-from <blacklist> reject
119
120
# Finaly, if we accept incoming messages
121
match from any for domain \"lepiller.eu\" action receive
122
match for local action receive
123
"))
124
125
(define (lepiller-imap-service domain)
126
  (service dovecot-service-type
127
           (dovecot-configuration
128
             (mail-location "maildir:~/Maildir")
129
             (ssl-cert (string-append
130
                         "</etc/letsencrypt/live/" domain "/fullchain.pem"))
131
             (ssl-key  (string-append
132
                         "</etc/letsencrypt/live/" domain "/privkey.pem")))))
133
134
(define (lepiller-smtp-service interface domain)
135
  (service opensmtpd-service-type
136
           (opensmtpd-configuration
137
             (config-file (opensmtpd-conf interface domain)))))
138
139
(define (lepiller-dkim-service domain)
140
  (service dkimproxy-out-service-type
141
           (dkimproxy-out-configuration
142
             (listen "127.0.0.1:10027")
143
             (relay "127.0.0.1:10028")
144
             (sender-map
145
               `((,domain
146
                  (,(dkimproxy-out-signature-configuration
147
                      (type 'dkim)
148
                      (key "/etc/mail/dkim/private.key")
149
                      (algorithm "rsa-sha256")
150
                      (method "relaxed")
151
                      (selector "dkim"))
152
                   ,(dkimproxy-out-signature-configuration
153
                      (type 'domainkeys)
154
                      (method "nofws")))))))))
155
156
(define (lepiller-rspamd-service)
157
  (service rspamd-service-type))
158
159
(define* (lepiller-mail-services #:key interface domain)
160
  (list
161
    (lepiller-smtp-service interface domain)
162
    (lepiller-imap-service domain)
163
    (lepiller-dkim-service domain)
164
    (lepiller-rspamd-service)))
165
166