Software /
code /
prosody
Annotate
plugins/mod_component.lua @ 4592:0a9528b178fd
mod_s2s: remove conn sessions clearing redundancy and leave it only in listener.ondisconnect
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Mon, 05 Mar 2012 16:39:50 +0000 |
parent | 4464:b0574fc78a0a |
child | 4650:464ca74ddf6a |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1405
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2490
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2490
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 -- |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 -- COPYING file in the source package for more information. |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 -- |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 if module:get_host_type() ~= "component" then |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 error("Don't load mod_component manually, it should be for a component, please see http://prosody.im/doc/components", 0); |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local t_concat = table.concat; |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local sha1 = require "util.hashes".sha1; |
1042
a3d77353c18a
mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents:
981
diff
changeset
|
16 local st = require "util.stanza"; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local log = module._log; |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
3581
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
20 local main_session, send; |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
21 |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
22 local function on_destroy(session, err) |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
23 if main_session == session then |
4464
b0574fc78a0a
mod_component: removed unused variable reference, added "flagging" to assert if a component is connected or not.
Marco Cirillo <maranda@lightwitch.org>
parents:
4323
diff
changeset
|
24 connected = false; |
3581
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
25 main_session = nil; |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
26 send = nil; |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
27 session.on_destroy = nil; |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
28 end |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
29 end |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
30 |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
31 local function handle_stanza(event) |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
32 local stanza = event.stanza; |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
33 if send then |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
34 stanza.attr.xmlns = nil; |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
35 send(stanza); |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
36 else |
4323
53f3c3001499
mod_component: Clearer log message when bouncing a stanza from a component that is not connected (thanks MK)
Matthew Wild <mwild1@gmail.com>
parents:
4301
diff
changeset
|
37 log("warn", "Component not connected, bouncing error for: %s", stanza:top_tag()); |
3581
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
38 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
39 event.origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable")); |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
40 end |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
41 end |
3615
c72d24c2d97b
mod_component: Return true from stanza handler to indicate that we actually did handle the stanza.
Waqas Hussain <waqas20@gmail.com>
parents:
3604
diff
changeset
|
42 return true; |
3581
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
43 end |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
44 |
3674
4b7281c577b9
mod_component: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents:
3618
diff
changeset
|
45 module:hook("iq/bare", handle_stanza, -1); |
4b7281c577b9
mod_component: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents:
3618
diff
changeset
|
46 module:hook("message/bare", handle_stanza, -1); |
4b7281c577b9
mod_component: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents:
3618
diff
changeset
|
47 module:hook("presence/bare", handle_stanza, -1); |
4b7281c577b9
mod_component: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents:
3618
diff
changeset
|
48 module:hook("iq/full", handle_stanza, -1); |
4b7281c577b9
mod_component: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents:
3618
diff
changeset
|
49 module:hook("message/full", handle_stanza, -1); |
4b7281c577b9
mod_component: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents:
3618
diff
changeset
|
50 module:hook("presence/full", handle_stanza, -1); |
4b7281c577b9
mod_component: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents:
3618
diff
changeset
|
51 module:hook("iq/host", handle_stanza, -1); |
4b7281c577b9
mod_component: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents:
3618
diff
changeset
|
52 module:hook("message/host", handle_stanza, -1); |
4b7281c577b9
mod_component: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents:
3618
diff
changeset
|
53 module:hook("presence/host", handle_stanza, -1); |
3581
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
54 |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 --- Handle authentication attempts by components |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
56 function handle_component_auth(event) |
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
57 local session, stanza = event.origin, event.stanza; |
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
58 |
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
59 if session.type ~= "component" then return; end |
3581
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
60 if main_session == session then return; end |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
61 |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 if (not session.host) or #stanza.tags > 0 then |
3618
321767e78029
mod_component: Logging tweaks.
Waqas Hussain <waqas20@gmail.com>
parents:
3617
diff
changeset
|
63 (session.log or log)("warn", "Invalid component handshake for host: %s", session.host); |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 session:close("not-authorized"); |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
65 return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
3617
26c9ba8f309c
mod_component: Use module:get_option() instead of configmanager.
Waqas Hussain <waqas20@gmail.com>
parents:
3616
diff
changeset
|
68 local secret = module:get_option("component_secret"); |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 if not secret then |
3503
85e511e01d3c
net.xmppcomponent_listener, mod_component: Removed useless undocumented option 'component_address'.
Waqas Hussain <waqas20@gmail.com>
parents:
3319
diff
changeset
|
70 (session.log or log)("warn", "Component attempted to identify as %s, but component_secret is not set", session.host); |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 session:close("not-authorized"); |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
72 return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 local supplied_token = t_concat(stanza); |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 local calculated_token = sha1(session.streamid..secret, true); |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 if supplied_token:lower() ~= calculated_token:lower() then |
3618
321767e78029
mod_component: Logging tweaks.
Waqas Hussain <waqas20@gmail.com>
parents:
3617
diff
changeset
|
78 log("info", "Component authentication failed for %s", session.host); |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" }; |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
80 return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 -- If component not already created for this host, create one now |
3581
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
84 if not main_session then |
4464
b0574fc78a0a
mod_component: removed unused variable reference, added "flagging" to assert if a component is connected or not.
Marco Cirillo <maranda@lightwitch.org>
parents:
4323
diff
changeset
|
85 connected = true; |
3581
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
86 send = session.send; |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
87 main_session = session; |
3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents:
3579
diff
changeset
|
88 session.on_destroy = on_destroy; |
4301
1484ac561b28
mod_component: Small code clarity fix
Matthew Wild <mwild1@gmail.com>
parents:
3674
diff
changeset
|
89 session.component_validate_from = module:get_option_boolean("validate_from_addresses", true); |
3618
321767e78029
mod_component: Logging tweaks.
Waqas Hussain <waqas20@gmail.com>
parents:
3617
diff
changeset
|
90 log("info", "Component successfully authenticated: %s", session.host); |
3616
95ae7af2c82b
mod_component: Rearranged the code a little.
Waqas Hussain <waqas20@gmail.com>
parents:
3615
diff
changeset
|
91 session.send(st.stanza("handshake")); |
3618
321767e78029
mod_component: Logging tweaks.
Waqas Hussain <waqas20@gmail.com>
parents:
3617
diff
changeset
|
92 else -- TODO: Implement stanza distribution |
321767e78029
mod_component: Logging tweaks.
Waqas Hussain <waqas20@gmail.com>
parents:
3617
diff
changeset
|
93 log("error", "Multiple components bound to the same address, first one wins: %s", session.host); |
3579
9720fa5e0991
mod_component: Send back a <conflict/> stream error when multiple sessions attempt to bind.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
94 session:close{ condition = "conflict", text = "Component already connected" }; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
97 return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
100 module:hook("stanza/jabber:component:accept:handshake", handle_component_auth); |