plugins/mod_component.lua
author Waqas Hussain <waqas20@gmail.com>
Sun, 11 Jul 2010 21:21:38 +0500
changeset 3347 99f56bed5228
parent 3319 95fc08869273
child 3503 85e511e01d3c
permissions -rw-r--r--
mod_disco: Cache disco#info reply and caps hash for host, return correct caps hash, and respond to disco#info to the caps hash node.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
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
    27
	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
    28
	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
    29
		(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
    30
		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
    31
		return;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
	end
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 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
    35
	if not secret then
2490
6eee75fb3159 mod_component: Fix name of config option in error message (thanks nulani!)
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    36
		(session.log or log)("warn", "Component attempted to identify as %s, but component_secret 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
    37
		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
    38
		return;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
	end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
	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
    42
	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
    43
	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
    44
		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
    45
		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
    46
		return;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
	end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
	
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
	
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	-- 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
    51
	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
    52
	
3319
95fc08869273 mod_component: Read validate_from_addresses option from the config
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    53
	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
    54
	
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
	-- 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
    56
	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
    57
		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
    58
		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
    59
				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
    60
					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
    61
				end
19269d278c38 mod_component: Rewrite jabber:client stanzas to jabber:component:accept, thanks JaredH!
Matthew Wild <mwild1@gmail.com>
parents: 1108
diff changeset
    62
				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
    63
			end);
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
		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
    65
		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
    66
	else
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
		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
    68
	end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
	
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
	-- Signal successful authentication
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
	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
    72
end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
2495
9c32f469b82c mod_component: Cleaned up unused variables.
Waqas Hussain <waqas20@gmail.com>
parents: 2490
diff changeset
    74
module:add_handler("component", "handshake", "jabber:component:accept", handle_component_auth);