2017-01-16 16:13:48 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% File : mod_block_strangers.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Block packets from non-subscribers
|
|
|
|
%%% Created : 25 Dec 2016 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2024-01-22 16:40:01 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2024 ProcessOne
|
2017-01-16 16:13:48 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(mod_block_strangers).
|
|
|
|
|
|
|
|
-author('alexey@process-one.net').
|
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
|
|
|
%% API
|
2020-01-08 10:24:51 +01:00
|
|
|
-export([start/2, stop/1, reload/3, mod_doc/0,
|
2018-01-23 08:54:52 +01:00
|
|
|
depends/2, mod_opt_type/1, mod_options/1]).
|
2017-01-16 16:13:48 +01:00
|
|
|
|
2018-01-26 13:02:06 +01:00
|
|
|
-export([filter_packet/1, filter_offline_msg/1, filter_subscription/2]).
|
2017-01-16 16:13:48 +01:00
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2017-01-16 16:13:48 +01:00
|
|
|
-include("logger.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2017-01-16 16:13:48 +01:00
|
|
|
|
|
|
|
-define(SETS, gb_sets).
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-type c2s_state() :: ejabberd_c2s:state().
|
|
|
|
|
2018-01-26 13:02:06 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Callbacks and hooks
|
|
|
|
%%%===================================================================
|
2023-08-04 17:53:50 +02:00
|
|
|
start(_Host, _Opts) ->
|
|
|
|
{ok, [{hook, user_receive_packet, filter_packet, 25},
|
|
|
|
{hook, roster_in_subscription, filter_subscription, 25},
|
|
|
|
{hook, offline_message_hook, filter_offline_msg, 25}]}.
|
2017-01-16 16:13:48 +01:00
|
|
|
|
2023-08-04 17:53:50 +02:00
|
|
|
stop(_Host) ->
|
|
|
|
ok.
|
2017-01-16 16:13:48 +01:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(_Host, _NewOpts, _OldOpts) ->
|
|
|
|
ok.
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec filter_packet({stanza(), c2s_state()}) -> {stanza(), c2s_state()} |
|
|
|
|
{stop, {drop, c2s_state()}}.
|
2017-09-02 21:54:46 +02:00
|
|
|
filter_packet({#message{from = From} = Msg, State} = Acc) ->
|
2017-01-16 16:13:48 +01:00
|
|
|
LFrom = jid:tolower(From),
|
|
|
|
LBFrom = jid:remove_resource(LFrom),
|
2017-09-02 21:54:46 +02:00
|
|
|
#{pres_a := PresA} = State,
|
|
|
|
case (?SETS):is_element(LFrom, PresA)
|
|
|
|
orelse (?SETS):is_element(LBFrom, PresA)
|
|
|
|
orelse sets_bare_member(LBFrom, PresA) of
|
|
|
|
false ->
|
|
|
|
case check_message(Msg) of
|
|
|
|
allow -> Acc;
|
2017-09-07 12:41:51 +02:00
|
|
|
deny -> {stop, {drop, State}}
|
2017-09-02 21:54:46 +02:00
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
Acc
|
|
|
|
end;
|
|
|
|
filter_packet(Acc) ->
|
|
|
|
Acc.
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec filter_offline_msg({_, message()}) -> {_, message()} | {stop, {drop, message()}}.
|
2017-09-02 21:54:46 +02:00
|
|
|
filter_offline_msg({_Action, #message{} = Msg} = Acc) ->
|
|
|
|
case check_message(Msg) of
|
|
|
|
allow -> Acc;
|
|
|
|
deny -> {stop, {drop, Msg}}
|
|
|
|
end.
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec filter_subscription(boolean(), presence()) -> boolean() | {stop, false}.
|
2018-01-26 13:02:06 +01:00
|
|
|
filter_subscription(Acc, #presence{meta = #{captcha := passed}}) ->
|
|
|
|
Acc;
|
|
|
|
filter_subscription(Acc, #presence{from = From, to = To, lang = Lang,
|
|
|
|
id = SID, type = subscribe} = Pres) ->
|
2017-09-02 21:54:46 +02:00
|
|
|
LServer = To#jid.lserver,
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_block_strangers_opt:drop(LServer) andalso
|
|
|
|
mod_block_strangers_opt:captcha(LServer) andalso
|
2018-01-26 13:02:06 +01:00
|
|
|
need_check(Pres) of
|
|
|
|
true ->
|
|
|
|
case check_subscription(From, To) of
|
|
|
|
false ->
|
|
|
|
BFrom = jid:remove_resource(From),
|
|
|
|
BTo = jid:remove_resource(To),
|
|
|
|
Limiter = jid:tolower(BFrom),
|
|
|
|
case ejabberd_captcha:create_captcha(
|
2018-01-26 20:56:49 +01:00
|
|
|
SID, BTo, BFrom, Lang, Limiter,
|
2018-01-26 13:02:06 +01:00
|
|
|
fun(Res) -> handle_captcha_result(Res, Pres) end) of
|
|
|
|
{ok, ID, Body, CaptchaEls} ->
|
|
|
|
Msg = #message{from = BTo, to = From,
|
|
|
|
id = ID, body = Body,
|
|
|
|
sub_els = CaptchaEls},
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_block_strangers_opt:log(LServer) of
|
2018-01-26 13:02:06 +01:00
|
|
|
true ->
|
|
|
|
?INFO_MSG("Challenge subscription request "
|
2019-09-23 14:17:20 +02:00
|
|
|
"from stranger ~ts to ~ts with "
|
2018-01-26 13:02:06 +01:00
|
|
|
"CAPTCHA",
|
|
|
|
[jid:encode(From), jid:encode(To)]);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
ejabberd_router:route(Msg);
|
|
|
|
{error, limit} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Too many CAPTCHA requests"),
|
2018-01-26 13:02:06 +01:00
|
|
|
Err = xmpp:err_resource_constraint(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(Pres, Err);
|
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Unable to generate a CAPTCHA"),
|
2018-01-26 13:02:06 +01:00
|
|
|
Err = xmpp:err_internal_server_error(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(Pres, Err)
|
|
|
|
end,
|
|
|
|
{stop, false};
|
|
|
|
true ->
|
|
|
|
Acc
|
|
|
|
end;
|
2017-01-16 16:13:48 +01:00
|
|
|
false ->
|
2018-01-26 13:02:06 +01:00
|
|
|
Acc
|
|
|
|
end;
|
|
|
|
filter_subscription(Acc, _) ->
|
|
|
|
Acc.
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec handle_captcha_result(captcha_succeed | captcha_failed, presence()) -> ok.
|
2018-01-26 13:02:06 +01:00
|
|
|
handle_captcha_result(captcha_succeed, Pres) ->
|
|
|
|
Pres1 = xmpp:put_meta(Pres, captcha, passed),
|
|
|
|
ejabberd_router:route(Pres1);
|
|
|
|
handle_captcha_result(captcha_failed, #presence{lang = Lang} = Pres) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("The CAPTCHA verification has failed"),
|
2018-01-26 13:02:06 +01:00
|
|
|
ejabberd_router:route_error(Pres, xmpp:err_not_allowed(Txt, Lang)).
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec check_message(message()) -> allow | deny.
|
2018-01-26 13:02:06 +01:00
|
|
|
check_message(#message{from = From, to = To, lang = Lang} = Msg) ->
|
|
|
|
LServer = To#jid.lserver,
|
|
|
|
case need_check(Msg) of
|
|
|
|
true ->
|
2017-09-08 22:10:01 +02:00
|
|
|
case check_subscription(From, To) of
|
2018-01-26 13:02:06 +01:00
|
|
|
false ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Drop = mod_block_strangers_opt:drop(LServer),
|
|
|
|
Log = mod_block_strangers_opt:log(LServer),
|
2017-05-17 13:47:35 +02:00
|
|
|
if
|
|
|
|
Log ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("~ts message from stranger ~ts to ~ts",
|
2018-01-03 00:38:50 +01:00
|
|
|
[if Drop -> "Rejecting";
|
2017-09-08 22:10:01 +02:00
|
|
|
true -> "Allow"
|
|
|
|
end,
|
|
|
|
jid:encode(From), jid:encode(To)]);
|
2017-05-17 13:47:35 +02:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
Drop ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Messages from strangers are rejected"),
|
2018-01-03 00:38:50 +01:00
|
|
|
Err = xmpp:err_policy_violation(Txt, Lang),
|
2018-01-24 11:49:31 +01:00
|
|
|
Msg1 = maybe_adjust_from(Msg),
|
|
|
|
ejabberd_router:route_error(Msg1, Err),
|
2017-09-02 21:54:46 +02:00
|
|
|
deny;
|
2017-05-17 13:47:35 +02:00
|
|
|
true ->
|
2017-09-02 21:54:46 +02:00
|
|
|
allow
|
2017-05-17 13:47:35 +02:00
|
|
|
end;
|
2018-01-26 13:02:06 +01:00
|
|
|
true ->
|
2017-09-02 21:54:46 +02:00
|
|
|
allow
|
2017-05-17 13:47:35 +02:00
|
|
|
end;
|
2018-01-26 13:02:06 +01:00
|
|
|
false ->
|
2017-09-02 21:54:46 +02:00
|
|
|
allow
|
|
|
|
end.
|
2017-01-16 16:13:48 +01:00
|
|
|
|
2018-01-24 11:49:31 +01:00
|
|
|
-spec maybe_adjust_from(message()) -> message().
|
|
|
|
maybe_adjust_from(#message{type = groupchat, from = From} = Msg) ->
|
|
|
|
Msg#message{from = jid:remove_resource(From)};
|
|
|
|
maybe_adjust_from(#message{} = Msg) ->
|
|
|
|
Msg.
|
|
|
|
|
2018-01-26 13:02:06 +01:00
|
|
|
-spec need_check(presence() | message()) -> boolean().
|
|
|
|
need_check(Pkt) ->
|
|
|
|
To = xmpp:get_to(Pkt),
|
|
|
|
From = xmpp:get_from(Pkt),
|
2018-06-27 14:02:03 +02:00
|
|
|
IsSelf = To#jid.luser == From#jid.luser andalso
|
|
|
|
To#jid.lserver == From#jid.lserver,
|
2018-01-26 13:02:06 +01:00
|
|
|
LServer = To#jid.lserver,
|
|
|
|
IsEmpty = case Pkt of
|
|
|
|
#message{body = [], subject = []} ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end,
|
2020-05-29 12:43:28 +02:00
|
|
|
IsError = (error == xmpp:get_type(Pkt)),
|
2019-06-14 11:33:26 +02:00
|
|
|
AllowLocalUsers = mod_block_strangers_opt:allow_local_users(LServer),
|
|
|
|
Access = mod_block_strangers_opt:access(LServer),
|
2020-05-29 12:43:28 +02:00
|
|
|
not (IsSelf orelse IsEmpty orelse IsError
|
2018-06-27 14:02:03 +02:00
|
|
|
orelse acl:match_rule(LServer, Access, From) == allow
|
2018-02-17 16:53:35 +01:00
|
|
|
orelse ((AllowLocalUsers orelse From#jid.luser == <<"">>)
|
|
|
|
andalso ejabberd_router:is_my_host(From#jid.lserver))).
|
2018-01-26 13:02:06 +01:00
|
|
|
|
|
|
|
-spec check_subscription(jid(), jid()) -> boolean().
|
2017-09-08 22:10:01 +02:00
|
|
|
check_subscription(From, To) ->
|
2018-06-28 09:37:20 +02:00
|
|
|
LocalServer = To#jid.lserver,
|
2017-09-08 22:10:01 +02:00
|
|
|
{RemoteUser, RemoteServer, _} = jid:tolower(From),
|
2018-06-28 09:37:20 +02:00
|
|
|
case mod_roster:is_subscribed(From, To) of
|
2018-01-26 13:02:06 +01:00
|
|
|
false when RemoteUser == <<"">> ->
|
|
|
|
false;
|
|
|
|
false ->
|
|
|
|
%% Check if the contact's server is in the roster
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_block_strangers_opt:allow_transports(LocalServer)
|
2018-06-28 09:37:20 +02:00
|
|
|
andalso mod_roster:is_subscribed(jid:make(RemoteServer), To);
|
2018-01-26 13:02:06 +01:00
|
|
|
true ->
|
|
|
|
true
|
2017-09-08 22:10:01 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec sets_bare_member(ljid(), ?SETS:set()) -> boolean().
|
2017-01-16 16:13:48 +01:00
|
|
|
sets_bare_member({U, S, <<"">>} = LBJID, Set) ->
|
2018-11-29 11:01:00 +01:00
|
|
|
case ?SETS:next(?SETS:iterator_from(LBJID, Set)) of
|
2017-01-16 16:13:48 +01:00
|
|
|
{{U, S, _}, _} -> true;
|
|
|
|
_ -> false
|
|
|
|
end.
|
|
|
|
|
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(access) ->
|
|
|
|
econf:acl();
|
2017-01-16 16:13:48 +01:00
|
|
|
mod_opt_type(drop) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2017-01-16 16:13:48 +01:00
|
|
|
mod_opt_type(log) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
|
|
|
mod_opt_type(captcha) ->
|
|
|
|
econf:bool();
|
2017-06-29 13:55:24 +02:00
|
|
|
mod_opt_type(allow_local_users) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2017-09-08 22:10:01 +02:00
|
|
|
mod_opt_type(allow_transports) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool().
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_options(_) ->
|
2018-02-17 16:53:35 +01:00
|
|
|
[{access, none},
|
|
|
|
{drop, true},
|
2018-01-23 08:54:52 +01:00
|
|
|
{log, false},
|
2018-01-26 13:02:06 +01:00
|
|
|
{captcha, false},
|
2018-01-23 08:54:52 +01:00
|
|
|
{allow_local_users, true},
|
|
|
|
{allow_transports, true}].
|
2020-01-08 10:24:51 +01:00
|
|
|
|
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
2024-01-11 13:23:45 +01:00
|
|
|
?T("This module blocks and logs any messages coming from an "
|
2020-01-08 10:24:51 +01:00
|
|
|
"unknown entity. If a writing entity is not in your roster, "
|
|
|
|
"you can let this module drop and/or log the message. "
|
|
|
|
"By default you'll just not receive message from that entity. "
|
|
|
|
"Enable this module if you want to drop SPAM messages."),
|
|
|
|
opts =>
|
|
|
|
[{access,
|
|
|
|
#{value => ?T("AccessName"),
|
|
|
|
desc =>
|
|
|
|
?T("The option is supposed to be used when 'allow_local_users' "
|
|
|
|
"and 'allow_transports' are not enough. It's an ACL where "
|
|
|
|
"'deny' means the message will be rejected (or a CAPTCHA "
|
|
|
|
"would be generated for a presence, if configured), and "
|
|
|
|
"'allow' means the sender is whitelisted and the stanza "
|
|
|
|
"will pass through. The default value is 'none', which "
|
|
|
|
"means nothing is whitelisted.")}},
|
|
|
|
{drop,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("This option specifies if strangers messages should "
|
|
|
|
"be dropped or not. The default value is 'true'.")}},
|
|
|
|
{log,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("This option specifies if strangers' messages should "
|
|
|
|
"be logged (as info message) in ejabberd.log. "
|
|
|
|
"The default value is 'false'.")}},
|
|
|
|
{allow_local_users,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("This option specifies if strangers from the same "
|
|
|
|
"local host should be accepted or not. "
|
|
|
|
"The default value is 'true'.")}},
|
|
|
|
{allow_transports,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("If set to 'true' and some server's JID is in user's "
|
|
|
|
"roster, then messages from any user of this server "
|
|
|
|
"are accepted even if no subscription present. "
|
|
|
|
"The default value is 'true'.")}},
|
|
|
|
{captcha,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("Whether to generate CAPTCHA or not in response to "
|
|
|
|
"messages from strangers. See also section "
|
|
|
|
"https://docs.ejabberd.im/admin/configuration/#captcha"
|
|
|
|
"[CAPTCHA] of the Configuration Guide. "
|
|
|
|
"The default value is 'false'.")}}]}.
|