Update mod_filter to work with recent ejabberd

This commit is contained in:
Badlop 2018-09-18 11:53:25 +02:00
parent 1a6284efa9
commit a43bc6f794
1 changed files with 27 additions and 29 deletions

View File

@ -3,21 +3,17 @@
%%% Author : Magnus Henoch <henoch@dtek.chalmers.se> %%% Author : Magnus Henoch <henoch@dtek.chalmers.se>
%%% Purpose : flexible filtering by server policy %%% Purpose : flexible filtering by server policy
%%% Created : 21 Sep 2005 by Magnus Henoch <henoch@dtek.chalmers.se> %%% Created : 21 Sep 2005 by Magnus Henoch <henoch@dtek.chalmers.se>
%%% Updated : 14 Jan 2016 by John Brodie <john@brodie.me>
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
-module(mod_filter). -module(mod_filter).
-author('henoch@dtek.chalmers.se'). -author('henoch@dtek.chalmers.se').
%% -vsn('$Revision$ ').
-behaviour(gen_mod). -behaviour(gen_mod).
-export([start/2, stop/1, -export([start/2, stop/1, depends/2, mod_options/1, filter_packet/1]).
filter_packet/1, mod_opt_type/1]).
-include("logger.hrl"). -include("logger.hrl").
-include("ejabberd.hrl"). -include("xmpp.hrl").
-include("jlib.hrl").
start(_Host, _Opts) -> start(_Host, _Opts) ->
ejabberd_hooks:add(filter_packet, global, ?MODULE, filter_packet, 100). ejabberd_hooks:add(filter_packet, global, ?MODULE, filter_packet, 100).
@ -30,13 +26,15 @@ stop(_Host) ->
%% From and To are jid records. %% From and To are jid records.
filter_packet(drop) -> filter_packet(drop) ->
drop; drop;
filter_packet({From, To, Packet} = Input) -> filter_packet(Packet) ->
From = xmpp:get_from(Packet),
To = xmpp:get_to(Packet),
%% It probably doesn't make any sense to block packets to oneself. %% It probably doesn't make any sense to block packets to oneself.
R = if From#jid.luser == To#jid.luser, R = if From#jid.luser == To#jid.luser,
From#jid.lserver == To#jid.lserver -> From#jid.lserver == To#jid.lserver ->
Input; Packet;
true -> true ->
check_stanza(Input) check_stanza(Packet)
end, end,
?DEBUG("filtering packet...~nFrom: ~p~nTo: ~p~nPacket: ~p~nResult: ~p", ?DEBUG("filtering packet...~nFrom: ~p~nTo: ~p~nPacket: ~p~nResult: ~p",
[From, To, Packet, R]), [From, To, Packet, R]),
@ -46,35 +44,35 @@ filter_packet({From, To, Packet} = Input) ->
_ -> R _ -> R
end. end.
check_stanza({_From, _To, #xmlel{name = StanzaType}} = Input) -> check_stanza(Packet) ->
AccessRule = case StanzaType of AccessRule = case element(1, Packet) of
<<"presence">> -> presence ->
mod_filter_presence; mod_filter_presence;
<<"message">> -> message ->
mod_filter_message; mod_filter_message;
<<"iq">> -> iq ->
mod_filter_iq mod_filter_iq
end, end,
check_stanza_type(AccessRule, Input). check_stanza_type(AccessRule, Packet).
check_stanza_type(AccessRule, {From, To, _Packet} = Input) -> check_stanza_type(AccessRule, Packet) ->
FromAccess = acl:match_rule(global, AccessRule, From), FromAccess = acl:match_rule(global, AccessRule, xmpp:get_from(Packet)),
case FromAccess of case FromAccess of
allow -> allow ->
check_access(Input); check_access(Packet);
deny -> deny ->
{drop, AccessRule, sender}; {drop, AccessRule, sender};
ToAccessRule -> ToAccessRule ->
ToAccess = acl:match_rule(global, ToAccessRule, To), ToAccess = acl:match_rule(global, ToAccessRule, xmpp:get_to(Packet)),
case ToAccess of case ToAccess of
allow -> allow ->
check_access(Input); check_access(Packet);
deny -> deny ->
{drop, AccessRule, receiver} {drop, AccessRule, receiver}
end end
end. end.
check_access({From, To, _Packet} = Input) -> check_access(Packet) ->
%% Beginning of a complicated ACL matching procedure. %% Beginning of a complicated ACL matching procedure.
%% The access option given to the module applies to senders. %% The access option given to the module applies to senders.
@ -82,26 +80,26 @@ check_access({From, To, _Packet} = Input) ->
%% anymore what "host" we are on. Thus hardcoding access rule. %% anymore what "host" we are on. Thus hardcoding access rule.
%%AccessRule = gen_mod:get_module_opt(global, ?MODULE, access, all), %%AccessRule = gen_mod:get_module_opt(global, ?MODULE, access, all),
AccessRule = mod_filter, AccessRule = mod_filter,
FromAccess = acl:match_rule(global, AccessRule, From), FromAccess = acl:match_rule(global, AccessRule, xmpp:get_from(Packet)),
%% If the rule results in 'allow' or 'deny', treat that as the %% If the rule results in 'allow' or 'deny', treat that as the
%% result. Else it is a rule to be applied to the receiver. %% result. Else it is a rule to be applied to the receiver.
case FromAccess of case FromAccess of
allow -> allow ->
Input; Packet;
deny -> deny ->
{drop, sender}; {drop, sender};
ToAccessRule -> ToAccessRule ->
ToAccess = acl:match_rule(global, ToAccessRule, To), ToAccess = acl:match_rule(global, ToAccessRule, xmpp:get_to(Packet)),
case ToAccess of case ToAccess of
allow -> allow ->
Input; Packet;
deny -> deny ->
{drop, receiver} {drop, receiver}
end end
end. end.
mod_opt_type(access) ->
fun (A) when is_atom(A) -> A end;
mod_opt_type(_) -> depends(_Host, _Opts) ->
[access]. [].
mod_options(_) -> [].