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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 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'}).
|
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
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
|
2017-02-16 09:00:26 +01:00
|
|
|
-export([start_link/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,
|
2017-02-23 08:12:19 +01:00
|
|
|
tls_required/1, tls_verify/1, tls_enabled/1, tls_options/2,
|
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]).
|
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-export([get_info_s2s_connections/1,
|
|
|
|
transform_options/1, opt_type/1]).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-include("xmpp.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-10-12 14:04:10 +02:00
|
|
|
-include("ejabberd_commands.hrl").
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2014-04-28 01:42:02 +02:00
|
|
|
-include_lib("public_key/include/public_key.hrl").
|
|
|
|
|
|
|
|
-define(PKIXEXPLICIT, 'OTP-PUB-KEY').
|
|
|
|
|
|
|
|
-define(PKIXIMPLICIT, 'OTP-PUB-KEY').
|
|
|
|
|
|
|
|
-include("XmppAddr.hrl").
|
|
|
|
|
2007-09-25 18:32:18 +02:00
|
|
|
-define(DEFAULT_MAX_S2S_CONNECTIONS_NUMBER, 1).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-09-25 18:32:18 +02:00
|
|
|
-define(DEFAULT_MAX_S2S_CONNECTIONS_NUMBER_PER_NODE, 1).
|
2007-09-14 16:18:51 +02:00
|
|
|
|
2011-12-02 19:30:20 +01:00
|
|
|
-define(S2S_OVERLOAD_BLOCK_PERIOD, 60).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2011-12-02 19:30:20 +01:00
|
|
|
%% once a server is temporarly blocked, it stay blocked for 60 seconds
|
|
|
|
|
2013-06-24 12:04:56 +02:00
|
|
|
-record(s2s, {fromto = {<<"">>, <<"">>} :: {binary(), binary()} | '_',
|
2016-01-11 12:22:17 +01:00
|
|
|
pid = self() :: pid() | '_' | '$1'}).
|
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
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(temporarily_blocked, {host = <<"">> :: binary(),
|
2015-12-07 16:08:57 +01:00
|
|
|
timestamp :: integer()}).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-type temporarily_blocked() :: #temporarily_blocked{}.
|
2011-12-02 19:30:20 +01:00
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [],
|
|
|
|
[]).
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec route(stanza()) -> ok.
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
route(Packet) ->
|
|
|
|
try do_route(Packet)
|
|
|
|
catch E:R ->
|
|
|
|
?ERROR_MSG("failed to route packet:~n~s~nReason = ~p",
|
|
|
|
[xmpp:pp(Packet), {E, {R, erlang:get_stacktrace()}}])
|
2004-11-05 22:14:31 +01:00
|
|
|
end.
|
|
|
|
|
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) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("Disabling connections from ~s for ~p "
|
|
|
|
"seconds",
|
|
|
|
[Host, ?S2S_OVERLOAD_BLOCK_PERIOD]),
|
|
|
|
mnesia:transaction(fun () ->
|
2015-12-07 16:08:57 +01:00
|
|
|
Time = p1_time_compat: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
|
|
|
|
[] -> false;
|
|
|
|
[#temporarily_blocked{timestamp = T} = Entry] ->
|
2015-12-07 16:08:57 +01:00
|
|
|
Diff = p1_time_compat:monotonic_time() - T,
|
|
|
|
case p1_time_compat:convert_time_unit(Diff, native, micro_seconds) of
|
2013-03-14 10:33:02 +01:00
|
|
|
N when N > (?S2S_OVERLOAD_BLOCK_PERIOD) * 1000 * 1000 ->
|
|
|
|
mnesia:dirty_delete_object(Entry), false;
|
|
|
|
_ -> true
|
|
|
|
end
|
|
|
|
end.
|
2011-12-02 19:30:20 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec remove_connection({binary(), binary()},
|
2016-01-11 12:22:17 +01:00
|
|
|
pid()) -> {atomic, ok} | ok | {aborted, any()}.
|
2011-12-02 19:30:20 +01:00
|
|
|
|
2016-01-11 12:22:17 +01:00
|
|
|
remove_connection(FromTo, Pid) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch mnesia:dirty_match_object(s2s,
|
2016-01-11 12:22:17 +01:00
|
|
|
#s2s{fromto = FromTo, pid = Pid})
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
2016-01-11 12:22:17 +01:00
|
|
|
[#s2s{pid = Pid}] ->
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2016-01-11 12:22:17 +01:00
|
|
|
mnesia:delete_object(#s2s{fromto = FromTo, pid = Pid})
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F);
|
|
|
|
_ -> 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
|
2015-10-07 00:06:58 +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().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
try_register(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 ->
|
2016-01-11 12:22:17 +01: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
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, Res} -> Res;
|
|
|
|
_ -> 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).
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec tls_options(binary(), [proplists:property()]) -> [proplists:property()].
|
|
|
|
tls_options(LServer, DefaultOpts) ->
|
2017-10-31 22:20:27 +01:00
|
|
|
TLSOpts1 = case get_certfile(LServer) of
|
2017-01-26 08:58:26 +01:00
|
|
|
undefined -> DefaultOpts;
|
2017-10-31 22:20:27 +01:00
|
|
|
CertFile ->
|
|
|
|
lists:keystore(certfile, 1, DefaultOpts,
|
|
|
|
{certfile, CertFile})
|
2016-12-28 07:47:11 +01:00
|
|
|
end,
|
|
|
|
TLSOpts2 = case ejabberd_config:get_option(
|
2017-04-29 10:39:40 +02:00
|
|
|
{s2s_ciphers, LServer}) of
|
2016-12-28 07:47:11 +01:00
|
|
|
undefined -> TLSOpts1;
|
|
|
|
Ciphers -> lists:keystore(ciphers, 1, TLSOpts1,
|
|
|
|
{ciphers, Ciphers})
|
|
|
|
end,
|
|
|
|
TLSOpts3 = case ejabberd_config:get_option(
|
2017-04-29 10:39:40 +02:00
|
|
|
{s2s_protocol_options, LServer}) of
|
2016-12-28 07:47:11 +01:00
|
|
|
undefined -> TLSOpts2;
|
|
|
|
ProtoOpts -> lists:keystore(protocol_options, 1, TLSOpts2,
|
|
|
|
{protocol_options, ProtoOpts})
|
|
|
|
end,
|
|
|
|
TLSOpts4 = case ejabberd_config:get_option(
|
2017-04-29 10:39:40 +02:00
|
|
|
{s2s_dhfile, LServer}) of
|
2016-12-28 07:47:11 +01:00
|
|
|
undefined -> TLSOpts3;
|
|
|
|
DHFile -> lists:keystore(dhfile, 1, TLSOpts3,
|
|
|
|
{dhfile, DHFile})
|
|
|
|
end,
|
2017-11-26 16:10:25 +01:00
|
|
|
TLSOpts5 = case get_cafile(LServer) of
|
2016-12-28 07:47:11 +01:00
|
|
|
undefined -> TLSOpts4;
|
|
|
|
CAFile -> lists:keystore(cafile, 1, TLSOpts4,
|
|
|
|
{cafile, CAFile})
|
|
|
|
end,
|
2017-04-29 10:39:40 +02:00
|
|
|
case ejabberd_config:get_option({s2s_tls_compression, LServer}) 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),
|
|
|
|
TLS == required orelse TLS == required_trusted.
|
|
|
|
|
|
|
|
-spec tls_verify(binary()) -> boolean().
|
|
|
|
tls_verify(LServer) ->
|
|
|
|
TLS = use_starttls(LServer),
|
|
|
|
TLS == required_trusted.
|
|
|
|
|
|
|
|
-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) ->
|
2017-04-29 10:39:40 +02:00
|
|
|
ejabberd_config:get_option({s2s_zlib, LServer}, false).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
-spec use_starttls(binary()) -> boolean() | optional | required | required_trusted.
|
|
|
|
use_starttls(LServer) ->
|
2017-04-29 10:39:40 +02:00
|
|
|
ejabberd_config:get_option({s2s_use_starttls, LServer}, false).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
-spec get_idle_timeout(binary()) -> non_neg_integer() | infinity.
|
|
|
|
get_idle_timeout(LServer) ->
|
2017-04-29 10:39:40 +02:00
|
|
|
ejabberd_config:get_option({s2s_timeout, LServer}, timer:minutes(10)).
|
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) ->
|
2017-04-29 10:39:40 +02:00
|
|
|
ejabberd_config:get_option(
|
|
|
|
{s2s_queue_type, LServer},
|
|
|
|
ejabberd_config:default_queue_type(LServer)).
|
2017-03-10 13:12:43 +01:00
|
|
|
|
2017-11-26 16:10:25 +01:00
|
|
|
-spec get_certfile(binary()) -> file:filename_all() | undefined.
|
2017-10-31 22:20:27 +01:00
|
|
|
get_certfile(LServer) ->
|
|
|
|
case ejabberd_pkix:get_certfile(LServer) of
|
|
|
|
{ok, CertFile} ->
|
|
|
|
CertFile;
|
|
|
|
error ->
|
|
|
|
ejabberd_config:get_option(
|
|
|
|
{domain_certfile, LServer},
|
|
|
|
ejabberd_config:get_option({s2s_certfile, LServer}))
|
|
|
|
end.
|
|
|
|
|
2017-11-26 16:10:25 +01:00
|
|
|
-spec get_cafile(binary()) -> file:filename_all() | undefined.
|
|
|
|
get_cafile(LServer) ->
|
|
|
|
case ejabberd_config:get_option({s2s_cafile, LServer}) of
|
|
|
|
undefined ->
|
|
|
|
ejabberd_pkix:ca_file();
|
|
|
|
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,
|
2015-10-07 00:06:58 +02:00
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{attributes, record_info(fields, s2s)}]),
|
2006-01-29 05:38:31 +01:00
|
|
|
mnesia:subscribe(system),
|
2016-01-26 10:00:11 +01:00
|
|
|
ejabberd_commands:register_commands(get_commands_spec()),
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, temporarily_blocked,
|
2015-10-07 00:06:58 +02:00
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, temporarily_blocked)}]),
|
2017-02-23 08:12:19 +01:00
|
|
|
ejabberd_hooks:add(host_up, ?MODULE, host_up, 50),
|
|
|
|
ejabberd_hooks:add(host_down, ?MODULE, host_down, 60),
|
|
|
|
lists:foreach(fun host_up/1, ?MYHOSTS),
|
2006-01-29 05:38:31 +01:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
handle_call(_Request, _From, State) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
{reply, ok, State}.
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{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) ->
|
|
|
|
route(Packet),
|
2006-01-29 05:38:31 +01:00
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(_Info, State) -> {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()),
|
2017-02-23 08:12:19 +01:00
|
|
|
lists:foreach(fun host_down/1, ?MYHOSTS),
|
|
|
|
ejabberd_hooks:delete(host_up, ?MODULE, host_up, 50),
|
|
|
|
ejabberd_hooks:delete(host_down, ?MODULE, host_down, 60),
|
2006-01-29 05:38:31 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2017-02-23 08:12:19 +01:00
|
|
|
host_up(Host) ->
|
|
|
|
ejabberd_s2s_in:host_up(Host),
|
|
|
|
ejabberd_s2s_out:host_up(Host).
|
|
|
|
|
|
|
|
host_down(Host) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(#s2s{fromto = {From, _}, pid = Pid}) when node(Pid) == node() ->
|
|
|
|
case ejabberd_router:host_of_route(From) of
|
|
|
|
Host ->
|
|
|
|
ejabberd_s2s_out:send(Pid, xmpp:serr_system_shutdown()),
|
|
|
|
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,
|
|
|
|
[{#s2s{pid = '$1', _ = '_'},
|
|
|
|
[{'==', {node, '$1'}, Node}],
|
|
|
|
['$_']}]),
|
|
|
|
lists:foreach(fun(E) ->
|
|
|
|
mnesia:delete_object(E)
|
|
|
|
end, Es)
|
|
|
|
end,
|
2010-02-19 08:38:18 +01:00
|
|
|
mnesia:async_dirty(F).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec do_route(stanza()) -> ok.
|
|
|
|
do_route(Packet) ->
|
|
|
|
?DEBUG("local route:~n~s", [xmpp:pp(Packet)]),
|
|
|
|
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) ->
|
2013-03-14 10:33:02 +01: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} ->
|
2016-07-27 09:45:08 +02:00
|
|
|
Lang = xmpp:get_lang(Packet),
|
2016-12-28 07:47:11 +01:00
|
|
|
Err = case Reason of
|
2017-01-17 11:38:05 +01:00
|
|
|
policy_violation ->
|
|
|
|
xmpp:err_policy_violation(
|
|
|
|
<<"Server connections to local "
|
|
|
|
"subdomains are forbidden">>, Lang);
|
2016-12-28 07:47:11 +01:00
|
|
|
forbidden ->
|
2017-09-24 13:32:37 +02:00
|
|
|
xmpp:err_forbidden(<<"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())
|
|
|
|
-> {ok, pid()} | {error, policy_violation | 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()])
|
2017-01-17 11:38:05 +01:00
|
|
|
-> {ok, pid()} | {error, policy_violation | 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
|
2013-03-14 10:33:02 +01: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:
|
2016-12-28 07:47:11 +01:00
|
|
|
LServer = ejabberd_router:host_of_route(MyServer),
|
2017-01-17 11:38:05 +01:00
|
|
|
case is_service(From, To) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2017-01-17 11:38:05 +01:00
|
|
|
{error, policy_violation};
|
|
|
|
false ->
|
|
|
|
case allow_host(LServer, Server) of
|
|
|
|
true ->
|
|
|
|
NeededConnections = needed_connections_number(
|
|
|
|
[],
|
2013-03-14 10:33:02 +01:00
|
|
|
MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode),
|
|
|
|
open_several_connections(NeededConnections, MyServer,
|
|
|
|
Server, From, FromTo,
|
|
|
|
MaxS2SConnectionsNumber,
|
2017-01-17 11:38:05 +01:00
|
|
|
MaxS2SConnectionsNumberPerNode, Opts);
|
|
|
|
false ->
|
|
|
|
{error, forbidden}
|
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
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,
|
2016-12-28 07:47:11 +01:00
|
|
|
MaxS2SConnectionsNumberPerNode, Opts);
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
%% We choose a connexion from the pool of opened ones.
|
2016-12-28 07:47:11 +01:00
|
|
|
{ok, choose_connection(From, L)}
|
2013-03-14 10:33:02 +01:00
|
|
|
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
|
2013-03-14 10:33:02 +01: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.
|
|
|
|
|
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,
|
2013-03-14 10:33:02 +01: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
|
|
|
|
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 ->
|
2016-01-11 12:22:17 +01:00
|
|
|
mnesia:write(#s2s{fromto = FromTo, pid = Pid}),
|
2013-03-14 10:33:02 +01:00
|
|
|
Pid;
|
|
|
|
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
|
2017-05-25 12:46:17 +02:00
|
|
|
{atomic, Pid1} ->
|
|
|
|
if Pid1 == Pid ->
|
|
|
|
ejabberd_s2s_out:connect(Pid);
|
|
|
|
true ->
|
|
|
|
ejabberd_s2s_out:stop(Pid)
|
|
|
|
end,
|
|
|
|
[Pid1];
|
2016-12-28 07:47:11 +01:00
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("failed to register connection ~s -> ~s: ~p",
|
|
|
|
[MyServer, Server, Reason]),
|
|
|
|
ejabberd_s2s_out:stop(Pid),
|
|
|
|
[]
|
|
|
|
end.
|
2007-09-14 16:15:44 +02:00
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec max_s2s_connections_number({binary(), binary()}) -> integer().
|
2007-09-25 18:32:18 +02:00
|
|
|
max_s2s_connections_number({From, To}) ->
|
2016-07-27 09:45:08 +02:00
|
|
|
case acl:match_rule(From, max_s2s_connections, jid:make(To)) of
|
2013-03-14 10:33:02 +01:00
|
|
|
Max when is_integer(Max) -> Max;
|
|
|
|
_ -> ?DEFAULT_MAX_S2S_CONNECTIONS_NUMBER
|
2007-09-25 18:32:18 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec max_s2s_connections_number_per_node({binary(), binary()}) -> integer().
|
2007-09-25 18:32:18 +02:00
|
|
|
max_s2s_connections_number_per_node({From, To}) ->
|
2016-07-27 09:45:08 +02:00
|
|
|
case acl:match_rule(From, max_s2s_connections_per_node, jid:make(To)) of
|
2013-03-14 10:33:02 +01:00
|
|
|
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)]).
|
|
|
|
|
2007-02-18 18:58:47 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: is_service(From, To) -> true | false
|
|
|
|
%% Description: Return true if the destination must be considered as a
|
|
|
|
%% service.
|
|
|
|
%% --------------------------------------------------------------------
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec is_service(jid(), jid()) -> boolean().
|
2007-02-18 18:58:47 +01:00
|
|
|
is_service(From, To) ->
|
|
|
|
LFromDomain = From#jid.lserver,
|
2017-04-29 10:39:40 +02:00
|
|
|
case ejabberd_config:get_option({route_subdomains, LFromDomain}, local) of
|
2013-03-14 10:33:02 +01:00
|
|
|
s2s -> % bypass RFC 3920 10.3
|
|
|
|
false;
|
2013-08-12 14:25:05 +02:00
|
|
|
local ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Hosts = (?MYHOSTS),
|
|
|
|
P = fun (ParentDomain) ->
|
|
|
|
lists:member(ParentDomain, Hosts)
|
|
|
|
end,
|
|
|
|
lists:any(P, parent_domains(To#jid.lserver))
|
2007-02-18 18:58:47 +01:00
|
|
|
end.
|
|
|
|
|
2010-10-14 21:15:40 +02:00
|
|
|
parent_domains(Domain) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foldl(fun (Label, []) -> [Label];
|
|
|
|
(Label, [Head | Tail]) ->
|
|
|
|
[<<Label/binary, ".", Head/binary>>, Head | Tail]
|
|
|
|
end,
|
|
|
|
[], lists:reverse(str:tokens(Domain, <<".">>))).
|
2003-01-10 20:44:35 +01:00
|
|
|
|
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).
|
|
|
|
|
|
|
|
supervisor_count(Supervisor) ->
|
|
|
|
case catch supervisor:which_children(Supervisor) of
|
|
|
|
{'EXIT', _} -> 0;
|
|
|
|
Result ->
|
|
|
|
length(Result)
|
|
|
|
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() ->
|
2016-09-23 11:30:33 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({_Id, Pid, _Type, _Module}) ->
|
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}) ->
|
|
|
|
supervisor:terminate_child(ejabberd_s2s_out_sup, Pid)
|
|
|
|
end, supervisor:which_children(ejabberd_s2s_out_sup)),
|
|
|
|
mnesia:clear_table(s2s),
|
|
|
|
ok.
|
2016-09-23 11:30:33 +02:00
|
|
|
|
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() ->
|
2007-09-14 16:15:44 +02:00
|
|
|
case catch mnesia:table_info(s2s, type) of
|
2013-03-14 10:33:02 +01:00
|
|
|
bag -> ok;
|
|
|
|
{'EXIT', _} -> ok;
|
|
|
|
_ -> mnesia:delete_table(s2s)
|
2007-09-14 16:15:44 +02:00
|
|
|
end,
|
2003-12-13 22:36:53 +01:00
|
|
|
case catch mnesia:table_info(s2s, attributes) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[fromto, node, key] ->
|
2016-01-11 12:22:17 +01:00
|
|
|
mnesia:transform_table(s2s, ignore, [fromto, pid]),
|
|
|
|
mnesia:clear_table(s2s);
|
|
|
|
[fromto, pid, key] ->
|
|
|
|
mnesia:transform_table(s2s, ignore, [fromto, pid]),
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:clear_table(s2s);
|
2016-01-11 12:22:17 +01:00
|
|
|
[fromto, pid] -> ok;
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', _} -> ok
|
2003-12-13 22:36:53 +01:00
|
|
|
end,
|
|
|
|
case lists:member(local_s2s, mnesia:system_info(tables)) of
|
2015-10-07 00:06:58 +02:00
|
|
|
true -> mnesia:delete_table(local_s2s);
|
|
|
|
false -> ok
|
2003-12-13 22:36:53 +01:00
|
|
|
end.
|
2007-07-18 19:42:53 +02:00
|
|
|
|
|
|
|
%% Check if host is in blacklist or white list
|
|
|
|
allow_host(MyServer, S2SHost) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
allow_host1(MyServer, S2SHost) andalso
|
2013-03-14 10:33:02 +01:00
|
|
|
not is_temporarly_blocked(S2SHost).
|
2011-12-02 19:30:20 +01:00
|
|
|
|
2009-11-13 17:19:46 +01:00
|
|
|
allow_host1(MyHost, S2SHost) ->
|
2017-04-29 10:39:40 +02:00
|
|
|
Rule = ejabberd_config:get_option({s2s_access, MyHost}, all),
|
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;
|
|
|
|
allow -> true;
|
|
|
|
_ -> true
|
|
|
|
end
|
2007-07-18 19:42:53 +02:00
|
|
|
end.
|
2009-05-19 11:38:17 +02:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
transform_options(Opts) ->
|
|
|
|
lists:foldl(fun transform_options/2, [], Opts).
|
|
|
|
|
|
|
|
transform_options({{s2s_host, Host}, Action}, Opts) ->
|
|
|
|
?WARNING_MSG("Option 's2s_host' is deprecated. "
|
|
|
|
"The option is still supported but it is better to "
|
|
|
|
"fix your config: use access rules instead.", []),
|
2017-04-11 12:13:58 +02:00
|
|
|
ACLName = misc:binary_to_atom(
|
2013-08-12 14:25:05 +02:00
|
|
|
iolist_to_binary(["s2s_access_", Host])),
|
|
|
|
[{acl, ACLName, {server, Host}},
|
|
|
|
{access, s2s, [{Action, ACLName}]},
|
|
|
|
{s2s_access, s2s} |
|
|
|
|
Opts];
|
|
|
|
transform_options({s2s_default_policy, Action}, Opts) ->
|
|
|
|
?WARNING_MSG("Option 's2s_default_policy' is deprecated. "
|
|
|
|
"The option is still supported but it is better to "
|
|
|
|
"fix your config: "
|
|
|
|
"use 's2s_access' with an access rule.", []),
|
|
|
|
[{access, s2s, [{Action, all}]},
|
|
|
|
{s2s_access, s2s} |
|
|
|
|
Opts];
|
|
|
|
transform_options(Opt, Opts) ->
|
|
|
|
[Opt|Opts].
|
|
|
|
|
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
|
2013-03-14 10:33:02 +01: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()}].
|
2009-05-19 11:38:17 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_s2s_state(S2sPid) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
Infos = case p1_fsm:sync_send_all_state_event(S2sPid,
|
2013-03-14 10:33:02 +01: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].
|
2014-04-28 01:42:02 +02:00
|
|
|
|
2017-05-08 11:59:28 +02:00
|
|
|
-type use_starttls() :: boolean() | optional | required | required_trusted.
|
|
|
|
-spec opt_type(route_subdomains) -> fun((s2s | local) -> s2s | local);
|
|
|
|
(s2s_access) -> fun((any()) -> any());
|
|
|
|
(s2s_ciphers) -> fun((binary()) -> binary());
|
|
|
|
(s2s_dhfile) -> fun((binary()) -> binary());
|
|
|
|
(s2s_cafile) -> fun((binary()) -> binary());
|
|
|
|
(s2s_protocol_options) -> fun(([binary()]) -> binary());
|
|
|
|
(s2s_tls_compression) -> fun((boolean()) -> boolean());
|
|
|
|
(s2s_use_starttls) -> fun((use_starttls()) -> use_starttls());
|
|
|
|
(s2s_zlib) -> fun((boolean()) -> boolean());
|
|
|
|
(s2s_timeout) -> fun((timeout()) -> timeout());
|
|
|
|
(s2s_queue_type) -> fun((ram | file) -> ram | file);
|
|
|
|
(atom()) -> [atom()].
|
2015-06-01 14:38:27 +02:00
|
|
|
opt_type(route_subdomains) ->
|
|
|
|
fun (s2s) -> s2s;
|
|
|
|
(local) -> local
|
|
|
|
end;
|
|
|
|
opt_type(s2s_access) ->
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1;
|
2017-05-17 14:42:18 +02:00
|
|
|
opt_type(s2s_ciphers) -> fun iolist_to_binary/1;
|
2017-05-12 08:34:57 +02:00
|
|
|
opt_type(s2s_dhfile) -> fun misc:try_read_file/1;
|
|
|
|
opt_type(s2s_cafile) -> fun misc:try_read_file/1;
|
2016-12-28 07:47:11 +01:00
|
|
|
opt_type(s2s_protocol_options) ->
|
|
|
|
fun (Options) -> str:join(Options, <<"|">>) end;
|
|
|
|
opt_type(s2s_tls_compression) ->
|
|
|
|
fun (true) -> true;
|
|
|
|
(false) -> false
|
|
|
|
end;
|
|
|
|
opt_type(s2s_use_starttls) ->
|
|
|
|
fun (true) -> true;
|
|
|
|
(false) -> false;
|
|
|
|
(optional) -> optional;
|
|
|
|
(required) -> required;
|
2017-09-02 10:44:11 +02:00
|
|
|
(required_trusted) ->
|
|
|
|
?WARNING_MSG("The value 'required_trusted' of option "
|
|
|
|
"'s2s_use_starttls' is deprected and will be "
|
|
|
|
"unsupported in future releases. Instead, "
|
|
|
|
"set it to 'required' and make sure "
|
|
|
|
"mod_s2s_dialback is *NOT* loaded", []),
|
|
|
|
required_trusted
|
2016-12-28 07:47:11 +01:00
|
|
|
end;
|
2017-04-28 12:23:32 +02:00
|
|
|
opt_type(s2s_zlib) ->
|
|
|
|
fun(B) when is_boolean(B) -> B end;
|
2016-12-28 07:47:11 +01:00
|
|
|
opt_type(s2s_timeout) ->
|
2017-04-29 10:39:40 +02:00
|
|
|
fun(I) when is_integer(I), I >= 0 -> timer:seconds(I);
|
|
|
|
(infinity) -> infinity;
|
|
|
|
(unlimited) -> infinity
|
2016-12-28 07:47:11 +01:00
|
|
|
end;
|
2017-03-10 13:12:43 +01:00
|
|
|
opt_type(s2s_queue_type) ->
|
|
|
|
fun(ram) -> ram; (file) -> file end;
|
2016-12-28 07:47:11 +01:00
|
|
|
opt_type(_) ->
|
2017-11-23 09:04:47 +01:00
|
|
|
[route_subdomains, s2s_access, s2s_zlib,
|
2017-01-23 11:51:05 +01:00
|
|
|
s2s_ciphers, s2s_dhfile, s2s_cafile, s2s_protocol_options,
|
2017-03-10 13:12:43 +01:00
|
|
|
s2s_tls_compression, s2s_use_starttls, s2s_timeout, s2s_queue_type].
|