2016-12-11 13:03:37 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% Created : 8 Dec 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
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
|
|
|
%%%
|
2016-12-11 13:03:37 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
2002-11-18 21:39:47 +01:00
|
|
|
-module(ejabberd_c2s).
|
2016-12-11 13:03:37 +01:00
|
|
|
-behaviour(xmpp_stream_in).
|
2016-12-11 16:24:51 +01:00
|
|
|
-behaviour(ejabberd_config).
|
2016-12-28 07:47:11 +01:00
|
|
|
-behaviour(ejabberd_socket).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
|
|
|
-protocol({rfc, 6121}).
|
|
|
|
|
|
|
|
%% ejabberd_socket callbacks
|
2016-12-28 07:47:11 +01:00
|
|
|
-export([start/2, start_link/2, socket_type/0]).
|
2016-12-11 16:24:51 +01:00
|
|
|
%% ejabberd_config callbacks
|
|
|
|
-export([opt_type/1, transform_listen_option/2]).
|
2016-12-11 13:03:37 +01:00
|
|
|
%% xmpp_stream_in callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
|
|
|
handle_info/2, terminate/2, code_change/3]).
|
2017-01-09 15:02:17 +01:00
|
|
|
-export([tls_options/1, tls_required/1, tls_verify/1, tls_enabled/1,
|
|
|
|
compress_methods/1, bind/2, sasl_mechanisms/2,
|
|
|
|
get_password_fun/1, check_password_fun/1, check_password_digest_fun/1,
|
2016-12-11 13:03:37 +01:00
|
|
|
unauthenticated_stream_features/1, authenticated_stream_features/1,
|
2016-12-29 22:00:36 +01:00
|
|
|
handle_stream_start/2, handle_stream_end/2,
|
2016-12-11 13:03:37 +01:00
|
|
|
handle_unauthenticated_packet/2, handle_authenticated_packet/2,
|
2016-12-28 07:47:11 +01:00
|
|
|
handle_auth_success/4, handle_auth_failure/4, handle_send/3,
|
|
|
|
handle_recv/3, handle_cdata/2, handle_unbinded_packet/2]).
|
|
|
|
%% Hooks
|
2016-12-29 22:00:36 +01:00
|
|
|
-export([handle_unexpected_cast/2,
|
|
|
|
reject_unauthenticated_packet/2, process_closed/2,
|
|
|
|
process_terminated/2, process_info/2]).
|
2016-12-11 13:03:37 +01:00
|
|
|
%% API
|
|
|
|
-export([get_presence/1, get_subscription/2, get_subscribed/1,
|
2017-01-09 15:02:17 +01:00
|
|
|
open_session/1, call/3, send/2, close/1, close/2, stop/1,
|
2016-12-29 22:00:36 +01:00
|
|
|
reply/2, copy_state/2, set_timeout/2, add_hooks/1]).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2016-07-18 14:01:32 +02:00
|
|
|
-include("xmpp.hrl").
|
2016-12-11 13:03:37 +01:00
|
|
|
-include("logger.hrl").
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
-define(SETS, gb_sets).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-type state() :: map().
|
2016-12-28 07:47:11 +01:00
|
|
|
-export_type([state/0]).
|
2002-11-19 21:58:49 +01:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% ejabberd_socket API
|
|
|
|
%%%===================================================================
|
2003-07-20 22:35:35 +02:00
|
|
|
start(SockData, Opts) ->
|
2017-01-11 08:56:04 +01:00
|
|
|
case proplists:get_value(supervisor, Opts, true) of
|
|
|
|
true ->
|
|
|
|
supervisor:start_child(ejabberd_c2s_sup, [SockData, Opts]);
|
|
|
|
_ ->
|
|
|
|
xmpp_stream_in:start(?MODULE, [SockData, Opts],
|
|
|
|
ejabberd_config:fsm_limit_opts(Opts))
|
|
|
|
end.
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
start_link(SockData, Opts) ->
|
|
|
|
xmpp_stream_in:start_link(?MODULE, [SockData, Opts],
|
|
|
|
ejabberd_config:fsm_limit_opts(Opts)).
|
2006-09-25 05:51:11 +02:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
socket_type() ->
|
|
|
|
xml_stream.
|
2004-03-02 22:16:55 +01:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Common API
|
|
|
|
%%%===================================================================
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec call(pid(), term(), non_neg_integer() | infinity) -> term().
|
|
|
|
call(Ref, Msg, Timeout) ->
|
|
|
|
xmpp_stream_in:call(Ref, Msg, Timeout).
|
|
|
|
|
2016-12-29 22:00:36 +01:00
|
|
|
reply(Ref, Reply) ->
|
|
|
|
xmpp_stream_in:reply(Ref, Reply).
|
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec get_presence(pid()) -> presence().
|
|
|
|
get_presence(Ref) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
call(Ref, get_presence, 1000).
|
2010-09-28 06:18:57 +02:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec get_subscription(jid() | ljid(), state()) -> both | from | to | none.
|
2016-12-11 13:03:37 +01:00
|
|
|
get_subscription(#jid{} = From, State) ->
|
|
|
|
get_subscription(jid:tolower(From), State);
|
|
|
|
get_subscription(LFrom, #{pres_f := PresF, pres_t := PresT}) ->
|
|
|
|
LBFrom = jid:remove_resource(LFrom),
|
|
|
|
F = ?SETS:is_element(LFrom, PresF) orelse ?SETS:is_element(LBFrom, PresF),
|
|
|
|
T = ?SETS:is_element(LFrom, PresT) orelse ?SETS:is_element(LBFrom, PresT),
|
2010-09-28 06:18:57 +02:00
|
|
|
if F and T -> both;
|
|
|
|
F -> from;
|
|
|
|
T -> to;
|
|
|
|
true -> none
|
|
|
|
end.
|
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec get_subscribed(pid()) -> [ljid()].
|
2016-12-11 13:03:37 +01:00
|
|
|
%% Return list of all available resources of contacts
|
|
|
|
get_subscribed(Ref) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
call(Ref, get_subscribed, 1000).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
|
|
|
close(Ref) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
xmpp_stream_in:close(Ref).
|
|
|
|
|
|
|
|
close(Ref, SendTrailer) ->
|
|
|
|
xmpp_stream_in:close(Ref, SendTrailer).
|
|
|
|
|
|
|
|
stop(Ref) ->
|
|
|
|
xmpp_stream_in:stop(Ref).
|
|
|
|
|
|
|
|
-spec send(pid(), xmpp_element()) -> ok;
|
|
|
|
(state(), xmpp_element()) -> state().
|
|
|
|
send(Pid, Pkt) when is_pid(Pid) ->
|
|
|
|
xmpp_stream_in:send(Pid, Pkt);
|
|
|
|
send(#{lserver := LServer} = State, Pkt) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Pkt1 = fix_from_to(Pkt, State),
|
|
|
|
case ejabberd_hooks:run_fold(c2s_filter_send, LServer, {Pkt1, State}, []) of
|
2016-12-29 22:00:36 +01:00
|
|
|
{drop, State1} -> State1;
|
2017-01-09 15:02:17 +01:00
|
|
|
{Pkt2, State1} -> xmpp_stream_in:send(State1, Pkt2)
|
2016-12-28 07:47:11 +01:00
|
|
|
end.
|
|
|
|
|
2016-12-29 22:00:36 +01:00
|
|
|
-spec set_timeout(state(), timeout()) -> state().
|
|
|
|
set_timeout(State, Timeout) ->
|
|
|
|
xmpp_stream_in:set_timeout(State, Timeout).
|
|
|
|
|
|
|
|
-spec add_hooks(binary()) -> ok.
|
|
|
|
add_hooks(Host) ->
|
|
|
|
ejabberd_hooks:add(c2s_closed, Host, ?MODULE, process_closed, 100),
|
|
|
|
ejabberd_hooks:add(c2s_terminated, Host, ?MODULE,
|
|
|
|
process_terminated, 100),
|
|
|
|
ejabberd_hooks:add(c2s_unauthenticated_packet, Host, ?MODULE,
|
|
|
|
reject_unauthenticated_packet, 100),
|
|
|
|
ejabberd_hooks:add(c2s_handle_info, Host, ?MODULE,
|
|
|
|
process_info, 100),
|
|
|
|
ejabberd_hooks:add(c2s_handle_cast, Host, ?MODULE,
|
|
|
|
handle_unexpected_cast, 100).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
%% Copies content of one c2s state to another.
|
|
|
|
%% This is needed for session migration from one pid to another.
|
|
|
|
-spec copy_state(state(), state()) -> state().
|
|
|
|
copy_state(#{owner := Owner} = NewState,
|
2016-12-29 22:00:36 +01:00
|
|
|
#{jid := JID, resource := Resource, sid := {Time, _},
|
|
|
|
auth_module := AuthModule, lserver := LServer,
|
|
|
|
pres_t := PresT, pres_a := PresA,
|
|
|
|
pres_f := PresF} = OldState) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
State1 = case OldState of
|
|
|
|
#{pres_last := Pres, pres_timestamp := PresTS} ->
|
|
|
|
NewState#{pres_last => Pres, pres_timestamp => PresTS};
|
|
|
|
_ ->
|
|
|
|
NewState
|
|
|
|
end,
|
|
|
|
Conn = get_conn_type(State1),
|
|
|
|
State2 = State1#{jid => JID, resource => Resource,
|
|
|
|
conn => Conn,
|
|
|
|
sid => {Time, Owner},
|
|
|
|
auth_module => AuthModule,
|
|
|
|
pres_t => PresT, pres_a => PresA,
|
|
|
|
pres_f => PresF},
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_copy_session, LServer, State2, [OldState]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2016-12-29 22:00:36 +01:00
|
|
|
-spec open_session(state()) -> {ok, state()} | state().
|
|
|
|
open_session(#{user := U, server := S, resource := R,
|
|
|
|
sid := SID, ip := IP, auth_module := AuthModule} = State) ->
|
|
|
|
JID = jid:make(U, S, R),
|
|
|
|
change_shaper(State),
|
|
|
|
Conn = get_conn_type(State),
|
|
|
|
State1 = State#{conn => Conn, resource => R, jid => JID},
|
|
|
|
Prio = try maps:get(pres_last, State) of
|
|
|
|
Pres -> get_priority_from_presence(Pres)
|
|
|
|
catch _:{badkey, _} ->
|
|
|
|
undefined
|
|
|
|
end,
|
|
|
|
Info = [{ip, IP}, {conn, Conn}, {auth_module, AuthModule}],
|
|
|
|
ejabberd_sm:open_session(SID, U, S, R, Prio, Info),
|
|
|
|
xmpp_stream_in:establish(State1).
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Hooks
|
|
|
|
%%%===================================================================
|
2016-12-29 22:00:36 +01:00
|
|
|
process_info(#{lserver := LServer} = State,
|
|
|
|
{route, From, To, Packet0}) ->
|
|
|
|
Packet = xmpp:set_from_to(Packet0, From, To),
|
|
|
|
{Pass, State1} = case Packet of
|
|
|
|
#presence{} ->
|
|
|
|
process_presence_in(State, Packet);
|
|
|
|
#message{} ->
|
|
|
|
process_message_in(State, Packet);
|
|
|
|
#iq{} ->
|
|
|
|
process_iq_in(State, Packet)
|
|
|
|
end,
|
|
|
|
if Pass ->
|
2017-01-09 15:02:17 +01:00
|
|
|
{Packet1, State2} = ejabberd_hooks:run_fold(
|
|
|
|
user_receive_packet, LServer,
|
|
|
|
{Packet, State1}, []),
|
|
|
|
case Packet1 of
|
|
|
|
drop -> State2;
|
|
|
|
_ -> send(State2, Packet1)
|
|
|
|
end;
|
2016-12-29 22:00:36 +01:00
|
|
|
true ->
|
|
|
|
State1
|
|
|
|
end;
|
2017-01-09 15:02:17 +01:00
|
|
|
process_info(State, force_update_presence) ->
|
|
|
|
try maps:get(pres_last, State) of
|
|
|
|
Pres -> process_self_presence(State, Pres)
|
|
|
|
catch _:{badkey, _} ->
|
|
|
|
State
|
|
|
|
end;
|
2016-12-29 22:00:36 +01:00
|
|
|
process_info(State, Info) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
?WARNING_MSG("got unexpected info: ~p", [Info]),
|
|
|
|
State.
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
handle_unexpected_cast(State, Msg) ->
|
|
|
|
?WARNING_MSG("got unexpected cast: ~p", [Msg]),
|
|
|
|
State.
|
|
|
|
|
2017-01-13 09:35:47 +01:00
|
|
|
reject_unauthenticated_packet(State, _Pkt) ->
|
|
|
|
Err = xmpp:serr_not_authorized(),
|
|
|
|
send(State, Err).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2016-12-29 22:00:36 +01:00
|
|
|
process_closed(State, Reason) ->
|
|
|
|
stop(State#{stop_reason => Reason}).
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
process_terminated(#{sockmod := SockMod, socket := Socket, jid := JID} = State,
|
2016-12-29 22:00:36 +01:00
|
|
|
Reason) ->
|
|
|
|
Status = format_reason(State, Reason),
|
2016-12-31 11:48:55 +01:00
|
|
|
?INFO_MSG("(~s) Closing c2s session for ~s: ~s",
|
2017-01-09 15:02:17 +01:00
|
|
|
[SockMod:pp(Socket), jid:to_string(JID), Status]),
|
|
|
|
State1 = case maps:is_key(pres_last, State) of
|
|
|
|
true ->
|
|
|
|
Pres = #presence{type = unavailable,
|
|
|
|
status = xmpp:mk_text(Status),
|
|
|
|
from = JID,
|
|
|
|
to = jid:remove_resource(JID)},
|
|
|
|
broadcast_presence_unavailable(State, Pres);
|
|
|
|
false ->
|
|
|
|
State
|
|
|
|
end,
|
2016-12-29 22:00:36 +01:00
|
|
|
bounce_message_queue(),
|
|
|
|
State1;
|
|
|
|
process_terminated(State, _Reason) ->
|
|
|
|
State.
|
2016-12-11 13:03:37 +01:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% xmpp_stream_in callbacks
|
|
|
|
%%%===================================================================
|
2017-01-09 15:02:17 +01:00
|
|
|
tls_options(#{lserver := LServer, tls_options := DefaultOpts}) ->
|
|
|
|
TLSOpts1 = case ejabberd_config:get_option(
|
|
|
|
{c2s_certfile, LServer},
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
ejabberd_config:get_option(
|
|
|
|
{domain_certfile, LServer},
|
|
|
|
fun iolist_to_binary/1)) of
|
|
|
|
undefined -> [];
|
|
|
|
CertFile -> lists:keystore(certfile, 1, DefaultOpts,
|
|
|
|
{certfile, CertFile})
|
|
|
|
end,
|
|
|
|
TLSOpts2 = case ejabberd_config:get_option(
|
|
|
|
{c2s_ciphers, LServer},
|
|
|
|
fun iolist_to_binary/1) of
|
|
|
|
undefined -> TLSOpts1;
|
|
|
|
Ciphers -> lists:keystore(ciphers, 1, TLSOpts1,
|
|
|
|
{ciphers, Ciphers})
|
|
|
|
end,
|
|
|
|
TLSOpts3 = case ejabberd_config:get_option(
|
|
|
|
{c2s_protocol_options, LServer},
|
|
|
|
fun (Options) -> str:join(Options, <<$|>>) end) of
|
|
|
|
undefined -> TLSOpts2;
|
|
|
|
ProtoOpts -> lists:keystore(protocol_options, 1, TLSOpts2,
|
|
|
|
{protocol_options, ProtoOpts})
|
|
|
|
end,
|
|
|
|
TLSOpts4 = case ejabberd_config:get_option(
|
|
|
|
{c2s_dhfile, LServer},
|
|
|
|
fun iolist_to_binary/1) of
|
|
|
|
undefined -> TLSOpts3;
|
|
|
|
DHFile -> lists:keystore(dhfile, 1, TLSOpts3,
|
|
|
|
{dhfile, DHFile})
|
|
|
|
end,
|
|
|
|
TLSOpts5 = case ejabberd_config:get_option(
|
|
|
|
{c2s_cafile, LServer},
|
|
|
|
fun iolist_to_binary/1) of
|
|
|
|
undefined -> TLSOpts4;
|
|
|
|
CAFile -> lists:keystore(cafile, 1, TLSOpts4,
|
|
|
|
{cafile, CAFile})
|
|
|
|
end,
|
|
|
|
case ejabberd_config:get_option(
|
|
|
|
{c2s_tls_compression, LServer},
|
|
|
|
fun(B) when is_boolean(B) -> B end) of
|
|
|
|
undefined -> TLSOpts5;
|
|
|
|
false -> [compression_none | TLSOpts5];
|
|
|
|
true -> lists:delete(compression_none, TLSOpts5)
|
2016-12-11 13:03:37 +01:00
|
|
|
end.
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
tls_required(#{tls_required := TLSRequired}) ->
|
|
|
|
TLSRequired.
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
tls_verify(#{tls_verify := TLSVerify}) ->
|
|
|
|
TLSVerify.
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
tls_enabled(#{tls_enabled := TLSEnabled,
|
|
|
|
tls_required := TLSRequired,
|
|
|
|
tls_verify := TLSVerify}) ->
|
|
|
|
TLSEnabled or TLSRequired or TLSVerify.
|
|
|
|
|
2016-12-11 16:24:51 +01:00
|
|
|
compress_methods(#{zlib := true}) ->
|
|
|
|
[<<"zlib">>];
|
|
|
|
compress_methods(_) ->
|
|
|
|
[].
|
|
|
|
|
|
|
|
unauthenticated_stream_features(#{lserver := LServer}) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_pre_auth_features, LServer, [], [LServer]).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2016-12-11 16:24:51 +01:00
|
|
|
authenticated_stream_features(#{lserver := LServer}) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_post_auth_features, LServer, [], [LServer]).
|
2016-07-18 14:01:32 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
sasl_mechanisms(Mechs, #{lserver := LServer}) ->
|
|
|
|
Mechs1 = ejabberd_config:get_option(
|
|
|
|
{disable_sasl_mechanisms, LServer},
|
|
|
|
fun(V) when is_list(V) ->
|
|
|
|
lists:map(fun(M) -> str:to_upper(M) end, V);
|
|
|
|
(V) ->
|
|
|
|
[str:to_upper(V)]
|
|
|
|
end, []),
|
|
|
|
Mechs2 = case ejabberd_auth_anonymous:is_sasl_anonymous_enabled(LServer) of
|
|
|
|
true -> Mechs1;
|
|
|
|
false -> [<<"ANONYMOUS">>|Mechs1]
|
|
|
|
end,
|
|
|
|
Mechs -- Mechs2.
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
get_password_fun(#{lserver := LServer}) ->
|
|
|
|
fun(U) ->
|
|
|
|
ejabberd_auth:get_password_with_authmodule(U, LServer)
|
|
|
|
end.
|
|
|
|
|
|
|
|
check_password_fun(#{lserver := LServer}) ->
|
|
|
|
fun(U, AuthzId, P) ->
|
|
|
|
ejabberd_auth:check_password_with_authmodule(U, AuthzId, LServer, P)
|
|
|
|
end.
|
|
|
|
|
|
|
|
check_password_digest_fun(#{lserver := LServer}) ->
|
|
|
|
fun(U, AuthzId, P, D, DG) ->
|
|
|
|
ejabberd_auth:check_password_with_authmodule(U, AuthzId, LServer, P, D, DG)
|
|
|
|
end.
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
bind(<<"">>, State) ->
|
|
|
|
bind(new_uniq_id(), State);
|
2016-12-28 07:47:11 +01:00
|
|
|
bind(R, #{user := U, server := S, access := Access, lang := Lang,
|
2017-01-09 15:02:17 +01:00
|
|
|
lserver := LServer, sockmod := SockMod, socket := Socket,
|
|
|
|
ip := IP} = State) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
case resource_conflict_action(U, S, R) of
|
|
|
|
closenew ->
|
|
|
|
{error, xmpp:err_conflict(), State};
|
|
|
|
{accept_resource, Resource} ->
|
2016-12-28 07:47:11 +01:00
|
|
|
JID = jid:make(U, S, Resource),
|
|
|
|
case acl:access_matches(Access,
|
|
|
|
#{usr => jid:split(JID), ip => IP},
|
|
|
|
LServer) of
|
|
|
|
allow ->
|
2017-01-09 15:02:17 +01:00
|
|
|
State1 = open_session(State#{resource => Resource,
|
|
|
|
sid => ejabberd_sm:make_sid()}),
|
2016-12-28 07:47:11 +01:00
|
|
|
State2 = ejabberd_hooks:run_fold(
|
|
|
|
c2s_session_opened, LServer, State1, []),
|
2016-12-31 11:48:55 +01:00
|
|
|
?INFO_MSG("(~s) Opened c2s session for ~s",
|
2017-01-09 15:02:17 +01:00
|
|
|
[SockMod:pp(Socket), jid:to_string(JID)]),
|
2016-12-28 07:47:11 +01:00
|
|
|
{ok, State2};
|
|
|
|
deny ->
|
|
|
|
ejabberd_hooks:run(forbidden_session_hook, LServer, [JID]),
|
2016-12-31 11:48:55 +01:00
|
|
|
?INFO_MSG("(~s) Forbidden c2s session for ~s",
|
2017-01-09 15:02:17 +01:00
|
|
|
[SockMod:pp(Socket), jid:to_string(JID)]),
|
2016-12-28 07:47:11 +01:00
|
|
|
Txt = <<"Denied by ACL">>,
|
|
|
|
{error, xmpp:err_not_allowed(Txt, Lang), State}
|
|
|
|
end
|
2016-07-18 14:01:32 +02:00
|
|
|
end.
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
handle_stream_start(StreamStart, #{lserver := LServer} = State) ->
|
2016-12-29 22:00:36 +01:00
|
|
|
case ejabberd_router:is_my_host(LServer) of
|
2016-12-11 13:03:37 +01:00
|
|
|
false ->
|
2016-12-28 07:47:11 +01:00
|
|
|
send(State, xmpp:serr_host_unknown());
|
2016-12-11 13:03:37 +01:00
|
|
|
true ->
|
2017-01-09 15:02:17 +01:00
|
|
|
change_shaper(State),
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
c2s_stream_started, LServer, State, [StreamStart])
|
2016-01-18 14:33:37 +01:00
|
|
|
end.
|
2003-06-07 19:30:25 +02:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
handle_stream_end(Reason, #{lserver := LServer} = State) ->
|
2016-12-29 22:00:36 +01:00
|
|
|
State1 = State#{stop_reason => Reason},
|
|
|
|
ejabberd_hooks:run_fold(c2s_closed, LServer, State1, [Reason]).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
|
|
|
handle_auth_success(User, Mech, AuthModule,
|
2017-01-09 15:02:17 +01:00
|
|
|
#{socket := Socket, sockmod := SockMod,
|
|
|
|
ip := IP, lserver := LServer} = State) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
?INFO_MSG("(~s) Accepted c2s ~s authentication for ~s@~s by ~s backend from ~s",
|
2017-01-09 15:02:17 +01:00
|
|
|
[SockMod:pp(Socket), Mech, User, LServer,
|
2016-12-28 07:47:11 +01:00
|
|
|
ejabberd_auth:backend_type(AuthModule),
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_config:may_hide_data(jlib:ip_to_list(IP))]),
|
|
|
|
State1 = State#{auth_module => AuthModule},
|
2016-12-29 22:00:36 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_auth_result, LServer, State1, [true, User]).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
|
|
|
handle_auth_failure(User, Mech, Reason,
|
2017-01-09 15:02:17 +01:00
|
|
|
#{socket := Socket, sockmod := SockMod,
|
|
|
|
ip := IP, lserver := LServer} = State) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
?INFO_MSG("(~s) Failed c2s ~s authentication ~sfrom ~s: ~s",
|
2017-01-09 15:02:17 +01:00
|
|
|
[SockMod:pp(Socket), Mech,
|
2016-12-11 13:03:37 +01:00
|
|
|
if User /= <<"">> -> ["for ", User, "@", LServer, " "];
|
|
|
|
true -> ""
|
|
|
|
end,
|
|
|
|
ejabberd_config:may_hide_data(jlib:ip_to_list(IP)), Reason]),
|
2016-12-29 22:00:36 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_auth_result, LServer, State, [false, User]).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2016-12-11 16:24:51 +01:00
|
|
|
handle_unbinded_packet(Pkt, #{lserver := LServer} = State) ->
|
2016-12-29 22:00:36 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_unbinded_packet, LServer, State, [Pkt]).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2016-12-11 16:24:51 +01:00
|
|
|
handle_unauthenticated_packet(Pkt, #{lserver := LServer} = State) ->
|
2016-12-29 22:00:36 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_unauthenticated_packet, LServer, State, [Pkt]).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2016-12-11 16:24:51 +01:00
|
|
|
handle_authenticated_packet(Pkt, #{lserver := LServer} = State) when not ?is_stanza(Pkt) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_authenticated_packet,
|
2016-12-28 07:47:11 +01:00
|
|
|
LServer, State, [Pkt]);
|
2017-01-09 15:02:17 +01:00
|
|
|
handle_authenticated_packet(Pkt, #{lserver := LServer, jid := JID} = State) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
State1 = ejabberd_hooks:run_fold(c2s_authenticated_packet,
|
|
|
|
LServer, State, [Pkt]),
|
2017-01-09 15:02:17 +01:00
|
|
|
#jid{luser = LUser} = JID,
|
|
|
|
{Pkt1, State2} = ejabberd_hooks:run_fold(
|
|
|
|
user_send_packet, LServer, {Pkt, State1}, []),
|
2016-12-28 07:47:11 +01:00
|
|
|
case Pkt1 of
|
2017-01-09 15:02:17 +01:00
|
|
|
drop ->
|
|
|
|
State2;
|
|
|
|
#presence{to = #jid{luser = LUser, lserver = LServer,
|
|
|
|
lresource = <<"">>}} ->
|
|
|
|
process_self_presence(State2, Pkt1);
|
2016-12-28 07:47:11 +01:00
|
|
|
#presence{} ->
|
2017-01-09 15:02:17 +01:00
|
|
|
process_presence_out(State2, Pkt1);
|
2016-12-28 07:47:11 +01:00
|
|
|
_ ->
|
2017-01-09 15:02:17 +01:00
|
|
|
check_privacy_then_route(State2, Pkt1)
|
2016-12-11 13:03:37 +01:00
|
|
|
end.
|
2008-12-08 10:21:36 +01:00
|
|
|
|
2016-12-11 16:24:51 +01:00
|
|
|
handle_cdata(Data, #{lserver := LServer} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(c2s_handle_cdata, LServer,
|
2016-12-28 07:47:11 +01:00
|
|
|
State, [Data]).
|
|
|
|
|
|
|
|
handle_recv(El, Pkt, #{lserver := LServer} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(c2s_handle_recv, LServer, State, [El, Pkt]).
|
2016-12-11 16:24:51 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
handle_send(Pkt, Result, #{lserver := LServer} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(c2s_handle_send, LServer, State, [Pkt, Result]).
|
2016-12-11 16:24:51 +01:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
init([State, Opts]) ->
|
|
|
|
Access = gen_mod:get_opt(access, Opts, fun acl:access_rules_validator/1, all),
|
|
|
|
Shaper = gen_mod:get_opt(shaper, Opts, fun acl:shaper_rules_validator/1, none),
|
2017-01-09 15:02:17 +01:00
|
|
|
TLSOpts1 = lists:filter(
|
|
|
|
fun({certfile, _}) -> true;
|
|
|
|
({ciphers, _}) -> true;
|
|
|
|
({dhfile, _}) -> true;
|
|
|
|
({cafile, _}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end, Opts),
|
|
|
|
TLSOpts2 = case lists:keyfind(protocol_options, 1, Opts) of
|
|
|
|
false -> TLSOpts1;
|
|
|
|
{_, OptString} ->
|
|
|
|
ProtoOpts = str:join(OptString, <<$|>>),
|
|
|
|
[{protocol_options, ProtoOpts}|TLSOpts1]
|
|
|
|
end,
|
|
|
|
TLSOpts3 = case proplists:get_bool(tls_compression, Opts) of
|
|
|
|
false -> [compression_none | TLSOpts2];
|
|
|
|
true -> TLSOpts2
|
|
|
|
end,
|
|
|
|
TLSEnabled = proplists:get_bool(starttls, Opts),
|
2016-12-11 13:03:37 +01:00
|
|
|
TLSRequired = proplists:get_bool(starttls_required, Opts),
|
|
|
|
TLSVerify = proplists:get_bool(tls_verify, Opts),
|
2016-12-11 16:24:51 +01:00
|
|
|
Zlib = proplists:get_bool(zlib, Opts),
|
2017-01-09 15:02:17 +01:00
|
|
|
State1 = State#{tls_options => TLSOpts3,
|
2016-12-11 13:03:37 +01:00
|
|
|
tls_required => TLSRequired,
|
2017-01-09 15:02:17 +01:00
|
|
|
tls_enabled => TLSEnabled,
|
2016-12-11 13:03:37 +01:00
|
|
|
tls_verify => TLSVerify,
|
|
|
|
pres_a => ?SETS:new(),
|
|
|
|
pres_f => ?SETS:new(),
|
|
|
|
pres_t => ?SETS:new(),
|
2016-12-11 16:24:51 +01:00
|
|
|
zlib => Zlib,
|
2016-12-11 13:03:37 +01:00
|
|
|
lang => ?MYLANG,
|
|
|
|
server => ?MYNAME,
|
2016-12-29 22:00:36 +01:00
|
|
|
lserver => ?MYNAME,
|
2016-12-11 13:03:37 +01:00
|
|
|
access => Access,
|
|
|
|
shaper => Shaper},
|
2016-12-28 07:47:11 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_init, {ok, State1}, [Opts]).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2016-12-29 22:00:36 +01:00
|
|
|
handle_call(get_presence, From, #{jid := JID} = State) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
Pres = try maps:get(pres_last, State)
|
|
|
|
catch _:{badkey, _} ->
|
2016-12-11 16:24:51 +01:00
|
|
|
BareJID = jid:remove_resource(JID),
|
2016-12-28 07:47:11 +01:00
|
|
|
#presence{from = JID, to = BareJID, type = unavailable}
|
2016-07-18 14:01:32 +02:00
|
|
|
end,
|
2016-12-29 22:00:36 +01:00
|
|
|
reply(From, Pres),
|
|
|
|
State;
|
|
|
|
handle_call(get_subscribed, From, #{pres_f := PresF} = State) ->
|
|
|
|
reply(From, ?SETS:to_list(PresF)),
|
|
|
|
State;
|
2016-12-11 16:24:51 +01:00
|
|
|
handle_call(Request, From, #{lserver := LServer} = State) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_hooks:run_fold(
|
2016-12-28 07:47:11 +01:00
|
|
|
c2s_handle_call, LServer, State, [Request, From]).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2016-12-11 16:24:51 +01:00
|
|
|
handle_cast(Msg, #{lserver := LServer} = State) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_handle_cast, LServer, State, [Msg]).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2016-12-11 16:24:51 +01:00
|
|
|
handle_info(Info, #{lserver := LServer} = State) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_handle_info, LServer, State, [Info]).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
terminate(Reason, #{sid := SID,
|
2016-12-29 22:00:36 +01:00
|
|
|
user := U, server := S, resource := R,
|
|
|
|
lserver := LServer} = State) ->
|
|
|
|
case maps:is_key(pres_last, State) of
|
|
|
|
true ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Status = format_reason(State, Reason),
|
2016-12-29 22:00:36 +01:00
|
|
|
ejabberd_sm:close_session_unset_presence(SID, U, S, R, Status);
|
|
|
|
false ->
|
|
|
|
ejabberd_sm:close_session(SID, U, S, R)
|
|
|
|
end,
|
|
|
|
ejabberd_hooks:run_fold(c2s_terminated, LServer, State, [Reason]);
|
|
|
|
terminate(Reason, #{lserver := LServer} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(c2s_terminated, LServer, State, [Reason]).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
2016-07-27 09:45:08 +02:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
-spec process_iq_in(state(), iq()) -> {boolean(), state()}.
|
|
|
|
process_iq_in(State, #iq{} = IQ) ->
|
|
|
|
case privacy_check_packet(State, IQ, in) of
|
|
|
|
allow ->
|
|
|
|
{true, State};
|
|
|
|
deny ->
|
|
|
|
route_error(IQ, xmpp:err_service_unavailable()),
|
|
|
|
{false, State}
|
2016-09-23 11:30:33 +02:00
|
|
|
end.
|
2009-08-31 20:37:52 +02:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec process_message_in(state(), message()) -> {boolean(), state()}.
|
|
|
|
process_message_in(State, #message{type = T} = Msg) ->
|
2017-01-16 16:40:30 +01:00
|
|
|
case privacy_check_packet(State, Msg, in) of
|
2016-12-11 13:03:37 +01:00
|
|
|
allow ->
|
|
|
|
{true, State};
|
|
|
|
deny when T == groupchat; T == headline ->
|
|
|
|
ok;
|
|
|
|
deny ->
|
|
|
|
case xmpp:has_subtag(Msg, #muc_user{}) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
route_error(Msg, xmpp:err_service_unavailable())
|
|
|
|
end,
|
|
|
|
{false, State}
|
2008-02-15 17:35:32 +01:00
|
|
|
end.
|
2002-11-20 21:19:20 +01:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec process_presence_in(state(), presence()) -> {boolean(), state()}.
|
2016-12-11 16:24:51 +01:00
|
|
|
process_presence_in(#{lserver := LServer, pres_a := PresA} = State0,
|
2016-12-11 13:03:37 +01:00
|
|
|
#presence{from = From, to = To, type = T} = Pres) ->
|
|
|
|
State = ejabberd_hooks:run_fold(c2s_presence_in, LServer, State0, [Pres]),
|
|
|
|
case T of
|
|
|
|
probe ->
|
2017-01-09 15:02:17 +01:00
|
|
|
NewState = add_to_pres_a(State, From),
|
2016-12-11 13:03:37 +01:00
|
|
|
route_probe_reply(From, To, NewState),
|
|
|
|
{false, NewState};
|
|
|
|
error ->
|
|
|
|
A = ?SETS:del_element(jid:tolower(From), PresA),
|
|
|
|
{true, State#{pres_a => A}};
|
2002-12-17 21:49:45 +01:00
|
|
|
_ ->
|
2016-12-11 13:03:37 +01:00
|
|
|
case privacy_check_packet(State, Pres, in) of
|
|
|
|
allow when T == error ->
|
|
|
|
{true, State};
|
|
|
|
allow ->
|
2017-01-09 15:02:17 +01:00
|
|
|
NewState = add_to_pres_a(State, From),
|
2016-12-11 13:03:37 +01:00
|
|
|
{true, NewState};
|
|
|
|
deny ->
|
|
|
|
{false, State}
|
2002-12-17 21:49:45 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec route_probe_reply(jid(), jid(), state()) -> ok.
|
2016-12-11 16:24:51 +01:00
|
|
|
route_probe_reply(From, To, #{lserver := LServer, pres_f := PresF,
|
2016-12-11 13:03:37 +01:00
|
|
|
pres_last := LastPres,
|
|
|
|
pres_timestamp := TS} = State) ->
|
|
|
|
LFrom = jid:tolower(From),
|
|
|
|
LBFrom = jid:remove_resource(LFrom),
|
|
|
|
case ?SETS:is_element(LFrom, PresF)
|
|
|
|
orelse ?SETS:is_element(LBFrom, PresF) of
|
|
|
|
true ->
|
|
|
|
%% To is my JID
|
|
|
|
Packet = xmpp_util:add_delay_info(LastPres, To, TS),
|
|
|
|
case privacy_check_packet(State, Packet, out) of
|
|
|
|
deny ->
|
|
|
|
ok;
|
|
|
|
allow ->
|
|
|
|
ejabberd_hooks:run(presence_probe_hook,
|
|
|
|
LServer,
|
|
|
|
[From, To, self()]),
|
|
|
|
%% Don't route a presence probe to oneself
|
|
|
|
case From == To of
|
|
|
|
false ->
|
|
|
|
route(xmpp:set_from_to(Packet, To, From));
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2016-12-11 13:03:37 +01:00
|
|
|
ok
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
route_probe_reply(_, _, _) ->
|
|
|
|
ok.
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec process_presence_out(state(), presence()) -> state().
|
2016-12-11 16:24:51 +01:00
|
|
|
process_presence_out(#{user := User, server := Server, lserver := LServer,
|
|
|
|
jid := JID, lang := Lang, pres_a := PresA} = State,
|
2016-12-11 13:03:37 +01:00
|
|
|
#presence{from = From, to = To, type = Type} = Pres) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LTo = jid:tolower(To),
|
2016-12-11 13:03:37 +01:00
|
|
|
case privacy_check_packet(State, Pres, out) of
|
2016-10-22 12:01:45 +02:00
|
|
|
deny ->
|
|
|
|
ErrText = <<"Your active privacy list has denied "
|
|
|
|
"the routing of this stanza.">>,
|
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
2016-12-11 13:03:37 +01:00
|
|
|
xmpp_stream_in:send_error(State, Pres, Err);
|
2016-10-22 12:01:45 +02:00
|
|
|
allow when Type == subscribe; Type == subscribed;
|
|
|
|
Type == unsubscribe; Type == unsubscribed ->
|
2016-12-11 13:03:37 +01:00
|
|
|
Access = gen_mod:get_module_opt(LServer, mod_roster, access,
|
2016-10-22 12:01:45 +02:00
|
|
|
fun(A) when is_atom(A) -> A end,
|
|
|
|
all),
|
2016-12-11 16:24:51 +01:00
|
|
|
MyBareJID = jid:remove_resource(JID),
|
2016-12-11 13:03:37 +01:00
|
|
|
case acl:match_rule(LServer, Access, MyBareJID) of
|
2016-10-22 12:01:45 +02:00
|
|
|
deny ->
|
|
|
|
ErrText = <<"Denied by ACL">>,
|
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
2016-12-11 13:03:37 +01:00
|
|
|
xmpp_stream_in:send_error(State, Pres, Err);
|
2016-10-22 12:01:45 +02:00
|
|
|
allow ->
|
|
|
|
ejabberd_hooks:run(roster_out_subscription,
|
2016-12-11 13:03:37 +01:00
|
|
|
LServer,
|
2016-10-22 12:01:45 +02:00
|
|
|
[User, Server, To, Type]),
|
2016-12-11 13:03:37 +01:00
|
|
|
BareFrom = jid:remove_resource(From),
|
|
|
|
route(xmpp:set_from_to(Pres, BareFrom, To)),
|
2016-12-28 07:47:11 +01:00
|
|
|
State
|
2016-10-22 12:01:45 +02:00
|
|
|
end;
|
|
|
|
allow when Type == error; Type == probe ->
|
2016-12-11 13:03:37 +01:00
|
|
|
route(Pres),
|
2016-12-28 07:47:11 +01:00
|
|
|
State;
|
2016-10-22 12:01:45 +02:00
|
|
|
allow ->
|
2016-12-11 13:03:37 +01:00
|
|
|
route(Pres),
|
2016-10-22 12:01:45 +02:00
|
|
|
A = case Type of
|
2016-12-11 13:03:37 +01:00
|
|
|
available -> ?SETS:add_element(LTo, PresA);
|
|
|
|
unavailable -> ?SETS:del_element(LTo, PresA)
|
2016-10-22 12:01:45 +02:00
|
|
|
end,
|
2016-12-28 07:47:11 +01:00
|
|
|
State#{pres_a => A}
|
2002-12-18 21:26:08 +01:00
|
|
|
end.
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec process_self_presence(state(), presence()) -> state().
|
2017-01-09 15:02:17 +01:00
|
|
|
process_self_presence(#{ip := IP, conn := Conn, lserver := LServer,
|
2016-12-11 13:03:37 +01:00
|
|
|
auth_module := AuthMod, sid := SID,
|
|
|
|
user := U, server := S, resource := R} = State,
|
|
|
|
#presence{type = unavailable} = Pres) ->
|
|
|
|
Status = xmpp:get_text(Pres#presence.status),
|
|
|
|
Info = [{ip, IP}, {conn, Conn}, {auth_module, AuthMod}],
|
|
|
|
ejabberd_sm:unset_presence(SID, U, S, R, Status, Info),
|
2017-01-09 15:02:17 +01:00
|
|
|
{Pres1, State1} = ejabberd_hooks:run_fold(
|
|
|
|
c2s_self_presence, LServer, {Pres, State}, []),
|
|
|
|
State2 = broadcast_presence_unavailable(State1, Pres1),
|
|
|
|
maps:remove(pres_last, maps:remove(pres_timestamp, State2));
|
2016-12-11 16:24:51 +01:00
|
|
|
process_self_presence(#{lserver := LServer} = State,
|
2016-12-11 13:03:37 +01:00
|
|
|
#presence{type = available} = Pres) ->
|
|
|
|
PreviousPres = maps:get(pres_last, State, undefined),
|
|
|
|
update_priority(State, Pres),
|
2017-01-09 15:02:17 +01:00
|
|
|
{Pres1, State1} = ejabberd_hooks:run_fold(
|
|
|
|
c2s_self_presence, LServer, {Pres, State}, []),
|
|
|
|
State2 = State1#{pres_last => Pres1,
|
2016-12-11 13:03:37 +01:00
|
|
|
pres_timestamp => p1_time_compat:timestamp()},
|
|
|
|
FromUnavailable = PreviousPres == undefined,
|
2017-01-09 15:02:17 +01:00
|
|
|
broadcast_presence_available(State2, Pres1, FromUnavailable);
|
2016-12-11 13:03:37 +01:00
|
|
|
process_self_presence(State, _Pres) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
State.
|
2016-12-11 13:03:37 +01:00
|
|
|
|
|
|
|
-spec update_priority(state(), presence()) -> ok.
|
|
|
|
update_priority(#{ip := IP, conn := Conn, auth_module := AuthMod,
|
|
|
|
sid := SID, user := U, server := S, resource := R},
|
|
|
|
Pres) ->
|
|
|
|
Priority = get_priority_from_presence(Pres),
|
|
|
|
Info = [{ip, IP}, {conn, Conn}, {auth_module, AuthMod}],
|
|
|
|
ejabberd_sm:set_presence(SID, U, S, R, Priority, Pres, Info).
|
|
|
|
|
|
|
|
-spec broadcast_presence_unavailable(state(), presence()) -> state().
|
|
|
|
broadcast_presence_unavailable(#{pres_a := PresA} = State, Pres) ->
|
|
|
|
JIDs = filter_blocked(State, Pres, PresA),
|
|
|
|
route_multiple(State, JIDs, Pres),
|
|
|
|
State#{pres_a => ?SETS:new()}.
|
|
|
|
|
|
|
|
-spec broadcast_presence_available(state(), presence(), boolean()) -> state().
|
|
|
|
broadcast_presence_available(#{pres_a := PresA, pres_f := PresF,
|
2017-01-09 15:02:17 +01:00
|
|
|
pres_t := PresT, jid := JID} = State,
|
2016-12-11 13:03:37 +01:00
|
|
|
Pres, _FromUnavailable = true) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Probe = #presence{from = JID, type = probe},
|
2016-12-11 13:03:37 +01:00
|
|
|
TJIDs = filter_blocked(State, Probe, PresT),
|
|
|
|
FJIDs = filter_blocked(State, Pres, PresF),
|
|
|
|
route_multiple(State, TJIDs, Probe),
|
|
|
|
route_multiple(State, FJIDs, Pres),
|
|
|
|
State#{pres_a => ?SETS:union(PresA, PresF)};
|
|
|
|
broadcast_presence_available(#{pres_a := PresA, pres_f := PresF} = State,
|
|
|
|
Pres, _FromUnavailable = false) ->
|
|
|
|
JIDs = filter_blocked(State, Pres, ?SETS:intersection(PresA, PresF)),
|
|
|
|
route_multiple(State, JIDs, Pres),
|
|
|
|
State.
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec check_privacy_then_route(state(), stanza()) -> state().
|
2016-12-11 13:03:37 +01:00
|
|
|
check_privacy_then_route(#{lang := Lang} = State, Pkt) ->
|
|
|
|
case privacy_check_packet(State, Pkt, out) of
|
2015-04-20 16:01:56 +02:00
|
|
|
deny ->
|
|
|
|
ErrText = <<"Your active privacy list has denied "
|
2016-10-22 12:01:45 +02:00
|
|
|
"the routing of this stanza.">>,
|
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
2016-12-11 13:03:37 +01:00
|
|
|
xmpp_stream_in:send_error(State, Pkt, Err);
|
2015-04-20 22:03:18 +02:00
|
|
|
allow ->
|
2016-12-11 13:03:37 +01:00
|
|
|
route(Pkt),
|
2016-12-28 07:47:11 +01:00
|
|
|
State
|
2002-12-22 21:56:29 +01:00
|
|
|
end.
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec privacy_check_packet(state(), stanza(), in | out) -> allow | deny.
|
2016-12-11 16:24:51 +01:00
|
|
|
privacy_check_packet(#{lserver := LServer} = State, Pkt, Dir) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_hooks:run_fold(privacy_check_packet, LServer, allow, [State, Pkt, Dir]).
|
2004-09-17 21:52:59 +02:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec get_priority_from_presence(presence()) -> integer().
|
2016-07-18 14:01:32 +02:00
|
|
|
get_priority_from_presence(#presence{priority = Prio}) ->
|
|
|
|
case Prio of
|
|
|
|
undefined -> 0;
|
|
|
|
_ -> Prio
|
2006-04-23 16:57:37 +02:00
|
|
|
end.
|
2003-08-03 21:09:40 +02:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec filter_blocked(state(), presence(), ?SETS:set()) -> [jid()].
|
2016-12-11 16:24:51 +01:00
|
|
|
filter_blocked(#{jid := From} = State, Pres, LJIDSet) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
?SETS:fold(
|
|
|
|
fun(LJID, Acc) ->
|
|
|
|
To = jid:make(LJID),
|
|
|
|
Pkt = xmpp:set_from_to(Pres, From, To),
|
|
|
|
case privacy_check_packet(State, Pkt, out) of
|
|
|
|
allow -> [To|Acc];
|
|
|
|
deny -> Acc
|
|
|
|
end
|
|
|
|
end, [], LJIDSet).
|
|
|
|
|
|
|
|
-spec route(stanza()) -> ok.
|
|
|
|
route(Pkt) ->
|
|
|
|
From = xmpp:get_from(Pkt),
|
|
|
|
To = xmpp:get_to(Pkt),
|
|
|
|
ejabberd_router:route(From, To, Pkt).
|
|
|
|
|
|
|
|
-spec route_error(stanza(), stanza_error()) -> ok.
|
|
|
|
route_error(Pkt, Err) ->
|
|
|
|
From = xmpp:get_from(Pkt),
|
|
|
|
To = xmpp:get_to(Pkt),
|
|
|
|
ejabberd_router:route_error(To, From, Pkt, Err).
|
|
|
|
|
|
|
|
-spec route_multiple(state(), [jid()], stanza()) -> ok.
|
2016-12-11 16:24:51 +01:00
|
|
|
route_multiple(#{lserver := LServer}, JIDs, Pkt) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
From = xmpp:get_from(Pkt),
|
|
|
|
ejabberd_router_multicast:route_multicast(From, LServer, JIDs, Pkt).
|
2003-08-03 21:09:40 +02:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec resource_conflict_action(binary(), binary(), binary()) ->
|
|
|
|
{accept_resource, binary()} | closenew.
|
|
|
|
resource_conflict_action(U, S, R) ->
|
|
|
|
OptionRaw = case ejabberd_sm:is_existing_resource(U, S, R) of
|
|
|
|
true ->
|
|
|
|
ejabberd_config:get_option(
|
|
|
|
{resource_conflict, S},
|
|
|
|
fun(setresource) -> setresource;
|
|
|
|
(closeold) -> closeold;
|
|
|
|
(closenew) -> closenew;
|
|
|
|
(acceptnew) -> acceptnew
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
acceptnew
|
2014-03-12 23:34:14 +01:00
|
|
|
end,
|
2016-12-11 13:03:37 +01:00
|
|
|
Option = case OptionRaw of
|
|
|
|
setresource -> setresource;
|
|
|
|
closeold ->
|
|
|
|
acceptnew; %% ejabberd_sm will close old session
|
|
|
|
closenew -> closenew;
|
|
|
|
acceptnew -> acceptnew;
|
|
|
|
_ -> acceptnew %% default ejabberd behavior
|
|
|
|
end,
|
|
|
|
case Option of
|
|
|
|
acceptnew -> {accept_resource, R};
|
|
|
|
closenew -> closenew;
|
|
|
|
setresource ->
|
|
|
|
Rnew = new_uniq_id(),
|
|
|
|
{accept_resource, Rnew}
|
2010-01-28 14:00:04 +01:00
|
|
|
end.
|
|
|
|
|
2016-12-29 22:00:36 +01:00
|
|
|
-spec bounce_message_queue() -> ok.
|
|
|
|
bounce_message_queue() ->
|
|
|
|
receive {route, From, To, Pkt} ->
|
|
|
|
ejabberd_router:route(From, To, Pkt),
|
|
|
|
bounce_message_queue()
|
|
|
|
after 0 ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec new_uniq_id() -> binary().
|
|
|
|
new_uniq_id() ->
|
|
|
|
iolist_to_binary(
|
|
|
|
[randoms:get_string(),
|
|
|
|
integer_to_binary(p1_time_compat:unique_integer([positive]))]).
|
2010-03-17 15:40:48 +01:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec get_conn_type(state()) -> c2s | c2s_tls | c2s_compressed | websocket |
|
|
|
|
c2s_compressed_tls | http_bind.
|
|
|
|
get_conn_type(State) ->
|
|
|
|
case xmpp_stream_in:get_transport(State) of
|
|
|
|
tcp -> c2s;
|
|
|
|
tls -> c2s_tls;
|
|
|
|
tcp_zlib -> c2s_compressed;
|
|
|
|
tls_zlib -> c2s_compressed_tls;
|
|
|
|
http_bind -> http_bind;
|
|
|
|
websocket -> websocket
|
2016-02-19 18:53:28 +01:00
|
|
|
end.
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec fix_from_to(xmpp_element(), state()) -> stanza().
|
|
|
|
fix_from_to(Pkt, #{jid := JID}) when ?is_stanza(Pkt) ->
|
|
|
|
#jid{luser = U, lserver = S, lresource = R} = JID,
|
|
|
|
From = xmpp:get_from(Pkt),
|
|
|
|
From1 = case jid:tolower(From) of
|
|
|
|
{U, S, R} -> JID;
|
|
|
|
{U, S, _} -> jid:replace_resource(JID, From#jid.resource);
|
|
|
|
_ -> From
|
|
|
|
end,
|
|
|
|
xmpp:set_from_to(Pkt, From1, JID);
|
|
|
|
fix_from_to(Pkt, _State) ->
|
|
|
|
Pkt.
|
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec change_shaper(state()) -> ok.
|
2016-12-11 16:24:51 +01:00
|
|
|
change_shaper(#{shaper := ShaperName, ip := IP, lserver := LServer,
|
2016-12-11 13:03:37 +01:00
|
|
|
user := U, server := S, resource := R} = State) ->
|
2016-12-11 16:24:51 +01:00
|
|
|
JID = jid:make(U, S, R),
|
2016-12-11 13:03:37 +01:00
|
|
|
Shaper = acl:access_matches(ShaperName,
|
|
|
|
#{usr => jid:split(JID), ip => IP},
|
|
|
|
LServer),
|
|
|
|
xmpp_stream_in:change_shaper(State, Shaper).
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec add_to_pres_a(state(), jid()) -> state().
|
|
|
|
add_to_pres_a(#{pres_a := PresA, pres_f := PresF} = State, From) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
LFrom = jid:tolower(From),
|
|
|
|
LBFrom = jid:remove_resource(LFrom),
|
|
|
|
case (?SETS):is_element(LFrom, PresA) orelse
|
|
|
|
(?SETS):is_element(LBFrom, PresA) of
|
2016-07-18 14:01:32 +02:00
|
|
|
true ->
|
2016-12-11 13:03:37 +01:00
|
|
|
State;
|
2016-07-18 14:01:32 +02:00
|
|
|
false ->
|
2016-12-11 13:03:37 +01:00
|
|
|
case (?SETS):is_element(LFrom, PresF) of
|
2014-04-07 21:21:11 +02:00
|
|
|
true ->
|
2016-12-11 13:03:37 +01:00
|
|
|
A = (?SETS):add_element(LFrom, PresA),
|
|
|
|
State#{pres_a => A};
|
2014-04-07 21:21:11 +02:00
|
|
|
false ->
|
2016-12-11 13:03:37 +01:00
|
|
|
case (?SETS):is_element(LBFrom, PresF) of
|
|
|
|
true ->
|
|
|
|
A = (?SETS):add_element(LBFrom, PresA),
|
|
|
|
State#{pres_a => A};
|
|
|
|
false ->
|
|
|
|
State
|
2014-04-07 21:21:11 +02:00
|
|
|
end
|
2016-12-11 13:03:37 +01:00
|
|
|
end
|
2009-09-28 19:23:37 +02:00
|
|
|
end.
|
2013-08-12 14:25:05 +02:00
|
|
|
|
2016-12-29 22:00:36 +01:00
|
|
|
-spec format_reason(state(), term()) -> binary().
|
|
|
|
format_reason(#{stop_reason := Reason}, _) ->
|
|
|
|
xmpp_stream_in:format_error(Reason);
|
2017-01-09 15:02:17 +01:00
|
|
|
format_reason(_, normal) ->
|
|
|
|
<<"unknown reason">>;
|
|
|
|
format_reason(_, shutdown) ->
|
|
|
|
<<"stopped by supervisor">>;
|
|
|
|
format_reason(_, {shutdown, _}) ->
|
|
|
|
<<"stopped by supervisor">>;
|
2016-12-29 22:00:36 +01:00
|
|
|
format_reason(_, _) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
<<"internal server error">>.
|
2016-12-29 22:00:36 +01:00
|
|
|
|
2016-12-11 16:24:51 +01:00
|
|
|
transform_listen_option(Opt, Opts) ->
|
|
|
|
[Opt|Opts].
|
|
|
|
|
|
|
|
opt_type(domain_certfile) -> fun iolist_to_binary/1;
|
2017-01-09 15:02:17 +01:00
|
|
|
opt_type(c2s_certfile) -> fun iolist_to_binary/1;
|
|
|
|
opt_type(c2s_ciphers) -> fun iolist_to_binary/1;
|
|
|
|
opt_type(c2s_dhfile) -> fun iolist_to_binary/1;
|
|
|
|
opt_type(c2s_cafile) -> fun iolist_to_binary/1;
|
|
|
|
opt_type(c2s_protocol_options) ->
|
|
|
|
fun (Options) -> str:join(Options, <<"|">>) end;
|
|
|
|
opt_type(c2s_tls_compression) ->
|
|
|
|
fun (true) -> true;
|
|
|
|
(false) -> false
|
|
|
|
end;
|
2016-12-11 16:24:51 +01:00
|
|
|
opt_type(resource_conflict) ->
|
|
|
|
fun (setresource) -> setresource;
|
|
|
|
(closeold) -> closeold;
|
|
|
|
(closenew) -> closenew;
|
|
|
|
(acceptnew) -> acceptnew
|
|
|
|
end;
|
2017-01-09 15:02:17 +01:00
|
|
|
opt_type(disable_sasl_mechanisms) ->
|
|
|
|
fun (V) when is_list(V) ->
|
|
|
|
lists:map(fun (M) -> str:to_upper(M) end, V);
|
|
|
|
(V) -> [str:to_upper(V)]
|
|
|
|
end;
|
2016-12-11 16:24:51 +01:00
|
|
|
opt_type(_) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
[domain_certfile, c2s_certfile, c2s_ciphers, c2s_cafile,
|
|
|
|
c2s_protocol_options, c2s_tls_compression, resource_conflict,
|
|
|
|
disable_sasl_mechanisms].
|