2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2016-04-20 11:27:32 +02:00
|
|
|
%%% File : ejabberd_auth_sql.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 ODBC
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 12 Dec 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2023-01-09 17:09:06 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2023 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-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-04-20 11:27:32 +02:00
|
|
|
-module(ejabberd_auth_sql).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-14 00:00:12 +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, count_users/2, get_password/2,
|
|
|
|
remove_user/2, store_type/1, plain_password_required/1,
|
2019-11-27 10:35:52 +01:00
|
|
|
export/1, which_users_exists/2]).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/scram.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2017-10-25 20:21:52 +02:00
|
|
|
-include("ejabberd_auth.hrl").
|
2005-07-13 05:24:13 +02:00
|
|
|
|
2015-02-17 21:26:31 +01:00
|
|
|
-define(SALT_LENGTH, 16).
|
|
|
|
|
2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2023-09-28 02:37:36 +02:00
|
|
|
start(Host) ->
|
|
|
|
ejabberd_sql_schema:update_schema(Host, ?MODULE, schemas()),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
schemas() ->
|
|
|
|
[#sql_schema{
|
|
|
|
version = 1,
|
|
|
|
tables =
|
|
|
|
[#sql_table{
|
|
|
|
name = <<"users">>,
|
|
|
|
columns =
|
|
|
|
[#sql_column{name = <<"username">>, type = text},
|
|
|
|
#sql_column{name = <<"server_host">>, type = text},
|
|
|
|
#sql_column{name = <<"password">>, type = text},
|
|
|
|
#sql_column{name = <<"serverkey">>, type = {text, 128},
|
|
|
|
default = true},
|
|
|
|
#sql_column{name = <<"salt">>, type = {text, 128},
|
|
|
|
default = true},
|
|
|
|
#sql_column{name = <<"iterationcount">>, type = integer,
|
|
|
|
default = true},
|
|
|
|
#sql_column{name = <<"created_at">>, type = timestamp,
|
|
|
|
default = true}],
|
|
|
|
indices = [#sql_index{
|
|
|
|
columns = [<<"server_host">>, <<"username">>],
|
|
|
|
unique = true}]}]}].
|
2017-02-23 14:19:22 +01:00
|
|
|
|
|
|
|
stop(_Host) -> ok.
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
plain_password_required(Server) ->
|
|
|
|
store_type(Server) == scram.
|
2011-08-16 00:25:03 +02:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
store_type(Server) ->
|
|
|
|
ejabberd_auth:password_format(Server).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
set_password(User, Server, Password) ->
|
2020-12-08 12:06:52 +01:00
|
|
|
F =
|
|
|
|
fun() ->
|
|
|
|
case Password of
|
|
|
|
#scram{hash = Hash, storedkey = SK, serverkey = SEK,
|
|
|
|
salt = Salt, iterationcount = IC} ->
|
|
|
|
SK2 = scram_hash_encode(Hash, SK),
|
|
|
|
set_password_scram_t(
|
|
|
|
User, Server,
|
|
|
|
SK2, SEK, Salt, IC);
|
|
|
|
_ ->
|
|
|
|
set_password_t(User, Server, Password)
|
|
|
|
end
|
|
|
|
end,
|
2017-05-23 11:25:13 +02:00
|
|
|
case ejabberd_sql:sql_transaction(Server, F) of
|
2017-05-11 13:37:21 +02:00
|
|
|
{atomic, _} ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{cache, {ok, Password}};
|
2017-12-17 17:46:55 +01:00
|
|
|
{aborted, _} ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{nocache, {error, db_failure}}
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
try_register(User, Server, Password) ->
|
2020-12-08 12:06:52 +01:00
|
|
|
Res =
|
|
|
|
case Password of
|
|
|
|
#scram{hash = Hash, storedkey = SK, serverkey = SEK,
|
|
|
|
salt = Salt, iterationcount = IC} ->
|
|
|
|
SK2 = scram_hash_encode(Hash, SK),
|
|
|
|
add_user_scram(
|
|
|
|
Server, User,
|
|
|
|
SK2, SEK, Salt, IC);
|
|
|
|
_ ->
|
|
|
|
add_user(Server, User, Password)
|
|
|
|
end,
|
2017-05-11 13:37:21 +02:00
|
|
|
case Res of
|
2019-06-30 16:15:43 +02:00
|
|
|
{updated, 1} -> {cache, {ok, Password}};
|
|
|
|
_ -> {nocache, {error, exists}}
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
get_users(Server, Opts) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
case list_users(Server, Opts) of
|
2017-05-11 13:37:21 +02:00
|
|
|
{selected, Res} ->
|
|
|
|
[{U, Server} || {U} <- Res];
|
|
|
|
_ -> []
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
count_users(Server, Opts) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
case users_number(Server, Opts) of
|
2017-05-11 13:37:21 +02:00
|
|
|
{selected, [{Res}]} ->
|
|
|
|
Res;
|
|
|
|
_Other -> 0
|
2007-11-03 18:35:23 +01:00
|
|
|
end.
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
get_password(User, Server) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
case get_password_scram(Server, User) of
|
2017-05-11 13:37:21 +02:00
|
|
|
{selected, [{Password, <<>>, <<>>, 0}]} ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{cache, {ok, Password}};
|
2017-05-11 13:37:21 +02:00
|
|
|
{selected, [{StoredKey, ServerKey, Salt, IterationCount}]} ->
|
2020-12-08 12:06:52 +01:00
|
|
|
{Hash, SK} = case StoredKey of
|
|
|
|
<<"sha256:", Rest/binary>> -> {sha256, Rest};
|
|
|
|
<<"sha512:", Rest/binary>> -> {sha512, Rest};
|
|
|
|
Other -> {sha, Other}
|
|
|
|
end,
|
|
|
|
{cache, {ok, #scram{storedkey = SK,
|
2019-06-30 16:15:43 +02:00
|
|
|
serverkey = ServerKey,
|
|
|
|
salt = Salt,
|
2020-12-08 12:06:52 +01:00
|
|
|
hash = Hash,
|
2019-06-30 16:15:43 +02:00
|
|
|
iterationcount = IterationCount}}};
|
2017-05-11 13:37:21 +02:00
|
|
|
{selected, []} ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{cache, error};
|
2017-12-17 17:46:55 +01:00
|
|
|
_ ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{nocache, error}
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
remove_user(User, Server) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
case del_user(Server, User) of
|
2017-05-11 13:37:21 +02:00
|
|
|
{updated, _} ->
|
|
|
|
ok;
|
2017-12-17 17:46:55 +01:00
|
|
|
_ ->
|
2017-05-11 13:37:21 +02:00
|
|
|
{error, db_failure}
|
2017-02-12 08:06:30 +01:00
|
|
|
end.
|
2015-02-17 21:26:31 +01:00
|
|
|
|
2015-03-09 14:38:16 +01:00
|
|
|
-define(BATCH_SIZE, 1000).
|
|
|
|
|
2020-12-08 12:06:52 +01:00
|
|
|
scram_hash_encode(Hash, StoreKey) ->
|
|
|
|
case Hash of
|
|
|
|
sha -> StoreKey;
|
|
|
|
sha256 -> <<"sha256:", StoreKey/binary>>;
|
|
|
|
sha512 -> <<"sha512:", StoreKey/binary>>
|
|
|
|
end.
|
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
set_password_scram_t(LUser, LServer,
|
2015-03-09 14:38:16 +01:00
|
|
|
StoredKey, ServerKey, Salt, IterationCount) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"users",
|
|
|
|
["!username=%(LUser)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"!server_host=%(LServer)s",
|
2016-05-04 20:01:05 +02:00
|
|
|
"password=%(StoredKey)s",
|
|
|
|
"serverkey=%(ServerKey)s",
|
|
|
|
"salt=%(Salt)s",
|
|
|
|
"iterationcount=%(IterationCount)d"]).
|
2015-03-09 14:38:16 +01:00
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
set_password_t(LUser, LServer, Password) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"users",
|
|
|
|
["!username=%(LUser)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"!server_host=%(LServer)s",
|
2023-08-03 13:06:01 +02:00
|
|
|
"password=%(Password)s",
|
|
|
|
"serverkey=''",
|
|
|
|
"salt=''",
|
|
|
|
"iterationcount=0"]).
|
2017-05-23 11:25:13 +02:00
|
|
|
|
|
|
|
get_password_scram(LServer, LUser) ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(password)s, @(serverkey)s, @(salt)s, @(iterationcount)d"
|
|
|
|
" from users"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H")).
|
2017-05-23 11:25:13 +02:00
|
|
|
|
|
|
|
add_user_scram(LServer, LUser,
|
|
|
|
StoredKey, ServerKey, Salt, IterationCount) ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL_INSERT(
|
|
|
|
"users",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"password=%(StoredKey)s",
|
|
|
|
"serverkey=%(ServerKey)s",
|
|
|
|
"salt=%(Salt)s",
|
|
|
|
"iterationcount=%(IterationCount)d"])).
|
2017-05-23 11:25:13 +02:00
|
|
|
|
|
|
|
add_user(LServer, LUser, Password) ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL_INSERT(
|
|
|
|
"users",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"password=%(Password)s"])).
|
2017-05-23 11:25:13 +02:00
|
|
|
|
|
|
|
del_user(LServer, LUser) ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("delete from users where username=%(LUser)s and %(LServer)H")).
|
2017-05-23 11:25:13 +02:00
|
|
|
|
|
|
|
list_users(LServer, []) ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("select @(username)s from users where %(LServer)H"));
|
2017-05-23 11:25:13 +02:00
|
|
|
list_users(LServer, [{from, Start}, {to, End}])
|
|
|
|
when is_integer(Start) and is_integer(End) ->
|
|
|
|
list_users(LServer,
|
|
|
|
[{limit, End - Start + 1}, {offset, Start - 1}]);
|
|
|
|
list_users(LServer,
|
|
|
|
[{prefix, Prefix}, {from, Start}, {to, End}])
|
|
|
|
when is_binary(Prefix) and is_integer(Start) and
|
|
|
|
is_integer(End) ->
|
|
|
|
list_users(LServer,
|
|
|
|
[{prefix, Prefix}, {limit, End - Start + 1},
|
|
|
|
{offset, Start - 1}]);
|
|
|
|
list_users(LServer, [{limit, Limit}, {offset, Offset}])
|
|
|
|
when is_integer(Limit) and is_integer(Offset) ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(username)s from users "
|
2017-11-02 15:03:30 +01:00
|
|
|
"where %(LServer)H "
|
2017-05-23 11:25:13 +02:00
|
|
|
"order by username "
|
|
|
|
"limit %(Limit)d offset %(Offset)d"));
|
|
|
|
list_users(LServer,
|
|
|
|
[{prefix, Prefix}, {limit, Limit}, {offset, Offset}])
|
|
|
|
when is_binary(Prefix) and is_integer(Limit) and
|
|
|
|
is_integer(Offset) ->
|
2020-01-13 01:36:52 +01:00
|
|
|
SPrefix = ejabberd_sql:escape_like_arg(Prefix),
|
2017-05-23 11:25:13 +02:00
|
|
|
SPrefix2 = <<SPrefix/binary, $%>>,
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(username)s from users "
|
2020-01-13 01:36:52 +01:00
|
|
|
"where username like %(SPrefix2)s %ESCAPE and %(LServer)H "
|
2017-05-23 11:25:13 +02:00
|
|
|
"order by username "
|
|
|
|
"limit %(Limit)d offset %(Offset)d")).
|
|
|
|
|
|
|
|
users_number(LServer) ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
fun(pgsql, _) ->
|
|
|
|
case
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:pgsql_users_number_estimate(LServer) of
|
2017-05-23 11:25:13 +02:00
|
|
|
true ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(reltuples :: bigint)d from pg_class"
|
|
|
|
" where oid = 'users'::regclass::oid"));
|
|
|
|
_ ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("select @(count(*))d from users where %(LServer)H"))
|
2017-05-23 11:25:13 +02:00
|
|
|
end;
|
|
|
|
(_Type, _) ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("select @(count(*))d from users where %(LServer)H"))
|
2017-05-23 11:25:13 +02:00
|
|
|
end).
|
|
|
|
|
|
|
|
users_number(LServer, [{prefix, Prefix}])
|
|
|
|
when is_binary(Prefix) ->
|
2020-01-13 01:36:52 +01:00
|
|
|
SPrefix = ejabberd_sql:escape_like_arg(Prefix),
|
2017-05-23 11:25:13 +02:00
|
|
|
SPrefix2 = <<SPrefix/binary, $%>>,
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(count(*))d from users "
|
2020-01-13 01:36:52 +01:00
|
|
|
"where username like %(SPrefix2)s %ESCAPE and %(LServer)H"));
|
2017-05-23 11:25:13 +02:00
|
|
|
users_number(LServer, []) ->
|
|
|
|
users_number(LServer).
|
|
|
|
|
2018-12-05 14:22:09 +01:00
|
|
|
which_users_exists(LServer, LUsers) when length(LUsers) =< 100 ->
|
|
|
|
try ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(username)s from users where username in %(LUsers)ls")) of
|
|
|
|
{selected, Matching} ->
|
|
|
|
[U || {U} <- Matching];
|
|
|
|
{error, _} = E ->
|
|
|
|
E
|
|
|
|
catch _:B ->
|
|
|
|
{error, B}
|
|
|
|
end;
|
|
|
|
which_users_exists(LServer, LUsers) ->
|
|
|
|
{First, Rest} = lists:split(100, LUsers),
|
|
|
|
case which_users_exists(LServer, First) of
|
|
|
|
{error, _} = E ->
|
|
|
|
E;
|
|
|
|
V ->
|
|
|
|
case which_users_exists(LServer, Rest) of
|
|
|
|
{error, _} = E2 ->
|
|
|
|
E2;
|
|
|
|
V2 ->
|
|
|
|
V ++ V2
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2017-10-25 20:21:52 +02:00
|
|
|
export(_Server) ->
|
|
|
|
[{passwd,
|
|
|
|
fun(Host, #passwd{us = {LUser, LServer}, password = Password})
|
|
|
|
when LServer == Host,
|
|
|
|
is_binary(Password) ->
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL("delete from users where username=%(LUser)s and %(LServer)H;"),
|
|
|
|
?SQL_INSERT(
|
|
|
|
"users",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"password=%(Password)s"])];
|
2021-11-17 16:50:14 +01:00
|
|
|
(Host, {passwd, {LUser, LServer},
|
|
|
|
{scram, StoredKey1, ServerKey, Salt, IterationCount}})
|
2021-09-27 17:05:42 +02:00
|
|
|
when LServer == Host ->
|
|
|
|
Hash = sha,
|
|
|
|
StoredKey = scram_hash_encode(Hash, StoredKey1),
|
|
|
|
[?SQL("delete from users where username=%(LUser)s and %(LServer)H;"),
|
|
|
|
?SQL_INSERT(
|
|
|
|
"users",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"password=%(StoredKey)s",
|
|
|
|
"serverkey=%(ServerKey)s",
|
|
|
|
"salt=%(Salt)s",
|
|
|
|
"iterationcount=%(IterationCount)d"])];
|
2017-10-25 20:21:52 +02:00
|
|
|
(Host, #passwd{us = {LUser, LServer}, password = #scram{} = Scram})
|
|
|
|
when LServer == Host ->
|
2020-12-08 12:06:52 +01:00
|
|
|
StoredKey = scram_hash_encode(Scram#scram.hash, Scram#scram.storedkey),
|
2017-10-25 20:21:52 +02:00
|
|
|
ServerKey = Scram#scram.serverkey,
|
|
|
|
Salt = Scram#scram.salt,
|
|
|
|
IterationCount = Scram#scram.iterationcount,
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL("delete from users where username=%(LUser)s and %(LServer)H;"),
|
|
|
|
?SQL_INSERT(
|
|
|
|
"users",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"password=%(StoredKey)s",
|
|
|
|
"serverkey=%(ServerKey)s",
|
|
|
|
"salt=%(Salt)s",
|
|
|
|
"iterationcount=%(IterationCount)d"])];
|
2017-10-25 20:21:52 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|