]> git.openstreetmap.org Git - chef.git/blob - cookbooks/exim/recipes/default.rb
Merge remote-tracking branch 'github/pull/350' into master
[chef.git] / cookbooks / exim / recipes / default.rb
1 #
2 # Cookbook:: exim
3 # Recipe:: default
4 #
5 # Copyright:: 2011, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 include_recipe "munin"
21 include_recipe "networking"
22 include_recipe "prometheus"
23
24 package %w[
25   exim4
26   openssl
27   ssl-cert
28 ]
29
30 package "exim4-daemon-heavy" do
31   only_if { ::File.exist?("/var/run/clamav/clamd.ctl") }
32 end
33
34 group "ssl-cert" do
35   action :modify
36   members "Debian-exim"
37   append true
38 end
39
40 if node[:exim][:certificate_names]
41   include_recipe "apache"
42
43   apache_site node[:exim][:certificate_names].first do
44     template "apache.erb"
45     variables :aliases => node[:exim][:certificate_names].drop(1)
46   end
47
48   ssl_certificate node[:exim][:certificate_names].first do
49     domains node[:exim][:certificate_names]
50     notifies :restart, "service[exim4]"
51   end
52 else
53   openssl_x509_certificate "/etc/ssl/certs/exim.pem" do
54     key_file "/etc/ssl/private/exim.key"
55     owner "root"
56     group "ssl-cert"
57     mode "640"
58     org "OpenStreetMap"
59     email "postmaster@openstreetmap.org"
60     common_name node[:fqdn]
61     expire 3650
62     notifies :restart, "service[exim4]"
63   end
64 end
65
66 service "exim4" do
67   action [:enable, :start]
68   supports :status => true, :restart => true, :reload => true
69 end
70
71 relay_to_domains = node[:exim][:relay_to_domains]
72
73 node[:exim][:routes].each_value do |route|
74   relay_to_domains |= route[:domains] if route[:host]
75 end
76
77 relay_from_hosts = node[:exim][:relay_from_hosts]
78
79 if node[:exim][:smarthost_name]
80   search(:node, "roles:gateway") do |gateway|
81     allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
82       "#{interface[:network]}/#{interface[:prefix]}"
83     end
84
85     node.default[:networking][:wireguard][:peers] << {
86       :public_key => gateway[:networking][:wireguard][:public_key],
87       :allowed_ips => allowed_ips,
88       :endpoint => "#{gateway.name}:51820"
89     }
90   end
91
92   search(:node, "exim_smarthost_via:#{node[:exim][:smarthost_name]}\\:*").each do |host|
93     relay_from_hosts |= host.ipaddresses(:role => :external)
94   end
95
96   domains = node[:exim][:certificate_names].select { |c| c =~ /^a\.mx\./ }.collect { |c| c.sub(/^a\.mx./, "") }
97   primary_domain = domains.first
98
99   directory "/srv/mta-sts.#{primary_domain}" do
100     owner "root"
101     group "root"
102     mode "755"
103   end
104
105   domains.each do |domain|
106     template "/srv/mta-sts.#{primary_domain}/#{domain}.txt" do
107       source "mta-sts.erb"
108       owner "root"
109       group "root"
110       mode "644"
111       variables :domain => domain
112     end
113   end
114
115   ssl_certificate "mta-sts.#{primary_domain}" do
116     domains domains.collect { |d| "mta-sts.#{d}" }
117     notifies :reload, "service[apache2]"
118   end
119
120   apache_site "mta-sts.#{primary_domain}" do
121     template "apache-mta-sts.erb"
122     directory "/srv/mta-sts.#{primary_domain}"
123     variables :domains => domains
124   end
125 end
126
127 file "/etc/exim4/blocked-senders" do
128   owner "root"
129   group "Debian-exim"
130   mode "644"
131 end
132
133 if node[:exim][:dkim_selectors]
134   keys = data_bag_item("exim", "dkim")
135
136   template "/etc/exim4/dkim-domains" do
137     owner "root"
138     source "dkim-domains.erb"
139     group "Debian-exim"
140     mode "644"
141   end
142
143   template "/etc/exim4/dkim-selectors" do
144     owner "root"
145     source "dkim-selectors.erb"
146     group "Debian-exim"
147     mode "644"
148   end
149
150   directory "/etc/exim4/dkim-keys" do
151     owner "root"
152     group "Debian-exim"
153     mode "755"
154   end
155
156   node[:exim][:dkim_selectors].each do |domain, _selector|
157     file "/etc/exim4/dkim-keys/#{domain}" do
158       content keys[domain].join("\n")
159       owner "root"
160       group "Debian-exim"
161       mode "640"
162     end
163   end
164 end
165
166 template "/etc/exim4/exim4.conf" do
167   source "exim4.conf.erb"
168   owner "root"
169   group "Debian-exim"
170   mode "644"
171   variables :relay_to_domains => relay_to_domains.sort,
172             :relay_from_hosts => relay_from_hosts.sort
173   notifies :restart, "service[exim4]"
174 end
175
176 search(:accounts, "*:*").each do |account|
177   name = account["id"]
178   details = node[:accounts][:users][name] || {}
179
180   if details[:status] && account["email"]
181     node.default[:exim][:aliases][name] = account["email"]
182   end
183 end
184
185 if node[:exim][:private_aliases]
186   aliases = data_bag_item("exim", "aliases")
187
188   aliases[node[:exim][:private_aliases]].each do |name, address|
189     node.default[:exim][:aliases][name] = address
190   end
191 end
192
193 template "/etc/aliases" do
194   source "aliases.erb"
195   owner "root"
196   group "root"
197   mode "644"
198 end
199
200 remote_directory "/etc/exim4/noreply" do
201   source "noreply"
202   owner "root"
203   group "Debian-exim"
204   mode "755"
205   files_owner "root"
206   files_group "Debian-exim"
207   files_mode "755"
208   purge true
209 end
210
211 munin_plugin "exim_mailqueue"
212 munin_plugin "exim_mailstats"
213
214 prometheus_exporter "exim" do
215   port 9636
216 end
217
218 if node[:exim][:smarthost_name]
219   node[:exim][:daemon_smtp_ports].each do |port|
220     firewall_rule "accept-inbound-smtp-#{port}" do
221       action :accept
222       source "net"
223       dest "fw"
224       proto "tcp:syn"
225       dest_ports port
226       source_ports "1024:"
227     end
228   end
229 else
230   node[:exim][:daemon_smtp_ports].each do |port|
231     firewall_rule "accept-inbound-smtp-#{port}" do
232       action :accept
233       source "bm:mail.openstreetmap.org"
234       dest "fw"
235       proto "tcp:syn"
236       dest_ports port
237       source_ports "1024:"
238     end
239   end
240 end
241
242 if node[:exim][:smarthost_via]
243   firewall_rule "deny-outbound-smtp" do
244     action :reject
245     source "fw"
246     dest "net"
247     proto "tcp:syn"
248     dest_ports "smtp"
249   end
250 end