usermanager, mod_auth_internal_hashed, mod_legacyauth: New order of parameters for...
[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 = prosody.resolve_relative_path;
18
19 module "certmanager"
20
21 -- Global SSL options if not overridden per-host
22 local default_ssl_config = configmanager.get("*", "core", "ssl");
23 local default_capath = "/etc/ssl/certs";
24
25 function create_context(host, mode, config)
26         local user_ssl_config = config and config.core.ssl or default_ssl_config;
27
28         if not(ssl and user_ssl_config) then return nil; end
29         
30         local ssl_config = {
31                 mode = mode;
32                 protocol = user_ssl_config.protocol or "sslv23";
33                 key = resolve_path(user_ssl_config.key);
34                 password = user_ssl_config.password;
35                 certificate = resolve_path(user_ssl_config.certificate);
36                 capath = resolve_path(user_ssl_config.capath or default_capath);
37                 cafile = resolve_path(user_ssl_config.cafile);
38                 verify = user_ssl_config.verify or "none";
39                 options = user_ssl_config.options or "no_sslv2";
40                 ciphers = user_ssl_config.ciphers;
41                 depth = user_ssl_config.depth;
42         };
43
44         local ctx, err = ssl_newcontext(ssl_config);
45         if not ctx then
46                 err = err or "invalid ssl config"
47                 local file = err:match("^error loading (.-) %(");
48                 if file then
49                         if file == "private key" then
50                                 file = ssl_config.key or "your private key";
51                         elseif file == "certificate" then
52                                 file = ssl_config.certificate or "your certificate file";
53                         end
54                         local reason = err:match("%((.+)%)$") or "some reason";
55                         if reason == "Permission denied" then
56                                 reason = "Check that the permissions allow Prosody to read this file.";
57                         elseif reason == "No such file or directory" then
58                                 reason = "Check that the path is correct, and the file exists.";
59                         elseif reason == "system lib" then
60                                 reason = "Previous error (see logs), or other system error.";
61                         elseif reason == "(null)" or not reason then
62                                 reason = "Check that the file exists and the permissions are correct";
63                         else
64                                 reason = "Reason: "..tostring(reason):lower();
65                         end
66                         log("error", "SSL/TLS: Failed to load %s: %s", file, reason);
67                 else
68                         log("error", "SSL/TLS: Error initialising for host %s: %s", host, err );
69                 end
70                 ssl = false
71         end
72         return ctx, err;
73 end
74
75 function reload_ssl_config()
76         default_ssl_config = configmanager.get("*", "core", "ssl");
77 end
78
79 prosody.events.add_handler("config-reloaded", reload_ssl_config);
80
81 return _M;