2006-04-07 02:51:53 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_auth_anonymous.erl
|
|
|
|
%%% Author : Mickael Remond <mickael.remond@process-one.net>
|
|
|
|
%%% Purpose : Anonymous feature support in ejabberd
|
|
|
|
%%% Created : 17 Feb 2006 by Mickael Remond <mremond@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
|
|
|
|
%%%
|
2006-04-07 02:51:53 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_auth_anonymous).
|
|
|
|
-author('mickael.remond@process-one.net').
|
|
|
|
|
|
|
|
-export([start/1,
|
|
|
|
allow_anonymous/1,
|
|
|
|
is_sasl_anonymous_enabled/1,
|
|
|
|
is_login_anonymous_enabled/1,
|
|
|
|
anonymous_user_exist/2,
|
|
|
|
allow_multiple_connections/1,
|
2008-02-15 17:35:32 +01:00
|
|
|
register_connection/3,
|
|
|
|
unregister_connection/3
|
2006-04-07 02:51:53 +02:00
|
|
|
]).
|
|
|
|
|
|
|
|
|
|
|
|
%% Function used by ejabberd_auth:
|
|
|
|
-export([login/2,
|
|
|
|
set_password/3,
|
|
|
|
check_password/3,
|
|
|
|
check_password/5,
|
|
|
|
try_register/3,
|
|
|
|
dirty_get_registered_users/0,
|
|
|
|
get_vh_registered_users/1,
|
|
|
|
get_password/2,
|
|
|
|
get_password/3,
|
|
|
|
is_user_exists/2,
|
|
|
|
remove_user/2,
|
|
|
|
remove_user/3,
|
|
|
|
plain_password_required/0]).
|
|
|
|
|
2009-03-02 14:27:35 +01:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
2009-02-05 12:13:01 +01:00
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-record(anonymous, {us, sid}).
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Host) -> ok
|
|
|
|
%% Host = string()
|
|
|
|
%% @doc Create the anonymous table if at least one virtual host has
|
|
|
|
%% anonymous features enabled.
|
|
|
|
%% Register to login / logout events.
|
|
|
|
|
|
|
|
start(Host) when is_list(Host) ->
|
2009-03-01 20:03:35 +01:00
|
|
|
HostB = list_to_binary(Host),
|
2006-04-07 02:51:53 +02:00
|
|
|
%% TODO: Check cluster mode
|
2010-06-02 16:01:36 +02:00
|
|
|
update_tables(),
|
2006-04-07 02:51:53 +02:00
|
|
|
mnesia:create_table(anonymous, [{ram_copies, [node()]},
|
2010-06-02 16:01:36 +02:00
|
|
|
{type, bag}, {local_content, true},
|
|
|
|
{attributes, record_info(fields, anonymous)}]),
|
|
|
|
mnesia:add_table_copy(anonymous, node(), ram_copies),
|
2006-04-07 02:51:53 +02:00
|
|
|
%% The hooks are needed to add / remove users from the anonymous tables
|
2009-03-01 20:03:35 +01:00
|
|
|
ejabberd_hooks:add(sm_register_connection_hook, HostB,
|
2006-04-07 02:51:53 +02:00
|
|
|
?MODULE, register_connection, 100),
|
2009-03-01 20:03:35 +01:00
|
|
|
ejabberd_hooks:add(sm_remove_connection_hook, HostB,
|
2006-04-07 02:51:53 +02:00
|
|
|
?MODULE, unregister_connection, 100),
|
|
|
|
ok.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Host) -> bool()
|
|
|
|
%% Host = string()
|
|
|
|
%% @doc Return true if anonymous is allowed for host or false otherwise.
|
|
|
|
|
|
|
|
allow_anonymous(Host) when is_list(Host) ->
|
2006-04-20 17:42:51 +02:00
|
|
|
lists:member(?MODULE, ejabberd_auth:auth_modules(Host)).
|
2006-04-07 02:51:53 +02:00
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Host) -> bool()
|
|
|
|
%% Host = string()
|
|
|
|
%% @doc Return true if anonymous mode is enabled and if anonymous
|
|
|
|
%% protocol is SASL anonymous.
|
|
|
|
%% protocol can be: sasl_anon|login_anon|both
|
|
|
|
|
|
|
|
is_sasl_anonymous_enabled(Host) when is_list(Host) ->
|
2006-04-07 02:51:53 +02:00
|
|
|
case allow_anonymous(Host) of
|
2010-01-09 16:15:46 +01:00
|
|
|
false -> false;
|
2006-04-07 02:51:53 +02:00
|
|
|
true ->
|
|
|
|
case anonymous_protocol(Host) of
|
|
|
|
sasl_anon -> true;
|
|
|
|
both -> true;
|
|
|
|
_Other -> false
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Host) -> bool()
|
|
|
|
%% Host = string()
|
|
|
|
%% @doc Return true if anonymous login is enabled on the server.
|
2006-04-07 02:51:53 +02:00
|
|
|
%% anonymous login can be use using standard authentication method (i.e. with
|
|
|
|
%% clients that do not support anonymous login)
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
is_login_anonymous_enabled(Host) when is_list(Host) ->
|
2006-04-07 02:51:53 +02:00
|
|
|
case allow_anonymous(Host) of
|
|
|
|
false -> false;
|
|
|
|
true ->
|
|
|
|
case anonymous_protocol(Host) of
|
|
|
|
login_anon -> true;
|
2010-01-09 16:15:46 +01:00
|
|
|
both -> true;
|
2006-04-07 02:51:53 +02:00
|
|
|
_Other -> false
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Host) -> sasl_anon | login_anon | both
|
|
|
|
%% Host = string()
|
|
|
|
%% @doc Return the anonymous protocol to use: sasl_anon|login_anon|both.
|
2006-04-07 02:51:53 +02:00
|
|
|
%% defaults to login_anon
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
anonymous_protocol(Host) when is_list(Host) ->
|
2006-04-07 02:51:53 +02:00
|
|
|
case ejabberd_config:get_local_option({anonymous_protocol, Host}) of
|
|
|
|
sasl_anon -> sasl_anon;
|
|
|
|
login_anon -> login_anon;
|
|
|
|
both -> both;
|
|
|
|
_Other -> sasl_anon
|
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Host) -> bool()
|
|
|
|
%% Host = string()
|
|
|
|
%% @doc Return true if multiple connections have been allowed in the
|
|
|
|
%% config file.
|
2006-04-07 02:51:53 +02:00
|
|
|
%% defaults to false
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
allow_multiple_connections(Host) when is_list(Host) ->
|
2010-01-09 16:15:46 +01:00
|
|
|
ejabberd_config:get_local_option(
|
|
|
|
{allow_multiple_connections, Host}) =:= true.
|
2006-04-07 02:51:53 +02:00
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server) -> bool()
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
2010-01-09 16:19:48 +01:00
|
|
|
%% @doc Check if user exist in the anonymous database.
|
2009-02-05 12:13:01 +01:00
|
|
|
|
|
|
|
anonymous_user_exist(User, Server) when is_list(User), is_list(Server) ->
|
2008-07-09 11:16:03 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2006-04-07 02:51:53 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read({anonymous, US}) of
|
|
|
|
[] ->
|
|
|
|
false;
|
|
|
|
[_H|_T] ->
|
|
|
|
true
|
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (SID, LUser, LServer) -> term()
|
|
|
|
%% SID = term()
|
|
|
|
%% LUser = string()
|
|
|
|
%% LServer = string()
|
|
|
|
%% @doc Remove connection from Mnesia tables.
|
|
|
|
|
|
|
|
remove_connection(SID, LUser, LServer) when is_list(LUser), is_list(LServer) ->
|
2006-04-07 02:51:53 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete_object({anonymous, US, SID})
|
|
|
|
end,
|
2010-06-02 16:01:36 +02:00
|
|
|
mnesia:async_dirty(F).
|
2006-04-07 02:51:53 +02:00
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (SID, JID, Info) -> term()
|
|
|
|
%% SID = term()
|
|
|
|
%% JID = exmpp_jid:jid()
|
|
|
|
%% Info = [term()]
|
|
|
|
%% @doc Register connection.
|
|
|
|
|
|
|
|
register_connection(SID, JID, Info) when ?IS_JID(JID) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
LUser = exmpp_jid:prep_node(JID),
|
2009-06-01 18:37:15 +02:00
|
|
|
LServer = exmpp_jid:prep_domain(JID),
|
2008-07-09 11:16:03 +02:00
|
|
|
case proplists:get_value(auth_module, Info) of
|
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
?MODULE ->
|
2010-06-10 12:22:27 +02:00
|
|
|
ejabberd_hooks:run(register_user, LServer, [LUser, LServer]),
|
2008-04-22 19:41:30 +02:00
|
|
|
US = {LUser, LServer},
|
2010-06-02 16:01:36 +02:00
|
|
|
mnesia:async_dirty(
|
2008-04-22 19:41:30 +02:00
|
|
|
fun() -> mnesia:write(#anonymous{us = US, sid=SID})
|
|
|
|
end);
|
2008-07-09 11:16:03 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
2008-04-22 19:41:30 +02:00
|
|
|
end.
|
2006-04-07 02:51:53 +02:00
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (SID, JID, Ignored) -> term()
|
|
|
|
%% SID = term()
|
|
|
|
%% JID = exmpp_jid:jid()
|
|
|
|
%% Ignored = term()
|
|
|
|
%% @doc Remove an anonymous user from the anonymous users table.
|
|
|
|
|
|
|
|
unregister_connection(SID, JID, _) when ?IS_JID(JID) ->
|
2010-06-10 00:52:38 +02:00
|
|
|
LUser = exmpp_jid:prep_node_as_list(JID),
|
|
|
|
LServer = exmpp_jid:prep_domain_as_list(JID),
|
2007-06-28 19:39:53 +02:00
|
|
|
purge_hook(anonymous_user_exist(LUser, LServer),
|
|
|
|
LUser, LServer),
|
2006-04-07 02:51:53 +02:00
|
|
|
remove_connection(SID, LUser, LServer).
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (bool(), LUser, LServer) -> term()
|
|
|
|
%% LUser = string()
|
|
|
|
%% LServer = string()
|
|
|
|
%% @doc Launch the hook to purge user data only for anonymous users.
|
|
|
|
|
2007-06-28 19:39:53 +02:00
|
|
|
purge_hook(false, _LUser, _LServer) ->
|
|
|
|
ok;
|
2009-02-05 12:13:01 +01:00
|
|
|
purge_hook(true, LUser, LServer) when is_list(LUser), is_list(LServer) ->
|
2010-06-10 00:52:38 +02:00
|
|
|
ejabberd_hooks:run(anonymous_purge_hook, list_to_binary(LServer), [LUser, LServer]).
|
2007-06-28 19:39:53 +02:00
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
%% ---------------------------------
|
|
|
|
%% Specific anonymous auth functions
|
|
|
|
%% ---------------------------------
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server, Password) -> bool()
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
%% @doc When anonymous login is enabled, check the password for
|
|
|
|
%% permenant users before allowing access.
|
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
check_password(User, Server, Password) ->
|
|
|
|
check_password(User, Server, Password, undefined, undefined).
|
2009-04-22 13:44:03 +02:00
|
|
|
check_password(User, Server, _Password, _Digest, _DigestGen) ->
|
2006-04-07 02:51:53 +02:00
|
|
|
%% We refuse login for registered accounts (They cannot logged but
|
|
|
|
%% they however are "reserved")
|
2010-01-09 16:15:46 +01:00
|
|
|
case ejabberd_auth:is_user_exists_in_other_modules(?MODULE,
|
2006-06-13 18:52:38 +02:00
|
|
|
User, Server) of
|
2009-03-04 19:34:02 +01:00
|
|
|
%% If user exists in other module, reject anonnymous authentication
|
2006-04-07 02:51:53 +02:00
|
|
|
true -> false;
|
2009-03-04 19:34:02 +01:00
|
|
|
%% If we are not sure whether the user exists in other module, reject anon auth
|
|
|
|
maybe -> false;
|
2006-04-07 02:51:53 +02:00
|
|
|
false -> login(User, Server)
|
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server) -> bool()
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
login(User, Server) ->
|
|
|
|
case is_login_anonymous_enabled(Server) of
|
|
|
|
false -> false;
|
|
|
|
true ->
|
|
|
|
case anonymous_user_exist(User, Server) of
|
|
|
|
%% Reject the login if an anonymous user with the same login
|
|
|
|
%% is already logged and if multiple login has not been enable
|
|
|
|
%% in the config file.
|
|
|
|
true -> allow_multiple_connections(Server);
|
|
|
|
%% Accept login and add user to the anonymous table
|
|
|
|
false -> true
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server, Password) -> ok | {error, not_allowed}
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
%% @doc When anonymous login is enabled, check that the user is
|
|
|
|
%% permanent before changing its password.
|
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
set_password(User, Server, _Password) ->
|
|
|
|
case anonymous_user_exist(User, Server) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
{error, not_allowed}
|
|
|
|
end.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server, Password) -> {error, not_allowed}
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
%% @doc When anonymous login is enabled, check if permanent users are
|
|
|
|
%% allowed on the server:
|
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
try_register(_User, _Server, _Password) ->
|
|
|
|
{error, not_allowed}.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec () -> nil()
|
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
dirty_get_registered_users() ->
|
|
|
|
[].
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (Server) -> nil()
|
|
|
|
%% Server = string()
|
|
|
|
|
2010-06-10 12:22:27 +02:00
|
|
|
get_vh_registered_users(Server) ->
|
|
|
|
[{U, S} || {U, S, _R} <- ejabberd_sm:get_vh_session_list(list_to_binary(Server))].
|
2006-04-07 02:51:53 +02:00
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server) -> Password | false
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = nil()
|
|
|
|
%% @doc Return password of permanent user or false for anonymous users.
|
2006-04-07 02:51:53 +02:00
|
|
|
|
|
|
|
get_password(User, Server) ->
|
2006-04-20 17:42:51 +02:00
|
|
|
get_password(User, Server, "").
|
2006-04-07 02:51:53 +02:00
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server, DefaultValue) -> DefaultValue | false
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% DefaultValue = string()
|
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
get_password(User, Server, DefaultValue) ->
|
2009-04-22 13:44:03 +02:00
|
|
|
case anonymous_user_exist(User, Server) or login(User, Server) of
|
2006-04-07 02:51:53 +02:00
|
|
|
%% We return the default value if the user is anonymous
|
|
|
|
true ->
|
|
|
|
DefaultValue;
|
|
|
|
%% We return the permanent user password otherwise
|
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
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.
|
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
is_user_exists(User, Server) ->
|
|
|
|
anonymous_user_exist(User, Server).
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server) -> {error, not_allowed}
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
remove_user(_User, _Server) ->
|
|
|
|
{error, not_allowed}.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec (User, Server, Password) -> {error, not_allowed}
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Password = string()
|
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
remove_user(_User, _Server, _Password) ->
|
|
|
|
not_allowed.
|
|
|
|
|
2009-02-05 12:13:01 +01:00
|
|
|
%% @spec () -> bool()
|
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
plain_password_required() ->
|
|
|
|
false.
|
2010-06-02 16:01:36 +02:00
|
|
|
|
|
|
|
update_tables() ->
|
|
|
|
case catch mnesia:table_info(anonymous, local_content) of
|
|
|
|
false ->
|
|
|
|
mnesia:delete_table(anonymous);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|