Annotate

net/xmppcomponent_listener.lua @ 4001:2e8411f6cb14

configmanager: Added rawget().
author Waqas Hussain <waqas20@gmail.com>
date Wed, 05 Jan 2011 06:16:07 +0500
parent 3752:c34a58c3ea43
child 4463:df2cbc52c83b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1249
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2816
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2816
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local hosts = _G.hosts;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local t_concat = table.concat;
3751
46edbc8339b1 net.xmppcomponent_listener: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3706
diff changeset
13 local tostring = tostring;
46edbc8339b1 net.xmppcomponent_listener: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3706
diff changeset
14 local type = type;
46edbc8339b1 net.xmppcomponent_listener: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3706
diff changeset
15 local pairs = pairs;
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local lxp = require "lxp";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local logger = require "util.logger";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local config = require "core.configmanager";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local connlisteners = require "net.connlisteners";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local uuid_gen = require "util.uuid".generate;
3320
517ae40a12a2 xmppcomponent_listener: Import jid_split to fix undefined global reference
Matthew Wild <mwild1@gmail.com>
parents: 3316
diff changeset
22 local jid_split = require "util.jid".split;
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local sha1 = require "util.hashes".sha1;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local st = require "util.stanza";
3706
b758e4ad30a0 xmppcomponent_listener: Switch to util.xmppstream from xmlhandlers
Matthew Wild <mwild1@gmail.com>
parents: 3634
diff changeset
25 local new_xmpp_stream = require "util.xmppstream".new;
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local sessions = {};
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local log = logger.init("componentlistener");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local component_listener = { default_port = 5347; default_mode = "*a"; default_interface = config.get("*", "core", "component_interface") or "127.0.0.1" };
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local xmlns_component = 'jabber:component:accept';
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
3706
b758e4ad30a0 xmppcomponent_listener: Switch to util.xmppstream from xmlhandlers
Matthew Wild <mwild1@gmail.com>
parents: 3634
diff changeset
35 --- Callbacks/data for xmppstream to handle streams for us ---
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
2466
0e44b6035210 net.xmpp{client,server,component}: Update for new xmlhandlers syntax
Matthew Wild <mwild1@gmail.com>
parents: 2163
diff changeset
37 local stream_callbacks = { default_ns = xmlns_component };
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
2754
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
39 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 function stream_callbacks.error(session, error, data, data2)
2755
28f2416a3e4e net.xmppcomponent_listener: Don't handle stream errors from component sessions that have already been destroyed
Matthew Wild <mwild1@gmail.com>
parents: 2754
diff changeset
42 if session.destroyed then return; end
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 log("warn", "Error processing component stream: "..tostring(error));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 if error == "no-stream" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 session:close("invalid-namespace");
2754
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
46 elseif error == "parse-error" then
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
47 session.log("warn", "External component %s XML parse error: %s", tostring(session.host), tostring(data));
3634
0d6b57edb7bd net.xmpp{client,server,component}_listener: s/xml-not-well-formed/not-well-formed/ as per latest bis drafts.
Waqas Hussain <waqas20@gmail.com>
parents: 3604
diff changeset
48 session:close("not-well-formed");
2754
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
49 elseif error == "stream-error" then
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
50 local condition, text = "undefined-condition";
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
51 for child in data:children() do
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
52 if child.attr.xmlns == xmlns_xmpp_streams then
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
53 if child.name ~= "text" then
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
54 condition = child.name;
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
55 else
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
56 text = child:get_text();
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
57 end
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
58 if condition ~= "undefined-condition" and text then
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
59 break;
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
60 end
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
61 end
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
62 end
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
63 text = condition .. (text and (" ("..text..")") or "");
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
64 session.log("info", "Session closed by remote with error: %s", text);
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
65 session:close(nil, text);
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 function stream_callbacks.streamopened(session, attr)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 if config.get(attr.to, "core", "component_module") ~= "component" then
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3505
diff changeset
71 -- Trying to act as a component domain which
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 -- hasn't been configured
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 session:close{ condition = "host-unknown", text = tostring(attr.to).." does not match any configured external components" };
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 return;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3505
diff changeset
77 -- Note that we don't create the internal component
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 -- until after the external component auths successfully
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
3503
85e511e01d3c net.xmppcomponent_listener, mod_component: Removed useless undocumented option 'component_address'.
Waqas Hussain <waqas20@gmail.com>
parents: 3321
diff changeset
80 session.host = attr.to;
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 session.streamid = uuid_gen();
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 session.notopen = nil;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 session.send(st.stanza("stream:stream", { xmlns=xmlns_component,
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.host }):top_tag());
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 function stream_callbacks.streamclosed(session)
3504
adbb015b237e net.xmppcomponent_listener: Specify missing log level for a log statement.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
90 session.log("debug", "Received </stream:stream>");
2756
d81af905ccce net.xmppcomponent_listener: Close component sessions on receiving a </stream:stream>
Matthew Wild <mwild1@gmail.com>
parents: 2755
diff changeset
91 session:close();
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 local core_process_stanza = core_process_stanza;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 function stream_callbacks.handlestanza(session, stanza)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 -- Namespaces are icky.
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 if not stanza.attr.xmlns and stanza.name == "handshake" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 stanza.attr.xmlns = xmlns_component;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
3316
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
101 if not stanza.attr.xmlns or stanza.attr.xmlns == "jabber:client" then
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
102 local from = stanza.attr.from;
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
103 if from then
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
104 if session.component_validate_from then
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
105 local _, domain = jid_split(stanza.attr.from);
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
106 if domain ~= session.host then
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
107 -- Return error
3321
dcdfc04f9e3a xmppcomponent_listener: Add log messages for when stanzas from components have invalid or missing to/from addresses
Matthew Wild <mwild1@gmail.com>
parents: 3320
diff changeset
108 session.log("warn", "Component sent stanza with missing or invalid 'from' address");
3316
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
109 session:close{
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
110 condition = "invalid-from";
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
111 text = "Component tried to send from address <"..tostring(from)
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
112 .."> which is not in domain <"..tostring(session.host)..">";
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
113 };
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
114 return;
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
115 end
3315
f4d2de6ac22e xmppcomponent_listener: Validate to/from on components, fixes #104 and #162
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
116 end
3316
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
117 else
3315
f4d2de6ac22e xmppcomponent_listener: Validate to/from on components, fixes #104 and #162
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
118 stanza.attr.from = session.host;
3316
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
119 end
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
120 if not stanza.attr.to then
3321
dcdfc04f9e3a xmppcomponent_listener: Add log messages for when stanzas from components have invalid or missing to/from addresses
Matthew Wild <mwild1@gmail.com>
parents: 3320
diff changeset
121 session.log("warn", "Rejecting stanza with no 'to' address");
3316
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
122 session.send(st.error_reply(stanza, "modify", "bad-request", "Components MUST specify a 'to' address on stanzas"));
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
123 return;
6122a6270773 xmppcomponent_listener: Don't validate to/from on non-stanzas, fixes component auth.
Waqas Hussain <waqas20@gmail.com>
parents: 3315
diff changeset
124 end
3315
f4d2de6ac22e xmppcomponent_listener: Validate to/from on components, fixes #104 and #162
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
125 end
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 return core_process_stanza(session, stanza);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 --- Closing a component connection
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
2466
0e44b6035210 net.xmpp{client,server,component}: Update for new xmlhandlers syntax
Matthew Wild <mwild1@gmail.com>
parents: 2163
diff changeset
131 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local function session_close(session, reason)
2757
c13816e0c546 net.xmppcomponent_listener: Don't allow closing a session that has already been destroyed
Matthew Wild <mwild1@gmail.com>
parents: 2756
diff changeset
133 if session.destroyed then return; end
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 local log = session.log or log;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 if session.conn then
1617
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
136 if session.notopen then
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
137 session.send("<?xml version='1.0'?>");
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
138 session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
139 end
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 if reason then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 if type(reason) == "string" then -- assume stream error
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 log("info", "Disconnecting component, <stream:error> is: %s", reason);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 elseif type(reason) == "table" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 if reason.condition then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 if reason.text then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 if reason.extra then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 stanza:add_child(reason.extra);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 log("info", "Disconnecting component, <stream:error> is: %s", tostring(stanza));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 session.send(stanza);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 elseif reason.name then -- a stanza
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 log("info", "Disconnecting component, <stream:error> is: %s", tostring(reason));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 session.send(reason);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 session.send("</stream:stream>");
2489
d8296ab96f55 xmppcomponent_listener: Update for new connection API
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
162 session.conn:close();
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 2077
diff changeset
163 component_listener.ondisconnect(session.conn, "stream error");
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 --- Component connlistener
3752
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
168 function component_listener.onconnect(conn)
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
169 local _send = conn.write;
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
170 local session = { type = "component", conn = conn, send = function (data) return _send(conn, tostring(data)); end };
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
171
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
172 -- Logging functions --
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
173 local conn_name = "jcp"..tostring(conn):match("[a-f0-9]+$");
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
174 session.log = logger.init(conn_name);
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
175 session.close = session_close;
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
176
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
177 session.log("info", "Incoming Jabber component connection");
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
178
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
179 local stream = new_xmpp_stream(session, stream_callbacks);
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
180 session.stream = stream;
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
181
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
182 session.notopen = true;
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
183
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
184 function session.reset_stream()
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
185 session.notopen = true;
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
186 session.stream:reset();
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
187 end
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
188
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
189 function session.data(conn, data)
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
190 local ok, err = stream:feed(data);
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
191 if ok then return; end
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
192 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
193 session:close("not-well-formed");
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
194 end
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
195
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
196 session.dispatch_stanza = stream_callbacks.handlestanza;
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
197
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
198 sessions[conn] = session;
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
199 end
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 2077
diff changeset
200 function component_listener.onincoming(conn, data)
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 local session = sessions[conn];
3752
c34a58c3ea43 net.xmppcomponent_listener: Move session creation from listener.onincoming to listener.onconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3751
diff changeset
202 session.data(conn, data);
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 end
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 2077
diff changeset
204 function component_listener.ondisconnect(conn, err)
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 local session = sessions[conn];
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 if session then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 (session.log or log)("info", "component disconnected: %s (%s)", tostring(session.host), tostring(err));
3578
410d2df8a824 net.xmppcomponent_listener: Call session:on_destroy() on session disconnect.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
208 if session.on_destroy then session:on_destroy(err); end
3582
6a14c57b458a net.xmppcomponent_listener: Removed unnecessary and problematic cleanup code.
Waqas Hussain <waqas20@gmail.com>
parents: 3578
diff changeset
209 sessions[conn] = nil;
2759
48e8832e5cab net.xmppcomponent_listener: Don't remove 'log' and 'close' from sessions when destroying, and set destroyed = true
Matthew Wild <mwild1@gmail.com>
parents: 2758
diff changeset
210 for k in pairs(session) do
48e8832e5cab net.xmppcomponent_listener: Don't remove 'log' and 'close' from sessions when destroying, and set destroyed = true
Matthew Wild <mwild1@gmail.com>
parents: 2758
diff changeset
211 if k ~= "log" and k ~= "close" then
48e8832e5cab net.xmppcomponent_listener: Don't remove 'log' and 'close' from sessions when destroying, and set destroyed = true
Matthew Wild <mwild1@gmail.com>
parents: 2758
diff changeset
212 session[k] = nil;
48e8832e5cab net.xmppcomponent_listener: Don't remove 'log' and 'close' from sessions when destroying, and set destroyed = true
Matthew Wild <mwild1@gmail.com>
parents: 2758
diff changeset
213 end
48e8832e5cab net.xmppcomponent_listener: Don't remove 'log' and 'close' from sessions when destroying, and set destroyed = true
Matthew Wild <mwild1@gmail.com>
parents: 2758
diff changeset
214 end
48e8832e5cab net.xmppcomponent_listener: Don't remove 'log' and 'close' from sessions when destroying, and set destroyed = true
Matthew Wild <mwild1@gmail.com>
parents: 2758
diff changeset
215 session.destroyed = true;
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 session = nil;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 connlisteners.register('xmppcomponent', component_listener);