2002-12-08 18:23:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_register.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2005-06-20 05:18:13 +02:00
|
|
|
%%% Purpose : Inband registration support
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 8 Dec 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
2002-12-08 18:23:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_register).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 77, '2.4'}).
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/2, stop/1, stream_feature_register/2,
|
|
|
|
unauthenticated_iq_register/4, try_register/5,
|
2016-07-27 17:06:54 +02:00
|
|
|
process_iq/1, send_registration_notifications/3,
|
2015-06-01 14:38:27 +02:00
|
|
|
transform_options/1, transform_module_options/1,
|
2016-07-06 13:58:48 +02:00
|
|
|
mod_opt_type/1, opt_type/1, depends/2]).
|
2002-12-08 18:23:21 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-27 17:06:54 +02:00
|
|
|
-include("xmpp.hrl").
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
one_queue),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_REGISTER, ?MODULE, process_iq, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_REGISTER, ?MODULE, process_iq, IQDisc),
|
|
|
|
ejabberd_hooks:add(c2s_stream_features, Host, ?MODULE,
|
|
|
|
stream_feature_register, 50),
|
2005-07-15 00:28:21 +02:00
|
|
|
ejabberd_hooks:add(c2s_unauthenticated_iq, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
?MODULE, unauthenticated_iq_register, 50),
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, mod_register_ip,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{ram_copies, [node()]}, {local_content, true},
|
2008-04-22 23:51:32 +02:00
|
|
|
{attributes, [key, value]}]),
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:add_table_copy(mod_register_ip, node(),
|
|
|
|
ram_copies),
|
2002-12-11 21:57:45 +01:00
|
|
|
ok.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2005-07-15 00:28:21 +02:00
|
|
|
ejabberd_hooks:delete(c2s_stream_features, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
?MODULE, stream_feature_register, 50),
|
2005-07-15 00:28:21 +02:00
|
|
|
ejabberd_hooks:delete(c2s_unauthenticated_iq, Host,
|
|
|
|
?MODULE, unauthenticated_iq_register, 50),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_REGISTER),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_REGISTER).
|
2005-07-15 00:28:21 +02:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec stream_feature_register([xmpp_element()], binary()) -> [xmpp_element()].
|
2015-12-07 18:30:52 +01:00
|
|
|
stream_feature_register(Acc, Host) ->
|
|
|
|
AF = gen_mod:get_module_opt(Host, ?MODULE, access_from,
|
2016-06-21 13:18:24 +02:00
|
|
|
fun(A) -> A end,
|
2015-12-07 18:30:52 +01:00
|
|
|
all),
|
2016-07-27 17:06:54 +02:00
|
|
|
case (AF /= none) and lists:keymember(sasl_mechanisms, 1, Acc) of
|
2015-05-07 11:41:59 +02:00
|
|
|
true ->
|
2016-07-27 17:06:54 +02:00
|
|
|
[#feature_register{}|Acc];
|
2015-05-07 11:41:59 +02:00
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end.
|
2005-07-15 00:28:21 +02:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec unauthenticated_iq_register(empty | iq(), binary(), iq(),
|
|
|
|
{inet:ip_address(), non_neg_integer()}) ->
|
|
|
|
empty | iq().
|
2013-03-14 10:33:02 +01:00
|
|
|
unauthenticated_iq_register(_Acc, Server,
|
2016-07-27 17:06:54 +02:00
|
|
|
#iq{sub_els = [#register{}]} = IQ, IP) ->
|
2008-04-22 23:51:32 +02:00
|
|
|
Address = case IP of
|
2013-03-14 10:33:02 +01:00
|
|
|
{A, _Port} -> A;
|
|
|
|
_ -> undefined
|
2008-04-22 23:51:32 +02:00
|
|
|
end,
|
2016-07-27 17:06:54 +02:00
|
|
|
ResIQ = process_iq(xmpp:set_from_to(IQ, jid:make(<<>>), jid:make(Server)),
|
|
|
|
Address),
|
|
|
|
xmpp:set_from_to(ResIQ, jid:make(Server), undefined);
|
2008-04-22 23:51:32 +02:00
|
|
|
unauthenticated_iq_register(Acc, _Server, _IQ, _IP) ->
|
2005-07-15 00:28:21 +02:00
|
|
|
Acc.
|
|
|
|
|
2016-07-27 17:06:54 +02:00
|
|
|
process_iq(#iq{from = From} = IQ) ->
|
|
|
|
process_iq(IQ, jid:tolower(From)).
|
2008-04-22 23:51:32 +02:00
|
|
|
|
2016-07-27 17:06:54 +02:00
|
|
|
process_iq(#iq{from = From, to = To} = IQ, Source) ->
|
|
|
|
IsCaptchaEnabled =
|
|
|
|
case gen_mod:get_module_opt(To#jid.lserver, ?MODULE,
|
|
|
|
captcha_protected,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
false) of
|
|
|
|
true -> true;
|
|
|
|
false -> false
|
|
|
|
end,
|
|
|
|
Server = To#jid.lserver,
|
|
|
|
Access = gen_mod:get_module_opt(Server, ?MODULE, access,
|
|
|
|
fun(A) -> A end, all),
|
|
|
|
AllowRemove = allow == acl:match_rule(Server, Access, From),
|
|
|
|
process_iq(IQ, Source, IsCaptchaEnabled, AllowRemove).
|
|
|
|
|
|
|
|
process_iq(#iq{type = set, lang = Lang,
|
|
|
|
sub_els = [#register{remove = true}]} = IQ,
|
|
|
|
_Source, _IsCaptchaEnabled, _AllowRemove = false) ->
|
|
|
|
Txt = <<"Denied by ACL">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang));
|
|
|
|
process_iq(#iq{type = set, lang = Lang, to = To, from = From,
|
|
|
|
sub_els = [#register{remove = true,
|
|
|
|
username = User,
|
|
|
|
password = Password}]} = IQ,
|
|
|
|
_Source, _IsCaptchaEnabled, _AllowRemove = true) ->
|
|
|
|
Server = To#jid.lserver,
|
|
|
|
if is_binary(User) ->
|
|
|
|
case From of
|
|
|
|
#jid{user = User, lserver = Server} ->
|
|
|
|
ejabberd_auth:remove_user(User, Server),
|
|
|
|
xmpp:make_iq_result(IQ);
|
|
|
|
_ ->
|
|
|
|
if is_binary(Password) ->
|
|
|
|
ejabberd_auth:remove_user(User, Server, Password),
|
|
|
|
xmpp:make_iq_result(IQ);
|
|
|
|
true ->
|
|
|
|
Txt = <<"No 'password' found in this query">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang))
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
case From of
|
2016-09-13 11:30:05 +02:00
|
|
|
#jid{luser = LUser, lserver = Server} ->
|
2016-07-27 17:06:54 +02:00
|
|
|
ResIQ = xmpp:make_iq_result(IQ),
|
2016-09-13 11:30:05 +02:00
|
|
|
ejabberd_router:route(From, From, ResIQ),
|
|
|
|
ejabberd_auth:remove_user(LUser, Server),
|
2016-07-27 17:06:54 +02:00
|
|
|
ignore;
|
|
|
|
_ ->
|
|
|
|
Txt = <<"The query is only allowed from local users">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang))
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
process_iq(#iq{type = set, to = To,
|
|
|
|
sub_els = [#register{username = User,
|
|
|
|
password = Password}]} = IQ,
|
|
|
|
Source, IsCaptchaEnabled, _AllowRemove) when is_binary(User),
|
|
|
|
is_binary(Password) ->
|
|
|
|
Server = To#jid.lserver,
|
|
|
|
try_register_or_set_password(
|
|
|
|
User, Server, Password, IQ, Source, not IsCaptchaEnabled);
|
|
|
|
process_iq(#iq{type = set, to = To,
|
|
|
|
lang = Lang, sub_els = [#register{xdata = #xdata{} = X}]} = IQ,
|
|
|
|
Source, true, _AllowRemove) ->
|
|
|
|
Server = To#jid.lserver,
|
|
|
|
case ejabberd_captcha:process_reply(X) of
|
|
|
|
ok ->
|
|
|
|
case process_xdata_submit(X) of
|
|
|
|
{ok, User, Password} ->
|
|
|
|
try_register_or_set_password(
|
|
|
|
User, Server, Password, IQ, Source, true);
|
|
|
|
_ ->
|
|
|
|
Txt = <<"Incorrect data form">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang))
|
|
|
|
end;
|
|
|
|
{error, malformed} ->
|
|
|
|
Txt = <<"Incorrect CAPTCHA submit">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang));
|
|
|
|
_ ->
|
|
|
|
ErrText = <<"The CAPTCHA verification has failed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(ErrText, Lang))
|
|
|
|
end;
|
|
|
|
process_iq(#iq{type = set} = IQ, _Source, _IsCaptchaEnabled, _AllowRemove) ->
|
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request());
|
|
|
|
process_iq(#iq{type = get, from = From, to = To, id = ID, lang = Lang} = IQ,
|
|
|
|
Source, IsCaptchaEnabled, _AllowRemove) ->
|
|
|
|
Server = To#jid.lserver,
|
|
|
|
{IsRegistered, Username} =
|
|
|
|
case From of
|
|
|
|
#jid{user = User, lserver = Server} ->
|
|
|
|
case ejabberd_auth:is_user_exists(User, Server) of
|
|
|
|
true ->
|
|
|
|
{true, User};
|
|
|
|
false ->
|
|
|
|
{false, User}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{false, <<"">>}
|
|
|
|
end,
|
2016-11-26 08:05:22 +01:00
|
|
|
Instr = translate:translate(
|
|
|
|
Lang, <<"Choose a username and password to register "
|
|
|
|
"with this server">>),
|
2016-07-27 17:06:54 +02:00
|
|
|
if IsCaptchaEnabled and not IsRegistered ->
|
|
|
|
TopInstr = translate:translate(
|
|
|
|
Lang, <<"You need a client that supports x:data "
|
|
|
|
"and CAPTCHA to register">>),
|
|
|
|
UField = #xdata_field{type = 'text-single',
|
|
|
|
label = translate:translate(Lang, <<"User">>),
|
|
|
|
var = <<"username">>,
|
|
|
|
required = true},
|
|
|
|
PField = #xdata_field{type = 'text-private',
|
|
|
|
label = translate:translate(Lang, <<"Password">>),
|
|
|
|
var = <<"password">>,
|
|
|
|
required = true},
|
|
|
|
X = #xdata{type = form, instructions = [Instr],
|
|
|
|
fields = [UField, PField]},
|
|
|
|
case ejabberd_captcha:create_captcha_x(ID, To, Lang, Source, X) of
|
2016-07-28 14:10:41 +02:00
|
|
|
{ok, CaptchaEls} ->
|
2016-07-27 17:06:54 +02:00
|
|
|
xmpp:make_iq_result(
|
|
|
|
IQ, #register{instructions = TopInstr,
|
2016-07-28 14:10:41 +02:00
|
|
|
sub_els = CaptchaEls});
|
2016-07-27 17:06:54 +02:00
|
|
|
{error, limit} ->
|
|
|
|
ErrText = <<"Too many CAPTCHA requests">>,
|
|
|
|
xmpp:make_error(
|
|
|
|
IQ, xmpp:err_resource_constraint(ErrText, Lang));
|
|
|
|
_Err ->
|
|
|
|
ErrText = <<"Unable to generate a CAPTCHA">>,
|
|
|
|
xmpp:make_error(
|
|
|
|
IQ, xmpp:err_internal_server_error(ErrText, Lang))
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
xmpp:make_iq_result(
|
|
|
|
IQ,
|
2016-11-26 08:05:22 +01:00
|
|
|
#register{instructions = Instr,
|
2016-07-27 17:06:54 +02:00
|
|
|
username = Username,
|
|
|
|
password = <<"">>,
|
|
|
|
registered = IsRegistered})
|
2010-10-24 07:30:16 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
try_register_or_set_password(User, Server, Password,
|
2016-07-27 17:06:54 +02:00
|
|
|
#iq{from = From, lang = Lang} = IQ,
|
|
|
|
Source, CaptchaSucceed) ->
|
2010-10-24 07:30:16 +02:00
|
|
|
case From of
|
2016-07-27 17:06:54 +02:00
|
|
|
#jid{user = User, lserver = Server} ->
|
|
|
|
try_set_password(User, Server, Password, IQ);
|
|
|
|
_ when CaptchaSucceed ->
|
|
|
|
case check_from(From, Server) of
|
|
|
|
allow ->
|
|
|
|
case try_register(User, Server, Password, Source, Lang) of
|
|
|
|
ok ->
|
|
|
|
xmpp:make_iq_result(IQ);
|
|
|
|
{error, Error} ->
|
|
|
|
xmpp:make_error(IQ, Error)
|
|
|
|
end;
|
|
|
|
deny ->
|
|
|
|
Txt = <<"Denied by ACL">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang))
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed())
|
2002-12-08 18:23:21 +01:00
|
|
|
end.
|
|
|
|
|
2008-08-18 20:21:10 +02:00
|
|
|
%% @doc Try to change password and return IQ response
|
2016-07-27 17:06:54 +02:00
|
|
|
try_set_password(User, Server, Password, #iq{lang = Lang} = IQ) ->
|
2010-10-24 09:17:30 +02:00
|
|
|
case is_strong_password(Server, Password) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2016-07-27 17:06:54 +02:00
|
|
|
case ejabberd_auth:set_password(User, Server, Password) of
|
|
|
|
ok ->
|
|
|
|
xmpp:make_iq_result(IQ);
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, empty_password} ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Empty password">>,
|
2016-07-27 17:06:54 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang));
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, not_allowed} ->
|
2016-04-12 10:27:43 +02:00
|
|
|
Txt = <<"Changing password is not allowed">>,
|
2016-07-27 17:06:54 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, invalid_jid} ->
|
2016-07-27 17:06:54 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_jid_malformed());
|
2016-04-05 12:09:44 +02:00
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("failed to register user ~s@~s: ~p",
|
|
|
|
[User, Server, Err]),
|
2016-07-27 17:06:54 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error())
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
2016-06-14 23:35:47 +02:00
|
|
|
error_preparing_password ->
|
|
|
|
ErrText = <<"The password contains unacceptable characters">>,
|
2016-07-27 17:06:54 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_not_acceptable(ErrText, Lang));
|
2013-03-14 10:33:02 +01:00
|
|
|
false ->
|
|
|
|
ErrText = <<"The password is too weak">>,
|
2016-07-27 17:06:54 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_not_acceptable(ErrText, Lang))
|
2008-08-18 20:21:10 +02:00
|
|
|
end.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2010-11-05 19:00:16 +01:00
|
|
|
try_register(User, Server, Password, SourceRaw, Lang) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:is_nodename(User) of
|
2016-07-27 17:06:54 +02:00
|
|
|
false -> {error, xmpp:err_bad_request(<<"Malformed username">>, Lang)};
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
2015-11-24 16:44:13 +01:00
|
|
|
JID = jid:make(User, Server, <<"">>),
|
2013-03-14 10:33:02 +01:00
|
|
|
Access = gen_mod:get_module_opt(Server, ?MODULE, access,
|
2016-06-21 13:18:24 +02:00
|
|
|
fun(A) -> A end,
|
2013-03-14 10:33:02 +01:00
|
|
|
all),
|
|
|
|
IPAccess = get_ip_access(Server),
|
|
|
|
case {acl:match_rule(Server, Access, JID),
|
|
|
|
check_ip_access(SourceRaw, IPAccess)}
|
|
|
|
of
|
2016-07-27 17:06:54 +02:00
|
|
|
{deny, _} -> {error, xmpp:err_forbidden(<<"Denied by ACL">>, Lang)};
|
|
|
|
{_, deny} -> {error, xmpp:err_forbidden(<<"Denied by ACL">>, Lang)};
|
2013-03-14 10:33:02 +01:00
|
|
|
{allow, allow} ->
|
|
|
|
Source = may_remove_resource(SourceRaw),
|
|
|
|
case check_timeout(Source) of
|
|
|
|
true ->
|
|
|
|
case is_strong_password(Server, Password) of
|
2008-04-22 23:51:32 +02:00
|
|
|
true ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case ejabberd_auth:try_register(User, Server,
|
|
|
|
Password)
|
|
|
|
of
|
|
|
|
{atomic, ok} ->
|
|
|
|
send_welcome_message(JID),
|
|
|
|
send_registration_notifications(
|
|
|
|
?MODULE, JID, Source),
|
|
|
|
ok;
|
|
|
|
Error ->
|
|
|
|
remove_timeout(Source),
|
|
|
|
case Error of
|
2016-04-05 12:09:44 +02:00
|
|
|
{atomic, exists} ->
|
|
|
|
Txt = <<"User already exists">>,
|
2016-07-27 17:06:54 +02:00
|
|
|
{error, xmpp:err_conflict(Txt, Lang)};
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, invalid_jid} ->
|
2016-07-27 17:06:54 +02:00
|
|
|
{error, xmpp:err_jid_malformed()};
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, not_allowed} ->
|
2016-07-27 17:06:54 +02:00
|
|
|
{error, xmpp:err_not_allowed()};
|
2015-10-07 00:06:58 +02:00
|
|
|
{error, too_many_users} ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Too many users registered">>,
|
2016-07-27 17:06:54 +02:00
|
|
|
{error, xmpp:err_resource_constraint(Txt, Lang)};
|
2016-04-05 12:09:44 +02:00
|
|
|
{error, _} ->
|
|
|
|
?ERROR_MSG("failed to register user "
|
|
|
|
"~s@~s: ~p",
|
|
|
|
[User, Server, Error]),
|
2016-07-27 17:06:54 +02:00
|
|
|
{error, xmpp:err_internal_server_error()}
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2008-04-22 23:51:32 +02:00
|
|
|
end;
|
2016-06-14 23:35:47 +02:00
|
|
|
error_preparing_password ->
|
|
|
|
remove_timeout(Source),
|
|
|
|
ErrText = <<"The password contains unacceptable characters">>,
|
2016-07-27 17:06:54 +02:00
|
|
|
{error, xmpp:err_not_acceptable(ErrText, Lang)};
|
2008-04-22 23:51:32 +02:00
|
|
|
false ->
|
2016-05-23 23:27:42 +02:00
|
|
|
remove_timeout(Source),
|
2013-03-14 10:33:02 +01:00
|
|
|
ErrText = <<"The password is too weak">>,
|
2016-07-27 17:06:54 +02:00
|
|
|
{error, xmpp:err_not_acceptable(ErrText, Lang)}
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
ErrText =
|
|
|
|
<<"Users are not allowed to register accounts "
|
|
|
|
"so quickly">>,
|
2016-07-27 17:06:54 +02:00
|
|
|
{error, xmpp:err_resource_constraint(ErrText, Lang)}
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
|
|
|
end
|
2002-12-08 18:23:21 +01:00
|
|
|
end.
|
|
|
|
|
2003-10-14 21:34:17 +02:00
|
|
|
send_welcome_message(JID) ->
|
2005-07-13 05:24:13 +02:00
|
|
|
Host = JID#jid.lserver,
|
2013-03-14 10:33:02 +01:00
|
|
|
case gen_mod:get_module_opt(Host, ?MODULE, welcome_message,
|
2013-08-12 14:25:05 +02:00
|
|
|
fun(Opts) ->
|
|
|
|
S = proplists:get_value(
|
|
|
|
subject, Opts, <<>>),
|
|
|
|
B = proplists:get_value(
|
|
|
|
body, Opts, <<>>),
|
2013-03-14 10:33:02 +01:00
|
|
|
{iolist_to_binary(S),
|
|
|
|
iolist_to_binary(B)}
|
|
|
|
end, {<<"">>, <<"">>})
|
|
|
|
of
|
|
|
|
{<<"">>, <<"">>} -> ok;
|
|
|
|
{Subj, Body} ->
|
2016-07-27 17:06:54 +02:00
|
|
|
ejabberd_router:route(
|
|
|
|
jid:make(Host), JID,
|
|
|
|
#message{subject = xmpp:mk_text(Subj),
|
|
|
|
body = xmpp:mk_text(Body)});
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> ok
|
2003-10-14 21:34:17 +02:00
|
|
|
end.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
send_registration_notifications(Mod, UJID, Source) ->
|
2005-07-13 05:24:13 +02:00
|
|
|
Host = UJID#jid.lserver,
|
2013-03-14 10:33:02 +01:00
|
|
|
case gen_mod:get_module_opt(
|
|
|
|
Host, Mod, registration_watchers,
|
|
|
|
fun(Ss) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
[#jid{} = jid:from_string(iolist_to_binary(S))
|
2013-03-14 10:33:02 +01:00
|
|
|
|| S <- Ss]
|
|
|
|
end, []) of
|
|
|
|
[] -> ok;
|
|
|
|
JIDs when is_list(JIDs) ->
|
|
|
|
Body =
|
2016-11-24 13:06:06 +01:00
|
|
|
(str:format("[~s] The account ~s was registered from "
|
2013-03-14 10:33:02 +01:00
|
|
|
"IP address ~s on node ~w using ~p.",
|
|
|
|
[get_time_string(),
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(UJID),
|
2013-03-14 10:33:02 +01:00
|
|
|
ip_to_string(Source), node(),
|
|
|
|
Mod])),
|
|
|
|
lists:foreach(
|
|
|
|
fun(JID) ->
|
|
|
|
ejabberd_router:route(
|
2016-07-27 17:06:54 +02:00
|
|
|
jid:make(Host), JID,
|
|
|
|
#message{type = chat,
|
|
|
|
body = xmpp:mk_text(Body)})
|
2013-03-14 10:33:02 +01:00
|
|
|
end, JIDs)
|
2003-10-19 18:19:55 +02:00
|
|
|
end.
|
2008-04-22 23:51:32 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
check_from(#jid{user = <<"">>, server = <<"">>},
|
|
|
|
_Server) ->
|
2010-01-31 14:55:10 +01:00
|
|
|
allow;
|
|
|
|
check_from(JID, Server) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Access = gen_mod:get_module_opt(Server, ?MODULE, access_from,
|
2016-06-21 13:18:24 +02:00
|
|
|
fun(A) -> A end,
|
2013-03-14 10:33:02 +01:00
|
|
|
none),
|
2010-01-31 14:55:10 +01:00
|
|
|
acl:match_rule(Server, Access, JID).
|
2008-04-22 23:51:32 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
check_timeout(undefined) -> true;
|
2008-04-22 23:51:32 +02:00
|
|
|
check_timeout(Source) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
Timeout = ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
registration_timeout,
|
|
|
|
fun(TO) when is_integer(TO), TO > 0 ->
|
|
|
|
TO;
|
|
|
|
(infinity) ->
|
|
|
|
infinity;
|
|
|
|
(unlimited) ->
|
|
|
|
infinity
|
|
|
|
end, 600),
|
|
|
|
if is_integer(Timeout) ->
|
2015-12-07 16:08:57 +01:00
|
|
|
Priority = -p1_time_compat:system_time(seconds),
|
2013-03-14 10:33:02 +01:00
|
|
|
CleanPriority = Priority + Timeout,
|
|
|
|
F = fun () ->
|
|
|
|
Treap = case mnesia:read(mod_register_ip, treap, write)
|
|
|
|
of
|
|
|
|
[] -> treap:empty();
|
|
|
|
[{mod_register_ip, treap, T}] -> T
|
|
|
|
end,
|
|
|
|
Treap1 = clean_treap(Treap, CleanPriority),
|
|
|
|
case treap:lookup(Source, Treap1) of
|
|
|
|
error ->
|
|
|
|
Treap2 = treap:insert(Source, Priority, [],
|
|
|
|
Treap1),
|
|
|
|
mnesia:write({mod_register_ip, treap, Treap2}),
|
|
|
|
true;
|
|
|
|
{ok, _, _} ->
|
|
|
|
mnesia:write({mod_register_ip, treap, Treap1}),
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Res} -> Res;
|
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("mod_register: timeout check error: ~p~n",
|
|
|
|
[Reason]),
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
true -> true
|
2008-04-22 23:51:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
clean_treap(Treap, CleanPriority) ->
|
|
|
|
case treap:is_empty(Treap) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> Treap;
|
|
|
|
false ->
|
|
|
|
{_Key, Priority, _Value} = treap:get_root(Treap),
|
|
|
|
if Priority > CleanPriority ->
|
|
|
|
clean_treap(treap:delete_root(Treap), CleanPriority);
|
|
|
|
true -> Treap
|
|
|
|
end
|
2008-04-22 23:51:32 +02:00
|
|
|
end.
|
2008-04-23 15:14:08 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
remove_timeout(undefined) -> true;
|
2008-04-23 15:14:08 +02:00
|
|
|
remove_timeout(Source) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
Timeout = ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
registration_timeout,
|
|
|
|
fun(TO) when is_integer(TO), TO > 0 ->
|
|
|
|
TO;
|
|
|
|
(infinity) ->
|
|
|
|
infinity;
|
|
|
|
(unlimited) ->
|
|
|
|
infinity
|
|
|
|
end, 600),
|
|
|
|
if is_integer(Timeout) ->
|
|
|
|
F = fun () ->
|
|
|
|
Treap = case mnesia:read(mod_register_ip, treap, write)
|
|
|
|
of
|
|
|
|
[] -> treap:empty();
|
|
|
|
[{mod_register_ip, treap, T}] -> T
|
|
|
|
end,
|
|
|
|
Treap1 = treap:delete(Source, Treap),
|
|
|
|
mnesia:write({mod_register_ip, treap, Treap1}),
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, ok} -> ok;
|
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("mod_register: timeout remove error: "
|
|
|
|
"~p~n",
|
|
|
|
[Reason]),
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
true -> ok
|
2008-04-23 15:14:08 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
ip_to_string(Source) when is_tuple(Source) ->
|
|
|
|
jlib:ip_to_list(Source);
|
|
|
|
ip_to_string(undefined) -> <<"undefined">>;
|
|
|
|
ip_to_string(_) -> <<"unknown">>.
|
2009-11-17 12:14:31 +01:00
|
|
|
|
|
|
|
get_time_string() -> write_time(erlang:localtime()).
|
|
|
|
%% Function copied from ejabberd_logger_h.erl and customized
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
write_time({{Y, Mo, D}, {H, Mi, S}}) ->
|
2009-11-17 12:14:31 +01:00
|
|
|
io_lib:format("~w-~.2.0w-~.2.0w ~.2.0w:~.2.0w:~.2.0w",
|
|
|
|
[Y, Mo, D, H, Mi, S]).
|
2010-10-24 07:30:16 +02:00
|
|
|
|
2016-07-27 17:06:54 +02:00
|
|
|
process_xdata_submit(X) ->
|
|
|
|
case {xmpp_util:get_xdata_values(<<"username">>, X),
|
|
|
|
xmpp_util:get_xdata_values(<<"password">>, X)} of
|
|
|
|
{[User], [Pass]} -> {ok, User, Pass};
|
|
|
|
_ -> error
|
2010-10-24 07:30:16 +02:00
|
|
|
end.
|
2010-10-24 09:17:30 +02:00
|
|
|
|
|
|
|
is_strong_password(Server, Password) ->
|
2016-06-14 23:35:47 +02:00
|
|
|
case jid:resourceprep(Password) of
|
|
|
|
PP when is_binary(PP) ->
|
|
|
|
is_strong_password2(Server, Password);
|
|
|
|
error ->
|
|
|
|
error_preparing_password
|
|
|
|
end.
|
|
|
|
|
|
|
|
is_strong_password2(Server, Password) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
case gen_mod:get_module_opt(LServer, ?MODULE, password_strength,
|
|
|
|
fun(N) when is_number(N), N>=0 -> N end,
|
|
|
|
0) of
|
|
|
|
0 ->
|
|
|
|
true;
|
|
|
|
Entropy ->
|
|
|
|
ejabberd_auth:entropy(Password) >= Entropy
|
2010-10-24 09:17:30 +02:00
|
|
|
end.
|
2010-11-05 19:00:16 +01:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
transform_options(Opts) ->
|
|
|
|
Opts1 = transform_ip_access(Opts),
|
|
|
|
transform_module_options(Opts1).
|
|
|
|
|
|
|
|
transform_ip_access(Opts) ->
|
|
|
|
try
|
|
|
|
{value, {modules, ModOpts}, Opts1} = lists:keytake(modules, 1, Opts),
|
|
|
|
{value, {?MODULE, RegOpts}, ModOpts1} = lists:keytake(?MODULE, 1, ModOpts),
|
|
|
|
{value, {ip_access, L}, RegOpts1} = lists:keytake(ip_access, 1, RegOpts),
|
|
|
|
true = is_list(L),
|
|
|
|
?WARNING_MSG("Old 'ip_access' format detected. "
|
|
|
|
"The old format is still supported "
|
|
|
|
"but it is better to fix your config: "
|
|
|
|
"use access rules instead.", []),
|
|
|
|
ACLs = lists:flatmap(
|
|
|
|
fun({Action, S}) ->
|
|
|
|
ACLName = jlib:binary_to_atom(
|
|
|
|
iolist_to_binary(
|
|
|
|
["ip_", S])),
|
|
|
|
[{Action, ACLName},
|
|
|
|
{acl, ACLName, {ip, S}}]
|
|
|
|
end, L),
|
|
|
|
Access = {access, mod_register_networks,
|
|
|
|
[{Action, ACLName} || {Action, ACLName} <- ACLs]},
|
|
|
|
[ACL || {acl, _, _} = ACL <- ACLs] ++
|
|
|
|
[Access,
|
|
|
|
{modules,
|
|
|
|
[{mod_register,
|
|
|
|
[{ip_access, mod_register_networks}|RegOpts1]}
|
|
|
|
| ModOpts1]}|Opts1]
|
|
|
|
catch error:{badmatch, false} ->
|
|
|
|
Opts
|
|
|
|
end.
|
|
|
|
|
|
|
|
transform_module_options(Opts) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({welcome_message, {Subj, Body}}) ->
|
|
|
|
?WARNING_MSG("Old 'welcome_message' format detected. "
|
|
|
|
"The old format is still supported "
|
|
|
|
"but it is better to fix your config: "
|
|
|
|
"change it to {welcome_message, "
|
|
|
|
"[{subject, Subject}, {body, Body}]}",
|
|
|
|
[]),
|
|
|
|
[{welcome_message, [{subject, Subj}, {body, Body}]}];
|
|
|
|
(Opt) ->
|
|
|
|
[Opt]
|
|
|
|
end, Opts).
|
|
|
|
|
2010-11-05 19:00:16 +01:00
|
|
|
%%%
|
|
|
|
%%% ip_access management
|
|
|
|
%%%
|
|
|
|
|
|
|
|
may_remove_resource({_, _, _} = From) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:remove_resource(From);
|
2013-03-14 10:33:02 +01:00
|
|
|
may_remove_resource(From) -> From.
|
2010-11-05 19:00:16 +01:00
|
|
|
|
|
|
|
get_ip_access(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_mod:get_module_opt(Host, ?MODULE, ip_access,
|
2013-08-12 14:25:05 +02:00
|
|
|
fun(A) when is_atom(A) -> A end,
|
|
|
|
all).
|
2010-11-05 19:00:16 +01:00
|
|
|
|
|
|
|
check_ip_access({User, Server, Resource}, IPAccess) ->
|
|
|
|
case ejabberd_sm:get_user_ip(User, Server, Resource) of
|
2013-08-12 14:25:05 +02:00
|
|
|
{IPAddress, _PortNumber} ->
|
|
|
|
check_ip_access(IPAddress, IPAccess);
|
|
|
|
_ ->
|
|
|
|
deny
|
2010-11-05 19:00:16 +01:00
|
|
|
end;
|
2013-12-10 08:40:43 +01:00
|
|
|
check_ip_access(undefined, _IPAccess) ->
|
|
|
|
deny;
|
2013-08-12 14:25:05 +02:00
|
|
|
check_ip_access(IPAddress, IPAccess) ->
|
|
|
|
acl:match_rule(global, IPAccess, IPAddress).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-11-15 18:14:21 +01:00
|
|
|
mod_opt_type(access) -> fun acl:access_rules_validator/1;
|
|
|
|
mod_opt_type(access_from) -> fun acl:access_rules_validator/1;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(captcha_protected) ->
|
|
|
|
fun (B) when is_boolean(B) -> B end;
|
2016-11-15 18:14:21 +01:00
|
|
|
mod_opt_type(ip_access) -> fun acl:access_rules_validator/1;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
|
|
|
|
mod_opt_type(password_strength) ->
|
|
|
|
fun (N) when is_number(N), N >= 0 -> N end;
|
|
|
|
mod_opt_type(registration_watchers) ->
|
|
|
|
fun (Ss) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
[#jid{} = jid:from_string(iolist_to_binary(S))
|
2015-06-01 14:38:27 +02:00
|
|
|
|| S <- Ss]
|
|
|
|
end;
|
|
|
|
mod_opt_type(welcome_message) ->
|
|
|
|
fun (Opts) ->
|
|
|
|
S = proplists:get_value(subject, Opts, <<>>),
|
|
|
|
B = proplists:get_value(body, Opts, <<>>),
|
|
|
|
{iolist_to_binary(S), iolist_to_binary(B)}
|
|
|
|
end;
|
|
|
|
mod_opt_type(_) ->
|
|
|
|
[access, access_from, captcha_protected, ip_access,
|
|
|
|
iqdisc, password_strength, registration_watchers,
|
|
|
|
welcome_message].
|
|
|
|
|
|
|
|
opt_type(registration_timeout) ->
|
|
|
|
fun (TO) when is_integer(TO), TO > 0 -> TO;
|
|
|
|
(infinity) -> infinity;
|
|
|
|
(unlimited) -> infinity
|
|
|
|
end;
|
|
|
|
opt_type(_) -> [registration_timeout].
|