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