]> git.openstreetmap.org Git - chef.git/blob - cookbooks/exim/recipes/default.rb
Use string for file modes in remote_directory resources
[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, "exim_smarthost_via:#{node[:exim][:smarthost_name]}\\:*").each do |host|
80     relay_from_hosts |= host.ipaddresses(:role => :external)
81   end
82
83   domains = node[:exim][:certificate_names].select { |c| c =~ /^a\.mx\./ }.collect { |c| c.sub(/^a\.mx./, "") }
84   primary_domain = domains.first
85
86   directory "/srv/mta-sts.#{primary_domain}" do
87     owner "root"
88     group "root"
89     mode "755"
90   end
91
92   domains.each do |domain|
93     template "/srv/mta-sts.#{primary_domain}/#{domain}.txt" do
94       source "mta-sts.erb"
95       owner "root"
96       group "root"
97       mode "644"
98       variables :domain => domain
99     end
100   end
101
102   ssl_certificate "mta-sts.#{primary_domain}" do
103     domains domains.collect { |d| "mta-sts.#{d}" }
104     notifies :reload, "service[apache2]"
105   end
106
107   apache_site "mta-sts.#{primary_domain}" do
108     template "apache-mta-sts.erb"
109     directory "/srv/mta-sts.#{primary_domain}"
110     variables :domains => domains
111   end
112 end
113
114 file "/etc/exim4/blocked-senders" do
115   owner "root"
116   group "Debian-exim"
117   mode "644"
118 end
119
120 if node[:exim][:dkim_selectors]
121   keys = data_bag_item("exim", "dkim")
122
123   template "/etc/exim4/dkim-domains" do
124     owner "root"
125     source "dkim-domains.erb"
126     group "Debian-exim"
127     mode "644"
128   end
129
130   template "/etc/exim4/dkim-selectors" do
131     owner "root"
132     source "dkim-selectors.erb"
133     group "Debian-exim"
134     mode "644"
135   end
136
137   directory "/etc/exim4/dkim-keys" do
138     owner "root"
139     group "Debian-exim"
140     mode "755"
141   end
142
143   node[:exim][:dkim_selectors].each do |domain, _selector|
144     file "/etc/exim4/dkim-keys/#{domain}" do
145       content keys[domain].join("\n")
146       owner "root"
147       group "Debian-exim"
148       mode "640"
149     end
150   end
151 end
152
153 template "/etc/exim4/exim4.conf" do
154   source "exim4.conf.erb"
155   owner "root"
156   group "Debian-exim"
157   mode "644"
158   variables :relay_to_domains => relay_to_domains.sort,
159             :relay_from_hosts => relay_from_hosts.sort
160   notifies :restart, "service[exim4]"
161 end
162
163 search(:accounts, "*:*").each do |account|
164   name = account["id"]
165   details = node[:accounts][:users][name] || {}
166
167   if details[:status] && account["email"]
168     node.default[:exim][:aliases][name] = account["email"]
169   end
170 end
171
172 if node[:exim][:private_aliases]
173   aliases = data_bag_item("exim", "aliases")
174
175   aliases[node[:exim][:private_aliases]].each do |name, address|
176     node.default[:exim][:aliases][name] = address
177   end
178 end
179
180 template "/etc/aliases" do
181   source "aliases.erb"
182   owner "root"
183   group "root"
184   mode "644"
185 end
186
187 remote_directory "/etc/exim4/noreply" do
188   source "noreply"
189   owner "root"
190   group "Debian-exim"
191   mode "755"
192   files_owner "root"
193   files_group "Debian-exim"
194   files_mode "755"
195   purge true
196 end
197
198 munin_plugin "exim_mailqueue"
199 munin_plugin "exim_mailstats"
200
201 if node[:exim][:smarthost_name]
202   node[:exim][:daemon_smtp_ports].each do |port|
203     firewall_rule "accept-inbound-smtp-#{port}" do
204       action :accept
205       source "net"
206       dest "fw"
207       proto "tcp:syn"
208       dest_ports port
209       source_ports "1024:"
210     end
211   end
212 else
213   node[:exim][:daemon_smtp_ports].each do |port|
214     firewall_rule "accept-inbound-smtp-#{port}" do
215       action :accept
216       source "bm:mail.openstreetmap.org"
217       dest "fw"
218       proto "tcp:syn"
219       dest_ports port
220       source_ports "1024:"
221     end
222   end
223 end
224
225 if node[:exim][:smarthost_via]
226   firewall_rule "deny-outbound-smtp" do
227     action :reject
228     source "fw"
229     dest "net"
230     proto "tcp:syn"
231     dest_ports "smtp"
232   end
233 end