2016-11-12 11:27:15 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-11-13 14:46:04 +01:00
|
|
|
%%% File : mod_delegation.erl
|
|
|
|
%%% Author : Anna Mukharram <amuhar3@gmail.com>
|
|
|
|
%%% Purpose : XEP-0355: Namespace Delegation
|
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
2016-11-13 14:46:04 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2016-11-12 11:27:15 +01:00
|
|
|
%%%
|
|
|
|
%%%-------------------------------------------------------------------
|
2016-09-07 14:34:31 +02:00
|
|
|
-module(mod_delegation).
|
|
|
|
|
2016-11-13 14:46:04 +01:00
|
|
|
-author('amuhar3@gmail.com').
|
|
|
|
|
|
|
|
-protocol({xep, 0355, '0.3'}).
|
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-behaviour(gen_server).
|
2016-09-07 14:34:31 +02:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
%% API
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, mod_opt_type/1, depends/2]).
|
2016-11-12 11:27:15 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
-export([component_connected/1, component_disconnected/2,
|
2016-11-13 12:17:21 +01:00
|
|
|
ejabberd_local/1, ejabberd_sm/1, decode_iq_subel/1,
|
2016-11-12 11:27:15 +01:00
|
|
|
disco_local_features/5, disco_sm_features/5,
|
|
|
|
disco_local_identity/5, disco_sm_identity/5]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("logger.hrl").
|
|
|
|
-include("xmpp.hrl").
|
|
|
|
|
|
|
|
-type disco_acc() :: {error, stanza_error()} | {result, [binary()]} | empty.
|
|
|
|
-record(state, {server_host = <<"">> :: binary(),
|
|
|
|
delegations = dict:new() :: ?TDICT}).
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
start(Host, Opts) ->
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:start_child(?MODULE, Host, Opts).
|
2016-09-07 14:34:31 +02:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
stop(Host) ->
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:stop_child(?MODULE, Host).
|
2016-11-12 11:27:15 +01:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(_Host, _NewOpts, _OldOpts) ->
|
|
|
|
ok.
|
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
|
2017-04-30 18:01:47 +02:00
|
|
|
mod_opt_type(namespaces) ->
|
|
|
|
fun(L) ->
|
|
|
|
lists:map(
|
|
|
|
fun({NS, Opts}) ->
|
|
|
|
Attrs = proplists:get_value(filtering, Opts, []),
|
|
|
|
Access = proplists:get_value(access, Opts, none),
|
|
|
|
{NS, Attrs, Access}
|
|
|
|
end, L)
|
|
|
|
end;
|
2016-11-12 11:27:15 +01:00
|
|
|
mod_opt_type(_) ->
|
|
|
|
[namespaces, iqdisc].
|
|
|
|
|
|
|
|
depends(_, _) ->
|
|
|
|
[].
|
|
|
|
|
2017-02-16 12:18:36 +01:00
|
|
|
-spec decode_iq_subel(xmpp_element() | xmlel()) -> xmpp_element() | xmlel().
|
2016-11-13 12:17:21 +01:00
|
|
|
%% Tell gen_iq_handler not to auto-decode IQ payload
|
|
|
|
decode_iq_subel(El) ->
|
|
|
|
El.
|
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec component_connected(binary()) -> ok.
|
|
|
|
component_connected(Host) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(ServerHost) ->
|
|
|
|
Proc = gen_mod:get_module_proc(ServerHost, ?MODULE),
|
|
|
|
gen_server:cast(Proc, {component_connected, Host})
|
|
|
|
end, ?MYHOSTS).
|
|
|
|
|
|
|
|
-spec component_disconnected(binary(), binary()) -> ok.
|
|
|
|
component_disconnected(Host, _Reason) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(ServerHost) ->
|
|
|
|
Proc = gen_mod:get_module_proc(ServerHost, ?MODULE),
|
|
|
|
gen_server:cast(Proc, {component_disconnected, Host})
|
|
|
|
end, ?MYHOSTS).
|
|
|
|
|
|
|
|
-spec ejabberd_local(iq()) -> iq().
|
|
|
|
ejabberd_local(IQ) ->
|
|
|
|
process_iq(IQ, ejabberd_local).
|
|
|
|
|
|
|
|
-spec ejabberd_sm(iq()) -> iq().
|
|
|
|
ejabberd_sm(IQ) ->
|
|
|
|
process_iq(IQ, ejabberd_sm).
|
|
|
|
|
|
|
|
-spec disco_local_features(disco_acc(), jid(), jid(), binary(), binary()) -> disco_acc().
|
|
|
|
disco_local_features(Acc, From, To, Node, Lang) ->
|
|
|
|
disco_features(Acc, From, To, Node, Lang, ejabberd_local).
|
|
|
|
|
|
|
|
-spec disco_sm_features(disco_acc(), jid(), jid(), binary(), binary()) -> disco_acc().
|
|
|
|
disco_sm_features(Acc, From, To, Node, Lang) ->
|
|
|
|
disco_features(Acc, From, To, Node, Lang, ejabberd_sm).
|
|
|
|
|
|
|
|
-spec disco_local_identity(disco_acc(), jid(), jid(), binary(), binary()) -> disco_acc().
|
|
|
|
disco_local_identity(Acc, From, To, Node, Lang) ->
|
|
|
|
disco_identity(Acc, From, To, Node, Lang, ejabberd_local).
|
|
|
|
|
|
|
|
-spec disco_sm_identity(disco_acc(), jid(), jid(), binary(), binary()) -> disco_acc().
|
|
|
|
disco_sm_identity(Acc, From, To, Node, Lang) ->
|
|
|
|
disco_identity(Acc, From, To, Node, Lang, ejabberd_sm).
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%===================================================================
|
|
|
|
init([Host, _Opts]) ->
|
2017-02-14 08:25:08 +01:00
|
|
|
process_flag(trap_exit, true),
|
2016-11-12 11:27:15 +01:00
|
|
|
ejabberd_hooks:add(component_connected, ?MODULE,
|
|
|
|
component_connected, 50),
|
|
|
|
ejabberd_hooks:add(component_disconnected, ?MODULE,
|
|
|
|
component_disconnected, 50),
|
2016-09-07 14:34:31 +02:00
|
|
|
ejabberd_hooks:add(disco_local_features, Host, ?MODULE,
|
2016-11-12 11:27:15 +01:00
|
|
|
disco_local_features, 50),
|
|
|
|
ejabberd_hooks:add(disco_sm_features, Host, ?MODULE,
|
|
|
|
disco_sm_features, 50),
|
2016-09-07 14:34:31 +02:00
|
|
|
ejabberd_hooks:add(disco_local_identity, Host, ?MODULE,
|
2016-11-12 11:27:15 +01:00
|
|
|
disco_local_identity, 50),
|
2016-09-07 14:34:31 +02:00
|
|
|
ejabberd_hooks:add(disco_sm_identity, Host, ?MODULE,
|
2016-11-12 11:27:15 +01:00
|
|
|
disco_sm_identity, 50),
|
|
|
|
{ok, #state{server_host = Host}}.
|
|
|
|
|
|
|
|
handle_call(get_delegations, _From, State) ->
|
|
|
|
{reply, {ok, State#state.delegations}, State};
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, State}.
|
|
|
|
|
|
|
|
handle_cast({component_connected, Host}, State) ->
|
|
|
|
ServerHost = State#state.server_host,
|
|
|
|
To = jid:make(Host),
|
|
|
|
NSAttrsAccessList = gen_mod:get_module_opt(
|
2017-04-30 18:01:47 +02:00
|
|
|
ServerHost, ?MODULE, namespaces, []),
|
2016-11-12 11:27:15 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun({NS, _Attrs, Access}) ->
|
|
|
|
case acl:match_rule(ServerHost, Access, To) of
|
|
|
|
allow ->
|
|
|
|
send_disco_queries(ServerHost, Host, NS);
|
|
|
|
deny ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, NSAttrsAccessList),
|
|
|
|
{noreply, State};
|
|
|
|
handle_cast({disco_info, Type, Host, NS, Info}, State) ->
|
|
|
|
From = jid:make(State#state.server_host),
|
|
|
|
To = jid:make(Host),
|
|
|
|
case dict:find({NS, Type}, State#state.delegations) of
|
|
|
|
error ->
|
|
|
|
Msg = #message{from = From, to = To,
|
|
|
|
sub_els = [#delegation{delegated = [#delegated{ns = NS}]}]},
|
|
|
|
Delegations = dict:store({NS, Type}, {Host, Info}, State#state.delegations),
|
|
|
|
gen_iq_handler:add_iq_handler(Type, State#state.server_host, NS,
|
2017-05-04 11:24:47 +02:00
|
|
|
?MODULE, Type, gen_iq_handler:iqdisc(Host)),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(Msg),
|
2016-11-12 11:27:15 +01:00
|
|
|
?INFO_MSG("Namespace '~s' is delegated to external component '~s'",
|
|
|
|
[NS, Host]),
|
|
|
|
{noreply, State#state{delegations = Delegations}};
|
|
|
|
{ok, {AnotherHost, _}} ->
|
|
|
|
?WARNING_MSG("Failed to delegate namespace '~s' to "
|
|
|
|
"external component '~s' because it's already "
|
|
|
|
"delegated to '~s'",
|
|
|
|
[NS, Host, AnotherHost]),
|
|
|
|
{noreply, State}
|
2016-09-07 14:34:31 +02:00
|
|
|
end;
|
2016-11-12 11:27:15 +01:00
|
|
|
handle_cast({component_disconnected, Host}, State) ->
|
|
|
|
ServerHost = State#state.server_host,
|
|
|
|
Delegations =
|
|
|
|
dict:filter(
|
|
|
|
fun({NS, Type}, {H, _}) when H == Host ->
|
|
|
|
?INFO_MSG("Remove delegation of namespace '~s' "
|
|
|
|
"from external component '~s'",
|
|
|
|
[NS, Host]),
|
|
|
|
gen_iq_handler:remove_iq_handler(Type, ServerHost, NS),
|
|
|
|
false;
|
|
|
|
(_, _) ->
|
|
|
|
true
|
|
|
|
end, State#state.delegations),
|
|
|
|
{noreply, State#state{delegations = Delegations}};
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, State) ->
|
|
|
|
%% Note: we don't remove component_* hooks because they are global
|
|
|
|
%% and might be registered within a module on another virtual host
|
|
|
|
ServerHost = State#state.server_host,
|
|
|
|
ejabberd_hooks:delete(disco_local_features, ServerHost, ?MODULE,
|
|
|
|
disco_local_features, 50),
|
|
|
|
ejabberd_hooks:delete(disco_sm_features, ServerHost, ?MODULE,
|
|
|
|
disco_sm_features, 50),
|
|
|
|
ejabberd_hooks:delete(disco_local_identity, ServerHost, ?MODULE,
|
|
|
|
disco_local_identity, 50),
|
|
|
|
ejabberd_hooks:delete(disco_sm_identity, ServerHost, ?MODULE,
|
|
|
|
disco_sm_identity, 50),
|
|
|
|
lists:foreach(
|
|
|
|
fun({NS, Type}) ->
|
|
|
|
gen_iq_handler:remove_iq_handler(Type, ServerHost, NS)
|
|
|
|
end, dict:fetch_keys(State#state.delegations)).
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
-spec get_delegations(binary()) -> ?TDICT.
|
|
|
|
get_delegations(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
|
|
|
try gen_server:call(Proc, get_delegations) of
|
|
|
|
{ok, Delegations} -> Delegations
|
|
|
|
catch exit:{noproc, _} ->
|
|
|
|
%% No module is loaded for this virtual host
|
|
|
|
dict:new()
|
2016-09-07 14:34:31 +02:00
|
|
|
end.
|
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec process_iq(iq(), ejabberd_local | ejabberd_sm) -> ignore | iq().
|
|
|
|
process_iq(#iq{to = To, lang = Lang, sub_els = [SubEl]} = IQ, Type) ->
|
|
|
|
LServer = To#jid.lserver,
|
|
|
|
NS = xmpp:get_ns(SubEl),
|
|
|
|
Delegations = get_delegations(LServer),
|
|
|
|
case dict:find({NS, Type}, Delegations) of
|
|
|
|
{ok, {Host, _}} ->
|
2016-11-13 11:41:04 +01:00
|
|
|
Delegation = #delegation{
|
|
|
|
forwarded = #forwarded{xml_els = [xmpp:encode(IQ)]}},
|
2016-11-12 11:27:15 +01:00
|
|
|
NewFrom = jid:make(LServer),
|
|
|
|
NewTo = jid:make(Host),
|
|
|
|
ejabberd_local:route_iq(
|
|
|
|
#iq{type = set,
|
|
|
|
from = NewFrom,
|
|
|
|
to = NewTo,
|
|
|
|
sub_els = [Delegation]},
|
|
|
|
fun(Result) -> process_iq_result(IQ, Result) end),
|
|
|
|
ignore;
|
|
|
|
error ->
|
|
|
|
Txt = <<"Failed to map delegated namespace to external component">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang))
|
2016-09-07 14:34:31 +02:00
|
|
|
end.
|
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec process_iq_result(iq(), iq()) -> ok.
|
|
|
|
process_iq_result(#iq{from = From, to = To, id = ID, lang = Lang} = IQ,
|
|
|
|
#iq{type = result} = ResIQ) ->
|
2016-11-13 11:41:04 +01:00
|
|
|
try
|
|
|
|
#delegation{forwarded = #forwarded{xml_els = [SubEl]}} =
|
|
|
|
xmpp:get_subtag(ResIQ, #delegation{}),
|
|
|
|
case xmpp:decode(SubEl, ?NS_CLIENT, [ignore_els]) of
|
|
|
|
#iq{from = To, to = From, type = Type, id = ID} = Reply
|
|
|
|
when Type == error; Type == result ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(Reply)
|
2016-11-13 11:41:04 +01:00
|
|
|
end
|
|
|
|
catch _:_ ->
|
2016-11-12 11:27:15 +01:00
|
|
|
?ERROR_MSG("got iq-result with invalid delegated "
|
|
|
|
"payload:~n~s", [xmpp:pp(ResIQ)]),
|
|
|
|
Txt = <<"External component failure">>,
|
|
|
|
Err = xmpp:err_internal_server_error(Txt, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(IQ, Err)
|
2016-09-07 14:34:31 +02:00
|
|
|
end;
|
2016-11-12 11:27:15 +01:00
|
|
|
process_iq_result(#iq{from = From, to = To}, #iq{type = error} = ResIQ) ->
|
|
|
|
Err = xmpp:set_from_to(ResIQ, To, From),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(Err);
|
|
|
|
process_iq_result(#iq{lang = Lang} = IQ, timeout) ->
|
2016-11-12 11:27:15 +01:00
|
|
|
Txt = <<"External component timeout">>,
|
|
|
|
Err = xmpp:err_internal_server_error(Txt, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(IQ, Err).
|
2016-11-12 11:27:15 +01:00
|
|
|
|
|
|
|
-spec send_disco_queries(binary(), binary(), binary()) -> ok.
|
|
|
|
send_disco_queries(LServer, Host, NS) ->
|
|
|
|
From = jid:make(LServer),
|
|
|
|
To = jid:make(Host),
|
|
|
|
lists:foreach(
|
|
|
|
fun({Type, Node}) ->
|
|
|
|
ejabberd_local:route_iq(
|
2017-02-16 09:00:26 +01:00
|
|
|
#iq{type = get, from = From, to = To,
|
|
|
|
sub_els = [#disco_info{node = Node}]},
|
2016-11-13 14:56:05 +01:00
|
|
|
fun(#iq{type = result, sub_els = [SubEl]}) ->
|
|
|
|
try xmpp:decode(SubEl) of
|
|
|
|
#disco_info{} = Info->
|
|
|
|
Proc = gen_mod:get_module_proc(LServer, ?MODULE),
|
|
|
|
gen_server:cast(
|
|
|
|
Proc, {disco_info, Type, Host, NS, Info});
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
catch _:{xmpp_codec, _} ->
|
|
|
|
ok
|
|
|
|
end;
|
2016-11-12 11:27:15 +01:00
|
|
|
(_) ->
|
|
|
|
ok
|
|
|
|
end)
|
|
|
|
end, [{ejabberd_local, <<(?NS_DELEGATION)/binary, "::", NS/binary>>},
|
|
|
|
{ejabberd_sm, <<(?NS_DELEGATION)/binary, ":bare:", NS/binary>>}]).
|
|
|
|
|
|
|
|
-spec disco_features(disco_acc(), jid(), jid(), binary(), binary(),
|
|
|
|
ejabberd_local | ejabberd_sm) -> disco_acc().
|
|
|
|
disco_features(Acc, _From, To, <<"">>, _Lang, Type) ->
|
|
|
|
Delegations = get_delegations(To#jid.lserver),
|
|
|
|
Features = my_features(Type) ++
|
|
|
|
lists:flatmap(
|
|
|
|
fun({{_, T}, {_, Info}}) when T == Type ->
|
|
|
|
Info#disco_info.features;
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, dict:to_list(Delegations)),
|
|
|
|
case Acc of
|
|
|
|
empty when Features /= [] -> {result, Features};
|
|
|
|
{result, Fs} -> {result, Fs ++ Features};
|
|
|
|
_ -> Acc
|
|
|
|
end;
|
|
|
|
disco_features(Acc, _, _, _, _, _) ->
|
|
|
|
Acc.
|
2016-09-07 14:34:31 +02:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec disco_identity(disco_acc(), jid(), jid(), binary(), binary(),
|
|
|
|
ejabberd_local | ejabberd_sm) -> disco_acc().
|
|
|
|
disco_identity(Acc, _From, To, <<"">>, _Lang, Type) ->
|
|
|
|
Delegations = get_delegations(To#jid.lserver),
|
|
|
|
Identities = lists:flatmap(
|
|
|
|
fun({{_, T}, {_, Info}}) when T == Type ->
|
|
|
|
Info#disco_info.identities;
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, dict:to_list(Delegations)),
|
|
|
|
case Acc of
|
|
|
|
empty when Identities /= [] -> {result, Identities};
|
|
|
|
{result, Ids} -> {result, Ids ++ Identities};
|
|
|
|
Acc -> Acc
|
2016-11-21 12:21:34 +01:00
|
|
|
end;
|
|
|
|
disco_identity(Acc, _From, _To, _Node, _Lang, _Type) ->
|
|
|
|
Acc.
|
2016-09-07 14:34:31 +02:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
my_features(ejabberd_local) -> [?NS_DELEGATION];
|
|
|
|
my_features(ejabberd_sm) -> [].
|