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
|
|
|
%%%
|
|
|
|
%%%
|
2024-01-22 16:40:01 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2024 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).
|
2018-09-17 10:21:02 +02:00
|
|
|
-behaviour(ejabberd_listener).
|
2022-11-04 11:34:45 +01:00
|
|
|
|
|
|
|
-protocol({rfc, 3920}).
|
|
|
|
-protocol({rfc, 3921}).
|
|
|
|
-protocol({rfc, 6120}).
|
2016-12-11 13:03:37 +01:00
|
|
|
-protocol({rfc, 6121}).
|
2024-05-17 13:00:45 +02:00
|
|
|
-protocol({xep, 138, '2.1'}).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2018-09-17 10:21:02 +02:00
|
|
|
%% ejabberd_listener callbacks
|
2019-04-01 15:53:28 +02:00
|
|
|
-export([start/3, start_link/3, accept/1, listen_opt_type/1, listen_options/0]).
|
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]).
|
2018-07-06 00:07:36 +02:00
|
|
|
-export([tls_options/1, tls_required/1, tls_enabled/1,
|
2017-01-09 15:02:17 +01:00
|
|
|
compress_methods/1, bind/2, sasl_mechanisms/2,
|
2018-07-06 00:07:36 +02:00
|
|
|
get_password_fun/2, check_password_fun/2, check_password_digest_fun/2,
|
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,
|
2023-11-16 16:52:18 +01:00
|
|
|
handle_recv/3, handle_cdata/2, handle_unbinded_packet/2,
|
|
|
|
inline_stream_features/1, handle_sasl2_inline/2,
|
2023-11-20 18:50:11 +01:00
|
|
|
handle_sasl2_inline_post/3, handle_bind2_inline/2,
|
2024-01-16 12:03:35 +01:00
|
|
|
handle_bind2_inline_post/3, sasl_options/1]).
|
2016-12-28 07:47:11 +01:00
|
|
|
%% Hooks
|
2019-07-12 10:55:36 +02:00
|
|
|
-export([handle_unexpected_cast/2, handle_unexpected_call/3,
|
|
|
|
process_auth_result/3, reject_unauthenticated_packet/2,
|
|
|
|
process_closed/2, process_terminated/2, process_info/2]).
|
2016-12-11 13:03:37 +01:00
|
|
|
%% API
|
2017-07-07 10:55:08 +02:00
|
|
|
-export([get_presence/1, set_presence/2, resend_presence/1, resend_presence/2,
|
2020-04-01 14:35:49 +02:00
|
|
|
open_session/1, call/3, cast/2, send/2, close/1, close/2, stop_async/1,
|
2019-08-06 15:18:04 +02:00
|
|
|
reply/2, copy_state/2, set_timeout/2, route/2, format_reason/2,
|
2024-03-20 10:57:12 +01:00
|
|
|
host_up/1, host_down/1, send_ws_ping/1, bounce_message_queue/2,
|
|
|
|
reset_vcard_xupdate_resend_presence/1]).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2016-12-11 13:03:37 +01:00
|
|
|
-include("logger.hrl").
|
2017-05-17 13:47:35 +02:00
|
|
|
-include("mod_roster.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.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
|
|
|
|
2019-06-22 09:19:17 +02:00
|
|
|
-type state() :: xmpp_stream_in:state().
|
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
|
|
|
%%%===================================================================
|
2018-09-17 10:21:02 +02:00
|
|
|
%%% ejabberd_listener API
|
2016-12-11 13:03:37 +01:00
|
|
|
%%%===================================================================
|
2019-04-01 15:53:28 +02:00
|
|
|
start(SockMod, Socket, Opts) ->
|
|
|
|
xmpp_stream_in:start(?MODULE, [{SockMod, Socket}, Opts],
|
2018-09-17 10:21:02 +02:00
|
|
|
ejabberd_config:fsm_limit_opts(Opts)).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2019-04-01 15:53:28 +02:00
|
|
|
start_link(SockMod, Socket, Opts) ->
|
|
|
|
xmpp_stream_in:start_link(?MODULE, [{SockMod, Socket}, Opts],
|
2016-12-28 07:47:11 +01:00
|
|
|
ejabberd_config:fsm_limit_opts(Opts)).
|
2006-09-25 05:51:11 +02:00
|
|
|
|
2018-09-17 10:21:02 +02:00
|
|
|
accept(Ref) ->
|
|
|
|
xmpp_stream_in:accept(Ref).
|
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).
|
|
|
|
|
2017-07-20 20:22:50 +02:00
|
|
|
-spec cast(pid(), term()) -> ok.
|
|
|
|
cast(Ref, Msg) ->
|
|
|
|
xmpp_stream_in:cast(Ref, Msg).
|
|
|
|
|
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
|
|
|
|
2017-07-07 10:55:08 +02:00
|
|
|
-spec set_presence(pid(), presence()) -> ok.
|
|
|
|
set_presence(Ref, Pres) ->
|
|
|
|
call(Ref, {set_presence, Pres}, 1000).
|
|
|
|
|
2018-09-09 08:59:08 +02:00
|
|
|
-spec resend_presence(pid()) -> boolean().
|
2017-05-17 13:47:35 +02:00
|
|
|
resend_presence(Pid) ->
|
|
|
|
resend_presence(Pid, undefined).
|
2010-09-28 06:18:57 +02:00
|
|
|
|
2018-02-07 20:20:12 +01:00
|
|
|
-spec resend_presence(pid(), jid() | undefined) -> boolean().
|
2017-05-17 13:47:35 +02:00
|
|
|
resend_presence(Pid, To) ->
|
|
|
|
route(Pid, {resend_presence, To}).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2024-03-20 10:57:12 +01:00
|
|
|
-spec reset_vcard_xupdate_resend_presence(pid()) -> boolean().
|
|
|
|
reset_vcard_xupdate_resend_presence(Pid) ->
|
|
|
|
route(Pid, reset_vcard_xupdate_resend_presence).
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec close(pid()) -> ok;
|
|
|
|
(state()) -> state().
|
2016-12-11 13:03:37 +01:00
|
|
|
close(Ref) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
xmpp_stream_in:close(Ref).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec close(pid(), atom()) -> ok.
|
2017-04-15 07:30:41 +02:00
|
|
|
close(Ref, Reason) ->
|
|
|
|
xmpp_stream_in:close(Ref, Reason).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2020-04-01 14:35:49 +02:00
|
|
|
-spec stop_async(pid()) -> ok.
|
|
|
|
stop_async(Pid) ->
|
|
|
|
xmpp_stream_in:stop_async(Pid).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
-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.
|
|
|
|
|
2017-02-24 05:57:57 +01:00
|
|
|
-spec send_error(state(), xmpp_element(), stanza_error()) -> state().
|
|
|
|
send_error(#{lserver := LServer} = State, Pkt, Err) ->
|
|
|
|
case ejabberd_hooks:run_fold(c2s_filter_send, LServer, {Pkt, State}, []) of
|
|
|
|
{drop, State1} -> State1;
|
|
|
|
{Pkt1, State1} -> xmpp_stream_in:send_error(State1, Pkt1, Err)
|
|
|
|
end.
|
|
|
|
|
2018-10-29 11:57:45 +01:00
|
|
|
-spec send_ws_ping(pid()) -> ok;
|
|
|
|
(state()) -> state().
|
|
|
|
send_ws_ping(Ref) ->
|
|
|
|
xmpp_stream_in:send_ws_ping(Ref).
|
|
|
|
|
2017-12-06 12:15:20 +01:00
|
|
|
-spec route(pid(), term()) -> boolean().
|
2017-02-18 07:36:27 +01:00
|
|
|
route(Pid, Term) ->
|
2017-12-06 12:15:20 +01:00
|
|
|
ejabberd_cluster:send(Pid, Term).
|
2017-02-18 07:36:27 +01:00
|
|
|
|
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).
|
|
|
|
|
2017-02-23 08:12:19 +01:00
|
|
|
-spec host_up(binary()) -> ok.
|
|
|
|
host_up(Host) ->
|
2016-12-29 22:00:36 +01:00
|
|
|
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),
|
2019-05-15 16:21:09 +02:00
|
|
|
ejabberd_hooks:add(c2s_auth_result, Host, ?MODULE,
|
|
|
|
process_auth_result, 100),
|
2016-12-29 22:00:36 +01:00
|
|
|
ejabberd_hooks:add(c2s_handle_cast, Host, ?MODULE,
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_unexpected_cast, 100),
|
|
|
|
ejabberd_hooks:add(c2s_handle_call, Host, ?MODULE,
|
|
|
|
handle_unexpected_call, 100).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2017-02-23 08:12:19 +01:00
|
|
|
-spec host_down(binary()) -> ok.
|
|
|
|
host_down(Host) ->
|
|
|
|
ejabberd_hooks:delete(c2s_closed, Host, ?MODULE, process_closed, 100),
|
|
|
|
ejabberd_hooks:delete(c2s_terminated, Host, ?MODULE,
|
|
|
|
process_terminated, 100),
|
|
|
|
ejabberd_hooks:delete(c2s_unauthenticated_packet, Host, ?MODULE,
|
|
|
|
reject_unauthenticated_packet, 100),
|
|
|
|
ejabberd_hooks:delete(c2s_handle_info, Host, ?MODULE,
|
|
|
|
process_info, 100),
|
2019-05-15 16:21:09 +02:00
|
|
|
ejabberd_hooks:delete(c2s_auth_result, Host, ?MODULE,
|
|
|
|
process_auth_result, 100),
|
2017-02-23 08:12:19 +01:00
|
|
|
ejabberd_hooks:delete(c2s_handle_cast, Host, ?MODULE,
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_unexpected_cast, 100),
|
|
|
|
ejabberd_hooks:delete(c2s_handle_call, Host, ?MODULE,
|
|
|
|
handle_unexpected_call, 100).
|
2017-02-23 08:12:19 +01:00
|
|
|
|
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().
|
2020-06-01 21:33:55 +02:00
|
|
|
copy_state(NewState,
|
|
|
|
#{jid := JID, resource := Resource, auth_module := AuthModule,
|
|
|
|
lserver := LServer, pres_a := PresA} = 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,
|
|
|
|
auth_module => AuthModule,
|
2017-05-17 13:47:35 +02:00
|
|
|
pres_a => PresA},
|
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),
|
2017-12-26 16:55:57 +01:00
|
|
|
State1 = change_shaper(State),
|
|
|
|
Conn = get_conn_type(State1),
|
|
|
|
State2 = State1#{conn => Conn, resource => R, jid => JID},
|
2017-01-23 14:30:16 +01:00
|
|
|
Prio = case maps:get(pres_last, State, undefined) of
|
|
|
|
undefined -> undefined;
|
2016-12-29 22:00:36 +01:00
|
|
|
Pres -> get_priority_from_presence(Pres)
|
|
|
|
end,
|
|
|
|
Info = [{ip, IP}, {conn, Conn}, {auth_module, AuthModule}],
|
2023-11-21 13:55:40 +01:00
|
|
|
case State of
|
2023-11-22 18:34:18 +01:00
|
|
|
#{bind2_session_id := Tag} ->
|
2023-11-21 13:55:40 +01:00
|
|
|
ejabberd_sm:open_session(SID, U, S, R, Prio, Info, Tag);
|
|
|
|
_ ->
|
|
|
|
ejabberd_sm:open_session(SID, U, S, R, Prio, Info)
|
|
|
|
end,
|
2017-12-26 16:55:57 +01:00
|
|
|
xmpp_stream_in:establish(State2).
|
2016-12-29 22:00:36 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Hooks
|
|
|
|
%%%===================================================================
|
2017-02-16 09:00:26 +01:00
|
|
|
process_info(#{lserver := LServer} = State, {route, Packet}) ->
|
2016-12-29 22:00:36 +01:00
|
|
|
{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;
|
2024-03-20 10:57:12 +01:00
|
|
|
process_info(State, reset_vcard_xupdate_resend_presence) ->
|
|
|
|
case maps:get(pres_last, State, error) of
|
|
|
|
error -> State;
|
|
|
|
Pres ->
|
|
|
|
Pres2 = xmpp:remove_subtag(Pres, #vcard_xupdate{}),
|
|
|
|
process_self_presence(State#{pres_last => Pres2}, Pres2)
|
|
|
|
end;
|
2017-05-17 13:47:35 +02:00
|
|
|
process_info(#{jid := JID} = State, {resend_presence, To}) ->
|
2017-01-23 14:30:16 +01:00
|
|
|
case maps:get(pres_last, State, error) of
|
|
|
|
error -> State;
|
2017-05-17 13:47:35 +02:00
|
|
|
Pres when To == undefined ->
|
|
|
|
process_self_presence(State, Pres);
|
|
|
|
Pres when To#jid.luser == JID#jid.luser andalso
|
|
|
|
To#jid.lserver == JID#jid.lserver andalso
|
|
|
|
To#jid.lresource == <<"">> ->
|
|
|
|
process_self_presence(State, Pres);
|
|
|
|
Pres ->
|
|
|
|
process_presence_out(State, xmpp:set_to(Pres, To))
|
2017-01-09 15:02:17 +01:00
|
|
|
end;
|
2016-12-29 22:00:36 +01:00
|
|
|
process_info(State, Info) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2016-12-28 07:47:11 +01:00
|
|
|
State.
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_unexpected_call(State, From, Msg) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Msg]),
|
|
|
|
State.
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
handle_unexpected_cast(State, Msg) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2016-12-28 07:47:11 +01:00
|
|
|
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
|
|
|
|
2019-05-15 16:21:09 +02:00
|
|
|
process_auth_result(#{sasl_mech := Mech, auth_module := AuthModule,
|
|
|
|
socket := Socket, ip := IP, lserver := LServer} = State,
|
|
|
|
true, User) ->
|
2024-05-23 19:34:21 +02:00
|
|
|
misc:set_proc_label({?MODULE, User, LServer}),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("(~ts) Accepted c2s ~ts authentication for ~ts@~ts by ~ts backend from ~ts",
|
2019-05-15 16:21:09 +02:00
|
|
|
[xmpp_socket:pp(Socket), Mech, User, LServer,
|
|
|
|
ejabberd_auth:backend_type(AuthModule),
|
|
|
|
ejabberd_config:may_hide_data(misc:ip_to_list(IP))]),
|
|
|
|
State;
|
|
|
|
process_auth_result(#{sasl_mech := Mech,
|
|
|
|
socket := Socket, ip := IP, lserver := LServer} = State,
|
|
|
|
{false, Reason}, User) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("(~ts) Failed c2s ~ts authentication ~tsfrom ~ts: ~ts",
|
2019-05-15 16:21:09 +02:00
|
|
|
[xmpp_socket:pp(Socket), Mech,
|
|
|
|
if User /= <<"">> -> ["for ", User, "@", LServer, " "];
|
|
|
|
true -> ""
|
|
|
|
end,
|
|
|
|
ejabberd_config:may_hide_data(misc:ip_to_list(IP)), Reason]),
|
|
|
|
State.
|
|
|
|
|
2016-12-29 22:00:36 +01:00
|
|
|
process_closed(State, Reason) ->
|
2020-04-01 14:35:49 +02:00
|
|
|
stop_async(self()),
|
|
|
|
State#{stop_reason => Reason}.
|
2016-12-29 22:00:36 +01:00
|
|
|
|
2022-02-02 16:19:13 +01:00
|
|
|
process_terminated(#{sid := SID, jid := JID, user := U, server := S, resource := R} = State,
|
2016-12-29 22:00:36 +01:00
|
|
|
Reason) ->
|
|
|
|
Status = format_reason(State, Reason),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("(~ts) Closing c2s session for ~ts: ~ts",
|
2022-02-02 18:33:45 +01:00
|
|
|
[case maps:find(socket, State) of
|
|
|
|
{ok, Socket} -> xmpp_socket:pp(Socket);
|
|
|
|
_ -> <<"unknown">>
|
|
|
|
end, jid:encode(JID), Status]),
|
2020-05-07 10:40:08 +02:00
|
|
|
Pres = #presence{type = unavailable,
|
|
|
|
from = JID,
|
|
|
|
to = jid:remove_resource(JID)},
|
2017-01-09 15:02:17 +01:00
|
|
|
State1 = case maps:is_key(pres_last, State) of
|
|
|
|
true ->
|
2017-04-19 01:20:28 +02:00
|
|
|
ejabberd_sm:close_session_unset_presence(SID, U, S, R,
|
|
|
|
Status),
|
2020-05-07 10:40:08 +02:00
|
|
|
broadcast_presence_unavailable(State, Pres, true);
|
2017-01-09 15:02:17 +01:00
|
|
|
false ->
|
2017-04-19 01:20:28 +02:00
|
|
|
ejabberd_sm:close_session(SID, U, S, R),
|
2020-05-07 10:40:08 +02:00
|
|
|
broadcast_presence_unavailable(State, Pres, false)
|
2017-01-09 15:02:17 +01:00
|
|
|
end,
|
2019-06-26 10:56:25 +02:00
|
|
|
bounce_message_queue(SID, JID),
|
2016-12-29 22:00:36 +01:00
|
|
|
State1;
|
2022-02-02 16:26:39 +01:00
|
|
|
process_terminated(#{stop_reason := {tls, _}} = State, Reason) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("(~ts) Failed to secure c2s connection: ~ts",
|
2022-02-02 18:33:45 +01:00
|
|
|
[case maps:find(socket, State) of
|
|
|
|
{ok, Socket} -> xmpp_socket:pp(Socket);
|
|
|
|
_ -> <<"unknown">>
|
|
|
|
end, format_reason(State, Reason)]),
|
2017-03-07 16:46:02 +01:00
|
|
|
State;
|
2016-12-29 22:00:36 +01:00
|
|
|
process_terminated(State, _Reason) ->
|
|
|
|
State.
|
2016-12-11 13:03:37 +01:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% xmpp_stream_in callbacks
|
|
|
|
%%%===================================================================
|
2017-08-05 18:59:32 +02:00
|
|
|
tls_options(#{lserver := LServer, tls_options := DefaultOpts,
|
|
|
|
stream_encrypted := Encrypted}) ->
|
|
|
|
TLSOpts1 = case {Encrypted, proplists:get_value(certfile, DefaultOpts)} of
|
|
|
|
{true, CertFile} when CertFile /= undefined -> DefaultOpts;
|
|
|
|
{_, _} ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case ejabberd_pkix:get_certfile(LServer) of
|
|
|
|
error -> DefaultOpts;
|
|
|
|
{ok, CertFile} ->
|
|
|
|
lists:keystore(certfile, 1, DefaultOpts,
|
|
|
|
{certfile, CertFile})
|
2017-08-05 18:59:32 +02:00
|
|
|
end
|
2017-01-09 15:02:17 +01:00
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
TLSOpts2 = case ejabberd_option:c2s_ciphers(LServer) of
|
2017-01-09 15:02:17 +01:00
|
|
|
undefined -> TLSOpts1;
|
|
|
|
Ciphers -> lists:keystore(ciphers, 1, TLSOpts1,
|
|
|
|
{ciphers, Ciphers})
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
TLSOpts3 = case ejabberd_option:c2s_protocol_options(LServer) of
|
2017-01-09 15:02:17 +01:00
|
|
|
undefined -> TLSOpts2;
|
|
|
|
ProtoOpts -> lists:keystore(protocol_options, 1, TLSOpts2,
|
|
|
|
{protocol_options, ProtoOpts})
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
TLSOpts4 = case ejabberd_option:c2s_dhfile(LServer) of
|
2017-01-09 15:02:17 +01:00
|
|
|
undefined -> TLSOpts3;
|
|
|
|
DHFile -> lists:keystore(dhfile, 1, TLSOpts3,
|
|
|
|
{dhfile, DHFile})
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
TLSOpts5 = case ejabberd_option:c2s_cafile(LServer) of
|
2017-01-09 15:02:17 +01:00
|
|
|
undefined -> TLSOpts4;
|
|
|
|
CAFile -> lists:keystore(cafile, 1, TLSOpts4,
|
|
|
|
{cafile, CAFile})
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
case ejabberd_option:c2s_tls_compression(LServer) of
|
2017-01-09 15:02:17 +01:00
|
|
|
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
|
|
|
|
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
|
|
|
|
2023-11-16 16:52:18 +01:00
|
|
|
inline_stream_features(#{lserver := LServer}) ->
|
|
|
|
ejabberd_hooks:run_fold(c2s_inline_features, LServer, {[], []}, [LServer]).
|
|
|
|
|
2020-12-03 15:07:09 +01:00
|
|
|
sasl_mechanisms(Mechs, #{lserver := LServer, stream_encrypted := Encrypted} = State) ->
|
2018-07-06 00:07:36 +02:00
|
|
|
Type = ejabberd_auth:store_type(LServer),
|
2019-06-14 11:33:26 +02:00
|
|
|
Mechs1 = ejabberd_option:disable_sasl_mechanisms(LServer),
|
2020-12-08 12:06:52 +01:00
|
|
|
|
|
|
|
ScramHash = ejabberd_option:auth_scram_hash(LServer),
|
|
|
|
ShaAv = Type == plain orelse (Type == scram andalso ScramHash == sha),
|
|
|
|
Sha256Av = Type == plain orelse (Type == scram andalso ScramHash == sha256),
|
|
|
|
Sha512Av = Type == plain orelse (Type == scram andalso ScramHash == sha512),
|
2018-07-06 00:07:36 +02:00
|
|
|
%% I re-created it from cyrsasl ets magic, but I think it's wrong
|
|
|
|
%% TODO: need to check before 18.09 release
|
|
|
|
lists:filter(
|
|
|
|
fun(<<"ANONYMOUS">>) ->
|
|
|
|
ejabberd_auth_anonymous:is_sasl_anonymous_enabled(LServer);
|
|
|
|
(<<"DIGEST-MD5">>) -> Type == plain;
|
2020-12-08 12:06:52 +01:00
|
|
|
(<<"SCRAM-SHA-1">>) -> ShaAv;
|
|
|
|
(<<"SCRAM-SHA-1-PLUS">>) -> ShaAv andalso Encrypted;
|
|
|
|
(<<"SCRAM-SHA-256">>) -> Sha256Av;
|
|
|
|
(<<"SCRAM-SHA-256-PLUS">>) -> Sha256Av andalso Encrypted;
|
|
|
|
(<<"SCRAM-SHA-512">>) -> Sha512Av;
|
|
|
|
(<<"SCRAM-SHA-512-PLUS">>) -> Sha512Av andalso Encrypted;
|
2018-07-06 00:07:36 +02:00
|
|
|
(<<"PLAIN">>) -> true;
|
2020-04-27 20:03:07 +02:00
|
|
|
(<<"X-OAUTH2">>) -> [ejabberd_auth_anonymous] /= ejabberd_auth:auth_modules(LServer);
|
2018-07-06 00:07:36 +02:00
|
|
|
(<<"EXTERNAL">>) -> maps:get(tls_verify, State, false);
|
|
|
|
(_) -> false
|
|
|
|
end, Mechs -- Mechs1).
|
|
|
|
|
2024-01-16 12:03:35 +01:00
|
|
|
sasl_options(#{lserver := LServer}) ->
|
|
|
|
case ejabberd_option:disable_sasl_scram_downgrade_protection(LServer) of
|
|
|
|
true -> [{scram_downgrade_protection, false}];
|
|
|
|
_ -> []
|
|
|
|
end.
|
|
|
|
|
2018-07-06 00:07:36 +02:00
|
|
|
get_password_fun(_Mech, #{lserver := LServer}) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
fun(U) ->
|
|
|
|
ejabberd_auth:get_password_with_authmodule(U, LServer)
|
|
|
|
end.
|
|
|
|
|
2018-07-06 00:07:36 +02:00
|
|
|
check_password_fun(<<"X-OAUTH2">>, #{lserver := LServer}) ->
|
|
|
|
fun(User, _AuthzId, Token) ->
|
|
|
|
case ejabberd_oauth:check_token(
|
|
|
|
User, LServer, [<<"sasl_auth">>], Token) of
|
|
|
|
true -> {true, ejabberd_oauth};
|
|
|
|
_ -> {false, ejabberd_oauth}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
check_password_fun(_Mech, #{lserver := LServer}) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
fun(U, AuthzId, P) ->
|
|
|
|
ejabberd_auth:check_password_with_authmodule(U, AuthzId, LServer, P)
|
|
|
|
end.
|
|
|
|
|
2018-07-06 00:07:36 +02:00
|
|
|
check_password_digest_fun(_Mech, #{lserver := LServer}) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
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-12-26 16:55:57 +01:00
|
|
|
lserver := LServer, socket := Socket,
|
2017-01-09 15:02:17 +01:00
|
|
|
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),
|
2019-06-14 11:33:26 +02:00
|
|
|
case acl:match_rule(LServer, Access,
|
|
|
|
#{usr => jid:split(JID), ip => IP}) of
|
2016-12-28 07:47:11 +01:00
|
|
|
allow ->
|
2017-01-09 15:02:17 +01:00
|
|
|
State1 = open_session(State#{resource => Resource,
|
|
|
|
sid => ejabberd_sm:make_sid()}),
|
2017-05-17 13:47:35 +02:00
|
|
|
State2 = ejabberd_hooks:run_fold(
|
|
|
|
c2s_session_opened, LServer, State1, []),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("(~ts) Opened c2s session for ~ts",
|
2017-12-26 16:55:57 +01:00
|
|
|
[xmpp_socket:pp(Socket), jid:encode(JID)]),
|
2017-05-17 13:47:35 +02:00
|
|
|
{ok, State2};
|
2016-12-28 07:47:11 +01:00
|
|
|
deny ->
|
|
|
|
ejabberd_hooks:run(forbidden_session_hook, LServer, [JID]),
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("(~ts) Forbidden c2s session for ~ts",
|
2018-09-19 22:12:14 +02:00
|
|
|
[xmpp_socket:pp(Socket), jid:encode(JID)]),
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Access denied by service policy"),
|
2016-12-28 07:47:11 +01:00
|
|
|
{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 ->
|
2018-06-14 13:00:47 +02:00
|
|
|
send(State#{lserver => ejabberd_config:get_myname()}, xmpp:serr_host_unknown());
|
2016-12-11 13:03:37 +01:00
|
|
|
true ->
|
2017-12-26 16:55:57 +01:00
|
|
|
State1 = change_shaper(State),
|
2019-06-21 20:06:32 +02:00
|
|
|
Opts = ejabberd_config:codec_options(),
|
2018-02-09 16:12:50 +01:00
|
|
|
State2 = State1#{codec_options => Opts},
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:run_fold(
|
2018-02-09 16:12:50 +01:00
|
|
|
c2s_stream_started, LServer, State2, [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
|
|
|
|
2019-05-15 16:21:09 +02:00
|
|
|
handle_auth_success(User, _Mech, AuthModule,
|
|
|
|
#{lserver := LServer} = State) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
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
|
|
|
|
2019-05-15 16:21:09 +02:00
|
|
|
handle_auth_failure(User, _Mech, Reason,
|
|
|
|
#{lserver := LServer} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(c2s_auth_result, LServer, State, [{false, Reason}, 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-02-14 15:09:25 +01:00
|
|
|
handle_authenticated_packet(Pkt, #{lserver := LServer, jid := JID,
|
|
|
|
ip := {IP, _}} = State) ->
|
|
|
|
Pkt1 = xmpp:put_meta(Pkt, ip, IP),
|
2016-12-28 07:47:11 +01:00
|
|
|
State1 = ejabberd_hooks:run_fold(c2s_authenticated_packet,
|
2017-02-14 15:09:25 +01:00
|
|
|
LServer, State, [Pkt1]),
|
2017-01-09 15:02:17 +01:00
|
|
|
#jid{luser = LUser} = JID,
|
2017-02-14 15:09:25 +01:00
|
|
|
{Pkt2, State2} = ejabberd_hooks:run_fold(
|
|
|
|
user_send_packet, LServer, {Pkt1, State1}, []),
|
|
|
|
case Pkt2 of
|
2017-01-09 15:02:17 +01:00
|
|
|
drop ->
|
|
|
|
State2;
|
2017-02-19 22:43:10 +01:00
|
|
|
#iq{type = set, sub_els = [_]} ->
|
2017-12-09 20:39:43 +01:00
|
|
|
try xmpp:try_subtag(Pkt2, #xmpp_session{}) of
|
2017-02-19 22:43:10 +01:00
|
|
|
#xmpp_session{} ->
|
2020-04-09 09:22:24 +02:00
|
|
|
% It seems that some client are expecting to have response
|
|
|
|
% to session request be sent from server jid, let's make
|
|
|
|
% sure it is that.
|
|
|
|
Pkt3 = xmpp:set_to(Pkt2, jid:make(<<>>, LServer, <<>>)),
|
|
|
|
send(State2, xmpp:make_iq_result(Pkt3));
|
2017-02-19 22:43:10 +01:00
|
|
|
_ ->
|
2017-02-20 13:07:34 +01:00
|
|
|
check_privacy_then_route(State2, Pkt2)
|
2017-12-09 20:39:43 +01:00
|
|
|
catch _:{xmpp_codec, Why} ->
|
|
|
|
Txt = xmpp:io_format_error(Why),
|
|
|
|
Lang = maps:get(lang, State),
|
|
|
|
Err = xmpp:err_bad_request(Txt, Lang),
|
|
|
|
send_error(State2, Pkt2, Err)
|
2017-02-19 22:43:10 +01:00
|
|
|
end;
|
2017-01-09 15:02:17 +01:00
|
|
|
#presence{to = #jid{luser = LUser, lserver = LServer,
|
|
|
|
lresource = <<"">>}} ->
|
2017-02-14 15:09:25 +01:00
|
|
|
process_self_presence(State2, Pkt2);
|
2016-12-28 07:47:11 +01:00
|
|
|
#presence{} ->
|
2017-02-14 15:09:25 +01:00
|
|
|
process_presence_out(State2, Pkt2);
|
2016-12-28 07:47:11 +01:00
|
|
|
_ ->
|
2017-02-14 15:09:25 +01:00
|
|
|
check_privacy_then_route(State2, Pkt2)
|
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]).
|
|
|
|
|
2023-11-16 16:52:18 +01:00
|
|
|
handle_sasl2_inline(Els, #{lserver := LServer} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(c2s_handle_sasl2_inline, LServer,
|
|
|
|
{State, Els, []}, []).
|
|
|
|
|
|
|
|
handle_sasl2_inline_post(Els, Results, #{lserver := LServer} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(c2s_handle_sasl2_inline_post, LServer,
|
|
|
|
State, [Els, Results]).
|
|
|
|
|
|
|
|
handle_bind2_inline(Els, #{lserver := LServer} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(c2s_handle_bind2_inline, LServer,
|
2023-11-20 18:50:11 +01:00
|
|
|
{State, Els, []}, []).
|
|
|
|
|
|
|
|
handle_bind2_inline_post(Els, Results, #{lserver := LServer} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(c2s_handle_bind2_inline_post, LServer,
|
|
|
|
State, [Els, Results]).
|
2023-11-16 16:52:18 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
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]) ->
|
2017-05-08 13:34:35 +02:00
|
|
|
Access = proplists:get_value(access, Opts, all),
|
|
|
|
Shaper = proplists:get_value(shaper, Opts, none),
|
2017-01-09 15:02:17 +01:00
|
|
|
TLSOpts1 = lists:filter(
|
|
|
|
fun({certfile, _}) -> true;
|
|
|
|
({ciphers, _}) -> true;
|
|
|
|
({dhfile, _}) -> true;
|
|
|
|
({cafile, _}) -> true;
|
2017-04-30 18:01:47 +02:00
|
|
|
({protocol_options, _}) -> true;
|
2017-01-09 15:02:17 +01:00
|
|
|
(_) -> false
|
|
|
|
end, Opts),
|
2017-04-30 18:01:47 +02:00
|
|
|
TLSOpts2 = case proplists:get_bool(tls_compression, Opts) of
|
|
|
|
false -> [compression_none | TLSOpts1];
|
|
|
|
true -> TLSOpts1
|
2017-01-09 15:02:17 +01:00
|
|
|
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),
|
2019-06-14 11:33:26 +02:00
|
|
|
Timeout = ejabberd_option:negotiation_timeout(),
|
2017-04-30 18:01:47 +02:00
|
|
|
State1 = State#{tls_options => TLSOpts2,
|
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(),
|
2016-12-11 16:24:51 +01:00
|
|
|
zlib => Zlib,
|
2019-06-14 11:33:26 +02:00
|
|
|
lang => ejabberd_option:language(),
|
2018-06-14 13:00:47 +02:00
|
|
|
server => ejabberd_config:get_myname(),
|
|
|
|
lserver => ejabberd_config:get_myname(),
|
2016-12-11 13:03:37 +01:00
|
|
|
access => Access,
|
|
|
|
shaper => Shaper},
|
2018-02-20 09:38:00 +01:00
|
|
|
State2 = xmpp_stream_in:set_timeout(State1, Timeout),
|
2024-05-23 19:34:21 +02:00
|
|
|
misc:set_proc_label({?MODULE, init_state}),
|
2018-02-20 09:38:00 +01:00
|
|
|
ejabberd_hooks:run_fold(c2s_init, {ok, State2}, [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) ->
|
2017-01-23 14:30:16 +01:00
|
|
|
Pres = case maps:get(pres_last, State, error) of
|
|
|
|
error ->
|
2016-12-11 16:24:51 +01:00
|
|
|
BareJID = jid:remove_resource(JID),
|
2017-01-23 14:30:16 +01:00
|
|
|
#presence{from = JID, to = BareJID, type = unavailable};
|
|
|
|
P -> P
|
2016-07-18 14:01:32 +02:00
|
|
|
end,
|
2016-12-29 22:00:36 +01:00
|
|
|
reply(From, Pres),
|
|
|
|
State;
|
2017-07-07 10:55:08 +02:00
|
|
|
handle_call({set_presence, Pres}, From, State) ->
|
|
|
|
reply(From, ok),
|
|
|
|
process_self_presence(State, Pres);
|
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
|
|
|
|
2016-12-29 22:00:36 +01:00
|
|
|
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 ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(IQ, xmpp:err_service_unavailable()),
|
2016-12-11 13:03:37 +01:00
|
|
|
{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-02-24 05:57:57 +01:00
|
|
|
%% This function should be as simple as process_iq_in/2,
|
|
|
|
%% however, we don't route errors to MUC rooms in order
|
|
|
|
%% to avoid kicking us, because having a MUC room's JID blocked
|
|
|
|
%% most likely means having only some particular participant
|
|
|
|
%% blocked, i.e. room@conference.server.org/participant.
|
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 ->
|
2017-02-24 05:57:57 +01:00
|
|
|
{false, State};
|
2016-12-11 13:03:37 +01:00
|
|
|
deny ->
|
|
|
|
case xmpp:has_subtag(Msg, #muc_user{}) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(
|
|
|
|
Msg, xmpp:err_service_unavailable())
|
2016-12-11 13:03:37 +01:00
|
|
|
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,
|
2017-05-17 13:47:35 +02:00
|
|
|
#presence{from = From, type = T} = Pres) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
State = ejabberd_hooks:run_fold(c2s_presence_in, LServer, State0, [Pres]),
|
|
|
|
case T of
|
|
|
|
probe ->
|
2017-05-17 13:47:35 +02:00
|
|
|
route_probe_reply(From, State),
|
|
|
|
{false, State};
|
2016-12-11 13:03:37 +01:00
|
|
|
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 ->
|
2017-05-17 13:47:35 +02:00
|
|
|
{true, State};
|
2016-12-11 13:03:37 +01:00
|
|
|
deny ->
|
|
|
|
{false, State}
|
2002-12-17 21:49:45 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2017-05-17 13:47:35 +02:00
|
|
|
-spec route_probe_reply(jid(), state()) -> ok.
|
|
|
|
route_probe_reply(From, #{jid := To,
|
|
|
|
pres_last := LastPres,
|
|
|
|
pres_timestamp := TS} = State) ->
|
|
|
|
{LUser, LServer, LResource} = jid:tolower(To),
|
|
|
|
IsAnotherResource = case jid:tolower(From) of
|
|
|
|
{LUser, LServer, R} when R /= LResource -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
Subscription = get_subscription(To, From),
|
|
|
|
if IsAnotherResource orelse
|
|
|
|
Subscription == both orelse Subscription == from ->
|
2018-11-10 16:56:34 +01:00
|
|
|
Packet = xmpp:set_from_to(LastPres, To, From),
|
|
|
|
Packet2 = misc:add_delay_info(Packet, To, TS),
|
|
|
|
case privacy_check_packet(State, Packet2, out) of
|
2016-12-11 13:03:37 +01:00
|
|
|
deny ->
|
|
|
|
ok;
|
|
|
|
allow ->
|
|
|
|
ejabberd_hooks:run(presence_probe_hook,
|
|
|
|
LServer,
|
|
|
|
[From, To, self()]),
|
2018-11-10 16:56:34 +01:00
|
|
|
ejabberd_router:route(Packet2)
|
2016-12-11 13:03:37 +01:00
|
|
|
end;
|
2017-05-17 13:47:35 +02:00
|
|
|
true ->
|
2016-12-11 13:03:37 +01:00
|
|
|
ok
|
|
|
|
end;
|
2017-05-17 13:47:35 +02:00
|
|
|
route_probe_reply(_, _) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
ok.
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec process_presence_out(state(), presence()) -> state().
|
2018-01-26 13:02:06 +01:00
|
|
|
process_presence_out(#{lserver := LServer, jid := JID,
|
2019-06-14 11:33:26 +02:00
|
|
|
lang := Lang, pres_a := PresA} = State0,
|
2016-12-11 13:03:37 +01:00
|
|
|
#presence{from = From, to = To, type = Type} = Pres) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
State1 =
|
|
|
|
if Type == subscribe; Type == subscribed;
|
|
|
|
Type == unsubscribe; Type == unsubscribed ->
|
|
|
|
Access = mod_roster_opt:access(LServer),
|
|
|
|
MyBareJID = jid:remove_resource(JID),
|
|
|
|
case acl:match_rule(LServer, Access, MyBareJID) of
|
|
|
|
deny ->
|
2019-06-22 16:08:45 +02:00
|
|
|
AccessErrTxt = ?T("Access denied by service policy"),
|
2019-06-14 11:33:26 +02:00
|
|
|
AccessErr = xmpp:err_forbidden(AccessErrTxt, Lang),
|
|
|
|
send_error(State0, Pres, AccessErr);
|
|
|
|
allow ->
|
|
|
|
ejabberd_hooks:run(roster_out_subscription, LServer, [Pres]),
|
|
|
|
State0
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
State0
|
|
|
|
end,
|
|
|
|
case privacy_check_packet(State1, Pres, out) of
|
2017-12-23 22:45:01 +01:00
|
|
|
deny ->
|
2019-06-22 16:08:45 +02:00
|
|
|
PrivErrTxt = ?T("Your active privacy list has denied "
|
|
|
|
"the routing of this stanza."),
|
2017-12-23 22:45:01 +01:00
|
|
|
PrivErr = xmpp:err_not_acceptable(PrivErrTxt, Lang),
|
2019-06-14 11:33:26 +02:00
|
|
|
send_error(State1, Pres, PrivErr);
|
2017-12-23 22:45:01 +01:00
|
|
|
allow when Type == subscribe; Type == subscribed;
|
|
|
|
Type == unsubscribe; Type == unsubscribed ->
|
|
|
|
BareFrom = jid:remove_resource(From),
|
|
|
|
ejabberd_router:route(xmpp:set_from_to(Pres, BareFrom, To)),
|
2019-06-14 11:33:26 +02:00
|
|
|
State1;
|
2016-10-22 12:01:45 +02:00
|
|
|
allow when Type == error; Type == probe ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(Pres),
|
2019-06-14 11:33:26 +02:00
|
|
|
State1;
|
2016-10-22 12:01:45 +02:00
|
|
|
allow ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(Pres),
|
2017-12-23 22:45:01 +01:00
|
|
|
LTo = jid:tolower(To),
|
2017-05-17 13:47:35 +02:00
|
|
|
LBareTo = jid:remove_resource(LTo),
|
|
|
|
LBareFrom = jid:remove_resource(jid:tolower(From)),
|
|
|
|
if LBareTo /= LBareFrom ->
|
|
|
|
Subscription = get_subscription(From, To),
|
|
|
|
if Subscription /= both andalso Subscription /= from ->
|
|
|
|
A = case Type of
|
|
|
|
available -> ?SETS:add_element(LTo, PresA);
|
|
|
|
unavailable -> ?SETS:del_element(LTo, PresA)
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
State1#{pres_a => A};
|
2017-05-17 13:47:35 +02:00
|
|
|
true ->
|
2019-06-14 11:33:26 +02:00
|
|
|
State1
|
2017-05-17 13:47:35 +02:00
|
|
|
end;
|
|
|
|
true ->
|
2019-06-14 11:33:26 +02:00
|
|
|
State1
|
2017-05-17 13:47:35 +02:00
|
|
|
end
|
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().
|
2018-12-10 09:53:27 +01:00
|
|
|
process_self_presence(#{lserver := LServer, sid := SID,
|
2016-12-11 13:03:37 +01:00
|
|
|
user := U, server := S, resource := R} = State,
|
|
|
|
#presence{type = unavailable} = Pres) ->
|
|
|
|
Status = xmpp:get_text(Pres#presence.status),
|
2019-06-14 11:33:26 +02:00
|
|
|
_ = ejabberd_sm:unset_presence(SID, U, S, R, Status),
|
2017-01-09 15:02:17 +01:00
|
|
|
{Pres1, State1} = ejabberd_hooks:run_fold(
|
|
|
|
c2s_self_presence, LServer, {Pres, State}, []),
|
2020-05-07 10:40:08 +02:00
|
|
|
State2 = broadcast_presence_unavailable(State1, Pres1, true),
|
2017-01-09 15:02:17 +01:00
|
|
|
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),
|
2019-06-14 11:33:26 +02:00
|
|
|
_ = 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,
|
2019-02-27 09:56:20 +01:00
|
|
|
pres_timestamp => erlang:timestamp()},
|
2016-12-11 13:03:37 +01:00
|
|
|
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
|
|
|
|
2018-12-10 09:53:27 +01:00
|
|
|
-spec update_priority(state(), presence()) -> ok | {error, notfound}.
|
|
|
|
update_priority(#{sid := SID, user := U, server := S, resource := R},
|
2016-12-11 13:03:37 +01:00
|
|
|
Pres) ->
|
|
|
|
Priority = get_priority_from_presence(Pres),
|
2018-12-10 09:53:27 +01:00
|
|
|
ejabberd_sm:set_presence(SID, U, S, R, Priority, Pres).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2020-05-07 10:40:08 +02:00
|
|
|
-spec broadcast_presence_unavailable(state(), presence(), boolean()) -> state().
|
|
|
|
broadcast_presence_unavailable(#{jid := JID, pres_a := PresA} = State, Pres,
|
|
|
|
BroadcastToRoster) ->
|
2017-05-17 13:47:35 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer} = JID,
|
2020-05-07 10:40:08 +02:00
|
|
|
BareJID = jid:tolower(jid:remove_resource(JID)),
|
|
|
|
Items1 = case BroadcastToRoster of
|
|
|
|
true ->
|
|
|
|
Roster = ejabberd_hooks:run_fold(roster_get, LServer,
|
|
|
|
[], [{LUser, LServer}]),
|
|
|
|
lists:foldl(
|
2022-07-16 23:23:48 +02:00
|
|
|
fun(#roster_item{jid = ItemJID, subscription = Sub}, Acc)
|
2020-05-07 10:40:08 +02:00
|
|
|
when Sub == both; Sub == from ->
|
2022-07-16 23:23:48 +02:00
|
|
|
maps:put(jid:tolower(ItemJID), 1, Acc);
|
2020-05-07 10:40:08 +02:00
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, #{BareJID => 1}, Roster);
|
|
|
|
_ ->
|
|
|
|
#{BareJID => 1}
|
|
|
|
end,
|
2017-05-17 13:47:35 +02:00
|
|
|
Items2 = ?SETS:fold(
|
2020-05-07 10:40:08 +02:00
|
|
|
fun(LJID, Acc) ->
|
|
|
|
maps:put(LJID, 1, Acc)
|
|
|
|
end, Items1, PresA),
|
|
|
|
|
|
|
|
JIDs = lists:filtermap(
|
|
|
|
fun(LJid) ->
|
|
|
|
To = jid:make(LJid),
|
|
|
|
P = xmpp:set_to(Pres, To),
|
|
|
|
case privacy_check_packet(State, P, out) of
|
|
|
|
allow -> {true, To};
|
|
|
|
deny -> false
|
|
|
|
end
|
|
|
|
end, maps:keys(Items2)),
|
2016-12-11 13:03:37 +01:00
|
|
|
route_multiple(State, JIDs, Pres),
|
|
|
|
State#{pres_a => ?SETS:new()}.
|
|
|
|
|
|
|
|
-spec broadcast_presence_available(state(), presence(), boolean()) -> state().
|
2017-05-17 13:47:35 +02:00
|
|
|
broadcast_presence_available(#{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},
|
2017-05-17 13:47:35 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer} = JID,
|
|
|
|
BareJID = jid:remove_resource(JID),
|
|
|
|
Items = ejabberd_hooks:run_fold(roster_get, LServer,
|
|
|
|
[], [{LUser, LServer}]),
|
|
|
|
{FJIDs, TJIDs} =
|
|
|
|
lists:foldl(
|
2022-07-16 23:23:48 +02:00
|
|
|
fun(#roster_item{jid = To, subscription = Sub}, {F, T}) ->
|
2017-05-17 13:47:35 +02:00
|
|
|
F1 = if Sub == both orelse Sub == from ->
|
|
|
|
Pres1 = xmpp:set_to(Pres, To),
|
|
|
|
case privacy_check_packet(State, Pres1, out) of
|
|
|
|
allow -> [To|F];
|
|
|
|
deny -> F
|
|
|
|
end;
|
|
|
|
true -> F
|
|
|
|
end,
|
|
|
|
T1 = if Sub == both orelse Sub == to ->
|
|
|
|
Probe1 = xmpp:set_to(Probe, To),
|
|
|
|
case privacy_check_packet(State, Probe1, out) of
|
|
|
|
allow -> [To|T];
|
|
|
|
deny -> T
|
|
|
|
end;
|
|
|
|
true -> T
|
|
|
|
end,
|
|
|
|
{F1, T1}
|
|
|
|
end, {[BareJID], [BareJID]}, Items),
|
2016-12-11 13:03:37 +01:00
|
|
|
route_multiple(State, TJIDs, Probe),
|
|
|
|
route_multiple(State, FJIDs, Pres),
|
2017-05-17 13:47:35 +02:00
|
|
|
State;
|
|
|
|
broadcast_presence_available(#{jid := JID} = State,
|
2016-12-11 13:03:37 +01:00
|
|
|
Pres, _FromUnavailable = false) ->
|
2017-05-17 13:47:35 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer} = JID,
|
|
|
|
BareJID = jid:remove_resource(JID),
|
|
|
|
Items = ejabberd_hooks:run_fold(
|
|
|
|
roster_get, LServer, [], [{LUser, LServer}]),
|
|
|
|
JIDs = lists:foldl(
|
2022-07-16 23:23:48 +02:00
|
|
|
fun(#roster_item{jid = To, subscription = Sub}, Tos)
|
2017-05-17 13:47:35 +02:00
|
|
|
when Sub == both orelse Sub == from ->
|
2022-07-16 23:23:48 +02:00
|
|
|
P = xmpp:set_to(Pres, To),
|
2017-05-17 13:47:35 +02:00
|
|
|
case privacy_check_packet(State, P, out) of
|
|
|
|
allow -> [To|Tos];
|
|
|
|
deny -> Tos
|
|
|
|
end;
|
|
|
|
(_, Tos) ->
|
|
|
|
Tos
|
|
|
|
end, [BareJID], Items),
|
2016-12-11 13:03:37 +01:00
|
|
|
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 ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Your active privacy list has denied "
|
|
|
|
"the routing of this stanza."),
|
2016-10-22 12:01:45 +02:00
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
2017-02-24 05:57:57 +01:00
|
|
|
send_error(State, Pkt, Err);
|
2015-04-20 22:03:18 +02:00
|
|
|
allow ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router: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 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),
|
2021-07-13 16:01:25 +02:00
|
|
|
ejabberd_router_multicast:route_multicast(From, LServer, JIDs, Pkt, false).
|
2003-08-03 21:09:40 +02:00
|
|
|
|
2017-05-17 13:47:35 +02:00
|
|
|
get_subscription(#jid{luser = LUser, lserver = LServer}, JID) ->
|
2018-01-26 13:02:06 +01:00
|
|
|
{Subscription, _, _} = ejabberd_hooks:run_fold(
|
|
|
|
roster_get_jid_info, LServer, {none, none, []},
|
|
|
|
[LUser, LServer, JID]),
|
2017-05-17 13:47:35 +02:00
|
|
|
Subscription.
|
|
|
|
|
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 ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:resource_conflict(S);
|
2016-12-11 13:03:37 +01:00
|
|
|
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;
|
2017-04-29 10:39:40 +02:00
|
|
|
closeold -> acceptnew; %% ejabberd_sm will close old session
|
2016-12-11 13:03:37 +01:00
|
|
|
closenew -> closenew;
|
2017-04-29 10:39:40 +02:00
|
|
|
acceptnew -> acceptnew
|
2016-12-11 13:03:37 +01:00
|
|
|
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.
|
|
|
|
|
2019-06-26 10:56:25 +02:00
|
|
|
-spec bounce_message_queue(ejabberd_sm:sid(), jid:jid()) -> ok.
|
2019-07-03 11:58:59 +02:00
|
|
|
bounce_message_queue({_, Pid} = SID, JID) ->
|
2019-06-26 10:56:25 +02:00
|
|
|
{U, S, R} = jid:tolower(JID),
|
|
|
|
SIDs = ejabberd_sm:get_session_sids(U, S, R),
|
|
|
|
case lists:member(SID, SIDs) of
|
|
|
|
true ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("The session for ~ts@~ts/~ts is supposed to "
|
2019-06-26 10:56:25 +02:00
|
|
|
"be unregistered, but session identifier ~p "
|
|
|
|
"still presents in the 'session' table",
|
2019-07-03 11:58:59 +02:00
|
|
|
[U, S, R, Pid]);
|
2019-06-26 10:56:25 +02:00
|
|
|
false ->
|
|
|
|
receive {route, Pkt} ->
|
|
|
|
ejabberd_router:route(Pkt),
|
|
|
|
bounce_message_queue(SID, JID)
|
2020-03-23 15:16:33 +01:00
|
|
|
after 100 ->
|
2019-06-26 10:56:25 +02:00
|
|
|
ok
|
|
|
|
end
|
2016-12-29 22:00:36 +01:00
|
|
|
end.
|
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec new_uniq_id() -> binary().
|
|
|
|
new_uniq_id() ->
|
|
|
|
iolist_to_binary(
|
2018-07-05 10:51:49 +02:00
|
|
|
[p1_rand:get_string(),
|
2019-02-27 09:56:20 +01:00
|
|
|
integer_to_binary(erlang: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,
|
2017-04-18 00:38:35 +02:00
|
|
|
case xmpp:get_from(Pkt) of
|
|
|
|
undefined ->
|
|
|
|
Pkt;
|
|
|
|
From ->
|
|
|
|
From1 = case jid:tolower(From) of
|
|
|
|
{U, S, R} -> JID;
|
|
|
|
{U, S, _} -> jid:replace_resource(JID, From#jid.resource);
|
|
|
|
_ -> From
|
|
|
|
end,
|
2020-03-25 16:00:16 +01:00
|
|
|
To1 = case xmpp:get_to(Pkt) of
|
|
|
|
#jid{lresource = <<>>} = To2 -> To2;
|
|
|
|
_ -> JID
|
|
|
|
end,
|
|
|
|
xmpp:set_from_to(Pkt, From1, To1)
|
2017-04-18 00:38:35 +02:00
|
|
|
end;
|
2017-01-09 15:02:17 +01:00
|
|
|
fix_from_to(Pkt, _State) ->
|
|
|
|
Pkt.
|
|
|
|
|
2017-12-26 16:55:57 +01:00
|
|
|
-spec change_shaper(state()) -> state().
|
2019-06-22 09:19:17 +02: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),
|
2019-06-14 11:33:26 +02:00
|
|
|
Shaper = ejabberd_shaper:match(LServer, ShaperName,
|
|
|
|
#{usr => jid:split(JID), ip => IP}),
|
2018-07-06 00:07:36 +02:00
|
|
|
xmpp_stream_in:change_shaper(State, ejabberd_shaper:new(Shaper)).
|
2016-12-11 13:03:37 +01: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
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
listen_opt_type(starttls) ->
|
|
|
|
econf:bool();
|
|
|
|
listen_opt_type(starttls_required) ->
|
|
|
|
econf:bool();
|
|
|
|
listen_opt_type(tls_verify) ->
|
|
|
|
econf:bool();
|
2018-09-18 11:53:36 +02:00
|
|
|
listen_opt_type(zlib) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:and_then(
|
|
|
|
econf:bool(),
|
|
|
|
fun(false) -> false;
|
|
|
|
(true) ->
|
|
|
|
ejabberd:start_app(ezlib),
|
|
|
|
true
|
|
|
|
end).
|
2018-09-18 11:53:36 +02:00
|
|
|
|
|
|
|
listen_options() ->
|
|
|
|
[{access, all},
|
|
|
|
{shaper, none},
|
|
|
|
{ciphers, undefined},
|
|
|
|
{dhfile, undefined},
|
|
|
|
{cafile, undefined},
|
|
|
|
{protocol_options, undefined},
|
|
|
|
{tls, false},
|
|
|
|
{tls_compression, false},
|
|
|
|
{starttls, false},
|
|
|
|
{starttls_required, false},
|
|
|
|
{tls_verify, false},
|
|
|
|
{zlib, false},
|
|
|
|
{max_stanza_size, infinity},
|
2020-05-05 01:34:12 +02:00
|
|
|
{max_fsm_queue, 10000}].
|