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