mod_storage_sql: Add method for listing stores
[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 tostring = tostring;
15
16 local prosody = prosody;
17 local resolve_path = configmanager.resolve_relative_path;
18 local config_path = prosody.paths.config;
19
20 local luasec_has_noticket;
21 if ssl then
22         local luasec_major, luasec_minor = ssl._VERSION:match("^(%d+)%.(%d+)");
23         luasec_has_noticket = tonumber(luasec_major)>0 or tonumber(luasec_minor)>=4;
24 end
25
26 module "certmanager"
27
28 -- Global SSL options if not overridden per-host
29 local default_ssl_config = configmanager.get("*", "core", "ssl");
30 local default_capath = "/etc/ssl/certs";
31 local default_verify = (ssl and ssl.x509 and { "peer", "client_once", "continue", "ignore_purpose" }) or "none";
32 local default_options = { "no_sslv2", luasec_has_noticket and "no_ticket" or nil };
33
34 function create_context(host, mode, user_ssl_config)
35         user_ssl_config = user_ssl_config or default_ssl_config;
36
37         if not ssl then return nil, "LuaSec (required for encryption) was not found"; end
38         if not user_ssl_config then return nil, "No SSL/TLS configuration present for "..host; end
39         
40         local ssl_config = {
41                 mode = mode;
42                 protocol = user_ssl_config.protocol or "sslv23";
43                 key = resolve_path(config_path, user_ssl_config.key);
44                 password = user_ssl_config.password or function() log("error", "Encrypted certificate for %s requires 'ssl' 'password' to be set in config", host); end;
45                 certificate = resolve_path(config_path, user_ssl_config.certificate);
46                 capath = resolve_path(config_path, user_ssl_config.capath or default_capath);
47                 cafile = resolve_path(config_path, user_ssl_config.cafile);
48                 verify = user_ssl_config.verify or default_verify;
49                 options = user_ssl_config.options or default_options;
50                 depth = user_ssl_config.depth;
51         };
52
53         local ctx, err = ssl_newcontext(ssl_config);
54
55         -- LuaSec ignores the cipher list from the config, so we have to take care
56         -- of it ourselves (W/A for #x)
57         if ctx and user_ssl_config.ciphers then
58                 local success;
59                 success, err = ssl.context.setcipher(ctx, user_ssl_config.ciphers);
60                 if not success then ctx = nil; end
61         end
62
63         if not ctx then
64                 err = err or "invalid ssl config"
65                 local file = err:match("^error loading (.-) %(");
66                 if file then
67                         if file == "private key" then
68                                 file = ssl_config.key or "your private key";
69                         elseif file == "certificate" then
70                                 file = ssl_config.certificate or "your certificate file";
71                         end
72                         local reason = err:match("%((.+)%)$") or "some reason";
73                         if reason == "Permission denied" then
74                                 reason = "Check that the permissions allow Prosody to read this file.";
75                         elseif reason == "No such file or directory" then
76                                 reason = "Check that the path is correct, and the file exists.";
77                         elseif reason == "system lib" then
78                                 reason = "Previous error (see logs), or other system error.";
79                         elseif reason == "(null)" or not reason then
80                                 reason = "Check that the file exists and the permissions are correct";
81                         else
82                                 reason = "Reason: "..tostring(reason):lower();
83                         end
84                         log("error", "SSL/TLS: Failed to load '%s': %s (for %s)", file, reason, host);
85                 else
86                         log("error", "SSL/TLS: Error initialising for %s: %s", host, err);
87                 end
88         end
89         return ctx, err;
90 end
91
92 function reload_ssl_config()
93         default_ssl_config = configmanager.get("*", "core", "ssl");
94 end
95
96 prosody.events.add_handler("config-reloaded", reload_ssl_config);
97
98 return _M;