From d2ddf6be603c66fbc1b09827176cec7e5d6e294c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Fri, 23 Apr 2021 14:14:18 +0200 Subject: [PATCH 01/24] Restrict arguments of run_for_host_type - For binary: run as before - For 'undefined': log an error and return Acc (no-op) This might happen if host type is taken from the Acc. We need to eradicate this case from the code later. --- src/ejabberd_hooks.erl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ejabberd_hooks.erl b/src/ejabberd_hooks.erl index f331438c3af..bc774c14839 100644 --- a/src/ejabberd_hooks.erl +++ b/src/ejabberd_hooks.erl @@ -139,11 +139,16 @@ run_global(HookName, Acc, Args) -> %% @doc run hook for the host type, for more details see run_fold/4 documentation. -spec run_for_host_type(HookName :: atom(), - HostType :: binary(), + HostType :: binary() | undefined, Acc :: term(), Args :: [term()]) -> NewAcc :: term() | stopped. -run_for_host_type(HookName, HostType, Acc, Args) -> +run_for_host_type(HookName, undefined, Acc, Args) -> + ?LOG_ERROR(#{what => undefined_host_type, + text => <<"Running hook for an undefined host type">>, + hook_name => HookName, hook_acc => Acc, hook_args => Args}), + Acc; +run_for_host_type(HookName, HostType, Acc, Args) when is_binary(HostType) -> run_fold(HookName, HostType, Acc, Args). %%%---------------------------------------------------------------------- From d317c2e304bd34e2b4d08dd10abc32647b7ce029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Fri, 23 Apr 2021 11:55:40 +0200 Subject: [PATCH 02/24] Get rid of deprecated functions They have been deprecated for a long time and they conflict with new functions that will be introduced for host_type. --- src/ejabberd_sm.erl | 106 -------------------------------------------- 1 file changed, 106 deletions(-) diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index 61ba5bf7ee1..08abefc09a3 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -65,39 +65,6 @@ get_user_present_pids/2 ]). -%% Deprecated API --export([ - open_session/5, - open_session/6, - close_session/6, - close_session_unset_presence/7, - unset_presence/7, - get_raw_sessions/2, - store_info/4, - set_presence/8, - remove_info/4, - get_user_resources/2, - get_session_pid/3, - get_session/3, - get_session_ip/3, - get_user_present_resources/2 - ]). - --deprecated({open_session, 5, eventually}). --deprecated({open_session, 6, eventually}). --deprecated({close_session, 6, eventually}). --deprecated({close_session_unset_presence, 7, eventually}). --deprecated({unset_presence, 7, eventually}). --deprecated({get_raw_sessions, 2, eventually}). --deprecated({store_info, 4, eventually}). --deprecated({set_presence, 8, eventually}). --deprecated({remove_info, 4, eventually}). --deprecated({get_user_resources, 2, eventually}). --deprecated({get_session_pid, 3, eventually}). --deprecated({get_session, 3, eventually}). --deprecated({get_session_ip, 3, eventually}). --deprecated({get_user_present_resources, 2, eventually}). - %% Hook handlers -export([node_cleanup/2]). @@ -1082,76 +1049,3 @@ get_cached_unique_count() -> -spec sm_backend() -> backend(). sm_backend() -> ejabberd_sm_backend:backend(). - -%%==================================================================== -%% Deprecated API -%%==================================================================== - --define(FUNCTION_STRING, - ?MODULE_STRING ++ ":" ++ - atom_to_list(?FUNCTION_NAME) ++ "/" ++ - integer_to_list(?FUNCTION_ARITY)). - --define(DEPRECATE_FUNCTION, - mongoose_deprecations:log( - {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}, - #{what => sm_function_deprecated, - text => <<"The function ", (list_to_binary(?FUNCTION_STRING))/binary, - " is deprecated, please use the #jid{} equivalent instead">>}, - [{log_level, warning}])). - -open_session(SID, U, S, R, Info) -> - ?DEPRECATE_FUNCTION, - open_session(SID, jid:make(U, S, R), undefined, Info). - -open_session(SID, U, S, R, Priority, Info) -> - ?DEPRECATE_FUNCTION, - open_session(SID, jid:make(U, S, R), Priority, Info). - -close_session(Acc, SID, U, S, R, Reason) -> - ?DEPRECATE_FUNCTION, - close_session(Acc, SID, jid:make(U, S, R), Reason). - -close_session_unset_presence(Acc, SID, U, S, R, Status, Reason) -> - ?DEPRECATE_FUNCTION, - close_session_unset_presence(Acc, SID, jid:make(U, S, R), Status, Reason). - -unset_presence(Acc, SID, U, S, R, Status, Info) -> - ?DEPRECATE_FUNCTION, - unset_presence(Acc, SID, jid:make(U, S, R), Status, Info). - -get_raw_sessions(U, S) -> - ?DEPRECATE_FUNCTION, - get_raw_sessions(jid:make(U, S, <<>>)). - -store_info(U, S, R, {Key, _Value} = KV) -> - ?DEPRECATE_FUNCTION, - store_info(jid:make(U, S, R), {Key, _Value} = KV). - -set_presence(Acc, SID, U, S, R, Priority, Presence, Info) -> - ?DEPRECATE_FUNCTION, - set_presence(Acc, SID, jid:make(U, S, R), Priority, Presence, Info). - -remove_info(U, S, R, Key) -> - ?DEPRECATE_FUNCTION, - remove_info(jid:make(U, S, R), Key). - -get_user_resources(U, S) -> - ?DEPRECATE_FUNCTION, - get_user_resources(jid:make(U, S, <<>>)). - -get_session_pid(U, S, R) -> - ?DEPRECATE_FUNCTION, - get_session_pid(jid:make(U, S, R)). - -get_session(U, S, R) -> - ?DEPRECATE_FUNCTION, - get_session(jid:make(U, S, R)). - -get_session_ip(U, S, R) -> - ?DEPRECATE_FUNCTION, - get_session_ip(jid:make(U, S, R)). - -get_user_present_resources(U, S) -> - ?DEPRECATE_FUNCTION, - get_user_present_resources(jid:make(U, S, <<>>)). From 8f0abd4c130ec91d3150fc5c730bd565d4526923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Fri, 23 Apr 2021 11:59:15 +0200 Subject: [PATCH 03/24] Add host_type to sm_register_connection_hook This required adding it to open_session, what in turn required adding it to mongoose_client_api. --- src/ejabberd_c2s.erl | 9 ++++----- src/ejabberd_sm.erl | 16 +++++++++------- src/mongoose_client_api/mongoose_client_api.erl | 12 ++++++------ .../mongoose_client_api_sse.erl | 5 +++-- src/mongoose_hooks.erl | 8 ++++---- test/ejabberd_c2s_SUITE_mocks.erl | 2 +- test/ejabberd_sm_SUITE.erl | 2 +- 7 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index 10e608745db..cf83e6dd196 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -786,7 +786,7 @@ do_open_session_common(Acc, JID, #state{host_type = HostType, Conn = get_conn_type(NewStateData0), Info = #{ip => NewStateData0#state.ip, conn => Conn, auth_module => NewStateData0#state.auth_module }, - ReplacedPids = ejabberd_sm:open_session(SID, JID, Info), + ReplacedPids = ejabberd_sm:open_session(HostType, SID, JID, Info), RefsAndPids = [{monitor(process, PID), PID} || PID <- ReplacedPids], case RefsAndPids of @@ -855,7 +855,7 @@ session_established({xmlstreamelement, El}, StateData) -> El1 = fix_message_from_user(El, StateData#state.lang), Acc0 = element_to_origin_accum(El1, StateData), Acc1 = mongoose_hooks:c2s_preprocessing_hook(StateData#state.host_type, - Acc0, NewState), + Acc0, NewState), case mongoose_acc:get(hook, result, undefined, Acc1) of drop -> fsm_next_state(session_established, NewState); _ -> process_outgoing_stanza(Acc1, NewState) @@ -2051,8 +2051,7 @@ process_presence_subscription_and_route(Acc, Type, StateData) -> From = mongoose_acc:from_jid(Acc), HostType = StateData#state.host_type, To = mongoose_acc:to_jid(Acc), - Acc1 = mongoose_hooks:roster_out_subscription( - HostType, Acc, From, To, Type), + Acc1 = mongoose_hooks:roster_out_subscription(HostType, Acc, From, To, Type), check_privacy_and_route(Acc1, jid:to_bare(From), StateData). -spec check_privacy_and_route(Acc :: mongoose_acc:t(), @@ -3090,7 +3089,7 @@ do_resume_session(SMID, El, {sid, {_, Pid}}, StateData) -> Priority = get_priority_from_presence(NSD#state.pres_last), Info = #{ip => NSD#state.ip, conn => NSD#state.conn, auth_module => NSD#state.auth_module }, - ejabberd_sm:open_session(SID, NSD#state.jid, Priority, Info), + ejabberd_sm:open_session(NSD#state.host_type, SID, NSD#state.jid, Priority, Info), ok = mod_stream_management:register_smid(SMID, SID), try Resumed = stream_mgmt_resumed(NSD#state.stream_mgmt_id, diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index 08abefc09a3..0db14df5570 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -33,7 +33,7 @@ route/3, route/4, make_new_sid/0, - open_session/3, open_session/4, + open_session/4, open_session/5, close_session/4, store_info/2, get_info/2, @@ -185,24 +185,26 @@ route(From, To, Acc, El) -> make_new_sid() -> {erlang:system_time(microsecond), self()}. --spec open_session(SID, JID, Info) -> ReplacedPids when +-spec open_session(HostType, SID, JID, Info) -> ReplacedPids when + HostType :: binary(), SID :: 'undefined' | sid(), JID :: jid:jid(), Info :: info(), ReplacedPids :: [pid()]. -open_session(SID, JID, Info) -> - open_session(SID, JID, undefined, Info). +open_session(HostType, SID, JID, Info) -> + open_session(HostType, SID, JID, undefined, Info). --spec open_session(SID, JID, Priority, Info) -> ReplacedPids when +-spec open_session(HostType, SID, JID, Priority, Info) -> ReplacedPids when + HostType :: binary(), SID :: 'undefined' | sid(), JID :: jid:jid(), Priority :: integer() | undefined, Info :: info(), ReplacedPids :: [pid()]. -open_session(SID, JID, Priority, Info) -> +open_session(HostType, SID, JID, Priority, Info) -> set_session(SID, JID, Priority, Info), ReplacedPIDs = check_for_sessions_to_replace(JID), - mongoose_hooks:sm_register_connection_hook(JID#jid.lserver, SID, JID, Info), + mongoose_hooks:sm_register_connection_hook(HostType, SID, JID, Info), ReplacedPIDs. -spec close_session(Acc, SID, JID, Reason) -> Acc1 when diff --git a/src/mongoose_client_api/mongoose_client_api.erl b/src/mongoose_client_api/mongoose_client_api.erl index b13b8dc55fd..f193b1d6d19 100644 --- a/src/mongoose_client_api/mongoose_client_api.erl +++ b/src/mongoose_client_api/mongoose_client_api.erl @@ -97,8 +97,8 @@ authorize(AuthMethod, User, Password, HTTPMethod, Req, State) -> case do_authorize(AuthMethod, MaybeJID, Password, HTTPMethod) of noauth -> {true, Req, State}; - true -> - {true, Req, State#{user => User, jid => MaybeJID}}; + {true, Creds} -> + {true, Req, State#{user => User, jid => MaybeJID, creds => Creds}}; false -> mongoose_api_common:make_unauthorized_response(Req, State) end. @@ -108,11 +108,11 @@ do_authorize(AuthMethod, MaybeJID, Password, HTTPMethod) -> true -> noauth; false -> - check_password(MaybeJID, Password) andalso - mongoose_api_common:is_known_auth_method(AuthMethod) + mongoose_api_common:is_known_auth_method(AuthMethod) andalso + check_password(MaybeJID, Password) end. --spec check_password(jid:jid() | error, binary()) -> boolean(). +-spec check_password(jid:jid() | error, binary()) -> {true, mongoose_credentials:t()} | false. check_password(error, _) -> false; check_password(JID, Password) -> @@ -123,7 +123,7 @@ check_password(JID, Password) -> Creds1 = mongoose_credentials:set(Creds0, username, LUser), Creds2 = mongoose_credentials:set(Creds1, password, Password), case ejabberd_auth:authorize(Creds2) of - {ok, _} -> true; + {ok, Creds} -> {true, Creds}; _ -> false end; {error, not_found} -> false diff --git a/src/mongoose_client_api/mongoose_client_api_sse.erl b/src/mongoose_client_api/mongoose_client_api_sse.erl index 5602c0833d2..303f7d04204 100644 --- a/src/mongoose_client_api/mongoose_client_api_sse.erl +++ b/src/mongoose_client_api/mongoose_client_api_sse.erl @@ -16,12 +16,13 @@ init(_InitArgs, _LastEvtId, Req) -> {Authorization, Req2, State} = mongoose_client_api:is_authorized(Req1, State0), maybe_init(Authorization, Req2, State#{id => 1}). -maybe_init(true, Req, #{jid := JID} = State) -> +maybe_init(true, Req, #{jid := JID, creds := Creds} = State) -> SID = ejabberd_sm:make_new_sid(), UUID = uuid:uuid_to_string(uuid:get_v4(), binary_standard), Resource = <<"sse-", UUID/binary>>, NewJid = jid:replace_resource(JID, Resource), - ejabberd_sm:open_session(SID, NewJid, 1, #{}), + HostType = mongoose_credentials:host_type(Creds), + ejabberd_sm:open_session(HostType, SID, NewJid, 1, #{}), {ok, Req, State#{sid => SID, jid => NewJid}}; maybe_init(true, Req, State) -> %% This is for OPTIONS method diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 92fa0d3ed3c..1e98f04f599 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -735,14 +735,14 @@ sm_filter_offline_message(LServer, From, To, Packet) -> ejabberd_hooks:run_for_host_type(sm_filter_offline_message, LServer, false, [From, To, Packet]). --spec sm_register_connection_hook(LServer, SID, JID, Info) -> Result when - LServer :: jid:lserver(), +-spec sm_register_connection_hook(HostType, SID, JID, Info) -> Result when + HostType :: binary(), SID :: 'undefined' | ejabberd_sm:sid(), JID :: jid:jid(), Info :: ejabberd_sm:info(), Result :: ok. -sm_register_connection_hook(LServer, SID, JID, Info) -> - ejabberd_hooks:run_for_host_type(sm_register_connection_hook, LServer, ok, +sm_register_connection_hook(HostType, SID, JID, Info) -> + ejabberd_hooks:run_for_host_type(sm_register_connection_hook, HostType, ok, [SID, JID, Info]). -spec sm_remove_connection_hook(LServer, Acc, SID, JID, Info, Reason) -> Result when diff --git a/test/ejabberd_c2s_SUITE_mocks.erl b/test/ejabberd_c2s_SUITE_mocks.erl index e84c3561c9a..55db7d98969 100644 --- a/test/ejabberd_c2s_SUITE_mocks.erl +++ b/test/ejabberd_c2s_SUITE_mocks.erl @@ -7,7 +7,7 @@ setup() -> fun() -> {erlang:system_time(microsecond), self()} end), meck:expect(ejabberd_sm, close_session, fun(Acc, _SID, _JID, _Reason) -> Acc end), - meck:expect(ejabberd_sm, open_session, fun(_, _, _) -> [] end), + meck:expect(ejabberd_sm, open_session, fun(_, _, _, _) -> [] end), meck:new(ejabberd_socket), meck:expect(ejabberd_socket, close, diff --git a/test/ejabberd_sm_SUITE.erl b/test/ejabberd_sm_SUITE.erl index 5a36dbe4b8e..1a141195b9d 100644 --- a/test/ejabberd_sm_SUITE.erl +++ b/test/ejabberd_sm_SUITE.erl @@ -407,7 +407,7 @@ given_session_opened(Sid, {U, S, R}, Priority) -> given_session_opened(Sid, {U, S, R}, Priority, Info) -> JID = jid:make_noprep(U, S, R), - ejabberd_sm:open_session(Sid, JID, Priority, maps:from_list(Info)). + ejabberd_sm:open_session(S, Sid, JID, Priority, maps:from_list(Info)). when_session_opened(Sid, {U, S, R}, Priority, Info) -> given_session_opened(Sid, {U, S, R}, Priority, Info). From 95c6e1e35ef7913118bc198ab7de6b1d97e0d5d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Fri, 23 Apr 2021 14:22:41 +0200 Subject: [PATCH 04/24] Use host_type in sm_remove_connection_hook Take it from Acc as the hook is always run for a local user. --- src/ejabberd_sm.erl | 2 +- src/mongoose_hooks.erl | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index 0db14df5570..5ae147abdde 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -222,7 +222,7 @@ close_session(Acc, SID, JID, Reason) -> [] end, ejabberd_gen_sm:delete_session(sm_backend(), SID, LUser, LServer, LResource), - mongoose_hooks:sm_remove_connection_hook(JID#jid.lserver, Acc, SID, JID, Info, Reason). + mongoose_hooks:sm_remove_connection_hook(Acc, SID, JID, Info, Reason). -spec store_info(jid:jid(), info_item()) -> {ok, {any(), any()}} | {error, offline}. diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 1e98f04f599..964a645526d 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -67,7 +67,7 @@ sm_broadcast/6, sm_filter_offline_message/4, sm_register_connection_hook/4, - sm_remove_connection_hook/6, + sm_remove_connection_hook/5, unset_presence_hook/5, xmpp_bounce_message/2]). @@ -745,16 +745,16 @@ sm_register_connection_hook(HostType, SID, JID, Info) -> ejabberd_hooks:run_for_host_type(sm_register_connection_hook, HostType, ok, [SID, JID, Info]). --spec sm_remove_connection_hook(LServer, Acc, SID, JID, Info, Reason) -> Result when - LServer :: jid:lserver(), +-spec sm_remove_connection_hook(Acc, SID, JID, Info, Reason) -> Result when Acc :: mongoose_acc:t(), SID :: 'undefined' | ejabberd_sm:sid(), JID :: jid:jid(), Info :: ejabberd_sm:info(), Reason :: ejabberd_sm:close_reason(), Result :: mongoose_acc:t(). -sm_remove_connection_hook(LServer, Acc, SID, JID, Info, Reason) -> - ejabberd_hooks:run_for_host_type(sm_remove_connection_hook, LServer, Acc, +sm_remove_connection_hook(Acc, SID, JID, Info, Reason) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(sm_remove_connection_hook, HostType, Acc, [SID, JID, Info, Reason]). -spec unset_presence_hook(LServer, Acc, LUser, LResource, Status) -> Result when From 2c5ab01ffda69fb287fe212da2c8b6bec6edf00d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Fri, 23 Apr 2021 15:09:19 +0200 Subject: [PATCH 05/24] Set 'lserver' (and 'host_type') in Acc from the recipient Reason: The stanza is handled locally and host type can be used when running local hooks. Anyway, this clause is used only for sending MAM messages. --- src/ejabberd_sm.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index 5ae147abdde..8c2681a880b 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -140,7 +140,7 @@ start_link() -> Acc :: mongoose_acc:t(). route(From, To, #xmlel{} = Packet) -> Acc = mongoose_acc:new(#{ location => ?LOCATION, - lserver => From#jid.lserver, + lserver => To#jid.lserver, element => Packet, from_jid => From, to_jid => To }), From 9a5174c2ef3619ad603051e0192bb0d0fbefa0b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Fri, 23 Apr 2021 15:38:07 +0200 Subject: [PATCH 06/24] Use host type in xmpp_bounce_message It is safe to take it from the acc created for the local user. --- src/ejabberd_sm.erl | 2 +- src/mongoose_hooks.erl | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index 8c2681a880b..89b1a33279d 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -296,7 +296,7 @@ check_in_subscription(Acc, ToJID, _FromJID, _Type, _Reason) -> To :: jid:jid(), Packet :: exml:element(). bounce_offline_message(Acc, #jid{server = Server} = From, To, Packet) -> - Acc1 = mongoose_hooks:xmpp_bounce_message(Server, Acc), + Acc1 = mongoose_hooks:xmpp_bounce_message(Acc), {Acc2, Err} = jlib:make_error_reply(Acc1, Packet, mongoose_xmpp_errors:service_unavailable()), Acc3 = ejabberd_router:route(To, From, Acc2, Err), {stop, Acc3}. diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 964a645526d..3b13204d3a0 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -69,7 +69,7 @@ sm_register_connection_hook/4, sm_remove_connection_hook/5, unset_presence_hook/5, - xmpp_bounce_message/2]). + xmpp_bounce_message/1]). -export([roster_get/3, roster_get_jid_info/3, @@ -768,12 +768,12 @@ unset_presence_hook(LServer, Acc, LUser, LResource, Status) -> ejabberd_hooks:run_for_host_type(unset_presence_hook, LServer, Acc, [LUser, LServer, LResource, Status]). --spec xmpp_bounce_message(Server, Acc) -> Result when - Server :: jid:server(), +-spec xmpp_bounce_message(Acc) -> Result when Acc :: mongoose_acc:t(), Result :: mongoose_acc:t(). -xmpp_bounce_message(Server, Acc) -> - ejabberd_hooks:run_for_host_type(xmpp_bounce_message, Server, Acc, []). +xmpp_bounce_message(Acc) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(xmpp_bounce_message, HostType, Acc, []). %% Roster related hooks From e3aa84a9bbcfddff0777e7267da2cc10a76a95ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 26 Apr 2021 08:47:15 +0200 Subject: [PATCH 07/24] Get host type from Acc for (un)set_presence hooks The Acc is constructed in ejabberd_c2s from the local state, which contains the host type of the local user. --- src/ejabberd_sm.erl | 11 ++++------- src/mongoose_hooks.erl | 27 ++++++++++++++------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index 89b1a33279d..1c650b81448 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -295,7 +295,7 @@ check_in_subscription(Acc, ToJID, _FromJID, _Type, _Reason) -> From :: jid:jid(), To :: jid:jid(), Packet :: exml:element(). -bounce_offline_message(Acc, #jid{server = Server} = From, To, Packet) -> +bounce_offline_message(Acc, From, To, Packet) -> Acc1 = mongoose_hooks:xmpp_bounce_message(Acc), {Acc2, Err} = jlib:make_error_reply(Acc1, Packet, mongoose_xmpp_errors:service_unavailable()), Acc3 = ejabberd_router:route(To, From, Acc2, Err), @@ -353,9 +353,8 @@ get_raw_sessions(#jid{luser = LUser, lserver = LServer}) -> Presence :: any(), Info :: info(). set_presence(Acc, SID, JID, Priority, Presence, Info) -> - #jid{luser = LUser, lserver = LServer, lresource = LResource} = JID, set_session(SID, JID, Priority, Info), - mongoose_hooks:set_presence_hook(LServer, Acc, LUser, LResource, Presence). + mongoose_hooks:set_presence_hook(Acc, JID, Presence). -spec unset_presence(Acc, SID, JID, Status, Info) -> Acc1 when @@ -366,9 +365,8 @@ set_presence(Acc, SID, JID, Priority, Presence, Info) -> Status :: binary(), Info :: info(). unset_presence(Acc, SID, JID, Status, Info) -> - #jid{luser = LUser, lserver = LServer, lresource = LResource} = JID, set_session(SID, JID, undefined, Info), - mongoose_hooks:unset_presence_hook(LServer, Acc, LUser, LResource, Status). + mongoose_hooks:unset_presence_hook(Acc, JID, Status). -spec close_session_unset_presence(Acc, SID, JID, Status, Reason) -> Acc1 when @@ -379,9 +377,8 @@ unset_presence(Acc, SID, JID, Status, Info) -> Reason :: close_reason(), Acc1 :: mongoose_acc:t(). close_session_unset_presence(Acc, SID, JID, Status, Reason) -> - #jid{luser = LUser, lserver = LServer, lresource = LResource} = JID, Acc1 = close_session(Acc, SID, JID, Reason), - mongoose_hooks:unset_presence_hook(LServer, Acc1, LUser, LResource, Status). + mongoose_hooks:unset_presence_hook(Acc1, JID, Status). -spec get_session_pid(JID) -> none | pid() when diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 3b13204d3a0..9140206ed7f 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -5,6 +5,7 @@ %%% make sure they pass the expected arguments. -module(mongoose_hooks). +-include("jlib.hrl"). -include("mod_privacy.hrl"). -export([adhoc_local_items/4, @@ -63,12 +64,12 @@ -export([offline_groupchat_message_hook/5, offline_message_hook/5, - set_presence_hook/5, + set_presence_hook/3, sm_broadcast/6, sm_filter_offline_message/4, sm_register_connection_hook/4, sm_remove_connection_hook/5, - unset_presence_hook/5, + unset_presence_hook/3, xmpp_bounce_message/1]). -export([roster_get/3, @@ -704,13 +705,14 @@ offline_message_hook(LServer, Acc, From, To, Packet) -> ejabberd_hooks:run_for_host_type(offline_message_hook, LServer, Acc, [From, To, Packet]). --spec set_presence_hook(LServer, Acc, LUser, LResource, Presence) -> Result when - LServer :: jid:lserver(), Acc :: mongoose_acc:t(), LUser :: jid:luser(), - LResource :: jid:lresource(), +-spec set_presence_hook(Acc, JID, Presence) -> Result when + Acc :: mongoose_acc:t(), + JID :: jid:jid(), Presence :: any(), Result :: mongoose_acc:t(). -set_presence_hook(LServer, Acc, LUser, LResource, Presence) -> - ejabberd_hooks:run_for_host_type(set_presence_hook, LServer, Acc, +set_presence_hook(Acc, #jid{luser = LUser, lserver = LServer, lresource = LResource}, Presence) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(set_presence_hook, HostType, Acc, [LUser, LServer, LResource, Presence]). -spec sm_broadcast(LServer, Acc, From, To, Broadcast, SessionCount) -> Result when @@ -757,15 +759,14 @@ sm_remove_connection_hook(Acc, SID, JID, Info, Reason) -> ejabberd_hooks:run_for_host_type(sm_remove_connection_hook, HostType, Acc, [SID, JID, Info, Reason]). --spec unset_presence_hook(LServer, Acc, LUser, LResource, Status) -> Result when - LServer :: jid:lserver(), +-spec unset_presence_hook(Acc, JID, Status) -> Result when Acc :: mongoose_acc:t(), - LUser :: jid:luser(), - LResource :: jid:lresource(), + JID:: jid:jid(), Status :: binary(), Result :: mongoose_acc:t(). -unset_presence_hook(LServer, Acc, LUser, LResource, Status) -> - ejabberd_hooks:run_for_host_type(unset_presence_hook, LServer, Acc, +unset_presence_hook(Acc, #jid{luser = LUser, lserver = LServer, lresource = LResource}, Status) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(unset_presence_hook, HostType, Acc, [LUser, LServer, LResource, Status]). -spec xmpp_bounce_message(Acc) -> Result when From 8822067dea560b48fabb6a5473df0ece5e876a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 26 Apr 2021 09:10:23 +0200 Subject: [PATCH 08/24] Do not calculate host_type if it is already in state --- src/ejabberd_c2s.erl | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index cf83e6dd196..0f553913bfa 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -1033,10 +1033,11 @@ code_change(_OldVsn, StateName, StateData, _Extra) -> %%% system events -handle_info({exit, Reason}, _StateName, StateData) -> - Lang = StateData#state.lang, +handle_info({exit, Reason}, _StateName, + StateData = #state{lang = Lang, server = LServer, host_type = HostType}) -> Acc = mongoose_acc:new(#{ location => ?LOCATION, - lserver => ejabberd_c2s_state:server(StateData), + host_type => HostType, + lserver => LServer, element => undefined }), send_element(Acc, mongoose_xmpp_errors:stream_conflict(Lang, Reason), StateData), send_trailer(StateData), @@ -1048,9 +1049,11 @@ handle_info(replaced, _StateName, StateData) -> maybe_send_trailer_safe(StateData), {stop, normal, StateData#state{authenticated = replaced}}; handle_info(new_offline_messages, session_established, - #state{pres_last = Presence, pres_invis = Invisible, jid = JID} = StateData) + #state{pres_last = Presence, pres_invis = Invisible, jid = JID, + host_type = HostType} = StateData) when Presence =/= undefined orelse Invisible -> Acc = mongoose_acc:new(#{ location => ?LOCATION, + host_type => HostType, lserver => JID#jid.lserver, element => undefined }), resend_offline_messages(Acc, StateData), @@ -1133,8 +1136,9 @@ handle_incoming_message({send_text, Text}, StateName, StateData) -> fsm_next_state(StateName, StateData); handle_incoming_message({broadcast, Broadcast}, StateName, StateData) -> Acc = mongoose_acc:new(#{ location => ?LOCATION, - lserver => StateData#state.server, - element => undefined }), + host_type => StateData#state.host_type, + lserver => StateData#state.server, + element => undefined }), ?LOG_DEBUG(#{what => c2s_broadcast, brodcast_data => Broadcast, state_name => StateName, c2s_state => StateData}), @@ -1144,12 +1148,13 @@ handle_incoming_message({route, From, To, Acc}, StateName, StateData) -> process_incoming_stanza_with_conflict_check(From, To, Acc, StateName, StateData); handle_incoming_message({send_filtered, Feature, From, To, Packet}, StateName, StateData) -> % this is used by pubsub and should be rewritten when someone rewrites pubsub module + #state{server = Server, host_type = HostType} = StateData, Acc = mongoose_acc:new(#{ location => ?LOCATION, from_jid => From, to_jid => To, + host_type => HostType, lserver => To#jid.lserver, element => Packet }), - #state{server = Server, host_type = HostType} = StateData, Drop = mongoose_hooks:c2s_filter_packet(HostType, Server, StateData, Feature, To, Packet), case {Drop, StateData#state.jid} of @@ -1510,7 +1515,10 @@ terminate({handover_session, From}, StateName, StateData, UnreadMessages) -> terminate(normal, StateName, NewStateData, []); terminate(_Reason, StateName, StateData, UnreadMessages) -> InitialAcc0 = mongoose_acc:new( - #{location => ?LOCATION, lserver => StateData#state.server, element => undefined}), + #{location => ?LOCATION, + host_type => StateData#state.host_type, + lserver => StateData#state.server, + element => undefined}), Acc = case StateData#state.stream_mgmt of true -> mongoose_acc:set(stream_mgmt, h, StateData#state.stream_mgmt_in, InitialAcc0); _ -> InitialAcc0 @@ -1651,6 +1659,7 @@ send_element_from_server_jid(no_acc, #state{server = Server} = StateData, #xmlel Acc = mongoose_acc:new(#{location => ?LOCATION, from_jid => jid:make_noprep(<<>>, Server, <<>>), to_jid => StateData#state.jid, + host_type => StateData#state.host_type, lserver => Server, element => El }), send_element_from_server_jid(Acc, StateData, El); @@ -2090,6 +2099,7 @@ privacy_check_packet(#xmlel{} = Packet, From, To, Dir, StateData) -> Acc = mongoose_acc:new(#{ location => ?LOCATION, from_jid => From, to_jid => To, + host_type => StateData#state.host_type, lserver => StateData#state.server, element => Packet }), {_, Res} = privacy_check_packet(Acc, To, Dir, StateData), @@ -3176,7 +3186,10 @@ stream_mgmt_resumed(SMID, Handled) -> handover_session(SD, From)-> true = SD#state.stream_mgmt, Acc = mongoose_acc:new( - #{location => ?LOCATION, lserver => SD#state.server, element => undefined}), + #{location => ?LOCATION, + host_type => SD#state.host_type, + lserver => SD#state.server, + element => undefined}), ejabberd_sm:close_session(Acc, SD#state.sid, SD#state.jid, @@ -3323,9 +3336,10 @@ terminate_when_tls_required_but_not_enabled(_, _, StateData, El) -> %% @doc This function is executed when c2s receives a stanza from TCP connection. -spec element_to_origin_accum(jlib:xmlel(), StateData :: state()) -> mongoose_acc:t(). -element_to_origin_accum(El, #state{sid = SID, jid = JID, server = Server}) -> +element_to_origin_accum(El, #state{sid = SID, jid = JID, server = Server, host_type = HostType}) -> BaseParams = #{ location => ?LOCATION, + host_type => HostType, lserver => Server, element => El, from_jid => JID From 480c5ea8ee05416ea5135b3e2d6f8670aeb6d735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 26 Apr 2021 09:21:23 +0200 Subject: [PATCH 09/24] Unify acc construction for brevity and consistency --- src/ejabberd_c2s.erl | 73 +++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 48 deletions(-) diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index 0f553913bfa..9349c1773bd 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -1033,12 +1033,8 @@ code_change(_OldVsn, StateName, StateData, _Extra) -> %%% system events -handle_info({exit, Reason}, _StateName, - StateData = #state{lang = Lang, server = LServer, host_type = HostType}) -> - Acc = mongoose_acc:new(#{ location => ?LOCATION, - host_type => HostType, - lserver => LServer, - element => undefined }), +handle_info({exit, Reason}, _StateName, StateData = #state{lang = Lang}) -> + Acc = new_acc(StateData, #{location => ?LOCATION, element => undefined}), send_element(Acc, mongoose_xmpp_errors:stream_conflict(Lang, Reason), StateData), send_trailer(StateData), {stop, normal, StateData}; @@ -1049,13 +1045,9 @@ handle_info(replaced, _StateName, StateData) -> maybe_send_trailer_safe(StateData), {stop, normal, StateData#state{authenticated = replaced}}; handle_info(new_offline_messages, session_established, - #state{pres_last = Presence, pres_invis = Invisible, jid = JID, - host_type = HostType} = StateData) + #state{pres_last = Presence, pres_invis = Invisible} = StateData) when Presence =/= undefined orelse Invisible -> - Acc = mongoose_acc:new(#{ location => ?LOCATION, - host_type => HostType, - lserver => JID#jid.lserver, - element => undefined }), + Acc = new_acc(StateData, #{location => ?LOCATION, element => undefined}), resend_offline_messages(Acc, StateData), {next_state, session_established, StateData}; handle_info({'DOWN', Monitor, _Type, _Object, _Info}, _StateName, StateData) @@ -1135,10 +1127,7 @@ handle_incoming_message({send_text, Text}, StateName, StateData) -> send_text(StateData, Text), fsm_next_state(StateName, StateData); handle_incoming_message({broadcast, Broadcast}, StateName, StateData) -> - Acc = mongoose_acc:new(#{ location => ?LOCATION, - host_type => StateData#state.host_type, - lserver => StateData#state.server, - element => undefined }), + Acc = new_acc(StateData, #{location => ?LOCATION, element => undefined}), ?LOG_DEBUG(#{what => c2s_broadcast, brodcast_data => Broadcast, state_name => StateName, c2s_state => StateData}), @@ -1149,12 +1138,10 @@ handle_incoming_message({route, From, To, Acc}, StateName, StateData) -> handle_incoming_message({send_filtered, Feature, From, To, Packet}, StateName, StateData) -> % this is used by pubsub and should be rewritten when someone rewrites pubsub module #state{server = Server, host_type = HostType} = StateData, - Acc = mongoose_acc:new(#{ location => ?LOCATION, - from_jid => From, - to_jid => To, - host_type => HostType, - lserver => To#jid.lserver, - element => Packet }), + Acc = new_acc(StateData, #{location => ?LOCATION, + from_jid => From, + to_jid => To, + element => Packet}), Drop = mongoose_hooks:c2s_filter_packet(HostType, Server, StateData, Feature, To, Packet), case {Drop, StateData#state.jid} of @@ -1514,11 +1501,7 @@ terminate({handover_session, From}, StateName, StateData, UnreadMessages) -> % and then run the normal termination terminate(normal, StateName, NewStateData, []); terminate(_Reason, StateName, StateData, UnreadMessages) -> - InitialAcc0 = mongoose_acc:new( - #{location => ?LOCATION, - host_type => StateData#state.host_type, - lserver => StateData#state.server, - element => undefined}), + InitialAcc0 = new_acc(StateData, #{location => ?LOCATION, element => undefined}), Acc = case StateData#state.stream_mgmt of true -> mongoose_acc:set(stream_mgmt, h, StateData#state.stream_mgmt_in, InitialAcc0); _ -> InitialAcc0 @@ -1656,12 +1639,10 @@ send_element_from_server_jid(StateData, #xmlel{} = El) -> -spec send_element_from_server_jid(mongoose_acc:t() | no_acc, state(), exml:element()) -> any(). send_element_from_server_jid(no_acc, #state{server = Server} = StateData, #xmlel{} = El) -> - Acc = mongoose_acc:new(#{location => ?LOCATION, - from_jid => jid:make_noprep(<<>>, Server, <<>>), - to_jid => StateData#state.jid, - host_type => StateData#state.host_type, - lserver => Server, - element => El }), + Acc = new_acc(StateData, #{location => ?LOCATION, + from_jid => jid:make_noprep(<<>>, Server, <<>>), + to_jid => StateData#state.jid, + element => El}), send_element_from_server_jid(Acc, StateData, El); send_element_from_server_jid(Acc, StateData, #xmlel{} = El) -> Acc1 = send_element(Acc, El, StateData), @@ -2096,12 +2077,10 @@ check_privacy_and_route(Acc, FromRoute, StateData) -> StateData :: state()) -> allow|deny|block. privacy_check_packet(#xmlel{} = Packet, From, To, Dir, StateData) -> % in some cases we need an accumulator-less privacy check - Acc = mongoose_acc:new(#{ location => ?LOCATION, - from_jid => From, - to_jid => To, - host_type => StateData#state.host_type, - lserver => StateData#state.server, - element => Packet }), + Acc = new_acc(StateData, #{location => ?LOCATION, + from_jid => From, + to_jid => To, + element => Packet}), {_, Res} = privacy_check_packet(Acc, To, Dir, StateData), Res. @@ -3185,11 +3164,7 @@ stream_mgmt_resumed(SMID, Handled) -> handover_session(SD, From)-> true = SD#state.stream_mgmt, - Acc = mongoose_acc:new( - #{location => ?LOCATION, - host_type => SD#state.host_type, - lserver => SD#state.server, - element => undefined}), + Acc = new_acc(SD, #{location => ?LOCATION, element => undefined}), ejabberd_sm:close_session(Acc, SD#state.sid, SD#state.jid, @@ -3336,11 +3311,9 @@ terminate_when_tls_required_but_not_enabled(_, _, StateData, El) -> %% @doc This function is executed when c2s receives a stanza from TCP connection. -spec element_to_origin_accum(jlib:xmlel(), StateData :: state()) -> mongoose_acc:t(). -element_to_origin_accum(El, #state{sid = SID, jid = JID, server = Server, host_type = HostType}) -> +element_to_origin_accum(El, StateData = #state{sid = SID, jid = JID}) -> BaseParams = #{ location => ?LOCATION, - host_type => HostType, - lserver => Server, element => El, from_jid => JID }, @@ -3349,7 +3322,7 @@ element_to_origin_accum(El, #state{sid = SID, jid = JID, server = Server, host_t undefined -> BaseParams#{ to_jid => jid:to_bare(JID) }; _ToBin -> BaseParams end, - Acc = mongoose_acc:new(Params), + Acc = new_acc(StateData, Params), Acc1 = mongoose_acc:set_permanent(c2s, origin_sid, SID, Acc), mongoose_acc:set_permanent(c2s, origin_jid, JID, Acc1). @@ -3382,3 +3355,7 @@ update_stanza(From, To, #xmlel{} = Element, Acc) -> strip_c2s_fields(Acc) -> %% TODO: verify if we really need to strip down these 2 fields mongoose_acc:delete_many(c2s, [origin_jid, origin_sid], Acc). + +-spec new_acc(state(), mongoose_acc:new_acc_params()) -> mongoose_acc:t(). +new_acc(#state{host_type = HostType, server = LServer}, Params) -> + mongoose_acc:new(Params#{host_type => HostType, lserver => LServer}). From abe1930ef0a983a801542f47549269e931be831d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 26 Apr 2021 10:12:22 +0200 Subject: [PATCH 10/24] Use host_type in sm_broadcast --- src/ejabberd_sm.erl | 3 +-- src/mongoose_hooks.erl | 10 +++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index 1c650b81448..edf70b6363c 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -622,8 +622,7 @@ do_route(Acc, From, To, {broadcast, Payload} = Broadcast) -> case LResource of <<>> -> CurrentPids = get_user_present_pids(LUser, LServer), - Acc1 = mongoose_hooks:sm_broadcast(To#jid.lserver, Acc, From, To, - Broadcast, length(CurrentPids)), + Acc1 = mongoose_hooks:sm_broadcast(Acc, From, To, Broadcast, length(CurrentPids)), ?LOG_DEBUG(#{what => sm_broadcast, session_pids => CurrentPids}), BCast = {broadcast, Payload}, lists:foreach(fun({_, Pid}) -> Pid ! BCast end, CurrentPids), diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 9140206ed7f..4b53b6db627 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -65,7 +65,7 @@ -export([offline_groupchat_message_hook/5, offline_message_hook/5, set_presence_hook/3, - sm_broadcast/6, + sm_broadcast/5, sm_filter_offline_message/4, sm_register_connection_hook/4, sm_remove_connection_hook/5, @@ -715,16 +715,16 @@ set_presence_hook(Acc, #jid{luser = LUser, lserver = LServer, lresource = LResou ejabberd_hooks:run_for_host_type(set_presence_hook, HostType, Acc, [LUser, LServer, LResource, Presence]). --spec sm_broadcast(LServer, Acc, From, To, Broadcast, SessionCount) -> Result when - LServer :: jid:lserver(), +-spec sm_broadcast(Acc, From, To, Broadcast, SessionCount) -> Result when Acc :: mongoose_acc:t(), From :: jid:jid(), To :: jid:jid(), Broadcast :: ejabberd_c2s:broadcast(), SessionCount :: non_neg_integer(), Result :: mongoose_acc:t(). -sm_broadcast(LServer, Acc, From, To, Broadcast, SessionCount) -> - ejabberd_hooks:run_for_host_type(sm_broadcast, LServer, Acc, +sm_broadcast(Acc, From, To, Broadcast, SessionCount) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(sm_broadcast, HostType, Acc, [From, To, Broadcast, SessionCount]). -spec sm_filter_offline_message(LServer, From, To, Packet) -> Result when From a2cf4e719bb85ed698c7965a8e1d65a2a822fec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 26 Apr 2021 11:11:21 +0200 Subject: [PATCH 11/24] Use host_type in other hooks called from ejabberd_sm Hooks changed: - roster_in_subscription - sm_filter_offline_message - privacy_get_user_list - offline_groupchat_message_hook --- src/ejabberd_sm.erl | 13 ++++++------- src/mongoose_hooks.erl | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index edf70b6363c..b6d31844042 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -668,8 +668,7 @@ do_route(Acc, From, To, El) -> do_route_no_resource_presence_prv(From, To, Acc, Packet, Type, Reason) -> case is_privacy_allow(From, To, Acc, Packet) of true -> - Res = mongoose_hooks:roster_in_subscription(To#jid.lserver, - Acc, To, From, Type, Reason), + Res = mongoose_hooks:roster_in_subscription(Acc, To, From, Type, Reason), mongoose_acc:get(hook, result, false, Res); false -> false @@ -728,7 +727,8 @@ do_route_no_resource(_, _, _, _, Acc, _) -> Acc :: mongoose_acc:t(), Packet :: exml:element(). do_route_offline(<<"message">>, _, From, To, Acc, Packet) -> - Drop = mongoose_hooks:sm_filter_offline_message(To#jid.lserver, From, To, Packet), + HostType = mongoose_acc:host_type(Acc), + Drop = mongoose_hooks:sm_filter_offline_message(HostType, From, To, Packet), case Drop of false -> route_message(From, To, Acc, Packet); @@ -758,8 +758,8 @@ do_route_offline(_, _, _, _, Acc, _) -> Acc :: mongoose_acc:t(), Packet :: exml:element() | mongoose_acc:t(). is_privacy_allow(From, To, Acc, Packet) -> - Server = To#jid.server, - PrivacyList = mongoose_hooks:privacy_get_user_list(Server, To), + HostType = mongoose_acc:host_type(Acc), + PrivacyList = mongoose_hooks:privacy_get_user_list(HostType, To), is_privacy_allow(From, To, Acc, Packet, PrivacyList). @@ -808,8 +808,7 @@ route_message(From, To, Acc, Packet) -> route_message_by_type(<<"error">>, _From, _To, Acc, _Packet) -> Acc; route_message_by_type(<<"groupchat">>, From, To, Acc, Packet) -> - LServer = To#jid.lserver, - mongoose_hooks:offline_groupchat_message_hook(LServer, Acc, From, To, Packet); + mongoose_hooks:offline_groupchat_message_hook(Acc, From, To, Packet); route_message_by_type(<<"headline">>, From, To, Acc, Packet) -> {stop, Acc1} = bounce_offline_message(Acc, From, To, Packet), Acc1; diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 4b53b6db627..220cec09b88 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -62,7 +62,7 @@ privacy_iq_set/5, privacy_updated_list/3]). --export([offline_groupchat_message_hook/5, +-export([offline_groupchat_message_hook/4, offline_message_hook/5, set_presence_hook/3, sm_broadcast/5, @@ -77,7 +77,7 @@ roster_get_subscription_lists/3, roster_get_versioning_feature/2, roster_groups/1, - roster_in_subscription/6, + roster_in_subscription/5, roster_out_subscription/5, roster_process_item/2, roster_push/3, @@ -683,15 +683,15 @@ privacy_updated_list(HostType, OldList, NewList) -> %% Session management related hooks --spec offline_groupchat_message_hook(LServer, Acc, From, To, Packet) -> Result when - LServer :: jid:lserver(), +-spec offline_groupchat_message_hook(Acc, From, To, Packet) -> Result when Acc :: mongoose_acc:t(), From :: jid:jid(), To :: jid:jid(), Packet :: exml:element(), Result :: mongoose_acc:t(). -offline_groupchat_message_hook(LServer, Acc, From, To, Packet) -> - ejabberd_hooks:run_for_host_type(offline_groupchat_message_hook, LServer, Acc, +offline_groupchat_message_hook(Acc, From, To, Packet) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(offline_groupchat_message_hook, HostType, Acc, [From, To, Packet]). -spec offline_message_hook(LServer, Acc, From, To, Packet) -> Result when @@ -727,14 +727,14 @@ sm_broadcast(Acc, From, To, Broadcast, SessionCount) -> ejabberd_hooks:run_for_host_type(sm_broadcast, HostType, Acc, [From, To, Broadcast, SessionCount]). --spec sm_filter_offline_message(LServer, From, To, Packet) -> Result when - LServer :: jid:lserver(), +-spec sm_filter_offline_message(HostType, From, To, Packet) -> Result when + HostType :: binary(), From :: jid:jid(), To :: jid:jid(), Packet :: exml:element(), Result :: boolean(). -sm_filter_offline_message(LServer, From, To, Packet) -> - ejabberd_hooks:run_for_host_type(sm_filter_offline_message, LServer, false, +sm_filter_offline_message(HostType, From, To, Packet) -> + ejabberd_hooks:run_for_host_type(sm_filter_offline_message, HostType, false, [From, To, Packet]). -spec sm_register_connection_hook(HostType, SID, JID, Info) -> Result when @@ -834,16 +834,16 @@ roster_get_versioning_feature(HostType, Server) -> %%% @doc The `roster_in_subscription' hook is called to determine %%% if a subscription presence is routed to a user. --spec roster_in_subscription(LServer, Acc, To, From, Type, Reason) -> Result when - LServer :: jid:lserver(), +-spec roster_in_subscription(Acc, To, From, Type, Reason) -> Result when Acc :: mongoose_acc:t(), To :: jid:jid(), From :: jid:jid(), Type :: mod_roster:sub_presence(), Reason :: any(), Result :: mongoose_acc:t(). -roster_in_subscription(LServer, Acc, To, From, Type, Reason) -> - ejabberd_hooks:run_for_host_type(roster_in_subscription, LServer, Acc, +roster_in_subscription(Acc, To, From, Type, Reason) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(roster_in_subscription, HostType, Acc, [jid:to_bare(To), From, Type, Reason]). %%% @doc The `roster_out_subscription' hook is called From 7d31c0877e31de10b5662194dcf4d8538bc981ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 26 Apr 2021 12:34:32 +0200 Subject: [PATCH 12/24] Use host_type in remaining hooks called from ejabberd_sm Hooks changed: - offline_message_hook - failed_to_store_message --- src/ejabberd_sm.erl | 5 ++--- src/mongoose_hooks.erl | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index b6d31844042..b13868bf92a 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -813,14 +813,13 @@ route_message_by_type(<<"headline">>, From, To, Acc, Packet) -> {stop, Acc1} = bounce_offline_message(Acc, From, To, Packet), Acc1; route_message_by_type(_, From, To, Acc, Packet) -> - LServer = To#jid.lserver, case ejabberd_auth:does_user_exist(To) of true -> case is_privacy_allow(From, To, Acc, Packet) of true -> - mongoose_hooks:offline_message_hook(LServer, Acc, From, To, Packet); + mongoose_hooks:offline_message_hook(Acc, From, To, Packet); false -> - mongoose_hooks:failed_to_store_message(LServer, Acc) + mongoose_hooks:failed_to_store_message(Acc) end; _ -> {Acc1, Err} = jlib:make_error_reply( diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 220cec09b88..6f9a5f21765 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -15,7 +15,7 @@ anonymous_purge_hook/3, auth_failed/3, ejabberd_ctl_process/2, - failed_to_store_message/2, + failed_to_store_message/1, filter_local_packet/2, filter_packet/1, inbox_unread_count/3, @@ -63,7 +63,7 @@ privacy_updated_list/3]). -export([offline_groupchat_message_hook/4, - offline_message_hook/5, + offline_message_hook/4, set_presence_hook/3, sm_broadcast/5, sm_filter_offline_message/4, @@ -252,12 +252,12 @@ node_cleanup(Node) -> ejabberd_ctl_process(Acc, Args) -> ejabberd_hooks:run_global(ejabberd_ctl_process, Acc, [Args]). --spec failed_to_store_message(LServer, Acc) -> Result when - LServer :: jid:lserver(), +-spec failed_to_store_message(Acc) -> Result when Acc :: mongoose_acc:t(), Result :: mongoose_acc:t(). -failed_to_store_message(LServer, Acc) -> - ejabberd_hooks:run_for_host_type(failed_to_store_message, LServer, Acc, []). +failed_to_store_message(Acc) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(failed_to_store_message, HostType, Acc, []). %%% @doc The `filter_local_packet' hook is called to filter out %%% stanzas routed with `mongoose_local_delivery'. @@ -694,15 +694,15 @@ offline_groupchat_message_hook(Acc, From, To, Packet) -> ejabberd_hooks:run_for_host_type(offline_groupchat_message_hook, HostType, Acc, [From, To, Packet]). --spec offline_message_hook(LServer, Acc, From, To, Packet) -> Result when - LServer :: jid:lserver(), +-spec offline_message_hook(Acc, From, To, Packet) -> Result when Acc :: mongoose_acc:t(), From :: jid:jid(), To :: jid:jid(), Packet :: exml:element(), Result :: mongoose_acc:t(). -offline_message_hook(LServer, Acc, From, To, Packet) -> - ejabberd_hooks:run_for_host_type(offline_message_hook, LServer, Acc, +offline_message_hook(Acc, From, To, Packet) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(offline_message_hook, HostType, Acc, [From, To, Packet]). -spec set_presence_hook(Acc, JID, Presence) -> Result when From ae743ea6e5536078cd8d9fe44f38a833d8dd9fbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 26 Apr 2021 15:02:47 +0200 Subject: [PATCH 13/24] Use host_type in mongoose_privacy --- src/ejabberd_c2s.erl | 4 ++-- src/ejabberd_sm.erl | 4 ++-- src/mod_last.erl | 7 ++++--- src/mongoose_hooks.erl | 8 ++++---- src/mongoose_privacy.erl | 13 +++++++------ 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index 9349c1773bd..98cee7c058b 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -2090,7 +2090,7 @@ privacy_check_packet(#xmlel{} = Packet, From, To, Dir, StateData) -> StateData :: state()) -> {mongoose_acc:t(), allow|deny|block}. privacy_check_packet(Acc, To, Dir, StateData) -> mongoose_privacy:privacy_check_packet(Acc, - StateData#state.server, + StateData#state.host_type, StateData#state.jid, StateData#state.privacy_list, To, @@ -2104,7 +2104,7 @@ privacy_check_packet(Acc, To, Dir, StateData) -> StateData :: state()) -> {mongoose_acc:t(), allow|deny|block}. privacy_check_packet(Acc, Packet, From, To, Dir, StateData) -> mongoose_privacy:privacy_check_packet({Acc, Packet}, - StateData#state.server, + StateData#state.host_type, StateData#state.jid, StateData#state.privacy_list, From, diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index b13868bf92a..680cabda89b 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -772,8 +772,8 @@ is_privacy_allow(From, To, Acc, Packet) -> Packet :: exml:element(), PrivacyList :: mongoose_privacy:userlist(). is_privacy_allow(_From, To, Acc, _Packet, PrivacyList) -> - Server = To#jid.lserver, - {_, Res} = mongoose_privacy:privacy_check_packet(Acc, Server, To, PrivacyList, To, in), + HostType = mongoose_acc:host_type(Acc), + {_, Res} = mongoose_privacy:privacy_check_packet(Acc, HostType, To, PrivacyList, To, in), allow == Res. diff --git a/src/mod_last.erl b/src/mod_last.erl index f80c3695fc3..a428af1178d 100644 --- a/src/mod_last.erl +++ b/src/mod_last.erl @@ -181,9 +181,10 @@ process_sm_iq(From, To, Acc, #iq{type = get, sub_el = SubEl} = IQ) -> true -> UserListRecord = mongoose_hooks:privacy_get_user_list(Server, To), ok, - {Acc1, Res} = mongoose_privacy:privacy_check_packet(Acc, Server, To, - UserListRecord, To, From, - out), + HostType = mongoose_acc:host_type(Acc), + {Acc1, Res} = mongoose_privacy:privacy_check_packet(Acc, HostType, To, + UserListRecord, To, From, + out), {Acc1, make_response(IQ, SubEl, To, Res)}; false -> {Acc, IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:forbidden()]}} diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 6f9a5f21765..c1b1d82cabc 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -629,16 +629,16 @@ session_opening_allowed_for_user(HostType, JID) -> %% Privacy related hooks --spec privacy_check_packet(LServer, Acc, JID, PrivacyList, +-spec privacy_check_packet(HostType, Acc, JID, PrivacyList, FromToNameType, Dir) -> Result when - LServer :: jid:lserver(), Acc :: mongoose_acc:t(), JID :: jid:jid(), + HostType :: binary(), Acc :: mongoose_acc:t(), JID :: jid:jid(), PrivacyList :: mongoose_privacy:userlist(), FromToNameType :: {jid:jid(), jid:jid(), binary(), binary()}, Dir :: in | out, Result :: mongoose_acc:t(). -privacy_check_packet(LServer, Acc, JID, PrivacyList, FromToNameType, Dir) -> +privacy_check_packet(HostType, Acc, JID, PrivacyList, FromToNameType, Dir) -> AccWithRes = mongoose_acc:set(hook, result, allow, Acc), - ejabberd_hooks:run_for_host_type(privacy_check_packet, LServer, AccWithRes, + ejabberd_hooks:run_for_host_type(privacy_check_packet, HostType, AccWithRes, [JID, PrivacyList, FromToNameType, Dir]). -spec privacy_get_user_list(HostType, JID) -> Result when diff --git a/src/mongoose_privacy.erl b/src/mongoose_privacy.erl index 2587f3643e0..52a1c50dcc7 100644 --- a/src/mongoose_privacy.erl +++ b/src/mongoose_privacy.erl @@ -29,30 +29,31 @@ %% the first arg can be accumulator, if we are checking its element, or a tuple %% {Acc, El} for cases where one acc triggers sending many messages which have to be checked -spec privacy_check_packet(Acc :: mongoose_acc:t() | {mongoose_acc:t(), exml:element()}, - Server :: binary(), + HostType :: binary(), JID :: jid:jid(), PrivacyList :: userlist(), To :: jid:jid(), Dir :: 'in' | 'out') -> {mongoose_acc:t(), decision()}. -privacy_check_packet(Acc0, Server, JID, PrivacyList, To, Dir) -> +privacy_check_packet(Acc0, HostType, JID, PrivacyList, To, Dir) -> Acc1 = case Acc0 of {Acc, #xmlel{}} -> Acc; _ -> Acc0 end, From = mongoose_acc:from_jid(Acc1), - privacy_check_packet(Acc0, Server, JID, PrivacyList, From, To, Dir). + privacy_check_packet(Acc0, HostType, JID, PrivacyList, From, To, Dir). %% @doc check packet, store result in accumulator, return acc and result for quick check %% Acc can be either a single argument (an Accumulator) or a tuple of {Acc, Stanza} %% in the latter case name and type to check against privacy lists are taken from the Stanza -spec privacy_check_packet(Acc :: mongoose_acc:t() | {mongoose_acc:t(), exml:element()}, - Server :: binary(), + HostType :: binary(), JID :: jid:jid(), PrivacyList :: userlist(), From :: jid:jid(), To :: jid:jid(), Dir :: 'in' | 'out') -> {mongoose_acc:t(), decision()}. -privacy_check_packet(Acc0, Server, #jid{luser = User} = JID, PrivacyList, From, To, Dir) -> +privacy_check_packet(Acc0, HostType, #jid{luser = User, server = Server} = JID, + PrivacyList, From, To, Dir) -> % see if we have just Acc or also stanza to check - may have different name/type {Acc, Name, Type} = case Acc0 of {A, #xmlel{name = SName} = Stanza} -> @@ -65,7 +66,7 @@ privacy_check_packet(Acc0, Server, #jid{luser = User} = JID, PrivacyList, From, Key = {cached_check, Server, User, From, To, Name, Type, Dir}, case mongoose_acc:get(privacy, Key, undefined, Acc) of undefined -> - Acc1 = mongoose_hooks:privacy_check_packet(Server, Acc, JID, PrivacyList, + Acc1 = mongoose_hooks:privacy_check_packet(HostType, Acc, JID, PrivacyList, {From, To, Name, Type}, Dir), Res = mongoose_acc:get(hook, result, Acc1), {mongoose_acc:set(privacy, Key, Res, Acc1), Res}; From 38a5fb79f5aac9b793b6aa123db44a0064078150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 26 Apr 2021 15:18:04 +0200 Subject: [PATCH 14/24] Use Acc for host_type in privacy checks This change is in a separate commit for clarity. The host_type argument is removed as it is always calculated from the server part of the JID. --- src/ejabberd_c2s.erl | 2 -- src/ejabberd_sm.erl | 3 +-- src/mod_last.erl | 4 +--- src/mongoose_hooks.erl | 9 +++++---- src/mongoose_privacy.erl | 12 +++++------- test/privacy_SUITE.erl | 4 ++-- 6 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index 98cee7c058b..1711ec8ef67 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -2090,7 +2090,6 @@ privacy_check_packet(#xmlel{} = Packet, From, To, Dir, StateData) -> StateData :: state()) -> {mongoose_acc:t(), allow|deny|block}. privacy_check_packet(Acc, To, Dir, StateData) -> mongoose_privacy:privacy_check_packet(Acc, - StateData#state.host_type, StateData#state.jid, StateData#state.privacy_list, To, @@ -2104,7 +2103,6 @@ privacy_check_packet(Acc, To, Dir, StateData) -> StateData :: state()) -> {mongoose_acc:t(), allow|deny|block}. privacy_check_packet(Acc, Packet, From, To, Dir, StateData) -> mongoose_privacy:privacy_check_packet({Acc, Packet}, - StateData#state.host_type, StateData#state.jid, StateData#state.privacy_list, From, diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index 680cabda89b..4a682ba0cdf 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -772,8 +772,7 @@ is_privacy_allow(From, To, Acc, Packet) -> Packet :: exml:element(), PrivacyList :: mongoose_privacy:userlist(). is_privacy_allow(_From, To, Acc, _Packet, PrivacyList) -> - HostType = mongoose_acc:host_type(Acc), - {_, Res} = mongoose_privacy:privacy_check_packet(Acc, HostType, To, PrivacyList, To, in), + {_, Res} = mongoose_privacy:privacy_check_packet(Acc, To, PrivacyList, To, in), allow == Res. diff --git a/src/mod_last.erl b/src/mod_last.erl index a428af1178d..a17b34d8ece 100644 --- a/src/mod_last.erl +++ b/src/mod_last.erl @@ -180,9 +180,7 @@ process_sm_iq(From, To, Acc, #iq{type = get, sub_el = SubEl} = IQ) -> case MutualSubscription or RequesterSubscribedToTarget or QueryingSameUsersLast of true -> UserListRecord = mongoose_hooks:privacy_get_user_list(Server, To), - ok, - HostType = mongoose_acc:host_type(Acc), - {Acc1, Res} = mongoose_privacy:privacy_check_packet(Acc, HostType, To, + {Acc1, Res} = mongoose_privacy:privacy_check_packet(Acc, To, UserListRecord, To, From, out), {Acc1, make_response(IQ, SubEl, To, Res)}; diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index c1b1d82cabc..e7fa020aa08 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -56,7 +56,7 @@ forbidden_session_hook/3, session_opening_allowed_for_user/2]). --export([privacy_check_packet/6, +-export([privacy_check_packet/5, privacy_get_user_list/2, privacy_iq_get/6, privacy_iq_set/5, @@ -629,14 +629,15 @@ session_opening_allowed_for_user(HostType, JID) -> %% Privacy related hooks --spec privacy_check_packet(HostType, Acc, JID, PrivacyList, +-spec privacy_check_packet(Acc, JID, PrivacyList, FromToNameType, Dir) -> Result when - HostType :: binary(), Acc :: mongoose_acc:t(), JID :: jid:jid(), + Acc :: mongoose_acc:t(), JID :: jid:jid(), PrivacyList :: mongoose_privacy:userlist(), FromToNameType :: {jid:jid(), jid:jid(), binary(), binary()}, Dir :: in | out, Result :: mongoose_acc:t(). -privacy_check_packet(HostType, Acc, JID, PrivacyList, FromToNameType, Dir) -> +privacy_check_packet(Acc, JID, PrivacyList, FromToNameType, Dir) -> + HostType = mongoose_acc:host_type(Acc), AccWithRes = mongoose_acc:set(hook, result, allow, Acc), ejabberd_hooks:run_for_host_type(privacy_check_packet, HostType, AccWithRes, [JID, PrivacyList, FromToNameType, Dir]). diff --git a/src/mongoose_privacy.erl b/src/mongoose_privacy.erl index 52a1c50dcc7..849edc321ef 100644 --- a/src/mongoose_privacy.erl +++ b/src/mongoose_privacy.erl @@ -15,7 +15,7 @@ -include("mod_privacy.hrl"). %% API --export([privacy_check_packet/6, privacy_check_packet/7]). +-export([privacy_check_packet/5, privacy_check_packet/6]). -type userlist() :: #userlist{}. -type decision() :: allow | deny | block. @@ -29,30 +29,28 @@ %% the first arg can be accumulator, if we are checking its element, or a tuple %% {Acc, El} for cases where one acc triggers sending many messages which have to be checked -spec privacy_check_packet(Acc :: mongoose_acc:t() | {mongoose_acc:t(), exml:element()}, - HostType :: binary(), JID :: jid:jid(), PrivacyList :: userlist(), To :: jid:jid(), Dir :: 'in' | 'out') -> {mongoose_acc:t(), decision()}. -privacy_check_packet(Acc0, HostType, JID, PrivacyList, To, Dir) -> +privacy_check_packet(Acc0, JID, PrivacyList, To, Dir) -> Acc1 = case Acc0 of {Acc, #xmlel{}} -> Acc; _ -> Acc0 end, From = mongoose_acc:from_jid(Acc1), - privacy_check_packet(Acc0, HostType, JID, PrivacyList, From, To, Dir). + privacy_check_packet(Acc0, JID, PrivacyList, From, To, Dir). %% @doc check packet, store result in accumulator, return acc and result for quick check %% Acc can be either a single argument (an Accumulator) or a tuple of {Acc, Stanza} %% in the latter case name and type to check against privacy lists are taken from the Stanza -spec privacy_check_packet(Acc :: mongoose_acc:t() | {mongoose_acc:t(), exml:element()}, - HostType :: binary(), JID :: jid:jid(), PrivacyList :: userlist(), From :: jid:jid(), To :: jid:jid(), Dir :: 'in' | 'out') -> {mongoose_acc:t(), decision()}. -privacy_check_packet(Acc0, HostType, #jid{luser = User, server = Server} = JID, +privacy_check_packet(Acc0, #jid{luser = User, server = Server} = JID, PrivacyList, From, To, Dir) -> % see if we have just Acc or also stanza to check - may have different name/type {Acc, Name, Type} = case Acc0 of @@ -66,7 +64,7 @@ privacy_check_packet(Acc0, HostType, #jid{luser = User, server = Server} = JID, Key = {cached_check, Server, User, From, To, Name, Type, Dir}, case mongoose_acc:get(privacy, Key, undefined, Acc) of undefined -> - Acc1 = mongoose_hooks:privacy_check_packet(HostType, Acc, JID, PrivacyList, + Acc1 = mongoose_hooks:privacy_check_packet(Acc, JID, PrivacyList, {From, To, Name, Type}, Dir), Res = mongoose_acc:get(hook, result, Acc1), {mongoose_acc:set(privacy, Key, Res, Acc1), Res}; diff --git a/test/privacy_SUITE.erl b/test/privacy_SUITE.erl index c3a473bd5ae..6873eb66dda 100644 --- a/test/privacy_SUITE.erl +++ b/test/privacy_SUITE.erl @@ -120,8 +120,8 @@ check_with_changing_stanza(_C) -> make_check(Acc, PList, To, Dir, Exp) -> Server = <<"localhost">>, User = <<"alice">>, - {Acc1, Res} = mongoose_privacy:privacy_check_packet(Acc, Server, jid:make(User, Server, <<>>), - PList, To, Dir), + {Acc1, Res} = mongoose_privacy:privacy_check_packet(Acc, jid:make(User, Server, <<>>), + PList, To, Dir), ?assertEqual(Exp, Res), Acc1. From 9afd2d0fb97c0c2e6e4b0aed763ae8d906b7d467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 26 Apr 2021 15:29:09 +0200 Subject: [PATCH 15/24] Fix type spec --- src/ejabberd_local.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ejabberd_local.erl b/src/ejabberd_local.erl index 0a00daf8669..789ecfc21e0 100644 --- a/src/ejabberd_local.erl +++ b/src/ejabberd_local.erl @@ -397,7 +397,7 @@ code_change(_OldVsn, State, _Extra) -> -spec do_route(Acc :: mongoose_acc:t(), From :: jid:jid(), To :: jid:jid(), - El :: mongoose_acc:t()) -> mongoose_acc:t(). + El :: exml:element()) -> mongoose_acc:t(). do_route(Acc, From, To, El) -> ?LOG_DEBUG(#{what => local_routing, acc => Acc}), case directed_to(To) of From 530c21c6cf9f26f4f4f0d47afb52f588cf92e240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Tue, 27 Apr 2021 08:52:54 +0200 Subject: [PATCH 16/24] Make s2s hooks global Motivation: - Simplicity. - They were unused - no handlers in the code base. - One was already global, now they are consistent. - Host type can be introduced anytime when a need arises. --- src/ejabberd_s2s.erl | 5 +---- src/mongoose_hooks.erl | 15 ++++++--------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/ejabberd_s2s.erl b/src/ejabberd_s2s.erl index 789b4764219..8b4ea4e33e0 100644 --- a/src/ejabberd_s2s.erl +++ b/src/ejabberd_s2s.erl @@ -272,10 +272,7 @@ do_route(From, To, Acc, Packet) -> jid:to_binary(To), Attrs), NewPacket = Packet#xmlel{attrs = NewAttrs}, - #jid{lserver = MyServer} = From, - Acc1 = mongoose_hooks:s2s_send_packet(MyServer, - Acc, - From, To, Packet), + Acc1 = mongoose_hooks:s2s_send_packet(Acc, From, To, Packet), send_element(Pid, Acc1, NewPacket), done; {aborted, _Reason} -> diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index e7fa020aa08..c12a49ca221 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -116,7 +116,7 @@ s2s_connect_hook/2, s2s_receive_packet/2, s2s_stream_features/1, - s2s_send_packet/5]). + s2s_send_packet/4]). -export([disco_info/4, disco_local_features/5, @@ -1212,8 +1212,7 @@ find_s2s_bridge(Name, Server) -> S2SHost :: jid:server(), Result :: allow | deny. s2s_allow_host(MyHost, S2SHost) -> - ejabberd_hooks:run_for_host_type(s2s_allow_host, MyHost, allow, - [MyHost, S2SHost]). + ejabberd_hooks:run_global(s2s_allow_host, allow, [MyHost, S2SHost]). %%% @doc `s2s_connect_hook' hook is called when a s2s connection is established. -spec s2s_connect_hook(Name, Server) -> Result when @@ -1221,19 +1220,17 @@ s2s_allow_host(MyHost, S2SHost) -> Server :: jid:server(), Result :: any(). s2s_connect_hook(Name, Server) -> - ejabberd_hooks:run_for_host_type(s2s_connect_hook, Name, ok, [Server]). + ejabberd_hooks:run_global(s2s_connect_hook, ok, [Name, Server]). %%% @doc `s2s_send_packet' hook is called when a message is routed. --spec s2s_send_packet(Server, Acc, From, To, Packet) -> Result when - Server :: jid:server(), +-spec s2s_send_packet(Acc, From, To, Packet) -> Result when Acc :: mongoose_acc:t(), From :: jid:jid(), To :: jid:jid(), Packet :: exml:element(), Result :: mongoose_acc:t(). -s2s_send_packet(Server, Acc, From, To, Packet) -> - ejabberd_hooks:run_for_host_type(s2s_send_packet, Server, Acc, - [From, To, Packet]). +s2s_send_packet(Acc, From, To, Packet) -> + ejabberd_hooks:run_global(s2s_send_packet, Acc, [From, To, Packet]). %%% @doc `s2s_stream_features' hook is used to extract %%% the stream management features supported by the server. From c6ba7653efe9b61275a89295084deac15c2d32d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Tue, 27 Apr 2021 09:51:36 +0200 Subject: [PATCH 17/24] Use host_type in rest_user_send_packet --- .../mongoose_client_api_messages.erl | 6 +++--- src/mongoose_hooks.erl | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/mongoose_client_api/mongoose_client_api_messages.erl b/src/mongoose_client_api/mongoose_client_api_messages.erl index a159c1cc333..714d41df328 100644 --- a/src/mongoose_client_api/mongoose_client_api_messages.erl +++ b/src/mongoose_client_api/mongoose_client_api_messages.erl @@ -78,7 +78,7 @@ maybe_to_json_with_jid(WithJID, #jid{lserver = Server} = JID, Req, State) -> Resp = [make_json_msg(Msg, MAMId) || {MAMId, _, Msg} <- Msgs], {jiffy:encode(Resp), Req, State}. -send_message(Req, #{user := RawUser, jid := FromJID} = State) -> +send_message(Req, #{user := RawUser, jid := FromJID, creds := Creds} = State) -> {ok, Body, Req2} = cowboy_req:read_body(Req), case mongoose_client_api:json_to_map(Body) of {ok, #{<<"to">> := To, <<"body">> := MsgBody}} when is_binary(To), is_binary(MsgBody) -> @@ -86,12 +86,12 @@ send_message(Req, #{user := RawUser, jid := FromJID} = State) -> UUID = uuid:uuid_to_string(uuid:get_v4(), binary_standard), XMLMsg0 = build_message(RawUser, To, UUID, MsgBody), Acc0 = mongoose_acc:new(#{ location => ?LOCATION, + host_type => mongoose_credentials:host_type(Creds), lserver => FromJID#jid.lserver, from_jid => FromJID, to_jid => ToJID, element => XMLMsg0 }), - Acc1 = mongoose_hooks:rest_user_send_packet(FromJID#jid.lserver, Acc0, - FromJID, ToJID, XMLMsg0), + Acc1 = mongoose_hooks:rest_user_send_packet(Acc0, FromJID, ToJID, XMLMsg0), XMLMsg1 = mongoose_acc:element(Acc1), ejabberd_router:route(FromJID, ToJID, Acc1, XMLMsg1), Resp = #{<<"id">> => UUID}, diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index c12a49ca221..672d8db416e 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -29,7 +29,7 @@ register_user/2, remove_user/3, resend_offline_messages_hook/3, - rest_user_send_packet/5, + rest_user_send_packet/4, session_cleanup/5, set_vcard/3, unacknowledged_message/3, @@ -385,15 +385,15 @@ resend_offline_messages_hook(HostType, Acc, JID) -> %%% @doc The `rest_user_send_packet' hook is called when a user sends %%% a message using the REST API. --spec rest_user_send_packet(LServer, Acc, From, To, Packet) -> Result when - LServer :: jid:lserver(), +-spec rest_user_send_packet(Acc, From, To, Packet) -> Result when Acc :: mongoose_acc:t(), From :: jid:jid(), To :: jid:jid(), Packet :: exml:element(), Result :: mongoose_acc:t(). -rest_user_send_packet(LServer, Acc, From, To, Packet) -> - ejabberd_hooks:run_for_host_type(rest_user_send_packet, LServer, Acc, +rest_user_send_packet(Acc, From, To, Packet) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(rest_user_send_packet, HostType, Acc, [From, To, Packet]). %%% @doc The `session_cleanup' hook is called when sm backend cleans up a user's session. From 0925dddca0d28d99d27579bb747dac1b812528bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Tue, 27 Apr 2021 15:42:14 +0200 Subject: [PATCH 18/24] Add host_type to register_user and remove_user Also: - Avoid duplicate calculation of host_type in ejabberd_auth:remove_user/1 - Provide host_type in sm_register_connection_hook to pass it in a call to register_user from ejabberd_auth_anonymous --- src/auth/ejabberd_auth.erl | 20 ++++++++++++++----- src/auth/ejabberd_auth_anonymous.erl | 7 ++++--- .../mod_global_distrib_mapping.erl | 6 +++--- src/metrics/mongoose_metrics_hooks.erl | 6 +++--- src/mod_ping.erl | 4 ++-- src/mongoose_hooks.erl | 14 +++++++------ test/mongoose_cleanup_SUITE.erl | 2 +- 7 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/auth/ejabberd_auth.erl b/src/auth/ejabberd_auth.erl index 6374b678177..214c9bb3fcd 100644 --- a/src/auth/ejabberd_auth.erl +++ b/src/auth/ejabberd_auth.erl @@ -275,7 +275,7 @@ do_try_register_in_backend([], _, _, _, _) -> do_try_register_in_backend([M | Backends], HostType, LUser, LServer, Password) -> case M:try_register(HostType, LUser, LServer, Password) of ok -> - mongoose_hooks:register_user(LServer, LUser); + mongoose_hooks:register_user(HostType, LServer, LUser); _ -> do_try_register_in_backend(Backends, HostType, LUser, LServer, Password) end. @@ -433,11 +433,22 @@ does_user_exist_in_given_modules([Mod | Modules], LUser, LServer, Default) -> -spec remove_user(JID :: jid:jid()) -> ok | {error, not_allowed}; (error) -> error. remove_user(#jid{luser = LUser, lserver = LServer}) -> - AuthModules = auth_modules(LServer), - RemoveResult = [M:remove_user(LUser, LServer) || M <- AuthModules ], + case mongoose_domain_api:get_host_type(LServer) of + {ok, HostType} -> + remove_user_for_host_type(HostType, LUser, LServer); + {error, not_found} -> + {error, not_allowed} + end; +remove_user(error) -> error. + +-spec remove_user_for_host_type(binary(), jid:luser(), jid:lserver()) -> ok | {error, not_allowed}. +remove_user_for_host_type(HostType, LUser, LServer) -> + AuthModules = auth_modules_for_host_type(HostType), + RemoveResult = [M:remove_user(LUser, LServer) || M <- AuthModules], case lists:any(fun(El) -> El == ok end, RemoveResult) of true -> Acc = mongoose_acc:new(#{ location => ?LOCATION, + host_type => HostType, lserver => LServer, element => undefined }), mongoose_hooks:remove_user(LServer, Acc, LUser), @@ -447,8 +458,7 @@ remove_user(#jid{luser = LUser, lserver = LServer}) -> user => LUser, server => LServer, auth_modules => AuthModules}), {error, not_allowed} - end; -remove_user(error) -> error. + end. %% @doc Calculate informational entropy. -spec entropy(iolist()) -> float(). diff --git a/src/auth/ejabberd_auth_anonymous.erl b/src/auth/ejabberd_auth_anonymous.erl index c0e0b083458..cd0fbaa1865 100644 --- a/src/auth/ejabberd_auth_anonymous.erl +++ b/src/auth/ejabberd_auth_anonymous.erl @@ -31,7 +31,7 @@ stop/1, anonymous_user_exist/2, allow_multiple_connections/1, - register_connection/4, + register_connection/5, unregister_connection/5, session_cleanup/5 ]). @@ -153,13 +153,14 @@ remove_connection(SID, LUser, LServer) -> %% @doc Register connection -spec register_connection(Acc, + HostType :: binary(), SID :: ejabberd_sm:sid(), JID :: jid:jid(), Info :: list()) -> Acc when Acc :: any(). -register_connection(Acc, SID, #jid{luser = LUser, lserver = LServer}, Info) -> +register_connection(Acc, HostType, SID, #jid{luser = LUser, lserver = LServer}, Info) -> case lists:keyfind(auth_module, 1, Info) of {_, ?MODULE} -> - mongoose_hooks:register_user(LServer, LUser), + mongoose_hooks:register_user(HostType, LServer, LUser), US = {LUser, LServer}, mnesia:sync_dirty( fun() -> mnesia:write(#anonymous{us = US, sid=SID}) diff --git a/src/global_distrib/mod_global_distrib_mapping.erl b/src/global_distrib/mod_global_distrib_mapping.erl index 734b3ed0f88..00bffa70493 100644 --- a/src/global_distrib/mod_global_distrib_mapping.erl +++ b/src/global_distrib/mod_global_distrib_mapping.erl @@ -32,7 +32,7 @@ cache_domain/2, delete_for_domain/1, all_domains/0, public_domains/0]). -export([for_jid/1, insert_for_jid/1, cache_jid/2, delete_for_jid/1, clear_cache/1]). -export([register_subhost/3, unregister_subhost/2, packet_to_component/3, - session_opened/4, session_closed/5]). + session_opened/5, session_closed/5]). -export([endpoints/1, hosts/0]). -type endpoint() :: mod_global_distrib_utils:endpoint(). @@ -163,9 +163,9 @@ deps(Host, Opts) -> %% Hooks implementation %%-------------------------------------------------------------------- --spec session_opened(Acc, ejabberd_sm:sid(), UserJID :: jid:jid(), Info :: list()) -> +-spec session_opened(Acc, binary(), ejabberd_sm:sid(), UserJID :: jid:jid(), Info :: list()) -> Acc when Acc :: any(). -session_opened(Acc, _SID, UserJid, _Info) -> +session_opened(Acc, _HostType, _SID, UserJid, _Info) -> insert_for_jid(UserJid), Acc. diff --git a/src/metrics/mongoose_metrics_hooks.erl b/src/metrics/mongoose_metrics_hooks.erl index 81356ac7aeb..c2ed978f6cd 100644 --- a/src/metrics/mongoose_metrics_hooks.erl +++ b/src/metrics/mongoose_metrics_hooks.erl @@ -16,7 +16,7 @@ %%------------------- %% Internal exports %%------------------- --export([sm_register_connection_hook/4, +-export([sm_register_connection_hook/5, sm_remove_connection_hook/5, auth_failed/3, user_send_packet/4, @@ -69,9 +69,9 @@ get_hooks(Host) -> {sm_broadcast, Host, ?MODULE, privacy_list_push, 1} | mongoose_metrics_mam_hooks:get_hooks(Host)]. --spec sm_register_connection_hook(map(), tuple(), jid:jid(), term() +-spec sm_register_connection_hook(map(), binary(), tuple(), jid:jid(), term() ) -> metrics_notify_return(). -sm_register_connection_hook(Acc, _, #jid{server = Server}, _) -> +sm_register_connection_hook(Acc, _HostType, _, #jid{server = Server}, _) -> mongoose_metrics:update(Server, sessionSuccessfulLogins, 1), mongoose_metrics:update(Server, sessionCount, 1), Acc. diff --git a/src/mod_ping.erl b/src/mod_ping.erl index d5c12ed6399..df55a1bc1d3 100644 --- a/src/mod_ping.erl +++ b/src/mod_ping.erl @@ -23,7 +23,7 @@ %% Hook callbacks -export([iq_ping/4, - user_online/4, + user_online/5, user_offline/5, user_send/4, user_ping_response/4, @@ -144,7 +144,7 @@ handle_remote_hook(HandlerState, mod_ping, Args, C2SState) -> handle_remote_hook(HandlerState, _, _, _) -> HandlerState. -user_online(Acc, {_, Pid} = _SID, _Jid, _Info) -> +user_online(Acc, _HostType, {_, Pid} = _SID, _Jid, _Info) -> ejabberd_c2s:run_remote_hook(Pid, mod_ping, init), Acc. diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 672d8db416e..507edccdf3f 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -26,7 +26,7 @@ push_notifications/4, register_command/1, register_subhost/2, - register_user/2, + register_user/3, remove_user/3, resend_offline_messages_hook/3, rest_user_send_packet/4, @@ -359,12 +359,13 @@ register_subhost(LDomain, IsHidden) -> %%% @doc The `register_user' hook is called when a user is successfully %%% registered in an authentication backend. --spec register_user(LServer, LUser) -> Result when +-spec register_user(HostType, LServer, LUser) -> Result when + HostType :: binary(), LServer :: jid:lserver(), LUser :: jid:luser(), Result :: any(). -register_user(LServer, LUser) -> - ejabberd_hooks:run_for_host_type(register_user, LServer, ok, [LUser, LServer]). +register_user(HostType, LServer, LUser) -> + ejabberd_hooks:run_for_host_type(register_user, HostType, ok, [LUser, LServer]). %%% @doc The `remove_user' hook is called when a user is removed. -spec remove_user(LServer, Acc, LUser) -> Result when @@ -373,7 +374,8 @@ register_user(LServer, LUser) -> LUser :: jid:luser(), Result :: mongoose_acc:t(). remove_user(LServer, Acc, LUser) -> - ejabberd_hooks:run_for_host_type(remove_user, LServer, Acc, [LUser, LServer]). + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(remove_user, HostType, Acc, [LUser, LServer]). -spec resend_offline_messages_hook(HostType, Acc, JID) -> Result when HostType :: binary(), @@ -746,7 +748,7 @@ sm_filter_offline_message(HostType, From, To, Packet) -> Result :: ok. sm_register_connection_hook(HostType, SID, JID, Info) -> ejabberd_hooks:run_for_host_type(sm_register_connection_hook, HostType, ok, - [SID, JID, Info]). + [HostType, SID, JID, Info]). -spec sm_remove_connection_hook(Acc, SID, JID, Info, Reason) -> Result when Acc :: mongoose_acc:t(), diff --git a/test/mongoose_cleanup_SUITE.erl b/test/mongoose_cleanup_SUITE.erl index 1ab0327bee7..a058acbcdfb 100644 --- a/test/mongoose_cleanup_SUITE.erl +++ b/test/mongoose_cleanup_SUITE.erl @@ -84,7 +84,7 @@ auth_anonymous(_Config) -> {U, S, R, JID, SID} = get_fake_session(), ejabberd_auth_anonymous:start(S), Info = [{auth_module, ejabberd_auth_anonymous}], - ejabberd_auth_anonymous:register_connection(#{}, SID, JID, Info), + ejabberd_auth_anonymous:register_connection(#{}, S, SID, JID, Info), true = ejabberd_auth_anonymous:anonymous_user_exist(U, S), mongoose_hooks:session_cleanup(S, ok, U, R, SID), false = ejabberd_auth_anonymous:anonymous_user_exist(U, S). From 6348b76f73bb7ea6ff28a75c334d2efdec2d35cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Wed, 28 Apr 2021 08:47:44 +0200 Subject: [PATCH 19/24] Use host_type in local_send_to_resource_hook --- src/ejabberd_local.erl | 8 ++------ src/mongoose_hooks.erl | 10 +++++----- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/ejabberd_local.erl b/src/ejabberd_local.erl index 789ecfc21e0..fec63e3cdc6 100644 --- a/src/ejabberd_local.erl +++ b/src/ejabberd_local.erl @@ -231,7 +231,7 @@ unregister_iq_handler(Host, XMLNS) -> refresh_iq_handlers() -> ejabberd_local ! refresh_iq_handlers. --spec bounce_resource_packet(Acc:: mongoose_acc:t(), +-spec bounce_resource_packet(Acc :: mongoose_acc:t(), From :: jid:jid(), To :: jid:jid(), El :: exml:element()) -> {'stop', mongoose_acc:t()}. @@ -414,11 +414,7 @@ do_route(Acc, From, To, El) -> case mongoose_acc:stanza_type(Acc) of <<"error">> -> Acc; <<"result">> -> Acc; - _ -> - mongoose_hooks:local_send_to_resource_hook( - To#jid.lserver, - Acc, - From, To, El) + _ -> mongoose_hooks:local_send_to_resource_hook(Acc, From, To, El) end end. diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 507edccdf3f..eb2fe313852 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -19,7 +19,7 @@ filter_local_packet/2, filter_packet/1, inbox_unread_count/3, - local_send_to_resource_hook/5, + local_send_to_resource_hook/4, get_key/2, packet_to_component/3, presence_probe_hook/5, @@ -290,15 +290,15 @@ filter_packet(Acc) -> inbox_unread_count(LServer, Acc, User) -> ejabberd_hooks:run_for_host_type(inbox_unread_count, LServer, Acc, [User]). --spec local_send_to_resource_hook(LServer, Acc, From, To, Packet) -> Result when - LServer :: jid:lserver(), +-spec local_send_to_resource_hook(Acc, From, To, Packet) -> Result when Acc :: mongoose_acc:t(), From :: jid:jid(), To :: jid:jid(), Packet :: exml:element(), Result :: mongoose_acc:t(). -local_send_to_resource_hook(Server, Acc, From, To, Packet) -> - ejabberd_hooks:run_for_host_type(local_send_to_resource_hook, Server, Acc, +local_send_to_resource_hook(Acc, From, To, Packet) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(local_send_to_resource_hook, HostType, Acc, [From, To, Packet]). %%% @doc The `get_key' hook is called to extract a key from `mod_keystore'. From e677477bee7fe0d4832d2fc2df2216f2a01c282c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Wed, 28 Apr 2021 08:58:29 +0200 Subject: [PATCH 20/24] Use host_type in session_cleanup --- src/mongoose_hooks.erl | 3 ++- test/mongoose_cleanup_SUITE.erl | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index eb2fe313852..96f06995cd9 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -407,7 +407,8 @@ rest_user_send_packet(Acc, From, To, Packet) -> SID :: ejabberd_sm:sid(), Result :: mongoose_acc:t(). session_cleanup(Server, Acc, User, Resource, SID) -> - ejabberd_hooks:run_for_host_type(session_cleanup, Server, Acc, + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(session_cleanup, HostType, Acc, [User, Server, Resource, SID]). %%% @doc The `set_vcard' hook is called when the caller wants to set the VCard. diff --git a/test/mongoose_cleanup_SUITE.erl b/test/mongoose_cleanup_SUITE.erl index a058acbcdfb..4f214f982d1 100644 --- a/test/mongoose_cleanup_SUITE.erl +++ b/test/mongoose_cleanup_SUITE.erl @@ -86,7 +86,7 @@ auth_anonymous(_Config) -> Info = [{auth_module, ejabberd_auth_anonymous}], ejabberd_auth_anonymous:register_connection(#{}, S, SID, JID, Info), true = ejabberd_auth_anonymous:anonymous_user_exist(U, S), - mongoose_hooks:session_cleanup(S, ok, U, R, SID), + mongoose_hooks:session_cleanup(S, new_acc(S), U, R, SID), false = ejabberd_auth_anonymous:anonymous_user_exist(U, S). last(_Config) -> @@ -98,7 +98,7 @@ last(_Config) -> {ok, TS1, Status1} = mod_last:get_last_info(U, S), async_helper:wait_until( fun() -> - mongoose_hooks:session_cleanup(S, ok, U, R, SID), + mongoose_hooks:session_cleanup(S, new_acc(S), U, R, SID), {ok, TS2, <<>>} = mod_last:get_last_info(U, S), TS2 - TS1 > 0 end, @@ -110,12 +110,7 @@ stream_management(_Config) -> SMID = <<"123">>, mod_stream_management:register_smid(SMID, SID), {sid, SID} = mod_stream_management:get_sid(SMID), - Acc = mongoose_acc:new( - #{location => ?LOCATION, - lserver => S, - host_type => S, - element => undefined}), - mongoose_hooks:session_cleanup(S, Acc, U, R, SID), + mongoose_hooks:session_cleanup(S, new_acc(S), U, R, SID), {error, smid_not_found} = mod_stream_management:get_sid(SMID). local(_Config) -> @@ -215,3 +210,8 @@ get_fake_session() -> SID = {os:timestamp(), self()}, {U, S, R, JID, SID}. +new_acc(Server) -> + mongoose_acc:new(#{location => ?LOCATION, + lserver => Server, + host_type => Server, + element => undefined}). From c376aed80bc9964a74c7f89cb762609ae3e135d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Wed, 28 Apr 2021 09:45:26 +0200 Subject: [PATCH 21/24] Add host_type to further s2s hooks --- src/ejabberd_s2s_in.erl | 19 +++++++++++-------- src/mongoose_hooks.erl | 18 +++++++++--------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/ejabberd_s2s_in.erl b/src/ejabberd_s2s_in.erl index 8356d4d5937..a71a3b5ec3e 100644 --- a/src/ejabberd_s2s_in.erl +++ b/src/ejabberd_s2s_in.erl @@ -207,9 +207,7 @@ wait_for_stream({xmlstreamstart, _Name, Attrs}, StateData) -> _ -> send_element(StateData, #xmlel{name = <<"stream:features">>, - children = SASL ++ - StartTLS ++ - mongoose_hooks:s2s_stream_features(Server)}), + children = SASL ++ StartTLS ++ stream_features(Server)}), {next_state, wait_for_feature_request, StateData#state{server = Server}} end; {<<"jabber:server">>, _, Server, true} when @@ -217,7 +215,7 @@ wait_for_stream({xmlstreamstart, _Name, Attrs}, StateData) -> send_text(StateData, ?STREAM_HEADER(<<" version='1.0'">>)), send_element(StateData, #xmlel{name = <<"stream:features">>, - children = mongoose_hooks:s2s_stream_features(Server)}), + children = stream_features(Server)}), {next_state, stream_established, StateData}; {<<"jabber:server">>, <<"jabber:server:dialback">>, _Server, _} -> send_text(StateData, ?STREAM_HEADER(<<"">>)), @@ -228,7 +226,8 @@ wait_for_stream({xmlstreamstart, _Name, Attrs}, StateData) -> end; wait_for_stream({xmlstreamerror, _}, StateData) -> send_text(StateData, - <<(?STREAM_HEADER(<<"">>))/binary, (mongoose_xmpp_errors:xml_not_well_formed_bin())/binary, + <<(?STREAM_HEADER(<<"">>))/binary, + (mongoose_xmpp_errors:xml_not_well_formed_bin())/binary, (?STREAM_TRAILER)/binary>>), {stop, normal, StateData}; wait_for_stream(timeout, StateData) -> @@ -236,7 +235,6 @@ wait_for_stream(timeout, StateData) -> wait_for_stream(closed, StateData) -> {stop, normal, StateData}. - -spec wait_for_feature_request(ejabberd:xml_stream_item(), state() ) -> fsm_return(). wait_for_feature_request({xmlstreamelement, El}, StateData) -> @@ -456,8 +454,7 @@ route_stanza(_, _Acc) -> route_stanza(Acc) -> From = mongoose_acc:from_jid(Acc), To = mongoose_acc:to_jid(Acc), - LTo = To#jid.lserver, - Acc1 = mongoose_hooks:s2s_receive_packet(LTo, Acc), + Acc1 = mongoose_hooks:s2s_receive_packet(Acc), ejabberd_router:route(From, To, Acc1). %%---------------------------------------------------------------------- @@ -584,6 +581,12 @@ send_text(StateData, Text) -> send_element(StateData, El) -> send_text(StateData, exml:to_binary(El)). +-spec stream_features(binary()) -> [exml:element()]. +stream_features(Domain) -> + case mongoose_domain_api:get_host_type(Domain) of + {ok, HostType} -> mongoose_hooks:s2s_stream_features(HostType, Domain); + {error, not_found} -> [] + end. -spec change_shaper(state(), Host :: 'global' | binary(), jid:jid()) -> any(). change_shaper(StateData, Host, JID) -> diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 96f06995cd9..b11cf83d16c 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -114,8 +114,8 @@ -export([find_s2s_bridge/2, s2s_allow_host/2, s2s_connect_hook/2, - s2s_receive_packet/2, - s2s_stream_features/1, + s2s_receive_packet/1, + s2s_stream_features/2, s2s_send_packet/4]). -export([disco_info/4, @@ -1237,20 +1237,20 @@ s2s_send_packet(Acc, From, To, Packet) -> %%% @doc `s2s_stream_features' hook is used to extract %%% the stream management features supported by the server. --spec s2s_stream_features(Server) -> Result when +-spec s2s_stream_features(HostType, Server) -> Result when + HostType :: binary(), Server :: jid:server(), Result :: [exml:element()]. -s2s_stream_features(Server) -> - ejabberd_hooks:run_for_host_type(s2s_stream_features, Server, [], [Server]). +s2s_stream_features(HostType, Server) -> + ejabberd_hooks:run_for_host_type(s2s_stream_features, HostType, [], [Server]). %%% @doc `s2s_receive_packet' hook is called when %%% an incoming stanza is routed by the server. --spec s2s_receive_packet(LServer, Acc) -> Result when - LServer :: jid:lserver(), +-spec s2s_receive_packet(Acc) -> Result when Acc :: mongoose_acc:t(), Result :: mongoose_acc:t(). -s2s_receive_packet(LServer, Acc) -> - ejabberd_hooks:run_for_host_type(s2s_receive_packet, LServer, Acc, []). +s2s_receive_packet(Acc) -> + ejabberd_hooks:run_global(s2s_receive_packet, Acc, []). %% Discovery related hooks From 1a47fea8d645b867cf8cb3bd2c21bdcbb1c5db88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Wed, 28 Apr 2021 11:51:07 +0200 Subject: [PATCH 22/24] Use host_type in xmpp_stanza_dropped --- src/mongoose_hooks.erl | 8 ++++---- src/mongoose_local_delivery.erl | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index b11cf83d16c..d0a927b5d71 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -529,14 +529,14 @@ xmpp_send_element(HostType, Acc, El) -> %%% @doc The `xmpp_stanza_dropped' hook is called to inform that %%% an xmpp stanza has been dropped. --spec xmpp_stanza_dropped(Server, From, To, Packet) -> Result when - Server :: jid:lserver(), +-spec xmpp_stanza_dropped(HostType, From, To, Packet) -> Result when + HostType :: binary(), From :: jid:jid(), To :: jid:jid(), Packet :: exml:element(), Result :: any(). -xmpp_stanza_dropped(Server, From, To, Packet) -> - ejabberd_hooks:run_for_host_type(xmpp_stanza_dropped, Server, ok, +xmpp_stanza_dropped(HostType, From, To, Packet) -> + ejabberd_hooks:run_for_host_type(xmpp_stanza_dropped, HostType, ok, [From, To, Packet]). %% C2S related hooks diff --git a/src/mongoose_local_delivery.erl b/src/mongoose_local_delivery.erl index 24a1dd7f36c..49fbee78c9b 100644 --- a/src/mongoose_local_delivery.erl +++ b/src/mongoose_local_delivery.erl @@ -27,7 +27,7 @@ do_route(OrigFrom, OrigTo, OrigAcc, OrigPacket, LDstDomain, Handler) -> Acc1 = mongoose_acc:update_stanza(#{from_jid => From, to_jid => To, element => Packet}, Acc), mongoose_packet_handler:process(Handler, Acc1, From, To, Packet); drop -> - mongoose_hooks:xmpp_stanza_dropped(OrigFrom#jid.lserver, OrigFrom, + mongoose_hooks:xmpp_stanza_dropped(mongoose_acc:host_type(Acc0), OrigFrom, OrigTo, OrigPacket), ok end. From 2319c9b539948448ec259c0499c11990e5b6d5c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Wed, 28 Apr 2021 12:26:01 +0200 Subject: [PATCH 23/24] Use host_type in get_personal_data --- src/admin_extra/service_admin_extra_gdpr.erl | 3 ++- src/mongoose_hooks.erl | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/admin_extra/service_admin_extra_gdpr.erl b/src/admin_extra/service_admin_extra_gdpr.erl index e557a44bc06..e611fe41cf5 100644 --- a/src/admin_extra/service_admin_extra_gdpr.erl +++ b/src/admin_extra/service_admin_extra_gdpr.erl @@ -65,7 +65,8 @@ get_data_from_modules(Username, Domain) -> -spec get_data_from_modules(jid:jid()) -> gdpr:personal_data(). get_data_from_modules(JID) -> - mongoose_hooks:get_personal_data(JID#jid.lserver, JID). + {ok, HostType} = mongoose_domain_api:get_host_type(JID#jid.lserver), + mongoose_hooks:get_personal_data(HostType, JID). -spec to_csv_file(CsvFilename :: binary(), gdpr:schema(), gdpr:entities(), file:name()) -> ok. to_csv_file(Filename, DataSchema, DataRows, TmpDir) -> diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index d0a927b5d71..65ad73a817b 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -1187,12 +1187,12 @@ get_mam_muc_gdpr_data(HookServer, JID) -> %%% @doc `get_personal_data' hook is called to retrieve %%% a user's personal data for GDPR purposes. --spec get_personal_data(LServer, JID) -> Result when - LServer :: jid:lserver(), +-spec get_personal_data(HostType, JID) -> Result when + HostType :: binary(), JID :: jid:jid(), Result :: gdpr:personal_data(). -get_personal_data(LServer, JID) -> - ejabberd_hooks:run_for_host_type(get_personal_data, LServer, [], [JID]). +get_personal_data(HostType, JID) -> + ejabberd_hooks:run_for_host_type(get_personal_data, HostType, [], [JID]). %% S2S related hooks From fe918300813d33afd88509192c69101e6faa18ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Wed, 28 Apr 2021 12:37:24 +0200 Subject: [PATCH 24/24] Use host_type in roster_get --- src/admin_extra/service_admin_extra_roster.erl | 2 +- src/mod_commands.erl | 2 +- src/mod_disco.erl | 2 +- src/mod_roster.erl | 2 +- src/mongoose_hooks.erl | 10 +++++----- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/admin_extra/service_admin_extra_roster.erl b/src/admin_extra/service_admin_extra_roster.erl index 3efd7fbf450..934be7887ec 100644 --- a/src/admin_extra/service_admin_extra_roster.erl +++ b/src/admin_extra/service_admin_extra_roster.erl @@ -251,7 +251,7 @@ get_roster(User, Server) -> Acc = mongoose_acc:new(#{location => ?LOCATION, lserver => UserJID#jid.lserver, element => undefined}), - Acc2 = mongoose_hooks:roster_get(Server, Acc, UserJID), + Acc2 = mongoose_hooks:roster_get(Acc, UserJID), Items = mongoose_acc:get(roster, items, [], Acc2), make_roster(Items). diff --git a/src/mod_commands.erl b/src/mod_commands.erl index 9d407c2285b..fc3fa759439 100644 --- a/src/mod_commands.erl +++ b/src/mod_commands.erl @@ -343,7 +343,7 @@ list_contacts(Caller) -> lserver => LServer, element => undefined }), Acc1 = mongoose_acc:set(roster, show_full_roster, true, Acc0), - Acc2 = mongoose_hooks:roster_get(LServer, Acc1, CallerJID), + Acc2 = mongoose_hooks:roster_get(Acc1, CallerJID), Res = mongoose_acc:get(roster, items, Acc2), [roster_info(mod_roster:item_to_map(I)) || I <- Res]. diff --git a/src/mod_disco.erl b/src/mod_disco.erl index 1b77724c910..471000bf031 100644 --- a/src/mod_disco.erl +++ b/src/mod_disco.erl @@ -408,7 +408,7 @@ is_presence_subscribed(#jid{luser = LFromUser, lserver = LFromServer} = FromJID, A = mongoose_acc:new(#{ location => ?LOCATION, lserver => LFromServer, element => undefined }), - A2 = mongoose_hooks:roster_get(LFromServer, A, FromJID), + A2 = mongoose_hooks:roster_get(A, FromJID), Roster = mongoose_acc:get(roster, items, [], A2), lists:any(fun({roster, _, _, JID, _, S, _, _, _, _}) -> {TUser, TServer} = jid:to_lus(JID), diff --git a/src/mod_roster.erl b/src/mod_roster.erl index 9e1f2ded187..11943e6b875 100644 --- a/src/mod_roster.erl +++ b/src/mod_roster.erl @@ -1098,7 +1098,7 @@ get_roster_old(DestServer, JID) -> A = mongoose_acc:new(#{ location => ?LOCATION, lserver => DestServer, element => undefined }), - A2 = mongoose_hooks:roster_get(DestServer, A, JID), + A2 = mongoose_hooks:roster_get(A, JID), mongoose_acc:get(roster, items, [], A2). -spec item_to_map(roster()) -> map(). diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 65ad73a817b..3403c33b356 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -72,7 +72,7 @@ unset_presence_hook/3, xmpp_bounce_message/1]). --export([roster_get/3, +-export([roster_get/2, roster_get_jid_info/3, roster_get_subscription_lists/3, roster_get_versioning_feature/2, @@ -783,13 +783,13 @@ xmpp_bounce_message(Acc) -> %% Roster related hooks %%% @doc The `roster_get' hook is called to extract a user's roster. --spec roster_get(LServer, Acc, JID) -> Result when - LServer :: jid:lserver(), +-spec roster_get(Acc, JID) -> Result when Acc :: mongoose_acc:t(), JID :: jid:jid(), Result :: mongoose_acc:t(). -roster_get(LServer, Acc, JID) -> - ejabberd_hooks:run_for_host_type(roster_get, LServer, Acc, [JID]). +roster_get(Acc, JID) -> + HostType = mongoose_acc:host_type(Acc), + ejabberd_hooks:run_for_host_type(roster_get, HostType, Acc, [JID]). %%% @doc The `roster_groups' hook is called to extract roster groups. -spec roster_groups(LServer) -> Result when