2045d28fc3816f6ea6fae6a2c98739014401700e
[prosody.git] / net / xmppcomponent_listener.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 = { stream_tag = "http://etherx.jabber.org/streams\1stream", default_ns = xmlns_component };
36
37 function stream_callbacks.error(session, error, data, data2)
38         log("warn", "Error processing component stream: "..tostring(error));
39         if error == "no-stream" then
40                 session:close("invalid-namespace");
41         elseif error == "xml-parse-error" and data == "unexpected-element-close" then
42                 session.log("warn", "Unexpected close of '%s' tag", data2);
43                 session:close("xml-not-well-formed");
44         else
45                 session.log("warn", "External component %s XML parse error: %s", tostring(session.host), tostring(error));
46                 session:close("xml-not-well-formed");
47         end
48 end
49
50 function stream_callbacks.streamopened(session, attr)
51         if config.get(attr.to, "core", "component_module") ~= "component" then
52                 -- Trying to act as a component domain which 
53                 -- hasn't been configured
54                 session:close{ condition = "host-unknown", text = tostring(attr.to).." does not match any configured external components" };
55                 return;
56         end
57         
58         -- Store the original host (this is used for config, etc.)
59         session.user = attr.to;
60         -- Set the host for future reference
61         session.host = config.get(attr.to, "core", "component_address") or attr.to;
62         -- Note that we don't create the internal component 
63         -- until after the external component auths successfully
64
65         session.streamid = uuid_gen();
66         session.notopen = nil;
67         
68         session.send(st.stanza("stream:stream", { xmlns=xmlns_component,
69                         ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.host }):top_tag());
70
71 end
72
73 function stream_callbacks.streamclosed(session)
74         session.send("</stream:stream>");
75         session.notopen = true;
76 end
77
78 local core_process_stanza = core_process_stanza;
79
80 function stream_callbacks.handlestanza(session, stanza)
81         -- Namespaces are icky.
82         if not stanza.attr.xmlns and stanza.name == "handshake" then
83                 stanza.attr.xmlns = xmlns_component;
84         end
85         return core_process_stanza(session, stanza);
86 end
87
88 --- Closing a component connection
89 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
90 local default_stream_attr = { ["xmlns:stream"] = stream_callbacks.stream_tag:match("[^\1]*"), xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
91 local function session_close(session, reason)
92         local log = session.log or log;
93         if session.conn then
94                 if session.notopen then
95                         session.send("<?xml version='1.0'?>");
96                         session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
97                 end
98                 if reason then
99                         if type(reason) == "string" then -- assume stream error
100                                 log("info", "Disconnecting component, <stream:error> is: %s", reason);
101                                 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
102                         elseif type(reason) == "table" then
103                                 if reason.condition then
104                                         local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
105                                         if reason.text then
106                                                 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
107                                         end
108                                         if reason.extra then
109                                                 stanza:add_child(reason.extra);
110                                         end
111                                         log("info", "Disconnecting component, <stream:error> is: %s", tostring(stanza));
112                                         session.send(stanza);
113                                 elseif reason.name then -- a stanza
114                                         log("info", "Disconnecting component, <stream:error> is: %s", tostring(reason));
115                                         session.send(reason);
116                                 end
117                         end
118                 end
119                 session.send("</stream:stream>");
120                 session.conn.close();
121                 component_listener.disconnect(session.conn, "stream error");
122         end
123 end
124
125 --- Component connlistener
126 function component_listener.listener(conn, data)
127         local session = sessions[conn];
128         if not session then
129                 local _send = conn.write;
130                 session = { type = "component", conn = conn, send = function (data) return _send(tostring(data)); end };
131                 sessions[conn] = session;
132
133                 -- Logging functions --
134                 
135                 local conn_name = "jcp"..tostring(conn):match("[a-f0-9]+$");
136                 session.log = logger.init(conn_name);
137                 session.close = session_close;
138                 
139                 session.log("info", "Incoming Jabber component connection");
140                 
141                 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "\1");
142                 session.parser = parser;
143                 
144                 session.notopen = true;
145                 
146                 function session.data(conn, data)
147                         local ok, err = parser:parse(data);
148                         if ok then return; end
149                         session:close("xml-not-well-formed");
150                 end
151                 
152                 session.dispatch_stanza = stream_callbacks.handlestanza;
153                 
154         end
155         if data then
156                 session.data(conn, data);
157         end
158 end
159         
160 function component_listener.disconnect(conn, err)
161         local session = sessions[conn];
162         if session then
163                 (session.log or log)("info", "component disconnected: %s (%s)", tostring(session.host), tostring(err));
164                 if session.host then
165                         log("debug", "Deregistering component");
166                         cm_deregister_component(session.host);
167                         hosts[session.host].connected = nil;
168                 end
169                 sessions[conn]  = nil;
170                 for k in pairs(session) do session[k] = nil; end
171                 session = nil;
172         end
173 end
174
175 connlisteners.register('xmppcomponent', component_listener);