Fri, 17 Dec 2010 17:28:05 +0100
util.stanza: Iterate on childtags instead of all childs.
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 | |
1042
a3d77353c18a
mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents:
981
diff
changeset
|
13 | local hosts = _G.hosts; |
a3d77353c18a
mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents:
981
diff
changeset
|
14 | |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | 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
|
16 | |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | local sha1 = require "util.hashes".sha1; |
1042
a3d77353c18a
mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents:
981
diff
changeset
|
18 | 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
|
19 | |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | 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
|
21 | |
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
|
22 | 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
|
23 | |
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
|
24 | 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
|
25 | if main_session == session 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
|
26 | 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
|
27 | 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
|
28 | 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
|
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 | 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
|
31 | |
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 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
|
33 | 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
|
34 | 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
|
35 | 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
|
36 | 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
|
37 | else |
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 | log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag()); |
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 | 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
|
40 | 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
|
41 | 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
|
42 | 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
|
43 | 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
|
44 | 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
|
45 | |
3798
d835068a5791
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("iq/bare", handle_stanza, -1); |
d835068a5791
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("message/bare", handle_stanza, -1); |
d835068a5791
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("presence/bare", handle_stanza, -1); |
d835068a5791
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("iq/full", handle_stanza, -1); |
d835068a5791
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("message/full", handle_stanza, -1); |
d835068a5791
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("presence/full", handle_stanza, -1); |
d835068a5791
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("iq/host", handle_stanza, -1); |
d835068a5791
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("message/host", handle_stanza, -1); |
d835068a5791
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
|
54 | 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
|
55 | |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | --- 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
|
57 | function handle_component_auth(event) |
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
58 | 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
|
59 | |
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
60 | 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
|
61 | 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
|
62 | |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | if (not session.host) or #stanza.tags > 0 then |
3618
321767e78029
mod_component: Logging tweaks.
Waqas Hussain <waqas20@gmail.com>
parents:
3617
diff
changeset
|
64 | (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
|
65 | session:close("not-authorized"); |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
66 | return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | |
3617
26c9ba8f309c
mod_component: Use module:get_option() instead of configmanager.
Waqas Hussain <waqas20@gmail.com>
parents:
3616
diff
changeset
|
69 | 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
|
70 | 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
|
71 | (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
|
72 | session:close("not-authorized"); |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
73 | return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | 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
|
77 | 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
|
78 | if supplied_token:lower() ~= calculated_token:lower() then |
3618
321767e78029
mod_component: Logging tweaks.
Waqas Hussain <waqas20@gmail.com>
parents:
3617
diff
changeset
|
79 | 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
|
80 | 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
|
81 | return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 | end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 | |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 | -- 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
|
85 | if not main_session 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
|
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; |
3618
321767e78029
mod_component: Logging tweaks.
Waqas Hussain <waqas20@gmail.com>
parents:
3617
diff
changeset
|
89 | session.component_validate_from = module:get_option_boolean("validate_from_addresses") ~= false; |
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); |