util.dataforms: Fix verfication for booleans
[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 t_concat = table.concat;
11 local tostring = tostring;
12 local type = type;
13 local pairs = pairs;
14
15 local lxp = require "lxp";
16 local logger = require "util.logger";
17 local config = require "core.configmanager";
18 local connlisteners = require "net.connlisteners";
19 local uuid_gen = require "util.uuid".generate;
20 local jid_split = require "util.jid".split;
21 local sha1 = require "util.hashes".sha1;
22 local st = require "util.stanza";
23 local new_xmpp_stream = require "util.xmppstream".new;
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 xmppstream 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("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         -- Note that we don't create the internal component
76         -- until after the external component auths successfully
77
78         session.host = attr.to;
79         session.streamid = uuid_gen();
80         session.notopen = nil;
81         
82         session.send(st.stanza("stream:stream", { xmlns=xmlns_component,
83                         ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.host }):top_tag());
84
85 end
86
87 function stream_callbacks.streamclosed(session)
88         session.log("debug", "Received </stream:stream>");
89         session:close();
90 end
91
92 local core_process_stanza = core_process_stanza;
93
94 function stream_callbacks.handlestanza(session, stanza)
95         -- Namespaces are icky.
96         if not stanza.attr.xmlns and stanza.name == "handshake" then
97                 stanza.attr.xmlns = xmlns_component;
98         end
99         if not stanza.attr.xmlns or stanza.attr.xmlns == "jabber:client" then
100                 local from = stanza.attr.from;
101                 if from then
102                         if session.component_validate_from then
103                                 local _, domain = jid_split(stanza.attr.from);
104                                 if domain ~= session.host then
105                                         -- Return error
106                                         session.log("warn", "Component sent stanza with missing or invalid 'from' address");
107                                         session:close{
108                                                 condition = "invalid-from";
109                                                 text = "Component tried to send from address <"..tostring(from)
110                                                            .."> which is not in domain <"..tostring(session.host)..">";
111                                         };
112                                         return;
113                                 end
114                         end
115                 else
116                         stanza.attr.from = session.host;
117                 end
118                 if not stanza.attr.to then
119                         session.log("warn", "Rejecting stanza with no 'to' address");
120                         session.send(st.error_reply(stanza, "modify", "bad-request", "Components MUST specify a 'to' address on stanzas"));
121                         return;
122                 end
123         end
124         return core_process_stanza(session, stanza);
125 end
126
127 --- Closing a component connection
128 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
129 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
130 local function session_close(session, reason)
131         if session.destroyed then return; end
132         local log = session.log or log;
133         if session.conn then
134                 if session.notopen then
135                         session.send("<?xml version='1.0'?>");
136                         session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
137                 end
138                 if reason then
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();
145                                         if reason.text then
146                                                 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
147                                         end
148                                         if reason.extra then
149                                                 stanza:add_child(reason.extra);
150                                         end
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);
156                                 end
157                         end
158                 end
159                 session.send("</stream:stream>");
160                 session.conn:close();
161                 component_listener.ondisconnect(session.conn, "stream error");
162         end
163 end
164
165 --- Component connlistener
166 function component_listener.onconnect(conn)
167         local _send = conn.write;
168         local session = { type = "component", conn = conn, send = function (data) return _send(conn, tostring(data)); end };
169
170         -- Logging functions --
171         local conn_name = "jcp"..tostring(conn):match("[a-f0-9]+$");
172         session.log = logger.init(conn_name);
173         session.close = session_close;
174         
175         session.log("info", "Incoming Jabber component connection");
176         
177         local stream = new_xmpp_stream(session, stream_callbacks);
178         session.stream = stream;
179         
180         session.notopen = true;
181         
182         function session.reset_stream()
183                 session.notopen = true;
184                 session.stream:reset();
185         end
186
187         function session.data(conn, data)
188                 local ok, err = stream:feed(data);
189                 if ok then return; end
190                 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
191                 session:close("not-well-formed");
192         end
193         
194         session.dispatch_stanza = stream_callbacks.handlestanza;
195
196         sessions[conn] = session;
197 end
198 function component_listener.onincoming(conn, data)
199         local session = sessions[conn];
200         session.data(conn, data);
201 end
202 function component_listener.ondisconnect(conn, err)
203         local session = sessions[conn];
204         if session then
205                 (session.log or log)("info", "component disconnected: %s (%s)", tostring(session.host), tostring(err));
206                 if session.on_destroy then session:on_destroy(err); end
207                 sessions[conn] = nil;
208                 for k in pairs(session) do
209                         if k ~= "log" and k ~= "close" then
210                                 session[k] = nil;
211                         end
212                 end
213                 session.destroyed = true;
214                 session = nil;
215         end
216 end
217
218 connlisteners.register('xmppcomponent', component_listener);