Annotate

plugins/mod_component.lua @ 3579:9720fa5e0991

mod_component: Send back a <conflict/> stream error when multiple sessions attempt to bind.
author Waqas Hussain <waqas20@gmail.com>
date Tue, 09 Nov 2010 20:23:28 +0500
parent 3540:bc139431830b
child 3581:3f3f8227ba76
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
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 --- 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
26 function handle_component_auth(event)
f41e1cfe92f4 mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
27 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
28
f41e1cfe92f4 mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
29 if session.type ~= "component" then return; end
f41e1cfe92f4 mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
30
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
31 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
32 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
33 (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
34 session:close("not-authorized");
3531
f41e1cfe92f4 mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
35 return true;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
3503
85e511e01d3c net.xmppcomponent_listener, mod_component: Removed useless undocumented option 'component_address'.
Waqas Hussain <waqas20@gmail.com>
parents: 3319
diff changeset
38 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
39 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
40 (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
41 session:close("not-authorized");
3531
f41e1cfe92f4 mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
42 return true;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 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
46 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
47 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
48 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
49 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
50 return true;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 -- 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
55 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
56
3319
95fc08869273 mod_component: Read validate_from_addresses option from the config
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
57 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
58
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 -- If component not already created for this host, create one now
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 if not hosts[session.host].connected then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local send = session.send;
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3531
diff changeset
62 session.component_session = cm_register_component(session.host, function (_, data)
1405
19269d278c38 mod_component: Rewrite jabber:client stanzas to jabber:component:accept, thanks JaredH!
Matthew Wild <mwild1@gmail.com>
parents: 1108
diff changeset
63 if data.attr and data.attr.xmlns == "jabber:client" then
19269d278c38 mod_component: Rewrite jabber:client stanzas to jabber:component:accept, thanks JaredH!
Matthew Wild <mwild1@gmail.com>
parents: 1108
diff changeset
64 data.attr.xmlns = nil;
19269d278c38 mod_component: Rewrite jabber:client stanzas to jabber:component:accept, thanks JaredH!
Matthew Wild <mwild1@gmail.com>
parents: 1108
diff changeset
65 end
19269d278c38 mod_component: Rewrite jabber:client stanzas to jabber:component:accept, thanks JaredH!
Matthew Wild <mwild1@gmail.com>
parents: 1108
diff changeset
66 return send(data);
19269d278c38 mod_component: Rewrite jabber:client stanzas to jabber:component:accept, thanks JaredH!
Matthew Wild <mwild1@gmail.com>
parents: 1108
diff changeset
67 end);
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 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
69 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
70 else
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 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
72 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
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 -- Signal successful authentication
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 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
78 return true;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
3531
f41e1cfe92f4 mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
81 module:hook("stanza/jabber:component:accept:handshake", handle_component_auth);