aboutsummaryrefslogtreecommitdiffstats
path: root/core/portmanager.lua
blob: 103c5d44ce6dbbaeb3ac6269819e83044153bbe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

local multitable = require "util.multitable";
local fire_event = prosody.events.fire_event;

--- Config

local default_interfaces = { "*" };
local default_local_interfaces = { "127.0.0.1" };
if config.get("*", "use_ipv6") then
	table.insert(default_interfaces, "::");
	table.insert(default_local_interfaces, "::1");
end

--- Private state

-- service_name -> service_info
local services = {};

-- service_name, interface (string), port (number)
local active_services = multitable.new();

--- Private helpers

local function error_to_friendly_message(service_name, port, err)
	local friendly_message = err;
	if err:match(" in use") then
		-- FIXME: Use service_name here
		if port == 5222 or port == 5223 or port == 5269 then
			friendly_message = "check that Prosody or another XMPP server is "
				.."not already running and using this port";
		elseif port == 80 or port == 81 then
			friendly_message = "check that a HTTP server is not already using "
				.."this port";
		elseif port == 5280 then
			friendly_message = "check that Prosody or a BOSH connection manager "
				.."is not already running";
		else
			friendly_message = "this port is in use by another application";
		end
	elseif err:match("permission") then
		friendly_message = "Prosody does not have sufficient privileges to use this port";
	elseif err == "no ssl context" then
		if not config.get("*", "core", "ssl") then
			friendly_message = "there is no 'ssl' config under Host \"*\" which is "
				.."require for legacy SSL ports";
		else
			friendly_message = "initializing SSL support failed, see previous log entries";
		end
	end
	return friendly_message;
end

module("portmanager", package.seeall);

--- Public API

function activate_service(service_name)
	local service_info = services[service_name];
	if not service_info then
		return nil, "Unknown service: "..service_name;
	end

	local bind_interfaces = set.new(config.get("*", service_name.."_interfaces")
		or config.get("*", service_name.."_interface") -- COMPAT w/pre-0.9
		or (service_info.private and default_local_interfaces)
		or config.get("*", "interfaces")
		or config.get("*", "interface") -- COMPAT w/pre-0.9
		or service_info.default_interface -- COMPAT w/pre0.9
		or default_interfaces);
	
	local bind_ports = set.new(config.get("*", service_name.."_ports")
		or (service_info.multiplex and config.get("*", "ports"))
		or service_info.default_ports
		or {service_info.default_port});

	local listener = service_info.listener;
	local mode = listener.default_mode or "*a";
	local ssl;
	if service_info.encryption == "ssl" then
		ssl = prosody.global_ssl_ctx;
		if not ssl then
			return nil, "global-ssl-context-required";
		end
	end
	
	for interface in bind_interfaces do
		for port in bind_ports do
			if not service_info.multiplex and #active_services:search(nil, interface, port) > 0 then
				log("error", "Multiple services configured to listen on the same port: %s, %s", table.concat(active_services:search(nil, interface, port), ", "), service_name);
			else
				local handler, err = server.addserver(interface, port, listener, mode, ssl);
				if not handler then
					log("error", "Failed to open server port %d on %s, %s", port, interface, error_to_friendly_message(service_name, port, err));
				else
					log("debug", "Added listening service %s to [%s]:%d", service_name, interface, port);
					active_services:add(service_name, interface, port, {
						server = handler;
						service = service_info;
					});
				end
			end
		end
	end
	log("info", "Activated service '%s'", service_name);
end

function deactivate(service_name)
	local active = active_services:search(service_name)[1];
	if not active then return; end
	for interface, ports in pairs(active) do
		for port, active_service in pairs(ports) do
			active_service:close();
			active_services:remove(service_name, interface, port, active_service);
			log("debug", "Removed listening service %s from [%s]:%d", service_name, interface, port);
		end
	end
	log("info", "Deactivated service '%s'", service_name);
end

function register_service(service_name, service_info)
	services[service_name] = service_info;

	if not active_services[service_name] then
		activate_service(service_name);
	end
	
	fire_event("service-added", { name = service_name, service = service_info });
	return true;
end

function get_service(service_name)
	return services[service_name];
end

function get_active_services(...)
	return active_services;
end

return _M;