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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start_link/0, route/3, have_connection/1,
|
2016-01-11 12:22:17 +01:00
|
|
|
make_key/2, get_connections_pids/1, try_register/1,
|
|
|
|
remove_connection/2, find_connection/2,
|
2013-03-14 10:33:02 +01:00
|
|
|
dirty_get_connections/0, allow_host/2,
|
|
|
|
incoming_s2s_number/0, outgoing_s2s_number/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,
|
|
|
|
check_peer_certificate/3]).
|
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
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
-include("jlib.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, [],
|
|
|
|
[]).
|
|
|
|
|
|
|
|
-spec route(jid(), jid(), xmlel()) -> ok.
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2004-11-05 22:14:31 +01:00
|
|
|
route(From, To, Packet) ->
|
|
|
|
case catch do_route(From, To, Packet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ -> ok
|
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).
|
|
|
|
|
2014-04-28 01:42:02 +02:00
|
|
|
check_peer_certificate(SockMod, Sock, Peer) ->
|
|
|
|
case SockMod:get_peer_certificate(Sock) of
|
|
|
|
{ok, Cert} ->
|
|
|
|
case SockMod:get_verify_result(Sock) of
|
|
|
|
0 ->
|
2015-09-02 15:02:46 +02:00
|
|
|
case ejabberd_idna:domain_utf8_to_ascii(Peer) of
|
2014-04-28 01:42:02 +02:00
|
|
|
false ->
|
|
|
|
{error, <<"Cannot decode remote server name">>};
|
|
|
|
AsciiPeer ->
|
|
|
|
case
|
|
|
|
lists:any(fun(D) -> match_domain(AsciiPeer, D) end,
|
|
|
|
get_cert_domains(Cert)) of
|
|
|
|
true ->
|
|
|
|
{ok, <<"Verification successful">>};
|
|
|
|
false ->
|
|
|
|
{error, <<"Certificate host name mismatch">>}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
VerifyRes ->
|
|
|
|
{error, p1_tls:get_cert_verify_string(VerifyRes, Cert)}
|
|
|
|
end;
|
2015-08-19 22:47:45 +02:00
|
|
|
{error, _Reason} ->
|
2015-08-20 00:04:56 +02:00
|
|
|
{error, <<"Cannot get peer certificate">>};
|
|
|
|
error ->
|
|
|
|
{error, <<"Cannot get peer certificate">>}
|
2014-04-28 01:42:02 +02:00
|
|
|
end.
|
|
|
|
|
2016-01-11 12:22:17 +01:00
|
|
|
make_key({From, To}, StreamID) ->
|
|
|
|
Secret = ejabberd_config:get_option(shared_key, fun(V) -> V end),
|
|
|
|
p1_sha:to_hexlist(
|
|
|
|
crypto:hmac(sha256, p1_sha:to_hexlist(crypto:hash(sha256, Secret)),
|
|
|
|
[To, " ", From, " ", StreamID])).
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
init([]) ->
|
|
|
|
update_tables(),
|
2015-10-07 00:06:58 +02:00
|
|
|
mnesia:create_table(s2s,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{attributes, record_info(fields, s2s)}]),
|
2006-01-29 05:38:31 +01:00
|
|
|
mnesia:add_table_copy(s2s, node(), ram_copies),
|
|
|
|
mnesia:subscribe(system),
|
2008-10-12 14:04:10 +02:00
|
|
|
ejabberd_commands:register_commands(commands()),
|
2015-10-07 00:06:58 +02:00
|
|
|
mnesia:create_table(temporarily_blocked,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, temporarily_blocked)}]),
|
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};
|
|
|
|
handle_info({route, From, To, Packet}, State) ->
|
|
|
|
case catch do_route(From, To, Packet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ -> ok
|
2006-01-29 05:38:31 +01:00
|
|
|
end,
|
|
|
|
{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) ->
|
2008-10-12 14:04:10 +02:00
|
|
|
ejabberd_commands:unregister_commands(commands()),
|
2006-01-29 05:38:31 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2015-10-07 00:06:58 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
do_route(From, To, Packet) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("s2s manager~n\tfrom ~p~n\tto ~p~n\tpacket "
|
|
|
|
"~P~n",
|
|
|
|
[From, To, Packet, 8]),
|
2003-12-13 22:36:53 +01:00
|
|
|
case find_connection(From, To) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, Pid} when is_pid(Pid) ->
|
|
|
|
?DEBUG("sending to process ~p~n", [Pid]),
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = Els} =
|
|
|
|
Packet,
|
|
|
|
NewAttrs =
|
2015-11-24 16:44:13 +01:00
|
|
|
jlib:replace_from_to_attrs(jid:to_string(From),
|
|
|
|
jid:to_string(To), Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
#jid{lserver = MyServer} = From,
|
|
|
|
ejabberd_hooks:run(s2s_send_packet, MyServer,
|
|
|
|
[From, To, Packet]),
|
|
|
|
send_element(Pid,
|
|
|
|
#xmlel{name = Name, attrs = NewAttrs, children = Els}),
|
|
|
|
ok;
|
|
|
|
{aborted, _Reason} ->
|
|
|
|
case xml:get_tag_attr_s(<<"type">>, Packet) of
|
|
|
|
<<"error">> -> ok;
|
|
|
|
<<"result">> -> ok;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end,
|
|
|
|
false
|
2002-12-07 21:27:26 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec find_connection(jid(), jid()) -> {aborted, any()} | {atomic, pid()}.
|
|
|
|
|
2003-12-13 22:36:53 +01:00
|
|
|
find_connection(From, To) ->
|
|
|
|
#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]),
|
2003-12-13 22:36:53 +01:00
|
|
|
case catch mnesia:dirty_read(s2s, FromTo) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', Reason} -> {aborted, Reason};
|
|
|
|
[] ->
|
|
|
|
%% 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:
|
|
|
|
case not is_service(From, To) andalso
|
|
|
|
allow_host(MyServer, Server)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
NeededConnections = needed_connections_number([],
|
|
|
|
MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode),
|
|
|
|
open_several_connections(NeededConnections, MyServer,
|
|
|
|
Server, From, FromTo,
|
|
|
|
MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode);
|
|
|
|
false -> {aborted, error}
|
|
|
|
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,
|
|
|
|
MaxS2SConnectionsNumberPerNode);
|
|
|
|
true ->
|
|
|
|
%% We choose a connexion from the pool of opened ones.
|
|
|
|
{atomic, choose_connection(From, L)}
|
|
|
|
end
|
2003-01-26 21:16:53 +01:00
|
|
|
end.
|
|
|
|
|
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]).
|
|
|
|
|
|
|
|
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,
|
2007-09-25 18:32:18 +02:00
|
|
|
MaxS2SConnectionsNumberPerNode) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ConnectionsResult = [new_connection(MyServer, Server,
|
|
|
|
From, FromTo, MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode)
|
|
|
|
|| _N <- lists:seq(1, N)],
|
2007-09-25 18:32:18 +02:00
|
|
|
case [PID || {atomic, PID} <- ConnectionsResult] of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] -> hd(ConnectionsResult);
|
|
|
|
PIDs -> {atomic, 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,
|
|
|
|
MaxS2SConnectionsNumber, MaxS2SConnectionsNumberPerNode) ->
|
2007-09-14 16:15:44 +02:00
|
|
|
{ok, Pid} = ejabberd_s2s_out:start(
|
2016-01-11 12:22:17 +01:00
|
|
|
MyServer, Server, new),
|
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
|
|
|
?INFO_MSG("New s2s connection started ~p", [Pid]),
|
|
|
|
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
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, Pid} -> ejabberd_s2s_out:start_connection(Pid);
|
|
|
|
_ -> ejabberd_s2s_out:stop_connection(Pid)
|
2007-09-14 16:15:44 +02:00
|
|
|
end,
|
|
|
|
TRes.
|
|
|
|
|
2007-09-25 18:32:18 +02:00
|
|
|
max_s2s_connections_number({From, To}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case acl:match_rule(From, max_s2s_connections,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make(<<"">>, To, <<"">>))
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
Max when is_integer(Max) -> Max;
|
|
|
|
_ -> ?DEFAULT_MAX_S2S_CONNECTIONS_NUMBER
|
2007-09-25 18:32:18 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
max_s2s_connections_number_per_node({From, To}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case acl:match_rule(From, max_s2s_connections_per_node,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make(<<"">>, To, <<"">>))
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
Max when is_integer(Max) -> Max;
|
|
|
|
_ -> ?DEFAULT_MAX_S2S_CONNECTIONS_NUMBER_PER_NODE
|
2007-09-14 16:18:51 +02:00
|
|
|
end.
|
|
|
|
|
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.
|
|
|
|
%% --------------------------------------------------------------------
|
|
|
|
is_service(From, To) ->
|
|
|
|
LFromDomain = From#jid.lserver,
|
2013-08-12 14:25:05 +02:00
|
|
|
case ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
{route_subdomains, LFromDomain},
|
2013-08-12 14:25:05 +02:00
|
|
|
fun(s2s) -> s2s; (local) -> local end, 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
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
send_element(Pid, El) ->
|
|
|
|
Pid ! {send_element, El}.
|
2008-10-12 14:04:10 +02:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% ejabberd commands
|
|
|
|
|
|
|
|
commands() ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#ejabberd_commands{name = incoming_s2s_number,
|
|
|
|
tags = [stats, s2s],
|
|
|
|
desc =
|
|
|
|
"Number of incoming s2s connections on "
|
|
|
|
"the node",
|
2015-09-28 14:25:43 +02:00
|
|
|
policy = admin,
|
2013-03-14 10:33:02 +01:00
|
|
|
module = ?MODULE, function = incoming_s2s_number,
|
|
|
|
args = [], result = {s2s_incoming, integer}},
|
2008-10-12 14:04:10 +02:00
|
|
|
#ejabberd_commands{name = outgoing_s2s_number,
|
2013-03-14 10:33:02 +01:00
|
|
|
tags = [stats, s2s],
|
|
|
|
desc =
|
|
|
|
"Number of outgoing s2s connections on "
|
|
|
|
"the node",
|
2015-09-28 14:25:43 +02:00
|
|
|
policy = admin,
|
2013-03-14 10:33:02 +01:00
|
|
|
module = ?MODULE, function = outgoing_s2s_number,
|
|
|
|
args = [], result = {s2s_outgoing, integer}}].
|
2008-10-12 14:04:10 +02:00
|
|
|
|
|
|
|
incoming_s2s_number() ->
|
|
|
|
length(supervisor:which_children(ejabberd_s2s_in_sup)).
|
|
|
|
|
|
|
|
outgoing_s2s_number() ->
|
|
|
|
length(supervisor:which_children(ejabberd_s2s_out_sup)).
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% 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) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
allow_host2(MyServer, S2SHost) andalso
|
|
|
|
not is_temporarly_blocked(S2SHost).
|
2011-12-02 19:30:20 +01:00
|
|
|
|
|
|
|
allow_host2(MyServer, S2SHost) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Hosts = (?MYHOSTS),
|
|
|
|
case lists:dropwhile(fun (ParentDomain) ->
|
|
|
|
not lists:member(ParentDomain, Hosts)
|
|
|
|
end,
|
|
|
|
parent_domains(MyServer))
|
|
|
|
of
|
|
|
|
[MyHost | _] -> allow_host1(MyHost, S2SHost);
|
|
|
|
[] -> allow_host1(MyServer, S2SHost)
|
2009-11-13 17:19:46 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
allow_host1(MyHost, S2SHost) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
Rule = ejabberd_config:get_option(
|
|
|
|
s2s_access,
|
|
|
|
fun(A) when is_atom(A) -> A end,
|
|
|
|
all),
|
2015-11-24 16:44:13 +01: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.", []),
|
|
|
|
ACLName = jlib:binary_to_atom(
|
|
|
|
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) ->
|
|
|
|
Infos = case gen_fsm:sync_send_all_state_event(S2sPid,
|
|
|
|
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
|
|
|
|
|
|
|
get_cert_domains(Cert) ->
|
2015-10-23 17:41:35 +02:00
|
|
|
TBSCert = Cert#'Certificate'.tbsCertificate,
|
|
|
|
Subject = case TBSCert#'TBSCertificate'.subject of
|
|
|
|
{rdnSequence, Subj} -> lists:flatten(Subj);
|
|
|
|
_ -> []
|
|
|
|
end,
|
|
|
|
Extensions = case TBSCert#'TBSCertificate'.extensions of
|
|
|
|
Exts when is_list(Exts) -> Exts;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2014-04-28 01:42:02 +02:00
|
|
|
lists:flatmap(fun (#'AttributeTypeAndValue'{type =
|
|
|
|
?'id-at-commonName',
|
|
|
|
value = Val}) ->
|
|
|
|
case 'OTP-PUB-KEY':decode('X520CommonName', Val) of
|
|
|
|
{ok, {_, D1}} ->
|
|
|
|
D = if is_binary(D1) -> D1;
|
|
|
|
is_list(D1) -> list_to_binary(D1);
|
|
|
|
true -> error
|
|
|
|
end,
|
|
|
|
if D /= error ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:from_string(D) of
|
2014-04-28 01:42:02 +02:00
|
|
|
#jid{luser = <<"">>, lserver = LD,
|
|
|
|
lresource = <<"">>} ->
|
|
|
|
[LD];
|
|
|
|
_ -> []
|
|
|
|
end;
|
|
|
|
true -> []
|
|
|
|
end;
|
|
|
|
_ -> []
|
|
|
|
end;
|
|
|
|
(_) -> []
|
|
|
|
end,
|
2015-10-23 17:41:35 +02:00
|
|
|
Subject)
|
2014-04-28 01:42:02 +02:00
|
|
|
++
|
|
|
|
lists:flatmap(fun (#'Extension'{extnID =
|
|
|
|
?'id-ce-subjectAltName',
|
|
|
|
extnValue = Val}) ->
|
|
|
|
BVal = if is_list(Val) -> list_to_binary(Val);
|
|
|
|
true -> Val
|
|
|
|
end,
|
|
|
|
case 'OTP-PUB-KEY':decode('SubjectAltName', BVal)
|
|
|
|
of
|
|
|
|
{ok, SANs} ->
|
|
|
|
lists:flatmap(fun ({otherName,
|
|
|
|
#'AnotherName'{'type-id' =
|
|
|
|
?'id-on-xmppAddr',
|
|
|
|
value =
|
|
|
|
XmppAddr}}) ->
|
|
|
|
case
|
|
|
|
'XmppAddr':decode('XmppAddr',
|
|
|
|
XmppAddr)
|
|
|
|
of
|
|
|
|
{ok, D}
|
|
|
|
when
|
|
|
|
is_binary(D) ->
|
|
|
|
case
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:from_string((D))
|
2014-04-28 01:42:02 +02:00
|
|
|
of
|
|
|
|
#jid{luser =
|
|
|
|
<<"">>,
|
|
|
|
lserver =
|
|
|
|
LD,
|
|
|
|
lresource =
|
|
|
|
<<"">>} ->
|
|
|
|
case
|
2015-09-02 15:02:46 +02:00
|
|
|
ejabberd_idna:domain_utf8_to_ascii(LD)
|
2014-04-28 01:42:02 +02:00
|
|
|
of
|
|
|
|
false ->
|
|
|
|
[];
|
|
|
|
PCLD ->
|
|
|
|
[PCLD]
|
|
|
|
end;
|
|
|
|
_ -> []
|
|
|
|
end;
|
|
|
|
_ -> []
|
|
|
|
end;
|
|
|
|
({dNSName, D})
|
|
|
|
when is_list(D) ->
|
|
|
|
case
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:from_string(list_to_binary(D))
|
2014-04-28 01:42:02 +02:00
|
|
|
of
|
|
|
|
#jid{luser = <<"">>,
|
|
|
|
lserver = LD,
|
|
|
|
lresource =
|
|
|
|
<<"">>} ->
|
|
|
|
[LD];
|
|
|
|
_ -> []
|
|
|
|
end;
|
|
|
|
(_) -> []
|
|
|
|
end,
|
|
|
|
SANs);
|
|
|
|
_ -> []
|
|
|
|
end;
|
|
|
|
(_) -> []
|
|
|
|
end,
|
|
|
|
Extensions).
|
|
|
|
|
|
|
|
match_domain(Domain, Domain) -> true;
|
|
|
|
match_domain(Domain, Pattern) ->
|
|
|
|
DLabels = str:tokens(Domain, <<".">>),
|
|
|
|
PLabels = str:tokens(Pattern, <<".">>),
|
|
|
|
match_labels(DLabels, PLabels).
|
|
|
|
|
|
|
|
match_labels([], []) -> true;
|
|
|
|
match_labels([], [_ | _]) -> false;
|
|
|
|
match_labels([_ | _], []) -> false;
|
|
|
|
match_labels([DL | DLabels], [PL | PLabels]) ->
|
|
|
|
case lists:all(fun (C) ->
|
|
|
|
$a =< C andalso C =< $z orelse
|
|
|
|
$0 =< C andalso C =< $9 orelse
|
|
|
|
C == $- orelse C == $*
|
|
|
|
end,
|
|
|
|
binary_to_list(PL))
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
Regexp = ejabberd_regexp:sh_to_awk(PL),
|
|
|
|
case ejabberd_regexp:run(DL, Regexp) of
|
|
|
|
match -> match_labels(DLabels, PLabels);
|
|
|
|
nomatch -> false
|
|
|
|
end;
|
|
|
|
false -> false
|
|
|
|
end.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
opt_type(route_subdomains) ->
|
|
|
|
fun (s2s) -> s2s;
|
|
|
|
(local) -> local
|
|
|
|
end;
|
|
|
|
opt_type(s2s_access) ->
|
|
|
|
fun (A) when is_atom(A) -> A end;
|
|
|
|
opt_type(_) -> [route_subdomains, s2s_access].
|