]> git.openstreetmap.org Git - chef.git/blob - cookbooks/exim/recipes/default.rb
65ab8342fd2f068cfb7bb7933627c021e3f45f1c
[chef.git] / cookbooks / exim / recipes / default.rb
1 #
2 # Cookbook Name:: 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 template "/tmp/exim.ssl.cnf" do
37   source "ssl.cnf.erb"
38   owner "root"
39   group "root"
40   mode 0o644
41   not_if do
42     File.exist?("/etc/ssl/certs/exim.pem") && File.exist?("/etc/ssl/private/exim.key")
43   end
44 end
45
46 execute "/etc/ssl/certs/exim.pem" do
47   command "openssl req -x509 -newkey rsa:2048 -keyout /etc/ssl/private/exim.key -out /etc/ssl/certs/exim.pem -days 3650 -nodes -config /tmp/exim.ssl.cnf"
48   user "root"
49   group "ssl-cert"
50   not_if do
51     File.exist?("/etc/ssl/certs/exim.pem") && File.exist?("/etc/ssl/private/exim.key")
52   end
53 end
54
55 service "exim4" do
56   action [:enable, :start]
57   supports :status => true, :restart => true, :reload => true
58   subscribes :restart, "execute[/etc/ssl/certs/exim.pem]"
59 end
60
61 relay_to_domains = node[:exim][:relay_to_domains]
62
63 node[:exim][:routes].each_value do |route|
64   relay_to_domains |= route[:domains] if route[:host]
65 end
66
67 relay_from_hosts = node[:exim][:relay_from_hosts]
68
69 if node[:exim][:smarthost_name]
70   search(:node, "exim_smarthost_via:#{node[:exim][:smarthost_name]}\\:*").each do |host|
71     relay_from_hosts |= host.ipaddresses(:role => :external)
72   end
73 end
74
75 file "/etc/exim4/blocked-senders" do
76   owner "root"
77   group "Debian-exim"
78   mode 0o644
79 end
80
81 template "/etc/exim4/exim4.conf" do
82   source "exim4.conf.erb"
83   owner "root"
84   group "Debian-exim"
85   mode 0o644
86   variables :relay_to_domains => relay_to_domains.sort,
87             :relay_from_hosts => relay_from_hosts.sort
88   notifies :restart, "service[exim4]"
89 end
90
91 search(:accounts, "*:*").each do |account|
92   name = account["id"]
93   details = node[:accounts][:users][name] || {}
94
95   if details[:status] && account["email"]
96     node.default[:exim][:aliases][name] = account["email"]
97   end
98 end
99
100 if node[:exim][:private_aliases]
101   aliases = data_bag_item("exim", "aliases")
102
103   aliases[node[:exim][:private_aliases]].each do |name, address|
104     node.default[:exim][:aliases][name] = address
105   end
106 end
107
108 template "/etc/aliases" do
109   source "aliases.erb"
110   owner "root"
111   group "root"
112   mode 0o644
113 end
114
115 remote_directory "/etc/exim4/noreply" do
116   source "noreply"
117   owner "root"
118   group "Debian-exim"
119   mode 0o755
120   files_owner "root"
121   files_group "Debian-exim"
122   files_mode 0o755
123   purge true
124 end
125
126 munin_plugin "exim_mailqueue"
127 munin_plugin "exim_mailstats"
128
129 if node[:exim][:smarthost_name]
130   node[:exim][:daemon_smtp_ports].each do |port|
131     firewall_rule "accept-inbound-smtp-#{port}" do
132       action :accept
133       source "net"
134       dest "fw"
135       proto "tcp:syn"
136       dest_ports port
137       source_ports "1024:"
138     end
139   end
140 else
141   node[:exim][:daemon_smtp_ports].each do |port|
142     firewall_rule "accept-inbound-smtp-#{port}" do
143       action :accept
144       source "bm:mail.openstreetmap.org"
145       dest "fw"
146       proto "tcp:syn"
147       dest_ports port
148       source_ports "1024:"
149     end
150   end
151 end
152
153 if node[:exim][:smarthost_via] # ~FC023
154   firewall_rule "deny-outbound-smtp" do
155     action :reject
156     source "fw"
157     dest "net"
158     proto "tcp:syn"
159     dest_ports "smtp"
160   end
161 end