2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 if module:get_host_type() ~= "component" then
10 error("Don't load mod_component manually, it should be for a component, please see http://prosody.im/doc/components", 0);
13 local t_concat = table.concat;
15 local connlisteners = require "net.connlisteners";
16 local cm_register_component = require "core.componentmanager".register_component;
17 local uuid_gen = require "util.uuid".generate;
18 local sha1 = require "util.hashes".sha1;
20 local init_xmlhandlers = require "core.xmlhandlers";
24 local log = module._log;
26 local component_listener = { default_port = 5347; default_mode = "*a"; default_interface = config.get("*", "core", "component_interface") or "127.0.0.1" };
28 local xmlns_component = 'jabber:component:accept';
30 --- Callbacks/data for xmlhandlers to handle streams for us ---
32 local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", default_ns = xmlns_component };
34 function stream_callbacks.error(session, error, data, data2)
35 log("warn", "Error processing component stream: "..tostring(error));
36 if error == "no-stream" then
37 session:close("invalid-namespace");
38 elseif error == "xml-parse-error" and data == "unexpected-element-close" then
39 session.log("debug", "Unexpected close of '%s' tag", data2);
40 session:close("xml-not-well-formed");
42 session.log("debug", "External component %s XML parse error: %s", tostring(session.host), tostring(error));
43 print(data, debug.traceback())
44 session:close("xml-not-well-formed");
48 function stream_callbacks.streamopened(session, attr)
49 if config.get(attr.to, "core", "component_module") ~= "component" then
50 -- Trying to act as a component domain which
51 -- hasn't been configured
52 session:close{ condition = "host-unknown", text = tostring(attr.to).." does not match any configured external components" };
56 -- Store the original host (this is used for config, etc.)
57 session.user = attr.to;
58 -- Set the host for future reference
59 session.host = config.get(attr.to, "core", "component_address") or attr.to;
60 -- Note that we don't create the internal component
61 -- until after the external component auths successfully
63 session.streamid = uuid_gen();
64 session.notopen = nil;
66 session.send(st.stanza("stream:stream", { xmlns=xmlns_component,
67 ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.host }):top_tag());
71 function stream_callbacks.streamclosed(session)
74 local core_process_stanza = core_process_stanza;
76 function stream_callbacks.handlestanza(session, stanza)
77 -- Namespaces are icky.
78 log("warn", "Handing stanza with name %s", stanza.name);
79 if stanza.name ~= "handshake" then
80 return core_process_stanza(session, stanza);
82 handle_component_auth(session, stanza);
86 --- Handle authentication attempts by components
87 function handle_component_auth(session, stanza)
88 if (not session.host) or #stanza.tags > 0 then
89 session:close("not-authorized");
93 local secret = config.get(session.user, "core", "component_secret");
95 log("warn", "Component attempted to identify as %s, but component_password is not set", session.user);
96 session:close("not-authorized");
100 local supplied_token = t_concat(stanza);
101 local calculated_token = sha1(session.streamid..secret, true);
102 if supplied_token:lower() ~= calculated_token:lower() then
103 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" };
110 -- If component not already created for this host, create one now
111 if not hosts[session.host].connected then
112 local send = session.send;
113 session.component_session = cm_register_component(session.host, function (_, data) return send(data); end);
114 hosts[session.host].connected = true;
116 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)");
119 -- Signal successful authentication
120 session.send(st.stanza("handshake"));
123 module:add_handler("component", "handshake", xmlns_component, handle_component_auth);
126 -- Helper function to deal with errors processing component stanzas
127 local function handleerr(err) log("error", "Traceback[component]: %s: %s", tostring(err), debug.traceback()); end
128 function stream_callbacks.handlestanza(a, b)
129 xpcall(function () core_process_stanza(a, b) end, handleerr);
133 --- Closing a component connection
134 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
135 local function session_close(session, reason)
136 local log = session.log or log;
139 if type(reason) == "string" then -- assume stream error
140 log("info", "Disconnecting component, <stream:error> is: %s", reason);
141 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
142 elseif type(reason) == "table" then
143 if reason.condition then
144 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
146 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
149 stanza:add_child(reason.extra);
151 log("info", "Disconnecting component, <stream:error> is: %s", tostring(stanza));
152 session.send(stanza);
153 elseif reason.name then -- a stanza
154 log("info", "Disconnecting component, <stream:error> is: %s", tostring(reason));
155 session.send(reason);
159 session.send("</stream:stream>");
160 session.conn.close();
161 component_listener.disconnect(session.conn, "stream error");
165 --- Component connlistener
166 function component_listener.listener(conn, data)
167 local session = sessions[conn];
169 local _send = conn.write;
170 session = { type = "component", conn = conn, send = function (data) return _send(tostring(data)); end };
171 sessions[conn] = session;
173 -- Logging functions --
175 local conn_name = "xep114-"..tostring(conn):match("[a-f0-9]+$");
176 session.log = logger.init(conn_name);
177 session.close = session_close;
179 session.log("info", "Incoming XEP-0114 connection");
181 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|");
182 session.parser = parser;
184 session.notopen = true;
186 function session.data(conn, data)
187 local ok, err = parser:parse(data);
188 if ok then return; end
189 session:close("xml-not-well-formed");
192 session.dispatch_stanza = stream_callbacks.handlestanza;
196 session.data(conn, data);
200 function component_listener.disconnect(conn, err)
201 local session = sessions[conn];
203 (session.log or log)("info", "component disconnected: %s (%s)", tostring(session.host), tostring(err));
205 if session.component then
206 deregister_component(session.host);
209 sessions[conn] = nil;
211 collectgarbage("collect");
215 connlisteners.register('component', component_listener);
217 module:add_event_hook("server-started",
219 if net_activate_ports then
220 net_activate_ports("component_ports", "component", {5437}, "tcp");
222 error("No net_activate_ports: Using an incompatible version of Prosody?");