plugins/mod_component.lua

Sun, 27 Sep 2009 12:10:50 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 27 Sep 2009 12:10:50 +0100
changeset 1832
5ae3209fefa2
parent 1523
841d61be198f
child 2490
6eee75fb3159
permissions
-rw-r--r--

Merge with waqas

1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1405
diff changeset
1 -- Prosody IM
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
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 lxp = require "lxp";
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
18 local logger = require "util.logger";
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
19 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
20 local connlisteners = require "net.connlisteners";
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 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
22 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
23 local uuid_gen = require "util.uuid".generate;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local sha1 = require "util.hashes".sha1;
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
25 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
26 local init_xmlhandlers = require "core.xmlhandlers";
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local sessions = {};
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 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
31
909
505a2cbb823d mod_component: Set default listening interface to 127.0.0.1
Matthew Wild <mwild1@gmail.com>
parents: 902
diff changeset
32 local component_listener = { default_port = 5347; default_mode = "*a"; default_interface = config.get("*", "core", "component_interface") or "127.0.0.1" };
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local xmlns_component = 'jabber:component:accept';
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 --- Handle authentication attempts by components
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 function handle_component_auth(session, stanza)
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
38 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
39 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
40 (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
41 session:close("not-authorized");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 return;
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 secret = config.get(session.user, "core", "component_secret");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if not secret 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
47 (session.log or log)("warn", "Component attempted to identify as %s, but component_password is not set", session.user);
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 session:close("not-authorized");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 return;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 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
53 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
54 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
55 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
56 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" };
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 return;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 -- 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
62 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
63
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 -- 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
65 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
66 local send = session.send;
1405
19269d278c38 mod_component: Rewrite jabber:client stanzas to jabber:component:accept, thanks JaredH!
Matthew Wild <mwild1@gmail.com>
parents: 1108
diff changeset
67 session.component_session = cm_register_component(session.host, function (_, data)
19269d278c38 mod_component: Rewrite jabber:client stanzas to jabber:component:accept, thanks JaredH!
Matthew Wild <mwild1@gmail.com>
parents: 1108
diff changeset
68 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
69 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
70 end
19269d278c38 mod_component: Rewrite jabber:client stanzas to jabber:component:accept, thanks JaredH!
Matthew Wild <mwild1@gmail.com>
parents: 1108
diff changeset
71 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
72 end);
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 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
74 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
75 else
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 -- Signal successful authentication
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 session.send(st.stanza("handshake"));
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 module:add_handler("component", "handshake", xmlns_component, handle_component_auth);

mercurial