]> git.openstreetmap.org Git - chef.git/blob - cookbooks/exim/recipes/default.rb
a74e4d6a508693c77a71b7ef0df74537a6a55ab4
[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 file "/etc/exim4/blocked-senders" do
74   owner "root"
75   group "Debian-exim"
76   mode 0o644
77 end
78
79 template "/etc/exim4/exim4.conf" do
80   source "exim4.conf.erb"
81   owner "root"
82   group "Debian-exim"
83   mode 0o644
84   variables :relay_to_domains => relay_to_domains.sort,
85             :relay_from_hosts => relay_from_hosts.sort
86   notifies :restart, "service[exim4]"
87 end
88
89 search(:accounts, "*:*").each do |account|
90   name = account["id"]
91   details = node[:accounts][:users][name] || {}
92
93   if details[:status] && account["email"]
94     node.default[:exim][:aliases][name] = account["email"]
95   end
96 end
97
98 if node[:exim][:private_aliases]
99   aliases = data_bag_item("exim", "aliases")
100
101   aliases[node[:exim][:private_aliases]].each do |name, address|
102     node.default[:exim][:aliases][name] = address
103   end
104 end
105
106 template "/etc/aliases" do
107   source "aliases.erb"
108   owner "root"
109   group "root"
110   mode 0o644
111 end
112
113 remote_directory "/etc/exim4/noreply" do
114   source "noreply"
115   owner "root"
116   group "Debian-exim"
117   mode 0o755
118   files_owner "root"
119   files_group "Debian-exim"
120   files_mode 0o755
121   purge true
122 end
123
124 munin_plugin "exim_mailqueue"
125 munin_plugin "exim_mailstats"
126
127 if node[:exim][:smarthost_name]
128   node[:exim][:daemon_smtp_ports].each do |port|
129     firewall_rule "accept-inbound-smtp-#{port}" do
130       action :accept
131       source "net"
132       dest "fw"
133       proto "tcp:syn"
134       dest_ports port
135       source_ports "1024:"
136     end
137   end
138 else
139   node[:exim][:daemon_smtp_ports].each do |port|
140     firewall_rule "accept-inbound-smtp-#{port}" do
141       action :accept
142       source "bm:mail.openstreetmap.org"
143       dest "fw"
144       proto "tcp:syn"
145       dest_ports port
146       source_ports "1024:"
147     end
148   end
149 end
150
151 if node[:exim][:smarthost_via] # ~FC023
152   firewall_rule "deny-outbound-smtp" do
153     action :reject
154     source "fw"
155     dest "net"
156     proto "tcp:syn"
157     dest_ports "smtp"
158   end
159 end