2002-11-23 21:55:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_auth.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
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 23 Nov 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 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
|
|
|
%%%
|
2002-11-23 21:55:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
-module(ejabberd_auth).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-02-24 10:05:47 +01:00
|
|
|
-behaviour(gen_server).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-11-23 21:55:05 +01:00
|
|
|
|
|
|
|
%% External exports
|
2017-02-24 12:06:47 +01:00
|
|
|
-export([start_link/0, host_up/1, host_down/1, config_reloaded/0,
|
|
|
|
set_password/3, check_password/4,
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password/6, check_password_with_authmodule/4,
|
|
|
|
check_password_with_authmodule/6, try_register/3,
|
2017-05-11 13:37:21 +02:00
|
|
|
get_users/0, get_users/1, password_to_scram/1,
|
2017-10-25 20:21:52 +02:00
|
|
|
get_users/2, import_info/0,
|
2017-05-11 13:37:21 +02:00
|
|
|
count_users/1, import/5, import_start/2,
|
|
|
|
count_users/2, get_password/2,
|
2013-03-14 10:33:02 +01:00
|
|
|
get_password_s/2, get_password_with_authmodule/2,
|
2017-05-11 14:49:06 +02:00
|
|
|
user_exists/2, user_exists_in_other_modules/3,
|
2013-03-14 10:33:02 +01:00
|
|
|
remove_user/2, remove_user/3, plain_password_required/1,
|
2018-12-05 14:22:09 +01:00
|
|
|
store_type/1, entropy/1, backend_type/1, password_format/1,
|
|
|
|
which_users_exists/1]).
|
2017-02-24 10:05:47 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-export([auth_modules/1]).
|
2006-04-07 02:39:24 +02:00
|
|
|
|
2018-06-14 13:00:47 +02:00
|
|
|
-include("scram.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2005-04-20 01:10:22 +02:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
-define(SALT_LENGTH, 16).
|
|
|
|
|
2019-06-27 14:22:27 +02:00
|
|
|
-record(state, {host_modules = #{} :: host_modules()}).
|
2017-02-24 10:05:47 +01:00
|
|
|
|
2019-06-27 14:22:27 +02:00
|
|
|
-type host_modules() :: #{binary => [module()]}.
|
2017-05-11 13:37:21 +02:00
|
|
|
-type password() :: binary() | #scram{}.
|
|
|
|
-type digest_fun() :: fun((binary()) -> binary()).
|
2017-02-18 07:36:27 +01:00
|
|
|
-export_type([password/0]).
|
|
|
|
|
2002-11-23 21:55:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
-type opts() :: [{prefix, binary()} | {from, integer()} |
|
|
|
|
{to, integer()} | {limit, integer()} |
|
|
|
|
{offset, integer()}].
|
|
|
|
|
|
|
|
-callback start(binary()) -> any().
|
2017-02-23 14:19:22 +01:00
|
|
|
-callback stop(binary()) -> any().
|
2018-05-07 18:27:18 +02:00
|
|
|
-callback reload(binary()) -> any().
|
2017-05-11 13:37:21 +02:00
|
|
|
-callback plain_password_required(binary()) -> boolean().
|
|
|
|
-callback store_type(binary()) -> plain | external | scram.
|
2019-06-30 16:15:43 +02:00
|
|
|
-callback set_password(binary(), binary(), password()) ->
|
2019-07-07 11:16:04 +02:00
|
|
|
{ets_cache:tag(), {ok, password()} | {error, db_failure | not_allowed}}.
|
2019-06-30 16:15:43 +02:00
|
|
|
-callback remove_user(binary(), binary()) -> ok | {error, db_failure | not_allowed}.
|
|
|
|
-callback user_exists(binary(), binary()) -> {ets_cache:tag(), boolean() | {error, db_failure}}.
|
|
|
|
-callback check_password(binary(), binary(), binary(), binary()) -> {ets_cache:tag(), boolean()}.
|
|
|
|
-callback try_register(binary(), binary(), password()) ->
|
|
|
|
{ets_cache:tag(), {ok, password()} | {error, exists | db_failure | not_allowed}}.
|
2017-05-11 13:37:21 +02:00
|
|
|
-callback get_users(binary(), opts()) -> [{binary(), binary()}].
|
|
|
|
-callback count_users(binary(), opts()) -> number().
|
2019-06-30 16:15:43 +02:00
|
|
|
-callback get_password(binary(), binary()) -> {ets_cache:tag(), {ok, password()} | error}.
|
2017-05-11 13:37:21 +02:00
|
|
|
-callback use_cache(binary()) -> boolean().
|
|
|
|
-callback cache_nodes(binary()) -> boolean().
|
|
|
|
|
2018-05-07 18:27:18 +02:00
|
|
|
-optional_callbacks([reload/1,
|
|
|
|
set_password/3,
|
2017-05-11 13:37:21 +02:00
|
|
|
remove_user/2,
|
2017-05-11 14:49:06 +02:00
|
|
|
user_exists/2,
|
2017-05-11 13:37:21 +02:00
|
|
|
check_password/4,
|
|
|
|
try_register/3,
|
|
|
|
get_users/2,
|
|
|
|
count_users/2,
|
|
|
|
get_password/2,
|
|
|
|
use_cache/1,
|
|
|
|
cache_nodes/1]).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2017-02-24 10:05:47 +01:00
|
|
|
-spec start_link() -> {ok, pid()} | {error, any()}.
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
init([]) ->
|
2017-02-24 12:06:47 +01:00
|
|
|
ejabberd_hooks:add(host_up, ?MODULE, host_up, 30),
|
|
|
|
ejabberd_hooks:add(host_down, ?MODULE, host_down, 80),
|
|
|
|
ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 40),
|
|
|
|
HostModules = lists:foldl(
|
|
|
|
fun(Host, Acc) ->
|
|
|
|
Modules = auth_modules(Host),
|
2017-02-27 20:38:59 +01:00
|
|
|
maps:put(Host, Modules, Acc)
|
2019-06-14 11:33:26 +02:00
|
|
|
end, #{}, ejabberd_option:hosts()),
|
2017-05-11 13:37:21 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({Host, Modules}) ->
|
|
|
|
start(Host, Modules)
|
|
|
|
end, maps:to_list(HostModules)),
|
|
|
|
init_cache(HostModules),
|
2017-02-24 12:06:47 +01:00
|
|
|
{ok, #state{host_modules = HostModules}}.
|
2017-02-24 10:05:47 +01:00
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2017-02-24 10:05:47 +01:00
|
|
|
|
2017-02-24 12:06:47 +01:00
|
|
|
handle_cast({host_up, Host}, #state{host_modules = HostModules} = State) ->
|
|
|
|
Modules = auth_modules(Host),
|
|
|
|
start(Host, Modules),
|
2017-02-27 20:38:59 +01:00
|
|
|
NewHostModules = maps:put(Host, Modules, HostModules),
|
2017-05-11 13:37:21 +02:00
|
|
|
init_cache(NewHostModules),
|
2017-02-24 12:06:47 +01:00
|
|
|
{noreply, State#state{host_modules = NewHostModules}};
|
|
|
|
handle_cast({host_down, Host}, #state{host_modules = HostModules} = State) ->
|
|
|
|
Modules = maps:get(Host, HostModules, []),
|
|
|
|
stop(Host, Modules),
|
|
|
|
NewHostModules = maps:remove(Host, HostModules),
|
2017-05-11 13:37:21 +02:00
|
|
|
init_cache(NewHostModules),
|
2017-02-24 12:06:47 +01:00
|
|
|
{noreply, State#state{host_modules = NewHostModules}};
|
|
|
|
handle_cast(config_reloaded, #state{host_modules = HostModules} = State) ->
|
2018-05-07 18:27:18 +02:00
|
|
|
NewHostModules =
|
|
|
|
lists:foldl(
|
|
|
|
fun(Host, Acc) ->
|
|
|
|
OldModules = maps:get(Host, HostModules, []),
|
|
|
|
NewModules = auth_modules(Host),
|
|
|
|
start(Host, NewModules -- OldModules),
|
|
|
|
stop(Host, OldModules -- NewModules),
|
2018-07-08 13:52:12 +02:00
|
|
|
reload(Host, misc:intersection(OldModules, NewModules)),
|
2018-05-07 18:27:18 +02:00
|
|
|
maps:put(Host, NewModules, Acc)
|
2019-06-14 11:33:26 +02:00
|
|
|
end, HostModules, ejabberd_option:hosts()),
|
2017-05-11 13:37:21 +02:00
|
|
|
init_cache(NewHostModules),
|
2017-02-24 12:06:47 +01:00
|
|
|
{noreply, State#state{host_modules = NewHostModules}};
|
|
|
|
handle_cast(Msg, State) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2017-02-24 10:05:47 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2017-02-24 10:05:47 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2017-02-24 12:06:47 +01:00
|
|
|
terminate(_Reason, State) ->
|
2019-07-29 09:46:20 +02:00
|
|
|
ejabberd_hooks:delete(host_up, ?MODULE, host_up, 30),
|
|
|
|
ejabberd_hooks:delete(host_down, ?MODULE, host_down, 80),
|
2017-02-24 12:06:47 +01:00
|
|
|
ejabberd_hooks:delete(config_reloaded, ?MODULE, config_reloaded, 40),
|
|
|
|
lists:foreach(
|
|
|
|
fun({Host, Modules}) ->
|
|
|
|
stop(Host, Modules)
|
|
|
|
end, maps:to_list(State#state.host_modules)).
|
2017-02-24 10:05:47 +01:00
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
2017-02-23 14:19:22 +01:00
|
|
|
|
2017-02-24 12:06:47 +01:00
|
|
|
start(Host, Modules) ->
|
2017-02-23 14:19:22 +01:00
|
|
|
lists:foreach(fun(M) -> M:start(Host) end, Modules).
|
|
|
|
|
2017-02-24 12:06:47 +01:00
|
|
|
stop(Host, Modules) ->
|
|
|
|
lists:foreach(fun(M) -> M:stop(Host) end, Modules).
|
|
|
|
|
2018-05-07 18:27:18 +02:00
|
|
|
reload(Host, Modules) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(M) ->
|
|
|
|
case erlang:function_exported(M, reload, 1) of
|
|
|
|
true -> M:reload(Host);
|
|
|
|
false -> ok
|
|
|
|
end
|
|
|
|
end, Modules).
|
|
|
|
|
2017-02-24 12:06:47 +01:00
|
|
|
host_up(Host) ->
|
|
|
|
gen_server:cast(?MODULE, {host_up, Host}).
|
|
|
|
|
|
|
|
host_down(Host) ->
|
|
|
|
gen_server:cast(?MODULE, {host_down, Host}).
|
|
|
|
|
|
|
|
config_reloaded() ->
|
|
|
|
gen_server:cast(?MODULE, config_reloaded).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
-spec plain_password_required(binary()) -> boolean().
|
2005-07-13 05:24:13 +02:00
|
|
|
plain_password_required(Server) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
lists:any(fun (M) -> M:plain_password_required(Server) end,
|
2013-03-14 10:33:02 +01:00
|
|
|
auth_modules(Server)).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
-spec store_type(binary()) -> plain | scram | external.
|
2011-08-16 00:26:49 +02:00
|
|
|
store_type(Server) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
lists:foldl(
|
|
|
|
fun(_, external) -> external;
|
|
|
|
(M, scram) ->
|
|
|
|
case M:store_type(Server) of
|
|
|
|
external -> external;
|
|
|
|
_ -> scram
|
|
|
|
end;
|
|
|
|
(M, plain) ->
|
|
|
|
M:store_type(Server)
|
|
|
|
end, plain, auth_modules(Server)).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-09 03:21:09 +02:00
|
|
|
-spec check_password(binary(), binary(), binary(), binary()) -> boolean().
|
|
|
|
check_password(User, AuthzId, Server, Password) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
check_password(User, AuthzId, Server, Password, <<"">>, undefined).
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2015-04-09 03:21:09 +02:00
|
|
|
-spec check_password(binary(), binary(), binary(), binary(), binary(),
|
2017-05-11 13:37:21 +02:00
|
|
|
digest_fun() | undefined) -> boolean().
|
|
|
|
check_password(User, AuthzId, Server, Password, Digest, DigestGen) ->
|
|
|
|
case check_password_with_authmodule(
|
|
|
|
User, AuthzId, Server, Password, Digest, DigestGen) of
|
|
|
|
{true, _AuthModule} -> true;
|
|
|
|
false -> false
|
2009-03-04 19:34:02 +01:00
|
|
|
end.
|
2002-12-20 21:42:08 +01:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
-spec check_password_with_authmodule(binary(), binary(),
|
|
|
|
binary(), binary()) -> false | {true, atom()}.
|
|
|
|
check_password_with_authmodule(User, AuthzId, Server, Password) ->
|
|
|
|
check_password_with_authmodule(
|
|
|
|
User, AuthzId, Server, Password, <<"">>, undefined).
|
|
|
|
|
|
|
|
-spec check_password_with_authmodule(
|
|
|
|
binary(), binary(), binary(), binary(), binary(),
|
|
|
|
digest_fun() | undefined) -> false | {true, atom()}.
|
|
|
|
check_password_with_authmodule(User, AuthzId, Server, Password, Digest, DigestGen) ->
|
|
|
|
case validate_credentials(User, Server) of
|
|
|
|
{ok, LUser, LServer} ->
|
2018-05-30 16:10:25 +02:00
|
|
|
case jid:nodeprep(AuthzId) of
|
|
|
|
error ->
|
|
|
|
false;
|
2018-05-30 21:53:34 +02:00
|
|
|
LAuthzId ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(Mod, false) ->
|
|
|
|
case db_check_password(
|
|
|
|
LUser, LAuthzId, LServer, Password,
|
|
|
|
Digest, DigestGen, Mod) of
|
|
|
|
true -> {true, Mod};
|
|
|
|
false -> false
|
|
|
|
end;
|
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, false, auth_modules(LServer))
|
2018-07-26 22:37:25 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
2008-04-22 19:41:30 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-07 11:16:04 +02:00
|
|
|
-spec set_password(binary(), binary(), password()) -> ok | {error,
|
|
|
|
db_failure | not_allowed |
|
|
|
|
invalid_jid | invalid_password}.
|
2005-04-17 20:08:34 +02:00
|
|
|
set_password(User, Server, Password) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case validate_credentials(User, Server, Password) of
|
|
|
|
{ok, LUser, LServer} ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(M, {error, _}) ->
|
|
|
|
db_set_password(LUser, LServer, Password, M);
|
|
|
|
(_, ok) ->
|
|
|
|
ok
|
|
|
|
end, {error, not_allowed}, auth_modules(LServer));
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
2019-07-07 11:16:04 +02:00
|
|
|
-spec try_register(binary(), binary(), password()) -> ok | {error,
|
|
|
|
db_failure | not_allowed | exists |
|
|
|
|
invalid_jid | invalid_password}.
|
2005-04-17 20:08:34 +02:00
|
|
|
try_register(User, Server, Password) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case validate_credentials(User, Server, Password) of
|
|
|
|
{ok, LUser, LServer} ->
|
2017-05-11 14:49:06 +02:00
|
|
|
case user_exists(LUser, LServer) of
|
2017-05-11 13:37:21 +02:00
|
|
|
true ->
|
|
|
|
{error, exists};
|
|
|
|
false ->
|
|
|
|
case ejabberd_router:is_my_host(LServer) of
|
|
|
|
true ->
|
|
|
|
case lists:foldl(
|
|
|
|
fun(_, ok) ->
|
|
|
|
ok;
|
|
|
|
(Mod, _) ->
|
|
|
|
db_try_register(
|
2017-08-04 11:53:32 +02:00
|
|
|
LUser, LServer, Password, Mod)
|
2017-05-11 13:37:21 +02:00
|
|
|
end, {error, not_allowed},
|
|
|
|
auth_modules(LServer)) of
|
|
|
|
ok ->
|
|
|
|
ejabberd_hooks:run(
|
2017-08-04 11:53:32 +02:00
|
|
|
register_user, LServer, [LUser, LServer]);
|
2017-05-11 13:37:21 +02:00
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
{error, not_allowed}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
Err ->
|
|
|
|
Err
|
2005-04-20 01:10:22 +02:00
|
|
|
end.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
-spec get_users() -> [{binary(), binary()}].
|
|
|
|
get_users() ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({Host, Mod}) ->
|
|
|
|
db_get_users(Host, [], Mod)
|
|
|
|
end, auth_modules()).
|
|
|
|
|
|
|
|
-spec get_users(binary()) -> [{binary(), binary()}].
|
|
|
|
get_users(Server) ->
|
|
|
|
get_users(Server, []).
|
|
|
|
|
|
|
|
-spec get_users(binary(), opts()) -> [{binary(), binary()}].
|
|
|
|
get_users(Server, Opts) ->
|
|
|
|
case jid:nameprep(Server) of
|
|
|
|
error -> [];
|
|
|
|
LServer ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun(M) -> db_get_users(LServer, Opts, M) end,
|
|
|
|
auth_modules(LServer))
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
-spec count_users(binary()) -> non_neg_integer().
|
|
|
|
count_users(Server) ->
|
|
|
|
count_users(Server, []).
|
|
|
|
|
|
|
|
-spec count_users(binary(), opts()) -> non_neg_integer().
|
|
|
|
count_users(Server, Opts) ->
|
|
|
|
case jid:nameprep(Server) of
|
|
|
|
error -> 0;
|
|
|
|
LServer ->
|
|
|
|
lists:sum(
|
|
|
|
lists:map(
|
|
|
|
fun(M) -> db_count_users(LServer, Opts, M) end,
|
|
|
|
auth_modules(LServer)))
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
-spec get_password(binary(), binary()) -> false | password().
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password(User, Server) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case validate_credentials(User, Server) of
|
|
|
|
{ok, LUser, LServer} ->
|
|
|
|
case lists:foldl(
|
|
|
|
fun(M, error) -> db_get_password(LUser, LServer, M);
|
|
|
|
(_M, Acc) -> Acc
|
|
|
|
end, error, auth_modules(LServer)) of
|
|
|
|
{ok, Password} ->
|
|
|
|
Password;
|
|
|
|
error ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec get_password_s(binary(), binary()) -> password().
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password_s(User, Server) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
case get_password(User, Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> <<"">>;
|
|
|
|
Password -> Password
|
2006-04-07 02:39:24 +02:00
|
|
|
end.
|
2003-01-26 21:16:53 +01:00
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec get_password_with_authmodule(binary(), binary()) -> {false | password(), module()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
get_password_with_authmodule(User, Server) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case validate_credentials(User, Server) of
|
|
|
|
{ok, LUser, LServer} ->
|
|
|
|
case lists:foldl(
|
|
|
|
fun(M, {error, _}) ->
|
|
|
|
{db_get_password(LUser, LServer, M), M};
|
|
|
|
(_M, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, {error, undefined}, auth_modules(LServer)) of
|
|
|
|
{{ok, Password}, Module} ->
|
|
|
|
{Password, Module};
|
|
|
|
{error, Module} ->
|
|
|
|
{false, Module}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{false, undefined}
|
|
|
|
end.
|
2003-02-21 20:52:15 +01:00
|
|
|
|
2017-05-11 14:49:06 +02:00
|
|
|
-spec user_exists(binary(), binary()) -> boolean().
|
|
|
|
user_exists(_User, <<"">>) ->
|
2014-04-30 17:20:58 +02:00
|
|
|
false;
|
2017-05-11 14:49:06 +02:00
|
|
|
user_exists(User, Server) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case validate_credentials(User, Server) of
|
|
|
|
{ok, LUser, LServer} ->
|
|
|
|
lists:any(
|
|
|
|
fun(M) ->
|
2017-05-11 14:49:06 +02:00
|
|
|
case db_user_exists(LUser, LServer, M) of
|
2017-05-11 13:37:21 +02:00
|
|
|
{error, _} ->
|
|
|
|
false;
|
|
|
|
Else ->
|
|
|
|
Else
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2017-05-11 13:37:21 +02:00
|
|
|
end, auth_modules(LServer));
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-05-11 14:49:06 +02:00
|
|
|
-spec user_exists_in_other_modules(atom(), binary(), binary()) -> boolean() | maybe.
|
|
|
|
user_exists_in_other_modules(Module, User, Server) ->
|
|
|
|
user_exists_in_other_modules_loop(
|
2017-05-11 13:37:21 +02:00
|
|
|
auth_modules(Server) -- [Module], User, Server).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-05-11 14:49:06 +02:00
|
|
|
user_exists_in_other_modules_loop([], _User, _Server) ->
|
2009-03-04 19:34:02 +01:00
|
|
|
false;
|
2017-05-11 14:49:06 +02:00
|
|
|
user_exists_in_other_modules_loop([AuthModule | AuthModules], User, Server) ->
|
|
|
|
case db_user_exists(User, Server, AuthModule) of
|
2017-05-11 13:37:21 +02:00
|
|
|
true ->
|
|
|
|
true;
|
|
|
|
false ->
|
2017-05-11 14:49:06 +02:00
|
|
|
user_exists_in_other_modules_loop(AuthModules, User, Server);
|
2017-05-11 13:37:21 +02:00
|
|
|
{error, _} ->
|
|
|
|
maybe
|
2009-03-04 19:34:02 +01:00
|
|
|
end.
|
|
|
|
|
2018-12-05 14:22:09 +01:00
|
|
|
-spec which_users_exists(list({binary(), binary()})) -> list({binary(), binary()}).
|
|
|
|
which_users_exists(USPairs) ->
|
|
|
|
ByServer = lists:foldl(
|
|
|
|
fun({User, Server}, Dict) ->
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
case gb_trees:lookup(LServer, Dict) of
|
|
|
|
none ->
|
|
|
|
gb_trees:insert(LServer, gb_sets:singleton(LUser), Dict);
|
|
|
|
{value, Set} ->
|
|
|
|
gb_trees:update(LServer, gb_sets:add(LUser, Set), Dict)
|
|
|
|
end
|
|
|
|
end, gb_trees:empty(), USPairs),
|
|
|
|
Set = lists:foldl(
|
|
|
|
fun({LServer, UsersSet}, Results) ->
|
|
|
|
UsersList = gb_sets:to_list(UsersSet),
|
|
|
|
lists:foldl(
|
|
|
|
fun(M, Results2) ->
|
|
|
|
try M:which_users_exists(LServer, UsersList) of
|
|
|
|
{error, _} ->
|
|
|
|
Results2;
|
|
|
|
Res ->
|
|
|
|
gb_sets:union(
|
|
|
|
gb_sets:from_list([{U, LServer} || U <- Res]),
|
|
|
|
Results2)
|
|
|
|
catch
|
|
|
|
_:undef ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(U, R2) ->
|
|
|
|
case user_exists(U, LServer) of
|
|
|
|
true ->
|
|
|
|
gb_sets:add({U, LServer}, R2);
|
|
|
|
_ ->
|
|
|
|
R2
|
|
|
|
end
|
|
|
|
end, Results2, UsersList)
|
|
|
|
end
|
|
|
|
end, Results, auth_modules(LServer))
|
|
|
|
end, gb_sets:empty(), gb_trees:to_list(ByServer)),
|
|
|
|
gb_sets:to_list(Set).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec remove_user(binary(), binary()) -> ok.
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case validate_credentials(User, Server) of
|
|
|
|
{ok, LUser, LServer} ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Mod) -> db_remove_user(LUser, LServer, Mod) end,
|
|
|
|
auth_modules(LServer)),
|
|
|
|
ejabberd_hooks:run(remove_user, LServer, [LUser, LServer]);
|
|
|
|
_Err ->
|
|
|
|
ok
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
-spec remove_user(binary(), binary(), password()) -> ok | {error, atom()}.
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server, Password) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case validate_credentials(User, Server, Password) of
|
|
|
|
{ok, LUser, LServer} ->
|
|
|
|
case lists:foldl(
|
|
|
|
fun (_, ok) ->
|
|
|
|
ok;
|
|
|
|
(Mod, _) ->
|
|
|
|
case db_check_password(
|
|
|
|
LUser, <<"">>, LServer, Password,
|
|
|
|
<<"">>, undefined, Mod) of
|
|
|
|
true ->
|
|
|
|
db_remove_user(LUser, LServer, Mod);
|
|
|
|
false ->
|
|
|
|
{error, not_allowed}
|
|
|
|
end
|
|
|
|
end, {error, not_allowed}, auth_modules(Server)) of
|
|
|
|
ok ->
|
|
|
|
ejabberd_hooks:run(
|
|
|
|
remove_user, LServer, [LUser, LServer]);
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
2010-10-24 09:17:30 +02:00
|
|
|
%% @doc Calculate informational entropy.
|
2017-05-11 13:37:21 +02:00
|
|
|
-spec entropy(iodata()) -> float().
|
2013-03-14 10:33:02 +01:00
|
|
|
entropy(B) ->
|
|
|
|
case binary_to_list(B) of
|
|
|
|
"" -> 0.0;
|
|
|
|
S ->
|
|
|
|
Set = lists:foldl(fun (C,
|
|
|
|
[Digit, Printable, LowLetter, HiLetter,
|
|
|
|
Other]) ->
|
|
|
|
if C >= $a, C =< $z ->
|
|
|
|
[Digit, Printable, 26, HiLetter,
|
|
|
|
Other];
|
|
|
|
C >= $0, C =< $9 ->
|
|
|
|
[9, Printable, LowLetter, HiLetter,
|
|
|
|
Other];
|
|
|
|
C >= $A, C =< $Z ->
|
|
|
|
[Digit, Printable, LowLetter, 26,
|
|
|
|
Other];
|
|
|
|
C >= 33, C =< 126 ->
|
|
|
|
[Digit, 33, LowLetter, HiLetter,
|
|
|
|
Other];
|
|
|
|
true ->
|
|
|
|
[Digit, Printable, LowLetter,
|
|
|
|
HiLetter, 128]
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[0, 0, 0, 0, 0], S),
|
|
|
|
length(S) * math:log(lists:sum(Set)) / math:log(2)
|
2010-10-24 09:17:30 +02:00
|
|
|
end.
|
2006-02-20 05:07:42 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec backend_type(atom()) -> atom().
|
|
|
|
backend_type(Mod) ->
|
|
|
|
case atom_to_list(Mod) of
|
|
|
|
"ejabberd_auth_" ++ T -> list_to_atom(T);
|
|
|
|
_ -> Mod
|
|
|
|
end.
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
-spec password_format(binary() | global) -> plain | scram.
|
2017-04-28 12:23:32 +02:00
|
|
|
password_format(LServer) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:auth_password_format(LServer).
|
2017-04-28 12:23:32 +02:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Backend calls
|
|
|
|
%%%----------------------------------------------------------------------
|
2019-07-07 11:16:04 +02:00
|
|
|
-spec db_try_register(binary(), binary(), password(), module()) -> ok | {error, exists | db_failure | not_allowed}.
|
2017-05-11 13:37:21 +02:00
|
|
|
db_try_register(User, Server, Password, Mod) ->
|
|
|
|
case erlang:function_exported(Mod, try_register, 3) of
|
|
|
|
true ->
|
|
|
|
Password1 = case Mod:store_type(Server) of
|
|
|
|
scram -> password_to_scram(Password);
|
|
|
|
_ -> Password
|
|
|
|
end,
|
2019-07-07 11:16:04 +02:00
|
|
|
Ret = case use_cache(Mod, Server) of
|
|
|
|
true ->
|
|
|
|
ets_cache:update(
|
|
|
|
cache_tab(Mod), {User, Server}, {ok, Password},
|
|
|
|
fun() -> Mod:try_register(User, Server, Password1) end,
|
|
|
|
cache_nodes(Mod, Server));
|
|
|
|
false ->
|
|
|
|
ets_cache:untag(Mod:try_register(User, Server, Password1))
|
|
|
|
end,
|
|
|
|
case Ret of
|
|
|
|
{ok, _} -> ok;
|
|
|
|
{error, _} = Err -> Err
|
2017-05-11 13:37:21 +02:00
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
{error, not_allowed}
|
|
|
|
end.
|
|
|
|
|
2019-07-07 11:16:04 +02:00
|
|
|
-spec db_set_password(binary(), binary(), password(), module()) -> ok | {error, db_failure | not_allowed}.
|
2017-05-11 13:37:21 +02:00
|
|
|
db_set_password(User, Server, Password, Mod) ->
|
|
|
|
case erlang:function_exported(Mod, set_password, 3) of
|
|
|
|
true ->
|
|
|
|
Password1 = case Mod:store_type(Server) of
|
|
|
|
scram -> password_to_scram(Password);
|
|
|
|
_ -> Password
|
|
|
|
end,
|
2019-07-07 11:16:04 +02:00
|
|
|
Ret = case use_cache(Mod, Server) of
|
|
|
|
true ->
|
|
|
|
ets_cache:update(
|
|
|
|
cache_tab(Mod), {User, Server}, {ok, Password},
|
|
|
|
fun() -> Mod:set_password(User, Server, Password1) end,
|
|
|
|
cache_nodes(Mod, Server));
|
|
|
|
false ->
|
|
|
|
ets_cache:untag(Mod:set_password(User, Server, Password1))
|
|
|
|
end,
|
|
|
|
case Ret of
|
|
|
|
{ok, _} -> ok;
|
|
|
|
{error, _} = Err -> Err
|
2017-05-11 13:37:21 +02:00
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
{error, not_allowed}
|
|
|
|
end.
|
|
|
|
|
|
|
|
db_get_password(User, Server, Mod) ->
|
|
|
|
UseCache = use_cache(Mod, Server),
|
|
|
|
case erlang:function_exported(Mod, get_password, 2) of
|
|
|
|
false when UseCache ->
|
2019-05-23 10:32:55 +02:00
|
|
|
case ets_cache:lookup(cache_tab(Mod), {User, Server}) of
|
2018-02-16 18:50:22 +01:00
|
|
|
{ok, exists} -> error;
|
|
|
|
Other -> Other
|
|
|
|
end;
|
2017-05-11 13:37:21 +02:00
|
|
|
false ->
|
|
|
|
error;
|
|
|
|
true when UseCache ->
|
|
|
|
ets_cache:lookup(
|
2019-05-23 10:32:55 +02:00
|
|
|
cache_tab(Mod), {User, Server},
|
2017-05-11 13:37:21 +02:00
|
|
|
fun() -> Mod:get_password(User, Server) end);
|
|
|
|
true ->
|
2019-04-19 14:08:41 +02:00
|
|
|
ets_cache:untag(Mod:get_password(User, Server))
|
2017-05-11 13:37:21 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-11 14:49:06 +02:00
|
|
|
db_user_exists(User, Server, Mod) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
case db_get_password(User, Server, Mod) of
|
|
|
|
{ok, _} ->
|
|
|
|
true;
|
|
|
|
error ->
|
2018-05-03 00:31:33 +02:00
|
|
|
case {Mod:store_type(Server), use_cache(Mod, Server)} of
|
|
|
|
{external, true} ->
|
2018-02-18 07:02:23 +01:00
|
|
|
case ets_cache:lookup(
|
2019-05-23 10:32:55 +02:00
|
|
|
cache_tab(Mod), {User, Server},
|
2018-02-16 18:50:22 +01:00
|
|
|
fun() ->
|
|
|
|
case Mod:user_exists(User, Server) of
|
2019-04-19 14:42:24 +02:00
|
|
|
{CacheTag, true} -> {CacheTag, {ok, exists}};
|
|
|
|
{CacheTag, false} -> {CacheTag, error};
|
|
|
|
{_, {error, _}} = Err -> Err
|
2018-02-16 18:50:22 +01:00
|
|
|
end
|
2018-02-18 07:02:23 +01:00
|
|
|
end) of
|
2018-02-16 18:50:22 +01:00
|
|
|
{ok, _} ->
|
|
|
|
true;
|
|
|
|
error ->
|
2018-05-07 18:27:18 +02:00
|
|
|
false;
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
2018-02-16 18:50:22 +01:00
|
|
|
end;
|
2018-05-03 00:31:33 +02:00
|
|
|
{external, false} ->
|
2019-04-19 14:08:41 +02:00
|
|
|
ets_cache:untag(Mod:user_exists(User, Server));
|
2017-05-11 13:37:21 +02:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
db_check_password(User, AuthzId, Server, ProvidedPassword,
|
|
|
|
Digest, DigestFun, Mod) ->
|
|
|
|
case db_get_password(User, Server, Mod) of
|
|
|
|
{ok, ValidPassword} ->
|
|
|
|
match_passwords(ProvidedPassword, ValidPassword, Digest, DigestFun);
|
|
|
|
error ->
|
|
|
|
case {Mod:store_type(Server), use_cache(Mod, Server)} of
|
|
|
|
{external, true} ->
|
|
|
|
case ets_cache:update(
|
2019-05-23 10:32:55 +02:00
|
|
|
cache_tab(Mod), {User, Server}, {ok, ProvidedPassword},
|
2017-05-11 13:37:21 +02:00
|
|
|
fun() ->
|
|
|
|
case Mod:check_password(
|
|
|
|
User, AuthzId, Server, ProvidedPassword) of
|
2019-04-19 14:42:24 +02:00
|
|
|
{CacheTag, true} -> {CacheTag, {ok, ProvidedPassword}};
|
|
|
|
{CacheTag, false} -> {CacheTag, error}
|
2017-05-11 13:37:21 +02:00
|
|
|
end
|
2018-02-18 07:02:23 +01:00
|
|
|
end) of
|
2017-05-11 13:37:21 +02:00
|
|
|
{ok, _} ->
|
|
|
|
true;
|
|
|
|
error ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
{external, false} ->
|
2019-04-19 14:08:41 +02:00
|
|
|
ets_cache:untag(
|
|
|
|
Mod:check_password(User, AuthzId, Server, ProvidedPassword));
|
2017-05-11 13:37:21 +02:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
db_remove_user(User, Server, Mod) ->
|
|
|
|
case erlang:function_exported(Mod, remove_user, 2) of
|
|
|
|
true ->
|
2019-06-30 16:15:43 +02:00
|
|
|
case Mod:remove_user(User, Server) of
|
2017-05-11 13:37:21 +02:00
|
|
|
ok ->
|
|
|
|
case use_cache(Mod, Server) of
|
|
|
|
true ->
|
2019-05-23 10:32:55 +02:00
|
|
|
ets_cache:delete(cache_tab(Mod), {User, Server},
|
2017-05-11 13:37:21 +02:00
|
|
|
cache_nodes(Mod, Server));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
{error, not_allowed}
|
|
|
|
end.
|
|
|
|
|
|
|
|
db_get_users(Server, Opts, Mod) ->
|
|
|
|
case erlang:function_exported(Mod, get_users, 2) of
|
|
|
|
true ->
|
2019-06-30 16:15:43 +02:00
|
|
|
Mod:get_users(Server, Opts);
|
2017-05-11 13:37:21 +02:00
|
|
|
false ->
|
|
|
|
case use_cache(Mod, Server) of
|
|
|
|
true ->
|
|
|
|
ets_cache:fold(
|
|
|
|
fun({User, S}, {ok, _}, Users) when S == Server ->
|
|
|
|
[{User, Server}|Users];
|
|
|
|
(_, _, Users) ->
|
|
|
|
Users
|
2019-05-23 10:32:55 +02:00
|
|
|
end, [], cache_tab(Mod));
|
2017-05-11 13:37:21 +02:00
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
db_count_users(Server, Opts, Mod) ->
|
|
|
|
case erlang:function_exported(Mod, count_users, 2) of
|
|
|
|
true ->
|
2019-06-30 16:15:43 +02:00
|
|
|
Mod:count_users(Server, Opts);
|
2017-05-11 13:37:21 +02:00
|
|
|
false ->
|
|
|
|
case use_cache(Mod, Server) of
|
|
|
|
true ->
|
|
|
|
ets_cache:fold(
|
|
|
|
fun({_, S}, {ok, _}, Num) when S == Server ->
|
|
|
|
Num + 1;
|
|
|
|
(_, _, Num) ->
|
|
|
|
Num
|
2019-05-23 10:32:55 +02:00
|
|
|
end, 0, cache_tab(Mod));
|
2017-05-11 13:37:21 +02:00
|
|
|
false ->
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% SCRAM stuff
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
is_password_scram_valid(Password, Scram) ->
|
|
|
|
case jid:resourceprep(Password) of
|
|
|
|
error ->
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
IterationCount = Scram#scram.iterationcount,
|
2017-05-23 09:43:26 +02:00
|
|
|
Salt = base64:decode(Scram#scram.salt),
|
2017-05-11 13:37:21 +02:00
|
|
|
SaltedPassword = scram:salted_password(Password, Salt, IterationCount),
|
|
|
|
StoredKey = scram:stored_key(scram:client_key(SaltedPassword)),
|
2017-05-23 09:43:26 +02:00
|
|
|
base64:decode(Scram#scram.storedkey) == StoredKey
|
2017-05-11 13:37:21 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
password_to_scram(Password) ->
|
|
|
|
password_to_scram(Password, ?SCRAM_DEFAULT_ITERATION_COUNT).
|
|
|
|
|
|
|
|
password_to_scram(#scram{} = Password, _IterationCount) ->
|
|
|
|
Password;
|
|
|
|
password_to_scram(Password, IterationCount) ->
|
2018-07-05 10:51:49 +02:00
|
|
|
Salt = p1_rand:bytes(?SALT_LENGTH),
|
2017-05-11 13:37:21 +02:00
|
|
|
SaltedPassword = scram:salted_password(Password, Salt, IterationCount),
|
|
|
|
StoredKey = scram:stored_key(scram:client_key(SaltedPassword)),
|
|
|
|
ServerKey = scram:server_key(SaltedPassword),
|
2017-05-23 09:43:26 +02:00
|
|
|
#scram{storedkey = base64:encode(StoredKey),
|
|
|
|
serverkey = base64:encode(ServerKey),
|
|
|
|
salt = base64:encode(Salt),
|
2017-05-11 13:37:21 +02:00
|
|
|
iterationcount = IterationCount}.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Cache stuff
|
|
|
|
%%%----------------------------------------------------------------------
|
2019-06-27 14:22:27 +02:00
|
|
|
-spec init_cache(host_modules()) -> ok.
|
2017-05-11 13:37:21 +02:00
|
|
|
init_cache(HostModules) ->
|
2019-05-23 10:32:55 +02:00
|
|
|
CacheOpts = cache_opts(),
|
|
|
|
{True, False} = use_cache(HostModules),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Module) ->
|
|
|
|
ets_cache:new(cache_tab(Module), CacheOpts)
|
|
|
|
end, True),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Module) ->
|
|
|
|
ets_cache:delete(cache_tab(Module))
|
|
|
|
end, False).
|
2017-05-11 13:37:21 +02:00
|
|
|
|
|
|
|
-spec cache_opts() -> [proplists:property()].
|
|
|
|
cache_opts() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxSize = ejabberd_option:auth_cache_size(),
|
|
|
|
CacheMissed = ejabberd_option:auth_cache_missed(),
|
2019-06-15 11:53:16 +02:00
|
|
|
LifeTime = ejabberd_option:auth_cache_life_time(),
|
2017-05-11 13:37:21 +02:00
|
|
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
|
|
|
|
2019-06-27 14:22:27 +02:00
|
|
|
-spec use_cache(host_modules()) -> {True :: [module()], False :: [module()]}.
|
2017-05-11 13:37:21 +02:00
|
|
|
use_cache(HostModules) ->
|
2019-05-23 10:32:55 +02:00
|
|
|
{Enabled, Disabled} =
|
|
|
|
maps:fold(
|
|
|
|
fun(Host, Modules, Acc) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(Module, {True, False}) ->
|
|
|
|
case use_cache(Module, Host) of
|
|
|
|
true ->
|
|
|
|
{sets:add_element(Module, True), False};
|
|
|
|
false ->
|
|
|
|
{True, sets:add_element(Module, False)}
|
|
|
|
end
|
|
|
|
end, Acc, Modules)
|
|
|
|
end, {sets:new(), sets:new()}, HostModules),
|
|
|
|
{sets:to_list(Enabled), sets:to_list(sets:subtract(Disabled, Enabled))}.
|
2017-05-11 13:37:21 +02:00
|
|
|
|
|
|
|
-spec use_cache(module(), binary()) -> boolean().
|
|
|
|
use_cache(Mod, LServer) ->
|
|
|
|
case erlang:function_exported(Mod, use_cache, 1) of
|
|
|
|
true -> Mod:use_cache(LServer);
|
|
|
|
false ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:auth_use_cache(LServer)
|
2017-05-11 13:37:21 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec cache_nodes(module(), binary()) -> [node()].
|
|
|
|
cache_nodes(Mod, LServer) ->
|
|
|
|
case erlang:function_exported(Mod, cache_nodes, 1) of
|
|
|
|
true -> Mod:cache_nodes(LServer);
|
|
|
|
false -> ejabberd_cluster:get_nodes()
|
|
|
|
end.
|
|
|
|
|
2019-05-23 10:32:55 +02:00
|
|
|
-spec cache_tab(module()) -> atom().
|
|
|
|
cache_tab(Mod) ->
|
|
|
|
list_to_atom(atom_to_list(Mod) ++ "_cache").
|
|
|
|
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
2017-05-11 13:37:21 +02:00
|
|
|
-spec auth_modules() -> [{binary(), module()}].
|
2008-02-19 14:49:29 +01:00
|
|
|
auth_modules() ->
|
2017-05-11 13:37:21 +02:00
|
|
|
lists:flatmap(
|
|
|
|
fun(Host) ->
|
|
|
|
[{Host, Mod} || Mod <- auth_modules(Host)]
|
2019-06-14 11:33:26 +02:00
|
|
|
end, ejabberd_option:hosts()).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-02-24 12:06:47 +01:00
|
|
|
-spec auth_modules(binary()) -> [module()].
|
2006-04-07 02:39:24 +02:00
|
|
|
auth_modules(Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2019-06-14 11:33:26 +02:00
|
|
|
Methods = ejabberd_option:auth_method(LServer),
|
2017-10-31 11:04:32 +01:00
|
|
|
[ejabberd:module_name([<<"ejabberd">>, <<"auth">>,
|
|
|
|
misc:atom_to_binary(M)])
|
2013-03-14 10:33:02 +01:00
|
|
|
|| M <- Methods].
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
-spec match_passwords(password(), password(),
|
|
|
|
binary(), digest_fun() | undefined) -> boolean().
|
|
|
|
match_passwords(Password, #scram{} = Scram, <<"">>, undefined) ->
|
|
|
|
is_password_scram_valid(Password, Scram);
|
|
|
|
match_passwords(Password, #scram{} = Scram, Digest, DigestFun) ->
|
2017-05-23 09:43:26 +02:00
|
|
|
StoredKey = base64:decode(Scram#scram.storedkey),
|
2017-05-11 13:37:21 +02:00
|
|
|
DigRes = if Digest /= <<"">> ->
|
|
|
|
Digest == DigestFun(StoredKey);
|
|
|
|
true -> false
|
|
|
|
end,
|
|
|
|
if DigRes ->
|
|
|
|
true;
|
|
|
|
true ->
|
|
|
|
StoredKey == Password andalso Password /= <<"">>
|
|
|
|
end;
|
|
|
|
match_passwords(ProvidedPassword, ValidPassword, <<"">>, undefined) ->
|
|
|
|
ProvidedPassword == ValidPassword andalso ProvidedPassword /= <<"">>;
|
|
|
|
match_passwords(ProvidedPassword, ValidPassword, Digest, DigestFun) ->
|
|
|
|
DigRes = if Digest /= <<"">> ->
|
|
|
|
Digest == DigestFun(ValidPassword);
|
|
|
|
true -> false
|
|
|
|
end,
|
|
|
|
if DigRes ->
|
|
|
|
true;
|
|
|
|
true ->
|
|
|
|
ValidPassword == ProvidedPassword andalso ProvidedPassword /= <<"">>
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec validate_credentials(binary(), binary()) ->
|
|
|
|
{ok, binary(), binary()} | {error, invalid_jid}.
|
|
|
|
validate_credentials(User, Server) ->
|
|
|
|
validate_credentials(User, Server, #scram{}).
|
|
|
|
|
|
|
|
-spec validate_credentials(binary(), binary(), password()) ->
|
|
|
|
{ok, binary(), binary()} | {error, invalid_jid | invalid_password}.
|
|
|
|
validate_credentials(_User, _Server, <<"">>) ->
|
|
|
|
{error, invalid_password};
|
|
|
|
validate_credentials(User, Server, Password) ->
|
|
|
|
case jid:nodeprep(User) of
|
|
|
|
error ->
|
|
|
|
{error, invalid_jid};
|
|
|
|
LUser ->
|
|
|
|
case jid:nameprep(Server) of
|
|
|
|
error ->
|
|
|
|
{error, invalid_jid};
|
|
|
|
LServer ->
|
|
|
|
if is_record(Password, scram) ->
|
|
|
|
{ok, LUser, LServer};
|
|
|
|
true ->
|
|
|
|
case jid:resourceprep(Password) of
|
|
|
|
error ->
|
|
|
|
{error, invalid_password};
|
|
|
|
_ ->
|
|
|
|
{ok, LUser, LServer}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import_info() ->
|
|
|
|
[{<<"users">>, 3}].
|
2013-07-21 12:24:36 +02:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import_start(_LServer, mnesia) ->
|
|
|
|
ejabberd_auth_mnesia:init_db();
|
|
|
|
import_start(_LServer, _) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
import(Server, {sql, _}, mnesia, <<"users">>, Fields) ->
|
|
|
|
ejabberd_auth_mnesia:import(Server, Fields);
|
|
|
|
import(_LServer, {sql, _}, sql, <<"users">>, _) ->
|
|
|
|
ok.
|