2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_auth_internal.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2004-12-12 22:00:34 +01:00
|
|
|
%%% Purpose : Authentification via mnesia
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 12 Dec 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2015 ProcessOne
|
2007-12-24 12:41:41 +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 12:41:41 +01:00
|
|
|
%%%
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_auth_internal).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-behaviour(ejabberd_auth).
|
|
|
|
|
|
|
|
-export([start/1, set_password/3, check_password/3,
|
|
|
|
check_password/5, try_register/3,
|
|
|
|
dirty_get_registered_users/0, get_vh_registered_users/1,
|
2007-11-16 14:58:00 +01:00
|
|
|
get_vh_registered_users/2,
|
|
|
|
get_vh_registered_users_number/1,
|
2013-03-14 10:33:02 +01:00
|
|
|
get_vh_registered_users_number/2, get_password/2,
|
|
|
|
get_password_s/2, is_user_exists/2, remove_user/2,
|
2013-07-21 12:24:36 +02:00
|
|
|
remove_user/3, store_type/0, export/1, import/1,
|
2015-06-01 14:38:27 +02:00
|
|
|
import/3, plain_password_required/0, opt_type/1]).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(passwd, {us = {<<"">>, <<"">>} :: {binary(), binary()} | '$1',
|
|
|
|
password = <<"">> :: binary() | scram() | '_'}).
|
|
|
|
|
|
|
|
-record(reg_users_counter, {vhost = <<"">> :: binary(),
|
|
|
|
count = 0 :: integer() | '$1'}).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2011-08-16 00:26:49 +02:00
|
|
|
-define(SALT_LENGTH, 16).
|
|
|
|
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-07-17 21:05:55 +02:00
|
|
|
start(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:create_table(passwd,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, passwd)}]),
|
2009-07-17 21:05:55 +02:00
|
|
|
mnesia:create_table(reg_users_counter,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, reg_users_counter)}]),
|
2005-04-17 20:08:34 +02:00
|
|
|
update_table(),
|
2009-07-17 21:05:55 +02:00
|
|
|
update_reg_users_counter_table(Host),
|
2011-08-16 00:26:49 +02:00
|
|
|
maybe_alert_password_scrammed_without_option(),
|
2004-12-12 22:00:34 +01:00
|
|
|
ok.
|
|
|
|
|
2009-07-17 21:05:55 +02:00
|
|
|
update_reg_users_counter_table(Server) ->
|
|
|
|
Set = get_vh_registered_users(Server),
|
|
|
|
Size = length(Set),
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
|
|
|
mnesia:write(#reg_users_counter{vhost = LServer,
|
|
|
|
count = Size})
|
2010-02-15 23:34:33 +01:00
|
|
|
end,
|
|
|
|
mnesia:sync_dirty(F).
|
2009-07-17 21:05:55 +02:00
|
|
|
|
2004-12-12 22:00:34 +01:00
|
|
|
plain_password_required() ->
|
2015-01-09 16:19:46 +01:00
|
|
|
is_scrammed().
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2011-08-16 00:26:49 +02:00
|
|
|
store_type() ->
|
|
|
|
case is_scrammed() of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> plain; %% allows: PLAIN DIGEST-MD5 SCRAM
|
|
|
|
true -> scram %% allows: PLAIN SCRAM
|
2011-08-16 00:26:49 +02:00
|
|
|
end.
|
2011-08-16 00:25:03 +02:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
check_password(User, Server, Password) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read({passwd, US}) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[#passwd{password = Password}]
|
|
|
|
when is_binary(Password) ->
|
|
|
|
Password /= <<"">>;
|
|
|
|
[#passwd{password = Scram}]
|
|
|
|
when is_record(Scram, scram) ->
|
|
|
|
is_password_scram_valid(Password, Scram);
|
|
|
|
_ -> false
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
check_password(User, Server, Password, Digest,
|
|
|
|
DigestGen) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read({passwd, US}) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[#passwd{password = Passwd}] when is_binary(Passwd) ->
|
|
|
|
DigRes = if Digest /= <<"">> ->
|
|
|
|
Digest == DigestGen(Passwd);
|
|
|
|
true -> false
|
|
|
|
end,
|
|
|
|
if DigRes -> true;
|
|
|
|
true -> (Passwd == Password) and (Password /= <<"">>)
|
|
|
|
end;
|
|
|
|
[#passwd{password = Scram}]
|
|
|
|
when is_record(Scram, scram) ->
|
|
|
|
Passwd = jlib:decode_base64(Scram#scram.storedkey),
|
|
|
|
DigRes = if Digest /= <<"">> ->
|
|
|
|
Digest == DigestGen(Passwd);
|
|
|
|
true -> false
|
|
|
|
end,
|
|
|
|
if DigRes -> true;
|
|
|
|
true -> (Passwd == Password) and (Password /= <<"">>)
|
|
|
|
end;
|
|
|
|
_ -> false
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2008-08-18 20:21:10 +02:00
|
|
|
%% @spec (User::string(), Server::string(), Password::string()) ->
|
|
|
|
%% ok | {error, invalid_jid}
|
2005-04-17 20:08:34 +02:00
|
|
|
set_password(User, Server, Password) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
if (LUser == error) or (LServer == error) ->
|
|
|
|
{error, invalid_jid};
|
|
|
|
true ->
|
|
|
|
F = fun () ->
|
|
|
|
Password2 = case is_scrammed() and is_binary(Password)
|
|
|
|
of
|
|
|
|
true -> password_to_scram(Password);
|
|
|
|
false -> Password
|
|
|
|
end,
|
|
|
|
mnesia:write(#passwd{us = US, password = Password2})
|
|
|
|
end,
|
|
|
|
{atomic, ok} = mnesia:transaction(F),
|
|
|
|
ok
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2015-01-09 16:19:46 +01:00
|
|
|
%% @spec (User, Server, Password) -> {atomic, ok} | {atomic, exists} | {error, invalid_jid} | {error, not_allowed} | {error, Reason}
|
2013-03-14 10:33:02 +01:00
|
|
|
try_register(User, Server, PasswordList) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2015-02-05 16:44:08 +01:00
|
|
|
Password = if is_list(PasswordList); is_binary(PasswordList) ->
|
|
|
|
iolist_to_binary(PasswordList);
|
|
|
|
true -> PasswordList
|
|
|
|
end,
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
if (LUser == error) or (LServer == error) ->
|
|
|
|
{error, invalid_jid};
|
|
|
|
true ->
|
|
|
|
F = fun () ->
|
|
|
|
case mnesia:read({passwd, US}) of
|
|
|
|
[] ->
|
|
|
|
Password2 = case is_scrammed() and
|
|
|
|
is_binary(Password)
|
|
|
|
of
|
|
|
|
true -> password_to_scram(Password);
|
|
|
|
false -> Password
|
|
|
|
end,
|
|
|
|
mnesia:write(#passwd{us = US,
|
|
|
|
password = Password2}),
|
|
|
|
mnesia:dirty_update_counter(reg_users_counter,
|
|
|
|
LServer, 1),
|
|
|
|
ok;
|
|
|
|
[_E] -> exists
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2008-02-19 14:49:29 +01:00
|
|
|
%% Get all registered users in Mnesia
|
2004-12-12 22:00:34 +01:00
|
|
|
dirty_get_registered_users() ->
|
|
|
|
mnesia:dirty_all_keys(passwd).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_registered_users(Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:dirty_select(passwd,
|
|
|
|
[{#passwd{us = '$1', _ = '_'},
|
|
|
|
[{'==', {element, 2, '$1'}, LServer}], ['$1']}]).
|
|
|
|
|
|
|
|
get_vh_registered_users(Server,
|
|
|
|
[{from, Start}, {to, End}])
|
|
|
|
when is_integer(Start) and is_integer(End) ->
|
|
|
|
get_vh_registered_users(Server,
|
|
|
|
[{limit, End - Start + 1}, {offset, Start}]);
|
|
|
|
get_vh_registered_users(Server,
|
|
|
|
[{limit, Limit}, {offset, Offset}])
|
|
|
|
when is_integer(Limit) and is_integer(Offset) ->
|
2007-11-16 14:58:00 +01:00
|
|
|
case get_vh_registered_users(Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] -> [];
|
|
|
|
Users ->
|
|
|
|
Set = lists:keysort(1, Users),
|
|
|
|
L = length(Set),
|
|
|
|
Start = if Offset < 1 -> 1;
|
|
|
|
Offset > L -> L;
|
|
|
|
true -> Offset
|
|
|
|
end,
|
|
|
|
lists:sublist(Set, Start, Limit)
|
2007-11-16 14:58:00 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_vh_registered_users(Server, [{prefix, Prefix}])
|
|
|
|
when is_binary(Prefix) ->
|
|
|
|
Set = [{U, S}
|
|
|
|
|| {U, S} <- get_vh_registered_users(Server),
|
|
|
|
str:prefix(Prefix, U)],
|
2007-11-16 14:58:00 +01:00
|
|
|
lists:keysort(1, Set);
|
2013-03-14 10:33:02 +01:00
|
|
|
get_vh_registered_users(Server,
|
|
|
|
[{prefix, Prefix}, {from, Start}, {to, End}])
|
|
|
|
when is_binary(Prefix) and is_integer(Start) and
|
|
|
|
is_integer(End) ->
|
|
|
|
get_vh_registered_users(Server,
|
|
|
|
[{prefix, Prefix}, {limit, End - Start + 1},
|
|
|
|
{offset, Start}]);
|
|
|
|
get_vh_registered_users(Server,
|
|
|
|
[{prefix, Prefix}, {limit, Limit}, {offset, Offset}])
|
|
|
|
when is_binary(Prefix) and is_integer(Limit) and
|
|
|
|
is_integer(Offset) ->
|
|
|
|
case [{U, S}
|
|
|
|
|| {U, S} <- get_vh_registered_users(Server),
|
|
|
|
str:prefix(Prefix, U)]
|
|
|
|
of
|
|
|
|
[] -> [];
|
|
|
|
Users ->
|
|
|
|
Set = lists:keysort(1, Users),
|
|
|
|
L = length(Set),
|
|
|
|
Start = if Offset < 1 -> 1;
|
|
|
|
Offset > L -> L;
|
|
|
|
true -> Offset
|
|
|
|
end,
|
|
|
|
lists:sublist(Set, Start, Limit)
|
2007-11-16 14:58:00 +01:00
|
|
|
end;
|
|
|
|
get_vh_registered_users(Server, _) ->
|
|
|
|
get_vh_registered_users(Server).
|
|
|
|
|
|
|
|
get_vh_registered_users_number(Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
Query = mnesia:dirty_select(reg_users_counter,
|
|
|
|
[{#reg_users_counter{vhost = LServer,
|
|
|
|
count = '$1'},
|
|
|
|
[], ['$1']}]),
|
2009-07-17 21:05:55 +02:00
|
|
|
case Query of
|
2013-03-14 10:33:02 +01:00
|
|
|
[Count] -> Count;
|
|
|
|
_ -> 0
|
2009-07-17 21:05:55 +02:00
|
|
|
end.
|
2007-11-16 14:58:00 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_vh_registered_users_number(Server,
|
|
|
|
[{prefix, Prefix}])
|
|
|
|
when is_binary(Prefix) ->
|
|
|
|
Set = [{U, S}
|
|
|
|
|| {U, S} <- get_vh_registered_users(Server),
|
|
|
|
str:prefix(Prefix, U)],
|
2007-11-16 14:58:00 +01:00
|
|
|
length(Set);
|
|
|
|
get_vh_registered_users_number(Server, _) ->
|
|
|
|
get_vh_registered_users_number(Server).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read(passwd, US) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[#passwd{password = Password}]
|
|
|
|
when is_binary(Password) ->
|
|
|
|
Password;
|
|
|
|
[#passwd{password = Scram}]
|
|
|
|
when is_record(Scram, scram) ->
|
|
|
|
{jlib:decode_base64(Scram#scram.storedkey),
|
|
|
|
jlib:decode_base64(Scram#scram.serverkey),
|
|
|
|
jlib:decode_base64(Scram#scram.salt),
|
|
|
|
Scram#scram.iterationcount};
|
|
|
|
_ -> false
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password_s(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read(passwd, US) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[#passwd{password = Password}]
|
|
|
|
when is_binary(Password) ->
|
|
|
|
Password;
|
|
|
|
[#passwd{password = Scram}]
|
|
|
|
when is_record(Scram, scram) ->
|
|
|
|
<<"">>;
|
|
|
|
_ -> <<"">>
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2009-03-04 19:34:02 +01:00
|
|
|
%% @spec (User, Server) -> true | false | {error, Error}
|
2005-04-17 20:08:34 +02:00
|
|
|
is_user_exists(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read({passwd, US}) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] -> false;
|
|
|
|
[_] -> true;
|
|
|
|
Other -> {error, Other}
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2008-12-23 20:15:33 +01:00
|
|
|
%% @spec (User, Server) -> ok
|
2009-01-03 22:29:54 +01:00
|
|
|
%% @doc Remove user.
|
2008-12-23 20:15:33 +01:00
|
|
|
%% Note: it returns ok even if there was some problem removing the user.
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2009-07-17 21:05:55 +02:00
|
|
|
mnesia:delete({passwd, US}),
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:dirty_update_counter(reg_users_counter, LServer,
|
|
|
|
-1)
|
|
|
|
end,
|
2004-12-12 22:00:34 +01:00
|
|
|
mnesia:transaction(F),
|
2013-03-14 10:33:02 +01:00
|
|
|
ok.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2008-12-23 20:15:33 +01:00
|
|
|
%% @spec (User, Server, Password) -> ok | not_exists | not_allowed | bad_request
|
2009-01-03 22:29:54 +01:00
|
|
|
%% @doc Remove user if the provided password is correct.
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server, Password) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case mnesia:read({passwd, US}) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[#passwd{password = Password}]
|
|
|
|
when is_binary(Password) ->
|
|
|
|
mnesia:delete({passwd, US}),
|
|
|
|
mnesia:dirty_update_counter(reg_users_counter, LServer,
|
|
|
|
-1),
|
|
|
|
ok;
|
|
|
|
[#passwd{password = Scram}]
|
|
|
|
when is_record(Scram, scram) ->
|
|
|
|
case is_password_scram_valid(Password, Scram) of
|
|
|
|
true ->
|
|
|
|
mnesia:delete({passwd, US}),
|
|
|
|
mnesia:dirty_update_counter(reg_users_counter,
|
|
|
|
LServer, -1),
|
|
|
|
ok;
|
|
|
|
false -> not_allowed
|
|
|
|
end;
|
|
|
|
_ -> not_exists
|
2004-12-12 22:00:34 +01:00
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2004-12-12 22:00:34 +01:00
|
|
|
case mnesia:transaction(F) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, ok} -> ok;
|
|
|
|
{atomic, Res} -> Res;
|
|
|
|
_ -> bad_request
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, passwd),
|
|
|
|
case mnesia:table_info(passwd, attributes) of
|
2013-03-14 10:33:02 +01:00
|
|
|
Fields ->
|
|
|
|
convert_to_binary(Fields),
|
|
|
|
maybe_scram_passwords(),
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating passwd table", []),
|
|
|
|
mnesia:transform_table(passwd, ignore, Fields)
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
convert_to_binary(Fields) ->
|
|
|
|
ejabberd_config:convert_table_to_binary(
|
|
|
|
passwd, Fields, set,
|
|
|
|
fun(#passwd{us = {U, _}}) -> U end,
|
|
|
|
fun(#passwd{us = {U, S}, password = Pass} = R) ->
|
|
|
|
NewUS = {iolist_to_binary(U), iolist_to_binary(S)},
|
|
|
|
NewPass = case Pass of
|
|
|
|
#scram{storedkey = StoredKey,
|
|
|
|
serverkey = ServerKey,
|
|
|
|
salt = Salt} ->
|
|
|
|
Pass#scram{
|
|
|
|
storedkey = iolist_to_binary(StoredKey),
|
|
|
|
serverkey = iolist_to_binary(ServerKey),
|
|
|
|
salt = iolist_to_binary(Salt)};
|
|
|
|
_ ->
|
|
|
|
iolist_to_binary(Pass)
|
|
|
|
end,
|
|
|
|
R#passwd{us = NewUS, password = NewPass}
|
|
|
|
end).
|
|
|
|
|
2011-08-16 00:26:49 +02:00
|
|
|
%%%
|
|
|
|
%%% SCRAM
|
|
|
|
%%%
|
|
|
|
|
|
|
|
%% The passwords are stored scrammed in the table either if the option says so,
|
|
|
|
%% or if at least the first password is scrammed.
|
|
|
|
is_scrammed() ->
|
|
|
|
OptionScram = is_option_scram(),
|
2013-03-14 10:33:02 +01:00
|
|
|
FirstElement = mnesia:dirty_read(passwd,
|
|
|
|
mnesia:dirty_first(passwd)),
|
2011-08-16 00:26:49 +02:00
|
|
|
case {OptionScram, FirstElement} of
|
2013-03-14 10:33:02 +01:00
|
|
|
{true, _} -> true;
|
|
|
|
{false, [#passwd{password = Scram}]}
|
|
|
|
when is_record(Scram, scram) ->
|
|
|
|
true;
|
|
|
|
_ -> false
|
2011-08-16 00:26:49 +02:00
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2011-08-16 00:26:49 +02:00
|
|
|
is_option_scram() ->
|
2013-03-14 10:33:02 +01:00
|
|
|
scram ==
|
2013-08-12 14:25:05 +02:00
|
|
|
ejabberd_config:get_option({auth_password_format, ?MYNAME},
|
2013-03-14 10:33:02 +01:00
|
|
|
fun(V) -> V end).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2011-08-16 00:26:49 +02:00
|
|
|
maybe_alert_password_scrammed_without_option() ->
|
|
|
|
case is_scrammed() andalso not is_option_scram() of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
?ERROR_MSG("Some passwords were stored in the database "
|
|
|
|
"as SCRAM, but 'auth_password_format' "
|
|
|
|
"is not configured 'scram'. The option "
|
|
|
|
"will now be considered to be 'scram'.",
|
|
|
|
[]);
|
|
|
|
false -> ok
|
2011-08-16 00:26:49 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
maybe_scram_passwords() ->
|
|
|
|
case is_scrammed() of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> scram_passwords();
|
|
|
|
false -> ok
|
2011-08-16 00:26:49 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
scram_passwords() ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("Converting the stored passwords into "
|
|
|
|
"SCRAM bits",
|
|
|
|
[]),
|
|
|
|
Fun = fun (#passwd{password = Password} = P) ->
|
2011-08-16 00:26:49 +02:00
|
|
|
Scram = password_to_scram(Password),
|
|
|
|
P#passwd{password = Scram}
|
|
|
|
end,
|
|
|
|
Fields = record_info(fields, passwd),
|
|
|
|
mnesia:transform_table(passwd, Fun, Fields).
|
|
|
|
|
|
|
|
password_to_scram(Password) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
password_to_scram(Password,
|
|
|
|
?SCRAM_DEFAULT_ITERATION_COUNT).
|
2011-08-16 00:26:49 +02:00
|
|
|
|
|
|
|
password_to_scram(Password, IterationCount) ->
|
|
|
|
Salt = crypto:rand_bytes(?SALT_LENGTH),
|
2013-03-14 10:33:02 +01:00
|
|
|
SaltedPassword = scram:salted_password(Password, Salt,
|
|
|
|
IterationCount),
|
|
|
|
StoredKey =
|
|
|
|
scram:stored_key(scram:client_key(SaltedPassword)),
|
2011-08-16 00:26:49 +02:00
|
|
|
ServerKey = scram:server_key(SaltedPassword),
|
2013-03-14 10:33:02 +01:00
|
|
|
#scram{storedkey = jlib:encode_base64(StoredKey),
|
|
|
|
serverkey = jlib:encode_base64(ServerKey),
|
|
|
|
salt = jlib:encode_base64(Salt),
|
2011-08-16 00:26:49 +02:00
|
|
|
iterationcount = IterationCount}.
|
|
|
|
|
|
|
|
is_password_scram_valid(Password, Scram) ->
|
|
|
|
IterationCount = Scram#scram.iterationcount,
|
2013-03-14 10:33:02 +01:00
|
|
|
Salt = jlib:decode_base64(Scram#scram.salt),
|
|
|
|
SaltedPassword = scram:salted_password(Password, Salt,
|
|
|
|
IterationCount),
|
|
|
|
StoredKey =
|
|
|
|
scram:stored_key(scram:client_key(SaltedPassword)),
|
|
|
|
jlib:decode_base64(Scram#scram.storedkey) == StoredKey.
|
|
|
|
|
|
|
|
export(_Server) ->
|
|
|
|
[{passwd,
|
|
|
|
fun(Host, #passwd{us = {LUser, LServer}, password = Password})
|
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
Pass = ejabberd_odbc:escape(Password),
|
|
|
|
[[<<"delete from users where username='">>, Username, <<"';">>],
|
|
|
|
[<<"insert into users(username, password) "
|
|
|
|
"values ('">>, Username, <<"', '">>, Pass, <<"');">>]];
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
2013-07-21 12:24:36 +02:00
|
|
|
|
|
|
|
import(LServer) ->
|
|
|
|
[{<<"select username, password from users;">>,
|
|
|
|
fun([LUser, Password]) ->
|
|
|
|
#passwd{us = {LUser, LServer}, password = Password}
|
|
|
|
end}].
|
|
|
|
|
|
|
|
import(_LServer, mnesia, #passwd{} = P) ->
|
|
|
|
mnesia:dirty_write(P);
|
|
|
|
import(_, _, _) ->
|
|
|
|
pass.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
opt_type(auth_password_format) -> fun (V) -> V end;
|
|
|
|
opt_type(_) -> [auth_password_format].
|