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