2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_auth_odbc.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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_auth_odbc).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-14 00:00:12 +01:00
|
|
|
|
|
|
|
%% External exports
|
2005-07-13 05:24:13 +02:00
|
|
|
-export([start/1,
|
2005-04-17 20:08:34 +02:00
|
|
|
set_password/3,
|
|
|
|
check_password/3,
|
|
|
|
check_password/5,
|
|
|
|
try_register/3,
|
2004-12-14 00:00:12 +01:00
|
|
|
dirty_get_registered_users/0,
|
2005-04-17 23:39:41 +02:00
|
|
|
get_vh_registered_users/1,
|
2007-11-16 14:58:00 +01:00
|
|
|
get_vh_registered_users/2,
|
2007-05-12 20:09:38 +02:00
|
|
|
get_vh_registered_users_number/1,
|
2007-11-16 14:58:00 +01:00
|
|
|
get_vh_registered_users_number/2,
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password/2,
|
|
|
|
get_password_s/2,
|
|
|
|
is_user_exists/2,
|
2004-12-14 00:00:12 +01:00
|
|
|
remove_user/2,
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user/3,
|
2004-12-14 00:00:12 +01:00
|
|
|
plain_password_required/0
|
|
|
|
]).
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
%% @spec (Host) -> ok
|
|
|
|
%% Host = string()
|
|
|
|
|
2008-10-13 12:11:19 +02:00
|
|
|
start(_Host) ->
|
2004-12-14 00:00:12 +01:00
|
|
|
ok.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec () -> bool()
|
|
|
|
|
2004-12-14 00:00:12 +01:00
|
|
|
plain_password_required() ->
|
|
|
|
false.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server, Password) -> bool()
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
check_password(User, Server, Password) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2009-03-04 19:34:02 +01:00
|
|
|
try odbc_queries:get_password(LServer, Username) of
|
2008-07-09 11:16:03 +02:00
|
|
|
{selected, ["password"], [{Password}]} ->
|
2009-03-04 19:34:02 +01:00
|
|
|
Password /= ""; %% Password is correct, and not empty
|
|
|
|
{selected, ["password"], [{_Password2}]} ->
|
|
|
|
false; %% Password is not correct
|
|
|
|
{selected, ["password"], []} ->
|
|
|
|
false; %% Account does not exist
|
|
|
|
{error, _Error} ->
|
|
|
|
false %% Typical error is that table doesn't exist
|
|
|
|
catch
|
|
|
|
_:_ ->
|
|
|
|
false %% Typical error is database not accessible
|
2008-07-09 11:16:03 +02:00
|
|
|
end
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
false
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2009-04-22 13:44:03 +02:00
|
|
|
%% @spec (User, Server, Password, Digest, DigestGen) -> bool()
|
2009-02-05 12:13:01 +01:00
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
%% Digest = string()
|
2009-04-22 13:44:03 +02:00
|
|
|
%% DigestGen = function()
|
2009-02-05 12:13:01 +01:00
|
|
|
|
2009-04-22 13:44:03 +02:00
|
|
|
check_password(User, Server, Password, Digest, DigestGen) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
try
|
2008-09-23 15:06:45 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
2008-07-09 11:16:03 +02:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2009-03-04 19:34:02 +01:00
|
|
|
try odbc_queries:get_password(LServer, Username) of
|
|
|
|
%% Account exists, check if password is valid
|
2008-07-09 11:16:03 +02:00
|
|
|
{selected, ["password"], [{Passwd}]} ->
|
|
|
|
DigRes = if
|
|
|
|
Digest /= "" ->
|
2009-04-22 13:44:03 +02:00
|
|
|
Digest == DigestGen(Passwd);
|
2008-07-09 11:16:03 +02:00
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end,
|
|
|
|
if DigRes ->
|
|
|
|
true;
|
|
|
|
true ->
|
|
|
|
(Passwd == Password) and (Password /= "")
|
|
|
|
end;
|
2009-03-04 19:34:02 +01:00
|
|
|
{selected, ["password"], []} ->
|
|
|
|
false; %% Account does not exist
|
|
|
|
{error, _Error} ->
|
|
|
|
false %% Typical error is that table doesn't exist
|
|
|
|
catch
|
|
|
|
_:_ ->
|
|
|
|
false %% Typical error is database not accessible
|
2008-07-09 11:16:03 +02:00
|
|
|
end
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
false
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server, Password) -> ok | {error, invalid_jid}
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
set_password(User, Server, Password) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
Pass = ejabberd_odbc:escape(Password),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2008-09-16 16:39:57 +02:00
|
|
|
case catch odbc_queries:set_password_t(LServer, Username, Pass) of
|
|
|
|
{atomic, ok} -> ok;
|
|
|
|
Other -> {error, Other}
|
|
|
|
end
|
2008-07-09 11:16:03 +02:00
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
{error, invalid_jid}
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @spec (User, Server, Password) -> {atomic, ok} | {atomic, exists} | {error, invalid_jid}
|
2009-02-05 12:13:01 +01:00
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
try_register(User, Server, Password) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
Pass = ejabberd_odbc:escape(Password),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
case catch odbc_queries:add_user(LServer, Username, Pass) of
|
|
|
|
{updated, 1} ->
|
|
|
|
{atomic, ok};
|
|
|
|
_ ->
|
|
|
|
{atomic, exists}
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
{error, invalid_jid}
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec () -> [{LUser, LServer}]
|
|
|
|
%% LUser = string()
|
|
|
|
%% LServer = string()
|
|
|
|
|
2004-12-14 00:00:12 +01:00
|
|
|
dirty_get_registered_users() ->
|
2008-02-19 14:49:29 +01:00
|
|
|
Servers = ejabberd_config:get_vh_by_auth_method(odbc),
|
|
|
|
lists:flatmap(
|
|
|
|
fun(Server) ->
|
|
|
|
get_vh_registered_users(Server)
|
|
|
|
end, Servers).
|
2005-07-13 05:24:13 +02:00
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Server) -> [{LUser, LServer}]
|
|
|
|
%% Server = string()
|
|
|
|
%% LUser = string()
|
|
|
|
%% LServer = string()
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
get_vh_registered_users(Server) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2006-09-03 17:15:46 +02:00
|
|
|
case catch odbc_queries:list_users(LServer) of
|
2004-12-14 00:00:12 +01:00
|
|
|
{selected, ["username"], Res} ->
|
2005-08-25 22:48:45 +02:00
|
|
|
[{U, LServer} || {U} <- Res];
|
2004-12-14 00:00:12 +01:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Server, Opts) -> [{LUser, LServer}]
|
|
|
|
%% Server = string()
|
|
|
|
%% Opts = [{Opt, Val}]
|
|
|
|
%% Opt = atom()
|
|
|
|
%% Val = term()
|
|
|
|
%% LUser = string()
|
|
|
|
%% LServer = string()
|
|
|
|
|
2007-11-03 18:35:23 +01:00
|
|
|
get_vh_registered_users(Server, Opts) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2007-11-03 18:35:23 +01:00
|
|
|
case catch odbc_queries:list_users(LServer, Opts) of
|
|
|
|
{selected, ["username"], Res} ->
|
|
|
|
[{U, LServer} || {U} <- Res];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Server) -> Users_Number
|
|
|
|
%% Server = string()
|
|
|
|
%% Users_Number = integer()
|
|
|
|
|
2007-05-12 20:09:38 +02:00
|
|
|
get_vh_registered_users_number(Server) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2007-05-12 20:09:38 +02:00
|
|
|
case catch odbc_queries:users_number(LServer) of
|
|
|
|
{selected, [_], [{Res}]} ->
|
|
|
|
list_to_integer(Res);
|
|
|
|
_ ->
|
|
|
|
0
|
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Server, Opts) -> Users_Number
|
|
|
|
%% Server = string()
|
|
|
|
%% Opts = [{Opt, Val}]
|
|
|
|
%% Opt = atom()
|
|
|
|
%% Val = term()
|
|
|
|
%% Users_Number = integer()
|
|
|
|
|
2007-11-03 18:35:23 +01:00
|
|
|
get_vh_registered_users_number(Server, Opts) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2007-11-03 18:35:23 +01:00
|
|
|
case catch odbc_queries:users_number(LServer, Opts) of
|
|
|
|
{selected, [_], [{Res}]} ->
|
|
|
|
list_to_integer(Res);
|
2007-12-07 00:15:04 +01:00
|
|
|
_Other ->
|
2007-11-03 18:35:23 +01:00
|
|
|
0
|
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server) -> Password | false
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
get_password(User, Server) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
case catch odbc_queries:get_password(LServer, Username) of
|
|
|
|
{selected, ["password"], [{Password}]} ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
false
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server) -> Password | nil()
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
get_password_s(User, Server) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
case catch odbc_queries:get_password(LServer, Username) of
|
|
|
|
{selected, ["password"], [{Password}]} ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
""
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
""
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server) -> bool()
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
|
2009-03-04 19:34:02 +01:00
|
|
|
%% @spec (User, Server) -> true | false | {error, Error}
|
2005-07-13 05:24:13 +02:00
|
|
|
is_user_exists(User, Server) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2009-03-04 19:34:02 +01:00
|
|
|
try odbc_queries:get_password(LServer, Username) of
|
2008-07-09 11:16:03 +02:00
|
|
|
{selected, ["password"], [{_Password}]} ->
|
2009-03-04 19:34:02 +01:00
|
|
|
true; %% Account exists
|
|
|
|
{selected, ["password"], []} ->
|
|
|
|
false; %% Account does not exist
|
|
|
|
{error, Error} ->
|
|
|
|
{error, Error} %% Typical error is that table doesn't exist
|
|
|
|
catch
|
|
|
|
_:B ->
|
|
|
|
{error, B} %% Typical error is database not accessible
|
2008-07-09 11:16:03 +02:00
|
|
|
end
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
false
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @spec (User, Server) -> ok | error
|
2009-02-05 12:13:01 +01:00
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
2009-01-19 12:59:40 +01:00
|
|
|
%% @doc Remove user.
|
2009-01-19 11:14:04 +01:00
|
|
|
%% Note: it may return ok even if there was some problem removing the user.
|
2009-02-05 12:13:01 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
remove_user(User, Server) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
catch odbc_queries:del_user(LServer, Username),
|
2009-01-19 11:14:04 +01:00
|
|
|
ok
|
2008-07-09 11:16:03 +02:00
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
error
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @spec (User, Server, Password) -> ok | error | not_exists | not_allowed
|
2009-02-05 12:13:01 +01:00
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
2009-01-19 12:59:40 +01:00
|
|
|
%% @doc Remove user if the provided password is correct.
|
2009-02-05 12:13:01 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
remove_user(User, Server, Password) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
Pass = ejabberd_odbc:escape(Password),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
F = fun() ->
|
|
|
|
Result = odbc_queries:del_user_return_password(
|
|
|
|
LServer, Username, Pass),
|
|
|
|
case Result of
|
|
|
|
{selected, ["password"], [{Password}]} ->
|
|
|
|
ok;
|
|
|
|
{selected, ["password"], []} ->
|
|
|
|
not_exists;
|
|
|
|
_ ->
|
|
|
|
not_allowed
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
{atomic, Result} = odbc_queries:sql_transaction(LServer, F),
|
|
|
|
Result
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
error
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|