Merge 0.10->trunk
[prosody.git] / core / certmanager.lua
index 05f0f809d93dd1e69f313a4010f2ae9c5cb51d47..29a5a6c8403906428bddec299950a79efaf5f506 100644 (file)
@@ -23,8 +23,9 @@ local ssl_context = ssl.context or softreq"ssl.context";
 local ssl_x509 = ssl.x509 or softreq"ssl.x509";
 local ssl_newcontext = ssl.newcontext;
 local new_config = require"util.sslconfig".new;
+local stat = require "lfs".attributes;
 
-local tostring = tostring;
+local tonumber, tostring = tonumber, tostring;
 local pairs = pairs;
 local type = type;
 local io_open = io.open;
@@ -35,7 +36,7 @@ local resolve_path = require"util.paths".resolve_relative_path;
 local config_path = prosody.paths.config;
 
 local luasec_major, luasec_minor = ssl._VERSION:match("^(%d+)%.(%d+)");
-local luasec_version = luasec_major * 100 + luasec_minor;
+local luasec_version = tonumber(luasec_major) * 100 + tonumber(luasec_minor);
 local luasec_has = {
        -- TODO If LuaSec ever starts exposing these things itself, use that instead
        cipher_server_preference = luasec_version >= 2;
@@ -50,6 +51,43 @@ local _ENV = nil;
 -- Global SSL options if not overridden per-host
 local global_ssl_config = configmanager.get("*", "ssl");
 
+local global_certificates = configmanager.get("*", "certificates") or "certs";
+
+local crt_try = { "", "/%s.crt", "/%s/fullchain.pem", "/%s.pem", };
+local key_try = { "", "/%s.key", "/%s/privkey.pem",   "/%s.pem", };
+
+local function find_cert(user_certs, name)
+       local certs = resolve_path(config_path, user_certs or global_certificates);
+       for i = 1, #crt_try do
+               local crt_path = certs .. crt_try[i]:format(name);
+               local key_path = certs .. key_try[i]:format(name);
+
+               if stat(crt_path, "mode") == "file" then
+                       if key_path:sub(-4) == ".crt" then
+                               key_path = key_path:sub(1, -4) .. "key";
+                               if stat(key_path, "mode") == "file" then
+                                       return { certificate = crt_path, key = key_path };
+                               end
+                       elseif stat(key_path, "mode") == "file" then
+                               return { certificate = crt_path, key = key_path };
+                       end
+               end
+       end
+end
+
+local function find_host_cert(host)
+       if not host then return nil; end
+       return find_cert(configmanager.get(host, "certificate"), host) or find_host_cert(host:match("%.(.+)$"));
+end
+
+local function find_service_cert(service, port)
+       local cert_config = configmanager.get("*", service.."_certificate");
+       if type(cert_config) == "table" then
+               cert_config = cert_config[port] or cert_config.default;
+       end
+       return find_cert(cert_config, service);
+end
+
 -- Built-in defaults
 local core_defaults = {
        capath = "/etc/ssl/certs";
@@ -81,12 +119,18 @@ end
 local function create_context(host, mode, ...)
        local cfg = new_config();
        cfg:apply(core_defaults);
-       cfg:apply(global_ssl_config);
+       local service_name, port = host:match("^(%w+) port (%d+)$");
+       if service_name then
+               cfg:apply(find_service_cert(service_name, tonumber(port)));
+       else
+               cfg:apply(find_host_cert(host));
+       end
        cfg:apply({
                mode = mode,
                -- We can't read the password interactively when daemonized
                password = function() log("error", "Encrypted certificate for %s requires 'ssl' 'password' to be set in config", host); end;
        });
+       cfg:apply(global_ssl_config);
 
        for i = select('#', ...), 1, -1 do
                cfg:apply(select(i, ...));