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:15:16 +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-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
|
|
|
|
%%%
|
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-13 12:11:19 +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").
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @type authmodule() = ejabberd_auth_anonymous | ejabberd_auth_external |
|
|
|
|
%% ejabberd_auth_internal | ejabberd_auth_ldap |
|
|
|
|
%% ejabberd_auth_odbc | ejabberd_auth_pam | atom().
|
|
|
|
|
2002-11-23 21:55:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
%% @spec () -> term()
|
|
|
|
|
2002-11-23 21:55:05 +01:00
|
|
|
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
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Server) -> bool()
|
|
|
|
%% Server = string()
|
|
|
|
|
|
|
|
plain_password_required(Server) when is_list(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
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server, Password) -> bool()
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
2008-04-22 19:41:30 +02:00
|
|
|
%% @doc Check if the user and password can login in server.
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
check_password(User, Server, Password)
|
|
|
|
when is_list(User), is_list(Server), is_list(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
|
|
|
|
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()
|
2008-04-22 19:41:30 +02:00
|
|
|
%% @doc Check if the user and password can login in server.
|
2009-02-05 12:13:01 +01:00
|
|
|
|
2009-04-22 13:44:03 +02:00
|
|
|
check_password(User, Server, Password, Digest, DigestGen)
|
2009-02-05 12:13:01 +01:00
|
|
|
when is_list(User), is_list(Server), is_list(Password),
|
2009-04-22 13:44:03 +02:00
|
|
|
is_list(Digest), is_function(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
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server, Password) -> {true, AuthModule} | false
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
%% AuthModule = authmodule()
|
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.
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
check_password_with_authmodule(User, Server, Password)
|
|
|
|
when is_list(User), is_list(Server), is_list(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
|
|
|
%% @spec (User, Server, Password, Digest, DigestGen) -> {true, AuthModule} | false
|
2009-02-05 12:13:01 +01:00
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
2009-02-21 15:41:29 +01:00
|
|
|
%% Password = string() | undefined
|
|
|
|
%% Digest = string() | undefined
|
2009-04-22 13:44:03 +02:00
|
|
|
%% DigestGen = function()
|
2009-02-05 12:13:01 +01:00
|
|
|
%% AuthModule = authmodule()
|
2009-08-17 19:17:34 +02:00
|
|
|
%% @doc Check the password is valid and also return the authentication module that accepts it.
|
2009-02-21 15:41:29 +01:00
|
|
|
%% The password is 'undefined' if the client
|
2010-01-09 17:38:07 +01:00
|
|
|
%% authenticates using the digest method as defined in
|
2009-02-21 15:41:29 +01:00
|
|
|
%% XEP-0078: Non-SASL Authentication
|
2009-02-05 12:13:01 +01:00
|
|
|
|
2009-04-22 13:44:03 +02:00
|
|
|
check_password_with_authmodule(User, Server, Password, Digest, DigestGen)
|
2009-02-21 15:41:29 +01:00
|
|
|
when is_list(User), is_list(Server), (is_list(Password) orelse Password == 'undefined'),
|
2009-04-22 13:44:03 +02:00
|
|
|
is_function(DigestGen), (is_list(Digest) orelse Digest == 'undefined')->
|
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-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server, Password) -> ok | {error, ErrorType}
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
%% ErrorType = empty_password | not_allowed | invalid_jid
|
2008-02-11 19:19:42 +01:00
|
|
|
set_password(_User, _Server, "") ->
|
2008-09-16 16:39:57 +02:00
|
|
|
%% We do not allow empty password
|
|
|
|
{error, empty_password};
|
2009-02-05 12:13:01 +01:00
|
|
|
set_password(User, Server, Password)
|
|
|
|
when is_list(User), is_list(Server), is_list(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
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @spec (User, Server, Password) -> {atomic, ok} | {atomic, exists} | {error, not_allowed}
|
2009-02-05 12:13:01 +01:00
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string() | nil()
|
2008-02-11 19:19:42 +01:00
|
|
|
try_register(_User, _Server, "") ->
|
2008-09-16 16:39:57 +02:00
|
|
|
%% We do not allow empty password
|
2010-01-09 17:38:07 +01:00
|
|
|
{error, not_allowed};
|
2009-02-05 12:13:01 +01:00
|
|
|
try_register(User, Server, Password)
|
|
|
|
when is_list(User), is_list(Server), is_list(Password) ->
|
2010-01-09 17:38:07 +01: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 ->
|
2008-07-09 11:16:03 +02:00
|
|
|
case lists:member(exmpp_stringprep:nameprep(Server), ?MYHOSTS) of
|
2006-04-22 05:35:13 +02:00
|
|
|
true ->
|
2009-01-19 11:14:04 +01:00
|
|
|
Res = lists:foldl(
|
2010-01-09 17:38:07 +01:00
|
|
|
fun (_M, {atomic, ok} = Res) ->
|
|
|
|
Res;
|
|
|
|
(M, _) ->
|
|
|
|
M:try_register(User, Server, Password)
|
|
|
|
end, {error, not_allowed}, auth_modules(Server)),
|
|
|
|
trigger_register_hooks(Res, User, Server);
|
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
|
|
|
|
2010-01-09 17:38:07 +01:00
|
|
|
trigger_register_hooks({atomic, ok} = Res, User, Server) ->
|
|
|
|
ejabberd_hooks:run(register_user, list_to_binary(Server),
|
|
|
|
[User, Server]),
|
|
|
|
Res;
|
|
|
|
trigger_register_hooks(Res, _User, _Server) ->
|
|
|
|
Res.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec () -> [{LUser, LServer}]
|
|
|
|
%% LUser = string()
|
|
|
|
%% LServer = string()
|
|
|
|
%% @doc 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
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Server) -> [{LUser, LServer}]
|
|
|
|
%% Server = string()
|
|
|
|
%% LUser = string()
|
|
|
|
%% LServer = string()
|
|
|
|
%% @doc Registered users list do not include anonymous users logged.
|
|
|
|
|
|
|
|
get_vh_registered_users(Server) when is_list(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
|
|
|
|
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()
|
|
|
|
|
|
|
|
get_vh_registered_users(Server, Opts) when is_list(Server) ->
|
2007-11-03 18:35:23 +01:00
|
|
|
lists:flatmap(
|
|
|
|
fun(M) ->
|
2009-02-13 16:01:55 +01:00
|
|
|
case erlang:function_exported(
|
|
|
|
M, get_vh_registered_users_number, 2) of
|
|
|
|
true ->
|
|
|
|
M:get_vh_registered_users_number(Server, Opts);
|
|
|
|
false ->
|
|
|
|
M:get_vh_registered_users_number(Server)
|
|
|
|
end
|
2007-11-03 18:35:23 +01:00
|
|
|
end, auth_modules(Server)).
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Server) -> Users_Number
|
|
|
|
%% Server = string()
|
|
|
|
%% Users_Number = integer()
|
|
|
|
|
|
|
|
get_vh_registered_users_number(Server) when is_list(Server) ->
|
2007-05-12 20:09:38 +02:00
|
|
|
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))).
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
get_vh_registered_users_number(Server, Opts) when is_list(Server) ->
|
2007-11-03 18:35:23 +01:00
|
|
|
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))).
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server) -> Password | false
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
2008-04-22 19:41:30 +02:00
|
|
|
%% @doc Get the password of the user.
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
get_password(User, Server) when is_list(User), is_list(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
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server) -> Password | nil()
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
%% @doc Get the password of the user.
|
|
|
|
|
|
|
|
get_password_s(User, Server) when is_list(User), is_list(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
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server) -> {Password, AuthModule} | {false, none}
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
%% AuthModule = authmodule()
|
2008-04-22 19:41:30 +02:00
|
|
|
%% @doc Get the password of the user and the auth module.
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
get_password_with_authmodule(User, Server)
|
|
|
|
when is_list(User), is_list(Server) ->
|
2008-04-22 19:41:30 +02:00
|
|
|
lists:foldl(
|
|
|
|
fun(M, {false, _}) ->
|
|
|
|
{M:get_password(User, Server), M};
|
|
|
|
(_M, {Password, AuthModule}) ->
|
|
|
|
{Password, AuthModule}
|
|
|
|
end, {false, none}, auth_modules(Server)).
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server) -> bool()
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% @doc Returns true if the user exists in the DB or if an anonymous
|
|
|
|
%% user is logged under the given name.
|
|
|
|
|
|
|
|
is_user_exists(User, Server) when is_list(User), is_list(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
|
|
|
|
2009-03-04 19:34:02 +01:00
|
|
|
%% @spec (Module, User, Server) -> true | false | maybe
|
2009-02-05 12:13:01 +01:00
|
|
|
%% Module = authmodule()
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% @doc Check if the user exists in all authentications module except
|
|
|
|
%% the module passed as parameter.
|
|
|
|
|
|
|
|
is_user_exists_in_other_modules(Module, User, Server)
|
|
|
|
when is_list(User), is_list(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
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @spec (User, Server) -> ok | error | {error, not_allowed}
|
2009-02-05 12:13:01 +01:00
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
2009-01-19 12:59:40 +01:00
|
|
|
%% @doc Remove user.
|
2010-01-09 17:38:07 +01:00
|
|
|
%% TODO: Fix me: It always return ok even if there was some problem removing the user.
|
|
|
|
%% dialyzer warning
|
|
|
|
%% ejabberd_auth.erl:388: The variable _ can never match since previous clauses completely covered the type 'ok'
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
remove_user(User, Server) when is_list(User), is_list(Server) ->
|
2009-01-19 11:14:04 +01:00
|
|
|
R = lists:foreach(
|
2010-01-09 17:38:07 +01:00
|
|
|
fun(M) ->
|
|
|
|
M:remove_user(User, Server)
|
|
|
|
end, auth_modules(Server)),
|
2010-06-10 00:52:38 +02:00
|
|
|
ejabberd_hooks:run(remove_user, list_to_binary(exmpp_stringprep:nameprep(Server)),
|
|
|
|
[list_to_binary(User), list_to_binary(Server)]),
|
2009-01-19 11:14:04 +01:00
|
|
|
R.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @spec (User, Server, Password) -> ok | not_exists | not_allowed | bad_request | error
|
2009-02-05 12:13:01 +01:00
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
2009-01-19 12:59:40 +01:00
|
|
|
%% @doc Try to remove user if the provided password is correct.
|
2009-01-19 11:14:04 +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.
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
remove_user(User, Server, Password)
|
|
|
|
when is_list(User), is_list(Server), is_list(Password) ->
|
2009-01-19 11:14:04 +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)
|
2009-01-19 11:14:04 +01:00
|
|
|
end, error, auth_modules(Server)),
|
|
|
|
case R of
|
2010-01-09 17:38:07 +01:00
|
|
|
ok -> ejabberd_hooks:run(remove_user, list_to_binary(exmpp_stringprep:nameprep(Server)),
|
2009-02-20 16:30:16 +01:00
|
|
|
[list_to_binary(User), list_to_binary(Server)]);
|
2009-01-19 11:14:04 +01:00
|
|
|
_ -> 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
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
%% @spec () -> [authmodule()]
|
|
|
|
%% @doc Return the lists of all the auth modules actually used in the
|
|
|
|
%% configuration.
|
|
|
|
|
2008-02-19 14:49:29 +01:00
|
|
|
auth_modules() ->
|
|
|
|
lists:usort(
|
|
|
|
lists:flatmap(
|
|
|
|
fun(Server) ->
|
|
|
|
auth_modules(Server)
|
|
|
|
end, ?MYHOSTS)).
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Server) -> [authmodule()]
|
|
|
|
%% Server = string()
|
|
|
|
%% @doc Return the list of authenticated modules for a given host.
|
|
|
|
|
|
|
|
auth_modules(Server) when is_list(Server) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
LServer = exmpp_stringprep: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].
|