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
|
|
|
|
%%%
|
|
|
|
%%%
|
2023-01-09 17:09:06 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2023 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').
|
|
|
|
|
2022-11-07 12:32:38 +01:00
|
|
|
-protocol({xep, 355, '0.4.1', '16.09', "", ""}).
|
2016-11-13 14:46:04 +01:00
|
|
|
|
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
|
2018-01-23 08:54:52 +01:00
|
|
|
-export([start/2, stop/1, reload/3, mod_opt_type/1, depends/2, mod_options/1]).
|
2020-01-08 10:24:51 +01:00
|
|
|
-export([mod_doc/0]).
|
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("logger.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2016-11-12 11:27:15 +01:00
|
|
|
|
2019-07-08 08:46:50 +02:00
|
|
|
-type route_type() :: ejabberd_sm | ejabberd_local.
|
|
|
|
-type delegations() :: #{{binary(), route_type()} => {binary(), disco_info()}}.
|
|
|
|
-record(state, {server_host = <<"">> :: binary()}).
|
2016-11-12 11:27:15 +01:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% 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.
|
|
|
|
|
2017-04-30 18:01:47 +02:00
|
|
|
mod_opt_type(namespaces) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:and_then(
|
|
|
|
econf:map(
|
|
|
|
econf:binary(),
|
|
|
|
econf:options(
|
|
|
|
#{filtering => econf:list(econf:binary()),
|
|
|
|
access => econf:acl()})),
|
|
|
|
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).
|
|
|
|
|
|
|
|
-spec mod_options(binary()) -> [{namespaces,
|
|
|
|
[{binary(), [binary()], acl:acl()}]} |
|
|
|
|
{atom(), term()}].
|
2018-02-11 10:54:15 +01:00
|
|
|
mod_options(_Host) ->
|
|
|
|
[{namespaces, []}].
|
2016-11-12 11:27:15 +01:00
|
|
|
|
2020-01-08 10:24:51 +01:00
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
2020-04-08 18:49:41 +02:00
|
|
|
[?T("This module is an implementation of "
|
2020-01-08 10:24:51 +01:00
|
|
|
"https://xmpp.org/extensions/xep-0355.html"
|
|
|
|
"[XEP-0355: Namespace Delegation]. "
|
|
|
|
"Only admin mode has been implemented by now. "
|
|
|
|
"Namespace delegation allows external services to "
|
|
|
|
"handle IQ using specific namespace. This may be applied "
|
2020-04-08 18:49:41 +02:00
|
|
|
"for external PEP service."), "",
|
|
|
|
?T("WARNING: Security issue: Namespace delegation gives components "
|
|
|
|
"access to sensitive data, so permission should be granted "
|
|
|
|
"carefully, only if you trust the component."), "",
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("NOTE: This module is complementary to _`mod_privilege`_ but can "
|
2020-04-08 18:49:41 +02:00
|
|
|
"also be used separately.")],
|
2020-01-08 10:24:51 +01:00
|
|
|
opts =>
|
|
|
|
[{namespaces,
|
|
|
|
#{value => "{Namespace: Options}",
|
|
|
|
desc =>
|
|
|
|
?T("If you want to delegate namespaces to a component, "
|
|
|
|
"specify them in this option, and associate them "
|
|
|
|
"to an access rule. The 'Options' are:")},
|
|
|
|
[{filtering,
|
|
|
|
#{value => ?T("Attributes"),
|
|
|
|
desc =>
|
|
|
|
?T("The list of attributes. Currently not used.")}},
|
|
|
|
{access,
|
|
|
|
#{value => ?T("AccessName"),
|
|
|
|
desc =>
|
|
|
|
?T("The option defines which components are allowed "
|
|
|
|
"for namespace delegation. The default value is 'none'.")}}]}],
|
|
|
|
example =>
|
2020-04-08 18:49:41 +02:00
|
|
|
[{?T("Make sure you do not delegate the same namespace to several "
|
|
|
|
"services at the same time. As in the example provided later, "
|
|
|
|
"to have the 'sat-pubsub.example.org' component perform "
|
|
|
|
"correctly disable the 'mod_pubsub' module."),
|
2020-01-08 10:24:51 +01:00
|
|
|
["access_rules:",
|
|
|
|
" external_pubsub:",
|
|
|
|
" allow: external_component",
|
|
|
|
" external_mam:",
|
|
|
|
" allow: external_component",
|
|
|
|
"",
|
|
|
|
"acl:",
|
|
|
|
" external_component:",
|
|
|
|
" server: sat-pubsub.example.org",
|
|
|
|
"",
|
|
|
|
"modules:",
|
|
|
|
" ...",
|
|
|
|
" mod_delegation:",
|
|
|
|
" namespaces:",
|
|
|
|
" urn:xmpp:mam:1:",
|
|
|
|
" access: external_mam",
|
|
|
|
" http://jabber.org/protocol/pubsub:",
|
2020-04-08 18:49:41 +02:00
|
|
|
" access: external_pubsub"]}]}.
|
2020-01-08 10:24:51 +01:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
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})
|
2019-06-14 11:33:26 +02:00
|
|
|
end, ejabberd_option:hosts()).
|
2016-11-12 11:27:15 +01:00
|
|
|
|
|
|
|
-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})
|
2019-06-14 11:33:26 +02:00
|
|
|
end, ejabberd_option:hosts()).
|
2016-11-12 11:27:15 +01:00
|
|
|
|
|
|
|
-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).
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec disco_local_features(mod_disco:features_acc(), jid(), jid(),
|
|
|
|
binary(), binary()) -> mod_disco:features_acc().
|
2016-11-12 11:27:15 +01:00
|
|
|
disco_local_features(Acc, From, To, Node, Lang) ->
|
|
|
|
disco_features(Acc, From, To, Node, Lang, ejabberd_local).
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec disco_sm_features(mod_disco:features_acc(), jid(), jid(),
|
|
|
|
binary(), binary()) -> mod_disco:features_acc().
|
2016-11-12 11:27:15 +01:00
|
|
|
disco_sm_features(Acc, From, To, Node, Lang) ->
|
|
|
|
disco_features(Acc, From, To, Node, Lang, ejabberd_sm).
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec disco_local_identity([identity()], jid(), jid(), binary(), binary()) -> [identity()].
|
2016-11-12 11:27:15 +01:00
|
|
|
disco_local_identity(Acc, From, To, Node, Lang) ->
|
|
|
|
disco_identity(Acc, From, To, Node, Lang, ejabberd_local).
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec disco_sm_identity([identity()], jid(), jid(), binary(), binary()) -> [identity()].
|
2016-11-12 11:27:15 +01:00
|
|
|
disco_sm_identity(Acc, From, To, Node, Lang) ->
|
|
|
|
disco_identity(Acc, From, To, Node, Lang, ejabberd_sm).
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%===================================================================
|
2019-08-04 20:46:18 +02:00
|
|
|
init([Host|_]) ->
|
2017-02-14 08:25:08 +01:00
|
|
|
process_flag(trap_exit, true),
|
2019-07-08 08:46:50 +02:00
|
|
|
catch ets:new(?MODULE,
|
|
|
|
[named_table, public,
|
|
|
|
{heir, erlang:group_leader(), none}]),
|
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}}.
|
|
|
|
|
2019-07-08 08:46:50 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2016-11-12 11:27:15 +01:00
|
|
|
|
|
|
|
handle_cast({component_connected, Host}, State) ->
|
|
|
|
ServerHost = State#state.server_host,
|
|
|
|
To = jid:make(Host),
|
2019-06-14 11:33:26 +02:00
|
|
|
NSAttrsAccessList = mod_delegation_opt:namespaces(ServerHost),
|
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 ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Denied delegation for ~ts on ~ts", [Host, NS])
|
2016-11-12 11:27:15 +01:00
|
|
|
end
|
|
|
|
end, NSAttrsAccessList),
|
|
|
|
{noreply, State};
|
|
|
|
handle_cast({component_disconnected, Host}, State) ->
|
|
|
|
ServerHost = State#state.server_host,
|
|
|
|
Delegations =
|
2019-07-08 08:46:50 +02:00
|
|
|
maps:filter(
|
2016-11-12 11:27:15 +01:00
|
|
|
fun({NS, Type}, {H, _}) when H == Host ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Remove delegation of namespace '~ts' "
|
|
|
|
"from external component '~ts'",
|
2016-11-12 11:27:15 +01:00
|
|
|
[NS, Host]),
|
|
|
|
gen_iq_handler:remove_iq_handler(Type, ServerHost, NS),
|
|
|
|
false;
|
|
|
|
(_, _) ->
|
|
|
|
true
|
2019-07-08 08:46:50 +02:00
|
|
|
end, get_delegations(ServerHost)),
|
|
|
|
set_delegations(ServerHost, Delegations),
|
|
|
|
{noreply, State};
|
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2016-11-12 11:27:15 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2017-11-10 16:02:22 +01:00
|
|
|
handle_info({iq_reply, ResIQ, {disco_info, Type, Host, NS}}, State) ->
|
2019-07-08 08:46:50 +02:00
|
|
|
case ResIQ of
|
|
|
|
#iq{type = result, sub_els = [SubEl]} ->
|
|
|
|
try xmpp:decode(SubEl) of
|
|
|
|
#disco_info{} = Info ->
|
|
|
|
ServerHost = State#state.server_host,
|
|
|
|
process_disco_info(ServerHost, Type, Host, NS, Info)
|
|
|
|
catch _:{xmpp_codec, _} ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
2017-11-10 16:02:22 +01:00
|
|
|
handle_info({iq_reply, ResIQ, #iq{} = IQ}, State) ->
|
|
|
|
process_iq_result(IQ, ResIQ),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info(Info, State) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2016-11-12 11:27:15 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, State) ->
|
|
|
|
ServerHost = State#state.server_host,
|
2019-07-29 09:46:20 +02:00
|
|
|
case gen_mod:is_loaded_elsewhere(ServerHost, ?MODULE) of
|
|
|
|
false ->
|
|
|
|
ejabberd_hooks:delete(component_connected, ?MODULE,
|
|
|
|
component_connected, 50),
|
|
|
|
ejabberd_hooks:delete(component_disconnected, ?MODULE,
|
|
|
|
component_disconnected, 50);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2016-11-12 11:27:15 +01:00
|
|
|
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)
|
2019-07-08 08:46:50 +02:00
|
|
|
end, maps:keys(get_delegations(ServerHost))),
|
|
|
|
ets:delete(?MODULE, ServerHost).
|
2016-11-12 11:27:15 +01:00
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2019-07-08 08:46:50 +02:00
|
|
|
-spec get_delegations(binary()) -> delegations().
|
2016-11-12 11:27:15 +01:00
|
|
|
get_delegations(Host) ->
|
2019-07-08 08:46:50 +02:00
|
|
|
try ets:lookup_element(?MODULE, Host, 2)
|
|
|
|
catch _:badarg -> #{}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec set_delegations(binary(), delegations()) -> true.
|
|
|
|
set_delegations(ServerHost, Delegations) ->
|
|
|
|
case maps:size(Delegations) of
|
|
|
|
0 -> ets:delete(?MODULE, ServerHost);
|
|
|
|
_ -> ets:insert(?MODULE, {ServerHost, Delegations})
|
2016-09-07 14:34:31 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-08 08:46:50 +02:00
|
|
|
-spec process_iq(iq(), route_type()) -> ignore | iq().
|
2016-11-12 11:27:15 +01:00
|
|
|
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),
|
2019-07-08 08:46:50 +02:00
|
|
|
case maps:find({NS, Type}, Delegations) of
|
2016-11-12 11:27:15 +01:00
|
|
|
{ok, {Host, _}} ->
|
2016-11-13 11:41:04 +01:00
|
|
|
Delegation = #delegation{
|
2018-01-25 18:02:47 +01:00
|
|
|
forwarded = #forwarded{sub_els = [IQ]}},
|
2016-11-12 11:27:15 +01:00
|
|
|
NewFrom = jid:make(LServer),
|
|
|
|
NewTo = jid:make(Host),
|
2017-11-10 16:02:22 +01:00
|
|
|
ejabberd_router:route_iq(
|
2016-11-12 11:27:15 +01:00
|
|
|
#iq{type = set,
|
|
|
|
from = NewFrom,
|
|
|
|
to = NewTo,
|
|
|
|
sub_els = [Delegation]},
|
2017-11-10 16:02:22 +01:00
|
|
|
IQ, gen_mod:get_module_proc(LServer, ?MODULE)),
|
2016-11-12 11:27:15 +01:00
|
|
|
ignore;
|
|
|
|
error ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Failed to map delegated namespace to external component"),
|
2016-11-12 11:27:15 +01:00
|
|
|
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
|
2019-06-21 20:06:32 +02:00
|
|
|
CodecOpts = ejabberd_config:codec_options(),
|
2018-01-25 18:02:47 +01:00
|
|
|
#delegation{forwarded = #forwarded{sub_els = [SubEl]}} =
|
2016-11-13 11:41:04 +01:00
|
|
|
xmpp:get_subtag(ResIQ, #delegation{}),
|
2018-02-09 16:12:50 +01:00
|
|
|
case xmpp:decode(SubEl, ?NS_CLIENT, CodecOpts) of
|
2016-11-13 11:41:04 +01:00
|
|
|
#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 _:_ ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Got iq-result with invalid delegated "
|
2019-09-23 14:17:20 +02:00
|
|
|
"payload:~n~ts", [xmpp:pp(ResIQ)]),
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("External component failure"),
|
2016-11-12 11:27:15 +01:00
|
|
|
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) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("External component timeout"),
|
2016-11-12 11:27:15 +01:00
|
|
|
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
|
|
|
|
2019-07-08 08:46:50 +02:00
|
|
|
-spec process_disco_info(binary(), route_type(),
|
|
|
|
binary(), binary(), disco_info()) -> ok.
|
|
|
|
process_disco_info(ServerHost, Type, Host, NS, Info) ->
|
|
|
|
From = jid:make(ServerHost),
|
2017-11-10 16:02:22 +01:00
|
|
|
To = jid:make(Host),
|
2019-07-08 08:46:50 +02:00
|
|
|
Delegations = get_delegations(ServerHost),
|
|
|
|
case maps:find({NS, Type}, Delegations) of
|
2017-11-10 16:02:22 +01:00
|
|
|
error ->
|
|
|
|
Msg = #message{from = From, to = To,
|
|
|
|
sub_els = [#delegation{delegated = [#delegated{ns = NS}]}]},
|
2019-07-08 08:46:50 +02:00
|
|
|
Delegations1 = maps:put({NS, Type}, {Host, Info}, Delegations),
|
|
|
|
gen_iq_handler:add_iq_handler(Type, ServerHost, NS, ?MODULE, Type),
|
2017-11-10 16:02:22 +01:00
|
|
|
ejabberd_router:route(Msg),
|
2019-07-08 08:46:50 +02:00
|
|
|
set_delegations(ServerHost, Delegations1),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Namespace '~ts' is delegated to external component '~ts'",
|
2019-07-08 08:46:50 +02:00
|
|
|
[NS, Host]);
|
2017-11-10 16:02:22 +01:00
|
|
|
{ok, {AnotherHost, _}} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Failed to delegate namespace '~ts' to "
|
|
|
|
"external component '~ts' because it's already "
|
|
|
|
"delegated to '~ts'",
|
2019-07-08 08:46:50 +02:00
|
|
|
[NS, Host, AnotherHost])
|
2017-11-10 16:02:22 +01:00
|
|
|
end.
|
|
|
|
|
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}) ->
|
2017-11-10 16:02:22 +01:00
|
|
|
ejabberd_router:route_iq(
|
2017-02-16 09:00:26 +01:00
|
|
|
#iq{type = get, from = From, to = To,
|
|
|
|
sub_els = [#disco_info{node = Node}]},
|
2017-11-10 16:02:22 +01:00
|
|
|
{disco_info, Type, Host, NS},
|
|
|
|
gen_mod:get_module_proc(LServer, ?MODULE))
|
2016-11-12 11:27:15 +01:00
|
|
|
end, [{ejabberd_local, <<(?NS_DELEGATION)/binary, "::", NS/binary>>},
|
|
|
|
{ejabberd_sm, <<(?NS_DELEGATION)/binary, ":bare:", NS/binary>>}]).
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec disco_features(mod_disco:features_acc(), jid(), jid(), binary(), binary(),
|
|
|
|
route_type()) -> mod_disco:features_acc().
|
2016-11-12 11:27:15 +01:00
|
|
|
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;
|
|
|
|
(_) ->
|
|
|
|
[]
|
2019-07-08 08:46:50 +02:00
|
|
|
end, maps:to_list(Delegations)),
|
2016-11-12 11:27:15 +01:00
|
|
|
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
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec disco_identity([identity()], jid(), jid(), binary(), binary(),
|
|
|
|
route_type()) -> [identity()].
|
2016-11-12 11:27:15 +01:00
|
|
|
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;
|
|
|
|
(_) ->
|
|
|
|
[]
|
2019-07-08 08:46:50 +02:00
|
|
|
end, maps:to_list(Delegations)),
|
2019-07-10 09:31:51 +02:00
|
|
|
Acc ++ Identities;
|
2016-11-21 12:21:34 +01:00
|
|
|
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) -> [].
|