xmlhandlers: Don't restrict CDATA
[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                         ssl = false
52                 end
53                 return ctx, err;
54         end
55         return nil;
56 end
57
58 function reload_ssl_config()
59         default_ssl_config = configmanager.get("*", "core", "ssl");
60 end
61
62 prosody.events.add_handler("config-reloaded", reload_ssl_config);
63
64 return _M;