2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_c2s.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-12-17 21:13:21 +01:00
|
|
|
%%% Purpose : Serve C2S connection
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 16 Nov 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-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_c2s).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 78, '2.5'}).
|
|
|
|
-protocol({xep, 138, '2.0'}).
|
|
|
|
-protocol({xep, 198, '1.3'}).
|
|
|
|
|
2006-01-27 06:00:27 +01:00
|
|
|
-update_info({update, 0}).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2010-01-28 14:00:04 +01:00
|
|
|
-define(GEN_FSM, p1_fsm).
|
|
|
|
|
|
|
|
-behaviour(?GEN_FSM).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
%% External exports
|
2003-07-20 22:35:35 +02:00
|
|
|
-export([start/2,
|
2009-07-30 12:25:54 +02:00
|
|
|
stop/1,
|
2003-07-20 22:35:35 +02:00
|
|
|
start_link/2,
|
2015-10-17 23:15:31 +02:00
|
|
|
close/1,
|
2003-07-20 22:35:35 +02:00
|
|
|
send_text/2,
|
2004-03-02 22:16:55 +01:00
|
|
|
send_element/2,
|
2006-09-25 05:51:11 +02:00
|
|
|
socket_type/0,
|
2007-12-01 06:16:30 +01:00
|
|
|
get_presence/1,
|
2010-09-28 06:18:57 +02:00
|
|
|
get_aux_field/2,
|
|
|
|
set_aux_field/3,
|
|
|
|
del_aux_field/2,
|
|
|
|
get_subscription/2,
|
2014-11-14 01:03:26 +01:00
|
|
|
send_filtered/5,
|
2010-09-28 06:18:57 +02:00
|
|
|
broadcast/4,
|
2013-08-12 14:25:05 +02:00
|
|
|
get_subscribed/1,
|
|
|
|
transform_listen_option/2]).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-export([init/1, wait_for_stream/2, wait_for_auth/2,
|
|
|
|
wait_for_feature_request/2, wait_for_bind/2,
|
2016-01-18 14:33:37 +01:00
|
|
|
wait_for_sasl_response/2,
|
2015-06-01 14:38:27 +02:00
|
|
|
wait_for_resume/2, session_established/2,
|
|
|
|
handle_event/3, handle_sync_event/4, code_change/4,
|
|
|
|
handle_info/3, terminate/3, print_state/1, opt_type/1]).
|
2002-11-18 21:39:47 +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-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-04-10 10:14:38 +02:00
|
|
|
-include("mod_privacy.hrl").
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
-define(SETS, gb_sets).
|
2007-12-01 06:16:30 +01:00
|
|
|
-define(DICT, dict).
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2008-11-08 18:50:54 +01:00
|
|
|
%% pres_a contains all the presence available send (either through roster mechanism or directed).
|
|
|
|
%% Directed presence unavailable remove user from pres_a.
|
2006-09-05 06:26:28 +02:00
|
|
|
-record(state, {socket,
|
2006-10-01 03:53:37 +02:00
|
|
|
sockmod,
|
2007-04-12 08:08:32 +02:00
|
|
|
socket_monitor,
|
2009-08-31 20:37:52 +02:00
|
|
|
xml_socket,
|
2003-04-15 15:48:43 +02:00
|
|
|
streamid,
|
2003-03-09 21:46:47 +01:00
|
|
|
sasl_state,
|
2003-02-01 21:21:28 +01:00
|
|
|
access,
|
2003-02-09 20:17:23 +01:00
|
|
|
shaper,
|
2006-01-19 03:17:31 +01:00
|
|
|
zlib = false,
|
2004-07-28 22:08:53 +02:00
|
|
|
tls = false,
|
2004-12-05 21:54:55 +01:00
|
|
|
tls_required = false,
|
2004-07-28 22:08:53 +02:00
|
|
|
tls_enabled = false,
|
|
|
|
tls_options = [],
|
2005-07-15 00:28:21 +02:00
|
|
|
authenticated = false,
|
2003-10-07 22:31:44 +02:00
|
|
|
jid,
|
2014-11-14 15:32:48 +01:00
|
|
|
user = <<"">>, server = <<"">>, resource = <<"">>,
|
2006-01-24 00:13:06 +01:00
|
|
|
sid,
|
2002-12-17 21:49:45 +01:00
|
|
|
pres_t = ?SETS:new(),
|
|
|
|
pres_f = ?SETS:new(),
|
|
|
|
pres_a = ?SETS:new(),
|
2014-11-26 00:15:19 +01:00
|
|
|
pres_last,
|
2002-12-17 21:49:45 +01:00
|
|
|
pres_timestamp,
|
2008-04-10 10:14:38 +02:00
|
|
|
privacy_list = #userlist{},
|
2008-02-15 17:35:32 +01:00
|
|
|
conn = unknown,
|
2008-04-22 19:41:30 +02:00
|
|
|
auth_module = unknown,
|
2007-05-21 05:21:34 +02:00
|
|
|
ip,
|
2010-09-28 06:18:57 +02:00
|
|
|
aux_fields = [],
|
2014-09-11 17:44:29 +02:00
|
|
|
csi_state = active,
|
|
|
|
csi_queue = [],
|
2014-05-06 07:27:10 +02:00
|
|
|
mgmt_state,
|
|
|
|
mgmt_xmlns,
|
|
|
|
mgmt_queue,
|
|
|
|
mgmt_max_queue,
|
|
|
|
mgmt_pending_since,
|
|
|
|
mgmt_timeout,
|
2015-09-03 21:20:15 +02:00
|
|
|
mgmt_max_timeout,
|
2014-05-06 07:27:10 +02:00
|
|
|
mgmt_resend,
|
|
|
|
mgmt_stanzas_in = 0,
|
|
|
|
mgmt_stanzas_out = 0,
|
2014-04-29 11:41:24 +02:00
|
|
|
lang = <<"">>}).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
%-define(DBGFSM, true).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
-else.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
-define(FSMOPTS, []).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
-endif.
|
|
|
|
|
2007-09-14 16:17:42 +02:00
|
|
|
%% This is the timeout to apply between event when starting a new
|
|
|
|
%% session:
|
2007-12-06 18:42:25 +01:00
|
|
|
-define(C2S_OPEN_TIMEOUT, 60000).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-09-14 16:18:01 +02:00
|
|
|
-define(C2S_HIBERNATE_TIMEOUT, 90000).
|
2007-09-14 16:17:42 +02:00
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
-define(STREAM_HEADER,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"<?xml version='1.0'?><stream:stream "
|
|
|
|
"xmlns='jabber:client' xmlns:stream='http://et"
|
|
|
|
"herx.jabber.org/streams' id='~s' from='~s'~s~"
|
|
|
|
"s>">>).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(STREAM_TRAILER, <<"</stream:stream>">>).
|
2002-11-19 21:58:49 +01:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
-define(INVALID_NS_ERR, ?SERR_INVALID_NAMESPACE).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
-define(INVALID_XML_ERR, ?SERR_XML_NOT_WELL_FORMED).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
-define(HOST_UNKNOWN_ERR, ?SERR_HOST_UNKNOWN).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2004-12-05 21:54:55 +01:00
|
|
|
-define(POLICY_VIOLATION_ERR(Lang, Text),
|
2009-08-31 20:37:52 +02:00
|
|
|
?SERRT_POLICY_VIOLATION(Lang, Text)).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
-define(INVALID_FROM, ?SERR_INVALID_FROM).
|
2008-12-08 10:21:36 +01:00
|
|
|
|
2014-03-12 23:34:14 +01:00
|
|
|
%% XEP-0198:
|
|
|
|
|
|
|
|
-define(IS_STREAM_MGMT_TAG(Name),
|
2015-05-31 16:56:16 +02:00
|
|
|
(Name == <<"enable">>) or
|
|
|
|
(Name == <<"resume">>) or
|
|
|
|
(Name == <<"a">>) or
|
|
|
|
(Name == <<"r">>)).
|
2014-03-12 23:34:14 +01:00
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
-define(IS_SUPPORTED_MGMT_XMLNS(Xmlns),
|
2015-05-31 16:56:16 +02:00
|
|
|
(Xmlns == ?NS_STREAM_MGMT_2) or
|
|
|
|
(Xmlns == ?NS_STREAM_MGMT_3)).
|
2014-03-12 23:34:14 +01:00
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
-define(MGMT_FAILED(Condition, Xmlns),
|
2014-03-12 23:34:14 +01:00
|
|
|
#xmlel{name = <<"failed">>,
|
|
|
|
attrs = [{<<"xmlns">>, Xmlns}],
|
|
|
|
children = [#xmlel{name = Condition,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_STANZAS}],
|
|
|
|
children = []}]}).
|
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
-define(MGMT_BAD_REQUEST(Xmlns),
|
|
|
|
?MGMT_FAILED(<<"bad-request">>, Xmlns)).
|
2014-03-12 23:34:14 +01:00
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
-define(MGMT_ITEM_NOT_FOUND(Xmlns),
|
|
|
|
?MGMT_FAILED(<<"item-not-found">>, Xmlns)).
|
2014-03-19 00:51:33 +01:00
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
-define(MGMT_SERVICE_UNAVAILABLE(Xmlns),
|
|
|
|
?MGMT_FAILED(<<"service-unavailable">>, Xmlns)).
|
2014-03-12 23:34:14 +01:00
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
-define(MGMT_UNEXPECTED_REQUEST(Xmlns),
|
|
|
|
?MGMT_FAILED(<<"unexpected-request">>, Xmlns)).
|
2014-03-12 23:34:14 +01:00
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
-define(MGMT_UNSUPPORTED_VERSION(Xmlns),
|
|
|
|
?MGMT_FAILED(<<"unsupported-version">>, Xmlns)).
|
2002-11-19 21:58:49 +01:00
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2003-07-20 22:35:35 +02:00
|
|
|
start(SockData, Opts) ->
|
2015-11-04 16:24:35 +01:00
|
|
|
?GEN_FSM:start(ejabberd_c2s,
|
|
|
|
[SockData, Opts],
|
|
|
|
fsm_limit_opts(Opts) ++ ?FSMOPTS).
|
2003-07-20 22:35:35 +02:00
|
|
|
|
2003-07-14 20:06:03 +02:00
|
|
|
start_link(SockData, Opts) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
(?GEN_FSM):start_link(ejabberd_c2s,
|
|
|
|
[SockData, Opts],
|
|
|
|
fsm_limit_opts(Opts) ++ ?FSMOPTS).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
socket_type() -> xml_stream.
|
2006-09-25 05:51:11 +02:00
|
|
|
|
2004-03-02 22:16:55 +01:00
|
|
|
%% Return Username, Resource and presence information
|
|
|
|
get_presence(FsmRef) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
(?GEN_FSM):sync_send_all_state_event(FsmRef,
|
|
|
|
{get_presence}, 1000).
|
2004-03-02 22:16:55 +01:00
|
|
|
|
2010-09-28 06:18:57 +02:00
|
|
|
get_aux_field(Key, #state{aux_fields = Opts}) ->
|
|
|
|
case lists:keysearch(Key, 1, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, {_, Val}} -> {ok, Val};
|
|
|
|
_ -> error
|
2010-09-28 06:18:57 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
set_aux_field(Key, Val,
|
|
|
|
#state{aux_fields = Opts} = State) ->
|
2010-09-28 06:18:57 +02:00
|
|
|
Opts1 = lists:keydelete(Key, 1, Opts),
|
2013-03-14 10:33:02 +01:00
|
|
|
State#state{aux_fields = [{Key, Val} | Opts1]}.
|
2010-09-28 06:18:57 +02:00
|
|
|
|
|
|
|
del_aux_field(Key, #state{aux_fields = Opts} = State) ->
|
|
|
|
Opts1 = lists:keydelete(Key, 1, Opts),
|
|
|
|
State#state{aux_fields = Opts1}.
|
|
|
|
|
|
|
|
get_subscription(From = #jid{}, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
get_subscription(jid:tolower(From), StateData);
|
2010-09-28 06:18:57 +02:00
|
|
|
get_subscription(LFrom, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
LBFrom = setelement(3, LFrom, <<"">>),
|
|
|
|
F = (?SETS):is_element(LFrom, StateData#state.pres_f)
|
|
|
|
orelse
|
|
|
|
(?SETS):is_element(LBFrom, StateData#state.pres_f),
|
|
|
|
T = (?SETS):is_element(LFrom, StateData#state.pres_t)
|
|
|
|
orelse
|
|
|
|
(?SETS):is_element(LBFrom, StateData#state.pres_t),
|
2010-09-28 06:18:57 +02:00
|
|
|
if F and T -> both;
|
|
|
|
F -> from;
|
|
|
|
T -> to;
|
|
|
|
true -> none
|
|
|
|
end.
|
|
|
|
|
2014-11-14 01:03:26 +01:00
|
|
|
send_filtered(FsmRef, Feature, From, To, Packet) ->
|
|
|
|
FsmRef ! {send_filtered, Feature, From, To, Packet}.
|
|
|
|
|
2010-09-28 06:18:57 +02:00
|
|
|
broadcast(FsmRef, Type, From, Packet) ->
|
|
|
|
FsmRef ! {broadcast, Type, From, Packet}.
|
|
|
|
|
2015-10-17 23:15:31 +02:00
|
|
|
stop(FsmRef) -> (?GEN_FSM):send_event(FsmRef, stop).
|
|
|
|
|
|
|
|
close(FsmRef) -> (?GEN_FSM):send_event(FsmRef, closed).
|
2009-07-30 12:25:54 +02:00
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2006-10-01 03:53:37 +02:00
|
|
|
init([{SockMod, Socket}, Opts]) ->
|
2003-02-01 21:21:28 +01:00
|
|
|
Access = case lists:keysearch(access, 1, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, {_, A}} -> A;
|
|
|
|
_ -> all
|
2003-02-09 20:17:23 +01:00
|
|
|
end,
|
|
|
|
Shaper = case lists:keysearch(shaper, 1, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, {_, S}} -> S;
|
|
|
|
_ -> none
|
2003-02-01 21:21:28 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
XMLSocket = case lists:keysearch(xml_socket, 1, Opts) of
|
|
|
|
{value, {_, XS}} -> XS;
|
|
|
|
_ -> false
|
|
|
|
end,
|
2013-08-12 14:25:05 +02:00
|
|
|
Zlib = proplists:get_bool(zlib, Opts),
|
|
|
|
StartTLS = proplists:get_bool(starttls, Opts),
|
|
|
|
StartTLSRequired = proplists:get_bool(starttls_required, Opts),
|
|
|
|
TLSEnabled = proplists:get_bool(tls, Opts),
|
2013-03-14 10:33:02 +01:00
|
|
|
TLS = StartTLS orelse
|
|
|
|
StartTLSRequired orelse TLSEnabled,
|
|
|
|
TLSOpts1 = lists:filter(fun ({certfile, _}) -> true;
|
2013-11-28 18:39:11 +01:00
|
|
|
({ciphers, _}) -> true;
|
2015-05-26 21:06:04 +02:00
|
|
|
({dhfile, _}) -> true;
|
2013-03-14 10:33:02 +01:00
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
Opts),
|
2014-04-01 21:57:33 +02:00
|
|
|
TLSOpts2 = case lists:keysearch(protocol_options, 1, Opts) of
|
|
|
|
{value, {_, O}} ->
|
|
|
|
[_|ProtocolOptions] = lists:foldl(
|
|
|
|
fun(X, Acc) -> X ++ Acc end, [],
|
|
|
|
[["|" | binary_to_list(Opt)] || Opt <- O, is_binary(Opt)]
|
|
|
|
),
|
|
|
|
[{protocol_options, iolist_to_binary(ProtocolOptions)} | TLSOpts1];
|
|
|
|
_ -> TLSOpts1
|
2013-07-17 14:28:23 +02:00
|
|
|
end,
|
2014-04-01 21:57:33 +02:00
|
|
|
TLSOpts3 = case proplists:get_bool(tls_compression, Opts) of
|
|
|
|
false -> [compression_none | TLSOpts2];
|
|
|
|
true -> TLSOpts2
|
|
|
|
end,
|
|
|
|
TLSOpts = [verify_none | TLSOpts3],
|
2014-03-19 00:51:33 +01:00
|
|
|
StreamMgmtEnabled = proplists:get_value(stream_management, Opts, true),
|
|
|
|
StreamMgmtState = if StreamMgmtEnabled -> inactive;
|
|
|
|
true -> disabled
|
|
|
|
end,
|
2014-03-12 23:34:14 +01:00
|
|
|
MaxAckQueue = case proplists:get_value(max_ack_queue, Opts) of
|
|
|
|
Limit when is_integer(Limit), Limit > 0 -> Limit;
|
2014-08-20 12:53:26 +02:00
|
|
|
infinity -> infinity;
|
2015-07-01 21:15:05 +02:00
|
|
|
_ -> 1000
|
2014-03-12 23:34:14 +01:00
|
|
|
end,
|
2014-03-19 00:51:33 +01:00
|
|
|
ResumeTimeout = case proplists:get_value(resume_timeout, Opts) of
|
|
|
|
Timeout when is_integer(Timeout), Timeout >= 0 -> Timeout;
|
|
|
|
_ -> 300
|
|
|
|
end,
|
2015-09-03 21:20:15 +02:00
|
|
|
MaxResumeTimeout = case proplists:get_value(max_resume_timeout, Opts) of
|
|
|
|
Max when is_integer(Max), Max >= ResumeTimeout -> Max;
|
|
|
|
_ -> ResumeTimeout
|
|
|
|
end,
|
2014-09-16 22:42:34 +02:00
|
|
|
ResendOnTimeout = case proplists:get_value(resend_on_timeout, Opts) of
|
|
|
|
Resend when is_boolean(Resend) -> Resend;
|
|
|
|
if_offline -> if_offline;
|
|
|
|
_ -> false
|
|
|
|
end,
|
2007-05-21 05:21:34 +02:00
|
|
|
IP = peerip(SockMod, Socket),
|
2014-08-17 15:38:38 +02:00
|
|
|
Socket1 = if TLSEnabled andalso
|
|
|
|
SockMod /= ejabberd_frontend_socket ->
|
|
|
|
SockMod:starttls(Socket, TLSOpts);
|
|
|
|
true -> Socket
|
|
|
|
end,
|
|
|
|
SocketMonitor = SockMod:monitor(Socket1),
|
|
|
|
StateData = #state{socket = Socket1, sockmod = SockMod,
|
|
|
|
socket_monitor = SocketMonitor,
|
|
|
|
xml_socket = XMLSocket, zlib = Zlib, tls = TLS,
|
|
|
|
tls_required = StartTLSRequired,
|
|
|
|
tls_enabled = TLSEnabled, tls_options = TLSOpts,
|
2015-12-07 16:08:57 +01:00
|
|
|
sid = {p1_time_compat:timestamp(), self()}, streamid = new_id(),
|
2014-08-17 15:38:38 +02:00
|
|
|
access = Access, shaper = Shaper, ip = IP,
|
|
|
|
mgmt_state = StreamMgmtState,
|
|
|
|
mgmt_max_queue = MaxAckQueue,
|
|
|
|
mgmt_timeout = ResumeTimeout,
|
2015-09-03 21:20:15 +02:00
|
|
|
mgmt_max_timeout = MaxResumeTimeout,
|
2014-08-17 15:38:38 +02:00
|
|
|
mgmt_resend = ResendOnTimeout},
|
|
|
|
{ok, wait_for_stream, StateData, ?C2S_OPEN_TIMEOUT}.
|
2004-07-28 22:08:53 +02:00
|
|
|
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Return list of all available resources of contacts,
|
2008-08-27 11:28:47 +02:00
|
|
|
get_subscribed(FsmRef) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
(?GEN_FSM):sync_send_all_state_event(FsmRef,
|
|
|
|
get_subscribed, 1000).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
wait_for_stream({xmlstreamstart, _Name, Attrs}, StateData) ->
|
2013-06-24 12:04:56 +02:00
|
|
|
DefaultLang = ?MYLANG,
|
2013-03-14 10:33:02 +01:00
|
|
|
case xml:get_attr_s(<<"xmlns:stream">>, Attrs) of
|
2003-03-09 21:46:47 +01:00
|
|
|
?NS_STREAM ->
|
2015-10-07 00:06:58 +02:00
|
|
|
Server =
|
|
|
|
case StateData#state.server of
|
|
|
|
<<"">> ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:nameprep(xml:get_attr_s(<<"to">>, Attrs));
|
2015-10-07 00:06:58 +02:00
|
|
|
S -> S
|
|
|
|
end,
|
2014-08-17 15:38:38 +02:00
|
|
|
Lang = case xml:get_attr_s(<<"xml:lang">>, Attrs) of
|
2015-10-07 00:06:58 +02:00
|
|
|
Lang1 when byte_size(Lang1) =< 35 ->
|
|
|
|
%% As stated in BCP47, 4.4.1:
|
|
|
|
%% Protocols or specifications that
|
|
|
|
%% specify limited buffer sizes for
|
|
|
|
%% language tags MUST allow for
|
|
|
|
%% language tags of at least 35 characters.
|
|
|
|
Lang1;
|
|
|
|
_ ->
|
|
|
|
%% Do not store long language tag to
|
|
|
|
%% avoid possible DoS/flood attacks
|
|
|
|
<<"">>
|
|
|
|
end,
|
2014-08-17 15:38:38 +02:00
|
|
|
IsBlacklistedIP = is_ip_blacklisted(StateData#state.ip, Lang),
|
2005-04-17 20:08:34 +02:00
|
|
|
case lists:member(Server, ?MYHOSTS) of
|
2014-08-17 15:38:38 +02:00
|
|
|
true when IsBlacklistedIP == false ->
|
2015-11-24 16:44:13 +01:00
|
|
|
change_shaper(StateData, jid:make(<<"">>, Server, <<"">>)),
|
2013-03-14 10:33:02 +01:00
|
|
|
case xml:get_attr_s(<<"version">>, Attrs) of
|
|
|
|
<<"1.0">> ->
|
|
|
|
send_header(StateData, Server, <<"1.0">>, DefaultLang),
|
2005-07-15 00:28:21 +02:00
|
|
|
case StateData#state.authenticated of
|
2005-04-17 20:08:34 +02:00
|
|
|
false ->
|
2014-10-23 10:04:14 +02:00
|
|
|
TLS = StateData#state.tls,
|
|
|
|
TLSEnabled = StateData#state.tls_enabled,
|
|
|
|
TLSRequired = StateData#state.tls_required,
|
2015-10-07 00:06:58 +02:00
|
|
|
SASLState = cyrsasl:server_new(
|
|
|
|
<<"jabber">>, Server, <<"">>, [],
|
|
|
|
fun (U) ->
|
|
|
|
ejabberd_auth:get_password_with_authmodule(
|
|
|
|
U, Server)
|
|
|
|
end,
|
|
|
|
fun (U, P) ->
|
|
|
|
ejabberd_auth:check_password_with_authmodule(
|
|
|
|
U, Server, P)
|
|
|
|
end,
|
|
|
|
fun (U, P, D, DG) ->
|
|
|
|
ejabberd_auth:check_password_with_authmodule(
|
|
|
|
U, Server, P, D, DG)
|
|
|
|
end),
|
2014-10-23 10:04:14 +02:00
|
|
|
Mechs =
|
|
|
|
case TLSEnabled or not TLSRequired of
|
2015-10-07 00:06:58 +02:00
|
|
|
true ->
|
|
|
|
Ms = lists:map(fun (S) ->
|
|
|
|
#xmlel{name = <<"mechanism">>,
|
|
|
|
attrs = [],
|
|
|
|
children = [{xmlcdata, S}]}
|
|
|
|
end,
|
|
|
|
cyrsasl:listmech(Server)),
|
|
|
|
[#xmlel{name = <<"mechanisms">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_SASL}],
|
|
|
|
children = Ms}];
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end,
|
2006-10-01 03:53:37 +02:00
|
|
|
SockMod =
|
2015-10-07 00:06:58 +02:00
|
|
|
(StateData#state.sockmod):get_sockmod(StateData#state.socket),
|
2006-01-19 03:17:31 +01:00
|
|
|
Zlib = StateData#state.zlib,
|
2015-10-07 00:06:58 +02:00
|
|
|
CompressFeature = case Zlib andalso
|
|
|
|
((SockMod == gen_tcp) orelse (SockMod == p1_tls)) of
|
|
|
|
true ->
|
|
|
|
[#xmlel{name = <<"compression">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_FEATURE_COMPRESS}],
|
|
|
|
children = [#xmlel{name = <<"method">>,
|
|
|
|
attrs = [],
|
|
|
|
children = [{xmlcdata, <<"zlib">>}]}]}];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2005-04-17 20:08:34 +02:00
|
|
|
TLSFeature =
|
|
|
|
case (TLS == true) andalso
|
2015-10-07 00:06:58 +02:00
|
|
|
(TLSEnabled == false) andalso
|
|
|
|
(SockMod == gen_tcp) of
|
|
|
|
true ->
|
|
|
|
case TLSRequired of
|
|
|
|
true ->
|
|
|
|
[#xmlel{name = <<"starttls">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_TLS}],
|
|
|
|
children = [#xmlel{name = <<"required">>,
|
|
|
|
attrs = [],
|
|
|
|
children = []}]}];
|
|
|
|
_ ->
|
|
|
|
[#xmlel{name = <<"starttls">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_TLS}],
|
|
|
|
children = []}]
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end,
|
2015-05-07 11:41:59 +02:00
|
|
|
StreamFeatures1 = TLSFeature ++ CompressFeature ++ Mechs,
|
|
|
|
StreamFeatures = ejabberd_hooks:run_fold(c2s_stream_features,
|
2015-10-07 00:06:58 +02:00
|
|
|
Server, StreamFeatures1, [Server]),
|
2005-04-17 20:08:34 +02:00
|
|
|
send_element(StateData,
|
2015-10-07 00:06:58 +02:00
|
|
|
#xmlel{name = <<"stream:features">>,
|
|
|
|
attrs = [],
|
|
|
|
children = StreamFeatures}),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(wait_for_feature_request,
|
2015-10-07 00:06:58 +02:00
|
|
|
StateData#state{server = Server,
|
|
|
|
sasl_state = SASLState,
|
|
|
|
lang = Lang});
|
2003-11-07 21:51:23 +01:00
|
|
|
_ ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case StateData#state.resource of
|
2015-10-07 00:06:58 +02:00
|
|
|
<<"">> ->
|
|
|
|
RosterVersioningFeature =
|
|
|
|
ejabberd_hooks:run_fold(roster_get_versioning_feature,
|
|
|
|
Server, [],
|
|
|
|
[Server]),
|
|
|
|
StreamManagementFeature =
|
|
|
|
case stream_mgmt_enabled(StateData) of
|
|
|
|
true ->
|
|
|
|
[#xmlel{name = <<"sm">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_STREAM_MGMT_2}],
|
|
|
|
children = []},
|
|
|
|
#xmlel{name = <<"sm">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_STREAM_MGMT_3}],
|
|
|
|
children = []}];
|
|
|
|
false ->
|
|
|
|
[]
|
2014-03-12 23:34:14 +01:00
|
|
|
end,
|
2015-10-07 00:06:58 +02:00
|
|
|
StreamFeatures1 = [#xmlel{name = <<"bind">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_BIND}],
|
|
|
|
children = []},
|
|
|
|
#xmlel{name = <<"session">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_SESSION}],
|
2016-01-18 14:33:37 +01:00
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"optional">>}]}]
|
2015-10-07 00:06:58 +02:00
|
|
|
++
|
|
|
|
RosterVersioningFeature ++
|
|
|
|
StreamManagementFeature ++
|
|
|
|
ejabberd_hooks:run_fold(c2s_post_auth_features,
|
|
|
|
Server, [], [Server]),
|
|
|
|
StreamFeatures = ejabberd_hooks:run_fold(c2s_stream_features,
|
|
|
|
Server, StreamFeatures1, [Server]),
|
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"stream:features">>,
|
|
|
|
attrs = [],
|
|
|
|
children = StreamFeatures}),
|
|
|
|
fsm_next_state(wait_for_bind,
|
|
|
|
StateData#state{server = Server, lang = Lang});
|
|
|
|
_ ->
|
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"stream:features">>,
|
|
|
|
attrs = [],
|
|
|
|
children = []}),
|
2016-01-18 14:33:37 +01:00
|
|
|
fsm_next_state(session_established,
|
2015-10-07 00:06:58 +02:00
|
|
|
StateData#state{server = Server, lang = Lang})
|
2005-04-17 20:08:34 +02:00
|
|
|
end
|
|
|
|
end;
|
2015-10-07 00:06:58 +02:00
|
|
|
_ ->
|
|
|
|
send_header(StateData, Server, <<"">>, DefaultLang),
|
|
|
|
if not StateData#state.tls_enabled and
|
|
|
|
StateData#state.tls_required ->
|
|
|
|
send_element(StateData,
|
2013-03-14 10:33:02 +01:00
|
|
|
?POLICY_VIOLATION_ERR(Lang,
|
2015-10-07 00:06:58 +02:00
|
|
|
<<"Use of STARTTLS required">>)),
|
|
|
|
send_trailer(StateData),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
true ->
|
|
|
|
fsm_next_state(wait_for_auth,
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state{server = Server,
|
2015-10-07 00:06:58 +02:00
|
|
|
lang = Lang})
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
IP = StateData#state.ip,
|
|
|
|
{true, LogReason, ReasonT} = IsBlacklistedIP,
|
|
|
|
?INFO_MSG("Connection attempt from blacklisted IP ~s: ~s",
|
|
|
|
[jlib:ip_to_list(IP), LogReason]),
|
|
|
|
send_header(StateData, Server, <<"">>, DefaultLang),
|
|
|
|
send_element(StateData, ?POLICY_VIOLATION_ERR(Lang, ReasonT)),
|
|
|
|
send_trailer(StateData),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
_ ->
|
|
|
|
send_header(StateData, ?MYNAME, <<"">>, DefaultLang),
|
|
|
|
send_element(StateData, ?HOST_UNKNOWN_ERR),
|
|
|
|
send_trailer(StateData),
|
|
|
|
{stop, normal, StateData}
|
2003-03-09 21:46:47 +01:00
|
|
|
end;
|
2002-11-18 21:39:47 +01:00
|
|
|
_ ->
|
2013-03-14 10:33:02 +01:00
|
|
|
send_header(StateData, ?MYNAME, <<"">>, DefaultLang),
|
2015-10-07 00:06:58 +02:00
|
|
|
send_element(StateData, ?INVALID_NS_ERR),
|
2009-08-31 20:37:52 +02:00
|
|
|
send_trailer(StateData),
|
2002-11-19 21:58:49 +01:00
|
|
|
{stop, normal, StateData}
|
2002-11-18 21:39:47 +01:00
|
|
|
end;
|
2007-09-14 16:17:42 +02:00
|
|
|
wait_for_stream(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
2006-12-08 21:34:34 +01:00
|
|
|
wait_for_stream({xmlstreamelement, _}, StateData) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
send_element(StateData, ?INVALID_XML_ERR),
|
|
|
|
send_trailer(StateData),
|
2006-12-08 21:34:34 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
wait_for_stream({xmlstreamend, _}, StateData) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
send_element(StateData, ?INVALID_XML_ERR),
|
|
|
|
send_trailer(StateData),
|
2006-12-08 21:34:34 +01:00
|
|
|
{stop, normal, StateData};
|
2003-03-02 21:55:16 +01:00
|
|
|
wait_for_stream({xmlstreamerror, _}, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
send_header(StateData, ?MYNAME, <<"1.0">>, <<"">>),
|
2009-08-31 20:37:52 +02:00
|
|
|
send_element(StateData, ?INVALID_XML_ERR),
|
|
|
|
send_trailer(StateData),
|
2003-03-02 21:55:16 +01:00
|
|
|
{stop, normal, StateData};
|
2002-11-18 21:39:47 +01:00
|
|
|
wait_for_stream(closed, StateData) ->
|
2015-10-17 23:15:31 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
wait_for_stream(stop, StateData) ->
|
2002-11-18 21:39:47 +01:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2014-03-12 23:34:14 +01:00
|
|
|
wait_for_auth({xmlstreamelement, #xmlel{name = Name} = El}, StateData)
|
2015-10-07 00:06:58 +02:00
|
|
|
when ?IS_STREAM_MGMT_TAG(Name) ->
|
2014-03-22 20:25:43 +01:00
|
|
|
fsm_next_state(wait_for_auth, dispatch_stream_mgmt(El, StateData));
|
2002-11-18 21:39:47 +01:00
|
|
|
wait_for_auth({xmlstreamelement, El}, StateData) ->
|
2002-11-20 21:19:20 +01:00
|
|
|
case is_auth_packet(El) of
|
2015-10-07 00:06:58 +02:00
|
|
|
{auth, _ID, get, {U, _, _, _}} ->
|
|
|
|
#xmlel{name = Name, attrs = Attrs} = jlib:make_result_iq_reply(El),
|
|
|
|
case U of
|
|
|
|
<<"">> -> UCdata = [];
|
|
|
|
_ -> UCdata = [{xmlcdata, U}]
|
|
|
|
end,
|
|
|
|
Res = case
|
|
|
|
ejabberd_auth:plain_password_required(StateData#state.server)
|
|
|
|
of
|
|
|
|
false ->
|
|
|
|
#xmlel{name = Name, attrs = Attrs,
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_AUTH}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"username">>,
|
|
|
|
attrs = [],
|
|
|
|
children = UCdata},
|
|
|
|
#xmlel{name = <<"password">>,
|
|
|
|
attrs = [], children = []},
|
|
|
|
#xmlel{name = <<"digest">>,
|
|
|
|
attrs = [], children = []},
|
|
|
|
#xmlel{name = <<"resource">>,
|
|
|
|
attrs = [],
|
|
|
|
children = []}]}]};
|
|
|
|
true ->
|
|
|
|
#xmlel{name = Name, attrs = Attrs,
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_AUTH}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"username">>,
|
|
|
|
attrs = [],
|
|
|
|
children = UCdata},
|
|
|
|
#xmlel{name = <<"password">>,
|
|
|
|
attrs = [], children = []},
|
|
|
|
#xmlel{name = <<"resource">>,
|
|
|
|
attrs = [],
|
|
|
|
children = []}]}]}
|
|
|
|
end,
|
|
|
|
send_element(StateData, Res),
|
|
|
|
fsm_next_state(wait_for_auth, StateData);
|
|
|
|
{auth, _ID, set, {_U, _P, _D, <<"">>}} ->
|
|
|
|
Err = jlib:make_error_reply(El,
|
|
|
|
?ERR_AUTH_NO_RESOURCE_PROVIDED((StateData#state.lang))),
|
|
|
|
send_element(StateData, Err),
|
|
|
|
fsm_next_state(wait_for_auth, StateData);
|
|
|
|
{auth, _ID, set, {U, P, D, R}} ->
|
2015-11-24 16:44:13 +01:00
|
|
|
JID = jid:make(U, StateData#state.server, R),
|
2015-10-07 00:06:58 +02:00
|
|
|
case JID /= error andalso
|
|
|
|
acl:match_rule(StateData#state.server,
|
|
|
|
StateData#state.access, JID)
|
|
|
|
== allow
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
DGen = fun (PW) ->
|
|
|
|
p1_sha:sha(<<(StateData#state.streamid)/binary, PW/binary>>)
|
|
|
|
end,
|
|
|
|
case ejabberd_auth:check_password_with_authmodule(U,
|
|
|
|
StateData#state.server,
|
|
|
|
P, D, DGen)
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
2015-10-07 00:06:58 +02:00
|
|
|
{true, AuthModule} ->
|
|
|
|
?INFO_MSG("(~w) Accepted legacy authentication for ~s by ~p from ~s",
|
|
|
|
[StateData#state.socket,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(JID), AuthModule,
|
2015-10-07 00:06:58 +02:00
|
|
|
ejabberd_config:may_hide_data(jlib:ip_to_list(StateData#state.ip))]),
|
|
|
|
ejabberd_hooks:run(c2s_auth_result, StateData#state.server,
|
|
|
|
[true, U, StateData#state.server,
|
|
|
|
StateData#state.ip]),
|
|
|
|
Conn = get_conn_type(StateData),
|
|
|
|
Info = [{ip, StateData#state.ip}, {conn, Conn},
|
2008-04-22 19:41:30 +02:00
|
|
|
{auth_module, AuthModule}],
|
2015-10-07 00:06:58 +02:00
|
|
|
Res = jlib:make_result_iq_reply(
|
|
|
|
El#xmlel{children = []}),
|
|
|
|
send_element(StateData, Res),
|
|
|
|
ejabberd_sm:open_session(StateData#state.sid, U,
|
|
|
|
StateData#state.server, R,
|
|
|
|
Info),
|
|
|
|
change_shaper(StateData, JID),
|
|
|
|
{Fs, Ts} =
|
|
|
|
ejabberd_hooks:run_fold(roster_get_subscription_lists,
|
|
|
|
StateData#state.server,
|
|
|
|
{[], []},
|
|
|
|
[U, StateData#state.server]),
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(jid:remove_resource(JID)),
|
2015-10-07 00:06:58 +02:00
|
|
|
Fs1 = [LJID | Fs],
|
|
|
|
Ts1 = [LJID | Ts],
|
|
|
|
PrivList = ejabberd_hooks:run_fold(privacy_get_user_list,
|
|
|
|
StateData#state.server,
|
|
|
|
#userlist{},
|
|
|
|
[U, StateData#state.server]),
|
|
|
|
NewStateData = StateData#state{user = U,
|
|
|
|
resource = R,
|
|
|
|
jid = JID,
|
|
|
|
conn = Conn,
|
|
|
|
auth_module = AuthModule,
|
|
|
|
pres_f = (?SETS):from_list(Fs1),
|
|
|
|
pres_t = (?SETS):from_list(Ts1),
|
|
|
|
privacy_list = PrivList},
|
|
|
|
fsm_next_state(session_established, NewStateData);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("(~w) Failed legacy authentication for ~s from ~s",
|
|
|
|
[StateData#state.socket,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(JID),
|
2015-10-07 00:06:58 +02:00
|
|
|
ejabberd_config:may_hide_data(jlib:ip_to_list(StateData#state.ip))]),
|
|
|
|
ejabberd_hooks:run(c2s_auth_result, StateData#state.server,
|
|
|
|
[false, U, StateData#state.server,
|
|
|
|
StateData#state.ip]),
|
|
|
|
Err = jlib:make_error_reply(El, ?ERR_NOT_AUTHORIZED),
|
|
|
|
send_element(StateData, Err),
|
|
|
|
fsm_next_state(wait_for_auth, StateData)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
if JID == error ->
|
|
|
|
?INFO_MSG("(~w) Forbidden legacy authentication "
|
|
|
|
"for username '~s' with resource '~s'",
|
|
|
|
[StateData#state.socket, U, R]),
|
|
|
|
Err = jlib:make_error_reply(El, ?ERR_JID_MALFORMED),
|
|
|
|
send_element(StateData, Err),
|
|
|
|
fsm_next_state(wait_for_auth, StateData);
|
|
|
|
true ->
|
|
|
|
?INFO_MSG("(~w) Forbidden legacy authentication "
|
|
|
|
"for ~s from ~s",
|
|
|
|
[StateData#state.socket,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(JID),
|
2015-10-07 00:06:58 +02:00
|
|
|
ejabberd_config:may_hide_data(jlib:ip_to_list(StateData#state.ip))]),
|
|
|
|
ejabberd_hooks:run(c2s_auth_result, StateData#state.server,
|
|
|
|
[false, U, StateData#state.server,
|
|
|
|
StateData#state.ip]),
|
|
|
|
Err = jlib:make_error_reply(El, ?ERR_NOT_ALLOWED),
|
|
|
|
send_element(StateData, Err),
|
|
|
|
fsm_next_state(wait_for_auth, StateData)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
process_unauthenticated_stanza(StateData, El),
|
|
|
|
fsm_next_state(wait_for_auth, StateData)
|
2002-11-27 21:46:29 +01:00
|
|
|
end;
|
2007-09-14 16:17:42 +02:00
|
|
|
wait_for_auth(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
2003-12-17 21:13:21 +01:00
|
|
|
wait_for_auth({xmlstreamend, _Name}, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
send_trailer(StateData), {stop, normal, StateData};
|
2003-03-02 21:55:16 +01:00
|
|
|
wait_for_auth({xmlstreamerror, _}, StateData) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
send_element(StateData, ?INVALID_XML_ERR),
|
|
|
|
send_trailer(StateData),
|
2002-11-18 21:39:47 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
wait_for_auth(closed, StateData) ->
|
2015-10-17 23:15:31 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
wait_for_auth(stop, StateData) ->
|
2002-11-18 21:39:47 +01:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
wait_for_feature_request({xmlstreamelement, #xmlel{name = Name} = El},
|
|
|
|
StateData)
|
2014-03-12 23:34:14 +01:00
|
|
|
when ?IS_STREAM_MGMT_TAG(Name) ->
|
2014-05-06 07:27:10 +02:00
|
|
|
fsm_next_state(wait_for_feature_request,
|
|
|
|
dispatch_stream_mgmt(El, StateData));
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_for_feature_request({xmlstreamelement, El},
|
|
|
|
StateData) ->
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = Els} = El,
|
2006-01-19 03:17:31 +01:00
|
|
|
Zlib = StateData#state.zlib,
|
2004-08-01 22:12:20 +02:00
|
|
|
TLS = StateData#state.tls,
|
|
|
|
TLSEnabled = StateData#state.tls_enabled,
|
2004-12-05 21:54:55 +01:00
|
|
|
TLSRequired = StateData#state.tls_required,
|
2013-03-14 10:33:02 +01:00
|
|
|
SockMod =
|
|
|
|
(StateData#state.sockmod):get_sockmod(StateData#state.socket),
|
|
|
|
case {xml:get_attr_s(<<"xmlns">>, Attrs), Name} of
|
|
|
|
{?NS_SASL, <<"auth">>}
|
2014-10-12 02:08:08 +02:00
|
|
|
when TLSEnabled or not TLSRequired ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Mech = xml:get_attr_s(<<"mechanism">>, Attrs),
|
|
|
|
ClientIn = jlib:decode_base64(xml:get_cdata(Els)),
|
|
|
|
case cyrsasl:server_start(StateData#state.sasl_state,
|
|
|
|
Mech, ClientIn)
|
|
|
|
of
|
|
|
|
{ok, Props} ->
|
|
|
|
(StateData#state.sockmod):reset_stream(StateData#state.socket),
|
|
|
|
%U = xml:get_attr_s(username, Props),
|
|
|
|
U = proplists:get_value(username, Props, <<>>),
|
|
|
|
%AuthModule = xml:get_attr_s(auth_module, Props),
|
|
|
|
AuthModule = proplists:get_value(auth_module, Props, undefined),
|
|
|
|
?INFO_MSG("(~w) Accepted authentication for ~s "
|
2014-08-15 11:40:04 +02:00
|
|
|
"by ~p from ~s",
|
|
|
|
[StateData#state.socket, U, AuthModule,
|
2015-06-17 11:32:42 +02:00
|
|
|
ejabberd_config:may_hide_data(jlib:ip_to_list(StateData#state.ip))]),
|
2014-08-15 11:40:04 +02:00
|
|
|
ejabberd_hooks:run(c2s_auth_result, StateData#state.server,
|
|
|
|
[true, U, StateData#state.server,
|
|
|
|
StateData#state.ip]),
|
2013-03-14 10:33:02 +01:00
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"success">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_SASL}],
|
|
|
|
children = []}),
|
|
|
|
fsm_next_state(wait_for_stream,
|
|
|
|
StateData#state{streamid = new_id(),
|
|
|
|
authenticated = true,
|
|
|
|
auth_module = AuthModule,
|
2014-01-21 12:40:03 +01:00
|
|
|
sasl_state = undefined,
|
2013-03-14 10:33:02 +01:00
|
|
|
user = U});
|
|
|
|
{continue, ServerOut, NewSASLState} ->
|
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"challenge">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_SASL}],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
jlib:encode_base64(ServerOut)}]}),
|
|
|
|
fsm_next_state(wait_for_sasl_response,
|
|
|
|
StateData#state{sasl_state = NewSASLState});
|
|
|
|
{error, Error, Username} ->
|
2015-10-07 00:06:58 +02:00
|
|
|
?INFO_MSG("(~w) Failed authentication for ~s@~s from ~s",
|
|
|
|
[StateData#state.socket,
|
|
|
|
Username, StateData#state.server,
|
|
|
|
ejabberd_config:may_hide_data(jlib:ip_to_list(StateData#state.ip))]),
|
2014-08-15 11:40:04 +02:00
|
|
|
ejabberd_hooks:run(c2s_auth_result, StateData#state.server,
|
|
|
|
[false, Username, StateData#state.server,
|
|
|
|
StateData#state.ip]),
|
2013-03-14 10:33:02 +01:00
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"failure">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_SASL}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = Error, attrs = [],
|
|
|
|
children = []}]}),
|
2014-05-27 21:07:53 +02:00
|
|
|
fsm_next_state(wait_for_feature_request, StateData);
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, Error} ->
|
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"failure">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_SASL}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = Error, attrs = [],
|
|
|
|
children = []}]}),
|
|
|
|
fsm_next_state(wait_for_feature_request, StateData)
|
|
|
|
end;
|
|
|
|
{?NS_TLS, <<"starttls">>}
|
|
|
|
when TLS == true, TLSEnabled == false,
|
|
|
|
SockMod == gen_tcp ->
|
|
|
|
TLSOpts = case
|
2013-08-12 14:25:05 +02:00
|
|
|
ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
{domain_certfile, StateData#state.server},
|
|
|
|
fun iolist_to_binary/1)
|
|
|
|
of
|
|
|
|
undefined -> StateData#state.tls_options;
|
|
|
|
CertFile ->
|
|
|
|
[{certfile, CertFile} | lists:keydelete(certfile, 1,
|
|
|
|
StateData#state.tls_options)]
|
|
|
|
end,
|
|
|
|
Socket = StateData#state.socket,
|
2014-05-20 12:52:02 +02:00
|
|
|
BProceed = xml:element_to_binary(#xmlel{name = <<"proceed">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_TLS}]}),
|
2013-03-14 10:33:02 +01:00
|
|
|
TLSSocket = (StateData#state.sockmod):starttls(Socket,
|
|
|
|
TLSOpts,
|
2014-05-20 12:52:02 +02:00
|
|
|
BProceed),
|
2013-03-14 10:33:02 +01:00
|
|
|
fsm_next_state(wait_for_stream,
|
|
|
|
StateData#state{socket = TLSSocket,
|
|
|
|
streamid = new_id(),
|
|
|
|
tls_enabled = true});
|
|
|
|
{?NS_COMPRESS, <<"compress">>}
|
|
|
|
when Zlib == true,
|
2013-06-20 10:40:44 +02:00
|
|
|
(SockMod == gen_tcp) or (SockMod == p1_tls) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case xml:get_subtag(El, <<"method">>) of
|
|
|
|
false ->
|
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"failure">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_COMPRESS}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"setup-failed">>,
|
|
|
|
attrs = [], children = []}]}),
|
|
|
|
fsm_next_state(wait_for_feature_request, StateData);
|
|
|
|
Method ->
|
|
|
|
case xml:get_tag_cdata(Method) of
|
|
|
|
<<"zlib">> ->
|
|
|
|
Socket = StateData#state.socket,
|
2014-05-20 12:52:02 +02:00
|
|
|
BCompressed = xml:element_to_binary(#xmlel{name = <<"compressed">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_COMPRESS}]}),
|
2013-03-14 10:33:02 +01:00
|
|
|
ZlibSocket = (StateData#state.sockmod):compress(Socket,
|
2014-05-20 12:52:02 +02:00
|
|
|
BCompressed),
|
2013-03-14 10:33:02 +01:00
|
|
|
fsm_next_state(wait_for_stream,
|
|
|
|
StateData#state{socket = ZlibSocket,
|
|
|
|
streamid = new_id()});
|
|
|
|
_ ->
|
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"failure">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_COMPRESS}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name =
|
|
|
|
<<"unsupported-method">>,
|
|
|
|
attrs = [],
|
|
|
|
children = []}]}),
|
|
|
|
fsm_next_state(wait_for_feature_request, StateData)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ ->
|
2014-10-12 02:08:08 +02:00
|
|
|
if TLSRequired and not TLSEnabled ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Lang = StateData#state.lang,
|
|
|
|
send_element(StateData,
|
|
|
|
?POLICY_VIOLATION_ERR(Lang,
|
|
|
|
<<"Use of STARTTLS required">>)),
|
|
|
|
send_trailer(StateData),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
true ->
|
|
|
|
process_unauthenticated_stanza(StateData, El),
|
|
|
|
fsm_next_state(wait_for_feature_request, StateData)
|
|
|
|
end
|
2003-03-09 21:46:47 +01:00
|
|
|
end;
|
2007-09-14 16:17:42 +02:00
|
|
|
wait_for_feature_request(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_for_feature_request({xmlstreamend, _Name},
|
|
|
|
StateData) ->
|
|
|
|
send_trailer(StateData), {stop, normal, StateData};
|
|
|
|
wait_for_feature_request({xmlstreamerror, _},
|
|
|
|
StateData) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
send_element(StateData, ?INVALID_XML_ERR),
|
|
|
|
send_trailer(StateData),
|
2003-03-09 21:46:47 +01:00
|
|
|
{stop, normal, StateData};
|
2004-08-01 22:12:20 +02:00
|
|
|
wait_for_feature_request(closed, StateData) ->
|
2015-10-17 23:15:31 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
wait_for_feature_request(stop, StateData) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2014-03-12 23:34:14 +01:00
|
|
|
wait_for_sasl_response({xmlstreamelement, #xmlel{name = Name} = El}, StateData)
|
|
|
|
when ?IS_STREAM_MGMT_TAG(Name) ->
|
2014-03-22 20:25:43 +01:00
|
|
|
fsm_next_state(wait_for_sasl_response, dispatch_stream_mgmt(El, StateData));
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_for_sasl_response({xmlstreamelement, El},
|
|
|
|
StateData) ->
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = Els} = El,
|
|
|
|
case {xml:get_attr_s(<<"xmlns">>, Attrs), Name} of
|
|
|
|
{?NS_SASL, <<"response">>} ->
|
|
|
|
ClientIn = jlib:decode_base64(xml:get_cdata(Els)),
|
|
|
|
case cyrsasl:server_step(StateData#state.sasl_state,
|
|
|
|
ClientIn)
|
|
|
|
of
|
|
|
|
{ok, Props} ->
|
|
|
|
catch
|
|
|
|
(StateData#state.sockmod):reset_stream(StateData#state.socket),
|
|
|
|
% U = xml:get_attr_s(username, Props),
|
|
|
|
U = proplists:get_value(username, Props, <<>>),
|
|
|
|
% AuthModule = xml:get_attr_s(auth_module, Props),
|
|
|
|
AuthModule = proplists:get_value(auth_module, Props, <<>>),
|
|
|
|
?INFO_MSG("(~w) Accepted authentication for ~s "
|
2014-08-15 11:40:04 +02:00
|
|
|
"by ~p from ~s",
|
|
|
|
[StateData#state.socket, U, AuthModule,
|
2015-06-17 11:32:42 +02:00
|
|
|
ejabberd_config:may_hide_data(jlib:ip_to_list(StateData#state.ip))]),
|
2014-08-15 11:40:04 +02:00
|
|
|
ejabberd_hooks:run(c2s_auth_result, StateData#state.server,
|
|
|
|
[true, U, StateData#state.server,
|
|
|
|
StateData#state.ip]),
|
2013-03-14 10:33:02 +01:00
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"success">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_SASL}],
|
|
|
|
children = []}),
|
|
|
|
fsm_next_state(wait_for_stream,
|
|
|
|
StateData#state{streamid = new_id(),
|
|
|
|
authenticated = true,
|
|
|
|
auth_module = AuthModule,
|
2014-01-21 12:40:03 +01:00
|
|
|
sasl_state = undefined,
|
2013-03-14 10:33:02 +01:00
|
|
|
user = U});
|
|
|
|
{ok, Props, ServerOut} ->
|
|
|
|
(StateData#state.sockmod):reset_stream(StateData#state.socket),
|
|
|
|
% U = xml:get_attr_s(username, Props),
|
|
|
|
U = proplists:get_value(username, Props, <<>>),
|
|
|
|
% AuthModule = xml:get_attr_s(auth_module, Props),
|
|
|
|
AuthModule = proplists:get_value(auth_module, Props, undefined),
|
|
|
|
?INFO_MSG("(~w) Accepted authentication for ~s "
|
2014-08-15 11:40:04 +02:00
|
|
|
"by ~p from ~s",
|
|
|
|
[StateData#state.socket, U, AuthModule,
|
2015-06-17 11:32:42 +02:00
|
|
|
ejabberd_config:may_hide_data(jlib:ip_to_list(StateData#state.ip))]),
|
2014-08-15 11:40:04 +02:00
|
|
|
ejabberd_hooks:run(c2s_auth_result, StateData#state.server,
|
|
|
|
[true, U, StateData#state.server,
|
|
|
|
StateData#state.ip]),
|
2013-03-14 10:33:02 +01:00
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"success">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_SASL}],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
jlib:encode_base64(ServerOut)}]}),
|
|
|
|
fsm_next_state(wait_for_stream,
|
|
|
|
StateData#state{streamid = new_id(),
|
|
|
|
authenticated = true,
|
|
|
|
auth_module = AuthModule,
|
2014-01-21 12:40:03 +01:00
|
|
|
sasl_state = undefined,
|
2013-03-14 10:33:02 +01:00
|
|
|
user = U});
|
|
|
|
{continue, ServerOut, NewSASLState} ->
|
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"challenge">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_SASL}],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
jlib:encode_base64(ServerOut)}]}),
|
|
|
|
fsm_next_state(wait_for_sasl_response,
|
|
|
|
StateData#state{sasl_state = NewSASLState});
|
|
|
|
{error, Error, Username} ->
|
2014-08-15 11:40:04 +02:00
|
|
|
?INFO_MSG("(~w) Failed authentication for ~s@~s from ~s",
|
|
|
|
[StateData#state.socket,
|
|
|
|
Username, StateData#state.server,
|
2015-06-17 11:32:42 +02:00
|
|
|
ejabberd_config:may_hide_data(jlib:ip_to_list(StateData#state.ip))]),
|
2014-08-15 11:40:04 +02:00
|
|
|
ejabberd_hooks:run(c2s_auth_result, StateData#state.server,
|
|
|
|
[false, Username, StateData#state.server,
|
|
|
|
StateData#state.ip]),
|
2013-03-14 10:33:02 +01:00
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"failure">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_SASL}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = Error, attrs = [],
|
|
|
|
children = []}]}),
|
|
|
|
fsm_next_state(wait_for_feature_request, StateData);
|
|
|
|
{error, Error} ->
|
|
|
|
send_element(StateData,
|
|
|
|
#xmlel{name = <<"failure">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_SASL}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = Error, attrs = [],
|
|
|
|
children = []}]}),
|
|
|
|
fsm_next_state(wait_for_feature_request, StateData)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
process_unauthenticated_stanza(StateData, El),
|
|
|
|
fsm_next_state(wait_for_feature_request, StateData)
|
2003-03-12 20:48:05 +01:00
|
|
|
end;
|
2007-09-14 16:17:42 +02:00
|
|
|
wait_for_sasl_response(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_for_sasl_response({xmlstreamend, _Name},
|
|
|
|
StateData) ->
|
|
|
|
send_trailer(StateData), {stop, normal, StateData};
|
|
|
|
wait_for_sasl_response({xmlstreamerror, _},
|
|
|
|
StateData) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
send_element(StateData, ?INVALID_XML_ERR),
|
|
|
|
send_trailer(StateData),
|
2003-03-12 20:48:05 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
wait_for_sasl_response(closed, StateData) ->
|
2015-10-17 23:15:31 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
wait_for_sasl_response(stop, StateData) ->
|
2003-03-12 20:48:05 +01:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2011-08-17 19:42:11 +02:00
|
|
|
resource_conflict_action(U, S, R) ->
|
2014-05-20 12:54:49 +02:00
|
|
|
OptionRaw = case ejabberd_sm:is_existing_resource(U, S, R) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2013-08-12 14:25:05 +02:00
|
|
|
ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
{resource_conflict, S},
|
|
|
|
fun(setresource) -> setresource;
|
|
|
|
(closeold) -> closeold;
|
|
|
|
(closenew) -> closenew;
|
|
|
|
(acceptnew) -> acceptnew
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
acceptnew
|
2011-08-17 19:42:11 +02:00
|
|
|
end,
|
|
|
|
Option = case OptionRaw of
|
2013-03-14 10:33:02 +01:00
|
|
|
setresource -> setresource;
|
|
|
|
closeold ->
|
|
|
|
acceptnew; %% ejabberd_sm will close old session
|
|
|
|
closenew -> closenew;
|
|
|
|
acceptnew -> acceptnew;
|
|
|
|
_ -> acceptnew %% default ejabberd behavior
|
2011-08-17 19:42:11 +02:00
|
|
|
end,
|
|
|
|
case Option of
|
2013-03-14 10:33:02 +01:00
|
|
|
acceptnew -> {accept_resource, R};
|
|
|
|
closenew -> closenew;
|
|
|
|
setresource ->
|
2015-12-07 16:08:57 +01:00
|
|
|
Rnew = iolist_to_binary([randoms:get_string(),randoms:get_string(),randoms:get_string()]),
|
2013-03-14 10:33:02 +01:00
|
|
|
{accept_resource, Rnew}
|
2011-08-17 19:42:11 +02:00
|
|
|
end.
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2014-03-19 00:51:33 +01:00
|
|
|
wait_for_bind({xmlstreamelement, #xmlel{name = Name, attrs = Attrs} = El},
|
|
|
|
StateData)
|
2014-03-12 23:34:14 +01:00
|
|
|
when ?IS_STREAM_MGMT_TAG(Name) ->
|
2014-03-19 00:51:33 +01:00
|
|
|
case Name of
|
|
|
|
<<"resume">> ->
|
|
|
|
case handle_resume(StateData, Attrs) of
|
|
|
|
{ok, ResumedState} ->
|
|
|
|
fsm_next_state(session_established, ResumedState);
|
|
|
|
error ->
|
|
|
|
fsm_next_state(wait_for_bind, StateData)
|
|
|
|
end;
|
|
|
|
_ ->
|
2014-03-22 20:25:43 +01:00
|
|
|
fsm_next_state(wait_for_bind, dispatch_stream_mgmt(El, StateData))
|
2014-03-19 00:51:33 +01:00
|
|
|
end;
|
2003-11-07 21:51:23 +01:00
|
|
|
wait_for_bind({xmlstreamelement, El}, StateData) ->
|
|
|
|
case jlib:iq_query_info(El) of
|
2013-03-14 10:33:02 +01:00
|
|
|
#iq{type = set, xmlns = ?NS_BIND, sub_el = SubEl} =
|
|
|
|
IQ ->
|
|
|
|
U = StateData#state.user,
|
|
|
|
R1 = xml:get_path_s(SubEl,
|
|
|
|
[{elem, <<"resource">>}, cdata]),
|
2015-11-24 16:44:13 +01:00
|
|
|
R = case jid:resourceprep(R1) of
|
2013-03-14 10:33:02 +01:00
|
|
|
error -> error;
|
|
|
|
<<"">> ->
|
2015-12-07 16:08:57 +01:00
|
|
|
iolist_to_binary([randoms:get_string(),randoms:get_string(),randoms:get_string()]);
|
2013-03-14 10:33:02 +01:00
|
|
|
Resource -> Resource
|
|
|
|
end,
|
|
|
|
case R of
|
|
|
|
error ->
|
|
|
|
Err = jlib:make_error_reply(El, ?ERR_BAD_REQUEST),
|
|
|
|
send_element(StateData, Err),
|
|
|
|
fsm_next_state(wait_for_bind, StateData);
|
|
|
|
_ ->
|
|
|
|
case resource_conflict_action(U, StateData#state.server,
|
|
|
|
R)
|
|
|
|
of
|
|
|
|
closenew ->
|
|
|
|
Err = jlib:make_error_reply(El,
|
|
|
|
?STANZA_ERROR(<<"409">>,
|
|
|
|
<<"modify">>,
|
|
|
|
<<"conflict">>)),
|
|
|
|
send_element(StateData, Err),
|
|
|
|
fsm_next_state(wait_for_bind, StateData);
|
|
|
|
{accept_resource, R2} ->
|
2016-01-18 14:33:37 +01:00
|
|
|
JID = jid:make(U, StateData#state.server, R2),
|
|
|
|
StateData2 =
|
|
|
|
StateData#state{resource = R2, jid = JID},
|
|
|
|
case open_session(StateData2) of
|
|
|
|
{ok, StateData3} ->
|
|
|
|
Res =
|
|
|
|
IQ#iq{
|
|
|
|
type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"bind">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_BIND}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"jid">>,
|
|
|
|
attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
jid:to_string(JID)}]}]}]},
|
|
|
|
send_element(StateData3, jlib:iq_to_xml(Res)),
|
|
|
|
fsm_next_state_pack(
|
|
|
|
session_established,
|
|
|
|
StateData3);
|
|
|
|
{error, Error} ->
|
|
|
|
Err = jlib:make_error_reply(El, Error),
|
|
|
|
send_element(StateData, Err),
|
|
|
|
fsm_next_state(wait_for_bind, StateData)
|
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ -> fsm_next_state(wait_for_bind, StateData)
|
2003-11-07 21:51:23 +01:00
|
|
|
end;
|
2007-09-14 16:17:42 +02:00
|
|
|
wait_for_bind(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
2003-12-17 21:13:21 +01:00
|
|
|
wait_for_bind({xmlstreamend, _Name}, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
send_trailer(StateData), {stop, normal, StateData};
|
2003-11-07 21:51:23 +01:00
|
|
|
wait_for_bind({xmlstreamerror, _}, StateData) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
send_element(StateData, ?INVALID_XML_ERR),
|
|
|
|
send_trailer(StateData),
|
2003-11-07 21:51:23 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
wait_for_bind(closed, StateData) ->
|
2015-10-17 23:15:31 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
wait_for_bind(stop, StateData) ->
|
2003-11-07 21:51:23 +01:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2016-01-18 14:33:37 +01:00
|
|
|
open_session(StateData) ->
|
|
|
|
U = StateData#state.user,
|
|
|
|
R = StateData#state.resource,
|
|
|
|
JID = StateData#state.jid,
|
|
|
|
case acl:match_rule(StateData#state.server,
|
|
|
|
StateData#state.access, JID) of
|
|
|
|
allow ->
|
|
|
|
?INFO_MSG("(~w) Opened session for ~s",
|
|
|
|
[StateData#state.socket, jid:to_string(JID)]),
|
|
|
|
change_shaper(StateData, JID),
|
|
|
|
{Fs, Ts} = ejabberd_hooks:run_fold(
|
|
|
|
roster_get_subscription_lists,
|
|
|
|
StateData#state.server,
|
|
|
|
{[], []},
|
|
|
|
[U, StateData#state.server]),
|
|
|
|
LJID = jid:tolower(jid:remove_resource(JID)),
|
|
|
|
Fs1 = [LJID | Fs],
|
|
|
|
Ts1 = [LJID | Ts],
|
|
|
|
PrivList =
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
privacy_get_user_list,
|
|
|
|
StateData#state.server,
|
|
|
|
#userlist{},
|
|
|
|
[U, StateData#state.server]),
|
|
|
|
Conn = get_conn_type(StateData),
|
|
|
|
Info = [{ip, StateData#state.ip}, {conn, Conn},
|
|
|
|
{auth_module, StateData#state.auth_module}],
|
|
|
|
ejabberd_sm:open_session(
|
|
|
|
StateData#state.sid, U, StateData#state.server, R, Info),
|
|
|
|
UpdatedStateData =
|
|
|
|
StateData#state{
|
|
|
|
conn = Conn,
|
|
|
|
pres_f = ?SETS:from_list(Fs1),
|
|
|
|
pres_t = ?SETS:from_list(Ts1),
|
|
|
|
privacy_list = PrivList},
|
|
|
|
{ok, UpdatedStateData};
|
|
|
|
_ ->
|
|
|
|
ejabberd_hooks:run(forbidden_session_hook,
|
|
|
|
StateData#state.server, [JID]),
|
|
|
|
?INFO_MSG("(~w) Forbidden session for ~s",
|
|
|
|
[StateData#state.socket, jid:to_string(JID)]),
|
|
|
|
{error, ?ERR_NOT_ALLOWED}
|
|
|
|
end.
|
2003-06-07 19:30:25 +02:00
|
|
|
|
2014-03-12 23:34:14 +01:00
|
|
|
session_established({xmlstreamelement, #xmlel{name = Name} = El}, StateData)
|
|
|
|
when ?IS_STREAM_MGMT_TAG(Name) ->
|
2014-03-22 20:25:43 +01:00
|
|
|
fsm_next_state(session_established, dispatch_stream_mgmt(El, StateData));
|
2014-09-11 17:44:29 +02:00
|
|
|
session_established({xmlstreamelement,
|
|
|
|
#xmlel{name = <<"active">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_CLIENT_STATE}]}},
|
|
|
|
StateData) ->
|
|
|
|
NewStateData = csi_queue_flush(StateData),
|
|
|
|
fsm_next_state(session_established, NewStateData#state{csi_state = active});
|
|
|
|
session_established({xmlstreamelement,
|
|
|
|
#xmlel{name = <<"inactive">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_CLIENT_STATE}]}},
|
|
|
|
StateData) ->
|
|
|
|
fsm_next_state(session_established, StateData#state{csi_state = inactive});
|
2013-03-14 10:33:02 +01:00
|
|
|
session_established({xmlstreamelement, El},
|
|
|
|
StateData) ->
|
2008-12-08 10:21:36 +01:00
|
|
|
FromJID = StateData#state.jid,
|
|
|
|
case check_from(El, FromJID) of
|
|
|
|
'invalid-from' ->
|
2009-08-31 20:37:52 +02:00
|
|
|
send_element(StateData, ?INVALID_FROM),
|
|
|
|
send_trailer(StateData),
|
2008-12-08 10:21:36 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
_NewEl ->
|
|
|
|
session_established2(El, StateData)
|
|
|
|
end;
|
|
|
|
%% We hibernate the process to reduce memory consumption after a
|
|
|
|
%% configurable activity timeout
|
|
|
|
session_established(timeout, StateData) ->
|
|
|
|
Options = [],
|
2010-01-28 14:00:04 +01:00
|
|
|
proc_lib:hibernate(?GEN_FSM, enter_loop,
|
2008-12-08 10:21:36 +01:00
|
|
|
[?MODULE, Options, session_established, StateData]),
|
|
|
|
fsm_next_state(session_established, StateData);
|
|
|
|
session_established({xmlstreamend, _Name}, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
send_trailer(StateData), {stop, normal, StateData};
|
|
|
|
session_established({xmlstreamerror,
|
|
|
|
<<"XML stanza is too big">> = E},
|
|
|
|
StateData) ->
|
|
|
|
send_element(StateData,
|
|
|
|
?POLICY_VIOLATION_ERR((StateData#state.lang), E)),
|
2009-08-31 20:37:52 +02:00
|
|
|
send_trailer(StateData),
|
2008-12-08 10:21:36 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
session_established({xmlstreamerror, _}, StateData) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
send_element(StateData, ?INVALID_XML_ERR),
|
|
|
|
send_trailer(StateData),
|
2008-12-08 10:21:36 +01:00
|
|
|
{stop, normal, StateData};
|
2014-09-06 20:29:02 +02:00
|
|
|
session_established(closed, #state{mgmt_state = active} = StateData) ->
|
2015-10-17 22:07:29 +02:00
|
|
|
catch (StateData#state.sockmod):close(StateData#state.socket),
|
2014-05-27 21:14:49 +02:00
|
|
|
fsm_next_state(wait_for_resume, StateData);
|
2008-12-08 10:21:36 +01:00
|
|
|
session_established(closed, StateData) ->
|
2015-10-17 23:15:31 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
session_established(stop, StateData) ->
|
2008-12-08 10:21:36 +01:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
%% Process packets sent by user (coming from user on c2s XMPP connection)
|
2008-12-08 10:21:36 +01:00
|
|
|
session_established2(El, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = Name, attrs = Attrs} = El,
|
2014-03-12 23:34:14 +01:00
|
|
|
NewStateData = update_num_stanzas_in(StateData, El),
|
|
|
|
User = NewStateData#state.user,
|
|
|
|
Server = NewStateData#state.server,
|
|
|
|
FromJID = NewStateData#state.jid,
|
2013-03-14 10:33:02 +01:00
|
|
|
To = xml:get_attr_s(<<"to">>, Attrs),
|
2002-11-30 19:46:16 +01:00
|
|
|
ToJID = case To of
|
2015-11-24 16:44:13 +01:00
|
|
|
<<"">> -> jid:make(User, Server, <<"">>);
|
|
|
|
_ -> jid:from_string(To)
|
2002-11-30 19:46:16 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
NewEl1 = jlib:remove_attr(<<"xmlns">>, El),
|
|
|
|
NewEl = case xml:get_attr_s(<<"xml:lang">>, Attrs) of
|
|
|
|
<<"">> ->
|
2014-03-12 23:34:14 +01:00
|
|
|
case NewStateData#state.lang of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> NewEl1;
|
|
|
|
Lang ->
|
|
|
|
xml:replace_tag_attr(<<"xml:lang">>, Lang, NewEl1)
|
|
|
|
end;
|
|
|
|
_ -> NewEl1
|
2004-01-18 21:42:09 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
NewState = case ToJID of
|
|
|
|
error ->
|
|
|
|
case xml:get_attr_s(<<"type">>, Attrs) of
|
2014-03-12 23:34:14 +01:00
|
|
|
<<"error">> -> NewStateData;
|
|
|
|
<<"result">> -> NewStateData;
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(NewEl,
|
|
|
|
?ERR_JID_MALFORMED),
|
2014-05-12 19:20:25 +02:00
|
|
|
send_packet(NewStateData, Err)
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
case Name of
|
|
|
|
<<"presence">> ->
|
2015-06-22 15:56:08 +02:00
|
|
|
PresenceEl0 =
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_update_presence,
|
|
|
|
Server, NewEl,
|
|
|
|
[User, Server]),
|
2015-06-22 15:56:08 +02:00
|
|
|
PresenceEl =
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
user_send_packet, Server, PresenceEl0,
|
|
|
|
[NewStateData, FromJID, ToJID]),
|
2013-03-14 10:33:02 +01:00
|
|
|
case ToJID of
|
|
|
|
#jid{user = User, server = Server,
|
|
|
|
resource = <<"">>} ->
|
|
|
|
?DEBUG("presence_update(~p,~n\t~p,~n\t~p)",
|
2014-03-12 23:34:14 +01:00
|
|
|
[FromJID, PresenceEl, NewStateData]),
|
2013-03-14 10:33:02 +01:00
|
|
|
presence_update(FromJID, PresenceEl,
|
2014-03-12 23:34:14 +01:00
|
|
|
NewStateData);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
presence_track(FromJID, ToJID, PresenceEl,
|
2014-03-12 23:34:14 +01:00
|
|
|
NewStateData)
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
<<"iq">> ->
|
|
|
|
case jlib:iq_query_info(NewEl) of
|
|
|
|
#iq{xmlns = Xmlns} = IQ
|
|
|
|
when Xmlns == (?NS_PRIVACY);
|
|
|
|
Xmlns == (?NS_BLOCKING) ->
|
|
|
|
process_privacy_iq(FromJID, ToJID, IQ,
|
2014-03-12 23:34:14 +01:00
|
|
|
NewStateData);
|
2016-01-18 14:33:37 +01:00
|
|
|
#iq{xmlns = ?NS_SESSION} ->
|
|
|
|
Res = jlib:make_result_iq_reply(
|
|
|
|
NewEl#xmlel{children = []}),
|
|
|
|
send_stanza(NewStateData, Res);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
2015-06-22 15:56:08 +02:00
|
|
|
NewEl0 = ejabberd_hooks:run_fold(
|
|
|
|
user_send_packet, Server, NewEl,
|
|
|
|
[NewStateData, FromJID, ToJID]),
|
2014-03-12 23:34:14 +01:00
|
|
|
check_privacy_route(FromJID, NewStateData,
|
2015-06-22 15:56:08 +02:00
|
|
|
FromJID, ToJID, NewEl0)
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
<<"message">> ->
|
2015-06-22 15:56:08 +02:00
|
|
|
NewEl0 = ejabberd_hooks:run_fold(
|
|
|
|
user_send_packet, Server, NewEl,
|
|
|
|
[NewStateData, FromJID, ToJID]),
|
2014-03-12 23:34:14 +01:00
|
|
|
check_privacy_route(FromJID, NewStateData, FromJID,
|
2015-06-22 15:56:08 +02:00
|
|
|
ToJID, NewEl0);
|
2014-03-12 23:34:14 +01:00
|
|
|
_ -> NewStateData
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
ejabberd_hooks:run(c2s_loop_debug,
|
|
|
|
[{xmlstreamelement, El}]),
|
2008-12-08 10:21:36 +01:00
|
|
|
fsm_next_state(session_established, NewState).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2014-05-05 00:02:55 +02:00
|
|
|
wait_for_resume({xmlstreamelement, _El} = Event, StateData) ->
|
2015-12-02 19:25:10 +01:00
|
|
|
Result = session_established(Event, StateData),
|
|
|
|
fsm_next_state(wait_for_resume, element(3, Result));
|
2014-03-19 00:51:33 +01:00
|
|
|
wait_for_resume(timeout, StateData) ->
|
|
|
|
?DEBUG("Timed out waiting for resumption of stream for ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData#state.jid)]),
|
2014-03-19 00:51:33 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
wait_for_resume(Event, StateData) ->
|
2014-05-05 00:02:55 +02:00
|
|
|
?DEBUG("Ignoring event while waiting for resumption: ~p", [Event]),
|
2014-03-19 00:51:33 +01:00
|
|
|
fsm_next_state(wait_for_resume, StateData).
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
handle_event(_Event, StateName, StateData) ->
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(StateName, StateData).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event({get_presence}, _From, StateName,
|
|
|
|
StateData) ->
|
2004-03-02 22:16:55 +01:00
|
|
|
User = StateData#state.user,
|
2004-03-10 22:54:54 +01:00
|
|
|
PresLast = StateData#state.pres_last,
|
|
|
|
Show = get_showtag(PresLast),
|
|
|
|
Status = get_statustag(PresLast),
|
2004-07-25 23:27:56 +02:00
|
|
|
Resource = StateData#state.resource,
|
|
|
|
Reply = {User, Resource, Show, Status},
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_reply(Reply, StateName, StateData);
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(get_subscribed, _From, StateName,
|
|
|
|
StateData) ->
|
|
|
|
Subscribed = (?SETS):to_list(StateData#state.pres_f),
|
2008-12-08 15:10:55 +01:00
|
|
|
{reply, Subscribed, StateName, StateData};
|
2014-05-23 23:38:04 +02:00
|
|
|
handle_sync_event({resume_session, Time}, _From, _StateName,
|
|
|
|
StateData) when element(1, StateData#state.sid) == Time ->
|
2014-03-19 00:51:33 +01:00
|
|
|
%% The old session should be closed before the new one is opened, so we do
|
|
|
|
%% this here instead of leaving it to the terminate callback
|
|
|
|
ejabberd_sm:close_session(StateData#state.sid,
|
|
|
|
StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.resource),
|
2014-05-06 07:27:10 +02:00
|
|
|
{stop, normal, {ok, StateData}, StateData#state{mgmt_state = resumed}};
|
2014-05-23 23:38:04 +02:00
|
|
|
handle_sync_event({resume_session, _Time}, _From, StateName,
|
|
|
|
StateData) ->
|
|
|
|
{reply, {error, <<"Previous session not found">>}, StateName, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName,
|
|
|
|
StateData) ->
|
|
|
|
Reply = ok, fsm_reply(Reply, StateName, StateData).
|
2004-03-10 22:54:54 +01:00
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
{ok, StateName, StateData}.
|
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
handle_info({send_text, Text}, StateName, StateData) ->
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData, Text),
|
2007-09-14 16:20:56 +02:00
|
|
|
ejabberd_hooks:run(c2s_loop_debug, [Text]),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(StateName, StateData);
|
2014-03-26 16:01:37 +01:00
|
|
|
handle_info(replaced, StateName, StateData) ->
|
2005-11-22 19:00:56 +01:00
|
|
|
Lang = StateData#state.lang,
|
2014-03-26 16:01:37 +01:00
|
|
|
Xmlelement = ?SERRT_CONFLICT(Lang, <<"Replaced by new connection">>),
|
|
|
|
handle_info({kick, replaced, Xmlelement}, StateName, StateData);
|
2014-05-12 12:44:40 +02:00
|
|
|
handle_info(kick, StateName, StateData) ->
|
2014-04-29 11:56:28 +02:00
|
|
|
Lang = StateData#state.lang,
|
|
|
|
Xmlelement = ?SERRT_POLICY_VIOLATION(Lang, <<"has been kicked">>),
|
|
|
|
handle_info({kick, kicked_by_admin, Xmlelement}, StateName, StateData);
|
2014-03-26 16:01:37 +01:00
|
|
|
handle_info({kick, Reason, Xmlelement}, _StateName, StateData) ->
|
|
|
|
send_element(StateData, Xmlelement),
|
2009-08-31 20:37:52 +02:00
|
|
|
send_trailer(StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
{stop, normal,
|
2014-03-26 16:01:37 +01:00
|
|
|
StateData#state{authenticated = Reason}};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({route, _From, _To, {broadcast, Data}},
|
|
|
|
StateName, StateData) ->
|
|
|
|
?DEBUG("broadcast~n~p~n", [Data]),
|
|
|
|
case Data of
|
|
|
|
{item, IJID, ISubscription} ->
|
|
|
|
fsm_next_state(StateName,
|
|
|
|
roster_change(IJID, ISubscription, StateData));
|
|
|
|
{exit, Reason} ->
|
|
|
|
Lang = StateData#state.lang,
|
|
|
|
send_element(StateData, ?SERRT_CONFLICT(Lang, Reason)),
|
|
|
|
catch send_trailer(StateData),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
{privacy_list, PrivList, PrivListName} ->
|
|
|
|
case ejabberd_hooks:run_fold(privacy_updated_list,
|
|
|
|
StateData#state.server,
|
|
|
|
false,
|
|
|
|
[StateData#state.privacy_list,
|
|
|
|
PrivList]) of
|
|
|
|
false ->
|
|
|
|
fsm_next_state(StateName, StateData);
|
|
|
|
NewPL ->
|
|
|
|
PrivPushIQ = #iq{type = set,
|
|
|
|
id = <<"push",
|
|
|
|
(randoms:get_string())/binary>>,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs = [{<<"xmlns">>,
|
|
|
|
?NS_PRIVACY}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"list">>,
|
|
|
|
attrs = [{<<"name">>,
|
|
|
|
PrivListName}],
|
|
|
|
children = []}]}]},
|
|
|
|
PrivPushEl = jlib:replace_from_to(
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:remove_resource(StateData#state.jid),
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.jid,
|
|
|
|
jlib:iq_to_xml(PrivPushIQ)),
|
2015-10-07 00:06:58 +02:00
|
|
|
NewState = send_stanza(
|
|
|
|
StateData, PrivPushEl),
|
2013-03-14 10:33:02 +01:00
|
|
|
fsm_next_state(StateName,
|
2014-03-12 23:34:14 +01:00
|
|
|
NewState#state{privacy_list = NewPL})
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
{blocking, What} ->
|
2014-03-12 23:34:14 +01:00
|
|
|
NewState = route_blocking(What, StateData),
|
|
|
|
fsm_next_state(StateName, NewState);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
fsm_next_state(StateName, StateData)
|
|
|
|
end;
|
2009-01-17 17:33:35 +01:00
|
|
|
%% Process Packets that are to be send to the user
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({route, From, To,
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = Els} = Packet},
|
|
|
|
StateName, StateData) ->
|
|
|
|
{Pass, NewAttrs, NewState} = case Name of
|
|
|
|
<<"presence">> ->
|
|
|
|
State =
|
|
|
|
ejabberd_hooks:run_fold(c2s_presence_in,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData,
|
|
|
|
[{From, To,
|
|
|
|
Packet}]),
|
|
|
|
case xml:get_attr_s(<<"type">>, Attrs) of
|
|
|
|
<<"probe">> ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LFrom = jid:tolower(From),
|
2013-03-14 10:33:02 +01:00
|
|
|
LBFrom =
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:remove_resource(LFrom),
|
2013-03-14 10:33:02 +01:00
|
|
|
NewStateData = case
|
|
|
|
(?SETS):is_element(LFrom,
|
|
|
|
State#state.pres_a)
|
|
|
|
orelse
|
|
|
|
(?SETS):is_element(LBFrom,
|
|
|
|
State#state.pres_a)
|
|
|
|
of
|
|
|
|
true -> State;
|
|
|
|
false ->
|
|
|
|
case
|
|
|
|
(?SETS):is_element(LFrom,
|
|
|
|
State#state.pres_f)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
A =
|
|
|
|
(?SETS):add_element(LFrom,
|
|
|
|
State#state.pres_a),
|
|
|
|
State#state{pres_a
|
|
|
|
=
|
|
|
|
A};
|
|
|
|
false ->
|
|
|
|
case
|
|
|
|
(?SETS):is_element(LBFrom,
|
|
|
|
State#state.pres_f)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
A =
|
|
|
|
(?SETS):add_element(LBFrom,
|
|
|
|
State#state.pres_a),
|
|
|
|
State#state{pres_a
|
|
|
|
=
|
|
|
|
A};
|
|
|
|
false ->
|
|
|
|
State
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
process_presence_probe(From, To,
|
|
|
|
NewStateData),
|
|
|
|
{false, Attrs, NewStateData};
|
|
|
|
<<"error">> ->
|
|
|
|
NewA =
|
2015-11-24 16:44:13 +01:00
|
|
|
remove_element(jid:tolower(From),
|
2013-03-14 10:33:02 +01:00
|
|
|
State#state.pres_a),
|
|
|
|
{true, Attrs,
|
|
|
|
State#state{pres_a = NewA}};
|
|
|
|
<<"subscribe">> ->
|
|
|
|
SRes = is_privacy_allow(State,
|
|
|
|
From, To,
|
|
|
|
Packet,
|
|
|
|
in),
|
|
|
|
{SRes, Attrs, State};
|
|
|
|
<<"subscribed">> ->
|
|
|
|
SRes = is_privacy_allow(State,
|
|
|
|
From, To,
|
|
|
|
Packet,
|
|
|
|
in),
|
|
|
|
{SRes, Attrs, State};
|
|
|
|
<<"unsubscribe">> ->
|
|
|
|
SRes = is_privacy_allow(State,
|
|
|
|
From, To,
|
|
|
|
Packet,
|
|
|
|
in),
|
|
|
|
{SRes, Attrs, State};
|
|
|
|
<<"unsubscribed">> ->
|
|
|
|
SRes = is_privacy_allow(State,
|
|
|
|
From, To,
|
|
|
|
Packet,
|
|
|
|
in),
|
|
|
|
{SRes, Attrs, State};
|
|
|
|
_ ->
|
|
|
|
case privacy_check_packet(State,
|
|
|
|
From, To,
|
|
|
|
Packet,
|
|
|
|
in)
|
|
|
|
of
|
|
|
|
allow ->
|
|
|
|
LFrom =
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:tolower(From),
|
2013-03-14 10:33:02 +01:00
|
|
|
LBFrom =
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:remove_resource(LFrom),
|
2013-03-14 10:33:02 +01:00
|
|
|
case
|
|
|
|
(?SETS):is_element(LFrom,
|
|
|
|
State#state.pres_a)
|
|
|
|
orelse
|
|
|
|
(?SETS):is_element(LBFrom,
|
|
|
|
State#state.pres_a)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
{true, Attrs, State};
|
|
|
|
false ->
|
|
|
|
case
|
|
|
|
(?SETS):is_element(LFrom,
|
|
|
|
State#state.pres_f)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
A =
|
|
|
|
(?SETS):add_element(LFrom,
|
|
|
|
State#state.pres_a),
|
|
|
|
{true, Attrs,
|
|
|
|
State#state{pres_a
|
|
|
|
=
|
|
|
|
A}};
|
|
|
|
false ->
|
|
|
|
case
|
|
|
|
(?SETS):is_element(LBFrom,
|
|
|
|
State#state.pres_f)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
A =
|
|
|
|
(?SETS):add_element(LBFrom,
|
|
|
|
State#state.pres_a),
|
|
|
|
{true,
|
|
|
|
Attrs,
|
|
|
|
State#state{pres_a
|
|
|
|
=
|
|
|
|
A}};
|
|
|
|
false ->
|
|
|
|
{true,
|
|
|
|
Attrs,
|
|
|
|
State}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
deny -> {false, Attrs, State}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
<<"iq">> ->
|
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case IQ of
|
|
|
|
#iq{xmlns = ?NS_LAST} ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LFrom = jid:tolower(From),
|
2013-03-14 10:33:02 +01:00
|
|
|
LBFrom =
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:remove_resource(LFrom),
|
2013-03-14 10:33:02 +01:00
|
|
|
HasFromSub =
|
|
|
|
((?SETS):is_element(LFrom,
|
|
|
|
StateData#state.pres_f)
|
|
|
|
orelse
|
|
|
|
(?SETS):is_element(LBFrom,
|
|
|
|
StateData#state.pres_f))
|
|
|
|
andalso
|
|
|
|
is_privacy_allow(StateData,
|
|
|
|
To, From,
|
|
|
|
#xmlel{name
|
|
|
|
=
|
|
|
|
<<"presence">>,
|
|
|
|
attrs
|
|
|
|
=
|
|
|
|
[],
|
|
|
|
children
|
|
|
|
=
|
|
|
|
[]},
|
|
|
|
out),
|
|
|
|
case HasFromSub of
|
|
|
|
true ->
|
|
|
|
case
|
|
|
|
privacy_check_packet(StateData,
|
|
|
|
From,
|
|
|
|
To,
|
|
|
|
Packet,
|
|
|
|
in)
|
|
|
|
of
|
|
|
|
allow ->
|
|
|
|
{true, Attrs,
|
|
|
|
StateData};
|
|
|
|
deny ->
|
|
|
|
{false, Attrs,
|
|
|
|
StateData}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Err =
|
|
|
|
jlib:make_error_reply(Packet,
|
|
|
|
?ERR_FORBIDDEN),
|
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
Err),
|
|
|
|
{false, Attrs, StateData}
|
|
|
|
end;
|
|
|
|
IQ
|
|
|
|
when is_record(IQ, iq) or
|
|
|
|
(IQ == reply) ->
|
|
|
|
case
|
|
|
|
privacy_check_packet(StateData,
|
|
|
|
From, To,
|
|
|
|
Packet, in)
|
|
|
|
of
|
|
|
|
allow ->
|
|
|
|
{true, Attrs, StateData};
|
|
|
|
deny when is_record(IQ, iq) ->
|
|
|
|
Err =
|
|
|
|
jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
Err),
|
|
|
|
{false, Attrs, StateData};
|
|
|
|
deny when IQ == reply ->
|
|
|
|
{false, Attrs, StateData}
|
|
|
|
end;
|
|
|
|
IQ
|
|
|
|
when (IQ == invalid) or
|
|
|
|
(IQ == not_iq) ->
|
|
|
|
{false, Attrs, StateData}
|
|
|
|
end;
|
|
|
|
<<"message">> ->
|
|
|
|
case privacy_check_packet(StateData,
|
|
|
|
From, To,
|
|
|
|
Packet, in)
|
|
|
|
of
|
2015-10-07 00:06:58 +02:00
|
|
|
allow ->
|
|
|
|
{true, Attrs, StateData};
|
2015-03-24 23:10:47 +01:00
|
|
|
deny ->
|
2015-04-20 16:01:56 +02:00
|
|
|
case xml:get_attr_s(<<"type">>, Attrs) of
|
|
|
|
<<"error">> -> ok;
|
2016-01-12 10:20:35 +01:00
|
|
|
<<"groupchat">> -> ok;
|
|
|
|
<<"headline">> -> ok;
|
2015-04-20 16:01:56 +02:00
|
|
|
<<"result">> -> ok;
|
|
|
|
_ ->
|
|
|
|
Err =
|
|
|
|
jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
Err)
|
|
|
|
end,
|
|
|
|
{false, Attrs, StateData}
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
_ -> {true, Attrs, StateData}
|
|
|
|
end,
|
2015-03-27 12:28:11 +01:00
|
|
|
if Pass ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs2 =
|
2015-11-24 16:44:13 +01:00
|
|
|
jlib:replace_from_to_attrs(jid:to_string(From),
|
|
|
|
jid:to_string(To), NewAttrs),
|
2015-06-22 15:56:08 +02:00
|
|
|
FixedPacket0 = #xmlel{name = Name, attrs = Attrs2, children = Els},
|
|
|
|
FixedPacket = ejabberd_hooks:run_fold(
|
2015-10-07 00:06:58 +02:00
|
|
|
user_receive_packet,
|
|
|
|
NewState#state.server,
|
|
|
|
FixedPacket0,
|
|
|
|
[NewState, NewState#state.jid, From, To]),
|
2015-03-25 02:02:08 +01:00
|
|
|
SentStateData = send_packet(NewState, FixedPacket),
|
2007-09-14 16:20:56 +02:00
|
|
|
ejabberd_hooks:run(c2s_loop_debug, [{route, From, To, Packet}]),
|
2015-03-25 02:02:08 +01:00
|
|
|
fsm_next_state(StateName, SentStateData);
|
2003-01-29 18:12:23 +01:00
|
|
|
true ->
|
2007-09-14 16:20:56 +02:00
|
|
|
ejabberd_hooks:run(c2s_loop_debug, [{route, From, To, Packet}]),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(StateName, NewState)
|
2006-01-13 02:55:20 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({'DOWN', Monitor, _Type, _Object, _Info},
|
|
|
|
_StateName, StateData)
|
|
|
|
when Monitor == StateData#state.socket_monitor ->
|
2014-09-06 20:29:02 +02:00
|
|
|
if StateData#state.mgmt_state == active;
|
2014-05-06 07:27:10 +02:00
|
|
|
StateData#state.mgmt_state == pending ->
|
2014-05-27 21:14:49 +02:00
|
|
|
fsm_next_state(wait_for_resume, StateData);
|
2014-03-19 00:51:33 +01:00
|
|
|
true ->
|
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
2010-01-03 01:35:51 +01:00
|
|
|
handle_info(system_shutdown, StateName, StateData) ->
|
|
|
|
case StateName of
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_for_stream ->
|
|
|
|
send_header(StateData, ?MYNAME, <<"1.0">>, <<"en">>),
|
|
|
|
send_element(StateData, ?SERR_SYSTEM_SHUTDOWN),
|
|
|
|
send_trailer(StateData),
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
send_element(StateData, ?SERR_SYSTEM_SHUTDOWN),
|
|
|
|
send_trailer(StateData),
|
|
|
|
ok
|
2010-01-03 01:35:51 +01:00
|
|
|
end,
|
|
|
|
{stop, normal, StateData};
|
2015-03-27 15:55:57 +01:00
|
|
|
handle_info({route_xmlstreamelement, El}, _StateName, StateData) ->
|
2015-03-27 15:03:14 +01:00
|
|
|
{next_state, NStateName, NStateData, _Timeout} =
|
|
|
|
session_established({xmlstreamelement, El}, StateData),
|
|
|
|
fsm_next_state(NStateName, NStateData);
|
2015-08-20 18:15:16 +02:00
|
|
|
handle_info({force_update_presence, LUser, LServer}, StateName,
|
|
|
|
#state{jid = #jid{luser = LUser, lserver = LServer}} = StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
NewStateData = case StateData#state.pres_last of
|
|
|
|
#xmlel{name = <<"presence">>} ->
|
|
|
|
PresenceEl =
|
|
|
|
ejabberd_hooks:run_fold(c2s_update_presence,
|
|
|
|
LServer,
|
|
|
|
StateData#state.pres_last,
|
|
|
|
[LUser, LServer]),
|
|
|
|
StateData2 = StateData#state{pres_last = PresenceEl},
|
|
|
|
presence_update(StateData2#state.jid, PresenceEl,
|
|
|
|
StateData2),
|
|
|
|
StateData2;
|
|
|
|
_ -> StateData
|
|
|
|
end,
|
2014-05-27 21:07:53 +02:00
|
|
|
fsm_next_state(StateName, NewStateData);
|
2014-11-14 01:03:26 +01:00
|
|
|
handle_info({send_filtered, Feature, From, To, Packet}, StateName, StateData) ->
|
|
|
|
Drop = ejabberd_hooks:run_fold(c2s_filter_packet, StateData#state.server,
|
|
|
|
true, [StateData#state.server, StateData,
|
|
|
|
Feature, To, Packet]),
|
|
|
|
NewStateData = if Drop ->
|
|
|
|
?DEBUG("Dropping packet from ~p to ~p",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(From),
|
|
|
|
jid:to_string(To)]),
|
2014-11-14 01:03:26 +01:00
|
|
|
StateData;
|
|
|
|
true ->
|
|
|
|
FinalPacket = jlib:replace_from_to(From, To, Packet),
|
|
|
|
case StateData#state.jid of
|
|
|
|
To ->
|
2014-11-18 01:25:12 +01:00
|
|
|
case privacy_check_packet(StateData, From, To,
|
|
|
|
FinalPacket, in) of
|
|
|
|
deny ->
|
|
|
|
StateData;
|
|
|
|
allow ->
|
|
|
|
send_stanza(StateData, FinalPacket)
|
|
|
|
end;
|
2014-11-14 01:03:26 +01:00
|
|
|
_ ->
|
|
|
|
ejabberd_router:route(From, To, FinalPacket),
|
|
|
|
StateData
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
fsm_next_state(StateName, NewStateData);
|
2010-09-28 06:18:57 +02:00
|
|
|
handle_info({broadcast, Type, From, Packet}, StateName, StateData) ->
|
|
|
|
Recipients = ejabberd_hooks:run_fold(
|
|
|
|
c2s_broadcast_recipients, StateData#state.server,
|
|
|
|
[],
|
2014-07-22 17:42:49 +02:00
|
|
|
[StateData#state.server, StateData, Type, From, Packet]),
|
2010-09-28 06:18:57 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(USR) ->
|
|
|
|
ejabberd_router:route(
|
2015-11-24 16:44:13 +01:00
|
|
|
From, jid:make(USR), Packet)
|
2010-09-28 06:18:57 +02:00
|
|
|
end, lists:usort(Recipients)),
|
|
|
|
fsm_next_state(StateName, StateData);
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_info(Info, StateName, StateData) ->
|
|
|
|
?ERROR_MSG("Unexpected info: ~p", [Info]),
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(StateName, StateData).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2014-09-25 00:08:56 +02:00
|
|
|
print_state(State = #state{pres_t = T, pres_f = F, pres_a = A}) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
State#state{pres_t = {pres_t, (?SETS):size(T)},
|
|
|
|
pres_f = {pres_f, (?SETS):size(F)},
|
|
|
|
pres_a = {pres_a, (?SETS):size(A)}}.
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
terminate(_Reason, StateName, StateData) ->
|
2014-05-06 07:27:10 +02:00
|
|
|
case StateData#state.mgmt_state of
|
2014-03-19 00:51:33 +01:00
|
|
|
resumed ->
|
|
|
|
?INFO_MSG("Closing former stream of resumed session for ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData#state.jid)]);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
2014-03-19 00:51:33 +01:00
|
|
|
if StateName == session_established;
|
|
|
|
StateName == wait_for_resume ->
|
|
|
|
case StateData#state.authenticated of
|
|
|
|
replaced ->
|
|
|
|
?INFO_MSG("(~w) Replaced session for ~s",
|
|
|
|
[StateData#state.socket,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(StateData#state.jid)]),
|
2014-03-19 00:51:33 +01:00
|
|
|
From = StateData#state.jid,
|
|
|
|
Packet = #xmlel{name = <<"presence">>,
|
|
|
|
attrs = [{<<"type">>, <<"unavailable">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"status">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<"Replaced by new connection">>}]}]},
|
|
|
|
ejabberd_sm:close_session_unset_presence(StateData#state.sid,
|
|
|
|
StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.resource,
|
|
|
|
<<"Replaced by new connection">>),
|
|
|
|
presence_broadcast(StateData, From,
|
|
|
|
StateData#state.pres_a, Packet),
|
2014-03-25 23:23:38 +01:00
|
|
|
handle_unacked_stanzas(StateData);
|
2014-03-19 00:51:33 +01:00
|
|
|
_ ->
|
|
|
|
?INFO_MSG("(~w) Close session for ~s",
|
|
|
|
[StateData#state.socket,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(StateData#state.jid)]),
|
2014-03-19 00:51:33 +01:00
|
|
|
EmptySet = (?SETS):new(),
|
|
|
|
case StateData of
|
2014-09-25 00:08:56 +02:00
|
|
|
#state{pres_last = undefined, pres_a = EmptySet} ->
|
2014-03-19 00:51:33 +01:00
|
|
|
ejabberd_sm:close_session(StateData#state.sid,
|
|
|
|
StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.resource);
|
|
|
|
_ ->
|
|
|
|
From = StateData#state.jid,
|
|
|
|
Packet = #xmlel{name = <<"presence">>,
|
|
|
|
attrs = [{<<"type">>, <<"unavailable">>}],
|
|
|
|
children = []},
|
|
|
|
ejabberd_sm:close_session_unset_presence(StateData#state.sid,
|
|
|
|
StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.resource,
|
|
|
|
<<"">>),
|
|
|
|
presence_broadcast(StateData, From,
|
2014-09-25 00:08:56 +02:00
|
|
|
StateData#state.pres_a, Packet)
|
2014-03-19 00:51:33 +01:00
|
|
|
end,
|
2014-03-25 23:23:38 +01:00
|
|
|
handle_unacked_stanzas(StateData)
|
2014-03-19 00:51:33 +01:00
|
|
|
end,
|
|
|
|
bounce_messages();
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
2002-11-30 19:46:16 +01:00
|
|
|
end,
|
2006-10-01 03:53:37 +02:00
|
|
|
(StateData#state.sockmod):close(StateData#state.socket),
|
2002-11-18 21:39:47 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2003-02-09 20:17:23 +01:00
|
|
|
change_shaper(StateData, JID) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Shaper = acl:match_rule(StateData#state.server,
|
|
|
|
StateData#state.shaper, JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
(StateData#state.sockmod):change_shaper(StateData#state.socket,
|
|
|
|
Shaper).
|
2003-02-09 20:17:23 +01:00
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
send_text(StateData, Text) when StateData#state.mgmt_state == pending ->
|
2014-03-19 00:51:33 +01:00
|
|
|
?DEBUG("Cannot send text while waiting for resumption: ~p", [Text]);
|
2010-06-01 20:52:15 +02:00
|
|
|
send_text(StateData, Text) when StateData#state.xml_socket ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("Send Text on stream = ~p", [Text]),
|
2015-04-15 10:47:10 +02:00
|
|
|
(StateData#state.sockmod):send_xml(StateData#state.socket,
|
2010-06-01 20:52:15 +02:00
|
|
|
{xmlstreamraw, Text});
|
2014-05-23 11:38:54 +02:00
|
|
|
send_text(StateData, Text) when StateData#state.mgmt_state == active ->
|
|
|
|
?DEBUG("Send XML on stream = ~p", [Text]),
|
|
|
|
case catch (StateData#state.sockmod):send(StateData#state.socket, Text) of
|
|
|
|
{'EXIT', _} ->
|
2014-09-06 20:34:32 +02:00
|
|
|
(StateData#state.sockmod):close(StateData#state.socket),
|
2015-05-14 00:58:36 +02:00
|
|
|
{error, closed};
|
2014-05-23 11:38:54 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2003-04-15 15:48:43 +02:00
|
|
|
send_text(StateData, Text) ->
|
2010-07-01 12:54:01 +02:00
|
|
|
?DEBUG("Send XML on stream = ~p", [Text]),
|
2006-10-01 03:53:37 +02:00
|
|
|
(StateData#state.sockmod):send(StateData#state.socket, Text).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
send_element(StateData, El) when StateData#state.mgmt_state == pending ->
|
2014-03-19 00:51:33 +01:00
|
|
|
?DEBUG("Cannot send element while waiting for resumption: ~p", [El]);
|
2009-08-31 20:37:52 +02:00
|
|
|
send_element(StateData, El) when StateData#state.xml_socket ->
|
|
|
|
(StateData#state.sockmod):send_xml(StateData#state.socket,
|
|
|
|
{xmlstreamelement, El});
|
2003-04-15 15:48:43 +02:00
|
|
|
send_element(StateData, El) ->
|
2010-07-01 12:54:01 +02:00
|
|
|
send_text(StateData, xml:element_to_binary(El)).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2014-09-11 17:44:29 +02:00
|
|
|
send_stanza(StateData, Stanza) when StateData#state.csi_state == inactive ->
|
|
|
|
csi_filter_stanza(StateData, Stanza);
|
2014-05-06 07:27:10 +02:00
|
|
|
send_stanza(StateData, Stanza) when StateData#state.mgmt_state == pending ->
|
|
|
|
mgmt_queue_add(StateData, Stanza);
|
|
|
|
send_stanza(StateData, Stanza) when StateData#state.mgmt_state == active ->
|
2015-05-14 01:04:23 +02:00
|
|
|
NewStateData = send_stanza_and_ack_req(StateData, Stanza),
|
2014-09-06 20:34:32 +02:00
|
|
|
mgmt_queue_add(NewStateData, Stanza);
|
2014-03-12 23:34:14 +01:00
|
|
|
send_stanza(StateData, Stanza) ->
|
|
|
|
send_element(StateData, Stanza),
|
|
|
|
StateData.
|
|
|
|
|
2014-09-11 17:44:29 +02:00
|
|
|
send_packet(StateData, Packet) ->
|
2014-05-12 19:20:25 +02:00
|
|
|
case is_stanza(Packet) of
|
|
|
|
true ->
|
|
|
|
send_stanza(StateData, Packet);
|
|
|
|
false ->
|
|
|
|
send_element(StateData, Packet),
|
|
|
|
StateData
|
2014-09-11 17:44:29 +02:00
|
|
|
end.
|
2014-05-12 19:20:25 +02:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
send_header(StateData, Server, Version, Lang)
|
2013-03-14 10:33:02 +01:00
|
|
|
when StateData#state.xml_socket ->
|
|
|
|
VersionAttr = case Version of
|
|
|
|
<<"">> -> [];
|
|
|
|
_ -> [{<<"version">>, Version}]
|
|
|
|
end,
|
|
|
|
LangAttr = case Lang of
|
|
|
|
<<"">> -> [];
|
|
|
|
_ -> [{<<"xml:lang">>, Lang}]
|
|
|
|
end,
|
|
|
|
Header = {xmlstreamstart, <<"stream:stream">>,
|
|
|
|
VersionAttr ++
|
|
|
|
LangAttr ++
|
|
|
|
[{<<"xmlns">>, <<"jabber:client">>},
|
|
|
|
{<<"xmlns:stream">>,
|
|
|
|
<<"http://etherx.jabber.org/streams">>},
|
|
|
|
{<<"id">>, StateData#state.streamid},
|
|
|
|
{<<"from">>, Server}]},
|
|
|
|
(StateData#state.sockmod):send_xml(StateData#state.socket,
|
|
|
|
Header);
|
2009-08-31 20:37:52 +02:00
|
|
|
send_header(StateData, Server, Version, Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
VersionStr = case Version of
|
|
|
|
<<"">> -> <<"">>;
|
|
|
|
_ -> [<<" version='">>, Version, <<"'">>]
|
|
|
|
end,
|
|
|
|
LangStr = case Lang of
|
|
|
|
<<"">> -> <<"">>;
|
|
|
|
_ -> [<<" xml:lang='">>, Lang, <<"'">>]
|
|
|
|
end,
|
2009-08-31 20:37:52 +02:00
|
|
|
Header = io_lib:format(?STREAM_HEADER,
|
2013-03-14 10:33:02 +01:00
|
|
|
[StateData#state.streamid, Server, VersionStr,
|
2009-08-31 20:37:52 +02:00
|
|
|
LangStr]),
|
2013-03-14 10:33:02 +01:00
|
|
|
send_text(StateData, iolist_to_binary(Header)).
|
2009-08-31 20:37:52 +02:00
|
|
|
|
2014-05-05 00:02:55 +02:00
|
|
|
send_trailer(StateData)
|
2014-05-06 07:27:10 +02:00
|
|
|
when StateData#state.mgmt_state == pending ->
|
2014-05-05 00:02:55 +02:00
|
|
|
?DEBUG("Cannot send stream trailer while waiting for resumption", []);
|
2013-03-14 10:33:02 +01:00
|
|
|
send_trailer(StateData)
|
|
|
|
when StateData#state.xml_socket ->
|
|
|
|
(StateData#state.sockmod):send_xml(StateData#state.socket,
|
|
|
|
{xmlstreamend, <<"stream:stream">>});
|
2009-08-31 20:37:52 +02:00
|
|
|
send_trailer(StateData) ->
|
|
|
|
send_text(StateData, ?STREAM_TRAILER).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
new_id() -> randoms:get_string().
|
2002-11-20 21:19:20 +01:00
|
|
|
|
2003-01-16 21:24:53 +01:00
|
|
|
is_auth_packet(El) ->
|
|
|
|
case jlib:iq_query_info(El) of
|
2014-05-20 12:54:49 +02:00
|
|
|
#iq{id = ID, type = Type, xmlns = ?NS_AUTH, sub_el = SubEl} ->
|
|
|
|
#xmlel{children = Els} = SubEl,
|
|
|
|
{auth, ID, Type,
|
2015-10-07 00:06:58 +02:00
|
|
|
get_auth_tags(Els, <<"">>, <<"">>, <<"">>, <<"">>)};
|
2014-05-20 12:54:49 +02:00
|
|
|
_ -> false
|
2003-01-16 21:24:53 +01:00
|
|
|
end.
|
|
|
|
|
2014-03-12 23:34:14 +01:00
|
|
|
is_stanza(#xmlel{name = Name, attrs = Attrs}) when Name == <<"message">>;
|
|
|
|
Name == <<"presence">>;
|
|
|
|
Name == <<"iq">> ->
|
|
|
|
case xml:get_attr(<<"xmlns">>, Attrs) of
|
|
|
|
{value, NS} when NS /= <<"jabber:client">>,
|
|
|
|
NS /= <<"jabber:server">> ->
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
is_stanza(_El) ->
|
|
|
|
false.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_auth_tags([#xmlel{name = Name, children = Els} | L],
|
|
|
|
U, P, D, R) ->
|
2002-11-20 21:19:20 +01:00
|
|
|
CData = xml:get_cdata(Els),
|
|
|
|
case Name of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"username">> -> get_auth_tags(L, CData, P, D, R);
|
|
|
|
<<"password">> -> get_auth_tags(L, U, CData, D, R);
|
|
|
|
<<"digest">> -> get_auth_tags(L, U, P, CData, R);
|
|
|
|
<<"resource">> -> get_auth_tags(L, U, P, D, CData);
|
|
|
|
_ -> get_auth_tags(L, U, P, D, R)
|
2002-11-20 21:19:20 +01:00
|
|
|
end;
|
|
|
|
get_auth_tags([_ | L], U, P, D, R) ->
|
|
|
|
get_auth_tags(L, U, P, D, R);
|
|
|
|
get_auth_tags([], U, P, D, R) ->
|
|
|
|
{U, P, D, R}.
|
|
|
|
|
2009-08-31 18:29:25 +02:00
|
|
|
%% Copied from ejabberd_socket.erl
|
|
|
|
-record(socket_state, {sockmod, socket, receiver}).
|
|
|
|
|
2008-02-15 17:35:32 +01:00
|
|
|
get_conn_type(StateData) ->
|
2008-02-18 10:55:06 +01:00
|
|
|
case (StateData#state.sockmod):get_sockmod(StateData#state.socket) of
|
|
|
|
gen_tcp -> c2s;
|
2013-06-20 10:40:44 +02:00
|
|
|
p1_tls -> c2s_tls;
|
2013-06-17 15:33:25 +02:00
|
|
|
ezlib ->
|
|
|
|
case ezlib:get_sockmod((StateData#state.socket)#socket_state.socket) of
|
2009-08-31 18:29:25 +02:00
|
|
|
gen_tcp -> c2s_compressed;
|
2013-06-20 10:40:44 +02:00
|
|
|
p1_tls -> c2s_compressed_tls
|
2009-08-31 18:29:25 +02:00
|
|
|
end;
|
2008-02-15 17:35:32 +01:00
|
|
|
ejabberd_http_bind -> http_bind;
|
2015-07-22 11:54:11 +02:00
|
|
|
ejabberd_http_ws -> websocket;
|
2008-02-18 10:55:06 +01:00
|
|
|
_ -> unknown
|
2008-02-15 17:35:32 +01:00
|
|
|
end.
|
2002-11-20 21:19:20 +01:00
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
process_presence_probe(From, To, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LFrom = jid:tolower(From),
|
2013-03-14 10:33:02 +01:00
|
|
|
LBFrom = setelement(3, LFrom, <<"">>),
|
2002-12-17 21:49:45 +01:00
|
|
|
case StateData#state.pres_last of
|
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
2015-10-07 00:06:58 +02:00
|
|
|
Cond = ((?SETS):is_element(LFrom, StateData#state.pres_f)
|
|
|
|
orelse
|
|
|
|
((LFrom /= LBFrom) andalso
|
|
|
|
(?SETS):is_element(LBFrom, StateData#state.pres_f))),
|
|
|
|
if Cond ->
|
2014-12-07 15:55:18 +01:00
|
|
|
%% To is the one sending the presence (the probe target)
|
|
|
|
Packet = jlib:add_delay_info(StateData#state.pres_last, To,
|
|
|
|
StateData#state.pres_timestamp),
|
2010-11-05 03:48:13 +01:00
|
|
|
case privacy_check_packet(StateData, To, From, Packet, out) of
|
2003-10-10 21:01:11 +02:00
|
|
|
deny ->
|
|
|
|
ok;
|
2006-10-05 05:17:41 +02:00
|
|
|
allow ->
|
2008-01-11 02:39:10 +01:00
|
|
|
Pid=element(2, StateData#state.sid),
|
|
|
|
ejabberd_hooks:run(presence_probe_hook, StateData#state.server, [From, To, Pid]),
|
2008-04-04 00:35:26 +02:00
|
|
|
%% Don't route a presence probe to oneself
|
|
|
|
case From == To of
|
|
|
|
false ->
|
|
|
|
ejabberd_router:route(To, From, Packet);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
2003-10-10 21:01:11 +02:00
|
|
|
end;
|
2002-12-17 21:49:45 +01:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2009-01-17 17:33:35 +01:00
|
|
|
%% User updates his presence (non-directed presence packet)
|
2002-12-17 21:49:45 +01:00
|
|
|
presence_update(From, Packet, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{attrs = Attrs} = Packet,
|
|
|
|
case xml:get_attr_s(<<"type">>, Attrs) of
|
|
|
|
<<"unavailable">> ->
|
|
|
|
Status = case xml:get_subtag(Packet, <<"status">>) of
|
|
|
|
false -> <<"">>;
|
|
|
|
StatusTag -> xml:get_tag_cdata(StatusTag)
|
|
|
|
end,
|
|
|
|
Info = [{ip, StateData#state.ip},
|
|
|
|
{conn, StateData#state.conn},
|
|
|
|
{auth_module, StateData#state.auth_module}],
|
|
|
|
ejabberd_sm:unset_presence(StateData#state.sid,
|
|
|
|
StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.resource, Status, Info),
|
|
|
|
presence_broadcast(StateData, From,
|
|
|
|
StateData#state.pres_a, Packet),
|
|
|
|
StateData#state{pres_last = undefined,
|
|
|
|
pres_timestamp = undefined, pres_a = (?SETS):new()};
|
|
|
|
<<"error">> -> StateData;
|
|
|
|
<<"probe">> -> StateData;
|
|
|
|
<<"subscribe">> -> StateData;
|
|
|
|
<<"subscribed">> -> StateData;
|
|
|
|
<<"unsubscribe">> -> StateData;
|
|
|
|
<<"unsubscribed">> -> StateData;
|
|
|
|
_ ->
|
|
|
|
OldPriority = case StateData#state.pres_last of
|
|
|
|
undefined -> 0;
|
|
|
|
OldPresence -> get_priority_from_presence(OldPresence)
|
2006-04-23 16:57:37 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
NewPriority = get_priority_from_presence(Packet),
|
|
|
|
update_priority(NewPriority, Packet, StateData),
|
|
|
|
FromUnavail = (StateData#state.pres_last == undefined),
|
|
|
|
?DEBUG("from unavail = ~p~n", [FromUnavail]),
|
|
|
|
NewStateData = StateData#state{pres_last = Packet,
|
2015-12-07 16:08:57 +01:00
|
|
|
pres_timestamp = p1_time_compat:timestamp()},
|
2013-03-14 10:33:02 +01:00
|
|
|
NewState = if FromUnavail ->
|
|
|
|
ejabberd_hooks:run(user_available_hook,
|
|
|
|
NewStateData#state.server,
|
|
|
|
[NewStateData#state.jid]),
|
2014-03-12 23:34:14 +01:00
|
|
|
ResentStateData = if NewPriority >= 0 ->
|
|
|
|
resend_offline_messages(NewStateData),
|
|
|
|
resend_subscription_requests(NewStateData);
|
|
|
|
true -> NewStateData
|
|
|
|
end,
|
|
|
|
presence_broadcast_first(From, ResentStateData,
|
2013-03-14 10:33:02 +01:00
|
|
|
Packet);
|
|
|
|
true ->
|
|
|
|
presence_broadcast_to_trusted(NewStateData, From,
|
|
|
|
NewStateData#state.pres_f,
|
|
|
|
NewStateData#state.pres_a,
|
|
|
|
Packet),
|
|
|
|
if OldPriority < 0, NewPriority >= 0 ->
|
|
|
|
resend_offline_messages(NewStateData);
|
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
NewStateData
|
|
|
|
end,
|
|
|
|
NewState
|
2002-12-17 21:49:45 +01:00
|
|
|
end.
|
|
|
|
|
2009-01-17 17:33:35 +01:00
|
|
|
%% User sends a directed presence packet
|
2002-12-18 21:26:08 +01:00
|
|
|
presence_track(From, To, Packet, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{attrs = Attrs} = Packet,
|
2015-11-24 16:44:13 +01:00
|
|
|
LTo = jid:tolower(To),
|
2002-12-20 21:42:08 +01:00
|
|
|
User = StateData#state.user,
|
2005-04-17 20:08:34 +02:00
|
|
|
Server = StateData#state.server,
|
2013-03-14 10:33:02 +01:00
|
|
|
case xml:get_attr_s(<<"type">>, Attrs) of
|
|
|
|
<<"unavailable">> ->
|
|
|
|
A = remove_element(LTo, StateData#state.pres_a),
|
2015-04-20 22:03:18 +02:00
|
|
|
check_privacy_route(From, StateData#state{pres_a = A}, From, To, Packet);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"subscribe">> ->
|
2015-04-20 22:03:18 +02:00
|
|
|
try_roster_subscribe(subscribe, User, Server, From, To, Packet, StateData);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"subscribed">> ->
|
|
|
|
ejabberd_hooks:run(roster_out_subscription, Server,
|
|
|
|
[User, Server, To, subscribed]),
|
|
|
|
check_privacy_route(From, StateData,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:remove_resource(From), To, Packet);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"unsubscribe">> ->
|
2015-04-20 22:03:18 +02:00
|
|
|
try_roster_subscribe(unsubscribe, User, Server, From, To, Packet, StateData);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"unsubscribed">> ->
|
|
|
|
ejabberd_hooks:run(roster_out_subscription, Server,
|
|
|
|
[User, Server, To, unsubscribed]),
|
|
|
|
check_privacy_route(From, StateData,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:remove_resource(From), To, Packet);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"error">> ->
|
2015-04-20 22:03:18 +02:00
|
|
|
check_privacy_route(From, StateData, From, To, Packet);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"probe">> ->
|
2015-04-20 22:03:18 +02:00
|
|
|
check_privacy_route(From, StateData, From, To, Packet);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
A = (?SETS):add_element(LTo, StateData#state.pres_a),
|
2015-04-20 22:03:18 +02:00
|
|
|
check_privacy_route(From, StateData#state{pres_a = A}, From, To, Packet)
|
2002-12-18 21:26:08 +01:00
|
|
|
end.
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
check_privacy_route(From, StateData, FromRoute, To,
|
|
|
|
Packet) ->
|
|
|
|
case privacy_check_packet(StateData, From, To, Packet,
|
|
|
|
out)
|
|
|
|
of
|
2015-04-20 16:01:56 +02:00
|
|
|
deny ->
|
|
|
|
Lang = StateData#state.lang,
|
|
|
|
ErrText = <<"Your active privacy list has denied "
|
|
|
|
"the routing of this stanza.">>,
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERRT_NOT_ACCEPTABLE(Lang, ErrText)),
|
|
|
|
Err2 = jlib:replace_from_to(To, From, Err),
|
2015-04-20 22:03:18 +02:00
|
|
|
send_stanza(StateData, Err2);
|
|
|
|
allow ->
|
|
|
|
ejabberd_router:route(FromRoute, To, Packet),
|
|
|
|
StateData
|
2009-02-16 17:24:08 +01:00
|
|
|
end.
|
|
|
|
|
2009-03-24 17:22:33 +01:00
|
|
|
%% Check if privacy rules allow this delivery
|
2013-03-14 10:33:02 +01:00
|
|
|
privacy_check_packet(StateData, From, To, Packet,
|
|
|
|
Dir) ->
|
|
|
|
ejabberd_hooks:run_fold(privacy_check_packet,
|
|
|
|
StateData#state.server, allow,
|
|
|
|
[StateData#state.user, StateData#state.server,
|
|
|
|
StateData#state.privacy_list, {From, To, Packet},
|
|
|
|
Dir]).
|
|
|
|
|
2010-11-05 03:29:32 +01:00
|
|
|
is_privacy_allow(StateData, From, To, Packet, Dir) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
allow ==
|
|
|
|
privacy_check_packet(StateData, From, To, Packet, Dir).
|
2009-03-07 09:59:26 +01:00
|
|
|
|
2013-12-04 14:55:21 +01:00
|
|
|
%%% Check ACL before allowing to send a subscription stanza
|
|
|
|
try_roster_subscribe(Type, User, Server, From, To, Packet, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
JID1 = jid:make(User, Server, <<"">>),
|
2013-12-04 14:55:21 +01:00
|
|
|
Access = gen_mod:get_module_opt(Server, mod_roster, access, fun(A) when is_atom(A) -> A end, all),
|
|
|
|
case acl:match_rule(Server, Access, JID1) of
|
|
|
|
deny ->
|
|
|
|
%% Silently drop this (un)subscription request
|
2015-04-20 22:03:18 +02:00
|
|
|
StateData;
|
2013-12-04 14:55:21 +01:00
|
|
|
allow ->
|
|
|
|
ejabberd_hooks:run(roster_out_subscription,
|
|
|
|
Server,
|
|
|
|
[User, Server, To, Type]),
|
2015-11-24 16:44:13 +01:00
|
|
|
check_privacy_route(From, StateData, jid:remove_resource(From),
|
2013-12-04 14:55:21 +01:00
|
|
|
To, Packet)
|
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
%% Send presence when disconnecting
|
2003-10-10 21:01:11 +02:00
|
|
|
presence_broadcast(StateData, From, JIDSet, Packet) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
JIDs = ?SETS:to_list(JIDSet),
|
|
|
|
JIDs2 = format_and_check_privacy(From, StateData, Packet, JIDs, out),
|
2015-04-08 14:01:16 +02:00
|
|
|
Server = StateData#state.server,
|
|
|
|
send_multiple(From, Server, JIDs2, Packet).
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
%% Send presence when updating presence
|
|
|
|
presence_broadcast_to_trusted(StateData, From, Trusted, JIDSet, Packet) ->
|
|
|
|
JIDs = ?SETS:to_list(JIDSet),
|
|
|
|
JIDs_trusted = [JID || JID <- JIDs, ?SETS:is_element(JID, Trusted)],
|
|
|
|
JIDs2 = format_and_check_privacy(From, StateData, Packet, JIDs_trusted, out),
|
2015-04-08 14:01:16 +02:00
|
|
|
Server = StateData#state.server,
|
|
|
|
send_multiple(From, Server, JIDs2, Packet).
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
%% Send presence when connecting
|
2002-12-17 21:49:45 +01:00
|
|
|
presence_broadcast_first(From, StateData, Packet) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
JIDsProbe =
|
|
|
|
?SETS:fold(
|
|
|
|
fun(JID, L) -> [JID | L] end,
|
|
|
|
[],
|
|
|
|
StateData#state.pres_t),
|
|
|
|
PacketProbe = #xmlel{name = <<"presence">>, attrs = [{<<"type">>,<<"probe">>}], children = []},
|
2014-09-01 14:38:41 +02:00
|
|
|
JIDs2Probe = format_and_check_privacy(From, StateData, PacketProbe, JIDsProbe, out),
|
2013-03-14 10:33:02 +01:00
|
|
|
Server = StateData#state.server,
|
2015-04-08 14:01:16 +02:00
|
|
|
send_multiple(From, Server, JIDs2Probe, PacketProbe),
|
2013-03-14 10:33:02 +01:00
|
|
|
{As, JIDs} =
|
|
|
|
?SETS:fold(
|
|
|
|
fun(JID, {A, JID_list}) ->
|
|
|
|
{?SETS:add_element(JID, A), JID_list++[JID]}
|
|
|
|
end,
|
|
|
|
{StateData#state.pres_a, []},
|
|
|
|
StateData#state.pres_f),
|
|
|
|
JIDs2 = format_and_check_privacy(From, StateData, Packet, JIDs, out),
|
2015-04-08 14:01:16 +02:00
|
|
|
send_multiple(From, Server, JIDs2, Packet),
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state{pres_a = As}.
|
|
|
|
|
|
|
|
format_and_check_privacy(From, StateData, Packet, JIDs, Dir) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
FJIDs = [jid:make(JID) || JID <- JIDs],
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:filter(
|
|
|
|
fun(FJID) ->
|
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, StateData#state.server,
|
|
|
|
allow,
|
|
|
|
[StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.privacy_list,
|
|
|
|
{From, FJID, Packet},
|
|
|
|
Dir]) of
|
|
|
|
deny -> false;
|
|
|
|
allow -> true
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
FJIDs).
|
|
|
|
|
2015-04-08 14:01:16 +02:00
|
|
|
send_multiple(From, Server, JIDs, Packet) ->
|
|
|
|
ejabberd_router_multicast:route_multicast(From, Server, JIDs, Packet).
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2002-12-18 21:26:08 +01:00
|
|
|
remove_element(E, Set) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case (?SETS):is_element(E, Set) of
|
|
|
|
true -> (?SETS):del_element(E, Set);
|
|
|
|
_ -> Set
|
2002-12-18 21:26:08 +01:00
|
|
|
end.
|
|
|
|
|
2002-12-22 21:56:29 +01:00
|
|
|
roster_change(IJID, ISubscription, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LIJID = jid:tolower(IJID),
|
2014-05-20 12:54:49 +02:00
|
|
|
IsFrom = (ISubscription == both) or (ISubscription == from),
|
2013-03-14 10:33:02 +01:00
|
|
|
IsTo = (ISubscription == both) or (ISubscription == to),
|
2014-05-20 12:54:49 +02:00
|
|
|
OldIsFrom = (?SETS):is_element(LIJID, StateData#state.pres_f),
|
|
|
|
FSet = if
|
|
|
|
IsFrom -> (?SETS):add_element(LIJID, StateData#state.pres_f);
|
2015-10-07 00:06:58 +02:00
|
|
|
true -> remove_element(LIJID, StateData#state.pres_f)
|
2003-01-10 20:44:35 +01:00
|
|
|
end,
|
2014-05-20 12:54:49 +02:00
|
|
|
TSet = if
|
|
|
|
IsTo -> (?SETS):add_element(LIJID, StateData#state.pres_t);
|
2015-10-07 00:06:58 +02:00
|
|
|
true -> remove_element(LIJID, StateData#state.pres_t)
|
2003-01-10 20:44:35 +01:00
|
|
|
end,
|
2002-12-22 21:56:29 +01:00
|
|
|
case StateData#state.pres_last of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined ->
|
|
|
|
StateData#state{pres_f = FSet, pres_t = TSet};
|
|
|
|
P ->
|
|
|
|
?DEBUG("roster changed for ~p~n",
|
|
|
|
[StateData#state.user]),
|
|
|
|
From = StateData#state.jid,
|
2015-11-24 16:44:13 +01:00
|
|
|
To = jid:make(IJID),
|
2013-03-14 10:33:02 +01:00
|
|
|
Cond1 = IsFrom andalso not OldIsFrom,
|
|
|
|
Cond2 = not IsFrom andalso OldIsFrom andalso
|
|
|
|
((?SETS):is_element(LIJID, StateData#state.pres_a)),
|
|
|
|
if Cond1 ->
|
|
|
|
?DEBUG("C1: ~p~n", [LIJID]),
|
|
|
|
case privacy_check_packet(StateData, From, To, P, out)
|
|
|
|
of
|
|
|
|
deny -> ok;
|
|
|
|
allow -> ejabberd_router:route(From, To, P)
|
|
|
|
end,
|
|
|
|
A = (?SETS):add_element(LIJID, StateData#state.pres_a),
|
|
|
|
StateData#state{pres_a = A, pres_f = FSet,
|
|
|
|
pres_t = TSet};
|
|
|
|
Cond2 ->
|
|
|
|
?DEBUG("C2: ~p~n", [LIJID]),
|
|
|
|
PU = #xmlel{name = <<"presence">>,
|
|
|
|
attrs = [{<<"type">>, <<"unavailable">>}],
|
|
|
|
children = []},
|
|
|
|
case privacy_check_packet(StateData, From, To, PU, out)
|
|
|
|
of
|
|
|
|
deny -> ok;
|
|
|
|
allow -> ejabberd_router:route(From, To, PU)
|
|
|
|
end,
|
|
|
|
A = remove_element(LIJID, StateData#state.pres_a),
|
|
|
|
StateData#state{pres_a = A, pres_f = FSet,
|
|
|
|
pres_t = TSet};
|
|
|
|
true -> StateData#state{pres_f = FSet, pres_t = TSet}
|
|
|
|
end
|
2002-12-22 21:56:29 +01:00
|
|
|
end.
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2007-05-03 17:45:24 +02:00
|
|
|
update_priority(Priority, Packet, StateData) ->
|
2008-07-16 20:05:46 +02:00
|
|
|
Info = [{ip, StateData#state.ip}, {conn, StateData#state.conn},
|
|
|
|
{auth_module, StateData#state.auth_module}],
|
2006-01-24 00:13:06 +01:00
|
|
|
ejabberd_sm:set_presence(StateData#state.sid,
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.user, StateData#state.server,
|
|
|
|
StateData#state.resource, Priority, Packet, Info).
|
2004-09-17 21:52:59 +02:00
|
|
|
|
2006-04-23 16:57:37 +02:00
|
|
|
get_priority_from_presence(PresencePacket) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case xml:get_subtag(PresencePacket, <<"priority">>) of
|
|
|
|
false -> 0;
|
|
|
|
SubEl ->
|
|
|
|
case catch
|
|
|
|
jlib:binary_to_integer(xml:get_tag_cdata(SubEl))
|
|
|
|
of
|
|
|
|
P when is_integer(P) -> P;
|
|
|
|
_ -> 0
|
|
|
|
end
|
2006-04-23 16:57:37 +02:00
|
|
|
end.
|
2003-08-03 21:09:40 +02:00
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
process_privacy_iq(From, To,
|
2013-03-14 10:33:02 +01:00
|
|
|
#iq{type = Type, sub_el = SubEl} = IQ, StateData) ->
|
|
|
|
{Res, NewStateData} = case Type of
|
|
|
|
get ->
|
|
|
|
R = ejabberd_hooks:run_fold(privacy_iq_get,
|
|
|
|
StateData#state.server,
|
|
|
|
{error,
|
|
|
|
?ERR_FEATURE_NOT_IMPLEMENTED},
|
|
|
|
[From, To, IQ,
|
|
|
|
StateData#state.privacy_list]),
|
|
|
|
{R, StateData};
|
|
|
|
set ->
|
|
|
|
case ejabberd_hooks:run_fold(privacy_iq_set,
|
|
|
|
StateData#state.server,
|
|
|
|
{error,
|
|
|
|
?ERR_FEATURE_NOT_IMPLEMENTED},
|
|
|
|
[From, To, IQ])
|
|
|
|
of
|
|
|
|
{result, R, NewPrivList} ->
|
|
|
|
{{result, R},
|
|
|
|
StateData#state{privacy_list =
|
|
|
|
NewPrivList}};
|
|
|
|
R -> {R, StateData}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
IQRes = case Res of
|
|
|
|
{result, Result} ->
|
|
|
|
IQ#iq{type = result, sub_el = Result};
|
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
|
|
|
end,
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(IQRes)),
|
2003-08-03 21:09:40 +02:00
|
|
|
NewStateData.
|
|
|
|
|
2010-11-05 03:48:13 +01:00
|
|
|
resend_offline_messages(StateData) ->
|
2014-05-20 12:54:49 +02:00
|
|
|
case ejabberd_hooks:run_fold(resend_offline_messages_hook,
|
|
|
|
StateData#state.server, [],
|
|
|
|
[StateData#state.user, StateData#state.server])
|
|
|
|
of
|
2013-03-14 10:33:02 +01:00
|
|
|
Rs -> %%when is_list(Rs) ->
|
|
|
|
lists:foreach(fun ({route, From, To,
|
|
|
|
#xmlel{} = Packet}) ->
|
|
|
|
Pass = case privacy_check_packet(StateData,
|
|
|
|
From, To,
|
|
|
|
Packet, in)
|
|
|
|
of
|
|
|
|
allow -> true;
|
|
|
|
deny -> false
|
|
|
|
end,
|
|
|
|
if Pass ->
|
|
|
|
ejabberd_router:route(From, To, Packet);
|
|
|
|
true -> ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Rs)
|
2004-01-18 21:42:09 +01:00
|
|
|
end.
|
|
|
|
|
2006-05-23 22:19:37 +02:00
|
|
|
resend_subscription_requests(#state{user = User,
|
2014-05-20 12:54:49 +02:00
|
|
|
server = Server} = StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
PendingSubscriptions =
|
|
|
|
ejabberd_hooks:run_fold(resend_subscription_requests_hook,
|
|
|
|
Server, [], [User, Server]),
|
2014-03-12 23:34:14 +01:00
|
|
|
lists:foldl(fun (XMLPacket, AccStateData) ->
|
2014-05-12 19:20:25 +02:00
|
|
|
send_packet(AccStateData, XMLPacket)
|
2014-03-12 23:34:14 +01:00
|
|
|
end,
|
|
|
|
StateData,
|
|
|
|
PendingSubscriptions).
|
2006-05-23 22:19:37 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_showtag(undefined) -> <<"unavailable">>;
|
2004-03-10 22:54:54 +01:00
|
|
|
get_showtag(Presence) ->
|
2014-05-20 12:54:49 +02:00
|
|
|
case xml:get_path_s(Presence, [{elem, <<"show">>}, cdata]) of
|
|
|
|
<<"">> -> <<"available">>;
|
|
|
|
ShowTag -> ShowTag
|
2004-03-10 22:54:54 +01:00
|
|
|
end.
|
2004-01-18 21:42:09 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_statustag(undefined) -> <<"">>;
|
2004-03-10 22:54:54 +01:00
|
|
|
get_statustag(Presence) ->
|
2014-05-20 13:27:16 +02:00
|
|
|
xml:get_path_s(Presence, [{elem, <<"status">>}, cdata]).
|
2005-07-15 00:28:21 +02:00
|
|
|
|
|
|
|
process_unauthenticated_stanza(StateData, El) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
NewEl = case xml:get_tag_attr_s(<<"xml:lang">>, El) of
|
|
|
|
<<"">> ->
|
|
|
|
case StateData#state.lang of
|
|
|
|
<<"">> -> El;
|
|
|
|
Lang -> xml:replace_tag_attr(<<"xml:lang">>, Lang, El)
|
|
|
|
end;
|
|
|
|
_ -> El
|
2010-11-06 05:58:52 +01:00
|
|
|
end,
|
|
|
|
case jlib:iq_query_info(NewEl) of
|
2013-03-14 10:33:02 +01:00
|
|
|
#iq{} = IQ ->
|
|
|
|
Res = ejabberd_hooks:run_fold(c2s_unauthenticated_iq,
|
|
|
|
StateData#state.server, empty,
|
|
|
|
[StateData#state.server, IQ,
|
|
|
|
StateData#state.ip]),
|
|
|
|
case Res of
|
|
|
|
empty ->
|
|
|
|
ResIQ = IQ#iq{type = error,
|
|
|
|
sub_el = [?ERR_SERVICE_UNAVAILABLE]},
|
2015-11-24 16:44:13 +01:00
|
|
|
Res1 = jlib:replace_from_to(jid:make(<<"">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.server,
|
|
|
|
<<"">>),
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make(<<"">>, <<"">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">>),
|
|
|
|
jlib:iq_to_xml(ResIQ)),
|
|
|
|
send_element(StateData,
|
|
|
|
jlib:remove_attr(<<"to">>, Res1));
|
|
|
|
_ -> send_element(StateData, Res)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
% Drop any stanza, which isn't IQ stanza
|
|
|
|
ok
|
2005-07-15 00:28:21 +02:00
|
|
|
end.
|
|
|
|
|
2007-05-21 05:21:34 +02:00
|
|
|
peerip(SockMod, Socket) ->
|
|
|
|
IP = case SockMod of
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_tcp -> inet:peername(Socket);
|
|
|
|
_ -> SockMod:peername(Socket)
|
2007-05-21 05:21:34 +02:00
|
|
|
end,
|
|
|
|
case IP of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, IPOK} -> IPOK;
|
|
|
|
_ -> undefined
|
2007-05-21 05:21:34 +02:00
|
|
|
end.
|
2007-09-14 16:20:56 +02:00
|
|
|
|
2009-09-28 19:23:37 +02:00
|
|
|
%% fsm_next_state_pack: Pack the StateData structure to improve
|
|
|
|
%% sharing.
|
|
|
|
fsm_next_state_pack(StateName, StateData) ->
|
|
|
|
fsm_next_state_gc(StateName, pack(StateData)).
|
|
|
|
|
|
|
|
%% fsm_next_state_gc: Garbage collect the process heap to make use of
|
|
|
|
%% the newly packed StateData structure.
|
|
|
|
fsm_next_state_gc(StateName, PackedStateData) ->
|
|
|
|
erlang:garbage_collect(),
|
|
|
|
fsm_next_state(StateName, PackedStateData).
|
|
|
|
|
2007-09-14 16:18:01 +02:00
|
|
|
%% fsm_next_state: Generate the next_state FSM tuple with different
|
|
|
|
%% timeout, depending on the future state
|
2014-10-17 01:35:30 +02:00
|
|
|
fsm_next_state(session_established, #state{mgmt_max_queue = exceeded} =
|
|
|
|
StateData) ->
|
|
|
|
?WARNING_MSG("ACK queue too long, terminating session for ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData#state.jid)]),
|
2014-10-17 01:35:30 +02:00
|
|
|
Err = ?SERRT_POLICY_VIOLATION(StateData#state.lang,
|
|
|
|
<<"Too many unacked stanzas">>),
|
|
|
|
send_element(StateData, Err),
|
|
|
|
send_trailer(StateData),
|
|
|
|
{stop, normal, StateData#state{mgmt_resend = false}};
|
2014-09-06 20:34:32 +02:00
|
|
|
fsm_next_state(session_established, #state{mgmt_state = pending} = StateData) ->
|
|
|
|
fsm_next_state(wait_for_resume, StateData);
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(session_established, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, session_established, StateData,
|
|
|
|
?C2S_HIBERNATE_TIMEOUT};
|
2014-09-06 20:29:02 +02:00
|
|
|
fsm_next_state(wait_for_resume, #state{mgmt_timeout = 0} = StateData) ->
|
|
|
|
{stop, normal, StateData};
|
2014-09-06 20:34:32 +02:00
|
|
|
fsm_next_state(wait_for_resume, #state{mgmt_pending_since = undefined} =
|
|
|
|
StateData) ->
|
2014-05-27 21:14:49 +02:00
|
|
|
?INFO_MSG("Waiting for resumption of stream for ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData#state.jid)]),
|
2014-03-19 00:51:33 +01:00
|
|
|
{next_state, wait_for_resume,
|
2014-05-27 21:14:49 +02:00
|
|
|
StateData#state{mgmt_state = pending, mgmt_pending_since = os:timestamp()},
|
2014-05-06 07:27:10 +02:00
|
|
|
StateData#state.mgmt_timeout};
|
2014-03-19 00:51:33 +01:00
|
|
|
fsm_next_state(wait_for_resume, StateData) ->
|
2014-05-06 07:27:10 +02:00
|
|
|
Diff = timer:now_diff(os:timestamp(), StateData#state.mgmt_pending_since),
|
|
|
|
Timeout = max(StateData#state.mgmt_timeout - Diff div 1000, 1),
|
2014-03-19 00:51:33 +01:00
|
|
|
{next_state, wait_for_resume, StateData, Timeout};
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_next_state(StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData, ?C2S_OPEN_TIMEOUT}.
|
|
|
|
|
|
|
|
%% fsm_reply: Generate the reply FSM tuple with different timeout,
|
|
|
|
%% depending on the future state
|
|
|
|
fsm_reply(Reply, session_established, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, Reply, session_established, StateData,
|
|
|
|
?C2S_HIBERNATE_TIMEOUT};
|
2014-03-19 00:51:33 +01:00
|
|
|
fsm_reply(Reply, wait_for_resume, StateData) ->
|
2014-05-06 07:27:10 +02:00
|
|
|
Diff = timer:now_diff(os:timestamp(), StateData#state.mgmt_pending_since),
|
|
|
|
Timeout = max(StateData#state.mgmt_timeout - Diff div 1000, 1),
|
2014-03-19 00:51:33 +01:00
|
|
|
{reply, Reply, wait_for_resume, StateData, Timeout};
|
2007-09-14 16:18:01 +02:00
|
|
|
fsm_reply(Reply, StateName, StateData) ->
|
|
|
|
{reply, Reply, StateName, StateData, ?C2S_OPEN_TIMEOUT}.
|
2008-05-05 18:22:06 +02:00
|
|
|
|
|
|
|
%% Used by c2s blacklist plugins
|
2014-08-17 15:38:38 +02:00
|
|
|
is_ip_blacklisted(undefined, _Lang) -> false;
|
|
|
|
is_ip_blacklisted({IP, _Port}, Lang) ->
|
|
|
|
ejabberd_hooks:run_fold(check_bl_c2s, false, [IP, Lang]).
|
2008-12-08 10:21:36 +01:00
|
|
|
|
|
|
|
%% Check from attributes
|
|
|
|
%% returns invalid-from|NewElement
|
|
|
|
check_from(El, FromJID) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case xml:get_tag_attr(<<"from">>, El) of
|
2014-05-20 13:30:52 +02:00
|
|
|
false ->
|
|
|
|
El;
|
|
|
|
{value, SJID} ->
|
2015-11-24 16:44:13 +01:00
|
|
|
JID = jid:from_string(SJID),
|
2014-05-20 13:30:52 +02:00
|
|
|
case JID of
|
|
|
|
error ->
|
|
|
|
'invalid-from';
|
|
|
|
#jid{} ->
|
|
|
|
if
|
|
|
|
(JID#jid.luser == FromJID#jid.luser) and
|
2015-10-07 00:06:58 +02:00
|
|
|
(JID#jid.lserver == FromJID#jid.lserver) and
|
|
|
|
(JID#jid.lresource == FromJID#jid.lresource) ->
|
2014-05-20 13:30:52 +02:00
|
|
|
El;
|
|
|
|
(JID#jid.luser == FromJID#jid.luser) and
|
2015-10-07 00:06:58 +02:00
|
|
|
(JID#jid.lserver == FromJID#jid.lserver) and
|
|
|
|
(JID#jid.lresource == <<"">>) ->
|
2014-05-20 13:30:52 +02:00
|
|
|
El;
|
|
|
|
true ->
|
|
|
|
'invalid-from'
|
|
|
|
end
|
|
|
|
end
|
2008-12-08 10:21:36 +01:00
|
|
|
end.
|
2009-09-28 19:23:37 +02:00
|
|
|
|
2010-01-28 14:44:49 +01:00
|
|
|
fsm_limit_opts(Opts) ->
|
|
|
|
case lists:keysearch(max_fsm_queue, 1, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, {_, N}} when is_integer(N) -> [{max_queue, N}];
|
|
|
|
_ ->
|
2013-08-12 14:25:05 +02:00
|
|
|
case ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
max_fsm_queue,
|
|
|
|
fun(I) when is_integer(I), I > 0 -> I end) of
|
|
|
|
undefined -> [];
|
|
|
|
N -> [{max_queue, N}]
|
|
|
|
end
|
2010-01-28 14:00:04 +01:00
|
|
|
end.
|
|
|
|
|
2010-03-17 15:40:48 +01:00
|
|
|
bounce_messages() ->
|
|
|
|
receive
|
2013-03-14 10:33:02 +01:00
|
|
|
{route, From, To, El} ->
|
|
|
|
ejabberd_router:route(From, To, El), bounce_messages()
|
|
|
|
after 0 -> ok
|
2010-03-17 15:40:48 +01:00
|
|
|
end.
|
|
|
|
|
2011-05-27 11:43:52 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% XEP-0191
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
route_blocking(What, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
SubEl = case What of
|
|
|
|
{block, JIDs} ->
|
|
|
|
#xmlel{name = <<"block">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_BLOCKING}],
|
|
|
|
children =
|
|
|
|
lists:map(fun (JID) ->
|
|
|
|
#xmlel{name = <<"item">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"jid">>,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(JID)}],
|
2013-03-14 10:33:02 +01:00
|
|
|
children = []}
|
|
|
|
end,
|
|
|
|
JIDs)};
|
|
|
|
{unblock, JIDs} ->
|
|
|
|
#xmlel{name = <<"unblock">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_BLOCKING}],
|
|
|
|
children =
|
|
|
|
lists:map(fun (JID) ->
|
|
|
|
#xmlel{name = <<"item">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"jid">>,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(JID)}],
|
2013-03-14 10:33:02 +01:00
|
|
|
children = []}
|
|
|
|
end,
|
|
|
|
JIDs)};
|
|
|
|
unblock_all ->
|
|
|
|
#xmlel{name = <<"unblock">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_BLOCKING}], children = []}
|
|
|
|
end,
|
2014-05-21 00:07:57 +02:00
|
|
|
PrivPushIQ = #iq{type = set, id = <<"push">>, sub_el = [SubEl]},
|
2011-05-27 11:43:52 +02:00
|
|
|
PrivPushEl =
|
2015-11-24 16:44:13 +01:00
|
|
|
jlib:replace_from_to(jid:remove_resource(StateData#state.jid),
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.jid, jlib:iq_to_xml(PrivPushIQ)),
|
2011-05-27 11:43:52 +02:00
|
|
|
%% No need to replace active privacy list here,
|
|
|
|
%% blocking pushes are always accompanied by
|
|
|
|
%% Privacy List pushes
|
2014-03-12 23:34:14 +01:00
|
|
|
send_stanza(StateData, PrivPushEl).
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% XEP-0198
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
stream_mgmt_enabled(#state{mgmt_state = disabled}) ->
|
2014-03-12 23:34:14 +01:00
|
|
|
false;
|
|
|
|
stream_mgmt_enabled(_StateData) ->
|
|
|
|
true.
|
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
dispatch_stream_mgmt(El, StateData)
|
|
|
|
when StateData#state.mgmt_state == active;
|
|
|
|
StateData#state.mgmt_state == pending ->
|
2014-03-22 20:25:43 +01:00
|
|
|
perform_stream_mgmt(El, StateData);
|
|
|
|
dispatch_stream_mgmt(El, StateData) ->
|
|
|
|
negotiate_stream_mgmt(El, StateData).
|
|
|
|
|
2014-03-12 23:34:14 +01:00
|
|
|
negotiate_stream_mgmt(_El, #state{resource = <<"">>} = StateData) ->
|
|
|
|
%% XEP-0198 says: "For client-to-server connections, the client MUST NOT
|
|
|
|
%% attempt to enable stream management until after it has completed Resource
|
2014-03-19 00:51:33 +01:00
|
|
|
%% Binding unless it is resuming a previous session". However, it also
|
|
|
|
%% says: "Stream management errors SHOULD be considered recoverable", so we
|
|
|
|
%% won't bail out.
|
2014-05-06 07:27:10 +02:00
|
|
|
send_element(StateData, ?MGMT_UNEXPECTED_REQUEST(?NS_STREAM_MGMT_3)),
|
2014-03-12 23:34:14 +01:00
|
|
|
StateData;
|
|
|
|
negotiate_stream_mgmt(#xmlel{name = Name, attrs = Attrs}, StateData) ->
|
|
|
|
case xml:get_attr_s(<<"xmlns">>, Attrs) of
|
2014-05-06 07:27:10 +02:00
|
|
|
Xmlns when ?IS_SUPPORTED_MGMT_XMLNS(Xmlns) ->
|
2014-03-12 23:34:14 +01:00
|
|
|
case stream_mgmt_enabled(StateData) of
|
|
|
|
true ->
|
|
|
|
case Name of
|
|
|
|
<<"enable">> ->
|
2014-05-06 07:27:10 +02:00
|
|
|
handle_enable(StateData#state{mgmt_xmlns = Xmlns}, Attrs);
|
2014-03-12 23:34:14 +01:00
|
|
|
_ ->
|
|
|
|
Res = if Name == <<"a">>;
|
2014-03-19 00:51:33 +01:00
|
|
|
Name == <<"r">>;
|
|
|
|
Name == <<"resume">> ->
|
2014-05-06 07:27:10 +02:00
|
|
|
?MGMT_UNEXPECTED_REQUEST(Xmlns);
|
2014-03-12 23:34:14 +01:00
|
|
|
true ->
|
2014-05-06 07:27:10 +02:00
|
|
|
?MGMT_BAD_REQUEST(Xmlns)
|
2014-03-12 23:34:14 +01:00
|
|
|
end,
|
|
|
|
send_element(StateData, Res),
|
|
|
|
StateData
|
|
|
|
end;
|
|
|
|
false ->
|
2014-05-06 07:27:10 +02:00
|
|
|
send_element(StateData, ?MGMT_SERVICE_UNAVAILABLE(Xmlns)),
|
2014-03-12 23:34:14 +01:00
|
|
|
StateData
|
|
|
|
end;
|
|
|
|
_ ->
|
2014-05-06 07:27:10 +02:00
|
|
|
send_element(StateData, ?MGMT_UNSUPPORTED_VERSION(?NS_STREAM_MGMT_3)),
|
2014-03-12 23:34:14 +01:00
|
|
|
StateData
|
|
|
|
end.
|
|
|
|
|
|
|
|
perform_stream_mgmt(#xmlel{name = Name, attrs = Attrs}, StateData) ->
|
|
|
|
case xml:get_attr_s(<<"xmlns">>, Attrs) of
|
2014-05-06 07:27:10 +02:00
|
|
|
Xmlns when Xmlns == StateData#state.mgmt_xmlns ->
|
2014-03-12 23:34:14 +01:00
|
|
|
case Name of
|
|
|
|
<<"r">> ->
|
|
|
|
handle_r(StateData);
|
|
|
|
<<"a">> ->
|
|
|
|
handle_a(StateData, Attrs);
|
|
|
|
_ ->
|
2014-03-19 00:51:33 +01:00
|
|
|
Res = if Name == <<"enable">>;
|
|
|
|
Name == <<"resume">> ->
|
2014-05-06 07:27:10 +02:00
|
|
|
?MGMT_UNEXPECTED_REQUEST(Xmlns);
|
2014-03-12 23:34:14 +01:00
|
|
|
true ->
|
2014-05-06 07:27:10 +02:00
|
|
|
?MGMT_BAD_REQUEST(Xmlns)
|
2014-03-12 23:34:14 +01:00
|
|
|
end,
|
|
|
|
send_element(StateData, Res),
|
|
|
|
StateData
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
send_element(StateData,
|
2014-05-06 07:27:10 +02:00
|
|
|
?MGMT_UNSUPPORTED_VERSION(StateData#state.mgmt_xmlns)),
|
2014-03-12 23:34:14 +01:00
|
|
|
StateData
|
|
|
|
end.
|
|
|
|
|
2015-09-03 21:20:15 +02:00
|
|
|
handle_enable(#state{mgmt_timeout = DefaultTimeout,
|
|
|
|
mgmt_max_timeout = MaxTimeout} = StateData, Attrs) ->
|
2014-03-19 00:51:33 +01:00
|
|
|
Timeout = case xml:get_attr_s(<<"resume">>, Attrs) of
|
|
|
|
ResumeAttr when ResumeAttr == <<"true">>;
|
|
|
|
ResumeAttr == <<"1">> ->
|
|
|
|
MaxAttr = xml:get_attr_s(<<"max">>, Attrs),
|
|
|
|
case catch jlib:binary_to_integer(MaxAttr) of
|
2015-09-03 21:20:15 +02:00
|
|
|
Max when is_integer(Max), Max > 0, Max =< MaxTimeout ->
|
2014-03-19 00:51:33 +01:00
|
|
|
Max;
|
|
|
|
_ ->
|
2015-09-03 21:20:15 +02:00
|
|
|
DefaultTimeout
|
2014-03-19 00:51:33 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
0
|
|
|
|
end,
|
2014-05-06 07:27:10 +02:00
|
|
|
ResAttrs = [{<<"xmlns">>, StateData#state.mgmt_xmlns}] ++
|
2014-03-19 00:51:33 +01:00
|
|
|
if Timeout > 0 ->
|
|
|
|
?INFO_MSG("Stream management with resumption enabled for ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData#state.jid)]),
|
2014-03-19 00:51:33 +01:00
|
|
|
[{<<"id">>, make_resume_id(StateData)},
|
|
|
|
{<<"resume">>, <<"true">>},
|
|
|
|
{<<"max">>, jlib:integer_to_binary(Timeout)}];
|
|
|
|
true ->
|
|
|
|
?INFO_MSG("Stream management without resumption enabled for ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData#state.jid)]),
|
2014-03-19 00:51:33 +01:00
|
|
|
[]
|
|
|
|
end,
|
2014-03-12 23:34:14 +01:00
|
|
|
Res = #xmlel{name = <<"enabled">>,
|
2014-03-19 00:51:33 +01:00
|
|
|
attrs = ResAttrs,
|
2014-03-12 23:34:14 +01:00
|
|
|
children = []},
|
|
|
|
send_element(StateData, Res),
|
2014-05-06 07:27:10 +02:00
|
|
|
StateData#state{mgmt_state = active,
|
|
|
|
mgmt_queue = queue:new(),
|
|
|
|
mgmt_timeout = Timeout * 1000}.
|
2014-03-12 23:34:14 +01:00
|
|
|
|
|
|
|
handle_r(StateData) ->
|
2014-05-06 07:27:10 +02:00
|
|
|
H = jlib:integer_to_binary(StateData#state.mgmt_stanzas_in),
|
2014-03-12 23:34:14 +01:00
|
|
|
Res = #xmlel{name = <<"a">>,
|
2014-05-06 07:27:10 +02:00
|
|
|
attrs = [{<<"xmlns">>, StateData#state.mgmt_xmlns},
|
2014-03-12 23:34:14 +01:00
|
|
|
{<<"h">>, H}],
|
|
|
|
children = []},
|
|
|
|
send_element(StateData, Res),
|
|
|
|
StateData.
|
|
|
|
|
2014-05-09 18:28:14 +02:00
|
|
|
handle_a(StateData, Attrs) ->
|
2014-03-12 23:34:14 +01:00
|
|
|
case catch jlib:binary_to_integer(xml:get_attr_s(<<"h">>, Attrs)) of
|
|
|
|
H when is_integer(H), H >= 0 ->
|
2014-05-09 18:28:14 +02:00
|
|
|
check_h_attribute(StateData, H);
|
2014-03-12 23:34:14 +01:00
|
|
|
_ ->
|
2014-05-09 17:54:12 +02:00
|
|
|
?DEBUG("Ignoring invalid ACK element from ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData#state.jid)]),
|
2014-03-12 23:34:14 +01:00
|
|
|
StateData
|
|
|
|
end.
|
|
|
|
|
2014-03-19 00:51:33 +01:00
|
|
|
handle_resume(StateData, Attrs) ->
|
|
|
|
R = case xml:get_attr_s(<<"xmlns">>, Attrs) of
|
2014-05-06 07:27:10 +02:00
|
|
|
Xmlns when ?IS_SUPPORTED_MGMT_XMLNS(Xmlns) ->
|
2014-03-19 00:51:33 +01:00
|
|
|
case stream_mgmt_enabled(StateData) of
|
|
|
|
true ->
|
|
|
|
case {xml:get_attr(<<"previd">>, Attrs),
|
|
|
|
catch jlib:binary_to_integer(xml:get_attr_s(<<"h">>, Attrs))}
|
|
|
|
of
|
2014-05-09 18:01:31 +02:00
|
|
|
{{value, PrevID}, H} when is_integer(H), H >= 0 ->
|
2014-03-19 00:51:33 +01:00
|
|
|
case inherit_session_state(StateData, PrevID) of
|
|
|
|
{ok, InheritedState} ->
|
|
|
|
{ok, InheritedState, H};
|
|
|
|
{error, Err} ->
|
2014-05-06 07:27:10 +02:00
|
|
|
{error, ?MGMT_ITEM_NOT_FOUND(Xmlns), Err}
|
2014-03-19 00:51:33 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2014-05-06 07:27:10 +02:00
|
|
|
{error, ?MGMT_BAD_REQUEST(Xmlns),
|
|
|
|
<<"Invalid request">>}
|
2014-03-19 00:51:33 +01:00
|
|
|
end;
|
|
|
|
false ->
|
2014-05-06 07:27:10 +02:00
|
|
|
{error, ?MGMT_SERVICE_UNAVAILABLE(Xmlns),
|
|
|
|
<<"XEP-0198 disabled">>}
|
2014-03-19 00:51:33 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2014-05-06 07:27:10 +02:00
|
|
|
{error, ?MGMT_UNSUPPORTED_VERSION(?NS_STREAM_MGMT_3),
|
|
|
|
<<"Invalid XMLNS">>}
|
2014-03-19 00:51:33 +01:00
|
|
|
end,
|
|
|
|
case R of
|
|
|
|
{ok, ResumedState, NumHandled} ->
|
2014-05-09 18:28:14 +02:00
|
|
|
NewState = check_h_attribute(ResumedState, NumHandled),
|
2014-05-06 07:27:10 +02:00
|
|
|
AttrXmlns = NewState#state.mgmt_xmlns,
|
2014-03-19 00:51:33 +01:00
|
|
|
AttrId = make_resume_id(NewState),
|
2014-05-06 07:27:10 +02:00
|
|
|
AttrH = jlib:integer_to_binary(NewState#state.mgmt_stanzas_in),
|
2014-03-19 00:51:33 +01:00
|
|
|
send_element(NewState,
|
|
|
|
#xmlel{name = <<"resumed">>,
|
|
|
|
attrs = [{<<"xmlns">>, AttrXmlns},
|
|
|
|
{<<"h">>, AttrH},
|
|
|
|
{<<"previd">>, AttrId}],
|
|
|
|
children = []}),
|
2014-12-07 16:12:06 +01:00
|
|
|
SendFun = fun(_F, _T, El, Time) ->
|
|
|
|
NewEl = add_resent_delay_info(NewState, El, Time),
|
|
|
|
send_element(NewState, NewEl)
|
|
|
|
end,
|
2014-03-25 23:23:38 +01:00
|
|
|
handle_unacked_stanzas(NewState, SendFun),
|
2014-03-19 00:51:33 +01:00
|
|
|
send_element(NewState,
|
|
|
|
#xmlel{name = <<"r">>,
|
|
|
|
attrs = [{<<"xmlns">>, AttrXmlns}],
|
|
|
|
children = []}),
|
2014-09-11 17:44:29 +02:00
|
|
|
FlushedState = csi_queue_flush(NewState),
|
|
|
|
NewStateData = FlushedState#state{csi_state = active},
|
2014-03-19 00:51:33 +01:00
|
|
|
?INFO_MSG("Resumed session for ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(NewStateData#state.jid)]),
|
2014-09-11 17:44:29 +02:00
|
|
|
{ok, NewStateData};
|
2014-03-19 00:51:33 +01:00
|
|
|
{error, El, Msg} ->
|
|
|
|
send_element(StateData, El),
|
2014-04-28 01:01:30 +02:00
|
|
|
?INFO_MSG("Cannot resume session for ~s@~s: ~s",
|
|
|
|
[StateData#state.user, StateData#state.server, Msg]),
|
2014-03-19 00:51:33 +01:00
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
2014-05-09 18:28:14 +02:00
|
|
|
check_h_attribute(#state{mgmt_stanzas_out = NumStanzasOut} = StateData, H)
|
|
|
|
when H > NumStanzasOut ->
|
|
|
|
?DEBUG("~s acknowledged ~B stanzas, but only ~B were sent",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData#state.jid), H, NumStanzasOut]),
|
2014-05-09 18:28:14 +02:00
|
|
|
mgmt_queue_drop(StateData#state{mgmt_stanzas_out = H}, NumStanzasOut);
|
|
|
|
check_h_attribute(#state{mgmt_stanzas_out = NumStanzasOut} = StateData, H) ->
|
|
|
|
?DEBUG("~s acknowledged ~B of ~B stanzas",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData#state.jid), H, NumStanzasOut]),
|
2014-05-09 18:28:14 +02:00
|
|
|
mgmt_queue_drop(StateData, H).
|
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
update_num_stanzas_in(#state{mgmt_state = active} = StateData, El) ->
|
|
|
|
NewNum = case {is_stanza(El), StateData#state.mgmt_stanzas_in} of
|
2014-03-12 23:34:14 +01:00
|
|
|
{true, 4294967295} ->
|
|
|
|
0;
|
|
|
|
{true, Num} ->
|
|
|
|
Num + 1;
|
|
|
|
{false, Num} ->
|
|
|
|
Num
|
|
|
|
end,
|
2014-05-06 07:27:10 +02:00
|
|
|
StateData#state{mgmt_stanzas_in = NewNum};
|
2014-03-12 23:34:14 +01:00
|
|
|
update_num_stanzas_in(StateData, _El) ->
|
|
|
|
StateData.
|
|
|
|
|
|
|
|
send_stanza_and_ack_req(StateData, Stanza) ->
|
|
|
|
AckReq = #xmlel{name = <<"r">>,
|
2014-05-06 07:27:10 +02:00
|
|
|
attrs = [{<<"xmlns">>, StateData#state.mgmt_xmlns}],
|
2014-03-12 23:34:14 +01:00
|
|
|
children = []},
|
2015-05-14 01:04:23 +02:00
|
|
|
case send_element(StateData, Stanza) == ok andalso
|
|
|
|
send_element(StateData, AckReq) == ok of
|
|
|
|
true ->
|
|
|
|
StateData;
|
|
|
|
false ->
|
|
|
|
StateData#state{mgmt_state = pending}
|
2015-04-06 22:34:48 +02:00
|
|
|
end.
|
2014-03-12 23:34:14 +01:00
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
mgmt_queue_add(StateData, El) ->
|
|
|
|
NewNum = case StateData#state.mgmt_stanzas_out of
|
2014-03-12 23:34:14 +01:00
|
|
|
4294967295 ->
|
|
|
|
0;
|
|
|
|
Num ->
|
|
|
|
Num + 1
|
|
|
|
end,
|
2015-12-07 16:08:57 +01:00
|
|
|
NewQueue = queue:in({NewNum, p1_time_compat:timestamp(), El}, StateData#state.mgmt_queue),
|
2014-05-27 22:56:33 +02:00
|
|
|
NewState = StateData#state{mgmt_queue = NewQueue,
|
|
|
|
mgmt_stanzas_out = NewNum},
|
|
|
|
check_queue_length(NewState).
|
2014-03-12 23:34:14 +01:00
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
mgmt_queue_drop(StateData, NumHandled) ->
|
2014-12-07 16:12:06 +01:00
|
|
|
NewQueue = jlib:queue_drop_while(fun({N, _T, _E}) -> N =< NumHandled end,
|
2014-05-06 07:27:10 +02:00
|
|
|
StateData#state.mgmt_queue),
|
|
|
|
StateData#state{mgmt_queue = NewQueue}.
|
2014-03-12 23:34:14 +01:00
|
|
|
|
2014-10-17 01:35:30 +02:00
|
|
|
check_queue_length(#state{mgmt_max_queue = Limit} = StateData)
|
|
|
|
when Limit == infinity;
|
|
|
|
Limit == exceeded ->
|
2014-03-12 23:34:14 +01:00
|
|
|
StateData;
|
2014-05-27 22:56:33 +02:00
|
|
|
check_queue_length(#state{mgmt_queue = Queue,
|
2014-05-06 07:27:10 +02:00
|
|
|
mgmt_max_queue = Limit} = StateData) ->
|
2014-05-27 22:56:33 +02:00
|
|
|
case queue:len(Queue) > Limit of
|
2014-03-12 23:34:14 +01:00
|
|
|
true ->
|
2014-10-17 01:35:30 +02:00
|
|
|
StateData#state{mgmt_max_queue = exceeded};
|
2014-03-12 23:34:14 +01:00
|
|
|
false ->
|
|
|
|
StateData
|
|
|
|
end.
|
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
handle_unacked_stanzas(StateData, F)
|
|
|
|
when StateData#state.mgmt_state == active;
|
|
|
|
StateData#state.mgmt_state == pending ->
|
|
|
|
Queue = StateData#state.mgmt_queue,
|
2014-03-12 23:34:14 +01:00
|
|
|
case queue:len(Queue) of
|
|
|
|
0 ->
|
|
|
|
ok;
|
|
|
|
N ->
|
2014-03-25 23:23:38 +01:00
|
|
|
?INFO_MSG("~B stanzas were not acknowledged by ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[N, jid:to_string(StateData#state.jid)]),
|
2014-03-12 23:34:14 +01:00
|
|
|
lists:foreach(
|
2014-12-07 16:12:06 +01:00
|
|
|
fun({_, Time, #xmlel{attrs = Attrs} = El}) ->
|
2014-03-12 23:34:14 +01:00
|
|
|
From_s = xml:get_attr_s(<<"from">>, Attrs),
|
2015-11-24 16:44:13 +01:00
|
|
|
From = jid:from_string(From_s),
|
2014-03-12 23:34:14 +01:00
|
|
|
To_s = xml:get_attr_s(<<"to">>, Attrs),
|
2015-11-24 16:44:13 +01:00
|
|
|
To = jid:from_string(To_s),
|
2014-12-07 16:12:06 +01:00
|
|
|
F(From, To, El, Time)
|
2014-03-12 23:34:14 +01:00
|
|
|
end, queue:to_list(Queue))
|
|
|
|
end;
|
2014-03-25 23:23:38 +01:00
|
|
|
handle_unacked_stanzas(_StateData, _F) ->
|
2014-03-19 00:51:33 +01:00
|
|
|
ok.
|
|
|
|
|
2014-05-06 07:27:10 +02:00
|
|
|
handle_unacked_stanzas(StateData)
|
|
|
|
when StateData#state.mgmt_state == active;
|
|
|
|
StateData#state.mgmt_state == pending ->
|
2014-09-16 22:42:34 +02:00
|
|
|
ResendOnTimeout =
|
|
|
|
case StateData#state.mgmt_resend of
|
|
|
|
Resend when is_boolean(Resend) ->
|
|
|
|
Resend;
|
|
|
|
if_offline ->
|
|
|
|
ejabberd_sm:get_user_resources(StateData#state.user,
|
|
|
|
StateData#state.server) == []
|
|
|
|
end,
|
|
|
|
ReRoute = case ResendOnTimeout of
|
2014-04-07 21:21:11 +02:00
|
|
|
true ->
|
2014-12-07 16:12:06 +01:00
|
|
|
fun(From, To, El, Time) ->
|
|
|
|
NewEl = add_resent_delay_info(StateData, El, Time),
|
|
|
|
ejabberd_router:route(From, To, NewEl)
|
|
|
|
end;
|
2014-04-07 21:21:11 +02:00
|
|
|
false ->
|
2014-12-07 16:12:06 +01:00
|
|
|
fun(From, To, El, _Time) ->
|
2014-04-07 21:21:11 +02:00
|
|
|
Err =
|
|
|
|
jlib:make_error_reply(El,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end
|
|
|
|
end,
|
2015-09-15 22:42:22 +02:00
|
|
|
F = fun(From, _To, #xmlel{name = <<"presence">>}, _Time) ->
|
|
|
|
?DEBUG("Dropping presence stanza from ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(From)]);
|
2015-09-15 22:42:22 +02:00
|
|
|
(From, To, #xmlel{name = <<"iq">>} = El, _Time) ->
|
|
|
|
Err = jlib:make_error_reply(El, ?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
(From, To, El, Time) ->
|
2014-04-07 21:21:11 +02:00
|
|
|
%% We'll drop the stanza if it was <forwarded/> by some
|
|
|
|
%% encapsulating protocol as per XEP-0297. One such protocol is
|
|
|
|
%% XEP-0280, which says: "When a receiving server attempts to
|
|
|
|
%% deliver a forked message, and that message bounces with an
|
|
|
|
%% error for any reason, the receiving server MUST NOT forward
|
|
|
|
%% that error back to the original sender." Resending such a
|
|
|
|
%% stanza could easily lead to unexpected results as well.
|
|
|
|
case is_encapsulated_forward(El) of
|
|
|
|
true ->
|
2015-09-15 22:42:22 +02:00
|
|
|
?DEBUG("Dropping forwarded message stanza from ~s",
|
2014-04-07 21:21:11 +02:00
|
|
|
[xml:get_attr_s(<<"from">>, El#xmlel.attrs)]);
|
|
|
|
false ->
|
2016-01-15 01:08:22 +01:00
|
|
|
case ejabberd_hooks:run_fold(message_is_archived,
|
|
|
|
StateData#state.server,
|
|
|
|
false,
|
|
|
|
[StateData, From,
|
|
|
|
StateData#state.jid, El]) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
ReRoute(From, To, El, Time)
|
|
|
|
end
|
2014-04-07 21:21:11 +02:00
|
|
|
end
|
2014-03-25 23:23:38 +01:00
|
|
|
end,
|
|
|
|
handle_unacked_stanzas(StateData, F);
|
|
|
|
handle_unacked_stanzas(_StateData) ->
|
2011-05-27 11:43:52 +02:00
|
|
|
ok.
|
|
|
|
|
2014-04-07 21:21:11 +02:00
|
|
|
is_encapsulated_forward(#xmlel{name = <<"message">>} = El) ->
|
|
|
|
SubTag = case {xml:get_subtag(El, <<"sent">>),
|
|
|
|
xml:get_subtag(El, <<"received">>),
|
|
|
|
xml:get_subtag(El, <<"result">>)} of
|
|
|
|
{false, false, false} ->
|
|
|
|
false;
|
|
|
|
{Tag, false, false} ->
|
|
|
|
Tag;
|
|
|
|
{false, Tag, false} ->
|
|
|
|
Tag;
|
|
|
|
{_, _, Tag} ->
|
|
|
|
Tag
|
|
|
|
end,
|
|
|
|
if SubTag == false ->
|
|
|
|
false;
|
|
|
|
true ->
|
|
|
|
case xml:get_subtag(SubTag, <<"forwarded">>) of
|
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
is_encapsulated_forward(_El) ->
|
|
|
|
false.
|
|
|
|
|
2014-03-19 00:51:33 +01:00
|
|
|
inherit_session_state(#state{user = U, server = S} = StateData, ResumeID) ->
|
|
|
|
case jlib:base64_to_term(ResumeID) of
|
2014-08-15 10:56:59 +02:00
|
|
|
{term, {R, Time}} ->
|
2014-03-19 00:51:33 +01:00
|
|
|
case ejabberd_sm:get_session_pid(U, S, R) of
|
|
|
|
none ->
|
|
|
|
{error, <<"Previous session PID not found">>};
|
|
|
|
OldPID ->
|
|
|
|
OldSID = {Time, OldPID},
|
2014-05-23 23:38:04 +02:00
|
|
|
case catch resume_session(OldSID) of
|
|
|
|
{ok, OldStateData} ->
|
2014-03-19 00:51:33 +01:00
|
|
|
NewSID = {Time, self()}, % Old time, new PID
|
|
|
|
Priority = case OldStateData#state.pres_last of
|
|
|
|
undefined ->
|
|
|
|
0;
|
|
|
|
Presence ->
|
|
|
|
get_priority_from_presence(Presence)
|
|
|
|
end,
|
|
|
|
Conn = get_conn_type(StateData),
|
|
|
|
Info = [{ip, StateData#state.ip}, {conn, Conn},
|
|
|
|
{auth_module, StateData#state.auth_module}],
|
|
|
|
ejabberd_sm:open_session(NewSID, U, S, R,
|
|
|
|
Priority, Info),
|
2014-10-12 19:44:35 +02:00
|
|
|
{ok, StateData#state{conn = Conn,
|
|
|
|
sid = NewSID,
|
2014-03-19 00:51:33 +01:00
|
|
|
jid = OldStateData#state.jid,
|
|
|
|
resource = OldStateData#state.resource,
|
|
|
|
pres_t = OldStateData#state.pres_t,
|
|
|
|
pres_f = OldStateData#state.pres_f,
|
|
|
|
pres_a = OldStateData#state.pres_a,
|
|
|
|
pres_last = OldStateData#state.pres_last,
|
|
|
|
pres_timestamp = OldStateData#state.pres_timestamp,
|
|
|
|
privacy_list = OldStateData#state.privacy_list,
|
|
|
|
aux_fields = OldStateData#state.aux_fields,
|
2014-09-11 17:44:29 +02:00
|
|
|
csi_state = OldStateData#state.csi_state,
|
|
|
|
csi_queue = OldStateData#state.csi_queue,
|
2014-05-06 07:27:10 +02:00
|
|
|
mgmt_xmlns = OldStateData#state.mgmt_xmlns,
|
|
|
|
mgmt_queue = OldStateData#state.mgmt_queue,
|
|
|
|
mgmt_timeout = OldStateData#state.mgmt_timeout,
|
|
|
|
mgmt_stanzas_in = OldStateData#state.mgmt_stanzas_in,
|
|
|
|
mgmt_stanzas_out = OldStateData#state.mgmt_stanzas_out,
|
|
|
|
mgmt_state = active}};
|
2014-05-23 23:38:04 +02:00
|
|
|
{error, Msg} ->
|
|
|
|
{error, Msg};
|
2014-03-19 00:51:33 +01:00
|
|
|
_ ->
|
|
|
|
{error, <<"Cannot grab session state">>}
|
|
|
|
end
|
|
|
|
end;
|
2014-08-15 01:54:41 +02:00
|
|
|
_ ->
|
2014-03-19 00:51:33 +01:00
|
|
|
{error, <<"Invalid 'previd' value">>}
|
|
|
|
end.
|
|
|
|
|
2014-05-23 23:38:04 +02:00
|
|
|
resume_session({Time, PID}) ->
|
2015-07-01 21:37:06 +02:00
|
|
|
(?GEN_FSM):sync_send_all_state_event(PID, {resume_session, Time}, 5000).
|
2014-03-19 00:51:33 +01:00
|
|
|
|
|
|
|
make_resume_id(StateData) ->
|
|
|
|
{Time, _} = StateData#state.sid,
|
2014-08-15 10:56:59 +02:00
|
|
|
jlib:term_to_base64({StateData#state.resource, Time}).
|
2014-03-19 00:51:33 +01:00
|
|
|
|
2015-07-28 21:00:12 +02:00
|
|
|
add_resent_delay_info(_State, #xmlel{name = <<"iq">>} = El, _Time) ->
|
|
|
|
El;
|
2014-12-07 16:12:06 +01:00
|
|
|
add_resent_delay_info(#state{server = From}, El, Time) ->
|
|
|
|
jlib:add_delay_info(El, From, Time, <<"Resent">>).
|
|
|
|
|
2014-09-11 17:44:29 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% XEP-0352
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
csi_filter_stanza(#state{csi_state = CsiState, jid = JID} = StateData,
|
|
|
|
Stanza) ->
|
|
|
|
Action = ejabberd_hooks:run_fold(csi_filter_stanza,
|
|
|
|
StateData#state.server,
|
|
|
|
send, [Stanza]),
|
|
|
|
?DEBUG("Going to ~p stanza for inactive client ~p",
|
2015-11-24 16:44:13 +01:00
|
|
|
[Action, jid:to_string(JID)]),
|
2014-09-11 17:44:29 +02:00
|
|
|
case Action of
|
|
|
|
queue -> csi_queue_add(StateData, Stanza);
|
|
|
|
drop -> StateData;
|
|
|
|
send ->
|
|
|
|
From = xml:get_tag_attr_s(<<"from">>, Stanza),
|
|
|
|
StateData1 = csi_queue_send(StateData, From),
|
|
|
|
StateData2 = send_stanza(StateData1#state{csi_state = active},
|
|
|
|
Stanza),
|
|
|
|
StateData2#state{csi_state = CsiState}
|
|
|
|
end.
|
|
|
|
|
2014-12-07 16:27:51 +01:00
|
|
|
csi_queue_add(#state{csi_queue = Queue} = StateData, Stanza) ->
|
2014-09-11 17:44:29 +02:00
|
|
|
case length(StateData#state.csi_queue) >= csi_max_queue(StateData) of
|
2014-12-07 16:27:51 +01:00
|
|
|
true -> csi_queue_add(csi_queue_flush(StateData), Stanza);
|
2014-09-11 17:44:29 +02:00
|
|
|
false ->
|
2014-12-07 16:27:51 +01:00
|
|
|
From = xml:get_tag_attr_s(<<"from">>, Stanza),
|
2015-12-07 16:08:57 +01:00
|
|
|
NewQueue = lists:keystore(From, 1, Queue, {From, p1_time_compat:timestamp(), Stanza}),
|
2014-09-11 17:44:29 +02:00
|
|
|
StateData#state{csi_queue = NewQueue}
|
|
|
|
end.
|
|
|
|
|
2014-12-07 16:27:51 +01:00
|
|
|
csi_queue_send(#state{csi_queue = Queue, csi_state = CsiState, server = Host} =
|
|
|
|
StateData, From) ->
|
2014-09-11 17:44:29 +02:00
|
|
|
case lists:keytake(From, 1, Queue) of
|
2014-12-07 16:27:51 +01:00
|
|
|
{value, {From, Time, Stanza}, NewQueue} ->
|
|
|
|
NewStanza = jlib:add_delay_info(Stanza, Host, Time,
|
|
|
|
<<"Client Inactive">>),
|
2014-09-11 17:44:29 +02:00
|
|
|
NewStateData = send_stanza(StateData#state{csi_state = active},
|
2014-12-07 16:27:51 +01:00
|
|
|
NewStanza),
|
2014-09-11 17:44:29 +02:00
|
|
|
NewStateData#state{csi_queue = NewQueue, csi_state = CsiState};
|
|
|
|
false -> StateData
|
|
|
|
end.
|
|
|
|
|
2014-12-07 16:27:51 +01:00
|
|
|
csi_queue_flush(#state{csi_queue = Queue, csi_state = CsiState, jid = JID,
|
|
|
|
server = Host} = StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
?DEBUG("Flushing CSI queue for ~s", [jid:to_string(JID)]),
|
2014-09-11 17:44:29 +02:00
|
|
|
NewStateData =
|
2014-12-07 16:27:51 +01:00
|
|
|
lists:foldl(fun({_From, Time, Stanza}, AccState) ->
|
|
|
|
NewStanza =
|
|
|
|
jlib:add_delay_info(Stanza, Host, Time,
|
|
|
|
<<"Client Inactive">>),
|
|
|
|
send_stanza(AccState, NewStanza)
|
2014-09-11 17:44:29 +02:00
|
|
|
end, StateData#state{csi_state = active}, Queue),
|
|
|
|
NewStateData#state{csi_queue = [], csi_state = CsiState}.
|
|
|
|
|
|
|
|
%% Make sure we won't push too many messages to the XEP-0198 queue when the
|
|
|
|
%% client becomes 'active' again. Otherwise, the client might not manage to
|
|
|
|
%% acknowledge the message flood in time. Also, don't let the queue grow to
|
|
|
|
%% more than 100 stanzas.
|
|
|
|
csi_max_queue(#state{mgmt_max_queue = infinity}) -> 100;
|
|
|
|
csi_max_queue(#state{mgmt_max_queue = Max}) when Max > 200 -> 100;
|
|
|
|
csi_max_queue(#state{mgmt_max_queue = Max}) when Max < 2 -> 1;
|
|
|
|
csi_max_queue(#state{mgmt_max_queue = Max}) -> Max div 2.
|
|
|
|
|
2009-09-28 19:23:37 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% JID Set memory footprint reduction code
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%% Try to reduce the heap footprint of the four presence sets
|
|
|
|
%% by ensuring that we re-use strings and Jids wherever possible.
|
2013-03-14 10:33:02 +01:00
|
|
|
pack(S = #state{pres_a = A, pres_f = F,
|
|
|
|
pres_t = T}) ->
|
|
|
|
{NewA, Pack2} = pack_jid_set(A, gb_trees:empty()),
|
2009-09-28 19:23:37 +02:00
|
|
|
{NewF, Pack3} = pack_jid_set(F, Pack2),
|
|
|
|
{NewT, _Pack4} = pack_jid_set(T, Pack3),
|
2013-03-14 10:33:02 +01:00
|
|
|
S#state{pres_a = NewA, pres_f = NewF,
|
|
|
|
pres_t = NewT}.
|
2009-09-28 19:23:37 +02:00
|
|
|
|
|
|
|
pack_jid_set(Set, Pack) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Jids = (?SETS):to_list(Set),
|
2009-09-28 19:23:37 +02:00
|
|
|
{PackedJids, NewPack} = pack_jids(Jids, Pack, []),
|
2013-03-14 10:33:02 +01:00
|
|
|
{(?SETS):from_list(PackedJids), NewPack}.
|
2009-09-28 19:23:37 +02:00
|
|
|
|
|
|
|
pack_jids([], Pack, Acc) -> {Acc, Pack};
|
2013-03-14 10:33:02 +01:00
|
|
|
pack_jids([{U, S, R} = Jid | Jids], Pack, Acc) ->
|
2009-09-28 19:23:37 +02:00
|
|
|
case gb_trees:lookup(Jid, Pack) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, PackedJid} ->
|
|
|
|
pack_jids(Jids, Pack, [PackedJid | Acc]);
|
|
|
|
none ->
|
|
|
|
{NewU, Pack1} = pack_string(U, Pack),
|
|
|
|
{NewS, Pack2} = pack_string(S, Pack1),
|
|
|
|
{NewR, Pack3} = pack_string(R, Pack2),
|
|
|
|
NewJid = {NewU, NewS, NewR},
|
|
|
|
NewPack = gb_trees:insert(NewJid, NewJid, Pack3),
|
|
|
|
pack_jids(Jids, NewPack, [NewJid | Acc])
|
2009-09-28 19:23:37 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
pack_string(String, Pack) ->
|
|
|
|
case gb_trees:lookup(String, Pack) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, PackedString} -> {PackedString, Pack};
|
|
|
|
none -> {String, gb_trees:insert(String, String, Pack)}
|
2009-09-28 19:23:37 +02:00
|
|
|
end.
|
2013-08-12 14:25:05 +02:00
|
|
|
|
|
|
|
transform_listen_option(Opt, Opts) ->
|
|
|
|
[Opt|Opts].
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
opt_type(domain_certfile) -> fun iolist_to_binary/1;
|
|
|
|
opt_type(max_fsm_queue) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
|
|
|
opt_type(resource_conflict) ->
|
|
|
|
fun (setresource) -> setresource;
|
|
|
|
(closeold) -> closeold;
|
|
|
|
(closenew) -> closenew;
|
|
|
|
(acceptnew) -> acceptnew
|
|
|
|
end;
|
|
|
|
opt_type(_) ->
|
|
|
|
[domain_certfile, max_fsm_queue, resource_conflict].
|