Merge 0.6->0.7
[prosody.git] / core / certmanager.lua
1 local configmanager = require "core.configmanager";
2 local log = require "util.logger".init("certmanager");
3 local ssl = ssl;
4 local ssl_newcontext = ssl and ssl.newcontext;
5
6 local setmetatable, tostring = setmetatable, tostring;
7
8 local prosody = prosody;
9
10 module "certmanager"
11
12 -- These are the defaults if not overridden in the config
13 local default_ssl_ctx = { mode = "client", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none", options = "no_sslv2"; };
14 local default_ssl_ctx_in = { mode = "server", protocol = "sslv23", capath = "/etc/ssl/certs", verify = "none", options = "no_sslv2"; };
15
16 local default_ssl_ctx_mt = { __index = default_ssl_ctx };
17 local default_ssl_ctx_in_mt = { __index = default_ssl_ctx_in };
18
19 -- Global SSL options if not overridden per-host
20 local default_ssl_config = configmanager.get("*", "core", "ssl");
21
22 function create_context(host, mode, config)
23         local ssl_config = config and config.core.ssl or default_ssl_config;
24         if ssl and ssl_config then
25                 local ctx, err = ssl_newcontext(setmetatable(ssl_config, mode == "client" and default_ssl_ctx_mt or default_ssl_ctx_in_mt));
26                 if not ctx then
27                         err = err or "invalid ssl config"
28                         local file = err:match("^error loading (.-) %(");
29                         if file then
30                                 if file == "private key" then
31                                         file = ssl_config.key or "your private key";
32                                 elseif file == "certificate" then
33                                         file = ssl_config.certificate or "your certificate file";
34                                 end
35                                 local reason = err:match("%((.+)%)$") or "some reason";
36                                 if reason == "Permission denied" then
37                                         reason = "Check that the permissions allow Prosody to read this file.";
38                                 elseif reason == "No such file or directory" then
39                                         reason = "Check that the path is correct, and the file exists.";
40                                 elseif reason == "system lib" then
41                                         reason = "Previous error (see logs), or other system error.";
42                                 elseif reason == "(null)" or not reason then
43                                         reason = "Check that the file exists and the permissions are correct";
44                                 else
45                                         reason = "Reason: "..tostring(reason):lower();
46                                 end
47                                 log("error", "SSL/TLS: Failed to load %s: %s", file, reason);
48                         else
49                                 log("error", "SSL/TLS: Error initialising for host %s: %s", host, err );
50                         end
51                 end
52                 return ctx, err;
53         elseif not ssl then
54                 return nil, "LuaSec (required for encryption) was not found";
55         end
56         return nil, "No SSL/TLS configuration present for "..host;
57 end
58
59 function reload_ssl_config()
60         default_ssl_config = configmanager.get("*", "core", "ssl");
61 end
62
63 prosody.events.add_handler("config-reloaded", reload_ssl_config);
64
65 return _M;