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