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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-12 15:44:42 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 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
|
|
|
%%%
|
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,
|
2005-07-15 00:28:21 +02:00
|
|
|
stream_feature_register/1,
|
2008-04-22 23:51:32 +02:00
|
|
|
unauthenticated_iq_register/4,
|
2005-06-20 05:18:13 +02:00
|
|
|
process_iq/3]).
|
2002-12-08 18:23:21 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_REGISTER,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_iq, IQDisc),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_REGISTER,
|
2003-09-19 20:22:44 +02:00
|
|
|
?MODULE, process_iq, IQDisc),
|
2005-07-15 00:28:21 +02:00
|
|
|
ejabberd_hooks:add(c2s_stream_features, Host,
|
|
|
|
?MODULE, stream_feature_register, 50),
|
|
|
|
ejabberd_hooks:add(c2s_unauthenticated_iq, Host,
|
|
|
|
?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) ->
|
2005-07-15 00:28:21 +02:00
|
|
|
ejabberd_hooks:delete(c2s_stream_features, Host,
|
|
|
|
?MODULE, stream_feature_register, 50),
|
|
|
|
ejabberd_hooks:delete(c2s_unauthenticated_iq, Host,
|
|
|
|
?MODULE, unauthenticated_iq_register, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_REGISTER),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_REGISTER).
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2005-07-15 00:28:21 +02:00
|
|
|
|
|
|
|
stream_feature_register(Acc) ->
|
|
|
|
[{xmlelement, "register",
|
|
|
|
[{"xmlns", ?NS_FEATURE_IQREGISTER}], []} | Acc].
|
|
|
|
|
2008-04-22 23:51:32 +02:00
|
|
|
unauthenticated_iq_register(_Acc,
|
|
|
|
Server, #iq{xmlns = ?NS_REGISTER} = IQ, IP) ->
|
|
|
|
Address = case IP of
|
|
|
|
{A, _Port} -> A;
|
|
|
|
_ -> undefined
|
|
|
|
end,
|
2005-07-15 00:28:21 +02:00
|
|
|
ResIQ = process_iq(jlib:make_jid("", "", ""),
|
|
|
|
jlib:make_jid("", Server, ""),
|
2008-04-22 23:51:32 +02:00
|
|
|
IQ,
|
|
|
|
Address),
|
2005-07-15 00:28:21 +02:00
|
|
|
Res1 = jlib:replace_from_to(jlib:make_jid("", Server, ""),
|
|
|
|
jlib:make_jid("", "", ""),
|
|
|
|
jlib:iq_to_xml(ResIQ)),
|
|
|
|
jlib:remove_attr("to", Res1);
|
|
|
|
|
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) ->
|
|
|
|
process_iq(From, To, IQ, jlib:jid_tolower(jlib:jid_remove_resource(From))).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
process_iq(From, To,
|
2008-04-22 23:51:32 +02:00
|
|
|
#iq{type = Type, lang = Lang, sub_el = SubEl, id = ID} = IQ,
|
|
|
|
Source) ->
|
2002-12-08 18:23:21 +01:00
|
|
|
case Type of
|
|
|
|
set ->
|
2003-01-02 22:01:12 +01:00
|
|
|
UTag = xml:get_subtag(SubEl, "username"),
|
|
|
|
PTag = xml:get_subtag(SubEl, "password"),
|
|
|
|
RTag = xml:get_subtag(SubEl, "remove"),
|
2005-04-17 20:08:34 +02:00
|
|
|
Server = To#jid.lserver,
|
2002-12-08 18:23:21 +01:00
|
|
|
if
|
|
|
|
(UTag /= false) and (RTag /= false) ->
|
2003-02-21 20:52:15 +01:00
|
|
|
User = xml:get_tag_cdata(UTag),
|
|
|
|
case From of
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{user = User, lserver = Server} ->
|
2005-04-17 20:08:34 +02:00
|
|
|
ejabberd_auth:remove_user(User, Server),
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result, sub_el = [SubEl]};
|
2003-02-21 20:52:15 +01:00
|
|
|
_ ->
|
|
|
|
if
|
|
|
|
PTag /= false ->
|
|
|
|
Password = xml:get_tag_cdata(PTag),
|
|
|
|
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 ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [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 ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el =
|
|
|
|
[SubEl, ?ERR_NOT_ALLOWED]};
|
2003-02-21 20:52:15 +01:00
|
|
|
not_exists ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el =
|
|
|
|
[SubEl, ?ERR_ITEM_NOT_FOUND]};
|
2003-02-21 20:52:15 +01:00
|
|
|
_ ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el =
|
|
|
|
[SubEl,
|
|
|
|
?ERR_INTERNAL_SERVER_ERROR]}
|
2003-02-21 20:52:15 +01:00
|
|
|
end;
|
|
|
|
true ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_BAD_REQUEST]}
|
2003-02-21 20:52:15 +01:00
|
|
|
end
|
|
|
|
end;
|
2003-09-19 20:22:44 +02:00
|
|
|
(UTag == false) and (RTag /= false) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
case From of
|
2006-07-28 18:18:50 +02:00
|
|
|
#jid{user = User,
|
|
|
|
lserver = Server,
|
|
|
|
resource = Resource} ->
|
|
|
|
ResIQ = #iq{type = result, xmlns = ?NS_REGISTER,
|
|
|
|
id = ID,
|
|
|
|
sub_el = [SubEl]},
|
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(User, Server, Resource),
|
|
|
|
jlib:make_jid(User, Server, Resource),
|
|
|
|
jlib:iq_to_xml(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
|
|
|
_ ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_NOT_ALLOWED]}
|
2003-10-07 22:31:44 +02:00
|
|
|
end;
|
2002-12-08 18:23:21 +01:00
|
|
|
(UTag /= false) and (PTag /= false) ->
|
|
|
|
User = xml:get_tag_cdata(UTag),
|
|
|
|
Password = xml:get_tag_cdata(PTag),
|
|
|
|
case From of
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{user = User, lserver = Server} ->
|
2008-08-18 20:21:10 +02:00
|
|
|
try_set_password(User, Server, Password, IQ, SubEl);
|
2002-12-08 18:23:21 +01:00
|
|
|
_ ->
|
2008-04-22 23:51:32 +02:00
|
|
|
case try_register(User, Server, Password,
|
2008-07-24 19:49:37 +02:00
|
|
|
Source, Lang) of
|
2002-12-08 18:23:21 +01:00
|
|
|
ok ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result, sub_el = [SubEl]};
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, Error} ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, Error]}
|
2002-12-08 18:23:21 +01:00
|
|
|
end
|
2003-07-20 22:35:35 +02:00
|
|
|
end;
|
|
|
|
true ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_BAD_REQUEST]}
|
2002-12-08 18:23:21 +01:00
|
|
|
end;
|
|
|
|
get ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement,
|
|
|
|
"query",
|
|
|
|
[{"xmlns", "jabber:iq:register"}],
|
|
|
|
[{xmlelement, "instructions", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(
|
|
|
|
Lang,
|
|
|
|
"Choose a username and password "
|
2004-02-26 23:00:04 +01:00
|
|
|
"to register with this server")}]},
|
2003-12-17 21:13:21 +01:00
|
|
|
{xmlelement, "username", [], []},
|
|
|
|
{xmlelement, "password", [], []}]}]}
|
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
|
|
|
|
try_set_password(User, Server, Password, IQ, SubEl) ->
|
|
|
|
case ejabberd_auth:set_password(User, Server, Password) of
|
|
|
|
ok ->
|
|
|
|
IQ#iq{type = result, sub_el = [SubEl]};
|
|
|
|
{error, empty_password} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_BAD_REQUEST]};
|
|
|
|
{error, not_allowed} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
|
|
|
|
{error, invalid_jid} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_ITEM_NOT_FOUND]};
|
|
|
|
_ ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]}
|
|
|
|
end.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2008-07-24 19:49:37 +02:00
|
|
|
try_register(User, Server, Password, Source, Lang) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
case jlib:is_nodename(User) of
|
|
|
|
false ->
|
2003-05-09 21:32:59 +02:00
|
|
|
{error, ?ERR_BAD_REQUEST};
|
2003-01-11 21:25:11 +01:00
|
|
|
_ ->
|
2005-04-17 20:08:34 +02:00
|
|
|
JID = jlib:make_jid(User, Server, ""),
|
2005-06-20 05:18:13 +02:00
|
|
|
Access = gen_mod:get_module_opt(Server, ?MODULE, access, all),
|
|
|
|
case acl:match_rule(Server, Access, JID) of
|
2003-10-11 19:39:36 +02:00
|
|
|
deny ->
|
|
|
|
{error, ?ERR_CONFLICT};
|
|
|
|
allow ->
|
2008-04-22 23:51:32 +02:00
|
|
|
case check_timeout(Source) of
|
|
|
|
true ->
|
|
|
|
case ejabberd_auth:try_register(User, Server, Password) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
send_welcome_message(JID),
|
|
|
|
send_registration_notifications(JID),
|
|
|
|
ok;
|
2008-04-23 15:14:08 +02:00
|
|
|
Error ->
|
|
|
|
remove_timeout(Source),
|
|
|
|
case Error of
|
|
|
|
{atomic, exists} ->
|
|
|
|
{error, ?ERR_CONFLICT};
|
|
|
|
{error, invalid_jid} ->
|
|
|
|
{error, ?ERR_JID_MALFORMED};
|
|
|
|
{error, not_allowed} ->
|
|
|
|
{error, ?ERR_NOT_ALLOWED};
|
|
|
|
{error, _Reason} ->
|
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR}
|
|
|
|
end
|
2008-04-22 23:51:32 +02:00
|
|
|
end;
|
|
|
|
false ->
|
2008-07-24 19:49:37 +02:00
|
|
|
ErrText = "Users are not allowed to register "
|
|
|
|
"accounts so fast",
|
|
|
|
{error, ?ERRT_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) ->
|
2005-07-13 05:24:13 +02:00
|
|
|
Host = JID#jid.lserver,
|
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(
|
2005-07-13 05:24:13 +02:00
|
|
|
jlib:make_jid("", Host, ""),
|
2003-10-14 21:34:17 +02:00
|
|
|
JID,
|
|
|
|
{xmlelement, "message", [{"type", "normal"}],
|
|
|
|
[{xmlelement, "subject", [], [{xmlcdata, Subj}]},
|
2008-07-11 15:35:08 +02:00
|
|
|
{xmlelement, "body", [], [{xmlcdata, Body}]}]});
|
2003-10-14 21:34:17 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2003-10-19 18:19:55 +02:00
|
|
|
send_registration_notifications(UJID) ->
|
2005-07-13 05:24:13 +02:00
|
|
|
Host = UJID#jid.lserver,
|
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(
|
|
|
|
"The user '~s' was just created on node ~w.",
|
|
|
|
[jlib:jid_to_string(UJID), node()])),
|
|
|
|
lists:foreach(
|
|
|
|
fun(S) ->
|
|
|
|
case jlib:string_to_jid(S) of
|
|
|
|
error -> ok;
|
|
|
|
JID ->
|
|
|
|
ejabberd_router:route(
|
2005-07-13 05:24:13 +02:00
|
|
|
jlib:make_jid("", Host, ""),
|
2003-10-19 18:19:55 +02:00
|
|
|
JID,
|
|
|
|
{xmlelement, "message", [{"type", "chat"}],
|
|
|
|
[{xmlelement, "body", [],
|
|
|
|
[{xmlcdata, Body}]}]})
|
|
|
|
end
|
|
|
|
end, JIDs);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
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.
|
|
|
|
|