plugins/mod_component.lua

Mon, 04 May 2009 19:36:16 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 04 May 2009 19:36:16 +0100
changeset 1108
368754c54045
parent 1065
3806173670f2
child 1405
19269d278c38
permissions
-rw-r--r--

mod_component: Vastly reduce the code, having split most of it to where it should be, xmppcomponent_listener

902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM v0.4
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
1065
3806173670f2 mod_*: Fix many unnecessary global accesses in modules (already committed to main repo)
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
13 local hosts = _G.hosts;
3806173670f2 mod_*: Fix many unnecessary global accesses in modules (already committed to main repo)
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
1065
3806173670f2 mod_*: Fix many unnecessary global accesses in modules (already committed to main repo)
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
17 local lxp = require "lxp";
3806173670f2 mod_*: Fix many unnecessary global accesses in modules (already committed to main repo)
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
18 local logger = require "util.logger";
3806173670f2 mod_*: Fix many unnecessary global accesses in modules (already committed to main repo)
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;
1065
3806173670f2 mod_*: Fix many unnecessary global accesses in modules (already committed to main repo)
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: 1065
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: 1065
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: 1065
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: 1065
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: 1065
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;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 session.component_session = cm_register_component(session.host, function (_, data) return send(data); end);
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: 1065
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)");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 -- Signal successful authentication
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 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
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 module:add_handler("component", "handshake", xmlns_component, handle_component_auth);

mercurial