Annotate

plugins/mod_component.lua @ 3581:3f3f8227ba76

mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 10 Nov 2010 01:51:03 +0500
parent 3579:9720fa5e0991
child 3587:d94aacb2771a
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: 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 cm_register_component = require "core.componentmanager".register_component;
981
71fce47dff7b mod_component: Deregister component on disconnect
Matthew Wild <mwild1@gmail.com>
parents: 979
diff changeset
19 local cm_deregister_component = require "core.componentmanager".deregister_component;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local sha1 = require "util.hashes".sha1;
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
21 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
22
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 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
24
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 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
26
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 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
28 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
29 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
30 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
31 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
32 hosts[session.host].connected = 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
33 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
34 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
35
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 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
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 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
47 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
48
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("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
50 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
51 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
52 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
53 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
54 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
55 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
56 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
57 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
58
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
59 cm_register_component(module.host, function() 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
60
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 --- 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
62 function handle_component_auth(event)
f41e1cfe92f4 mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
63 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
64
f41e1cfe92f4 mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
65 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
66 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
67
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
68 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
69 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
70 (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
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
3503
85e511e01d3c net.xmppcomponent_listener, mod_component: Removed useless undocumented option 'component_address'.
Waqas Hussain <waqas20@gmail.com>
parents: 3319
diff changeset
75 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
76 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
77 (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
78 session:close("not-authorized");
3531
f41e1cfe92f4 mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
79 return true;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 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
83 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
84 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
85 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
86 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
87 return true;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 -- 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
92 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
93
3319
95fc08869273 mod_component: Read validate_from_addresses option from the config
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
94 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
95
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 -- 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
97 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
98 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
99 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
100 session.on_destroy = on_destroy;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 hosts[session.host].connected = true;
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
102 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
103 else
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 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
105 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
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
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 -- Signal successful authentication
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 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
111 return true;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
3531
f41e1cfe92f4 mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
114 module:hook("stanza/jabber:component:accept:handshake", handle_component_auth);