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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 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-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +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.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2006-04-07 02:51:53 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_auth_anonymous).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2017-04-29 10:39:40 +02:00
|
|
|
-behaviour(ejabberd_auth).
|
2006-04-07 02:51:53 +02:00
|
|
|
-author('mickael.remond@process-one.net').
|
|
|
|
|
|
|
|
-export([start/1,
|
2017-04-29 10:39:40 +02:00
|
|
|
stop/1,
|
2018-08-17 16:56:23 +02:00
|
|
|
use_cache/1,
|
2006-04-07 02:51:53 +02:00
|
|
|
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
|
|
|
]).
|
|
|
|
|
2017-05-11 14:49:06 +02:00
|
|
|
-export([login/2, check_password/4, user_exists/2,
|
2017-05-11 13:37:21 +02:00
|
|
|
get_users/2, count_users/2, store_type/1,
|
2019-06-14 11:33:26 +02:00
|
|
|
plain_password_required/1]).
|
2006-04-07 02:51:53 +02:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-26 10:29:17 +02:00
|
|
|
-include("jid.hrl").
|
2006-04-07 02:51:53 +02:00
|
|
|
|
|
|
|
start(Host) ->
|
|
|
|
ejabberd_hooks:add(sm_register_connection_hook, Host,
|
|
|
|
?MODULE, register_connection, 100),
|
|
|
|
ejabberd_hooks:add(sm_remove_connection_hook, Host,
|
|
|
|
?MODULE, unregister_connection, 100),
|
|
|
|
ok.
|
|
|
|
|
2017-04-29 10:39:40 +02:00
|
|
|
stop(Host) ->
|
|
|
|
ejabberd_hooks:delete(sm_register_connection_hook, Host,
|
|
|
|
?MODULE, register_connection, 100),
|
|
|
|
ejabberd_hooks:delete(sm_remove_connection_hook, Host,
|
|
|
|
?MODULE, unregister_connection, 100).
|
|
|
|
|
2018-08-17 16:56:23 +02:00
|
|
|
use_cache(_) ->
|
|
|
|
false.
|
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
%% Return true if anonymous is allowed for host or false otherwise
|
|
|
|
allow_anonymous(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
|
|
|
|
|
|
|
%% 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) ->
|
|
|
|
case allow_anonymous(Host) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> false;
|
|
|
|
true ->
|
|
|
|
case anonymous_protocol(Host) of
|
|
|
|
sasl_anon -> true;
|
|
|
|
both -> true;
|
|
|
|
_Other -> false
|
|
|
|
end
|
2006-04-07 02:51:53 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Return true if anonymous login is enabled on the server
|
|
|
|
%% anonymous login can be use using standard authentication method (i.e. with
|
|
|
|
%% clients that do not support anonymous login)
|
|
|
|
is_login_anonymous_enabled(Host) ->
|
|
|
|
case allow_anonymous(Host) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> false;
|
|
|
|
true ->
|
|
|
|
case anonymous_protocol(Host) of
|
|
|
|
login_anon -> true;
|
|
|
|
both -> true;
|
|
|
|
_Other -> false
|
|
|
|
end
|
2006-04-07 02:51:53 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Return the anonymous protocol to use: sasl_anon|login_anon|both
|
|
|
|
%% defaults to login_anon
|
|
|
|
anonymous_protocol(Host) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:anonymous_protocol(Host).
|
2006-04-07 02:51:53 +02:00
|
|
|
|
|
|
|
%% Return true if multiple connections have been allowed in the config file
|
|
|
|
%% defaults to false
|
|
|
|
allow_multiple_connections(Host) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:allow_multiple_connections(Host).
|
2006-04-07 02:51:53 +02:00
|
|
|
|
|
|
|
anonymous_user_exist(User, Server) ->
|
2017-01-13 10:03:39 +01:00
|
|
|
lists:any(
|
|
|
|
fun({_LResource, Info}) ->
|
|
|
|
proplists:get_value(auth_module, Info) == ?MODULE
|
|
|
|
end, ejabberd_sm:get_user_info(User, Server)).
|
2006-04-07 02:51:53 +02:00
|
|
|
|
|
|
|
%% Register connection
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec register_connection(ejabberd_sm:sid(), jid(), ejabberd_sm:info()) -> ok.
|
2017-01-13 10:03:39 +01:00
|
|
|
register_connection(_SID,
|
2019-05-07 11:02:53 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer, lresource = LResource}, Info) ->
|
2017-01-13 10:03:39 +01:00
|
|
|
case proplists:get_value(auth_module, Info) of
|
|
|
|
?MODULE ->
|
2019-05-07 11:02:53 +02:00
|
|
|
% Register user only if we are first resource
|
|
|
|
case ejabberd_sm:get_user_resources(LUser, LServer) of
|
|
|
|
[LResource] ->
|
|
|
|
ejabberd_hooks:run(register_user, LServer, [LUser, LServer]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2017-01-23 11:51:05 +01:00
|
|
|
_ ->
|
2017-01-13 10:03:39 +01:00
|
|
|
ok
|
2008-04-22 19:41:30 +02:00
|
|
|
end.
|
2006-04-07 02:51:53 +02:00
|
|
|
|
|
|
|
%% Remove an anonymous user from the anonymous users table
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec unregister_connection(ejabberd_sm:sid(), jid(), ejabberd_sm:info()) -> any().
|
2017-01-13 10:03:39 +01:00
|
|
|
unregister_connection(_SID,
|
|
|
|
#jid{luser = LUser, lserver = LServer}, Info) ->
|
|
|
|
case proplists:get_value(auth_module, Info) of
|
|
|
|
?MODULE ->
|
2019-05-07 11:02:53 +02:00
|
|
|
% Remove user data only if there is no more resources around
|
|
|
|
case ejabberd_sm:get_user_resources(LUser, LServer) of
|
|
|
|
[] ->
|
|
|
|
ejabberd_hooks:run(remove_user, LServer, [LUser, LServer]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2017-01-13 10:03:39 +01:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2007-06-28 19:39:53 +02:00
|
|
|
|
2006-04-07 02:51:53 +02:00
|
|
|
%% ---------------------------------
|
|
|
|
%% Specific anonymous auth functions
|
|
|
|
%% ---------------------------------
|
2017-05-11 13:37:21 +02:00
|
|
|
check_password(User, _AuthzId, Server, _Password) ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{nocache,
|
|
|
|
case ejabberd_auth: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}.
|
2006-04-07 02:51:53 +02:00
|
|
|
|
|
|
|
login(User, Server) ->
|
|
|
|
case is_login_anonymous_enabled(Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
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
|
2006-04-07 02:51:53 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
get_users(Server, _) ->
|
|
|
|
[{U, S} || {U, S, _R} <- ejabberd_sm:get_vh_session_list(Server)].
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
count_users(Server, Opts) ->
|
|
|
|
length(get_users(Server, Opts)).
|
2006-04-07 02:51:53 +02:00
|
|
|
|
2017-05-11 14:49:06 +02:00
|
|
|
user_exists(User, Server) ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{nocache, anonymous_user_exist(User, Server)}.
|
2006-04-07 02:51:53 +02:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
plain_password_required(_) ->
|
|
|
|
false.
|
2011-08-16 00:25:03 +02:00
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
store_type(_) ->
|
|
|
|
external.
|