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