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>
|
2004-12-12 22:00:34 +01:00
|
|
|
%%% Purpose : Authentification
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 23 Nov 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:11:32 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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
|
|
|
%%%
|
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
|
|
|
|
%%%
|
2002-11-23 21:55:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2006-04-07 02:39:24 +02:00
|
|
|
%% TODO: Use the functions in ejabberd auth to add and remove users.
|
|
|
|
|
2002-11-23 21:55:05 +01:00
|
|
|
-module(ejabberd_auth).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-11-23 21:55:05 +01:00
|
|
|
|
|
|
|
%% External exports
|
2004-12-12 22:00:34 +01:00
|
|
|
-export([start/0,
|
2005-04-17 20:08:34 +02:00
|
|
|
set_password/3,
|
|
|
|
check_password/3,
|
|
|
|
check_password/5,
|
2008-04-22 19:41:30 +02:00
|
|
|
check_password_with_authmodule/3,
|
|
|
|
check_password_with_authmodule/5,
|
2005-04-17 20:08:34 +02:00
|
|
|
try_register/3,
|
2003-01-05 21:24:59 +01:00
|
|
|
dirty_get_registered_users/0,
|
2005-04-17 20:08:34 +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,
|
2008-04-22 19:41:30 +02:00
|
|
|
get_password_with_authmodule/2,
|
2005-04-17 20:08:34 +02:00
|
|
|
is_user_exists/2,
|
2007-11-16 14:58:00 +01:00
|
|
|
is_user_exists_in_other_modules/3,
|
2003-11-23 21:11:21 +01:00
|
|
|
remove_user/2,
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user/3,
|
2008-10-12 14:01:56 +02:00
|
|
|
plain_password_required/1
|
2003-11-23 21:11:21 +01:00
|
|
|
]).
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2006-04-07 02:39:24 +02:00
|
|
|
-export([auth_modules/1]).
|
|
|
|
|
2005-04-20 01:10:22 +02:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2002-11-23 21:55:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
start() ->
|
2006-04-07 02:39:24 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(M) ->
|
|
|
|
M:start(Host)
|
|
|
|
end, auth_modules(Host))
|
|
|
|
end, ?MYHOSTS).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
plain_password_required(Server) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
lists:any(
|
|
|
|
fun(M) ->
|
|
|
|
M:plain_password_required()
|
|
|
|
end, auth_modules(Server)).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2008-04-22 19:41:30 +02:00
|
|
|
%% @doc Check if the user and password can login in server.
|
|
|
|
%% @spec (User::string(), Server::string(), Password::string()) ->
|
|
|
|
%% true | false
|
2005-04-17 20:08:34 +02:00
|
|
|
check_password(User, Server, Password) ->
|
2009-03-04 19:34:02 +01:00
|
|
|
case check_password_with_authmodule(User, Server, Password) of
|
|
|
|
{true, _AuthModule} -> true;
|
|
|
|
false -> false
|
|
|
|
end.
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2008-04-22 19:41:30 +02:00
|
|
|
%% @doc Check if the user and password can login in server.
|
2009-08-17 19:16:43 +02:00
|
|
|
%% @spec (User::string(), Server::string(), Password::string(),
|
2009-04-22 13:44:03 +02:00
|
|
|
%% Digest::string(), DigestGen::function()) ->
|
2008-04-22 19:41:30 +02:00
|
|
|
%% true | false
|
2009-04-22 13:44:03 +02:00
|
|
|
check_password(User, Server, Password, Digest, DigestGen) ->
|
2009-03-04 19:34:02 +01:00
|
|
|
case check_password_with_authmodule(User, Server, Password,
|
2009-04-22 13:44:03 +02:00
|
|
|
Digest, DigestGen) of
|
2009-03-04 19:34:02 +01:00
|
|
|
{true, _AuthModule} -> true;
|
|
|
|
false -> false
|
|
|
|
end.
|
2002-12-20 21:42:08 +01:00
|
|
|
|
2008-04-22 19:41:30 +02:00
|
|
|
%% @doc Check if the user and password can login in server.
|
|
|
|
%% The user can login if at least an authentication method accepts the user
|
|
|
|
%% and the password.
|
|
|
|
%% The first authentication method that accepts the credentials is returned.
|
|
|
|
%% @spec (User::string(), Server::string(), Password::string()) ->
|
|
|
|
%% {true, AuthModule} | false
|
|
|
|
%% where
|
|
|
|
%% AuthModule = ejabberd_auth_anonymous | ejabberd_auth_external
|
|
|
|
%% | ejabberd_auth_internal | ejabberd_auth_ldap
|
|
|
|
%% | ejabberd_auth_odbc | ejabberd_auth_pam
|
|
|
|
check_password_with_authmodule(User, Server, Password) ->
|
2009-03-04 19:34:02 +01:00
|
|
|
check_password_loop(auth_modules(Server), [User, Server, Password]).
|
2008-04-22 19:41:30 +02:00
|
|
|
|
2009-04-22 13:44:03 +02:00
|
|
|
check_password_with_authmodule(User, Server, Password, Digest, DigestGen) ->
|
2009-03-04 19:34:02 +01:00
|
|
|
check_password_loop(auth_modules(Server), [User, Server, Password,
|
2009-04-22 13:44:03 +02:00
|
|
|
Digest, DigestGen]).
|
2009-03-04 19:34:02 +01:00
|
|
|
|
|
|
|
check_password_loop([], _Args) ->
|
|
|
|
false;
|
|
|
|
check_password_loop([AuthModule | AuthModules], Args) ->
|
|
|
|
case apply(AuthModule, check_password, Args) of
|
|
|
|
true ->
|
|
|
|
{true, AuthModule};
|
|
|
|
false ->
|
|
|
|
check_password_loop(AuthModules, Args)
|
2008-04-22 19:41:30 +02:00
|
|
|
end.
|
|
|
|
|
2009-03-04 19:34:02 +01:00
|
|
|
|
2008-08-18 20:21:10 +02:00
|
|
|
%% @spec (User::string(), Server::string(), Password::string()) ->
|
|
|
|
%% ok | {error, ErrorType}
|
|
|
|
%% where ErrorType = empty_password | not_allowed | invalid_jid
|
2008-02-11 19:19:42 +01:00
|
|
|
set_password(_User, _Server, "") ->
|
2008-08-18 20:21:10 +02:00
|
|
|
%% We do not allow empty password
|
|
|
|
{error, empty_password};
|
2005-04-17 20:08:34 +02:00
|
|
|
set_password(User, Server, Password) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
lists:foldl(
|
|
|
|
fun(M, {error, _}) ->
|
|
|
|
M:set_password(User, Server, Password);
|
|
|
|
(_M, Res) ->
|
|
|
|
Res
|
|
|
|
end, {error, not_allowed}, auth_modules(Server)).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2008-12-23 20:15:33 +01:00
|
|
|
%% @spec (User, Server, Password) -> {atomic, ok} | {atomic, exists} | {error, not_allowed}
|
2008-02-11 19:19:42 +01:00
|
|
|
try_register(_User, _Server, "") ->
|
2008-08-18 20:21:10 +02:00
|
|
|
%% We do not allow empty password
|
2008-02-11 19:19:42 +01:00
|
|
|
{error, not_allowed};
|
2005-04-17 20:08:34 +02:00
|
|
|
try_register(User, Server, Password) ->
|
2006-04-22 05:35:13 +02:00
|
|
|
case is_user_exists(User,Server) of
|
2005-04-20 01:10:22 +02:00
|
|
|
true ->
|
2006-04-22 05:35:13 +02:00
|
|
|
{atomic, exists};
|
2005-04-20 01:10:22 +02:00
|
|
|
false ->
|
2006-04-22 05:35:13 +02:00
|
|
|
case lists:member(jlib:nameprep(Server), ?MYHOSTS) of
|
|
|
|
true ->
|
2008-12-23 20:15:33 +01:00
|
|
|
Res = lists:foldl(
|
2006-04-22 05:35:13 +02:00
|
|
|
fun(_M, {atomic, ok} = Res) ->
|
|
|
|
Res;
|
|
|
|
(M, _) ->
|
|
|
|
M:try_register(User, Server, Password)
|
2008-12-23 20:15:33 +01:00
|
|
|
end, {error, not_allowed}, auth_modules(Server)),
|
|
|
|
case Res of
|
|
|
|
{atomic, ok} ->
|
|
|
|
ejabberd_hooks:run(register_user, Server,
|
|
|
|
[User, Server]),
|
|
|
|
{atomic, ok};
|
|
|
|
_ -> Res
|
|
|
|
end;
|
2006-04-22 05:35:13 +02:00
|
|
|
false ->
|
|
|
|
{error, not_allowed}
|
|
|
|
end
|
2005-04-20 01:10:22 +02:00
|
|
|
end.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2006-04-07 02:39:24 +02:00
|
|
|
%% Registered users list do not include anonymous users logged
|
2003-01-01 20:54:44 +01:00
|
|
|
dirty_get_registered_users() ->
|
2006-04-07 02:39:24 +02:00
|
|
|
lists:flatmap(
|
|
|
|
fun(M) ->
|
|
|
|
M:dirty_get_registered_users()
|
2008-02-19 14:49:29 +01:00
|
|
|
end, auth_modules()).
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2006-04-07 02:39:24 +02:00
|
|
|
%% Registered users list do not include anonymous users logged
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_registered_users(Server) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
lists:flatmap(
|
|
|
|
fun(M) ->
|
|
|
|
M:get_vh_registered_users(Server)
|
|
|
|
end, auth_modules(Server)).
|
2003-03-12 20:48:05 +01:00
|
|
|
|
2007-11-03 18:35:23 +01:00
|
|
|
get_vh_registered_users(Server, Opts) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun(M) ->
|
2009-02-13 14:28:10 +01:00
|
|
|
case erlang:function_exported(
|
|
|
|
M, get_vh_registered_users, 2) of
|
|
|
|
true ->
|
|
|
|
M:get_vh_registered_users(Server, Opts);
|
|
|
|
false ->
|
|
|
|
M:get_vh_registered_users(Server)
|
|
|
|
end
|
2007-11-03 18:35:23 +01:00
|
|
|
end, auth_modules(Server)).
|
|
|
|
|
2007-05-12 20:09:38 +02:00
|
|
|
get_vh_registered_users_number(Server) ->
|
|
|
|
lists:sum(
|
|
|
|
lists:map(
|
|
|
|
fun(M) ->
|
|
|
|
case erlang:function_exported(
|
|
|
|
M, get_vh_registered_users_number, 1) of
|
|
|
|
true ->
|
|
|
|
M:get_vh_registered_users_number(Server);
|
|
|
|
false ->
|
|
|
|
length(M:get_vh_registered_users(Server))
|
2007-11-03 18:35:23 +01:00
|
|
|
end
|
|
|
|
end, auth_modules(Server))).
|
|
|
|
|
|
|
|
get_vh_registered_users_number(Server, Opts) ->
|
|
|
|
lists:sum(
|
|
|
|
lists:map(
|
|
|
|
fun(M) ->
|
|
|
|
case erlang:function_exported(
|
|
|
|
M, get_vh_registered_users_number, 2) of
|
|
|
|
true ->
|
|
|
|
M:get_vh_registered_users_number(Server, Opts);
|
|
|
|
false ->
|
|
|
|
length(M:get_vh_registered_users(Server))
|
2007-05-12 20:09:38 +02:00
|
|
|
end
|
|
|
|
end, auth_modules(Server))).
|
|
|
|
|
2008-04-22 19:41:30 +02:00
|
|
|
%% @doc Get the password of the user.
|
|
|
|
%% @spec (User::string(), Server::string()) -> Password::string()
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password(User, Server) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
lists:foldl(
|
|
|
|
fun(M, false) ->
|
|
|
|
M:get_password(User, Server);
|
|
|
|
(_M, Password) ->
|
|
|
|
Password
|
|
|
|
end, false, auth_modules(Server)).
|
2003-01-21 21:36:55 +01:00
|
|
|
|
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
|
|
|
|
false ->
|
|
|
|
"";
|
|
|
|
Password ->
|
|
|
|
Password
|
|
|
|
end.
|
2003-01-26 21:16:53 +01:00
|
|
|
|
2008-04-22 19:41:30 +02:00
|
|
|
%% @doc Get the password of the user and the auth module.
|
|
|
|
%% @spec (User::string(), Server::string()) ->
|
|
|
|
%% {Password::string(), AuthModule::atom()} | {false, none}
|
|
|
|
get_password_with_authmodule(User, Server) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(M, {false, _}) ->
|
|
|
|
{M:get_password(User, Server), M};
|
|
|
|
(_M, {Password, AuthModule}) ->
|
|
|
|
{Password, AuthModule}
|
|
|
|
end, {false, none}, auth_modules(Server)).
|
|
|
|
|
2006-04-07 02:39:24 +02:00
|
|
|
%% Returns true if the user exists in the DB or if an anonymous user is logged
|
|
|
|
%% under the given name
|
2005-04-17 20:08:34 +02:00
|
|
|
is_user_exists(User, Server) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
lists:any(
|
|
|
|
fun(M) ->
|
2010-05-28 01:35:59 +02:00
|
|
|
case M:is_user_exists(User, Server) of
|
|
|
|
{error, Error} ->
|
|
|
|
?ERROR_MSG("The authentication module ~p returned an "
|
|
|
|
"error~nwhen checking user ~p in server ~p~n"
|
|
|
|
"Error message: ~p",
|
|
|
|
[M, User, Server, Error]),
|
|
|
|
false;
|
|
|
|
Else ->
|
|
|
|
Else
|
|
|
|
end
|
2006-04-07 02:39:24 +02:00
|
|
|
end, auth_modules(Server)).
|
2003-02-21 20:52:15 +01:00
|
|
|
|
2006-06-13 18:52:38 +02:00
|
|
|
%% Check if the user exists in all authentications module except the module
|
|
|
|
%% passed as parameter
|
2009-03-04 19:34:02 +01:00
|
|
|
%% @spec (Module::atom(), User, Server) -> true | false | maybe
|
2006-06-13 18:52:38 +02:00
|
|
|
is_user_exists_in_other_modules(Module, User, Server) ->
|
2009-03-04 19:34:02 +01:00
|
|
|
is_user_exists_in_other_modules_loop(
|
|
|
|
auth_modules(Server)--[Module],
|
|
|
|
User, Server).
|
|
|
|
is_user_exists_in_other_modules_loop([], _User, _Server) ->
|
|
|
|
false;
|
|
|
|
is_user_exists_in_other_modules_loop([AuthModule|AuthModules], User, Server) ->
|
|
|
|
case AuthModule:is_user_exists(User, Server) of
|
|
|
|
true ->
|
|
|
|
true;
|
|
|
|
false ->
|
|
|
|
is_user_exists_in_other_modules_loop(AuthModules, User, Server);
|
|
|
|
{error, Error} ->
|
|
|
|
?DEBUG("The authentication module ~p returned an error~nwhen "
|
|
|
|
"checking user ~p in server ~p~nError message: ~p",
|
|
|
|
[AuthModule, User, Server, Error]),
|
|
|
|
maybe
|
|
|
|
end.
|
|
|
|
|
2006-06-13 18:52:38 +02:00
|
|
|
|
2008-12-23 20:15:33 +01:00
|
|
|
%% @spec (User, Server) -> ok | error | {error, not_allowed}
|
2009-01-03 22:29:54 +01:00
|
|
|
%% @doc Remove user.
|
2008-12-23 20:15:33 +01:00
|
|
|
%% Note: it may return ok even if there was some problem removing the user.
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2008-12-23 20:15:33 +01:00
|
|
|
R = lists:foreach(
|
2006-04-07 02:39:24 +02:00
|
|
|
fun(M) ->
|
|
|
|
M:remove_user(User, Server)
|
2008-12-23 20:15:33 +01:00
|
|
|
end, auth_modules(Server)),
|
|
|
|
case R of
|
|
|
|
ok -> ejabberd_hooks:run(remove_user, jlib:nameprep(Server), [User, Server]);
|
|
|
|
_ -> none
|
|
|
|
end,
|
|
|
|
R.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2008-12-23 20:15:33 +01:00
|
|
|
%% @spec (User, Server, Password) -> ok | not_exists | not_allowed | bad_request | error
|
2009-01-03 22:29:54 +01:00
|
|
|
%% @doc Try to remove user if the provided password is correct.
|
2008-12-23 20:15:33 +01:00
|
|
|
%% The removal is attempted in each auth method provided:
|
|
|
|
%% when one returns 'ok' the loop stops;
|
|
|
|
%% if no method returns 'ok' then it returns the error message indicated by the last method attempted.
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server, Password) ->
|
2008-12-23 20:15:33 +01:00
|
|
|
R = lists:foldl(
|
|
|
|
fun(_M, ok = Res) ->
|
|
|
|
Res;
|
|
|
|
(M, _) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
M:remove_user(User, Server, Password)
|
2008-12-23 20:15:33 +01:00
|
|
|
end, error, auth_modules(Server)),
|
|
|
|
case R of
|
|
|
|
ok -> ejabberd_hooks:run(remove_user, jlib:nameprep(Server), [User, Server]);
|
|
|
|
_ -> none
|
|
|
|
end,
|
|
|
|
R.
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2006-02-20 05:07:42 +01:00
|
|
|
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
2008-02-19 14:49:29 +01:00
|
|
|
%% Return the lists of all the auth modules actually used in the
|
|
|
|
%% configuration
|
|
|
|
auth_modules() ->
|
|
|
|
lists:usort(
|
|
|
|
lists:flatmap(
|
|
|
|
fun(Server) ->
|
|
|
|
auth_modules(Server)
|
|
|
|
end, ?MYHOSTS)).
|
|
|
|
|
|
|
|
%% Return the list of authenticated modules for a given host
|
2006-04-07 02:39:24 +02:00
|
|
|
auth_modules(Server) ->
|
2005-07-13 05:24:13 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2006-02-07 18:35:32 +01:00
|
|
|
Method = ejabberd_config:get_local_option({auth_method, LServer}),
|
2006-04-07 02:39:24 +02:00
|
|
|
Methods = if
|
2006-07-05 16:36:21 +02:00
|
|
|
Method == undefined -> [];
|
2006-04-07 02:39:24 +02:00
|
|
|
is_list(Method) -> Method;
|
|
|
|
is_atom(Method) -> [Method]
|
|
|
|
end,
|
|
|
|
[list_to_atom("ejabberd_auth_" ++ atom_to_list(M)) || M <- Methods].
|