2002-11-23 21:55:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_auth.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
2004-12-12 22:00:34 +01:00
|
|
|
%%% Purpose : Authentification
|
2002-11-23 21:55:05 +01:00
|
|
|
%%% Created : 23 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
|
2006-04-07 02:39:24 +02:00
|
|
|
%%% Updated : 23 Feb 2006 by Mickael Remond <mremond@process-one.net>
|
|
|
|
%%% for anonymous login support
|
2002-11-23 21:55:05 +01:00
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
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).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
|
|
|
%% 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,
|
|
|
|
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-05-12 20:09:38 +02:00
|
|
|
get_vh_registered_users_number/1,
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password/2,
|
|
|
|
get_password_s/2,
|
|
|
|
is_user_exists/2,
|
2006-06-13 18:52:38 +02: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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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()
|
|
|
|
end, auth_modules(?MYNAME)).
|
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-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))
|
|
|
|
end
|
|
|
|
end, auth_modules(Server))).
|
|
|
|
|
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
|
|
|
|
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),
|
|
|
|
io:format("~s", [FUsers]),
|
|
|
|
{stop, ?STATUS_SUCCESS};
|
|
|
|
ctl_process_get_registered(Val, _Host, _Args) ->
|
|
|
|
Val.
|
|
|
|
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
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].
|