]> git.openstreetmap.org Git - chef.git/blob - cookbooks/exim/recipes/default.rb
Use self signed certificates for exim
[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
25 if File.exist?("/var/run/clamav/clamd.ctl")
26   package "exim4-daemon-heavy"
27 end
28
29 group "ssl-cert" do
30   action :modify
31   members "Debian-exim"
32   append true
33 end
34
35 execute "/etc/ssl/certs/exim.pem" do
36   command "openssl req -x509 -newkey rsa:2048 -keyout /etc/ssl/private/exim.key -out /etc/ssl/certs/exim.pem -days 3650 -nodes -subj='/O=OpenStreetMap/CN=#{node[:name]}'"
37   user "root"
38   group "ssl-cert"
39   not_if do
40     File.exists?("/etc/ssl/certs/exim.pem") && File.exists?("/etc/ssl/private/exim.key")
41   end
42 end
43
44 service "exim4" do
45   action [ :enable, :start ]
46   supports :status => true, :restart => true, :reload => true
47   subscribes :restart, "execute[/etc/ssl/certs/exim.pem]"
48 end
49
50 relay_to_domains = node[:exim][:relay_to_domains]
51
52 node[:exim][:routes].each_value do |route|
53   relay_to_domains = relay_to_domains | route[:domains]
54 end
55
56 relay_from_hosts = node[:exim][:relay_from_hosts]
57
58 if node[:exim][:smarthost_name]
59   search(:node, "exim_smarthost_via:#{node[:exim][:smarthost_name]}\\:*").each do |host|
60     relay_from_hosts = relay_from_hosts | host.ipaddresses(:role => :external)
61   end
62 end
63
64 template "/etc/exim4/exim4.conf" do
65   source "exim4.conf.erb"
66   owner "root"
67   group "Debian-exim"
68   mode 0644
69   variables :relay_to_domains => relay_to_domains.sort,
70             :relay_from_hosts => relay_from_hosts.sort
71   notifies :restart, resources(:service => "exim4")
72 end
73
74 search(:accounts, "*:*").each do |account|
75   name = account["id"]
76   details = node[:accounts][:users][name] || {}
77
78   if details[:status] and account["email"]
79     node.default[:exim][:aliases][name] = account["email"]
80   end
81 end
82
83 if node[:exim][:private_aliases]
84   aliases = data_bag_item("exim", "aliases")
85
86   aliases[node[:exim][:private_aliases]].each do |name,address|
87     node.default[:exim][:aliases][name] = address
88   end
89 end
90
91 template "/etc/aliases" do
92   source "aliases.erb"
93   owner "root"
94   group "root"
95   mode 0644
96 end
97
98 remote_directory "/etc/exim4/noreply" do
99   source "noreply"
100   owner "root"
101   group "Debian-exim"
102   mode 0755
103   files_owner "root"
104   files_group "Debian-exim"
105   files_mode 0755
106   purge true
107 end
108
109 munin_plugin "exim_mailqueue"
110 munin_plugin "exim_mailstats"
111
112 if not relay_to_domains.empty? or not node[:exim][:local_domains].empty?
113   node[:exim][:daemon_smtp_ports].each do |port|
114     firewall_rule "accept-inbound-smtp-#{port}" do
115       action :accept
116       source "net"
117       dest "fw"
118       proto "tcp:syn"
119       dest_ports port
120       source_ports "1024:"
121     end
122   end
123 end
124
125 if node[:exim][:smarthost_via]
126   firewall_rule "deny-outbound-smtp" do
127     action :reject
128     source "fw"
129     dest "net"
130     proto "tcp:syn"
131     dest_ports "smtp"
132   end
133 end