2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2016-04-27 08:44:32 +02:00
|
|
|
%%% File : ejabberd_auth_mnesia.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2019-07-16 21:07:39 +02:00
|
|
|
%%% Purpose : Authentication via mnesia
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 12 Dec 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 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
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-04-27 08:44:32 +02:00
|
|
|
-module(ejabberd_auth_mnesia).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
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).
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
-export([start/1, stop/1, set_password/3, try_register/3,
|
|
|
|
get_users/2, init_db/0,
|
|
|
|
count_users/2, get_password/2,
|
2017-10-25 20:21:52 +02:00
|
|
|
remove_user/2, store_type/1, import/2,
|
2017-05-11 13:37:21 +02:00
|
|
|
plain_password_required/1, use_cache/1]).
|
2017-04-23 15:37:58 +02:00
|
|
|
-export([need_transform/1, transform/1]).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/scram.hrl").
|
2017-10-25 20:21:52 +02:00
|
|
|
-include("ejabberd_auth.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-record(reg_users_counter, {vhost = <<"">> :: binary(),
|
|
|
|
count = 0 :: integer() | '$1'}).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-07-17 21:05:55 +02:00
|
|
|
start(Host) ->
|
2016-11-22 17:51:21 +01:00
|
|
|
init_db(),
|
|
|
|
update_reg_users_counter_table(Host),
|
|
|
|
ok.
|
|
|
|
|
2017-02-23 14:19:22 +01:00
|
|
|
stop(_Host) ->
|
|
|
|
ok.
|
|
|
|
|
2016-11-22 17:51:21 +01:00
|
|
|
init_db() ->
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, passwd,
|
2017-05-11 13:37:21 +02:00
|
|
|
[{disc_only_copies, [node()]},
|
2013-03-14 10:33:02 +01:00
|
|
|
{attributes, record_info(fields, passwd)}]),
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, reg_users_counter,
|
2009-07-17 21:05:55 +02:00
|
|
|
[{ram_copies, [node()]},
|
2016-11-22 17:51:21 +01:00
|
|
|
{attributes, record_info(fields, reg_users_counter)}]).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2009-07-17 21:05:55 +02:00
|
|
|
update_reg_users_counter_table(Server) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
Set = get_users(Server, []),
|
2009-07-17 21:05:55 +02:00
|
|
|
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
|
|
|
|
2017-05-15 07:58:37 +02:00
|
|
|
use_cache(Host) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case mnesia:table_info(passwd, storage_type) of
|
2017-05-15 07:58:37 +02:00
|
|
|
disc_only_copies ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:auth_use_cache(Host);
|
2017-05-15 07:58:37 +02:00
|
|
|
_ ->
|
|
|
|
false
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
plain_password_required(Server) ->
|
|
|
|
store_type(Server) == scram.
|
|
|
|
|
|
|
|
store_type(Server) ->
|
|
|
|
ejabberd_auth:password_format(Server).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
set_password(User, Server, Password) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
US = {User, Server},
|
|
|
|
F = fun () ->
|
|
|
|
mnesia:write(#passwd{us = US, password = Password})
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, ok} ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{cache, {ok, Password}};
|
2017-05-11 13:37:21 +02:00
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("Mnesia transaction failed: ~p", [Reason]),
|
2019-06-30 16:15:43 +02:00
|
|
|
{nocache, {error, db_failure}}
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
try_register(User, Server, Password) ->
|
|
|
|
US = {User, Server},
|
|
|
|
F = fun () ->
|
|
|
|
case mnesia:read({passwd, US}) of
|
|
|
|
[] ->
|
|
|
|
mnesia:write(#passwd{us = US, password = Password}),
|
|
|
|
mnesia:dirty_update_counter(reg_users_counter, Server, 1),
|
2019-06-30 16:15:43 +02:00
|
|
|
{ok, Password};
|
2017-05-11 13:37:21 +02:00
|
|
|
[_] ->
|
|
|
|
{error, exists}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Res} ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{cache, Res};
|
2017-05-11 13:37:21 +02:00
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("Mnesia transaction failed: ~p", [Reason]),
|
2019-06-30 16:15:43 +02:00
|
|
|
{nocache, {error, db_failure}}
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
get_users(Server, []) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:dirty_select(passwd,
|
|
|
|
[{#passwd{us = '$1', _ = '_'},
|
2017-05-11 13:37:21 +02:00
|
|
|
[{'==', {element, 2, '$1'}, Server}], ['$1']}]);
|
|
|
|
get_users(Server, [{from, Start}, {to, End}])
|
|
|
|
when is_integer(Start) and is_integer(End) ->
|
|
|
|
get_users(Server, [{limit, End - Start + 1}, {offset, Start}]);
|
|
|
|
get_users(Server, [{limit, Limit}, {offset, Offset}])
|
|
|
|
when is_integer(Limit) and is_integer(Offset) ->
|
|
|
|
case get_users(Server, []) 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;
|
2017-05-11 13:37:21 +02:00
|
|
|
get_users(Server, [{prefix, Prefix}]) when is_binary(Prefix) ->
|
|
|
|
Set = [{U, S} || {U, S} <- get_users(Server, []), str:prefix(Prefix, U)],
|
2007-11-16 14:58:00 +01:00
|
|
|
lists:keysort(1, Set);
|
2017-05-11 13:37:21 +02:00
|
|
|
get_users(Server, [{prefix, Prefix}, {from, Start}, {to, End}])
|
|
|
|
when is_binary(Prefix) and is_integer(Start) and is_integer(End) ->
|
|
|
|
get_users(Server, [{prefix, Prefix}, {limit, End - Start + 1},
|
|
|
|
{offset, Start}]);
|
|
|
|
get_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_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;
|
2017-05-11 13:37:21 +02:00
|
|
|
get_users(Server, _) ->
|
|
|
|
get_users(Server, []).
|
|
|
|
|
|
|
|
count_users(Server, []) ->
|
|
|
|
case mnesia:dirty_select(
|
|
|
|
reg_users_counter,
|
|
|
|
[{#reg_users_counter{vhost = Server, count = '$1'},
|
|
|
|
[], ['$1']}]) of
|
|
|
|
[Count] -> Count;
|
|
|
|
_ -> 0
|
|
|
|
end;
|
|
|
|
count_users(Server, [{prefix, Prefix}]) when is_binary(Prefix) ->
|
|
|
|
Set = [{U, S} || {U, S} <- get_users(Server, []), str:prefix(Prefix, U)],
|
2007-11-16 14:58:00 +01:00
|
|
|
length(Set);
|
2017-05-11 13:37:21 +02:00
|
|
|
count_users(Server, _) ->
|
|
|
|
count_users(Server, []).
|
2007-11-16 14:58:00 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password(User, Server) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case mnesia:dirty_read(passwd, {User, Server}) of
|
2020-12-17 17:52:25 +01:00
|
|
|
[{passwd, _, {scram, SK, SEK, Salt, IC}}] ->
|
2020-12-08 12:18:03 +01:00
|
|
|
{cache, {ok, #scram{storedkey = SK, serverkey = SEK,
|
|
|
|
salt = Salt, hash = sha, iterationcount = IC}}};
|
2017-05-11 13:37:21 +02:00
|
|
|
[#passwd{password = Password}] ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{cache, {ok, Password}};
|
2017-05-11 13:37:21 +02:00
|
|
|
_ ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{cache, error}
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
US = {User, Server},
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2009-07-17 21:05:55 +02:00
|
|
|
mnesia:delete({passwd, US}),
|
2017-05-11 13:37:21 +02:00
|
|
|
mnesia:dirty_update_counter(reg_users_counter, Server, -1),
|
|
|
|
ok
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2004-12-12 22:00:34 +01:00
|
|
|
case mnesia:transaction(F) of
|
2017-05-11 13:37:21 +02:00
|
|
|
{atomic, ok} ->
|
|
|
|
ok;
|
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("Mnesia transaction failed: ~p", [Reason]),
|
|
|
|
{error, db_failure}
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2017-08-02 14:36:32 +02:00
|
|
|
need_transform(#reg_users_counter{}) ->
|
|
|
|
false;
|
2019-06-14 11:33:26 +02:00
|
|
|
need_transform({passwd, {U, S}, Pass}) ->
|
2020-12-10 14:00:13 +01:00
|
|
|
case Pass of
|
|
|
|
_ when is_binary(Pass) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case store_type(S) of
|
|
|
|
scram ->
|
2017-04-23 15:37:58 +02:00
|
|
|
?INFO_MSG("Passwords in Mnesia table 'passwd' "
|
2017-05-11 13:37:21 +02:00
|
|
|
"will be SCRAM'ed", []),
|
|
|
|
true;
|
|
|
|
plain ->
|
|
|
|
false
|
|
|
|
end;
|
2020-12-10 14:00:13 +01:00
|
|
|
{scram, _, _, _, _} ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case store_type(S) of
|
|
|
|
scram ->
|
|
|
|
false;
|
|
|
|
plain ->
|
2017-04-23 15:37:58 +02:00
|
|
|
?WARNING_MSG("Some passwords were stored in the database "
|
|
|
|
"as SCRAM, but 'auth_password_format' "
|
2017-05-11 13:37:21 +02:00
|
|
|
"is not configured as 'scram': some "
|
|
|
|
"authentication mechanisms such as DIGEST-MD5 "
|
|
|
|
"would *fail*", []),
|
2017-04-23 15:37:58 +02:00
|
|
|
false
|
|
|
|
end;
|
2020-12-10 14:00:13 +01:00
|
|
|
#scram{} ->
|
|
|
|
case store_type(S) of
|
|
|
|
scram ->
|
|
|
|
false;
|
|
|
|
plain ->
|
|
|
|
?WARNING_MSG("Some passwords were stored in the database "
|
|
|
|
"as SCRAM, but 'auth_password_format' "
|
|
|
|
"is not configured as 'scram': some "
|
|
|
|
"authentication mechanisms such as DIGEST-MD5 "
|
|
|
|
"would *fail*", []),
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ when is_list(U) orelse is_list(S) orelse is_list(Pass) ->
|
2017-04-23 15:37:58 +02:00
|
|
|
?INFO_MSG("Mnesia table 'passwd' will be converted to binary", []),
|
|
|
|
true
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
transform({passwd, {U, S}, Pass})
|
2017-04-23 15:37:58 +02:00
|
|
|
when is_list(U) orelse is_list(S) orelse is_list(Pass) ->
|
|
|
|
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,
|
2019-06-14 11:33:26 +02:00
|
|
|
transform(#passwd{us = NewUS, password = NewPass});
|
2017-04-23 15:37:58 +02:00
|
|
|
transform(#passwd{us = {U, S}, password = Password} = P)
|
|
|
|
when is_binary(Password) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case store_type(S) of
|
|
|
|
scram ->
|
2017-04-23 15:37:58 +02:00
|
|
|
case jid:resourceprep(Password) of
|
|
|
|
error ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("SASLprep failed for password of user ~ts@~ts",
|
2017-04-23 15:37:58 +02:00
|
|
|
[U, S]),
|
|
|
|
P;
|
|
|
|
_ ->
|
2020-12-10 14:00:13 +01:00
|
|
|
Scram = ejabberd_auth:password_to_scram(S, Password),
|
2017-04-23 15:37:58 +02:00
|
|
|
P#passwd{password = Scram}
|
|
|
|
end;
|
2017-05-11 13:37:21 +02:00
|
|
|
plain ->
|
2017-04-23 15:37:58 +02:00
|
|
|
P
|
|
|
|
end;
|
2020-12-17 17:52:25 +01:00
|
|
|
transform({passwd, _, {scram, _, _, _, _}} = P) ->
|
2020-12-10 14:02:28 +01:00
|
|
|
P;
|
|
|
|
transform(#passwd{password = #scram{}} = P) ->
|
2017-04-23 15:37:58 +02:00
|
|
|
P.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(LServer, [LUser, Password, _TimeStamp]) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
#passwd{us = {LUser, LServer}, password = Password}).
|