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