2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_s2s_in.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
2006-01-13 02:55:20 +01:00
|
|
|
%%% Purpose : Serve incoming s2s connection
|
2002-12-06 21:59:19 +01:00
|
|
|
%%% Created : 6 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_s2s_in).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% External exports
|
2003-11-10 22:25:36 +01:00
|
|
|
-export([start/2,
|
2006-01-13 02:55:20 +01:00
|
|
|
start_link/2,
|
|
|
|
become_controller/1,
|
|
|
|
match_domain/2]).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%% gen_fsm callbacks
|
2002-12-07 21:27:26 +01:00
|
|
|
-export([init/1,
|
|
|
|
wait_for_stream/2,
|
2005-10-25 03:08:37 +02:00
|
|
|
wait_for_feature_request/2,
|
2002-12-07 21:27:26 +01:00
|
|
|
stream_established/2,
|
2003-01-24 21:18:33 +01:00
|
|
|
handle_event/3,
|
|
|
|
handle_sync_event/4,
|
|
|
|
code_change/4,
|
2002-12-06 21:59:19 +01:00
|
|
|
handle_info/3,
|
|
|
|
terminate/3]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-05-12 20:36:13 +02:00
|
|
|
-include("jlib.hrl").
|
2006-04-01 23:43:52 +02:00
|
|
|
-include_lib("ssl/include/PKIX1Explicit88.hrl").
|
|
|
|
-include_lib("ssl/include/PKIX1Implicit88.hrl").
|
2005-11-17 06:29:33 +01:00
|
|
|
-include("XmppAddr.hrl").
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2003-10-29 21:09:09 +01:00
|
|
|
-define(DICT, dict).
|
|
|
|
|
2003-11-10 22:25:36 +01:00
|
|
|
-record(state, {socket,
|
2005-10-25 03:08:37 +02:00
|
|
|
sockmod,
|
2003-11-10 22:25:36 +01:00
|
|
|
receiver,
|
|
|
|
streamid,
|
|
|
|
shaper,
|
2005-10-25 03:08:37 +02:00
|
|
|
tls = false,
|
|
|
|
tls_enabled = false,
|
|
|
|
tls_options = [],
|
2005-11-03 06:04:54 +01:00
|
|
|
authenticated = false,
|
|
|
|
auth_domain,
|
2003-12-06 20:58:49 +01:00
|
|
|
connections = ?DICT:new(),
|
|
|
|
timer}).
|
2003-10-29 21:09:09 +01:00
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2003-01-13 21:51:17 +01:00
|
|
|
%-define(DBGFSM, true).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
|
|
|
-else.
|
|
|
|
-define(FSMOPTS, []).
|
|
|
|
-endif.
|
|
|
|
|
2005-10-25 03:08:37 +02:00
|
|
|
-define(STREAM_HEADER(Version),
|
2003-09-03 19:49:42 +02:00
|
|
|
("<?xml version='1.0'?>"
|
|
|
|
"<stream:stream "
|
|
|
|
"xmlns:stream='http://etherx.jabber.org/streams' "
|
|
|
|
"xmlns='jabber:server' "
|
|
|
|
"xmlns:db='jabber:server:dialback' "
|
2005-10-25 03:08:37 +02:00
|
|
|
"id='" ++ StateData#state.streamid ++ "'" ++ Version ++ ">")
|
2002-12-06 21:59:19 +01:00
|
|
|
).
|
|
|
|
|
|
|
|
-define(STREAM_TRAILER, "</stream:stream>").
|
|
|
|
|
2003-05-12 20:36:13 +02:00
|
|
|
-define(INVALID_NAMESPACE_ERR,
|
|
|
|
xml:element_to_string(?SERR_INVALID_NAMESPACE)).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2003-05-12 20:36:13 +02:00
|
|
|
-define(HOST_UNKNOWN_ERR,
|
|
|
|
xml:element_to_string(?SERR_HOST_UNKNOWN)).
|
|
|
|
|
|
|
|
-define(INVALID_XML_ERR,
|
|
|
|
xml:element_to_string(?SERR_XML_NOT_WELL_FORMED)).
|
2003-01-04 21:09:25 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2003-07-20 22:35:35 +02:00
|
|
|
start(SockData, Opts) ->
|
|
|
|
supervisor:start_child(ejabberd_s2s_in_sup, [SockData, Opts]).
|
|
|
|
|
2003-11-10 22:25:36 +01:00
|
|
|
start_link(SockData, Opts) ->
|
|
|
|
gen_fsm:start_link(ejabberd_s2s_in, [SockData, Opts], ?FSMOPTS).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
become_controller(Pid) ->
|
|
|
|
gen_fsm:send_all_state_event(Pid, become_controller).
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, StateName, StateData} |
|
|
|
|
%% {ok, StateName, StateData, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, StopReason}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-11-10 22:25:36 +01:00
|
|
|
init([{SockMod, Socket}, Opts]) ->
|
2003-05-09 21:32:59 +02:00
|
|
|
?INFO_MSG("started: ~p", [{SockMod, Socket}]),
|
2003-11-10 22:25:36 +01:00
|
|
|
ReceiverPid = ejabberd_receiver:start(Socket, SockMod, none),
|
|
|
|
Shaper = case lists:keysearch(shaper, 1, Opts) of
|
|
|
|
{value, {_, S}} -> S;
|
|
|
|
_ -> none
|
|
|
|
end,
|
2005-10-25 03:08:37 +02:00
|
|
|
StartTLS = case ejabberd_config:get_local_option(s2s_use_starttls) of
|
|
|
|
undefined ->
|
|
|
|
false;
|
|
|
|
UseStartTLS ->
|
|
|
|
UseStartTLS
|
|
|
|
end,
|
|
|
|
TLSOpts = case ejabberd_config:get_local_option(s2s_certfile) of
|
|
|
|
undefined ->
|
|
|
|
[];
|
|
|
|
CertFile ->
|
|
|
|
[{certfile, CertFile}]
|
|
|
|
end,
|
2003-12-06 20:58:49 +01:00
|
|
|
Timer = erlang:start_timer(?S2STIMEOUT, self(), []),
|
2003-05-12 20:36:13 +02:00
|
|
|
{ok, wait_for_stream,
|
|
|
|
#state{socket = Socket,
|
2005-10-25 03:08:37 +02:00
|
|
|
sockmod = SockMod,
|
2003-05-12 20:36:13 +02:00
|
|
|
receiver = ReceiverPid,
|
2003-11-10 22:25:36 +01:00
|
|
|
streamid = new_id(),
|
2003-12-06 20:58:49 +01:00
|
|
|
shaper = Shaper,
|
2005-10-25 03:08:37 +02:00
|
|
|
tls = StartTLS,
|
|
|
|
tls_enabled = false,
|
|
|
|
tls_options = TLSOpts,
|
2003-12-06 20:58:49 +01:00
|
|
|
timer = Timer}}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/2
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
|
2003-10-29 21:09:09 +01:00
|
|
|
wait_for_stream({xmlstreamstart, _Name, Attrs}, StateData) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
case {xml:get_attr_s("xmlns", Attrs),
|
|
|
|
xml:get_attr_s("xmlns:db", Attrs),
|
|
|
|
xml:get_attr_s("version", Attrs) == "1.0"} of
|
|
|
|
{"jabber:server", "jabber:server:dialback", true} when
|
2005-11-03 06:04:54 +01:00
|
|
|
StateData#state.tls and (not StateData#state.authenticated) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData, ?STREAM_HEADER(" version='1.0'")),
|
2005-11-03 06:04:54 +01:00
|
|
|
SASL =
|
|
|
|
if
|
|
|
|
StateData#state.tls_enabled ->
|
|
|
|
case tls:get_peer_certificate(StateData#state.socket) of
|
|
|
|
{ok, _Cert} ->
|
|
|
|
case tls:get_verify_result(
|
|
|
|
StateData#state.socket) of
|
|
|
|
0 ->
|
|
|
|
[{xmlelement, "mechanisms",
|
|
|
|
[{"xmlns", ?NS_SASL}],
|
|
|
|
[{xmlelement, "mechanism", [],
|
|
|
|
[{xmlcdata, "EXTERNAL"}]}]}];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
error ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
StartTLS = if
|
|
|
|
StateData#state.tls_enabled ->
|
|
|
|
[];
|
|
|
|
true ->
|
|
|
|
[{xmlelement, "starttls",
|
|
|
|
[{"xmlns", ?NS_TLS}], []}]
|
|
|
|
end,
|
2005-10-25 03:08:37 +02:00
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "stream:features", [],
|
2005-11-03 06:04:54 +01:00
|
|
|
SASL ++ StartTLS}),
|
2005-10-25 03:08:37 +02:00
|
|
|
{next_state, wait_for_feature_request, StateData};
|
2005-11-03 06:04:54 +01:00
|
|
|
{"jabber:server", _, true} when
|
|
|
|
StateData#state.authenticated ->
|
|
|
|
send_text(StateData, ?STREAM_HEADER(" version='1.0'")),
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "stream:features", [], []}),
|
|
|
|
{next_state, stream_established, StateData};
|
2005-10-25 03:08:37 +02:00
|
|
|
{"jabber:server", "jabber:server:dialback", _} ->
|
|
|
|
send_text(StateData, ?STREAM_HEADER("")),
|
|
|
|
{next_state, stream_established, StateData};
|
2002-12-06 21:59:19 +01:00
|
|
|
_ ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData, ?INVALID_NAMESPACE_ERR),
|
2002-12-06 21:59:19 +01:00
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
|
2003-05-12 20:36:13 +02:00
|
|
|
wait_for_stream({xmlstreamerror, _}, StateData) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData,
|
|
|
|
?STREAM_HEADER("") ++ ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2003-05-12 20:36:13 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_stream(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
wait_for_stream(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2005-10-25 03:08:37 +02:00
|
|
|
|
|
|
|
wait_for_feature_request({xmlstreamelement, El}, StateData) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = El,
|
|
|
|
TLS = StateData#state.tls,
|
|
|
|
TLSEnabled = StateData#state.tls_enabled,
|
|
|
|
SockMod = StateData#state.sockmod,
|
|
|
|
case {xml:get_attr_s("xmlns", Attrs), Name} of
|
|
|
|
{?NS_TLS, "starttls"} when TLS == true,
|
|
|
|
TLSEnabled == false,
|
|
|
|
SockMod == gen_tcp ->
|
|
|
|
?INFO_MSG("starttls", []),
|
|
|
|
Socket = StateData#state.socket,
|
|
|
|
TLSOpts = StateData#state.tls_options,
|
|
|
|
{ok, TLSSocket} = tls:tcp_to_tls(Socket, TLSOpts),
|
|
|
|
ejabberd_receiver:starttls(StateData#state.receiver, TLSSocket),
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "proceed", [{"xmlns", ?NS_TLS}], []}),
|
|
|
|
{next_state, wait_for_stream,
|
|
|
|
StateData#state{sockmod = tls,
|
|
|
|
socket = TLSSocket,
|
|
|
|
streamid = new_id(),
|
|
|
|
tls_enabled = true
|
|
|
|
}};
|
2005-11-03 06:04:54 +01:00
|
|
|
{?NS_SASL, "auth"} when TLSEnabled ->
|
|
|
|
Mech = xml:get_attr_s("mechanism", Attrs),
|
|
|
|
case Mech of
|
|
|
|
"EXTERNAL" ->
|
|
|
|
Auth = jlib:decode_base64(xml:get_cdata(Els)),
|
|
|
|
AuthDomain = jlib:nameprep(Auth),
|
|
|
|
AuthRes =
|
|
|
|
case tls:get_peer_certificate(StateData#state.socket) of
|
|
|
|
{ok, Cert} ->
|
|
|
|
case tls:get_verify_result(
|
|
|
|
StateData#state.socket) of
|
|
|
|
0 ->
|
|
|
|
case AuthDomain of
|
|
|
|
error ->
|
|
|
|
false;
|
|
|
|
_ ->
|
2005-11-17 06:29:33 +01:00
|
|
|
case idna:domain_utf8_to_ascii(AuthDomain) of
|
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
PCAuthDomain ->
|
|
|
|
lists:any(
|
|
|
|
fun(D) ->
|
|
|
|
match_domain(
|
|
|
|
PCAuthDomain, D)
|
|
|
|
end, get_cert_domains(Cert))
|
|
|
|
end
|
2005-11-03 06:04:54 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
error ->
|
|
|
|
false
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
AuthRes ->
|
|
|
|
ejabberd_receiver:reset_stream(
|
|
|
|
StateData#state.receiver),
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "success",
|
|
|
|
[{"xmlns", ?NS_SASL}], []}),
|
|
|
|
?INFO_MSG("(~w) Accepted s2s authentication for ~s",
|
|
|
|
[StateData#state.socket, AuthDomain]),
|
|
|
|
{next_state, wait_for_stream,
|
|
|
|
StateData#state{streamid = new_id(),
|
|
|
|
authenticated = true,
|
|
|
|
auth_domain = AuthDomain
|
|
|
|
}};
|
|
|
|
true ->
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "failure",
|
|
|
|
[{"xmlns", ?NS_SASL}], []}),
|
|
|
|
send_text(StateData, ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "failure",
|
|
|
|
[{"xmlns", ?NS_SASL}],
|
|
|
|
[{xmlelement, "invalid-mechanism", [], []}]}),
|
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
2005-10-25 03:08:37 +02:00
|
|
|
_ ->
|
|
|
|
stream_established({xmlstreamelement, El}, StateData)
|
|
|
|
end;
|
|
|
|
|
|
|
|
wait_for_feature_request({xmlstreamend, _Name}, StateData) ->
|
|
|
|
send_text(StateData, ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_feature_request({xmlstreamerror, _}, StateData) ->
|
|
|
|
send_text(StateData, ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_feature_request(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
2003-10-29 21:09:09 +01:00
|
|
|
stream_established({xmlstreamelement, El}, StateData) ->
|
2003-12-06 20:58:49 +01:00
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = erlang:start_timer(?S2STIMEOUT, self(), []),
|
2002-12-06 21:59:19 +01:00
|
|
|
case is_key_packet(El) of
|
|
|
|
{key, To, From, Id, Key} ->
|
2003-05-09 21:32:59 +02:00
|
|
|
?INFO_MSG("GET KEY: ~p", [{To, From, Id, Key}]),
|
2003-10-29 21:09:09 +01:00
|
|
|
LTo = jlib:nameprep(To),
|
|
|
|
LFrom = jlib:nameprep(From),
|
|
|
|
case lists:member(LTo, ejabberd_router:dirty_get_all_domains()) of
|
2003-03-27 16:28:47 +01:00
|
|
|
true ->
|
2003-07-20 22:35:35 +02:00
|
|
|
ejabberd_s2s_out:start(To, From,
|
2003-09-03 19:49:42 +02:00
|
|
|
{verify, self(),
|
|
|
|
Key, StateData#state.streamid}),
|
2003-10-29 21:09:09 +01:00
|
|
|
Conns = ?DICT:store({LFrom, LTo}, wait_for_verification,
|
|
|
|
StateData#state.connections),
|
2005-06-20 05:18:13 +02:00
|
|
|
change_shaper(StateData, LTo, jlib:make_jid("", LFrom, "")),
|
2003-03-27 16:28:47 +01:00
|
|
|
{next_state,
|
2003-10-29 21:09:09 +01:00
|
|
|
stream_established,
|
2003-12-06 20:58:49 +01:00
|
|
|
StateData#state{connections = Conns,
|
|
|
|
timer = Timer}};
|
2003-03-27 16:28:47 +01:00
|
|
|
_ ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData, ?HOST_UNKNOWN_ERR),
|
2003-03-27 16:28:47 +01:00
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
2002-12-06 21:59:19 +01:00
|
|
|
{verify, To, From, Id, Key} ->
|
2003-05-09 21:32:59 +02:00
|
|
|
?INFO_MSG("VERIFY KEY: ~p", [{To, From, Id, Key}]),
|
2003-10-29 21:09:09 +01:00
|
|
|
LTo = jlib:nameprep(To),
|
|
|
|
LFrom = jlib:nameprep(From),
|
|
|
|
Key1 = ejabberd_s2s:get_key({LTo, LFrom}),
|
2002-12-07 21:27:26 +01:00
|
|
|
Type = if Key == Key1 -> "valid";
|
|
|
|
true -> "invalid"
|
|
|
|
end,
|
2005-10-25 03:08:37 +02:00
|
|
|
send_element(StateData,
|
2002-12-07 21:27:26 +01:00
|
|
|
{xmlelement,
|
|
|
|
"db:verify",
|
2003-10-29 21:09:09 +01:00
|
|
|
[{"from", To},
|
2002-12-07 21:27:26 +01:00
|
|
|
{"to", From},
|
|
|
|
{"id", Id},
|
|
|
|
{"type", Type}],
|
|
|
|
[]}),
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, stream_established, StateData#state{timer = Timer}};
|
2002-12-06 21:59:19 +01:00
|
|
|
_ ->
|
2006-01-25 01:35:12 +01:00
|
|
|
NewEl = jlib:remove_attr("xmlns", El),
|
|
|
|
{xmlelement, Name, Attrs, _Els} = NewEl,
|
2003-10-29 21:09:09 +01:00
|
|
|
From_s = xml:get_attr_s("from", Attrs),
|
|
|
|
From = jlib:string_to_jid(From_s),
|
|
|
|
To_s = xml:get_attr_s("to", Attrs),
|
|
|
|
To = jlib:string_to_jid(To_s),
|
|
|
|
if
|
|
|
|
(To /= error) and (From /= error) ->
|
|
|
|
LFrom = From#jid.lserver,
|
|
|
|
LTo = To#jid.lserver,
|
2005-11-03 06:04:54 +01:00
|
|
|
if
|
|
|
|
StateData#state.authenticated ->
|
|
|
|
case (LFrom == StateData#state.auth_domain)
|
|
|
|
andalso
|
|
|
|
lists:member(
|
|
|
|
LTo,
|
|
|
|
ejabberd_router:dirty_get_all_domains()) of
|
|
|
|
true ->
|
|
|
|
if ((Name == "iq") or
|
|
|
|
(Name == "message") or
|
|
|
|
(Name == "presence")) ->
|
2006-01-25 01:35:12 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
From, To, NewEl);
|
2005-11-03 06:04:54 +01:00
|
|
|
true ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
false ->
|
2003-10-29 21:09:09 +01:00
|
|
|
error
|
|
|
|
end;
|
2005-11-03 06:04:54 +01:00
|
|
|
true ->
|
|
|
|
case ?DICT:find({LFrom, LTo},
|
|
|
|
StateData#state.connections) of
|
|
|
|
{ok, established} ->
|
|
|
|
if ((Name == "iq") or
|
|
|
|
(Name == "message") or
|
|
|
|
(Name == "presence")) ->
|
2006-01-25 01:35:12 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
From, To, NewEl);
|
2005-11-03 06:04:54 +01:00
|
|
|
true ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end
|
2003-10-29 21:09:09 +01:00
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
error
|
2003-12-06 20:58:49 +01:00
|
|
|
end,
|
|
|
|
{next_state, stream_established, StateData#state{timer = Timer}}
|
|
|
|
end;
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2003-10-29 21:09:09 +01:00
|
|
|
stream_established({valid, From, To}, StateData) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_element(StateData,
|
2002-12-07 21:27:26 +01:00
|
|
|
{xmlelement,
|
|
|
|
"db:result",
|
2003-10-29 21:09:09 +01:00
|
|
|
[{"from", To},
|
|
|
|
{"to", From},
|
2002-12-07 21:27:26 +01:00
|
|
|
{"type", "valid"}],
|
|
|
|
[]}),
|
2003-10-29 21:09:09 +01:00
|
|
|
LFrom = jlib:nameprep(From),
|
|
|
|
LTo = jlib:nameprep(To),
|
|
|
|
NSD = StateData#state{
|
|
|
|
connections = ?DICT:store({LFrom, LTo}, established,
|
|
|
|
StateData#state.connections)},
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, stream_established, NSD};
|
2003-10-29 21:09:09 +01:00
|
|
|
|
|
|
|
stream_established({invalid, From, To}, StateData) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_element(StateData,
|
2002-12-07 21:27:26 +01:00
|
|
|
{xmlelement,
|
|
|
|
"db:result",
|
2003-10-29 21:09:09 +01:00
|
|
|
[{"from", To},
|
|
|
|
{"to", From},
|
2002-12-07 21:27:26 +01:00
|
|
|
{"type", "invalid"}],
|
|
|
|
[]}),
|
2003-10-29 21:09:09 +01:00
|
|
|
LFrom = jlib:nameprep(From),
|
|
|
|
LTo = jlib:nameprep(To),
|
|
|
|
NSD = StateData#state{
|
2003-12-06 20:58:49 +01:00
|
|
|
connections = ?DICT:erase({LFrom, LTo},
|
2003-10-29 21:09:09 +01:00
|
|
|
StateData#state.connections)},
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, stream_established, NSD};
|
2003-10-29 21:09:09 +01:00
|
|
|
|
|
|
|
stream_established({xmlstreamend, _Name}, StateData) ->
|
2003-05-12 20:36:13 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
stream_established({xmlstreamerror, _}, StateData) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData,
|
|
|
|
?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2002-12-07 21:27:26 +01:00
|
|
|
{stop, normal, StateData};
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2003-01-26 21:16:53 +01:00
|
|
|
stream_established(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
stream_established(closed, StateData) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-01-24 21:18:33 +01:00
|
|
|
%state_name(Event, From, StateData) ->
|
|
|
|
% Reply = ok,
|
|
|
|
% {reply, Reply, state_name, StateData}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_event/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_event(become_controller, StateName, StateData) ->
|
|
|
|
ok = (StateData#state.sockmod):controlling_process(
|
|
|
|
StateData#state.socket,
|
|
|
|
StateData#state.receiver),
|
|
|
|
ejabberd_receiver:become_controller(StateData#state.receiver),
|
|
|
|
{next_state, StateName, StateData};
|
2003-10-29 21:09:09 +01:00
|
|
|
handle_event(_Event, StateName, StateData) ->
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, StateName, StateData}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_sync_event/4
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-10-29 21:09:09 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName, StateData) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, StateName, StateData}.
|
|
|
|
|
2003-10-29 21:09:09 +01:00
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
{ok, StateName, StateData}.
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_info/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_info({send_text, Text}, StateName, StateData) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData, Text),
|
2002-12-07 21:27:26 +01:00
|
|
|
{next_state, StateName, StateData};
|
2003-12-06 20:58:49 +01:00
|
|
|
|
2005-11-03 06:04:54 +01:00
|
|
|
handle_info({timeout, Timer, _}, _StateName,
|
2003-12-06 20:58:49 +01:00
|
|
|
#state{timer = Timer} = StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-10-29 21:09:09 +01:00
|
|
|
handle_info(_, StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData}.
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
2003-10-29 21:09:09 +01:00
|
|
|
terminate(Reason, _StateName, StateData) ->
|
2003-05-09 21:32:59 +02:00
|
|
|
?INFO_MSG("terminated: ~p", [Reason]),
|
2006-01-13 02:55:20 +01:00
|
|
|
ejabberd_receiver:close(StateData#state.receiver),
|
2002-12-06 21:59:19 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData, Text) ->
|
|
|
|
(StateData#state.sockmod):send(StateData#state.socket, Text).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2005-10-25 03:08:37 +02:00
|
|
|
send_element(StateData, El) ->
|
|
|
|
send_text(StateData, xml:element_to_string(El)).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
change_shaper(StateData, Host, JID) ->
|
|
|
|
Shaper = acl:match_rule(Host, StateData#state.shaper, JID),
|
2003-11-10 22:25:36 +01:00
|
|
|
ejabberd_receiver:change_shaper(StateData#state.receiver, Shaper).
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
new_id() ->
|
2002-12-13 21:58:27 +01:00
|
|
|
randoms:get_string().
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2003-12-06 20:58:49 +01:00
|
|
|
cancel_timer(Timer) ->
|
|
|
|
erlang:cancel_timer(Timer),
|
|
|
|
receive
|
|
|
|
{timeout, Timer, _} ->
|
|
|
|
ok
|
|
|
|
after 0 ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
is_key_packet({xmlelement, Name, Attrs, Els}) when Name == "db:result" ->
|
|
|
|
{key,
|
|
|
|
xml:get_attr_s("to", Attrs),
|
|
|
|
xml:get_attr_s("from", Attrs),
|
|
|
|
xml:get_attr_s("id", Attrs),
|
|
|
|
xml:get_cdata(Els)};
|
|
|
|
is_key_packet({xmlelement, Name, Attrs, Els}) when Name == "db:verify" ->
|
|
|
|
{verify,
|
|
|
|
xml:get_attr_s("to", Attrs),
|
|
|
|
xml:get_attr_s("from", Attrs),
|
|
|
|
xml:get_attr_s("id", Attrs),
|
|
|
|
xml:get_cdata(Els)};
|
|
|
|
is_key_packet(_) ->
|
|
|
|
false.
|
|
|
|
|
|
|
|
|
2005-11-03 06:04:54 +01:00
|
|
|
get_cert_domains(Cert) ->
|
|
|
|
{rdnSequence, Subject} =
|
|
|
|
(Cert#'Certificate'.tbsCertificate)#'TBSCertificate'.subject,
|
2005-11-05 22:15:53 +01:00
|
|
|
Extensions =
|
|
|
|
(Cert#'Certificate'.tbsCertificate)#'TBSCertificate'.extensions,
|
2005-11-03 06:04:54 +01:00
|
|
|
lists:flatmap(
|
|
|
|
fun(#'AttributeTypeAndValue'{type = ?'id-at-commonName',
|
|
|
|
value = Val}) ->
|
2005-11-17 06:29:33 +01:00
|
|
|
case 'PKIX1Explicit88':decode('X520CommonName', Val) of
|
2005-11-03 06:04:54 +01:00
|
|
|
{ok, {_, D1}} ->
|
|
|
|
D = if
|
|
|
|
is_list(D1) -> D1;
|
|
|
|
is_binary(D1) -> binary_to_list(D1);
|
|
|
|
true -> error
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
D /= error ->
|
2005-11-05 22:15:53 +01:00
|
|
|
case jlib:string_to_jid(D) of
|
|
|
|
#jid{luser = "",
|
|
|
|
lserver = LD,
|
|
|
|
lresource = ""} ->
|
|
|
|
[LD];
|
|
|
|
_ ->
|
|
|
|
[]
|
2005-11-03 06:04:54 +01:00
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
[]
|
2005-11-05 22:15:53 +01:00
|
|
|
end, lists:flatten(Subject)) ++
|
|
|
|
lists:flatmap(
|
|
|
|
fun(#'Extension'{extnID = ?'id-ce-subjectAltName',
|
|
|
|
extnValue = Val}) ->
|
|
|
|
BVal = if
|
|
|
|
is_list(Val) -> list_to_binary(Val);
|
|
|
|
is_binary(Val) -> Val;
|
|
|
|
true -> Val
|
|
|
|
end,
|
|
|
|
case 'PKIX1Implicit88':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 jlib:string_to_jid(
|
|
|
|
binary_to_list(D)) of
|
|
|
|
#jid{luser = "",
|
|
|
|
lserver = LD,
|
|
|
|
lresource = ""} ->
|
2005-11-17 06:29:33 +01:00
|
|
|
case idna:domain_utf8_to_ascii(LD) of
|
|
|
|
false ->
|
|
|
|
[];
|
|
|
|
PCLD ->
|
|
|
|
[PCLD]
|
|
|
|
end;
|
2005-11-05 22:15:53 +01:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end;
|
2005-11-17 06:29:33 +01:00
|
|
|
({dNSName, D}) when is_list(D) ->
|
|
|
|
case jlib:string_to_jid(D) of
|
|
|
|
#jid{luser = "",
|
|
|
|
lserver = LD,
|
|
|
|
lresource = ""} ->
|
|
|
|
[LD];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end;
|
2005-11-05 22:15:53 +01:00
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, SANs);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Extensions).
|
2005-11-03 06:04:54 +01:00
|
|
|
|
2005-11-17 06:29:33 +01:00
|
|
|
match_domain(Domain, Domain) ->
|
|
|
|
true;
|
|
|
|
match_domain(Domain, Pattern) ->
|
|
|
|
DLabels = string:tokens(Domain, "."),
|
|
|
|
PLabels = string: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, PL) of
|
|
|
|
true ->
|
|
|
|
Regexp = regexp:sh_to_awk(PL),
|
|
|
|
case regexp:match(DL, Regexp) of
|
|
|
|
{match, _, _} ->
|
|
|
|
match_labels(DLabels, PLabels);
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end.
|
2005-11-03 06:04:54 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
|