net/xmppcomponent_listener.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
 
10
local hosts = _G.hosts;
11
 
12
local t_concat = table.concat;
13
local tostring = tostring;
14
local type = type;
15
local pairs = pairs;
16
 
17
local logger = require "util.logger";
18
local config = require "core.configmanager";
19
local connlisteners = require "net.connlisteners";
20
local uuid_gen = require "util.uuid".generate;
21
local jid_split = require "util.jid".split;
22
local sha1 = require "util.hashes".sha1;
23
local st = require "util.stanza";
24
local new_xmpp_stream = require "util.xmppstream".new;
25
 
26
local sessions = {};
27
 
28
local log = logger.init("componentlistener");
29
 
30
local component_listener = { default_port = 5347; default_mode = "*a"; default_interface = config.get("*", "core", "component_interface") or "127.0.0.1" };
31
 
32
local xmlns_component = 'jabber:component:accept';
33
 
34
--- Callbacks/data for xmppstream to handle streams for us ---
35
 
36
local stream_callbacks = { default_ns = xmlns_component };
37
 
38
local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
39
 
40
function stream_callbacks.error(session, error, data, data2)
41
	if session.destroyed then return; end
42
	log("warn", "Error processing component stream: "..tostring(error));
43
	if error == "no-stream" then
44
		session:close("invalid-namespace");
45
	elseif error == "parse-error" then
46
		session.log("warn", "External component %s XML parse error: %s", tostring(session.host), tostring(data));
47
		session:close("not-well-formed");
48
	elseif error == "stream-error" then
49
		local condition, text = "undefined-condition";
50
		for child in data:children() do
51
			if child.attr.xmlns == xmlns_xmpp_streams then
52
				if child.name ~= "text" then
53
					condition = child.name;
54
				else
55
					text = child:get_text();
56
				end
57
				if condition ~= "undefined-condition" and text then
58
					break;
59
				end
60
			end
61
		end
62
		text = condition .. (text and (" ("..text..")") or "");
63
		session.log("info", "Session closed by remote with error: %s", text);
64
		session:close(nil, text);
65
	end
66
end
67
 
68
function stream_callbacks.streamopened(session, attr)
69
	if config.get(attr.to, "core", "component_module") ~= "component" then
70
		-- Trying to act as a component domain which
71
		-- hasn't been configured
72
		session:close{ condition = "host-unknown", text = tostring(attr.to).." does not match any configured external components" };
73
		return;
74
	end
75
	
76
	-- Note that we don't create the internal component
77
	-- until after the external component auths successfully
78
 
79
	session.host = attr.to;
80
	session.streamid = uuid_gen();
81
	session.notopen = nil;
82
	
83
	session.send(st.stanza("stream:stream", { xmlns=xmlns_component,
84
			["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.host }):top_tag());
85
 
86
end
87
 
88
function stream_callbacks.streamclosed(session)
89
	session.log("debug", "Received </stream:stream>");
90
	session:close();
91
end
92
 
93
local core_process_stanza = core_process_stanza;
94
 
95
function stream_callbacks.handlestanza(session, stanza)
96
	-- Namespaces are icky.
97
	if not stanza.attr.xmlns and stanza.name == "handshake" then
98
		stanza.attr.xmlns = xmlns_component;
99
	end
100
	if not stanza.attr.xmlns or stanza.attr.xmlns == "jabber:client" then
101
		local from = stanza.attr.from;
102
		if from then
103
			if session.component_validate_from then
104
				local _, domain = jid_split(stanza.attr.from);
105
				if domain ~= session.host then
106
					-- Return error
107
					session.log("warn", "Component sent stanza with missing or invalid 'from' address");
108
					session:close{
109
						condition = "invalid-from";
110
						text = "Component tried to send from address <"..tostring(from)
111
							   .."> which is not in domain <"..tostring(session.host)..">";
112
					};
113
					return;
114
				end
115
			end
116
		else
117
			stanza.attr.from = session.host;
118
		end
119
		if not stanza.attr.to then
120
			session.log("warn", "Rejecting stanza with no 'to' address");
121
			session.send(st.error_reply(stanza, "modify", "bad-request", "Components MUST specify a 'to' address on stanzas"));
122
			return;
123
		end
124
	end
125
	return core_process_stanza(session, stanza);
126
end
127
 
128
--- Closing a component connection
129
local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
130
local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
131
local function session_close(session, reason)
132
	if session.destroyed then return; end
133
	local log = session.log or log;
134
	if session.conn then
135
		if session.notopen then
136
			session.send("<?xml version='1.0'?>");
137
			session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
138
		end
139
		if reason then
140
			if type(reason) == "string" then -- assume stream error
141
				log("info", "Disconnecting component, <stream:error> is: %s", reason);
142
				session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
143
			elseif type(reason) == "table" then
144
				if reason.condition then
145
					local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
146
					if reason.text then
147
						stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
148
					end
149
					if reason.extra then
150
						stanza:add_child(reason.extra);
151
					end
152
					log("info", "Disconnecting component, <stream:error> is: %s", tostring(stanza));
153
					session.send(stanza);
154
				elseif reason.name then -- a stanza
155
					log("info", "Disconnecting component, <stream:error> is: %s", tostring(reason));
156
					session.send(reason);
157
				end
158
			end
159
		end
160
		session.send("</stream:stream>");
161
		session.conn:close();
162
		component_listener.ondisconnect(session.conn, "stream error");
163
	end
164
end
165
 
166
--- Component connlistener
167
function component_listener.onconnect(conn)
168
	local _send = conn.write;
169
	local session = { type = "component", conn = conn, send = function (data) return _send(conn, tostring(data)); end };
170
 
171
	-- Logging functions --
172
	local conn_name = "jcp"..tostring(conn):match("[a-f0-9]+$");
173
	session.log = logger.init(conn_name);
174
	session.close = session_close;
175
	
176
	session.log("info", "Incoming Jabber component connection");
177
	
178
	local stream = new_xmpp_stream(session, stream_callbacks);
179
	session.stream = stream;
180
	
181
	session.notopen = true;
182
	
183
	function session.reset_stream()
184
		session.notopen = true;
185
		session.stream:reset();
186
	end
187
 
188
	function session.data(conn, data)
189
		local ok, err = stream:feed(data);
190
		if ok then return; end
191
		log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
192
		session:close("not-well-formed");
193
	end
194
	
195
	session.dispatch_stanza = stream_callbacks.handlestanza;
196
 
197
	sessions[conn] = session;
198
end
199
function component_listener.onincoming(conn, data)
200
	local session = sessions[conn];
201
	session.data(conn, data);
202
end
203
function component_listener.ondisconnect(conn, err)
204
	local session = sessions[conn];
205
	if session then
206
		(session.log or log)("info", "component disconnected: %s (%s)", tostring(session.host), tostring(err));
207
		if session.on_destroy then session:on_destroy(err); end
208
		sessions[conn] = nil;
209
		for k in pairs(session) do
210
			if k ~= "log" and k ~= "close" then
211
				session[k] = nil;
212
			end
213
		end
214
		session.destroyed = true;
215
		session = nil;
216
	end
217
end
218
 
219
connlisteners.register('xmppcomponent', component_listener);