2014-05-08 14:08:07 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-01-03 15:58:52 +01:00
|
|
|
%%% File : ejabberd_stun.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Purpose : STUN RFC-5766
|
2014-05-08 14:08:07 +02:00
|
|
|
%%% Created : 8 May 2014 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2015-01-21 14:52:37 +01:00
|
|
|
%%%
|
2017-01-03 15:58:52 +01:00
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2013-2020 ProcessOne
|
2015-01-21 14:52:37 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2017-11-10 17:51:22 +01:00
|
|
|
%%%
|
2014-05-08 14:08:07 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-11-10 17:51:22 +01:00
|
|
|
|
2014-05-08 14:08:07 +02:00
|
|
|
-module(ejabberd_stun).
|
2018-09-17 10:21:02 +02:00
|
|
|
-behaviour(ejabberd_listener).
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({rfc, 5766}).
|
2015-06-22 13:11:11 +02:00
|
|
|
-protocol({xep, 176, '1.0'}).
|
2015-05-21 17:02:36 +02:00
|
|
|
|
2017-05-23 12:12:48 +02:00
|
|
|
-ifndef(STUN).
|
|
|
|
-include("logger.hrl").
|
2019-04-01 15:53:28 +02:00
|
|
|
-export([accept/1, start/3, start_link/3, listen_options/0]).
|
2018-09-18 17:19:42 +02:00
|
|
|
fail() ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?CRITICAL_MSG("Listening module ~ts is not available: "
|
2018-09-18 17:19:42 +02:00
|
|
|
"ejabberd is not compiled with STUN/TURN support",
|
|
|
|
[?MODULE]),
|
|
|
|
erlang:error(stun_not_compiled).
|
2018-09-17 10:21:02 +02:00
|
|
|
accept(_) ->
|
2018-09-18 17:19:42 +02:00
|
|
|
fail().
|
2018-09-18 11:53:36 +02:00
|
|
|
listen_options() ->
|
2018-09-18 17:19:42 +02:00
|
|
|
fail().
|
2019-04-01 15:53:28 +02:00
|
|
|
start(_, _, _) ->
|
2018-09-18 17:19:42 +02:00
|
|
|
fail().
|
2019-04-01 15:53:28 +02:00
|
|
|
start_link(_, _, _) ->
|
2018-09-18 17:19:42 +02:00
|
|
|
fail().
|
2017-05-23 12:12:48 +02:00
|
|
|
-else.
|
2019-04-01 15:53:28 +02:00
|
|
|
-export([tcp_init/2, udp_init/2, udp_recv/5, start/3,
|
2020-04-22 00:09:42 +02:00
|
|
|
start_link/3, accept/1, listen_opt_type/1, listen_options/0,
|
|
|
|
get_password/2]).
|
2014-05-08 14:08:07 +02:00
|
|
|
|
|
|
|
-include("logger.hrl").
|
2020-07-01 21:53:22 +02:00
|
|
|
-ifndef(LAGER).
|
|
|
|
-export([stun_filter/2]).
|
|
|
|
-define(STUN_MAX_LOG_LEVEL, notice). % Drop STUN/TURN info/debug messages.
|
|
|
|
-endif.
|
2014-05-08 14:08:07 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
tcp_init(Socket, Opts) ->
|
2020-07-01 21:53:22 +02:00
|
|
|
init_logger(),
|
2016-02-03 11:26:14 +01:00
|
|
|
ejabberd:start_app(stun),
|
2014-05-08 14:08:07 +02:00
|
|
|
stun:tcp_init(Socket, prepare_turn_opts(Opts)).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-dialyzer({nowarn_function, udp_init/2}).
|
2014-05-08 14:08:07 +02:00
|
|
|
udp_init(Socket, Opts) ->
|
2020-07-01 21:53:22 +02:00
|
|
|
init_logger(),
|
2016-02-03 11:26:14 +01:00
|
|
|
ejabberd:start_app(stun),
|
2014-05-08 14:08:07 +02:00
|
|
|
stun:udp_init(Socket, prepare_turn_opts(Opts)).
|
|
|
|
|
|
|
|
udp_recv(Socket, Addr, Port, Packet, Opts) ->
|
|
|
|
stun:udp_recv(Socket, Addr, Port, Packet, Opts).
|
|
|
|
|
2019-04-01 15:53:28 +02:00
|
|
|
start(SockMod, Socket, Opts) ->
|
|
|
|
stun:start({SockMod, Socket}, Opts).
|
2014-05-08 14:08:07 +02:00
|
|
|
|
2019-04-01 15:53:28 +02:00
|
|
|
start_link(_SockMod, Socket, Opts) ->
|
|
|
|
stun:start_link(Socket, Opts).
|
2018-09-17 10:21:02 +02:00
|
|
|
|
|
|
|
accept(_Pid) ->
|
|
|
|
ok.
|
2014-05-08 14:08:07 +02:00
|
|
|
|
2020-04-22 00:09:42 +02:00
|
|
|
get_password(User, Realm) ->
|
|
|
|
case ejabberd_hooks:run_fold(stun_get_password, <<>>, [User, Realm]) of
|
|
|
|
Password when byte_size(Password) > 0 ->
|
|
|
|
Password;
|
|
|
|
<<>> ->
|
2020-04-22 00:16:03 +02:00
|
|
|
case ejabberd_auth:get_password_s(User, Realm) of
|
|
|
|
Password when is_binary(Password) ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Cannot use hashed password of ~s@~s for "
|
|
|
|
"STUN/TURN authentication", [User, Realm]),
|
|
|
|
<<>>
|
|
|
|
end
|
2020-04-22 00:09:42 +02:00
|
|
|
end.
|
|
|
|
|
2014-05-08 14:08:07 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
prepare_turn_opts(Opts) ->
|
|
|
|
UseTurn = proplists:get_bool(use_turn, Opts),
|
|
|
|
prepare_turn_opts(Opts, UseTurn).
|
|
|
|
|
|
|
|
prepare_turn_opts(Opts, _UseTurn = false) ->
|
2017-12-24 10:27:51 +01:00
|
|
|
set_certfile(Opts);
|
2014-05-08 14:08:07 +02:00
|
|
|
prepare_turn_opts(Opts, _UseTurn = true) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
NumberOfMyHosts = length(ejabberd_option:hosts()),
|
2020-05-29 18:40:19 +02:00
|
|
|
TurnIP = case proplists:get_value(turn_ipv4_address, Opts) of
|
2020-04-23 18:32:40 +02:00
|
|
|
undefined ->
|
2020-05-29 18:40:19 +02:00
|
|
|
MyIP = misc:get_my_ipv4_address(),
|
2020-04-23 18:32:40 +02:00
|
|
|
case MyIP of
|
|
|
|
{127, _, _, _} ->
|
2020-05-29 18:40:19 +02:00
|
|
|
?WARNING_MSG("Option 'turn_ipv4_address' is "
|
|
|
|
"undefined and the server's hostname "
|
|
|
|
"doesn't resolve to a public IPv4 "
|
|
|
|
"address, most likely the TURN relay "
|
|
|
|
"won't be working properly", []);
|
2020-04-23 18:32:40 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2020-05-29 18:40:19 +02:00
|
|
|
[{turn_ipv4_address, MyIP}];
|
2020-04-23 18:32:40 +02:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2020-04-22 00:09:42 +02:00
|
|
|
AuthFun = fun ejabberd_stun:get_password/2,
|
2017-05-08 13:34:35 +02:00
|
|
|
Shaper = proplists:get_value(shaper, Opts, none),
|
|
|
|
AuthType = proplists:get_value(auth_type, Opts, user),
|
|
|
|
Realm = case proplists:get_value(auth_realm, Opts) of
|
2014-05-08 14:08:07 +02:00
|
|
|
undefined when AuthType == user ->
|
|
|
|
if NumberOfMyHosts > 1 ->
|
2020-04-27 22:28:59 +02:00
|
|
|
?INFO_MSG("You have several virtual hosts "
|
|
|
|
"configured, but option 'auth_realm' is "
|
|
|
|
"undefined and 'auth_type' is set to "
|
|
|
|
"'user', so the TURN relay might not be "
|
|
|
|
"working properly. Using ~ts as a "
|
|
|
|
"fallback",
|
|
|
|
[ejabberd_config:get_myname()]);
|
2014-05-08 14:08:07 +02:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2018-06-14 13:00:47 +02:00
|
|
|
[{auth_realm, ejabberd_config:get_myname()}];
|
2014-05-08 14:08:07 +02:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2018-07-05 08:31:55 +02:00
|
|
|
MaxRate = ejabberd_shaper:get_max_rate(Shaper),
|
2020-04-23 18:32:40 +02:00
|
|
|
Opts1 = TurnIP ++ Realm ++ [{auth_fun, AuthFun},{shaper, MaxRate} |
|
|
|
|
lists:keydelete(shaper, 1, Opts)],
|
2017-12-24 10:27:51 +01:00
|
|
|
set_certfile(Opts1).
|
|
|
|
|
|
|
|
set_certfile(Opts) ->
|
|
|
|
case lists:keymember(certfile, 1, Opts) of
|
|
|
|
true ->
|
|
|
|
Opts;
|
|
|
|
false ->
|
2018-06-14 13:00:47 +02:00
|
|
|
Realm = proplists:get_value(auth_realm, Opts, ejabberd_config:get_myname()),
|
2017-12-24 10:27:51 +01:00
|
|
|
case ejabberd_pkix:get_certfile(Realm) of
|
|
|
|
{ok, CertFile} ->
|
|
|
|
[{certfile, CertFile}|Opts];
|
|
|
|
error ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Opts
|
2017-12-24 10:27:51 +01:00
|
|
|
end
|
|
|
|
end.
|
2017-04-30 18:01:47 +02:00
|
|
|
|
|
|
|
listen_opt_type(use_turn) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
|
|
|
listen_opt_type(ip) ->
|
2020-05-19 20:22:58 +02:00
|
|
|
econf:ip();
|
2020-05-29 18:40:19 +02:00
|
|
|
listen_opt_type(turn_ipv4_address) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:ipv4();
|
2020-05-29 18:40:19 +02:00
|
|
|
listen_opt_type(turn_ipv6_address) ->
|
2020-05-19 21:42:41 +02:00
|
|
|
econf:ipv6();
|
2017-04-30 18:01:47 +02:00
|
|
|
listen_opt_type(auth_type) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:enum([anonymous, user]);
|
2017-04-30 18:01:47 +02:00
|
|
|
listen_opt_type(auth_realm) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:binary();
|
2017-04-30 18:01:47 +02:00
|
|
|
listen_opt_type(turn_min_port) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:int(1025, 65535);
|
2017-04-30 18:01:47 +02:00
|
|
|
listen_opt_type(turn_max_port) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:int(1025, 65535);
|
2017-04-30 18:01:47 +02:00
|
|
|
listen_opt_type(turn_max_allocations) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:pos_int(infinity);
|
2017-04-30 18:01:47 +02:00
|
|
|
listen_opt_type(turn_max_permissions) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:pos_int(infinity);
|
2020-05-21 21:46:02 +02:00
|
|
|
listen_opt_type(turn_blacklist) ->
|
|
|
|
econf:list_or_single(econf:ip_mask());
|
2017-04-30 18:01:47 +02:00
|
|
|
listen_opt_type(server_name) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:binary();
|
|
|
|
listen_opt_type(certfile) ->
|
|
|
|
econf:pem().
|
2018-09-18 11:53:36 +02:00
|
|
|
|
|
|
|
listen_options() ->
|
|
|
|
[{shaper, none},
|
2018-09-29 22:05:41 +02:00
|
|
|
{use_turn, false},
|
2020-05-29 18:40:19 +02:00
|
|
|
{turn_ipv4_address, undefined},
|
|
|
|
{turn_ipv6_address, undefined},
|
2018-09-18 11:53:36 +02:00
|
|
|
{auth_type, user},
|
|
|
|
{auth_realm, undefined},
|
|
|
|
{tls, false},
|
|
|
|
{certfile, undefined},
|
|
|
|
{turn_min_port, 49152},
|
|
|
|
{turn_max_port, 65535},
|
|
|
|
{turn_max_allocations, 10},
|
|
|
|
{turn_max_permissions, 10},
|
2020-05-21 21:46:02 +02:00
|
|
|
{turn_blacklist, [<<"2001::/32">>, <<"2002::/16">>]}, % Teredo, 6to4.
|
2018-09-18 11:53:36 +02:00
|
|
|
{server_name, <<"ejabberd">>}].
|
2020-07-01 21:53:22 +02:00
|
|
|
|
|
|
|
-spec init_logger() -> ok.
|
|
|
|
-ifdef(LAGER).
|
|
|
|
init_logger() ->
|
|
|
|
ok.
|
|
|
|
-else.
|
|
|
|
init_logger() ->
|
2020-10-01 16:37:36 +02:00
|
|
|
case logger:add_primary_filter(ejabberd_stun, {fun ?MODULE:stun_filter/2,
|
|
|
|
?STUN_MAX_LOG_LEVEL}) of
|
2020-07-01 21:53:22 +02:00
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
{error, {already_exist, _}} ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec stun_filter(logger:log_event(), logger:level() | term())
|
|
|
|
-> logger:filter_return().
|
|
|
|
stun_filter(#{meta := #{domain := [stun | _]}, level := Level}, MaxLevel) ->
|
|
|
|
case logger:compare_levels(Level, MaxLevel) of
|
|
|
|
lt ->
|
|
|
|
stop;
|
|
|
|
_ ->
|
|
|
|
ignore
|
|
|
|
end;
|
|
|
|
stun_filter(Event, _Extra) ->
|
|
|
|
Event.
|
|
|
|
-endif.
|
|
|
|
|
2017-05-23 12:12:48 +02:00
|
|
|
-endif.
|