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>
|
2004-12-14 00:00:12 +01:00
|
|
|
%%% Purpose : Authentification via ODBC
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 12 Dec 2004 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
|
|
|
%%%
|
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
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
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-23 11:25:13 +02:00
|
|
|
-behaviour(ejabberd_config).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
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,
|
2018-12-05 14:22:09 +01:00
|
|
|
convert_to_scram/1, opt_type/1, export/1, which_users_exists/2]).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2018-06-14 13:00:47 +02:00
|
|
|
-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
|
|
|
|
%%%----------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
start(_Host) -> ok.
|
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) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
F = fun() ->
|
|
|
|
if is_record(Password, scram) ->
|
|
|
|
set_password_scram_t(
|
2017-11-02 15:03:30 +01:00
|
|
|
User, Server,
|
2017-05-23 11:25:13 +02:00
|
|
|
Password#scram.storedkey, Password#scram.serverkey,
|
|
|
|
Password#scram.salt, Password#scram.iterationcount);
|
|
|
|
true ->
|
2017-11-02 15:03:30 +01:00
|
|
|
set_password_t(User, Server, Password)
|
2017-05-23 11:25:13 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
case ejabberd_sql:sql_transaction(Server, F) of
|
2017-05-11 13:37:21 +02:00
|
|
|
{atomic, _} ->
|
|
|
|
ok;
|
2017-12-17 17:46:55 +01:00
|
|
|
{aborted, _} ->
|
2017-05-11 13:37:21 +02:00
|
|
|
{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) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
Res = if is_record(Password, scram) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
add_user_scram(
|
2017-05-11 13:37:21 +02:00
|
|
|
Server, User,
|
|
|
|
Password#scram.storedkey, Password#scram.serverkey,
|
|
|
|
Password#scram.salt, Password#scram.iterationcount);
|
|
|
|
true ->
|
2017-05-23 11:25:13 +02:00
|
|
|
add_user(Server, User, Password)
|
2017-05-11 13:37:21 +02:00
|
|
|
end,
|
|
|
|
case Res of
|
|
|
|
{updated, 1} -> ok;
|
|
|
|
_ -> {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}]} ->
|
|
|
|
{ok, Password};
|
|
|
|
{selected, [{StoredKey, ServerKey, Salt, IterationCount}]} ->
|
|
|
|
{ok, #scram{storedkey = StoredKey,
|
|
|
|
serverkey = ServerKey,
|
|
|
|
salt = Salt,
|
|
|
|
iterationcount = IterationCount}};
|
|
|
|
{selected, []} ->
|
|
|
|
error;
|
2017-12-17 17:46:55 +01:00
|
|
|
_ ->
|
2017-05-11 13:37:21 +02:00
|
|
|
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).
|
|
|
|
|
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",
|
2017-05-23 11:25:13 +02:00
|
|
|
"password=%(Password)s"]).
|
|
|
|
|
|
|
|
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) ->
|
|
|
|
SPrefix = ejabberd_sql:escape_like_arg_circumflex(Prefix),
|
|
|
|
SPrefix2 = <<SPrefix/binary, $%>>,
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(username)s from users "
|
2017-11-02 15:03:30 +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
|
|
|
|
ejabberd_config:get_option(
|
|
|
|
{pgsql_users_number_estimate, LServer}, false) of
|
|
|
|
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) ->
|
|
|
|
SPrefix = ejabberd_sql:escape_like_arg_circumflex(Prefix),
|
|
|
|
SPrefix2 = <<SPrefix/binary, $%>>,
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(count(*))d from users "
|
2017-11-02 15:03:30 +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.
|
|
|
|
|
|
|
|
|
2015-03-09 14:38:16 +01:00
|
|
|
convert_to_scram(Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2015-03-09 14:38:16 +01:00
|
|
|
if
|
|
|
|
LServer == error;
|
|
|
|
LServer == <<>> ->
|
|
|
|
{error, {incorrect_server_name, Server}};
|
|
|
|
true ->
|
|
|
|
F = fun () ->
|
2016-05-04 20:01:05 +02:00
|
|
|
BatchSize = ?BATCH_SIZE,
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql:sql_query_t(
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL("select @(username)s, @(password)s"
|
|
|
|
" from users"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where iterationcount=0 and %(LServer)H"
|
2016-05-04 20:01:05 +02:00
|
|
|
" limit %(BatchSize)d")) of
|
|
|
|
{selected, []} ->
|
2015-03-09 14:38:16 +01:00
|
|
|
ok;
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, Rs} ->
|
2015-03-09 14:38:16 +01:00
|
|
|
lists:foreach(
|
2016-05-04 20:01:05 +02:00
|
|
|
fun({LUser, Password}) ->
|
2017-02-12 08:06:30 +01:00
|
|
|
case jid:resourceprep(Password) of
|
|
|
|
error ->
|
|
|
|
?ERROR_MSG(
|
|
|
|
"SASLprep failed for "
|
|
|
|
"password of user ~s@~s",
|
|
|
|
[LUser, LServer]);
|
|
|
|
_ ->
|
2017-05-11 13:37:21 +02:00
|
|
|
Scram = ejabberd_auth:password_to_scram(Password),
|
2017-02-12 08:06:30 +01:00
|
|
|
set_password_scram_t(
|
2017-11-02 15:03:30 +01:00
|
|
|
LUser, LServer,
|
2017-02-12 08:06:30 +01:00
|
|
|
Scram#scram.storedkey,
|
|
|
|
Scram#scram.serverkey,
|
|
|
|
Scram#scram.salt,
|
|
|
|
Scram#scram.iterationcount)
|
|
|
|
end
|
2015-03-09 14:38:16 +01:00
|
|
|
end, Rs),
|
|
|
|
continue;
|
|
|
|
Err -> {bad_reply, Err}
|
|
|
|
end
|
|
|
|
end,
|
2017-05-23 11:25:13 +02:00
|
|
|
case ejabberd_sql:sql_transaction(LServer, F) of
|
2015-03-09 14:38:16 +01:00
|
|
|
{atomic, ok} -> ok;
|
|
|
|
{atomic, continue} -> convert_to_scram(Server);
|
|
|
|
{atomic, Error} -> {error, Error};
|
|
|
|
Error -> Error
|
|
|
|
end
|
|
|
|
end.
|
2017-05-23 11:25:13 +02:00
|
|
|
|
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"])];
|
2017-10-25 20:21:52 +02:00
|
|
|
(Host, #passwd{us = {LUser, LServer}, password = #scram{} = Scram})
|
|
|
|
when LServer == Host ->
|
|
|
|
StoredKey = Scram#scram.storedkey,
|
|
|
|
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}].
|
|
|
|
|
2018-09-09 08:59:08 +02:00
|
|
|
-spec opt_type(atom()) -> fun((any()) -> any()) | [atom()].
|
2017-05-23 11:25:13 +02:00
|
|
|
opt_type(pgsql_users_number_estimate) ->
|
|
|
|
fun (V) when is_boolean(V) -> V end;
|
|
|
|
opt_type(_) -> [pgsql_users_number_estimate].
|