aboutsummaryrefslogtreecommitdiff
path: root/src/ejabberd_auth_anonymous.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/ejabberd_auth_anonymous.erl')
-rw-r--r--src/ejabberd_auth_anonymous.erl208
1 files changed, 113 insertions, 95 deletions
diff --git a/src/ejabberd_auth_anonymous.erl b/src/ejabberd_auth_anonymous.erl
index ebdbf9680..c19effabe 100644
--- a/src/ejabberd_auth_anonymous.erl
+++ b/src/ejabberd_auth_anonymous.erl
@@ -39,27 +39,24 @@
%% 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,
- store_type/0,
+-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_vh_registered_users/2, get_vh_registered_users_number/1,
+ get_vh_registered_users_number/2, get_password_s/2,
+ get_password/2, get_password/3, is_user_exists/2,
+ remove_user/2, remove_user/3, store_type/0,
plain_password_required/0]).
-include("ejabberd.hrl").
+
-include("jlib.hrl").
--record(anonymous, {us, sid}).
%% Create the anonymous table if at least one virtual host has anonymous features enabled
%% Register to login / logout events
+-record(anonymous, {us = {<<"">>, <<"">>} :: {binary(), binary()},
+ sid = {now(), self()} :: ejabberd_sm:sid()}).
+
start(Host) ->
%% TODO: Check cluster mode
mnesia:create_table(anonymous, [{ram_copies, [node()]},
@@ -80,13 +77,13 @@ allow_anonymous(Host) ->
%% anonymous protocol can be: sasl_anon|login_anon|both
is_sasl_anonymous_enabled(Host) ->
case allow_anonymous(Host) of
- false -> false;
- true ->
- case anonymous_protocol(Host) of
- sasl_anon -> true;
- both -> true;
- _Other -> false
- end
+ false -> false;
+ true ->
+ case anonymous_protocol(Host) of
+ sasl_anon -> true;
+ both -> true;
+ _Other -> false
+ end
end.
%% Return true if anonymous login is enabled on the server
@@ -94,30 +91,33 @@ is_sasl_anonymous_enabled(Host) ->
%% clients that do not support anonymous login)
is_login_anonymous_enabled(Host) ->
case allow_anonymous(Host) of
- false -> false;
- true ->
- case anonymous_protocol(Host) of
- login_anon -> true;
- both -> true;
- _Other -> false
- end
+ false -> false;
+ true ->
+ case anonymous_protocol(Host) of
+ login_anon -> true;
+ both -> true;
+ _Other -> false
+ end
end.
%% Return the anonymous protocol to use: sasl_anon|login_anon|both
%% defaults to login_anon
anonymous_protocol(Host) ->
- case ejabberd_config:get_local_option({anonymous_protocol, Host}) of
- sasl_anon -> sasl_anon;
- login_anon -> login_anon;
- both -> both;
- _Other -> sasl_anon
- end.
+ ejabberd_config:get_local_option(
+ {anonymous_protocol, Host},
+ fun(sasl_anon) -> sasl_anon;
+ (login_anon) -> login_anon;
+ (both) -> both
+ end,
+ sasl_anon).
%% Return true if multiple connections have been allowed in the config file
%% defaults to false
allow_multiple_connections(Host) ->
ejabberd_config:get_local_option(
- {allow_multiple_connections, Host}) =:= true.
+ {allow_multiple_connections, Host},
+ fun(V) when is_boolean(V) -> V end,
+ false).
%% Check if user exist in the anonymus database
anonymous_user_exist(User, Server) ->
@@ -134,36 +134,39 @@ anonymous_user_exist(User, Server) ->
%% Remove connection from Mnesia tables
remove_connection(SID, LUser, LServer) ->
US = {LUser, LServer},
- F = fun() ->
- mnesia:delete_object({anonymous, US, SID})
- end,
+ F = fun () -> mnesia:delete_object({anonymous, US, SID})
+ end,
mnesia:transaction(F).
%% Register connection
-register_connection(SID, #jid{luser = LUser, lserver = LServer}, Info) ->
- AuthModule = xml:get_attr_s(auth_module, Info),
- case AuthModule == ?MODULE of
- true ->
- ejabberd_hooks:run(register_user, LServer, [LUser, LServer]),
- US = {LUser, LServer},
- mnesia:sync_dirty(
- fun() -> mnesia:write(#anonymous{us = US, sid=SID})
- end);
- false ->
- ok
+register_connection(SID,
+ #jid{luser = LUser, lserver = LServer}, Info) ->
+ AuthModule = list_to_atom(binary_to_list(xml:get_attr_s(<<"auth_module">>, Info))),
+ case AuthModule == (?MODULE) of
+ true ->
+ ejabberd_hooks:run(register_user, LServer,
+ [LUser, LServer]),
+ US = {LUser, LServer},
+ mnesia:sync_dirty(fun () ->
+ mnesia:write(#anonymous{us = US,
+ sid = SID})
+ end);
+ false -> ok
end.
%% Remove an anonymous user from the anonymous users table
-unregister_connection(SID, #jid{luser = LUser, lserver = LServer}, _) ->
- purge_hook(anonymous_user_exist(LUser, LServer),
- LUser, LServer),
+unregister_connection(SID,
+ #jid{luser = LUser, lserver = LServer}, _) ->
+ purge_hook(anonymous_user_exist(LUser, LServer), LUser,
+ LServer),
remove_connection(SID, LUser, LServer).
%% Launch the hook to purge user data only for anonymous users
purge_hook(false, _LUser, _LServer) ->
ok;
purge_hook(true, LUser, LServer) ->
- ejabberd_hooks:run(anonymous_purge_hook, LServer, [LUser, LServer]).
+ ejabberd_hooks:run(anonymous_purge_hook, LServer,
+ [LUser, LServer]).
%% ---------------------------------
%% Specific anonymous auth functions
@@ -172,41 +175,42 @@ purge_hook(true, LUser, LServer) ->
%% When anonymous login is enabled, check the password for permenant users
%% before allowing access
check_password(User, Server, Password) ->
- check_password(User, Server, Password, undefined, undefined).
-check_password(User, Server, _Password, _Digest, _DigestGen) ->
- %% We refuse login for registered accounts (They cannot logged but
- %% they however are "reserved")
- case ejabberd_auth:is_user_exists_in_other_modules(?MODULE,
- User, Server) of
- %% If user exists in other module, reject anonnymous authentication
- true -> false;
- %% If we are not sure whether the user exists in other module, reject anon auth
- maybe -> false;
- false -> login(User, Server)
+ check_password(User, Server, Password, undefined,
+ undefined).
+
+check_password(User, Server, _Password, _Digest,
+ _DigestGen) ->
+ case
+ ejabberd_auth:is_user_exists_in_other_modules(?MODULE,
+ User, Server)
+ of
+ %% If user exists in other module, reject anonnymous authentication
+ true -> false;
+ %% If we are not sure whether the user exists in other module, reject anon auth
+ maybe -> false;
+ false -> login(User, Server)
end.
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
+ 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.
%% When anonymous login is enabled, check that the user is permanent before
%% changing its password
set_password(User, Server, _Password) ->
case anonymous_user_exist(User, Server) of
- true ->
- ok;
- false ->
- {error, not_allowed}
+ true -> ok;
+ false -> {error, not_allowed}
end.
%% When anonymous login is enabled, check if permanent users are allowed on
@@ -214,25 +218,42 @@ set_password(User, Server, _Password) ->
try_register(_User, _Server, _Password) ->
{error, not_allowed}.
-dirty_get_registered_users() ->
- [].
+dirty_get_registered_users() -> [].
get_vh_registered_users(Server) ->
- [{U, S} || {U, S, _R} <- ejabberd_sm:get_vh_session_list(Server)].
+ [{U, S}
+ || {U, S, _R}
+ <- ejabberd_sm:get_vh_session_list(Server)].
+get_vh_registered_users(Server, _) ->
+ get_vh_registered_users(Server).
+
+get_vh_registered_users_number(Server) ->
+ length(get_vh_registered_users(Server)).
+
+get_vh_registered_users_number(Server, _) ->
+ get_vh_registered_users_number(Server).
%% Return password of permanent user or false for anonymous users
get_password(User, Server) ->
- get_password(User, Server, "").
+ get_password(User, Server, <<"">>).
get_password(User, Server, DefaultValue) ->
- case anonymous_user_exist(User, Server) or login(User, Server) of
- %% We return the default value if the user is anonymous
- true ->
- DefaultValue;
- %% We return the permanent user password otherwise
- false ->
- false
+ case anonymous_user_exist(User, Server) or
+ login(User, Server)
+ of
+ %% We return the default value if the user is anonymous
+ true -> DefaultValue;
+ %% We return the permanent user password otherwise
+ false -> false
+ end.
+
+get_password_s(User, Server) ->
+ case get_password(User, Server) of
+ false ->
+ <<"">>;
+ Password ->
+ Password
end.
%% Returns true if the user exists in the DB or if an anonymous user is logged
@@ -240,14 +261,11 @@ get_password(User, Server, DefaultValue) ->
is_user_exists(User, Server) ->
anonymous_user_exist(User, Server).
-remove_user(_User, _Server) ->
- {error, not_allowed}.
+remove_user(_User, _Server) -> {error, not_allowed}.
-remove_user(_User, _Server, _Password) ->
- not_allowed.
+remove_user(_User, _Server, _Password) -> not_allowed.
-plain_password_required() ->
- false.
+plain_password_required() -> false.
store_type() ->
plain.