2014-04-30 17:20:38 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-01-03 15:58:52 +01:00
|
|
|
%%% File : mod_sip.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Purpose : SIP RFC-3261
|
2014-04-30 17:20:38 +02:00
|
|
|
%%% Created : 21 Apr 2014 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2015-01-21 14:52:37 +01:00
|
|
|
%%%
|
2015-10-07 00:06:58 +02:00
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2014-2018 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-04-30 17:20:38 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-11-10 17:51:22 +01:00
|
|
|
|
2014-04-30 17:20:38 +02:00
|
|
|
-module(mod_sip).
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({rfc, 3261}).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
2017-05-23 12:12:48 +02:00
|
|
|
-include("logger.hrl").
|
|
|
|
|
|
|
|
-ifndef(SIP).
|
2018-01-23 08:54:52 +01:00
|
|
|
-export([start/2, stop/1, depends/2, mod_options/1]).
|
2017-05-23 12:12:48 +02:00
|
|
|
start(_, _) ->
|
|
|
|
?CRITICAL_MSG("ejabberd is not compiled with SIP support", []),
|
|
|
|
{error, sip_not_compiled}.
|
|
|
|
stop(_) ->
|
|
|
|
ok.
|
|
|
|
depends(_, _) ->
|
|
|
|
[].
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_options(_) ->
|
2017-05-23 12:12:48 +02:00
|
|
|
[].
|
|
|
|
-else.
|
2014-04-30 17:20:38 +02:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
-behaviour(esip).
|
|
|
|
|
|
|
|
%% API
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3,
|
|
|
|
make_response/2, is_my_host/1, at_my_host/1]).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-export([data_in/2, data_out/2, message_in/2,
|
|
|
|
message_out/2, request/2, request/3, response/2,
|
2018-01-23 08:54:52 +01:00
|
|
|
locate/1, mod_opt_type/1, mod_options/1, depends/2]).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
2014-07-05 12:01:29 +02:00
|
|
|
-include_lib("esip/include/esip.hrl").
|
2014-04-30 17:20:38 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
start(_Host, _Opts) ->
|
|
|
|
ejabberd:start_app(esip),
|
|
|
|
esip:set_config_value(max_server_transactions, 10000),
|
|
|
|
esip:set_config_value(max_client_transactions, 10000),
|
2018-06-14 13:00:47 +02:00
|
|
|
esip:set_config_value(
|
|
|
|
software, <<"ejabberd ", (ejabberd_config:get_version())/binary>>),
|
2014-04-30 17:20:38 +02:00
|
|
|
esip:set_config_value(module, ?MODULE),
|
|
|
|
Spec = {mod_sip_registrar, {mod_sip_registrar, start_link, []},
|
|
|
|
transient, 2000, worker, [mod_sip_registrar]},
|
|
|
|
TmpSupSpec = {mod_sip_proxy_sup,
|
|
|
|
{ejabberd_tmp_sup, start_link,
|
|
|
|
[mod_sip_proxy_sup, mod_sip_proxy]},
|
|
|
|
permanent, infinity, supervisor, [ejabberd_tmp_sup]},
|
2017-02-24 10:05:47 +01:00
|
|
|
supervisor:start_child(ejabberd_gen_mod_sup, Spec),
|
|
|
|
supervisor:start_child(ejabberd_gen_mod_sup, TmpSupSpec),
|
2014-04-30 17:20:38 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
stop(_Host) ->
|
|
|
|
ok.
|
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(_Host, _NewOpts, _OldOpts) ->
|
|
|
|
ok.
|
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2014-04-30 17:20:38 +02:00
|
|
|
data_in(Data, #sip_socket{type = Transport,
|
|
|
|
addr = {MyIP, MyPort},
|
|
|
|
peer = {PeerIP, PeerPort}}) ->
|
|
|
|
?DEBUG(
|
|
|
|
"SIP [~p/in] ~s:~p -> ~s:~p:~n~s",
|
|
|
|
[Transport, inet_parse:ntoa(PeerIP), PeerPort,
|
|
|
|
inet_parse:ntoa(MyIP), MyPort, Data]).
|
|
|
|
|
|
|
|
data_out(Data, #sip_socket{type = Transport,
|
|
|
|
addr = {MyIP, MyPort},
|
|
|
|
peer = {PeerIP, PeerPort}}) ->
|
|
|
|
?DEBUG(
|
|
|
|
"SIP [~p/out] ~s:~p -> ~s:~p:~n~s",
|
|
|
|
[Transport, inet_parse:ntoa(MyIP), MyPort,
|
|
|
|
inet_parse:ntoa(PeerIP), PeerPort, Data]).
|
|
|
|
|
|
|
|
message_in(#sip{type = request, method = M} = Req, SIPSock)
|
|
|
|
when M /= <<"ACK">>, M /= <<"CANCEL">> ->
|
|
|
|
case action(Req, SIPSock) of
|
2014-05-01 11:27:35 +02:00
|
|
|
{relay, _LServer} ->
|
2014-04-30 17:20:38 +02:00
|
|
|
ok;
|
|
|
|
Action ->
|
|
|
|
request(Req, SIPSock, undefined, Action)
|
|
|
|
end;
|
2014-06-06 07:32:07 +02:00
|
|
|
message_in(ping, SIPSock) ->
|
|
|
|
mod_sip_registrar:ping(SIPSock);
|
2014-04-30 17:20:38 +02:00
|
|
|
message_in(_, _) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
message_out(_, _) ->
|
|
|
|
ok.
|
|
|
|
|
2014-05-01 21:35:36 +02:00
|
|
|
response(_Resp, _SIPSock) ->
|
|
|
|
ok.
|
2014-04-30 17:20:38 +02:00
|
|
|
|
2014-05-23 18:13:31 +02:00
|
|
|
request(#sip{method = <<"ACK">>} = Req, SIPSock) ->
|
|
|
|
case action(Req, SIPSock) of
|
|
|
|
{relay, LServer} ->
|
2014-06-01 11:34:51 +02:00
|
|
|
mod_sip_proxy:route(Req, LServer, [{authenticated, true}]);
|
|
|
|
{proxy_auth, LServer} ->
|
|
|
|
mod_sip_proxy:route(Req, LServer, [{authenticated, false}]);
|
2014-05-23 18:13:31 +02:00
|
|
|
_ ->
|
2014-07-16 12:54:02 +02:00
|
|
|
ok
|
2014-05-23 18:13:31 +02:00
|
|
|
end;
|
2014-05-01 11:27:35 +02:00
|
|
|
request(_Req, _SIPSock) ->
|
2014-07-16 12:54:02 +02:00
|
|
|
ok.
|
2014-04-30 17:20:38 +02:00
|
|
|
|
|
|
|
request(Req, SIPSock, TrID) ->
|
|
|
|
request(Req, SIPSock, TrID, action(Req, SIPSock)).
|
|
|
|
|
|
|
|
request(Req, SIPSock, TrID, Action) ->
|
|
|
|
case Action of
|
|
|
|
to_me ->
|
|
|
|
process(Req, SIPSock);
|
|
|
|
register ->
|
|
|
|
mod_sip_registrar:request(Req, SIPSock);
|
|
|
|
loop ->
|
|
|
|
make_response(Req, #sip{status = 483, type = response});
|
|
|
|
{unsupported, Require} ->
|
|
|
|
make_response(Req, #sip{status = 420,
|
|
|
|
type = response,
|
|
|
|
hdrs = [{'unsupported',
|
|
|
|
Require}]});
|
2014-05-01 11:27:35 +02:00
|
|
|
{relay, LServer} ->
|
2014-05-02 08:51:08 +02:00
|
|
|
case mod_sip_proxy:start(LServer, []) of
|
2014-04-30 17:20:38 +02:00
|
|
|
{ok, Pid} ->
|
|
|
|
mod_sip_proxy:route(Req, SIPSock, TrID, Pid),
|
|
|
|
{mod_sip_proxy, route, [Pid]};
|
|
|
|
Err ->
|
2018-09-19 22:12:14 +02:00
|
|
|
?WARNING_MSG("Failed to proxy request ~p: ~p", [Req, Err]),
|
2014-04-30 17:20:38 +02:00
|
|
|
Err
|
|
|
|
end;
|
2014-06-01 11:34:51 +02:00
|
|
|
{proxy_auth, LServer} ->
|
2014-04-30 17:20:38 +02:00
|
|
|
make_response(
|
|
|
|
Req,
|
|
|
|
#sip{status = 407,
|
|
|
|
type = response,
|
|
|
|
hdrs = [{'proxy-authenticate',
|
2014-06-01 11:34:51 +02:00
|
|
|
make_auth_hdr(LServer)}]});
|
|
|
|
{auth, LServer} ->
|
2014-04-30 17:20:38 +02:00
|
|
|
make_response(
|
|
|
|
Req,
|
|
|
|
#sip{status = 401,
|
|
|
|
type = response,
|
|
|
|
hdrs = [{'www-authenticate',
|
2014-06-01 11:34:51 +02:00
|
|
|
make_auth_hdr(LServer)}]});
|
2014-04-30 17:20:38 +02:00
|
|
|
deny ->
|
|
|
|
make_response(Req, #sip{status = 403,
|
|
|
|
type = response});
|
|
|
|
not_found ->
|
|
|
|
make_response(Req, #sip{status = 480,
|
|
|
|
type = response})
|
|
|
|
end.
|
|
|
|
|
|
|
|
locate(_SIPMsg) ->
|
|
|
|
ok.
|
|
|
|
|
2014-05-01 21:58:05 +02:00
|
|
|
find(#uri{user = User, host = Host}) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Host),
|
2014-05-01 21:58:05 +02:00
|
|
|
if LUser == <<"">> ->
|
|
|
|
to_me;
|
|
|
|
true ->
|
|
|
|
case mod_sip_registrar:find_sockets(LUser, LServer) of
|
|
|
|
[] ->
|
|
|
|
not_found;
|
|
|
|
[_|_] ->
|
|
|
|
{relay, LServer}
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2014-04-30 17:20:38 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
action(#sip{method = <<"REGISTER">>, type = request, hdrs = Hdrs,
|
|
|
|
uri = #uri{user = <<"">>} = URI} = Req, SIPSock) ->
|
|
|
|
case at_my_host(URI) of
|
|
|
|
true ->
|
2014-06-06 07:32:07 +02:00
|
|
|
Require = esip:get_hdrs('require', Hdrs) -- supported(),
|
|
|
|
case Require of
|
|
|
|
[_|_] ->
|
2014-04-30 17:20:38 +02:00
|
|
|
{unsupported, Require};
|
|
|
|
_ ->
|
|
|
|
{_, ToURI, _} = esip:get_hdr('to', Hdrs),
|
|
|
|
case at_my_host(ToURI) of
|
|
|
|
true ->
|
|
|
|
case check_auth(Req, 'authorization', SIPSock) of
|
|
|
|
true ->
|
|
|
|
register;
|
|
|
|
false ->
|
2015-11-24 16:44:13 +01:00
|
|
|
{auth, jid:nameprep(ToURI#uri.host)}
|
2014-04-30 17:20:38 +02:00
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
deny
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
deny
|
|
|
|
end;
|
|
|
|
action(#sip{method = Method, hdrs = Hdrs, type = request} = Req, SIPSock) ->
|
|
|
|
case esip:get_hdr('max-forwards', Hdrs) of
|
|
|
|
0 when Method == <<"OPTIONS">> ->
|
|
|
|
to_me;
|
|
|
|
0 ->
|
|
|
|
loop;
|
|
|
|
_ ->
|
2014-06-06 07:32:07 +02:00
|
|
|
Require = esip:get_hdrs('proxy-require', Hdrs) -- supported(),
|
|
|
|
case Require of
|
|
|
|
[_|_] ->
|
2014-04-30 17:20:38 +02:00
|
|
|
{unsupported, Require};
|
|
|
|
_ ->
|
|
|
|
{_, ToURI, _} = esip:get_hdr('to', Hdrs),
|
|
|
|
{_, FromURI, _} = esip:get_hdr('from', Hdrs),
|
|
|
|
case at_my_host(FromURI) of
|
|
|
|
true ->
|
|
|
|
case check_auth(Req, 'proxy-authorization', SIPSock) of
|
|
|
|
true ->
|
|
|
|
case at_my_host(ToURI) of
|
|
|
|
true ->
|
2014-05-01 21:58:05 +02:00
|
|
|
find(ToURI);
|
2014-04-30 17:20:38 +02:00
|
|
|
false ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(FromURI#uri.host),
|
2014-05-01 11:27:35 +02:00
|
|
|
{relay, LServer}
|
2014-04-30 17:20:38 +02:00
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
{proxy_auth, FromURI#uri.host}
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
case at_my_host(ToURI) of
|
|
|
|
true ->
|
2014-05-01 21:58:05 +02:00
|
|
|
find(ToURI);
|
2014-04-30 17:20:38 +02:00
|
|
|
false ->
|
|
|
|
deny
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
check_auth(#sip{method = <<"CANCEL">>}, _, _SIPSock) ->
|
|
|
|
true;
|
|
|
|
check_auth(#sip{method = Method, hdrs = Hdrs, body = Body}, AuthHdr, _SIPSock) ->
|
|
|
|
Issuer = case AuthHdr of
|
|
|
|
'authorization' ->
|
|
|
|
to;
|
|
|
|
'proxy-authorization' ->
|
|
|
|
from
|
|
|
|
end,
|
|
|
|
{_, #uri{user = User, host = Host}, _} = esip:get_hdr(Issuer, Hdrs),
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Host),
|
2014-04-30 17:20:38 +02:00
|
|
|
case lists:filter(
|
|
|
|
fun({_, Params}) ->
|
|
|
|
Username = esip:get_param(<<"username">>, Params),
|
|
|
|
Realm = esip:get_param(<<"realm">>, Params),
|
|
|
|
(LUser == esip:unquote(Username))
|
|
|
|
and (LServer == esip:unquote(Realm))
|
|
|
|
end, esip:get_hdrs(AuthHdr, Hdrs)) of
|
|
|
|
[Auth|_] ->
|
|
|
|
case ejabberd_auth:get_password_s(LUser, LServer) of
|
|
|
|
<<"">> ->
|
|
|
|
false;
|
2015-08-02 13:47:06 +02:00
|
|
|
Password when is_binary(Password) ->
|
|
|
|
esip:check_auth(Auth, Method, Body, Password);
|
|
|
|
_ScramedPassword ->
|
|
|
|
?ERROR_MSG("unable to authenticate ~s@~s against SCRAM'ed "
|
|
|
|
"password", [LUser, LServer]),
|
|
|
|
false
|
2014-04-30 17:20:38 +02:00
|
|
|
end;
|
|
|
|
[] ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
allow() ->
|
|
|
|
[<<"OPTIONS">>, <<"REGISTER">>].
|
|
|
|
|
2014-06-06 07:32:07 +02:00
|
|
|
supported() ->
|
|
|
|
[<<"path">>, <<"outbound">>].
|
|
|
|
|
2014-04-30 17:20:38 +02:00
|
|
|
process(#sip{method = <<"OPTIONS">>} = Req, _) ->
|
|
|
|
make_response(Req, #sip{type = response, status = 200,
|
2014-06-06 07:32:07 +02:00
|
|
|
hdrs = [{'allow', allow()},
|
|
|
|
{'supported', supported()}]});
|
2014-04-30 17:20:38 +02:00
|
|
|
process(#sip{method = <<"REGISTER">>} = Req, _) ->
|
|
|
|
make_response(Req, #sip{type = response, status = 400});
|
|
|
|
process(Req, _) ->
|
|
|
|
make_response(Req, #sip{type = response, status = 405,
|
|
|
|
hdrs = [{'allow', allow()}]}).
|
|
|
|
|
|
|
|
make_auth_hdr(LServer) ->
|
2014-06-01 11:34:51 +02:00
|
|
|
{<<"Digest">>, [{<<"realm">>, esip:quote(LServer)},
|
2014-04-30 17:20:38 +02:00
|
|
|
{<<"qop">>, esip:quote(<<"auth">>)},
|
|
|
|
{<<"nonce">>, esip:quote(esip:make_hexstr(20))}]}.
|
|
|
|
|
|
|
|
make_response(Req, Resp) ->
|
|
|
|
esip:make_response(Req, Resp, esip:make_tag()).
|
|
|
|
|
|
|
|
at_my_host(#uri{host = Host}) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
is_my_host(jid:nameprep(Host)).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
|
|
|
is_my_host(LServer) ->
|
|
|
|
gen_mod:is_loaded(LServer, ?MODULE).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(always_record_route) ->
|
|
|
|
fun (true) -> true;
|
|
|
|
(false) -> false
|
|
|
|
end;
|
|
|
|
mod_opt_type(flow_timeout_tcp) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
|
|
|
mod_opt_type(flow_timeout_udp) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
|
|
|
mod_opt_type(record_route) ->
|
|
|
|
fun (IOList) ->
|
|
|
|
S = iolist_to_binary(IOList),
|
|
|
|
#uri{} = esip:decode_uri(S)
|
|
|
|
end;
|
|
|
|
mod_opt_type(routes) ->
|
|
|
|
fun (L) ->
|
|
|
|
lists:map(fun (IOList) ->
|
|
|
|
S = iolist_to_binary(IOList),
|
|
|
|
#uri{} = esip:decode_uri(S)
|
|
|
|
end,
|
|
|
|
L)
|
|
|
|
end;
|
|
|
|
mod_opt_type(via) ->
|
|
|
|
fun (L) ->
|
|
|
|
lists:map(fun (Opts) ->
|
|
|
|
Type = proplists:get_value(type, Opts),
|
|
|
|
Host = proplists:get_value(host, Opts),
|
|
|
|
Port = proplists:get_value(port, Opts),
|
|
|
|
true = (Type == tcp) or (Type == tls) or
|
|
|
|
(Type == udp),
|
|
|
|
true = is_binary(Host) and (Host /= <<"">>),
|
|
|
|
true = is_integer(Port) and (Port > 0) and
|
|
|
|
(Port < 65536)
|
|
|
|
or (Port == undefined),
|
|
|
|
{Type, {Host, Port}}
|
|
|
|
end,
|
|
|
|
L)
|
2018-01-23 08:54:52 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
mod_options(Host) ->
|
|
|
|
Route = <<"sip:", Host/binary, ";lr">>,
|
|
|
|
[{always_record_route, true},
|
|
|
|
{flow_timeout_tcp, 120},
|
|
|
|
{flow_timeout_udp, 29},
|
|
|
|
{record_route, Route},
|
|
|
|
{routes, [Route]},
|
|
|
|
{via, []}].
|
2017-05-23 12:12:48 +02:00
|
|
|
|
|
|
|
-endif.
|