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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 13:58:05 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2002-12-08 18:23:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_register).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
2010-05-10 08:00:30 +02:00
|
|
|
stream_feature_register/2,
|
2008-04-22 23:51:32 +02:00
|
|
|
unauthenticated_iq_register/4,
|
2009-11-17 12:14:00 +01:00
|
|
|
try_register/5,
|
2005-06-20 05:18:13 +02:00
|
|
|
process_iq/3]).
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2008-09-23 15:11:05 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2002-12-08 18:23:21 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
2003-01-24 21:18:33 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, HostB, ?NS_INBAND_REGISTER,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_iq, IQDisc),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_INBAND_REGISTER,
|
2003-09-19 20:22:44 +02:00
|
|
|
?MODULE, process_iq, IQDisc),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(c2s_stream_features, HostB,
|
2005-07-15 00:28:21 +02:00
|
|
|
?MODULE, stream_feature_register, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(c2s_unauthenticated_iq, HostB,
|
2005-07-15 00:28:21 +02:00
|
|
|
?MODULE, unauthenticated_iq_register, 50),
|
2008-04-22 23:51:32 +02:00
|
|
|
mnesia:create_table(mod_register_ip,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{local_content, true},
|
|
|
|
{attributes, [key, value]}]),
|
2008-04-23 15:14:08 +02: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) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(c2s_stream_features, HostB,
|
2005-07-15 00:28:21 +02:00
|
|
|
?MODULE, stream_feature_register, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(c2s_unauthenticated_iq, HostB,
|
2005-07-15 00:28:21 +02:00
|
|
|
?MODULE, unauthenticated_iq_register, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, HostB, ?NS_INBAND_REGISTER),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB, ?NS_INBAND_REGISTER).
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2005-07-15 00:28:21 +02:00
|
|
|
|
2010-05-10 08:00:30 +02:00
|
|
|
stream_feature_register(Acc, _Host) ->
|
2008-09-23 15:11:05 +02:00
|
|
|
[#xmlel{ns = ?NS_INBAND_REGISTER_FEAT, name = 'register'} | Acc].
|
2005-07-15 00:28:21 +02:00
|
|
|
|
2008-04-22 23:51:32 +02:00
|
|
|
unauthenticated_iq_register(_Acc,
|
2008-09-23 15:11:05 +02:00
|
|
|
Server, #iq{ns = ?NS_INBAND_REGISTER} = IQ_Rec, IP) ->
|
2008-04-22 23:51:32 +02:00
|
|
|
Address = case IP of
|
|
|
|
{A, _Port} -> A;
|
|
|
|
_ -> undefined
|
|
|
|
end,
|
2009-06-01 18:26:00 +02:00
|
|
|
BareJID = exmpp_jid:make(Server),
|
|
|
|
ResIQ = process_iq(exmpp_jid:make(),
|
2009-01-03 16:15:38 +01:00
|
|
|
BareJID,
|
2008-09-23 15:11:05 +02:00
|
|
|
IQ_Rec,
|
2008-04-22 23:51:32 +02:00
|
|
|
Address),
|
2010-06-18 16:45:31 +02:00
|
|
|
exmpp_iq:iq_to_xmlel(ResIQ, BareJID, exmpp_jid:make());
|
2005-07-15 00:28:21 +02:00
|
|
|
|
2008-04-22 23:51:32 +02:00
|
|
|
unauthenticated_iq_register(Acc, _Server, _IQ, _IP) ->
|
2005-07-15 00:28:21 +02:00
|
|
|
Acc.
|
|
|
|
|
2008-04-22 23:51:32 +02:00
|
|
|
process_iq(From, To, IQ) ->
|
2009-06-01 18:40:51 +02:00
|
|
|
process_iq(From, To, IQ, {exmpp_jid:prep_node_as_list(From),
|
2009-06-01 18:38:28 +02:00
|
|
|
exmpp_jid:prep_domain_as_list(From),
|
2009-06-01 18:43:15 +02:00
|
|
|
exmpp_jid:prep_resource_as_list(From)}).
|
2008-04-22 23:51:32 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
process_iq(From, To,
|
2010-11-03 09:55:02 +01:00
|
|
|
#iq{type = Type, lang = Lang, payload = SubEl, id = ID} = IQ_Rec,
|
2008-04-22 23:51:32 +02:00
|
|
|
Source) ->
|
2010-11-03 09:55:02 +01:00
|
|
|
IsCaptchaEnabled = case gen_mod:get_module_opt(
|
2010-11-03 12:43:23 +01:00
|
|
|
exmpp_jid:domain_as_list(To), ?MODULE, captcha_protected, false) of
|
2010-11-03 09:55:02 +01:00
|
|
|
true ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end,
|
2002-12-08 18:23:21 +01:00
|
|
|
case Type of
|
|
|
|
set ->
|
2008-09-23 15:11:05 +02:00
|
|
|
UTag = exmpp_xml:get_element(SubEl, 'username'),
|
|
|
|
PTag = exmpp_xml:get_element(SubEl, 'password'),
|
|
|
|
RTag = exmpp_xml:get_element(SubEl, 'remove'),
|
2009-06-01 18:38:28 +02:00
|
|
|
Server = exmpp_jid:prep_domain_as_list(To),
|
2002-12-08 18:23:21 +01:00
|
|
|
if
|
2008-09-23 15:11:05 +02:00
|
|
|
(UTag /= undefined) and (RTag /= undefined) ->
|
|
|
|
User = exmpp_xml:get_cdata_as_list(UTag),
|
2009-06-01 18:38:28 +02:00
|
|
|
case {exmpp_jid:node_as_list(From), exmpp_jid:prep_domain_as_list(From)} of
|
2009-01-03 16:15:38 +01:00
|
|
|
{User, Server} ->
|
2005-04-17 20:08:34 +02:00
|
|
|
ejabberd_auth:remove_user(User, Server),
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, SubEl);
|
2003-02-21 20:52:15 +01:00
|
|
|
_ ->
|
|
|
|
if
|
2008-09-23 15:11:05 +02:00
|
|
|
PTag /= undefined ->
|
|
|
|
Password = exmpp_xml:get_cdata_as_list(PTag),
|
2003-02-21 20:52:15 +01:00
|
|
|
case ejabberd_auth:remove_user(User,
|
2005-04-17 20:08:34 +02:00
|
|
|
Server,
|
2003-02-21 20:52:15 +01:00
|
|
|
Password) of
|
|
|
|
ok ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, SubEl);
|
2007-07-26 12:13:04 +02:00
|
|
|
%% TODO FIXME: This piece of
|
|
|
|
%% code does not work since
|
|
|
|
%% the code have been changed
|
|
|
|
%% to allow several auth
|
|
|
|
%% modules. lists:foreach can
|
|
|
|
%% only return ok:
|
2003-02-21 20:52:15 +01:00
|
|
|
not_allowed ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec,
|
|
|
|
'not-allowed');
|
2003-02-21 20:52:15 +01:00
|
|
|
not_exists ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec,
|
|
|
|
'item-not-found');
|
2003-02-21 20:52:15 +01:00
|
|
|
_ ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec,
|
|
|
|
'internal-server-error')
|
2003-02-21 20:52:15 +01:00
|
|
|
end;
|
|
|
|
true ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'bad-request')
|
2003-02-21 20:52:15 +01:00
|
|
|
end
|
|
|
|
end;
|
2008-09-23 15:11:05 +02:00
|
|
|
(UTag == undefined) and (RTag /= undefined) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
case {exmpp_jid:node_as_list(From),
|
2009-06-01 18:38:28 +02:00
|
|
|
exmpp_jid:prep_domain_as_list(From),
|
2009-01-03 16:15:38 +01:00
|
|
|
exmpp_jid:resource_as_list(From)}of
|
|
|
|
{User, Server, Resource} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
ResIQ = exmpp_iq:result(IQ_Rec, SubEl),
|
2006-07-28 18:18:50 +02:00
|
|
|
ejabberd_router:route(
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(User,
|
2009-01-03 16:15:38 +01:00
|
|
|
Server,
|
|
|
|
Resource),
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(User,
|
2009-01-03 16:15:38 +01:00
|
|
|
Server,
|
|
|
|
Resource),
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:iq_to_xmlel(ResIQ)),
|
2005-04-17 20:08:34 +02:00
|
|
|
ejabberd_auth:remove_user(User, Server),
|
2006-07-28 18:18:50 +02:00
|
|
|
ignore;
|
2003-10-07 22:31:44 +02:00
|
|
|
_ ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed')
|
2003-10-07 22:31:44 +02:00
|
|
|
end;
|
2008-09-23 15:11:05 +02:00
|
|
|
(UTag /= undefined) and (PTag /= undefined) ->
|
|
|
|
User = exmpp_xml:get_cdata_as_list(UTag),
|
|
|
|
Password = exmpp_xml:get_cdata_as_list(PTag),
|
2010-11-03 09:55:02 +01:00
|
|
|
try_register_or_set_password(
|
|
|
|
User, Server, Password, From,
|
|
|
|
IQ_Rec, SubEl, Source, Lang, not IsCaptchaEnabled);
|
|
|
|
IsCaptchaEnabled ->
|
|
|
|
case ejabberd_captcha:process_reply(SubEl) of
|
|
|
|
ok ->
|
|
|
|
case process_xdata_submit(SubEl) of
|
|
|
|
{ok, User, Password} ->
|
|
|
|
try_register_or_set_password(
|
|
|
|
User, Server, Password, From,
|
|
|
|
IQ_Rec, SubEl, Source, Lang, true);
|
|
|
|
_ ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'bad-request')
|
|
|
|
end;
|
|
|
|
{error, malformed} ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'bad-request');
|
2002-12-08 18:23:21 +01:00
|
|
|
_ ->
|
2010-11-08 12:01:14 +01:00
|
|
|
ErrText = translate:translate(Lang, "Captcha test failed"),
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed', ErrText)
|
2010-11-03 09:55:02 +01:00
|
|
|
end;
|
2003-07-20 22:35:35 +02:00
|
|
|
true ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'bad-request')
|
2002-12-08 18:23:21 +01:00
|
|
|
end;
|
|
|
|
get ->
|
2010-11-03 09:55:02 +01:00
|
|
|
{IsRegistered, UsernameSubels, QuerySubels} =
|
2010-07-28 19:48:06 +02:00
|
|
|
case {exmpp_jid:node_as_list(From), exmpp_jid:prep_domain_as_list(From)} of
|
|
|
|
{User, Server} when is_list(User) and is_list(Server) ->
|
|
|
|
case ejabberd_auth:is_user_exists(User,Server) of
|
|
|
|
true ->
|
2010-11-03 09:55:02 +01:00
|
|
|
{true, [#xmlcdata{cdata = list_to_binary(User)}],
|
2010-07-28 19:48:06 +02:00
|
|
|
[#xmlel{ns = ?NS_INBAND_REGISTER, name = 'registered'}]};
|
|
|
|
false ->
|
2010-11-03 09:55:02 +01:00
|
|
|
{false, [#xmlcdata{cdata = list_to_binary(User)}], []}
|
2010-07-28 19:48:06 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2010-11-03 09:55:02 +01:00
|
|
|
{false, [], []}
|
2010-07-28 19:48:06 +02:00
|
|
|
end,
|
2010-11-03 09:55:02 +01:00
|
|
|
if IsCaptchaEnabled and not IsRegistered ->
|
2010-10-25 19:47:14 +02:00
|
|
|
TopInstrEl =
|
|
|
|
#xmlel{ns = ?NS_INBAND_REGISTER, name = 'instructions',
|
|
|
|
children =
|
|
|
|
[#xmlcdata{cdata =
|
|
|
|
list_to_binary(
|
|
|
|
translate:translate(Lang,
|
|
|
|
"You need an x:data capable client "
|
|
|
|
"with CAPTCHA support to register"))}]},
|
2010-11-03 09:55:02 +01:00
|
|
|
InstrEl =
|
|
|
|
#xmlel{ns = ?NS_INBAND_REGISTER, name = 'instructions',
|
|
|
|
children =
|
|
|
|
[#xmlcdata{cdata =
|
|
|
|
list_to_binary(
|
|
|
|
translate:translate(Lang,
|
|
|
|
"Choose a username and password "
|
|
|
|
"to register with this server"))}]},
|
|
|
|
UField = #xmlel{ns = ?NS_DATA_FORMS, name = 'field', attrs =
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"var">>, <<"username">>),
|
|
|
|
?XMLATTR(<<"type">>, <<"text-single">>),
|
|
|
|
?XMLATTR(<<"label">>, translate:translate(Lang, "User"))],
|
2010-11-03 09:55:02 +01:00
|
|
|
children =
|
|
|
|
[#xmlel{ns = ?NS_DATA_FORMS, name = 'required'}]},
|
|
|
|
PField =
|
|
|
|
#xmlel{ns = ?NS_DATA_FORMS, name = 'field', attrs =
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"var">>, <<"password">>),
|
|
|
|
?XMLATTR(<<"type">>, <<"text-private">>),
|
|
|
|
?XMLATTR(<<"label">>, translate:translate(Lang, "Password"))],
|
2010-11-03 09:55:02 +01:00
|
|
|
children = [
|
|
|
|
#xmlel{ns = ?NS_DATA_FORMS, name = 'required'}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
case ejabberd_captcha:create_captcha_x(
|
|
|
|
ID, To, Lang, [InstrEl, UField, PField]) of
|
|
|
|
{ok, CaptchaEls} ->
|
2010-10-25 19:47:14 +02:00
|
|
|
Result = #xmlel{ns = ?NS_INBAND_REGISTER, name = 'query', children = [TopInstrEl | CaptchaEls]},
|
2010-11-03 09:55:02 +01:00
|
|
|
exmpp_iq:result(IQ_Rec, Result);
|
|
|
|
error ->
|
2010-11-08 12:01:14 +01:00
|
|
|
ErrText = translate:translate(Lang, "Unable to generate a captcha"),
|
|
|
|
exmpp_iq:error(IQ_Rec, 'internal-server-error', ErrText)
|
2010-11-03 09:55:02 +01:00
|
|
|
end;
|
|
|
|
true ->
|
2008-09-23 15:11:05 +02:00
|
|
|
Result = #xmlel{ns = ?NS_INBAND_REGISTER, name = 'query', children =
|
2010-07-28 19:48:06 +02:00
|
|
|
[#xmlel{ns = ?NS_INBAND_REGISTER, name = 'instructions', children =
|
|
|
|
[#xmlcdata{cdata = list_to_binary(
|
|
|
|
translate:translate(Lang,
|
|
|
|
"Choose a username and password "
|
|
|
|
"to register with this server"))}]},
|
|
|
|
#xmlel{ns = ?NS_INBAND_REGISTER, name = 'username', children = UsernameSubels},
|
|
|
|
#xmlel{ns = ?NS_INBAND_REGISTER, name = 'password'}
|
|
|
|
| QuerySubels]},
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, Result)
|
2010-11-03 09:55:02 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
try_register_or_set_password(User, Server, Password, From, IQ_Rec,
|
|
|
|
SubEl, Source, Lang, CaptchaSucceed) ->
|
|
|
|
case {exmpp_jid:node_as_list(From), exmpp_jid:prep_domain_as_list(From)} of
|
|
|
|
{User, Server} ->
|
2010-10-24 09:17:30 +02:00
|
|
|
try_set_password(User, Server, Password, IQ_Rec, SubEl, Lang);
|
2010-11-03 09:55:02 +01:00
|
|
|
_ when CaptchaSucceed ->
|
|
|
|
case check_from(From, Server) of
|
|
|
|
allow ->
|
|
|
|
case try_register(User, Server, Password,
|
|
|
|
Source, Lang) of
|
|
|
|
ok ->
|
|
|
|
exmpp_iq:result(IQ_Rec, SubEl);
|
|
|
|
{error, Error} ->
|
|
|
|
exmpp_iq:error(IQ_Rec, Error)
|
|
|
|
end;
|
|
|
|
deny ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'forbidden')
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed')
|
2002-12-08 18:23:21 +01:00
|
|
|
end.
|
|
|
|
|
2008-09-16 16:39:57 +02:00
|
|
|
%% @doc Try to change password and return IQ response
|
2010-10-24 09:17:30 +02:00
|
|
|
try_set_password(User, Server, Password, IQ_Rec, SubEl, Lang) ->
|
|
|
|
case is_strong_password(Server, Password) of
|
|
|
|
true ->
|
|
|
|
try_set_password_strong(User, Server, Password, IQ_Rec, SubEl, Lang);
|
|
|
|
false ->
|
2010-11-08 12:01:14 +01:00
|
|
|
ErrText = translate:translate(Lang, "The password is too weak"),
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-acceptable', ErrText)
|
2010-10-24 09:17:30 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
try_set_password_strong(User, Server, Password, IQ_Rec, SubEl, _Lang) ->
|
2008-09-16 16:39:57 +02:00
|
|
|
case ejabberd_auth:set_password(User, Server, Password) of
|
|
|
|
ok ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, SubEl);
|
2008-09-16 16:39:57 +02:00
|
|
|
{error, empty_password} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'bad-request');
|
2008-09-16 16:39:57 +02:00
|
|
|
{error, not_allowed} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed');
|
2008-09-16 16:39:57 +02:00
|
|
|
{error, invalid_jid} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'item-not-found');
|
2008-09-16 16:39:57 +02:00
|
|
|
_ ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'internal-server-error')
|
2008-09-16 16:39:57 +02:00
|
|
|
end.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2010-10-24 09:17:30 +02:00
|
|
|
try_register_strong(User, Server, Password, Source, _Lang, JID) ->
|
2008-04-22 23:51:32 +02:00
|
|
|
case ejabberd_auth:try_register(User, Server, Password) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
send_welcome_message(JID),
|
2009-11-17 12:14:00 +01:00
|
|
|
send_registration_notifications(JID, Source),
|
2008-04-22 23:51:32 +02:00
|
|
|
ok;
|
2008-04-23 15:14:08 +02:00
|
|
|
Error ->
|
|
|
|
remove_timeout(Source),
|
|
|
|
case Error of
|
|
|
|
{atomic, exists} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
{error, 'conflict'};
|
2008-04-23 15:14:08 +02:00
|
|
|
{error, invalid_jid} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
{error, 'jid-malformed'};
|
2008-04-23 15:14:08 +02:00
|
|
|
{error, not_allowed} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
{error, 'not-allowed'};
|
2008-04-23 15:14:08 +02:00
|
|
|
{error, _Reason} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
{error, 'internal-server-error'}
|
2008-04-23 15:14:08 +02:00
|
|
|
end
|
2010-10-24 09:17:30 +02:00
|
|
|
end.
|
|
|
|
|
2010-11-05 19:00:16 +01:00
|
|
|
try_register(User, Server, Password, SourceRaw, Lang) ->
|
2010-10-24 09:17:30 +02:00
|
|
|
case exmpp_stringprep:is_node(User) of
|
|
|
|
false ->
|
|
|
|
{error, 'bad-request'};
|
|
|
|
_ ->
|
|
|
|
JID = exmpp_jid:make(User, Server),
|
|
|
|
Access = gen_mod:get_module_opt(Server, ?MODULE, access, all),
|
2010-11-05 19:00:16 +01:00
|
|
|
IPAccess = get_ip_access(Server),
|
|
|
|
case {acl:match_rule(Server, Access, JID),
|
|
|
|
check_ip_access(SourceRaw, IPAccess)} of
|
|
|
|
{deny, _} ->
|
2010-10-24 09:17:30 +02:00
|
|
|
{error, 'forbidden'};
|
2010-11-05 19:00:16 +01:00
|
|
|
{_, deny} ->
|
|
|
|
{error, 'forbidden'};
|
|
|
|
{allow, allow} ->
|
|
|
|
Source = may_remove_resource(SourceRaw),
|
2010-10-24 09:17:30 +02:00
|
|
|
case check_timeout(Source) of
|
|
|
|
true ->
|
|
|
|
case is_strong_password(Server, Password) of
|
|
|
|
true ->
|
|
|
|
try_register_strong(User, Server, Password, Source, Lang, JID);
|
|
|
|
false ->
|
2010-11-08 12:01:14 +01:00
|
|
|
ErrText = "The password is too weak",
|
|
|
|
{error, exmpp_stanza:error(?NS_JABBER_CLIENT, 'not-acceptable', {Lang, ErrText})}
|
2008-04-22 23:51:32 +02:00
|
|
|
end;
|
|
|
|
false ->
|
2008-09-16 16:39:57 +02:00
|
|
|
ErrText = "Users are not allowed to register "
|
2009-04-08 21:04:13 +02:00
|
|
|
"accounts so quickly",
|
2008-09-23 15:11:05 +02:00
|
|
|
{error, exmpp_stanza:error(?NS_JABBER_CLIENT, 'resource-constraint', {Lang, ErrText})}
|
2003-10-11 19:39:36 +02:00
|
|
|
end
|
2003-01-11 21:25:11 +01:00
|
|
|
end
|
2002-12-08 18:23:21 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2003-10-14 21:34:17 +02:00
|
|
|
send_welcome_message(JID) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
Host = exmpp_jid:prep_domain_as_list(JID),
|
2007-09-04 09:55:41 +02:00
|
|
|
case gen_mod:get_module_opt(Host, ?MODULE, welcome_message, {"", ""}) of
|
2003-10-14 21:34:17 +02:00
|
|
|
{"", ""} ->
|
|
|
|
ok;
|
|
|
|
{Subj, Body} ->
|
|
|
|
ejabberd_router:route(
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(Host),
|
2003-10-14 21:34:17 +02:00
|
|
|
JID,
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_message:normal(Subj, Body));
|
2003-10-14 21:34:17 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2009-11-17 12:14:00 +01:00
|
|
|
send_registration_notifications(UJID, Source) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
Host = exmpp_jid:prep_domain_as_list(UJID),
|
2007-09-04 09:55:41 +02:00
|
|
|
case gen_mod:get_module_opt(Host, ?MODULE, registration_watchers, []) of
|
2003-10-19 18:19:55 +02:00
|
|
|
[] -> ok;
|
|
|
|
JIDs when is_list(JIDs) ->
|
|
|
|
Body = lists:flatten(
|
|
|
|
io_lib:format(
|
2009-11-17 12:14:00 +01:00
|
|
|
"[~s] The account ~s was registered from IP address ~s "
|
|
|
|
"on node ~w using ~p.",
|
|
|
|
[get_time_string(), exmpp_jid:to_list(UJID),
|
|
|
|
ip_to_string(Source), node(), ?MODULE])),
|
2003-10-19 18:19:55 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(S) ->
|
2008-09-23 15:11:05 +02:00
|
|
|
try
|
2009-06-01 18:35:55 +02:00
|
|
|
JID = exmpp_jid:parse(S),
|
2008-09-23 15:11:05 +02:00
|
|
|
ejabberd_router:route(
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(Host),
|
2008-09-23 15:11:05 +02:00
|
|
|
JID,
|
|
|
|
exmpp_message:chat(Body))
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
2003-10-19 18:19:55 +02:00
|
|
|
end
|
|
|
|
end, JIDs);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2008-04-22 23:51:32 +02:00
|
|
|
|
2010-02-03 13:20:04 +01:00
|
|
|
check_from(JID, Server) ->
|
2010-04-19 16:08:01 +02:00
|
|
|
case {exmpp_jid:node(JID), exmpp_jid:prep_domain(JID)} of
|
|
|
|
{undefined, undefined} ->
|
2010-02-03 13:20:04 +01:00
|
|
|
allow;
|
|
|
|
_ ->
|
|
|
|
Access = gen_mod:get_module_opt(Server, ?MODULE, access_from, none),
|
|
|
|
acl:match_rule(Server, Access, JID)
|
|
|
|
end.
|
2008-04-22 23:51:32 +02:00
|
|
|
|
|
|
|
check_timeout(undefined) ->
|
|
|
|
true;
|
|
|
|
check_timeout(Source) ->
|
|
|
|
Timeout = case ejabberd_config:get_local_option(registration_timeout) of
|
|
|
|
undefined -> 600;
|
|
|
|
TO -> TO
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
is_integer(Timeout) ->
|
|
|
|
{MSec, Sec, _USec} = now(),
|
|
|
|
Priority = -(MSec * 1000000 + Sec),
|
|
|
|
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
|
|
|
|
end.
|
|
|
|
|
|
|
|
clean_treap(Treap, CleanPriority) ->
|
|
|
|
case treap:is_empty(Treap) of
|
|
|
|
true ->
|
|
|
|
Treap;
|
|
|
|
false ->
|
|
|
|
{_Key, Priority, _Value} = treap:get_root(Treap),
|
|
|
|
if
|
|
|
|
Priority > CleanPriority ->
|
|
|
|
clean_treap(treap:delete_root(Treap), CleanPriority);
|
|
|
|
true ->
|
|
|
|
Treap
|
|
|
|
end
|
|
|
|
end.
|
2008-04-23 15:14:08 +02:00
|
|
|
|
|
|
|
remove_timeout(undefined) ->
|
|
|
|
true;
|
|
|
|
remove_timeout(Source) ->
|
|
|
|
Timeout = case ejabberd_config:get_local_option(registration_timeout) of
|
|
|
|
undefined -> 600;
|
|
|
|
TO -> TO
|
|
|
|
end,
|
|
|
|
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
|
|
|
|
end.
|
|
|
|
|
2009-11-17 12:14:00 +01:00
|
|
|
ip_to_string(Source) when is_tuple(Source) -> inet_parse:ntoa(Source);
|
|
|
|
ip_to_string(undefined) -> "undefined";
|
|
|
|
ip_to_string(_) -> "unknown".
|
|
|
|
|
|
|
|
get_time_string() -> write_time(erlang:localtime()).
|
|
|
|
%% Function copied from ejabberd_logger_h.erl and customized
|
|
|
|
write_time({{Y,Mo,D},{H,Mi,S}}) ->
|
|
|
|
io_lib:format("~w-~.2.0w-~.2.0w ~.2.0w:~.2.0w:~.2.0w",
|
|
|
|
[Y, Mo, D, H, Mi, S]).
|
2010-11-03 09:55:02 +01:00
|
|
|
|
|
|
|
process_xdata_submit(El) ->
|
2010-11-03 12:43:23 +01:00
|
|
|
case exmpp_xml:get_element(El, x) of
|
2010-11-04 01:00:18 +01:00
|
|
|
#xmlel{} = Xdata ->
|
2010-11-03 09:55:02 +01:00
|
|
|
Fields = jlib:parse_xdata_submit(Xdata),
|
|
|
|
case catch {proplists:get_value("username", Fields),
|
|
|
|
proplists:get_value("password", Fields)} of
|
|
|
|
{[User|_], [Pass|_]} ->
|
|
|
|
{ok, User, Pass};
|
|
|
|
_ ->
|
|
|
|
error
|
2010-11-04 01:00:18 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
error
|
2010-11-03 09:55:02 +01:00
|
|
|
end.
|
2010-10-24 09:17:30 +02:00
|
|
|
|
|
|
|
is_strong_password(Server, Password) ->
|
2010-11-03 12:43:23 +01:00
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2010-10-24 09:17:30 +02:00
|
|
|
case gen_mod:get_module_opt(LServer, ?MODULE, password_strength, 0) of
|
|
|
|
Entropy when is_number(Entropy), Entropy >= 0 ->
|
|
|
|
if Entropy == 0 ->
|
|
|
|
true;
|
|
|
|
true ->
|
|
|
|
ejabberd_auth:entropy(Password) >= Entropy
|
|
|
|
end;
|
|
|
|
Wrong ->
|
|
|
|
?WARNING_MSG("Wrong value for password_strength option: ~p",
|
|
|
|
[Wrong]),
|
|
|
|
true
|
|
|
|
end.
|
2010-11-05 19:00:16 +01:00
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% ip_access management
|
|
|
|
%%%
|
|
|
|
|
2010-11-06 20:48:15 +01:00
|
|
|
may_remove_resource({U, S, _}) ->
|
2010-11-05 19:00:16 +01:00
|
|
|
{U, S, ""};
|
|
|
|
may_remove_resource(From) ->
|
|
|
|
From.
|
|
|
|
|
|
|
|
get_ip_access(Host) ->
|
|
|
|
IPAccess = gen_mod:get_module_opt(Host, ?MODULE, ip_access, []),
|
|
|
|
lists:flatmap(
|
|
|
|
fun({Access, S}) ->
|
|
|
|
case parse_ip_netmask(S) of
|
|
|
|
{ok, IP, Mask} ->
|
|
|
|
[{Access, IP, Mask}];
|
|
|
|
error ->
|
|
|
|
?ERROR_MSG("mod_register: invalid "
|
|
|
|
"network specification: ~p",
|
|
|
|
[S]),
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, IPAccess).
|
|
|
|
|
|
|
|
parse_ip_netmask(S) ->
|
|
|
|
case string:tokens(S, "/") of
|
|
|
|
[IPStr] ->
|
|
|
|
case inet_parse:address(IPStr) of
|
|
|
|
{ok, {_, _, _, _} = IP} ->
|
|
|
|
{ok, IP, 32};
|
|
|
|
{ok, {_, _, _, _, _, _, _, _} = IP} ->
|
|
|
|
{ok, IP, 128};
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
[IPStr, MaskStr] ->
|
|
|
|
case catch list_to_integer(MaskStr) of
|
|
|
|
Mask when is_integer(Mask),
|
|
|
|
Mask >= 0 ->
|
|
|
|
case inet_parse:address(IPStr) of
|
|
|
|
{ok, {_, _, _, _} = IP} when Mask =< 32 ->
|
|
|
|
{ok, IP, Mask};
|
|
|
|
{ok, {_, _, _, _, _, _, _, _} = IP} when Mask =< 128 ->
|
|
|
|
{ok, IP, Mask};
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
check_ip_access(_Source, []) ->
|
|
|
|
allow;
|
|
|
|
check_ip_access({User, Server, Resource}, IPAccess) ->
|
2010-11-06 20:48:15 +01:00
|
|
|
JID = exmpp_jid:make(User, Server, Resource),
|
|
|
|
case ejabberd_sm:get_user_ip(JID) of
|
2010-11-05 19:00:16 +01:00
|
|
|
{IPAddress, _PortNumber} -> check_ip_access(IPAddress, IPAccess);
|
|
|
|
_ -> true
|
|
|
|
end;
|
|
|
|
check_ip_access({_, _, _, _} = IP,
|
|
|
|
[{Access, {_, _, _, _} = Net, Mask} | IPAccess]) ->
|
|
|
|
IPInt = ip_to_integer(IP),
|
|
|
|
NetInt = ip_to_integer(Net),
|
|
|
|
M = bnot ((1 bsl (32 - Mask)) - 1),
|
|
|
|
if
|
|
|
|
IPInt band M =:= NetInt band M ->
|
|
|
|
Access;
|
|
|
|
true ->
|
|
|
|
check_ip_access(IP, IPAccess)
|
|
|
|
end;
|
|
|
|
check_ip_access({_, _, _, _, _, _, _, _} = IP,
|
|
|
|
[{Access, {_, _, _, _, _, _, _, _} = Net, Mask} | IPAccess]) ->
|
|
|
|
IPInt = ip_to_integer(IP),
|
|
|
|
NetInt = ip_to_integer(Net),
|
|
|
|
M = bnot ((1 bsl (128 - Mask)) - 1),
|
|
|
|
if
|
|
|
|
IPInt band M =:= NetInt band M ->
|
|
|
|
Access;
|
|
|
|
true ->
|
|
|
|
check_ip_access(IP, IPAccess)
|
|
|
|
end;
|
|
|
|
check_ip_access(IP, [_ | IPAccess]) ->
|
|
|
|
check_ip_access(IP, IPAccess).
|
|
|
|
|
|
|
|
ip_to_integer({IP1, IP2, IP3, IP4}) ->
|
|
|
|
(((((IP1 bsl 8) bor IP2) bsl 8) bor IP3) bsl 8) bor IP4;
|
|
|
|
ip_to_integer({IP1, IP2, IP3, IP4, IP5, IP6, IP7, IP8}) ->
|
|
|
|
(((((((((((((IP1 bsl 16) bor IP2) bsl 16) bor IP3) bsl 16) bor IP4)
|
|
|
|
bsl 16) bor IP5) bsl 16) bor IP6) bsl 16) bor IP7) bsl 16) bor IP8.
|