Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add host_type to hook calls in core modules #3097

Merged
merged 24 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d2ddf6b
Restrict arguments of run_for_host_type
chrzaszcz Apr 23, 2021
d317c2e
Get rid of deprecated functions
chrzaszcz Apr 23, 2021
8f0abd4
Add host_type to sm_register_connection_hook
chrzaszcz Apr 23, 2021
95c6e1e
Use host_type in sm_remove_connection_hook
chrzaszcz Apr 23, 2021
2c5ab01
Set 'lserver' (and 'host_type') in Acc from the recipient
chrzaszcz Apr 23, 2021
9a5174c
Use host type in xmpp_bounce_message
chrzaszcz Apr 23, 2021
e3aa84a
Get host type from Acc for (un)set_presence hooks
chrzaszcz Apr 26, 2021
8822067
Do not calculate host_type if it is already in state
chrzaszcz Apr 26, 2021
480c5ea
Unify acc construction for brevity and consistency
chrzaszcz Apr 26, 2021
abe1930
Use host_type in sm_broadcast
chrzaszcz Apr 26, 2021
a2cf4e7
Use host_type in other hooks called from ejabberd_sm
chrzaszcz Apr 26, 2021
7d31c08
Use host_type in remaining hooks called from ejabberd_sm
chrzaszcz Apr 26, 2021
ae743ea
Use host_type in mongoose_privacy
chrzaszcz Apr 26, 2021
38a5fb7
Use Acc for host_type in privacy checks
chrzaszcz Apr 26, 2021
9afd2d0
Fix type spec
chrzaszcz Apr 26, 2021
530c21c
Make s2s hooks global
chrzaszcz Apr 27, 2021
c6ba765
Use host_type in rest_user_send_packet
chrzaszcz Apr 27, 2021
0925ddd
Add host_type to register_user and remove_user
chrzaszcz Apr 27, 2021
6348b76
Use host_type in local_send_to_resource_hook
chrzaszcz Apr 28, 2021
e677477
Use host_type in session_cleanup
chrzaszcz Apr 28, 2021
c376aed
Add host_type to further s2s hooks
chrzaszcz Apr 28, 2021
1a47fea
Use host_type in xmpp_stanza_dropped
chrzaszcz Apr 28, 2021
2319c9b
Use host_type in get_personal_data
chrzaszcz Apr 28, 2021
fe91830
Use host_type in roster_get
chrzaszcz Apr 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/admin_extra/service_admin_extra_gdpr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down
2 changes: 1 addition & 1 deletion src/admin_extra/service_admin_extra_roster.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
20 changes: 15 additions & 5 deletions src/auth/ejabberd_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it could be remove_user(Acc, LServer, LUser). Though backward compatibility would suffer.

Copy link
Member Author

@chrzaszcz chrzaszcz Apr 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather rearrange the hook arguments in a separate PR as these changes are quite tricky.

As I said in the PR description: Do not rearrange the arguments when not necessary.

Expand All @@ -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().
Expand Down
7 changes: 4 additions & 3 deletions src/auth/ejabberd_auth_anonymous.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
]).
Expand Down Expand Up @@ -153,13 +153,14 @@ remove_connection(SID, LUser, LServer) ->

%% @doc Register connection
-spec register_connection(Acc,
HostType :: binary(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this type defined in mongooseim:host_type() since yesterday

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen it. I don't think it makes sense to amend this PR though. I'd rather do it later.

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})
Expand Down
70 changes: 29 additions & 41 deletions src/ejabberd_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1033,11 +1033,8 @@ code_change(_OldVsn, StateName, StateData, _Extra) ->


%%% system events
handle_info({exit, Reason}, _StateName, StateData) ->
Lang = StateData#state.lang,
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => ejabberd_c2s_state:server(StateData),
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};
Expand All @@ -1048,11 +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} = StateData)
#state{pres_last = Presence, pres_invis = Invisible} = StateData)
when Presence =/= undefined orelse Invisible ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
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)
Expand Down Expand Up @@ -1132,9 +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,
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}),
Expand All @@ -1144,12 +1137,11 @@ 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
Acc = mongoose_acc:new(#{ location => ?LOCATION,
from_jid => From,
to_jid => To,
lserver => To#jid.lserver,
element => Packet }),
#state{server = Server, host_type = HostType} = StateData,
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
Expand Down Expand Up @@ -1509,8 +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, 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
Expand Down Expand Up @@ -1648,11 +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,
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),
Expand Down Expand Up @@ -2051,8 +2041,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(),
Expand Down Expand Up @@ -2088,11 +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,
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.

Expand All @@ -2102,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.server,
StateData#state.jid,
StateData#state.privacy_list,
To,
Expand All @@ -2116,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.server,
StateData#state.jid,
StateData#state.privacy_list,
From,
Expand Down Expand Up @@ -3090,7 +3076,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,
Expand Down Expand Up @@ -3176,8 +3162,7 @@ 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}),
Acc = new_acc(SD, #{location => ?LOCATION, element => undefined}),
ejabberd_sm:close_session(Acc,
SD#state.sid,
SD#state.jid,
Expand Down Expand Up @@ -3324,10 +3309,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}) ->
element_to_origin_accum(El, StateData = #state{sid = SID, jid = JID}) ->
BaseParams = #{
location => ?LOCATION,
lserver => Server,
element => El,
from_jid => JID
},
Expand All @@ -3336,7 +3320,7 @@ element_to_origin_accum(El, #state{sid = SID, jid = JID, server = Server}) ->
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).

Expand Down Expand Up @@ -3369,3 +3353,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}).
9 changes: 7 additions & 2 deletions src/ejabberd_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).

%%%----------------------------------------------------------------------
Expand Down
10 changes: 3 additions & 7 deletions src/ejabberd_local.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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()}.
Expand Down Expand Up @@ -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
Expand All @@ -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.

Expand Down
5 changes: 1 addition & 4 deletions src/ejabberd_s2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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} ->
Expand Down
Loading