2002-12-07 21:27:26 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_s2s.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-12-13 22:36:53 +01:00
|
|
|
%%% Purpose : S2S connections manager
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 7 Dec 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
2007-12-24 12:41:41 +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.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% 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.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2002-12-07 21:27:26 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_s2s).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-22 13:11:11 +02:00
|
|
|
-protocol({xep, 220, '1.1'}).
|
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
2019-07-26 10:40:19 +02:00
|
|
|
-export([start_link/0, stop/0, route/1, have_connection/1,
|
2016-12-28 07:47:11 +01:00
|
|
|
get_connections_pids/1, try_register/1,
|
|
|
|
remove_connection/2, start_connection/2, start_connection/3,
|
2013-03-14 10:33:02 +01:00
|
|
|
dirty_get_connections/0, allow_host/2,
|
|
|
|
incoming_s2s_number/0, outgoing_s2s_number/0,
|
2017-08-07 15:06:07 +02:00
|
|
|
stop_s2s_connections/0,
|
2011-12-02 19:30:20 +01:00
|
|
|
clean_temporarily_blocked_table/0,
|
|
|
|
list_temporarily_blocked_hosts/0,
|
2014-04-28 01:42:02 +02:00
|
|
|
external_host_overloaded/1, is_temporarly_blocked/1,
|
2016-12-28 07:47:11 +01:00
|
|
|
get_commands_spec/0, zlib_enabled/1, get_idle_timeout/1,
|
2019-09-06 21:20:07 +02:00
|
|
|
tls_required/1, tls_enabled/1, tls_options/3,
|
2017-03-10 13:12:43 +01:00
|
|
|
host_up/1, host_down/1, queue_type/1]).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%% gen_server callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
|
|
|
handle_info/2, terminate/2, code_change/3]).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-export([get_info_s2s_connections/1]).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-27 09:45:08 +02:00
|
|
|
-include("xmpp.hrl").
|
2008-10-12 14:04:10 +02:00
|
|
|
-include("ejabberd_commands.hrl").
|
2019-07-09 15:42:24 +02:00
|
|
|
-include_lib("stdlib/include/ms_transform.hrl").
|
2018-12-13 11:45:45 +01:00
|
|
|
-include("ejabberd_stacktrace.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2014-04-28 01:42:02 +02:00
|
|
|
|
2007-09-25 18:32:18 +02:00
|
|
|
-define(DEFAULT_MAX_S2S_CONNECTIONS_NUMBER, 1).
|
|
|
|
-define(DEFAULT_MAX_S2S_CONNECTIONS_NUMBER_PER_NODE, 1).
|
2011-12-02 19:30:20 +01:00
|
|
|
-define(S2S_OVERLOAD_BLOCK_PERIOD, 60).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-07-16 21:07:39 +02:00
|
|
|
%% once a server is temporary blocked, it stay blocked for 60 seconds
|
2011-12-02 19:30:20 +01:00
|
|
|
|
2019-07-09 15:42:24 +02:00
|
|
|
-record(s2s, {fromto :: {binary(), binary()},
|
|
|
|
pid :: pid()}).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-record(state, {}).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2019-07-09 15:42:24 +02:00
|
|
|
-record(temporarily_blocked, {host :: binary(),
|
|
|
|
timestamp :: integer()}).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-type temporarily_blocked() :: #temporarily_blocked{}.
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
2019-07-09 15:42:24 +02:00
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-07-26 10:40:19 +02:00
|
|
|
-spec stop() -> ok.
|
|
|
|
stop() ->
|
|
|
|
_ = supervisor:terminate_child(ejabberd_sup, ?MODULE),
|
|
|
|
_ = supervisor:delete_child(ejabberd_sup, ?MODULE),
|
|
|
|
ok.
|
|
|
|
|
2011-12-02 19:30:20 +01:00
|
|
|
clean_temporarily_blocked_table() ->
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:clear_table(temporarily_blocked).
|
|
|
|
|
|
|
|
-spec list_temporarily_blocked_hosts() -> [temporarily_blocked()].
|
2011-12-02 19:30:20 +01:00
|
|
|
list_temporarily_blocked_hosts() ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ets:tab2list(temporarily_blocked).
|
|
|
|
|
|
|
|
-spec external_host_overloaded(binary()) -> {aborted, any()} | {atomic, ok}.
|
2011-12-02 19:30:20 +01:00
|
|
|
external_host_overloaded(Host) ->
|
2019-07-09 15:42:24 +02:00
|
|
|
?INFO_MSG("Disabling s2s connections to ~s for ~p seconds",
|
2013-03-14 10:33:02 +01:00
|
|
|
[Host, ?S2S_OVERLOAD_BLOCK_PERIOD]),
|
|
|
|
mnesia:transaction(fun () ->
|
2019-02-27 09:56:20 +01:00
|
|
|
Time = erlang:monotonic_time(),
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:write(#temporarily_blocked{host = Host,
|
2015-12-07 16:08:57 +01:00
|
|
|
timestamp = Time})
|
2013-03-14 10:33:02 +01:00
|
|
|
end).
|
|
|
|
|
|
|
|
-spec is_temporarly_blocked(binary()) -> boolean().
|
2011-12-02 19:30:20 +01:00
|
|
|
is_temporarly_blocked(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case mnesia:dirty_read(temporarily_blocked, Host) of
|
2019-07-09 15:42:24 +02:00
|
|
|
[] -> false;
|
|
|
|
[#temporarily_blocked{timestamp = T} = Entry] ->
|
|
|
|
Diff = erlang:monotonic_time() - T,
|
|
|
|
case erlang:convert_time_unit(Diff, native, microsecond) of
|
|
|
|
N when N > (?S2S_OVERLOAD_BLOCK_PERIOD) * 1000 * 1000 ->
|
|
|
|
mnesia:dirty_delete_object(Entry), false;
|
|
|
|
_ -> true
|
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end.
|
2011-12-02 19:30:20 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec remove_connection({binary(), binary()}, pid()) -> ok.
|
2019-07-09 15:42:24 +02:00
|
|
|
remove_connection({From, To} = FromTo, Pid) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case mnesia:dirty_match_object(s2s, #s2s{fromto = FromTo, pid = Pid}) of
|
|
|
|
[#s2s{pid = Pid}] ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete_object(#s2s{fromto = FromTo, pid = Pid})
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, _} -> ok;
|
|
|
|
{aborted, Reason} ->
|
2019-07-09 15:42:24 +02:00
|
|
|
?ERROR_MSG("Failed to unregister s2s connection ~s -> ~s: "
|
|
|
|
"Mnesia failure: ~p",
|
|
|
|
[From, To, Reason])
|
2019-06-14 11:33:26 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
2007-07-18 18:26:50 +02:00
|
|
|
end.
|
2007-07-15 09:28:47 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec have_connection({binary(), binary()}) -> boolean().
|
2003-01-05 21:24:59 +01:00
|
|
|
have_connection(FromTo) ->
|
2003-01-19 21:17:56 +01:00
|
|
|
case catch mnesia:dirty_read(s2s, FromTo) of
|
2019-07-09 15:42:24 +02:00
|
|
|
[_] ->
|
2007-10-30 18:38:40 +01:00
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
2002-12-07 21:27:26 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec get_connections_pids({binary(), binary()}) -> [pid()].
|
2008-04-18 14:19:45 +02:00
|
|
|
get_connections_pids(FromTo) ->
|
|
|
|
case catch mnesia:dirty_read(s2s, FromTo) of
|
|
|
|
L when is_list(L) ->
|
|
|
|
[Connection#s2s.pid || Connection <- L];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2016-01-11 12:22:17 +01:00
|
|
|
-spec try_register({binary(), binary()}) -> boolean().
|
2019-07-09 15:42:24 +02:00
|
|
|
try_register({From, To} = FromTo) ->
|
2007-09-25 18:32:18 +02:00
|
|
|
MaxS2SConnectionsNumber = max_s2s_connections_number(FromTo),
|
|
|
|
MaxS2SConnectionsNumberPerNode =
|
|
|
|
max_s2s_connections_number_per_node(FromTo),
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2007-09-25 18:32:18 +02:00
|
|
|
L = mnesia:read({s2s, FromTo}),
|
2013-03-14 10:33:02 +01:00
|
|
|
NeededConnections = needed_connections_number(L,
|
|
|
|
MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode),
|
|
|
|
if NeededConnections > 0 ->
|
2019-07-09 15:42:24 +02:00
|
|
|
mnesia:write(#s2s{fromto = FromTo, pid = self()}),
|
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> false
|
2007-09-25 18:32:18 +02:00
|
|
|
end
|
|
|
|
end,
|
2002-12-13 21:58:27 +01:00
|
|
|
case mnesia:transaction(F) of
|
2019-07-09 15:42:24 +02:00
|
|
|
{atomic, Res} -> Res;
|
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("Failed to register s2s connection ~s -> ~s: "
|
|
|
|
"Mnesia failure: ~p",
|
|
|
|
[From, To, Reason]),
|
|
|
|
false
|
2002-12-13 21:58:27 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec dirty_get_connections() -> [{binary(), binary()}].
|
2006-01-29 05:38:31 +01:00
|
|
|
dirty_get_connections() ->
|
|
|
|
mnesia:dirty_all_keys(s2s).
|
|
|
|
|
2019-09-06 21:20:07 +02:00
|
|
|
-spec tls_options(binary(), binary(), [proplists:property()]) -> [proplists:property()].
|
|
|
|
tls_options(LServer, ServerHost, DefaultOpts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
TLSOpts1 = case ejabberd_pkix:get_certfile(LServer) of
|
|
|
|
error -> DefaultOpts;
|
|
|
|
{ok, CertFile} ->
|
2017-10-31 22:20:27 +01:00
|
|
|
lists:keystore(certfile, 1, DefaultOpts,
|
|
|
|
{certfile, CertFile})
|
2016-12-28 07:47:11 +01:00
|
|
|
end,
|
2019-09-06 21:20:07 +02:00
|
|
|
TLSOpts2 = case ejabberd_option:s2s_ciphers(ServerHost) of
|
2016-12-28 07:47:11 +01:00
|
|
|
undefined -> TLSOpts1;
|
|
|
|
Ciphers -> lists:keystore(ciphers, 1, TLSOpts1,
|
|
|
|
{ciphers, Ciphers})
|
|
|
|
end,
|
2019-09-06 21:20:07 +02:00
|
|
|
TLSOpts3 = case ejabberd_option:s2s_protocol_options(ServerHost) of
|
2016-12-28 07:47:11 +01:00
|
|
|
undefined -> TLSOpts2;
|
|
|
|
ProtoOpts -> lists:keystore(protocol_options, 1, TLSOpts2,
|
|
|
|
{protocol_options, ProtoOpts})
|
|
|
|
end,
|
2019-09-06 21:20:07 +02:00
|
|
|
TLSOpts4 = case ejabberd_option:s2s_dhfile(ServerHost) of
|
2016-12-28 07:47:11 +01:00
|
|
|
undefined -> TLSOpts3;
|
|
|
|
DHFile -> lists:keystore(dhfile, 1, TLSOpts3,
|
|
|
|
{dhfile, DHFile})
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
TLSOpts5 = case lists:keymember(cafile, 1, TLSOpts4) of
|
|
|
|
true -> TLSOpts4;
|
2019-09-06 21:20:07 +02:00
|
|
|
false -> [{cafile, get_cafile(ServerHost)}|TLSOpts4]
|
2016-12-28 07:47:11 +01:00
|
|
|
end,
|
2019-09-06 21:20:07 +02:00
|
|
|
case ejabberd_option:s2s_tls_compression(ServerHost) of
|
2016-12-28 07:47:11 +01:00
|
|
|
undefined -> TLSOpts5;
|
|
|
|
false -> [compression_none | TLSOpts5];
|
|
|
|
true -> lists:delete(compression_none, TLSOpts5)
|
2014-04-28 01:42:02 +02:00
|
|
|
end.
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec tls_required(binary()) -> boolean().
|
|
|
|
tls_required(LServer) ->
|
|
|
|
TLS = use_starttls(LServer),
|
2019-06-14 11:33:26 +02:00
|
|
|
TLS == required.
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
-spec tls_enabled(binary()) -> boolean().
|
|
|
|
tls_enabled(LServer) ->
|
|
|
|
TLS = use_starttls(LServer),
|
2017-01-09 15:02:17 +01:00
|
|
|
TLS /= false.
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
-spec zlib_enabled(binary()) -> boolean().
|
|
|
|
zlib_enabled(LServer) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:s2s_zlib(LServer).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec use_starttls(binary()) -> boolean() | optional | required.
|
2016-12-28 07:47:11 +01:00
|
|
|
use_starttls(LServer) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:s2s_use_starttls(LServer).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
-spec get_idle_timeout(binary()) -> non_neg_integer() | infinity.
|
|
|
|
get_idle_timeout(LServer) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:s2s_timeout(LServer).
|
2016-01-11 12:22:17 +01:00
|
|
|
|
2017-03-10 13:12:43 +01:00
|
|
|
-spec queue_type(binary()) -> ram | file.
|
|
|
|
queue_type(LServer) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:s2s_queue_type(LServer).
|
2017-10-31 22:20:27 +01:00
|
|
|
|
2017-11-26 16:10:25 +01:00
|
|
|
-spec get_cafile(binary()) -> file:filename_all() | undefined.
|
|
|
|
get_cafile(LServer) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case ejabberd_option:s2s_cafile(LServer) of
|
2017-11-26 16:10:25 +01:00
|
|
|
undefined ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:ca_file();
|
2017-11-26 16:10:25 +01:00
|
|
|
File ->
|
|
|
|
File
|
|
|
|
end.
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
init([]) ->
|
|
|
|
update_tables(),
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, s2s,
|
2019-06-14 11:33:26 +02:00
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{attributes, record_info(fields, s2s)}]),
|
|
|
|
case mnesia:subscribe(system) of
|
|
|
|
{ok, _} ->
|
|
|
|
ejabberd_commands:register_commands(get_commands_spec()),
|
|
|
|
ejabberd_mnesia:create(
|
|
|
|
?MODULE, temporarily_blocked,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, temporarily_blocked)}]),
|
|
|
|
ejabberd_hooks:add(host_up, ?MODULE, host_up, 50),
|
|
|
|
ejabberd_hooks:add(host_down, ?MODULE, host_down, 60),
|
|
|
|
lists:foreach(fun host_up/1, ejabberd_option:hosts()),
|
|
|
|
{ok, #state{}};
|
|
|
|
{error, Reason} ->
|
|
|
|
{stop, Reason}
|
|
|
|
end.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2019-07-09 15:42:24 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2019-07-09 15:42:24 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2006-01-29 05:38:31 +01:00
|
|
|
{noreply, State}.
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
handle_info({mnesia_system_event, {mnesia_down, Node}}, State) ->
|
|
|
|
clean_table_from_bad_node(Node),
|
|
|
|
{noreply, State};
|
2017-02-16 09:00:26 +01:00
|
|
|
handle_info({route, Packet}, State) ->
|
2019-07-07 21:12:14 +02:00
|
|
|
try route(Packet)
|
|
|
|
catch ?EX_RULE(Class, Reason, St) ->
|
|
|
|
StackTrace = ?EX_STACK(St),
|
|
|
|
?ERROR_MSG("Failed to route packet:~n~s~n** ~s",
|
|
|
|
[xmpp:pp(Packet),
|
|
|
|
misc:format_exception(2, Class, Reason, StackTrace)])
|
|
|
|
end,
|
2006-01-29 05:38:31 +01:00
|
|
|
{noreply, State};
|
2019-07-09 15:42:24 +02:00
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
|
|
|
{noreply, State}.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
2016-01-26 10:00:11 +01:00
|
|
|
ejabberd_commands:unregister_commands(get_commands_spec()),
|
2019-07-10 11:15:58 +02:00
|
|
|
stop_s2s_connections(stream_error()),
|
2019-06-14 11:33:26 +02:00
|
|
|
lists:foreach(fun host_down/1, ejabberd_option:hosts()),
|
2017-02-23 08:12:19 +01:00
|
|
|
ejabberd_hooks:delete(host_up, ?MODULE, host_up, 50),
|
2019-07-09 15:42:24 +02:00
|
|
|
ejabberd_hooks:delete(host_down, ?MODULE, host_down, 60).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2019-07-09 15:42:24 +02:00
|
|
|
-spec host_up(binary()) -> ok.
|
2017-02-23 08:12:19 +01:00
|
|
|
host_up(Host) ->
|
|
|
|
ejabberd_s2s_in:host_up(Host),
|
|
|
|
ejabberd_s2s_out:host_up(Host).
|
|
|
|
|
2019-07-09 15:42:24 +02:00
|
|
|
-spec host_down(binary()) -> ok.
|
2017-02-23 08:12:19 +01:00
|
|
|
host_down(Host) ->
|
2019-07-10 11:15:58 +02:00
|
|
|
Err = stream_error(),
|
2017-02-23 08:12:19 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun(#s2s{fromto = {From, _}, pid = Pid}) when node(Pid) == node() ->
|
|
|
|
case ejabberd_router:host_of_route(From) of
|
|
|
|
Host ->
|
2019-07-10 11:15:58 +02:00
|
|
|
ejabberd_s2s_out:send(Pid, Err),
|
2017-02-23 08:12:19 +01:00
|
|
|
ejabberd_s2s_out:stop(Pid);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
ok
|
|
|
|
end, ets:tab2list(s2s)),
|
|
|
|
ejabberd_s2s_in:host_down(Host),
|
|
|
|
ejabberd_s2s_out:host_down(Host).
|
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec clean_table_from_bad_node(node()) -> any().
|
2006-01-29 05:38:31 +01:00
|
|
|
clean_table_from_bad_node(Node) ->
|
|
|
|
F = fun() ->
|
2007-09-25 18:32:18 +02:00
|
|
|
Es = mnesia:select(
|
|
|
|
s2s,
|
2019-07-09 15:42:24 +02:00
|
|
|
ets:fun2ms(
|
|
|
|
fun(#s2s{pid = Pid} = E) when node(Pid) == Node ->
|
|
|
|
E
|
|
|
|
end)),
|
|
|
|
lists:foreach(fun mnesia:delete_object/1, Es)
|
2007-09-25 18:32:18 +02:00
|
|
|
end,
|
2010-02-19 08:38:18 +01:00
|
|
|
mnesia:async_dirty(F).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2019-07-07 21:12:14 +02:00
|
|
|
-spec route(stanza()) -> ok.
|
|
|
|
route(Packet) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("Local route:~n~s", [xmpp:pp(Packet)]),
|
2017-02-16 09:00:26 +01:00
|
|
|
From = xmpp:get_from(Packet),
|
|
|
|
To = xmpp:get_to(Packet),
|
2016-12-28 07:47:11 +01:00
|
|
|
case start_connection(From, To) of
|
|
|
|
{ok, Pid} when is_pid(Pid) ->
|
2019-07-09 15:42:24 +02:00
|
|
|
?DEBUG("Sending to process ~p~n", [Pid]),
|
|
|
|
#jid{lserver = MyServer} = From,
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_hooks:run(s2s_send_packet, MyServer, [Packet]),
|
|
|
|
ejabberd_s2s_out:route(Pid, Packet);
|
2016-12-28 07:47:11 +01:00
|
|
|
{error, Reason} ->
|
2019-07-09 15:42:24 +02:00
|
|
|
Lang = xmpp:get_lang(Packet),
|
2016-12-28 07:47:11 +01:00
|
|
|
Err = case Reason of
|
|
|
|
forbidden ->
|
2019-06-22 16:08:45 +02:00
|
|
|
xmpp:err_forbidden(?T("Access denied by service policy"), Lang);
|
2016-12-28 07:47:11 +01:00
|
|
|
internal_server_error ->
|
|
|
|
xmpp:err_internal_server_error()
|
|
|
|
end,
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err)
|
2002-12-07 21:27:26 +01:00
|
|
|
end.
|
|
|
|
|
2017-01-17 11:38:05 +01:00
|
|
|
-spec start_connection(jid(), jid())
|
2019-07-09 15:42:24 +02:00
|
|
|
-> {ok, pid()} | {error, forbidden | internal_server_error}.
|
2016-12-28 07:47:11 +01:00
|
|
|
start_connection(From, To) ->
|
|
|
|
start_connection(From, To, []).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec start_connection(jid(), jid(), [proplists:property()])
|
2019-07-09 15:42:24 +02:00
|
|
|
-> {ok, pid()} | {error, forbidden | internal_server_error}.
|
2016-12-28 07:47:11 +01:00
|
|
|
start_connection(From, To, Opts) ->
|
2003-12-13 22:36:53 +01:00
|
|
|
#jid{lserver = MyServer} = From,
|
|
|
|
#jid{lserver = Server} = To,
|
|
|
|
FromTo = {MyServer, Server},
|
2013-03-14 10:33:02 +01:00
|
|
|
MaxS2SConnectionsNumber =
|
|
|
|
max_s2s_connections_number(FromTo),
|
2007-09-25 18:32:18 +02:00
|
|
|
MaxS2SConnectionsNumberPerNode =
|
|
|
|
max_s2s_connections_number_per_node(FromTo),
|
|
|
|
?DEBUG("Finding connection for ~p~n", [FromTo]),
|
2016-12-28 07:47:11 +01:00
|
|
|
case mnesia:dirty_read(s2s, FromTo) of
|
2019-07-09 15:42:24 +02:00
|
|
|
[] ->
|
|
|
|
%% We try to establish all the connections if the host is not a
|
|
|
|
%% service and if the s2s host is not blacklisted or
|
|
|
|
%% is in whitelist:
|
|
|
|
LServer = ejabberd_router:host_of_route(MyServer),
|
2019-06-26 09:40:30 +02:00
|
|
|
case allow_host(LServer, Server) of
|
|
|
|
true ->
|
|
|
|
NeededConnections = needed_connections_number(
|
|
|
|
[],
|
|
|
|
MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode),
|
|
|
|
open_several_connections(NeededConnections, MyServer,
|
|
|
|
Server, From, FromTo,
|
|
|
|
MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode, Opts);
|
|
|
|
false ->
|
|
|
|
{error, forbidden}
|
|
|
|
end;
|
2019-07-09 15:42:24 +02:00
|
|
|
L when is_list(L) ->
|
|
|
|
NeededConnections = needed_connections_number(L,
|
|
|
|
MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode),
|
|
|
|
if NeededConnections > 0 ->
|
|
|
|
%% We establish the missing connections for this pair.
|
|
|
|
open_several_connections(NeededConnections, MyServer,
|
|
|
|
Server, From, FromTo,
|
|
|
|
MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode, Opts);
|
|
|
|
true ->
|
|
|
|
%% We choose a connexion from the pool of opened ones.
|
|
|
|
{ok, choose_connection(From, L)}
|
|
|
|
end
|
2003-01-26 21:16:53 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec choose_connection(jid(), [#s2s{}]) -> pid().
|
2007-09-14 16:15:44 +02:00
|
|
|
choose_connection(From, Connections) ->
|
2007-09-25 18:32:18 +02:00
|
|
|
choose_pid(From, [C#s2s.pid || C <- Connections]).
|
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec choose_pid(jid(), [pid()]) -> pid().
|
2007-09-25 18:32:18 +02:00
|
|
|
choose_pid(From, Pids) ->
|
|
|
|
Pids1 = case [P || P <- Pids, node(P) == node()] of
|
2019-07-09 15:42:24 +02:00
|
|
|
[] -> Pids;
|
|
|
|
Ps -> Ps
|
2007-09-25 18:32:18 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Pid =
|
2015-11-24 16:44:13 +01:00
|
|
|
lists:nth(erlang:phash(jid:remove_resource(From),
|
2013-03-14 10:33:02 +01:00
|
|
|
length(Pids1)),
|
|
|
|
Pids1),
|
2007-09-25 18:32:18 +02:00
|
|
|
?DEBUG("Using ejabberd_s2s_out ~p~n", [Pid]),
|
2007-09-14 16:19:48 +02:00
|
|
|
Pid.
|
|
|
|
|
2019-07-09 15:42:24 +02:00
|
|
|
-spec open_several_connections(pos_integer(), binary(), binary(),
|
|
|
|
jid(), {binary(), binary()},
|
|
|
|
integer(), integer(), [proplists:property()]) ->
|
|
|
|
{ok, pid()} | {error, internal_server_error}.
|
2013-03-14 10:33:02 +01:00
|
|
|
open_several_connections(N, MyServer, Server, From,
|
|
|
|
FromTo, MaxS2SConnectionsNumber,
|
2016-12-28 07:47:11 +01:00
|
|
|
MaxS2SConnectionsNumberPerNode, Opts) ->
|
|
|
|
case lists:flatmap(
|
|
|
|
fun(_) ->
|
|
|
|
new_connection(MyServer, Server,
|
2019-07-09 15:42:24 +02:00
|
|
|
From, FromTo, MaxS2SConnectionsNumber,
|
2016-12-28 07:47:11 +01:00
|
|
|
MaxS2SConnectionsNumberPerNode, Opts)
|
|
|
|
end, lists:seq(1, N)) of
|
|
|
|
[] ->
|
|
|
|
{error, internal_server_error};
|
|
|
|
PIDs ->
|
|
|
|
{ok, choose_pid(From, PIDs)}
|
2007-09-14 16:20:43 +02:00
|
|
|
end.
|
2007-09-14 16:15:44 +02:00
|
|
|
|
2019-07-09 15:42:24 +02:00
|
|
|
-spec new_connection(binary(), binary(), jid(), {binary(), binary()},
|
|
|
|
integer(), integer(), [proplists:property()]) -> [pid()].
|
2007-09-25 18:32:18 +02:00
|
|
|
new_connection(MyServer, Server, From, FromTo,
|
2016-12-28 07:47:11 +01:00
|
|
|
MaxS2SConnectionsNumber, MaxS2SConnectionsNumberPerNode, Opts) ->
|
|
|
|
{ok, Pid} = ejabberd_s2s_out:start(MyServer, Server, Opts),
|
2007-09-14 16:15:44 +02:00
|
|
|
F = fun() ->
|
2007-09-25 18:32:18 +02:00
|
|
|
L = mnesia:read({s2s, FromTo}),
|
2013-03-14 10:33:02 +01:00
|
|
|
NeededConnections = needed_connections_number(L,
|
|
|
|
MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode),
|
|
|
|
if NeededConnections > 0 ->
|
2019-07-09 15:42:24 +02:00
|
|
|
mnesia:write(#s2s{fromto = FromTo, pid = Pid}),
|
|
|
|
Pid;
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> choose_connection(From, L)
|
2007-09-25 18:32:18 +02:00
|
|
|
end
|
|
|
|
end,
|
2007-09-14 16:15:44 +02:00
|
|
|
TRes = mnesia:transaction(F),
|
|
|
|
case TRes of
|
2019-07-09 15:42:24 +02:00
|
|
|
{atomic, Pid1} ->
|
2017-05-25 12:46:17 +02:00
|
|
|
if Pid1 == Pid ->
|
|
|
|
ejabberd_s2s_out:connect(Pid);
|
|
|
|
true ->
|
|
|
|
ejabberd_s2s_out:stop(Pid)
|
|
|
|
end,
|
|
|
|
[Pid1];
|
2019-07-09 15:42:24 +02:00
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("Failed to register s2s connection ~s -> ~s: "
|
|
|
|
"Mnesia failure: ~p",
|
2016-12-28 07:47:11 +01:00
|
|
|
[MyServer, Server, Reason]),
|
|
|
|
ejabberd_s2s_out:stop(Pid),
|
|
|
|
[]
|
|
|
|
end.
|
2007-09-14 16:15:44 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec max_s2s_connections_number({binary(), binary()}) -> pos_integer().
|
2007-09-25 18:32:18 +02:00
|
|
|
max_s2s_connections_number({From, To}) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case ejabberd_shaper:match(From, max_s2s_connections, jid:make(To)) of
|
|
|
|
Max when is_integer(Max) -> Max;
|
|
|
|
_ -> ?DEFAULT_MAX_S2S_CONNECTIONS_NUMBER
|
2007-09-25 18:32:18 +02:00
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec max_s2s_connections_number_per_node({binary(), binary()}) -> pos_integer().
|
2007-09-25 18:32:18 +02:00
|
|
|
max_s2s_connections_number_per_node({From, To}) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case ejabberd_shaper:match(From, max_s2s_connections_per_node, jid:make(To)) of
|
|
|
|
Max when is_integer(Max) -> Max;
|
|
|
|
_ -> ?DEFAULT_MAX_S2S_CONNECTIONS_NUMBER_PER_NODE
|
2007-09-14 16:18:51 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec needed_connections_number([#s2s{}], integer(), integer()) -> integer().
|
2007-09-25 18:32:18 +02:00
|
|
|
needed_connections_number(Ls, MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode) ->
|
|
|
|
LocalLs = [L || L <- Ls, node(L#s2s.pid) == node()],
|
|
|
|
lists:min([MaxS2SConnectionsNumber - length(Ls),
|
|
|
|
MaxS2SConnectionsNumberPerNode - length(LocalLs)]).
|
|
|
|
|
2008-10-12 14:04:10 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% ejabberd commands
|
|
|
|
|
2016-01-26 10:00:11 +01:00
|
|
|
get_commands_spec() ->
|
2016-07-30 18:50:58 +02:00
|
|
|
[#ejabberd_commands{
|
2017-07-20 11:15:02 +02:00
|
|
|
name = incoming_s2s_number, tags = [stats, s2s],
|
2016-07-30 18:50:58 +02:00
|
|
|
desc = "Number of incoming s2s connections on the node",
|
2017-07-20 11:15:02 +02:00
|
|
|
policy = admin,
|
|
|
|
module = ?MODULE, function = incoming_s2s_number,
|
|
|
|
args = [], result = {s2s_incoming, integer}},
|
2016-07-30 18:50:58 +02:00
|
|
|
#ejabberd_commands{
|
2017-07-20 11:15:02 +02:00
|
|
|
name = outgoing_s2s_number, tags = [stats, s2s],
|
2016-07-30 18:50:58 +02:00
|
|
|
desc = "Number of outgoing s2s connections on the node",
|
2017-07-20 11:15:02 +02:00
|
|
|
policy = admin,
|
|
|
|
module = ?MODULE, function = outgoing_s2s_number,
|
|
|
|
args = [], result = {s2s_outgoing, integer}},
|
|
|
|
#ejabberd_commands{
|
2017-08-07 15:06:07 +02:00
|
|
|
name = stop_s2s_connections, tags = [s2s],
|
|
|
|
desc = "Stop all s2s outgoing and incoming connections",
|
2017-07-20 11:15:02 +02:00
|
|
|
policy = admin,
|
2017-08-07 15:06:07 +02:00
|
|
|
module = ?MODULE, function = stop_s2s_connections,
|
2017-07-20 11:15:02 +02:00
|
|
|
args = [], result = {res, rescode}}].
|
2008-10-12 14:04:10 +02:00
|
|
|
|
2016-07-30 18:50:58 +02:00
|
|
|
%% TODO Move those stats commands to ejabberd stats command ?
|
2008-10-12 14:04:10 +02:00
|
|
|
incoming_s2s_number() ->
|
2016-07-30 18:50:58 +02:00
|
|
|
supervisor_count(ejabberd_s2s_in_sup).
|
2008-10-12 14:04:10 +02:00
|
|
|
|
|
|
|
outgoing_s2s_number() ->
|
2016-07-30 18:50:58 +02:00
|
|
|
supervisor_count(ejabberd_s2s_out_sup).
|
|
|
|
|
2019-07-09 15:42:24 +02:00
|
|
|
-spec supervisor_count(atom()) -> non_neg_integer().
|
2016-07-30 18:50:58 +02:00
|
|
|
supervisor_count(Supervisor) ->
|
2019-07-09 15:42:24 +02:00
|
|
|
try supervisor:count_children(Supervisor) of
|
|
|
|
Props ->
|
|
|
|
proplists:get_value(workers, Props, 0)
|
|
|
|
catch _:_ ->
|
|
|
|
0
|
2016-07-30 18:50:58 +02:00
|
|
|
end.
|
2008-10-12 14:04:10 +02:00
|
|
|
|
2017-08-07 15:06:07 +02:00
|
|
|
-spec stop_s2s_connections() -> ok.
|
|
|
|
stop_s2s_connections() ->
|
2019-07-10 11:15:58 +02:00
|
|
|
stop_s2s_connections(xmpp:serr_reset()).
|
|
|
|
|
|
|
|
-spec stop_s2s_connections(stream_error()) -> ok.
|
|
|
|
stop_s2s_connections(Err) ->
|
2016-09-23 11:30:33 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({_Id, Pid, _Type, _Module}) ->
|
2019-07-10 11:15:58 +02:00
|
|
|
ejabberd_s2s_in:send(Pid, Err),
|
|
|
|
ejabberd_s2s_in:stop(Pid),
|
2017-02-23 14:18:29 +01:00
|
|
|
supervisor:terminate_child(ejabberd_s2s_in_sup, Pid)
|
|
|
|
end, supervisor:which_children(ejabberd_s2s_in_sup)),
|
|
|
|
lists:foreach(
|
|
|
|
fun({_Id, Pid, _Type, _Module}) ->
|
2019-07-10 11:15:58 +02:00
|
|
|
ejabberd_s2s_out:send(Pid, Err),
|
|
|
|
ejabberd_s2s_out:stop(Pid),
|
2017-02-23 14:18:29 +01:00
|
|
|
supervisor:terminate_child(ejabberd_s2s_out_sup, Pid)
|
|
|
|
end, supervisor:which_children(ejabberd_s2s_out_sup)),
|
2019-06-14 11:33:26 +02:00
|
|
|
_ = mnesia:clear_table(s2s),
|
2017-02-23 14:18:29 +01:00
|
|
|
ok.
|
2016-09-23 11:30:33 +02:00
|
|
|
|
2019-07-10 11:15:58 +02:00
|
|
|
-spec stream_error() -> stream_error().
|
|
|
|
stream_error() ->
|
|
|
|
case ejabberd_cluster:get_nodes() of
|
|
|
|
[Node] when Node == node() -> xmpp:serr_system_shutdown();
|
|
|
|
_ -> xmpp:serr_reset()
|
|
|
|
end.
|
|
|
|
|
2008-10-12 14:04:10 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Update Mnesia tables
|
2006-09-25 15:36:41 +02:00
|
|
|
|
2003-12-13 22:36:53 +01:00
|
|
|
update_tables() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
_ = mnesia:delete_table(local_s2s),
|
|
|
|
ok.
|
2007-07-18 19:42:53 +02:00
|
|
|
|
|
|
|
%% Check if host is in blacklist or white list
|
2019-07-09 15:42:24 +02:00
|
|
|
-spec allow_host(binary(), binary()) -> boolean().
|
2007-07-18 19:42:53 +02:00
|
|
|
allow_host(MyServer, S2SHost) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
allow_host1(MyServer, S2SHost) andalso
|
2019-07-09 15:42:24 +02:00
|
|
|
not is_temporarly_blocked(S2SHost).
|
2011-12-02 19:30:20 +01:00
|
|
|
|
2019-07-09 15:42:24 +02:00
|
|
|
-spec allow_host1(binary(), binary()) -> boolean().
|
2009-11-13 17:19:46 +01:00
|
|
|
allow_host1(MyHost, S2SHost) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Rule = ejabberd_option:s2s_access(MyHost),
|
2016-07-27 09:45:08 +02:00
|
|
|
JID = jid:make(S2SHost),
|
2013-08-12 14:25:05 +02:00
|
|
|
case acl:match_rule(MyHost, Rule, JID) of
|
|
|
|
deny -> false;
|
|
|
|
allow ->
|
|
|
|
case ejabberd_hooks:run_fold(s2s_allow_host, MyHost,
|
|
|
|
allow, [MyHost, S2SHost]) of
|
|
|
|
deny -> false;
|
2019-07-09 15:42:24 +02:00
|
|
|
allow -> true
|
2013-08-12 14:25:05 +02:00
|
|
|
end
|
2007-07-18 19:42:53 +02:00
|
|
|
end.
|
2009-05-19 11:38:17 +02:00
|
|
|
|
|
|
|
%% Get information about S2S connections of the specified type.
|
|
|
|
%% @spec (Type) -> [Info]
|
|
|
|
%% where Type = in | out
|
|
|
|
%% Info = [{InfoName::atom(), InfoValue::any()}]
|
|
|
|
get_info_s2s_connections(Type) ->
|
|
|
|
ChildType = case Type of
|
2019-07-09 15:42:24 +02:00
|
|
|
in -> ejabberd_s2s_in_sup;
|
|
|
|
out -> ejabberd_s2s_out_sup
|
2009-05-19 11:38:17 +02:00
|
|
|
end,
|
|
|
|
Connections = supervisor:which_children(ChildType),
|
2013-03-14 10:33:02 +01:00
|
|
|
get_s2s_info(Connections, Type).
|
|
|
|
|
|
|
|
get_s2s_info(Connections, Type) ->
|
|
|
|
complete_s2s_info(Connections, Type, []).
|
|
|
|
|
|
|
|
complete_s2s_info([], _, Result) -> Result;
|
|
|
|
complete_s2s_info([Connection | T], Type, Result) ->
|
|
|
|
{_, PID, _, _} = Connection,
|
2009-05-19 11:38:17 +02:00
|
|
|
State = get_s2s_state(PID),
|
2013-03-14 10:33:02 +01:00
|
|
|
complete_s2s_info(T, Type, [State | Result]).
|
|
|
|
|
|
|
|
-spec get_s2s_state(pid()) -> [{status, open | closed | error} | {s2s_pid, pid()}].
|
|
|
|
get_s2s_state(S2sPid) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
Infos = case p1_fsm:sync_send_all_state_event(S2sPid,
|
2019-07-09 15:42:24 +02:00
|
|
|
get_state_infos)
|
|
|
|
of
|
|
|
|
{state_infos, Is} -> [{status, open} | Is];
|
|
|
|
{noproc, _} -> [{status, closed}]; %% Connection closed
|
|
|
|
{badrpc, _} -> [{status, error}]
|
2009-05-19 11:38:17 +02:00
|
|
|
end,
|
|
|
|
[{s2s_pid, S2sPid} | Infos].
|