Software /
code /
prosody
Annotate
plugins/mod_component.lua @ 3615:c72d24c2d97b
mod_component: Return true from stanza handler to indicate that we actually did handle the stanza.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 12 Nov 2010 22:39:37 +0500 |
parent | 3604:3e89f0509967 |
child | 3616:95ae7af2c82b |
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 |
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 |
1042
a3d77353c18a
mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents:
981
diff
changeset
|
17 local config = require "core.configmanager"; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local sha1 = require "util.hashes".sha1; |
1042
a3d77353c18a
mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents:
981
diff
changeset
|
19 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
|
20 |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 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
|
22 |
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
|
23 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
|
24 |
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 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
|
26 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
|
27 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
|
28 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
|
29 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
|
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 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
|
32 |
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 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 |
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
|
47 module:hook("iq/bare", handle_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
|
48 module:hook("message/bare", handle_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
|
49 module:hook("presence/bare", handle_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
|
50 module:hook("iq/full", handle_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
|
51 module:hook("message/full", handle_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
|
52 module:hook("presence/full", handle_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
|
53 module:hook("iq/host", handle_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
|
54 module:hook("message/host", handle_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
|
55 module:hook("presence/host", handle_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
|
56 |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 --- 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
|
58 function handle_component_auth(event) |
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
59 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
|
60 |
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
61 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
|
62 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
|
63 |
1108
368754c54045
mod_component: Vastly reduce the code, having split most of it to where it should be, xmppcomponent_listener
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
64 log("info", "Handling component auth"); |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 if (not session.host) or #stanza.tags > 0 then |
1108
368754c54045
mod_component: Vastly reduce the code, having split most of it to where it should be, xmppcomponent_listener
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
66 (session.log or log)("warn", "Component handshake invalid"); |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 session:close("not-authorized"); |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
68 return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
3503
85e511e01d3c
net.xmppcomponent_listener, mod_component: Removed useless undocumented option 'component_address'.
Waqas Hussain <waqas20@gmail.com>
parents:
3319
diff
changeset
|
71 local secret = config.get(session.host, "core", "component_secret"); |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 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
|
73 (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
|
74 session:close("not-authorized"); |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
75 return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 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
|
79 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
|
80 if supplied_token:lower() ~= calculated_token:lower() then |
1108
368754c54045
mod_component: Vastly reduce the code, having split most of it to where it should be, xmppcomponent_listener
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
81 log("info", "Component for %s authentication failed", session.host); |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 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
|
83 return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 -- Authenticated now |
1108
368754c54045
mod_component: Vastly reduce the code, having split most of it to where it should be, xmppcomponent_listener
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
88 log("info", "Component authenticated: %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
|
89 |
3319
95fc08869273
mod_component: Read validate_from_addresses option from the config
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
90 session.component_validate_from = module:get_option_boolean("validate_from_addresses") ~= false; |
95fc08869273
mod_component: Read validate_from_addresses option from the config
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
91 |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 -- 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
|
93 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
|
94 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
|
95 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
|
96 session.on_destroy = on_destroy; |
1108
368754c54045
mod_component: Vastly reduce the code, having split most of it to where it should be, xmppcomponent_listener
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
97 log("info", "Component successfully registered"); |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 else |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)"); |
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
|
100 session:close{ condition = "conflict", text = "Component already connected" }; |
9720fa5e0991
mod_component: Send back a <conflict/> stream error when multiple sessions attempt to bind.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
101 return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 -- Signal successful authentication |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 session.send(st.stanza("handshake")); |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
106 return true; |
902
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 end |
00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 |
3531
f41e1cfe92f4
mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
3503
diff
changeset
|
109 module:hook("stanza/jabber:component:accept:handshake", handle_component_auth); |