]> git.openstreetmap.org Git - chef.git/blob - cookbooks/exim/recipes/default.rb
Allow all mail relays to relay from any host
[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   mailutils
29 ]
30
31 package "exim4-daemon-heavy" do
32   only_if { ::File.exist?("/var/run/clamav/clamd.ctl") }
33 end
34
35 group "Debian-exim" do
36   action :modify
37   members "clamav"
38   append true
39   only_if { ::File.exist?("/var/run/clamav/clamd.ctl") }
40 end
41
42 group "ssl-cert" do
43   action :modify
44   members "Debian-exim"
45   append true
46 end
47
48 if node[:exim][:certificate_names]
49   include_recipe "apache"
50
51   apache_site node[:exim][:certificate_names].first do
52     template "apache.erb"
53     variables :aliases => node[:exim][:certificate_names].drop(1)
54   end
55
56   ssl_certificate node[:exim][:certificate_names].first do
57     domains node[:exim][:certificate_names]
58     notifies :restart, "service[exim4]"
59   end
60 else
61   openssl_x509_certificate "/etc/ssl/certs/exim.pem" do
62     key_file "/etc/ssl/private/exim.key"
63     owner "root"
64     group "ssl-cert"
65     mode "640"
66     org "OpenStreetMap"
67     email "postmaster@openstreetmap.org"
68     common_name node[:fqdn]
69     expire 3650
70     notifies :restart, "service[exim4]"
71   end
72 end
73
74 service "exim4" do
75   action [:enable, :start]
76   supports :status => true, :restart => true, :reload => true
77 end
78
79 relay_to_domains = node[:exim][:relay_to_domains]
80
81 node[:exim][:routes].each_value do |route|
82   relay_to_domains |= route[:domains] if route[:host]
83 end
84
85 relay_from_hosts = node[:exim][:relay_from_hosts]
86
87 if node[:exim][:smarthost_name]
88   search(:node, "exim_smarthost_via:*?").each do |host|
89     relay_from_hosts |= host.ipaddresses(:role => :external)
90   end
91
92   domains = node[:exim][:certificate_names].select { |c| c =~ /^a\.mx\./ }.collect { |c| c.sub(/^a\.mx./, "") }
93   primary_domain = domains.first
94
95   directory "/srv/mta-sts.#{primary_domain}" do
96     owner "root"
97     group "root"
98     mode "755"
99   end
100
101   domains.each do |domain|
102     template "/srv/mta-sts.#{primary_domain}/#{domain}.txt" do
103       source "mta-sts.erb"
104       owner "root"
105       group "root"
106       mode "644"
107       variables :domain => domain
108     end
109   end
110
111   ssl_certificate "mta-sts.#{primary_domain}" do
112     domains domains.collect { |d| "mta-sts.#{d}" }
113     notifies :reload, "service[apache2]"
114   end
115
116   apache_site "mta-sts.#{primary_domain}" do
117     template "apache-mta-sts.erb"
118     directory "/srv/mta-sts.#{primary_domain}"
119     variables :domains => domains
120   end
121 end
122
123 file "/etc/exim4/blocked-senders" do
124   owner "root"
125   group "Debian-exim"
126   mode "644"
127 end
128
129 if node[:exim][:dkim_selectors]
130   keys = data_bag_item("exim", "dkim")
131
132   template "/etc/exim4/dkim-domains" do
133     owner "root"
134     source "dkim-domains.erb"
135     group "Debian-exim"
136     mode "644"
137   end
138
139   template "/etc/exim4/dkim-selectors" do
140     owner "root"
141     source "dkim-selectors.erb"
142     group "Debian-exim"
143     mode "644"
144   end
145
146   directory "/etc/exim4/dkim-keys" do
147     owner "root"
148     group "Debian-exim"
149     mode "755"
150   end
151
152   node[:exim][:dkim_selectors].each do |domain, _selector|
153     file "/etc/exim4/dkim-keys/#{domain}" do
154       content keys[domain].join("\n")
155       owner "root"
156       group "Debian-exim"
157       mode "640"
158     end
159   end
160 end
161
162 template "/etc/default/exim4" do
163   source "default.erb"
164   owner "root"
165   group "root"
166   mode "044"
167   notifies :restart, "service[exim4]"
168 end
169
170 template "/etc/exim4/exim4.conf" do
171   source "exim4.conf.erb"
172   owner "root"
173   group "Debian-exim"
174   mode "644"
175   variables :relay_to_domains => relay_to_domains.sort,
176             :relay_from_hosts => relay_from_hosts.sort
177   notifies :restart, "service[exim4]"
178 end
179
180 search(:accounts, "*:*").each do |account|
181   name = account["id"]
182   details = node[:accounts][:users][name] || {}
183
184   if details[:status] && account["email"]
185     node.default[:exim][:aliases][name] = account["email"]
186   end
187 end
188
189 if node[:exim][:private_aliases]
190   aliases = data_bag_item("exim", "aliases")
191
192   aliases[node[:exim][:private_aliases]].each do |name, address|
193     node.default[:exim][:aliases][name] = address
194   end
195 end
196
197 template "/etc/aliases" do
198   source "aliases.erb"
199   owner "root"
200   group "root"
201   mode "644"
202 end
203
204 remote_directory "/etc/exim4/noreply" do
205   source "noreply"
206   owner "root"
207   group "Debian-exim"
208   mode "755"
209   files_owner "root"
210   files_group "Debian-exim"
211   files_mode "755"
212   purge true
213 end
214
215 template "/etc/mail.rc" do
216   source "mail.rc.erb"
217   owner "root"
218   group "root"
219   mode "644"
220 end
221
222 munin_plugin "exim_mailqueue"
223 munin_plugin "exim_mailstats"
224
225 prometheus_exporter "exim" do
226   port 9636
227   user "Debian-exim"
228   protect_proc "default"
229 end
230
231 if node[:exim][:smarthost_name]
232   node[:exim][:daemon_smtp_ports].each do |port|
233     firewall_rule "accept-inbound-smtp-#{port}" do
234       action :accept
235       source "net"
236       dest "fw"
237       proto "tcp:syn"
238       dest_ports port
239       source_ports "1024:"
240     end
241   end
242 else
243   node[:exim][:daemon_smtp_ports].each do |port|
244     firewall_rule "accept-inbound-smtp-#{port}" do
245       action :accept
246       source "bm:mail.openstreetmap.org"
247       dest "fw"
248       proto "tcp:syn"
249       dest_ports port
250       source_ports "1024:"
251     end
252   end
253 end
254
255 if node[:exim][:smarthost_via]
256   firewall_rule "deny-outbound-smtp" do
257     action :reject
258     source "fw"
259     dest "net"
260     proto "tcp:syn"
261     dest_ports "smtp"
262   end
263 end