2010-11-04 22:34:18 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% File : mod_register_web.erl
|
|
|
|
%%% Author : Badlop <badlop@process-one.net>
|
|
|
|
%%% Purpose : Web page to register account and related tasks
|
|
|
|
%%% Created : 4 May 2008 by Badlop <badlop@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2010-11-04 22:34:18 +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.
|
|
|
|
%%%
|
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.
|
2010-11-04 22:34:18 +01:00
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%% IDEAS:
|
|
|
|
%%%
|
|
|
|
%%% * Implement those options, already present in mod_register:
|
|
|
|
%%% + access
|
|
|
|
%%% + captcha_protected
|
|
|
|
%%% + password_strength
|
|
|
|
%%% + welcome_message
|
|
|
|
%%% + registration_timeout
|
|
|
|
%%%
|
|
|
|
%%% * Improve this module to allow each virtual host to have different
|
|
|
|
%%% options. See http://support.process-one.net/browse/EJAB-561
|
|
|
|
%%%
|
|
|
|
%%% * Check that all the text is translatable.
|
|
|
|
%%%
|
|
|
|
%%% * Add option to use a custom CSS file, or custom CSS lines.
|
|
|
|
%%%
|
|
|
|
%%% * Don't hardcode the "register" path in URL.
|
|
|
|
%%%
|
|
|
|
%%% * Allow private email during register, and store in custom table.
|
|
|
|
%%% * Optionally require private email to register.
|
|
|
|
%%% * Optionally require email confirmation to register.
|
|
|
|
%%% * Allow to set a private email address anytime.
|
|
|
|
%%% * Allow to recover password using private email to confirm (mod_passrecover)
|
|
|
|
%%% * Optionally require invitation
|
|
|
|
%%% * Optionally register request is forwarded to admin, no account created.
|
|
|
|
|
|
|
|
-module(mod_register_web).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-11-04 22:34:18 +01:00
|
|
|
-author('badlop@process-one.net').
|
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-export([start/2, stop/1, reload/3, process/2, mod_options/1, depends/2]).
|
2010-11-04 22:34:18 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-07-28 14:10:41 +02:00
|
|
|
-include("xmpp.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-11-04 22:34:18 +01:00
|
|
|
-include("ejabberd_http.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-11-04 22:34:18 +01:00
|
|
|
-include("ejabberd_web_admin.hrl").
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% gen_mod callbacks
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
start(_Host, _Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
%% case gen_mod:get_opt(docroot, Opts, fun(A) -> A end, undefined) of
|
2010-11-04 22:34:18 +01:00
|
|
|
ok.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
stop(_Host) -> ok.
|
2010-11-04 22:34:18 +01:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(_Host, _NewOpts, _OldOpts) ->
|
|
|
|
ok.
|
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[{mod_register, hard}].
|
|
|
|
|
2010-11-04 22:34:18 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% HTTP handlers
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
process([], #request{method = 'GET', lang = Lang}) ->
|
|
|
|
index_page(Lang);
|
2013-03-14 10:33:02 +01:00
|
|
|
process([<<"register.css">>],
|
|
|
|
#request{method = 'GET'}) ->
|
2010-11-04 22:34:18 +01:00
|
|
|
serve_css();
|
2013-03-14 10:33:02 +01:00
|
|
|
process([<<"new">>],
|
|
|
|
#request{method = 'GET', lang = Lang, host = Host,
|
|
|
|
ip = IP}) ->
|
|
|
|
{Addr, _Port} = IP, form_new_get(Host, Lang, Addr);
|
|
|
|
process([<<"delete">>],
|
|
|
|
#request{method = 'GET', lang = Lang, host = Host}) ->
|
2010-11-04 22:34:18 +01:00
|
|
|
form_del_get(Host, Lang);
|
2013-03-14 10:33:02 +01:00
|
|
|
process([<<"change_password">>],
|
|
|
|
#request{method = 'GET', lang = Lang, host = Host}) ->
|
2010-11-04 22:34:18 +01:00
|
|
|
form_changepass_get(Host, Lang);
|
2013-03-14 10:33:02 +01:00
|
|
|
process([<<"new">>],
|
|
|
|
#request{method = 'POST', q = Q, ip = {Ip, _Port},
|
2015-04-22 11:01:13 +02:00
|
|
|
lang = Lang, host = _HTTPHost}) ->
|
|
|
|
case form_new_post(Q) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{success, ok, {Username, Host, _Password}} ->
|
2017-02-25 08:01:01 +01:00
|
|
|
Jid = jid:make(Username, Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
mod_register:send_registration_notifications(?MODULE, Jid, Ip),
|
|
|
|
Text = (?T(<<"Your Jabber account was successfully "
|
|
|
|
"created.">>)),
|
|
|
|
{200, [], Text};
|
|
|
|
Error ->
|
|
|
|
ErrorText =
|
|
|
|
list_to_binary([?T(<<"There was an error creating the account: ">>),
|
|
|
|
?T(get_error_text(Error))]),
|
|
|
|
{404, [], ErrorText}
|
2010-11-04 22:34:18 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
process([<<"delete">>],
|
|
|
|
#request{method = 'POST', q = Q, lang = Lang,
|
2016-02-04 21:31:16 +01:00
|
|
|
host = _HTTPHost}) ->
|
|
|
|
case form_del_post(Q) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, ok} ->
|
|
|
|
Text = (?T(<<"Your Jabber account was successfully "
|
|
|
|
"deleted.">>)),
|
|
|
|
{200, [], Text};
|
|
|
|
Error ->
|
|
|
|
ErrorText =
|
|
|
|
list_to_binary([?T(<<"There was an error deleting the account: ">>),
|
|
|
|
?T(get_error_text(Error))]),
|
|
|
|
{404, [], ErrorText}
|
2010-11-04 22:34:18 +01:00
|
|
|
end;
|
|
|
|
%% TODO: Currently only the first vhost is usable. The web request record
|
|
|
|
%% should include the host where the POST was sent.
|
2013-03-14 10:33:02 +01:00
|
|
|
process([<<"change_password">>],
|
|
|
|
#request{method = 'POST', q = Q, lang = Lang,
|
2016-02-04 21:31:16 +01:00
|
|
|
host = _HTTPHost}) ->
|
|
|
|
case form_changepass_post(Q) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, ok} ->
|
|
|
|
Text = (?T(<<"The password of your Jabber account "
|
|
|
|
"was successfully changed.">>)),
|
|
|
|
{200, [], Text};
|
|
|
|
Error ->
|
|
|
|
ErrorText =
|
|
|
|
list_to_binary([?T(<<"There was an error changing the password: ">>),
|
|
|
|
?T(get_error_text(Error))]),
|
|
|
|
{404, [], ErrorText}
|
2014-04-30 06:39:17 +02:00
|
|
|
end;
|
|
|
|
|
2015-01-08 19:00:26 +01:00
|
|
|
process(_Path, _Request) ->
|
2014-04-30 06:39:17 +02:00
|
|
|
{404, [], "Not Found"}.
|
2010-11-04 22:34:18 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% CSS
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
serve_css() ->
|
2017-10-09 20:46:38 +02:00
|
|
|
case css() of
|
|
|
|
{ok, CSS} ->
|
|
|
|
{200,
|
|
|
|
[{<<"Content-Type">>, <<"text/css">>}, last_modified(),
|
|
|
|
cache_control_public()], CSS};
|
|
|
|
error ->
|
|
|
|
{404, [], "CSS not found"}
|
|
|
|
end.
|
2010-11-04 22:34:18 +01:00
|
|
|
|
|
|
|
last_modified() ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{<<"Last-Modified">>,
|
|
|
|
<<"Mon, 25 Feb 2008 13:23:30 GMT">>}.
|
|
|
|
|
2010-11-04 22:34:18 +01:00
|
|
|
cache_control_public() ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{<<"Cache-Control">>, <<"public">>}.
|
2010-11-04 22:34:18 +01:00
|
|
|
|
2017-10-09 20:46:38 +02:00
|
|
|
-spec css() -> {ok, binary()} | error.
|
2010-11-04 22:34:18 +01:00
|
|
|
css() ->
|
2017-10-09 20:46:38 +02:00
|
|
|
Dir = misc:css_dir(),
|
|
|
|
File = filename:join(Dir, "register.css"),
|
|
|
|
case file:read_file(File) of
|
|
|
|
{ok, Data} ->
|
|
|
|
{ok, Data};
|
|
|
|
{error, Why} ->
|
|
|
|
?ERROR_MSG("failed to read ~s: ~s", [File, file:format_error(Why)]),
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
meta() ->
|
|
|
|
?XA(<<"meta">>,
|
|
|
|
[{<<"name">>, <<"viewport">>},
|
|
|
|
{<<"content">>, <<"width=device-width, initial-scale=1">>}]).
|
2010-11-04 22:34:18 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Index page
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
index_page(Lang) ->
|
2017-10-09 20:46:38 +02:00
|
|
|
HeadEls = [meta(),
|
|
|
|
?XCT(<<"title">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"Jabber Account Registration">>),
|
|
|
|
?XA(<<"link">>,
|
|
|
|
[{<<"href">>, <<"/register/register.css">>},
|
|
|
|
{<<"type">>, <<"text/css">>},
|
|
|
|
{<<"rel">>, <<"stylesheet">>}])],
|
|
|
|
Els = [?XACT(<<"h1">>,
|
|
|
|
[{<<"class">>, <<"title">>},
|
|
|
|
{<<"style">>, <<"text-align:center;">>}],
|
|
|
|
<<"Jabber Account Registration">>),
|
|
|
|
?XE(<<"ul">>,
|
|
|
|
[?XE(<<"li">>,
|
|
|
|
[?ACT(<<"new">>, <<"Register a Jabber account">>)]),
|
|
|
|
?XE(<<"li">>,
|
|
|
|
[?ACT(<<"change_password">>, <<"Change Password">>)]),
|
|
|
|
?XE(<<"li">>,
|
|
|
|
[?ACT(<<"delete">>,
|
|
|
|
<<"Unregister a Jabber account">>)])])],
|
2010-11-04 22:34:18 +01:00
|
|
|
{200,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{<<"Server">>, <<"ejabberd">>},
|
|
|
|
{<<"Content-Type">>, <<"text/html">>}],
|
2010-11-04 22:34:18 +01:00
|
|
|
ejabberd_web:make_xhtml(HeadEls, Els)}.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Formulary new account GET
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2011-04-14 10:03:02 +02:00
|
|
|
form_new_get(Host, Lang, IP) ->
|
|
|
|
CaptchaEls = build_captcha_li_list(Lang, IP),
|
2017-10-09 20:46:38 +02:00
|
|
|
HeadEls = [meta(),
|
|
|
|
?XCT(<<"title">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"Register a Jabber account">>),
|
|
|
|
?XA(<<"link">>,
|
|
|
|
[{<<"href">>, <<"/register/register.css">>},
|
|
|
|
{<<"type">>, <<"text/css">>},
|
|
|
|
{<<"rel">>, <<"stylesheet">>}])],
|
|
|
|
Els = [?XACT(<<"h1">>,
|
|
|
|
[{<<"class">>, <<"title">>},
|
|
|
|
{<<"style">>, <<"text-align:center;">>}],
|
|
|
|
<<"Register a Jabber account">>),
|
|
|
|
?XCT(<<"p">>,
|
|
|
|
<<"This page allows to create a Jabber "
|
|
|
|
"account in this Jabber server. Your "
|
|
|
|
"JID (Jabber IDentifier) will be of the "
|
|
|
|
"form: username@server. Please read carefully "
|
|
|
|
"the instructions to fill correctly the "
|
|
|
|
"fields.">>),
|
|
|
|
?XAE(<<"form">>,
|
|
|
|
[{<<"action">>, <<"">>}, {<<"method">>, <<"post">>}],
|
|
|
|
[?XE(<<"ol">>,
|
|
|
|
([?XE(<<"li">>,
|
|
|
|
[?CT(<<"Username:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"text">>, <<"username">>, <<"">>,
|
|
|
|
<<"20">>),
|
|
|
|
?BR,
|
|
|
|
?XE(<<"ul">>,
|
|
|
|
[?XCT(<<"li">>,
|
|
|
|
<<"This is case insensitive: macbeth is "
|
|
|
|
"the same that MacBeth and Macbeth.">>),
|
|
|
|
?XC(<<"li">>,
|
|
|
|
<<(?T(<<"Characters not allowed:">>))/binary,
|
|
|
|
" \" & ' / : < > @ ">>)])]),
|
|
|
|
?XE(<<"li">>,
|
2015-04-22 11:01:13 +02:00
|
|
|
[?CT(<<"Server:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"text">>, <<"host">>, Host, <<"20">>)]),
|
2013-03-14 10:33:02 +01:00
|
|
|
?XE(<<"li">>,
|
|
|
|
[?CT(<<"Password:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"password">>, <<"password">>, <<"">>,
|
|
|
|
<<"20">>),
|
|
|
|
?BR,
|
|
|
|
?XE(<<"ul">>,
|
|
|
|
[?XCT(<<"li">>,
|
|
|
|
<<"Don't tell your password to anybody, "
|
|
|
|
"not even the administrators of the Jabber "
|
|
|
|
"server.">>),
|
|
|
|
?XCT(<<"li">>,
|
|
|
|
<<"You can later change your password using "
|
|
|
|
"a Jabber client.">>),
|
|
|
|
?XCT(<<"li">>,
|
|
|
|
<<"Some Jabber clients can store your password "
|
2015-12-28 15:08:59 +01:00
|
|
|
"in the computer, but you should do this only "
|
|
|
|
"in your personal computer for safety reasons.">>),
|
2013-03-14 10:33:02 +01:00
|
|
|
?XCT(<<"li">>,
|
|
|
|
<<"Memorize your password, or write it "
|
|
|
|
"in a paper placed in a safe place. In "
|
|
|
|
"Jabber there isn't an automated way "
|
|
|
|
"to recover your password if you forget "
|
|
|
|
"it.">>)])]),
|
|
|
|
?XE(<<"li">>,
|
|
|
|
[?CT(<<"Password Verification:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"password">>, <<"password2">>, <<"">>,
|
|
|
|
<<"20">>)])]
|
|
|
|
++
|
|
|
|
CaptchaEls ++
|
|
|
|
[?XE(<<"li">>,
|
|
|
|
[?INPUTT(<<"submit">>, <<"register">>,
|
|
|
|
<<"Register">>)])]))])],
|
2010-11-04 22:34:18 +01:00
|
|
|
{200,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{<<"Server">>, <<"ejabberd">>},
|
|
|
|
{<<"Content-Type">>, <<"text/html">>}],
|
2010-11-04 22:34:18 +01:00
|
|
|
ejabberd_web:make_xhtml(HeadEls, Els)}.
|
|
|
|
|
|
|
|
%% Copied from mod_register.erl
|
|
|
|
%% Function copied from ejabberd_logger_h.erl and customized
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Formulary new POST
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2015-04-22 11:01:13 +02:00
|
|
|
form_new_post(Q) ->
|
2010-11-04 22:34:18 +01:00
|
|
|
case catch get_register_parameters(Q) of
|
2015-04-22 11:01:13 +02:00
|
|
|
[Username, Host, Password, Password, Id, Key] ->
|
2013-03-14 10:33:02 +01:00
|
|
|
form_new_post(Username, Host, Password, {Id, Key});
|
2016-02-29 00:39:57 +01:00
|
|
|
[_Username, _Host, _Password, _Password2, false, false] ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, passwords_not_identical};
|
2016-02-29 00:39:57 +01:00
|
|
|
[_Username, _Host, _Password, _Password2, Id, Key] ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_captcha:check_captcha(Id, Key),
|
|
|
|
{error, passwords_not_identical};
|
|
|
|
_ -> {error, wrong_parameters}
|
2010-11-04 22:34:18 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_register_parameters(Q) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:map(fun (Key) ->
|
|
|
|
case lists:keysearch(Key, 1, Q) of
|
|
|
|
{value, {_Key, Value}} -> Value;
|
|
|
|
false -> false
|
|
|
|
end
|
|
|
|
end,
|
2015-04-22 11:01:13 +02:00
|
|
|
[<<"username">>, <<"host">>, <<"password">>, <<"password2">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"id">>, <<"key">>]).
|
|
|
|
|
|
|
|
form_new_post(Username, Host, Password,
|
|
|
|
{false, false}) ->
|
2010-11-04 22:34:18 +01:00
|
|
|
register_account(Username, Host, Password);
|
|
|
|
form_new_post(Username, Host, Password, {Id, Key}) ->
|
|
|
|
case ejabberd_captcha:check_captcha(Id, Key) of
|
2013-03-14 10:33:02 +01:00
|
|
|
captcha_valid ->
|
|
|
|
register_account(Username, Host, Password);
|
|
|
|
captcha_non_valid -> {error, captcha_non_valid};
|
|
|
|
captcha_not_found -> {error, captcha_non_valid}
|
2010-11-04 22:34:18 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Formulary Captcha support for new GET/POST
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2011-04-14 10:03:02 +02:00
|
|
|
build_captcha_li_list(Lang, IP) ->
|
2010-11-04 22:34:18 +01:00
|
|
|
case ejabberd_captcha:is_feature_available() of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> build_captcha_li_list2(Lang, IP);
|
|
|
|
false -> []
|
2010-11-04 22:34:18 +01:00
|
|
|
end.
|
|
|
|
|
2011-04-14 10:03:02 +02:00
|
|
|
build_captcha_li_list2(Lang, IP) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
SID = <<"">>,
|
|
|
|
From = #jid{user = <<"">>, server = <<"test">>,
|
|
|
|
resource = <<"">>},
|
|
|
|
To = #jid{user = <<"">>, server = <<"test">>,
|
|
|
|
resource = <<"">>},
|
2010-11-04 22:34:18 +01:00
|
|
|
Args = [],
|
2013-03-14 10:33:02 +01:00
|
|
|
case ejabberd_captcha:create_captcha(SID, From, To,
|
|
|
|
Lang, IP, Args)
|
|
|
|
of
|
2016-07-28 14:10:41 +02:00
|
|
|
{ok, Id, _, _} ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{_, {CImg, CText, CId, CKey}} =
|
|
|
|
ejabberd_captcha:build_captcha_html(Id, Lang),
|
|
|
|
[?XE(<<"li">>,
|
|
|
|
[CText, ?C(<<" ">>), CId, CKey, ?BR, CImg])];
|
|
|
|
_ -> []
|
2011-04-14 10:03:02 +02:00
|
|
|
end.
|
2010-11-04 22:34:18 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Formulary change password GET
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
form_changepass_get(Host, Lang) ->
|
2017-10-09 20:46:38 +02:00
|
|
|
HeadEls = [meta(),
|
|
|
|
?XCT(<<"title">>, <<"Change Password">>),
|
2013-03-14 10:33:02 +01:00
|
|
|
?XA(<<"link">>,
|
|
|
|
[{<<"href">>, <<"/register/register.css">>},
|
|
|
|
{<<"type">>, <<"text/css">>},
|
|
|
|
{<<"rel">>, <<"stylesheet">>}])],
|
|
|
|
Els = [?XACT(<<"h1">>,
|
|
|
|
[{<<"class">>, <<"title">>},
|
|
|
|
{<<"style">>, <<"text-align:center;">>}],
|
|
|
|
<<"Change Password">>),
|
|
|
|
?XAE(<<"form">>,
|
|
|
|
[{<<"action">>, <<"">>}, {<<"method">>, <<"post">>}],
|
|
|
|
[?XE(<<"ol">>,
|
|
|
|
[?XE(<<"li">>,
|
|
|
|
[?CT(<<"Username:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"text">>, <<"username">>, <<"">>,
|
|
|
|
<<"20">>)]),
|
|
|
|
?XE(<<"li">>,
|
2016-02-04 21:31:16 +01:00
|
|
|
[?CT(<<"Server:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"text">>, <<"host">>, Host, <<"20">>)]),
|
2013-03-14 10:33:02 +01:00
|
|
|
?XE(<<"li">>,
|
|
|
|
[?CT(<<"Old Password:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"password">>, <<"passwordold">>, <<"">>,
|
|
|
|
<<"20">>)]),
|
|
|
|
?XE(<<"li">>,
|
|
|
|
[?CT(<<"New Password:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"password">>, <<"password">>, <<"">>,
|
|
|
|
<<"20">>)]),
|
|
|
|
?XE(<<"li">>,
|
|
|
|
[?CT(<<"Password Verification:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"password">>, <<"password2">>, <<"">>,
|
|
|
|
<<"20">>)]),
|
|
|
|
?XE(<<"li">>,
|
|
|
|
[?INPUTT(<<"submit">>, <<"changepass">>,
|
|
|
|
<<"Change Password">>)])])])],
|
2010-11-04 22:34:18 +01:00
|
|
|
{200,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{<<"Server">>, <<"ejabberd">>},
|
|
|
|
{<<"Content-Type">>, <<"text/html">>}],
|
2010-11-04 22:34:18 +01:00
|
|
|
ejabberd_web:make_xhtml(HeadEls, Els)}.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Formulary change password POST
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-02-04 21:31:16 +01:00
|
|
|
form_changepass_post(Q) ->
|
2010-11-04 22:34:18 +01:00
|
|
|
case catch get_changepass_parameters(Q) of
|
2016-02-04 21:31:16 +01:00
|
|
|
[Username, Host, PasswordOld, Password, Password] ->
|
2013-03-14 10:33:02 +01:00
|
|
|
try_change_password(Username, Host, PasswordOld,
|
|
|
|
Password);
|
2016-02-29 00:39:57 +01:00
|
|
|
[_Username, _Host, _PasswordOld, _Password, _Password2] ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, passwords_not_identical};
|
|
|
|
_ -> {error, wrong_parameters}
|
2010-11-04 22:34:18 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_changepass_parameters(Q) ->
|
|
|
|
%% @spec(Username,Host,PasswordOld,Password) -> {atomic, ok} |
|
|
|
|
%% {error, account_doesnt_exist} |
|
|
|
|
%% {error, password_not_changed} |
|
|
|
|
%% {error, password_incorrect}
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:map(fun (Key) ->
|
|
|
|
{value, {_Key, Value}} = lists:keysearch(Key, 1, Q),
|
|
|
|
Value
|
|
|
|
end,
|
2016-02-04 21:31:16 +01:00
|
|
|
[<<"username">>, <<"host">>, <<"passwordold">>, <<"password">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"password2">>]).
|
|
|
|
|
|
|
|
try_change_password(Username, Host, PasswordOld,
|
|
|
|
Password) ->
|
|
|
|
try change_password(Username, Host, PasswordOld,
|
|
|
|
Password)
|
|
|
|
of
|
|
|
|
{atomic, ok} -> {atomic, ok}
|
2010-11-04 22:34:18 +01:00
|
|
|
catch
|
2013-03-14 10:33:02 +01:00
|
|
|
error:{badmatch, Error} -> {error, Error}
|
2010-11-04 22:34:18 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
change_password(Username, Host, PasswordOld,
|
|
|
|
Password) ->
|
2010-11-04 22:34:18 +01:00
|
|
|
account_exists = check_account_exists(Username, Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
password_correct = check_password(Username, Host,
|
|
|
|
PasswordOld),
|
|
|
|
ok = ejabberd_auth:set_password(Username, Host,
|
|
|
|
Password),
|
2010-11-04 22:34:18 +01:00
|
|
|
case check_password(Username, Host, Password) of
|
2013-03-14 10:33:02 +01:00
|
|
|
password_correct -> {atomic, ok};
|
|
|
|
password_incorrect -> {error, password_not_changed}
|
2010-11-04 22:34:18 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
check_account_exists(Username, Host) ->
|
2017-05-11 14:49:06 +02:00
|
|
|
case ejabberd_auth:user_exists(Username, Host) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> account_exists;
|
|
|
|
false -> account_doesnt_exist
|
2010-11-04 22:34:18 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
check_password(Username, Host, Password) ->
|
2015-04-09 03:21:09 +02:00
|
|
|
case ejabberd_auth:check_password(Username, <<"">>, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
Password)
|
|
|
|
of
|
|
|
|
true -> password_correct;
|
|
|
|
false -> password_incorrect
|
2010-11-04 22:34:18 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Formulary delete account GET
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
form_del_get(Host, Lang) ->
|
2017-10-09 20:46:38 +02:00
|
|
|
HeadEls = [meta(),
|
|
|
|
?XCT(<<"title">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"Unregister a Jabber account">>),
|
|
|
|
?XA(<<"link">>,
|
|
|
|
[{<<"href">>, <<"/register/register.css">>},
|
|
|
|
{<<"type">>, <<"text/css">>},
|
|
|
|
{<<"rel">>, <<"stylesheet">>}])],
|
|
|
|
Els = [?XACT(<<"h1">>,
|
|
|
|
[{<<"class">>, <<"title">>},
|
|
|
|
{<<"style">>, <<"text-align:center;">>}],
|
|
|
|
<<"Unregister a Jabber account">>),
|
|
|
|
?XCT(<<"p">>,
|
|
|
|
<<"This page allows to unregister a Jabber "
|
|
|
|
"account in this Jabber server.">>),
|
|
|
|
?XAE(<<"form">>,
|
|
|
|
[{<<"action">>, <<"">>}, {<<"method">>, <<"post">>}],
|
|
|
|
[?XE(<<"ol">>,
|
|
|
|
[?XE(<<"li">>,
|
|
|
|
[?CT(<<"Username:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"text">>, <<"username">>, <<"">>,
|
|
|
|
<<"20">>)]),
|
|
|
|
?XE(<<"li">>,
|
2016-02-04 21:31:16 +01:00
|
|
|
[?CT(<<"Server:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"text">>, <<"host">>, Host, <<"20">>)]),
|
2013-03-14 10:33:02 +01:00
|
|
|
?XE(<<"li">>,
|
|
|
|
[?CT(<<"Password:">>), ?C(<<" ">>),
|
|
|
|
?INPUTS(<<"password">>, <<"password">>, <<"">>,
|
|
|
|
<<"20">>)]),
|
|
|
|
?XE(<<"li">>,
|
|
|
|
[?INPUTT(<<"submit">>, <<"unregister">>,
|
|
|
|
<<"Unregister">>)])])])],
|
2010-11-04 22:34:18 +01:00
|
|
|
{200,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{<<"Server">>, <<"ejabberd">>},
|
|
|
|
{<<"Content-Type">>, <<"text/html">>}],
|
2010-11-04 22:34:18 +01:00
|
|
|
ejabberd_web:make_xhtml(HeadEls, Els)}.
|
|
|
|
|
|
|
|
%% @spec(Username, Host, Password) -> {success, ok, {Username, Host, Password} |
|
|
|
|
%% {success, exists, {Username, Host, Password}} |
|
|
|
|
%% {error, not_allowed} |
|
|
|
|
%% {error, invalid_jid}
|
|
|
|
register_account(Username, Host, Password) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
Access = gen_mod:get_module_opt(Host, mod_register, access),
|
2017-02-25 08:01:01 +01:00
|
|
|
case jid:make(Username, Host) of
|
2014-04-24 11:15:39 +02:00
|
|
|
error -> {error, invalid_jid};
|
|
|
|
JID ->
|
|
|
|
case acl:match_rule(Host, Access, JID) of
|
|
|
|
deny -> {error, not_allowed};
|
|
|
|
allow -> register_account2(Username, Host, Password)
|
|
|
|
end
|
2011-07-27 12:25:51 +02:00
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2011-07-27 12:25:51 +02:00
|
|
|
register_account2(Username, Host, Password) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case ejabberd_auth:try_register(Username, Host,
|
|
|
|
Password)
|
|
|
|
of
|
2017-05-11 13:37:21 +02:00
|
|
|
ok ->
|
|
|
|
{success, ok, {Username, Host, Password}};
|
2013-03-14 10:33:02 +01:00
|
|
|
Other -> Other
|
2010-11-04 22:34:18 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Formulary delete POST
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-02-04 21:31:16 +01:00
|
|
|
form_del_post(Q) ->
|
2010-11-04 22:34:18 +01:00
|
|
|
case catch get_unregister_parameters(Q) of
|
2016-02-04 21:31:16 +01:00
|
|
|
[Username, Host, Password] ->
|
2013-03-14 10:33:02 +01:00
|
|
|
try_unregister_account(Username, Host, Password);
|
|
|
|
_ -> {error, wrong_parameters}
|
2010-11-04 22:34:18 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_unregister_parameters(Q) ->
|
|
|
|
%% @spec(Username, Host, Password) -> {atomic, ok} |
|
|
|
|
%% {error, account_doesnt_exist} |
|
|
|
|
%% {error, account_exists} |
|
|
|
|
%% {error, password_incorrect}
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:map(fun (Key) ->
|
|
|
|
{value, {_Key, Value}} = lists:keysearch(Key, 1, Q),
|
|
|
|
Value
|
|
|
|
end,
|
2016-02-04 21:31:16 +01:00
|
|
|
[<<"username">>, <<"host">>, <<"password">>]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-11-04 22:34:18 +01:00
|
|
|
try_unregister_account(Username, Host, Password) ->
|
|
|
|
try unregister_account(Username, Host, Password) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, ok} -> {atomic, ok}
|
2010-11-04 22:34:18 +01:00
|
|
|
catch
|
2013-03-14 10:33:02 +01:00
|
|
|
error:{badmatch, Error} -> {error, Error}
|
2010-11-04 22:34:18 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
unregister_account(Username, Host, Password) ->
|
|
|
|
account_exists = check_account_exists(Username, Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
password_correct = check_password(Username, Host,
|
|
|
|
Password),
|
|
|
|
ok = ejabberd_auth:remove_user(Username, Host,
|
|
|
|
Password),
|
|
|
|
account_doesnt_exist = check_account_exists(Username,
|
|
|
|
Host),
|
2010-11-04 22:34:18 +01:00
|
|
|
{atomic, ok}.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Error texts
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
get_error_text({error, captcha_non_valid}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"The captcha you entered is wrong">>;
|
2011-07-27 11:44:35 +02:00
|
|
|
get_error_text({success, exists, _}) ->
|
|
|
|
get_error_text({atomic, exists});
|
2010-11-04 22:34:18 +01:00
|
|
|
get_error_text({atomic, exists}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"The account already exists">>;
|
2010-11-04 22:34:18 +01:00
|
|
|
get_error_text({error, password_incorrect}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"Incorrect password">>;
|
2010-11-04 22:34:18 +01:00
|
|
|
get_error_text({error, invalid_jid}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"The username is not valid">>;
|
2010-11-04 22:34:18 +01:00
|
|
|
get_error_text({error, not_allowed}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"Not allowed">>;
|
2010-11-04 22:34:18 +01:00
|
|
|
get_error_text({error, account_doesnt_exist}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"Account doesn't exist">>;
|
2010-11-04 22:34:18 +01:00
|
|
|
get_error_text({error, account_exists}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"The account was not deleted">>;
|
2010-11-04 22:34:18 +01:00
|
|
|
get_error_text({error, password_not_changed}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"The password was not changed">>;
|
2010-11-04 22:34:18 +01:00
|
|
|
get_error_text({error, passwords_not_identical}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"The passwords are different">>;
|
2010-11-04 22:34:18 +01:00
|
|
|
get_error_text({error, wrong_parameters}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"Wrong parameters in the web formulary">>.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_options(_) ->
|
|
|
|
[].
|