Merge 0.7->0.8
[prosody.git] / core / certmanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local configmanager = require "core.configmanager";
10 local log = require "util.logger".init("certmanager");
11 local ssl = ssl;
12 local ssl_newcontext = ssl and ssl.newcontext;
13
14 local setmetatable, tostring = setmetatable, tostring;
15
16 local prosody = prosody;
17 local resolve_path = configmanager.resolve_relative_path;
18 local config_path = prosody.paths.config;
19
20 module "certmanager"
21
22 -- Global SSL options if not overridden per-host
23 local default_ssl_config = configmanager.get("*", "core", "ssl");
24 local default_capath = "/etc/ssl/certs";
25
26 function create_context(host, mode, user_ssl_config)
27         user_ssl_config = user_ssl_config or default_ssl_config;
28
29         if not ssl then return nil, "LuaSec (required for encryption) was not found"; end
30         if not user_ssl_config then return nil, "No SSL/TLS configuration present for "..host; end
31         
32         local ssl_config = {
33                 mode = mode;
34                 protocol = user_ssl_config.protocol or "sslv23";
35                 key = resolve_path(config_path, user_ssl_config.key);
36                 password = user_ssl_config.password;
37                 certificate = resolve_path(config_path, user_ssl_config.certificate);
38                 capath = resolve_path(config_path, user_ssl_config.capath or default_capath);
39                 cafile = resolve_path(config_path, user_ssl_config.cafile);
40                 verify = user_ssl_config.verify or "none";
41                 options = user_ssl_config.options or "no_sslv2";
42                 ciphers = user_ssl_config.ciphers;
43                 depth = user_ssl_config.depth;
44         };
45
46         local ctx, err = ssl_newcontext(ssl_config);
47         if not ctx then
48                 err = err or "invalid ssl config"
49                 local file = err:match("^error loading (.-) %(");
50                 if file then
51                         if file == "private key" then
52                                 file = ssl_config.key or "your private key";
53                         elseif file == "certificate" then
54                                 file = ssl_config.certificate or "your certificate file";
55                         end
56                         local reason = err:match("%((.+)%)$") or "some reason";
57                         if reason == "Permission denied" then
58                                 reason = "Check that the permissions allow Prosody to read this file.";
59                         elseif reason == "No such file or directory" then
60                                 reason = "Check that the path is correct, and the file exists.";
61                         elseif reason == "system lib" then
62                                 reason = "Previous error (see logs), or other system error.";
63                         elseif reason == "(null)" or not reason then
64                                 reason = "Check that the file exists and the permissions are correct";
65                         else
66                                 reason = "Reason: "..tostring(reason):lower();
67                         end
68                         log("error", "SSL/TLS: Failed to load %s: %s", file, reason);
69                 else
70                         log("error", "SSL/TLS: Error initialising for host %s: %s", host, err );
71                 end
72         end
73         return ctx, err;
74 end
75
76 function reload_ssl_config()
77         default_ssl_config = configmanager.get("*", "core", "ssl");
78 end
79
80 prosody.events.add_handler("config-reloaded", reload_ssl_config);
81
82 return _M;