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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2008-07-31 13:18:49 +02:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 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.
|
|
|
|
%%%
|
|
|
|
%%% 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 20:56:29 +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 20:56:29 +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,
|
2006-02-20 05:07:42 +01:00
|
|
|
plain_password_required/1,
|
|
|
|
ctl_process_get_registered/3
|
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").
|
2006-02-20 05:07:42 +01:00
|
|
|
-include("ejabberd_ctl.hrl").
|
2005-04-20 01:10:22 +02:00
|
|
|
|
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 20:56:29 +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) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
lists:any(
|
|
|
|
fun(M) ->
|
|
|
|
M:check_password(User, Server, Password)
|
|
|
|
end, auth_modules(Server)).
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2008-04-22 20:56:29 +02:00
|
|
|
%% @doc Check if the user and password can login in server.
|
|
|
|
%% @spec (User::string(), Server::string(), Password::string(),
|
|
|
|
%% StreamID::string(), Digest::string()) ->
|
|
|
|
%% true | false
|
2005-04-17 20:08:34 +02:00
|
|
|
check_password(User, Server, Password, StreamID, Digest) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
lists:any(
|
|
|
|
fun(M) ->
|
|
|
|
M:check_password(User, Server, Password, StreamID, Digest)
|
|
|
|
end, auth_modules(Server)).
|
2002-12-20 21:42:08 +01:00
|
|
|
|
2008-04-22 20:56:29 +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) ->
|
|
|
|
Res = lists:dropwhile(
|
|
|
|
fun(M) ->
|
|
|
|
not apply(M, check_password,
|
|
|
|
[User, Server, Password])
|
|
|
|
end, auth_modules(Server)),
|
|
|
|
case Res of
|
|
|
|
[] -> false;
|
|
|
|
[AuthMod | _] -> {true, AuthMod}
|
|
|
|
end.
|
|
|
|
|
|
|
|
check_password_with_authmodule(User, Server, Password, StreamID, Digest) ->
|
|
|
|
Res = lists:dropwhile(
|
|
|
|
fun(M) ->
|
|
|
|
not apply(M, check_password,
|
|
|
|
[User, Server, Password, StreamID, Digest])
|
|
|
|
end, auth_modules(Server)),
|
|
|
|
case Res of
|
|
|
|
[] -> false;
|
|
|
|
[AuthMod | _] -> {true, AuthMod}
|
|
|
|
end.
|
|
|
|
|
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
|
|
|
|
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 ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(_M, {atomic, ok} = Res) ->
|
|
|
|
Res;
|
|
|
|
(M, _) ->
|
|
|
|
M:try_register(User, Server, Password)
|
|
|
|
end, {error, not_allowed}, auth_modules(Server));
|
|
|
|
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 16:19:10 +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) ->
|
|
|
|
M:get_vh_registered_users(Server, Opts)
|
|
|
|
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 20:56:29 +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 20:56:29 +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) ->
|
|
|
|
M:is_user_exists(User, Server)
|
|
|
|
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
|
|
|
|
is_user_exists_in_other_modules(Module, User, Server) ->
|
|
|
|
lists:any(
|
|
|
|
fun(M) ->
|
|
|
|
M:is_user_exists(User, Server)
|
|
|
|
end, auth_modules(Server)--[Module]).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(M) ->
|
|
|
|
M:remove_user(User, Server)
|
|
|
|
end, auth_modules(Server)).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
remove_user(User, Server, Password) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(M) ->
|
|
|
|
M:remove_user(User, Server, Password)
|
|
|
|
end, auth_modules(Server)).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2006-02-20 05:07:42 +01:00
|
|
|
ctl_process_get_registered(_Val, Host, ["registered-users"]) ->
|
|
|
|
Users = ejabberd_auth:get_vh_registered_users(Host),
|
|
|
|
NewLine = io_lib:format("~n", []),
|
|
|
|
SUsers = lists:sort(Users),
|
|
|
|
FUsers = lists:map(fun({U, _S}) -> [U, NewLine] end, SUsers),
|
2008-03-21 16:00:59 +01:00
|
|
|
?PRINT("~s", [FUsers]),
|
2006-02-20 05:07:42 +01:00
|
|
|
{stop, ?STATUS_SUCCESS};
|
|
|
|
ctl_process_get_registered(Val, _Host, _Args) ->
|
|
|
|
Val.
|
|
|
|
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
2008-02-19 16:19:10 +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].
|