2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_auth_external.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 via LDAP external script
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 12 Dec 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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
|
|
|
%%%
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_auth_external).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-behaviour(ejabberd_auth).
|
|
|
|
|
2015-04-09 03:21:09 +02:00
|
|
|
-export([start/1, set_password/3, check_password/4,
|
|
|
|
check_password/6, try_register/3,
|
2013-03-14 10:33:02 +01:00
|
|
|
dirty_get_registered_users/0, get_vh_registered_users/1,
|
2010-05-10 16:42:54 +02:00
|
|
|
get_vh_registered_users/2,
|
|
|
|
get_vh_registered_users_number/1,
|
2013-03-14 10:33:02 +01:00
|
|
|
get_vh_registered_users_number/2, get_password/2,
|
|
|
|
get_password_s/2, is_user_exists/2, remove_user/2,
|
2015-06-01 14:38:27 +02:00
|
|
|
remove_user/3, store_type/0, plain_password_required/0,
|
|
|
|
opt_type/1]).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2010-05-10 16:42:54 +02:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2010-05-10 16:42:54 +02:00
|
|
|
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2005-07-13 05:24:13 +02:00
|
|
|
start(Host) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
Cmd = ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
{extauth_program, Host},
|
|
|
|
fun(V) ->
|
|
|
|
binary_to_list(iolist_to_binary(V))
|
|
|
|
end,
|
|
|
|
"extauth"),
|
|
|
|
extauth:start(Host, Cmd),
|
2014-05-07 17:13:51 +02:00
|
|
|
check_cache_last_options(Host),
|
2016-04-27 08:44:32 +02:00
|
|
|
ejabberd_auth_mnesia:start(Host).
|
2010-05-10 16:42:54 +02:00
|
|
|
|
|
|
|
check_cache_last_options(Server) ->
|
|
|
|
case get_cache_option(Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> no_cache;
|
|
|
|
{true, _CacheTime} ->
|
|
|
|
case get_mod_last_configured(Server) of
|
|
|
|
no_mod_last ->
|
|
|
|
?ERROR_MSG("In host ~p extauth is used, extauth_cache "
|
|
|
|
"is enabled but mod_last is not enabled.",
|
|
|
|
[Server]),
|
|
|
|
no_cache;
|
|
|
|
_ -> cache
|
|
|
|
end
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
plain_password_required() -> true.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
store_type() -> external.
|
2011-08-16 00:25:03 +02:00
|
|
|
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password(User, AuthzId, Server, Password) ->
|
|
|
|
if AuthzId /= <<>> andalso AuthzId /= User ->
|
|
|
|
false;
|
|
|
|
true ->
|
2010-05-10 16:42:54 +02:00
|
|
|
case get_cache_option(Server) of
|
2015-04-09 03:21:09 +02:00
|
|
|
false -> check_password_extauth(User, AuthzId, Server, Password);
|
2013-03-14 10:33:02 +01:00
|
|
|
{true, CacheTime} ->
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password_cache(User, AuthzId, Server, Password, CacheTime)
|
|
|
|
end
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password(User, AuthzId, Server, Password, _Digest,
|
2013-03-14 10:33:02 +01:00
|
|
|
_DigestGen) ->
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password(User, AuthzId, Server, Password).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
set_password(User, Server, Password) ->
|
2008-08-18 20:21:10 +02:00
|
|
|
case extauth:set_password(User, Server, Password) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2016-04-27 08:44:32 +02:00
|
|
|
set_password_mnesia(User, Server, Password), ok;
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> {error, unknown_problem}
|
2008-08-18 20:21:10 +02:00
|
|
|
end.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2010-05-10 16:42:54 +02:00
|
|
|
try_register(User, Server, Password) ->
|
|
|
|
case get_cache_option(Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> try_register_extauth(User, Server, Password);
|
|
|
|
{true, _CacheTime} ->
|
|
|
|
try_register_external_cache(User, Server, Password)
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
|
|
|
dirty_get_registered_users() ->
|
2016-04-27 08:44:32 +02:00
|
|
|
ejabberd_auth_mnesia:dirty_get_registered_users().
|
2010-05-10 16:42:54 +02:00
|
|
|
|
|
|
|
get_vh_registered_users(Server) ->
|
2016-04-27 08:44:32 +02:00
|
|
|
ejabberd_auth_mnesia:get_vh_registered_users(Server).
|
2010-05-10 16:42:54 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_vh_registered_users(Server, Data) ->
|
2016-04-27 08:44:32 +02:00
|
|
|
ejabberd_auth_mnesia:get_vh_registered_users(Server,
|
2013-03-14 10:33:02 +01:00
|
|
|
Data).
|
2010-05-10 16:42:54 +02:00
|
|
|
|
|
|
|
get_vh_registered_users_number(Server) ->
|
2016-04-27 08:44:32 +02:00
|
|
|
ejabberd_auth_mnesia:get_vh_registered_users_number(Server).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2010-05-10 16:42:54 +02:00
|
|
|
get_vh_registered_users_number(Server, Data) ->
|
2016-04-27 08:44:32 +02:00
|
|
|
ejabberd_auth_mnesia:get_vh_registered_users_number(Server,
|
2013-03-14 10:33:02 +01:00
|
|
|
Data).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2010-05-10 16:42:54 +02:00
|
|
|
%% The password can only be returned if cache is enabled, cached info exists and is fresh enough.
|
|
|
|
get_password(User, Server) ->
|
|
|
|
case get_cache_option(Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> false;
|
|
|
|
{true, CacheTime} ->
|
|
|
|
get_password_cache(User, Server, CacheTime)
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2010-05-10 16:42:54 +02:00
|
|
|
get_password_s(User, Server) ->
|
|
|
|
case get_password(User, Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> <<"">>;
|
|
|
|
Other -> Other
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2009-03-04 19:34:02 +01:00
|
|
|
%% @spec (User, Server) -> true | false | {error, Error}
|
2005-07-13 05:24:13 +02:00
|
|
|
is_user_exists(User, Server) ->
|
2009-03-04 19:34:02 +01:00
|
|
|
try extauth:is_user_exists(User, Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
Res -> Res
|
2009-03-04 19:34:02 +01:00
|
|
|
catch
|
2013-03-14 10:33:02 +01:00
|
|
|
_:Error -> {error, Error}
|
2009-03-04 19:34:02 +01:00
|
|
|
end.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2010-05-10 16:42:54 +02:00
|
|
|
remove_user(User, Server) ->
|
|
|
|
case extauth:remove_user(User, Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> false;
|
|
|
|
true ->
|
|
|
|
case get_cache_option(Server) of
|
|
|
|
false -> false;
|
|
|
|
{true, _CacheTime} ->
|
2016-04-27 08:44:32 +02:00
|
|
|
ejabberd_auth_mnesia:remove_user(User, Server)
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
remove_user(User, Server, Password) ->
|
|
|
|
case extauth:remove_user(User, Server, Password) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> false;
|
|
|
|
true ->
|
|
|
|
case get_cache_option(Server) of
|
|
|
|
false -> false;
|
|
|
|
{true, _CacheTime} ->
|
2016-04-27 08:44:32 +02:00
|
|
|
ejabberd_auth_mnesia:remove_user(User, Server,
|
2013-03-14 10:33:02 +01:00
|
|
|
Password)
|
|
|
|
end
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Extauth cache management
|
|
|
|
%%%
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2010-05-10 16:42:54 +02:00
|
|
|
%% @spec (Host::string()) -> false | {true, CacheTime::integer()}
|
|
|
|
get_cache_option(Host) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
case ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
{extauth_cache, Host},
|
2014-04-16 14:15:14 +02:00
|
|
|
fun(false) -> undefined;
|
2014-05-05 13:13:35 +02:00
|
|
|
(I) when is_integer(I), I >= 0 -> I
|
2014-04-16 14:15:14 +02:00
|
|
|
end) of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined -> false;
|
|
|
|
CacheTime -> {true, CacheTime}
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2015-04-09 03:21:09 +02:00
|
|
|
%% @spec (User, AuthzId, Server, Password) -> true | false
|
|
|
|
check_password_extauth(User, _AuthzId, Server, Password) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
extauth:check_password(User, Server, Password) andalso
|
|
|
|
Password /= <<"">>.
|
2010-05-10 16:42:54 +02:00
|
|
|
|
|
|
|
%% @spec (User, Server, Password) -> true | false
|
|
|
|
try_register_extauth(User, Server, Password) ->
|
|
|
|
extauth:try_register(User, Server, Password).
|
|
|
|
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password_cache(User, AuthzId, Server, Password, 0) ->
|
|
|
|
check_password_external_cache(User, AuthzId, Server, Password);
|
|
|
|
check_password_cache(User, AuthzId, Server, Password,
|
2013-03-14 10:33:02 +01:00
|
|
|
CacheTime) ->
|
2010-05-10 16:42:54 +02:00
|
|
|
case get_last_access(User, Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
online ->
|
2016-04-27 08:44:32 +02:00
|
|
|
check_password_mnesia(User, AuthzId, Server, Password);
|
2013-03-14 10:33:02 +01:00
|
|
|
never ->
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password_external_cache(User, AuthzId, Server, Password);
|
2013-03-14 10:33:02 +01:00
|
|
|
mod_last_required ->
|
|
|
|
?ERROR_MSG("extauth is used, extauth_cache is enabled "
|
|
|
|
"but mod_last is not enabled in that "
|
|
|
|
"host",
|
|
|
|
[]),
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password_external_cache(User, AuthzId, Server, Password);
|
2013-03-14 10:33:02 +01:00
|
|
|
TimeStamp ->
|
|
|
|
case is_fresh_enough(TimeStamp, CacheTime) of
|
|
|
|
%% If no need to refresh, check password against Mnesia
|
|
|
|
true ->
|
2016-04-27 08:44:32 +02:00
|
|
|
case check_password_mnesia(User, AuthzId, Server, Password) of
|
2013-03-14 10:33:02 +01:00
|
|
|
%% If password valid in Mnesia, accept it
|
|
|
|
true -> true;
|
|
|
|
%% Else (password nonvalid in Mnesia), check in extauth and cache result
|
|
|
|
false ->
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password_external_cache(User, AuthzId, Server, Password)
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
%% Else (need to refresh), check in extauth and cache result
|
|
|
|
false ->
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password_external_cache(User, AuthzId, Server, Password)
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
|
|
|
|
2016-04-27 08:44:32 +02:00
|
|
|
get_password_mnesia(User, Server) ->
|
|
|
|
ejabberd_auth_mnesia:get_password(User, Server).
|
2010-05-10 16:42:54 +02:00
|
|
|
|
2016-03-07 14:47:42 +01:00
|
|
|
-spec get_password_cache(User::binary(), Server::binary(), CacheTime::integer()) -> Password::string() | false.
|
2010-05-10 16:42:54 +02:00
|
|
|
get_password_cache(User, Server, CacheTime) ->
|
|
|
|
case get_last_access(User, Server) of
|
2016-04-27 08:44:32 +02:00
|
|
|
online -> get_password_mnesia(User, Server);
|
2013-03-14 10:33:02 +01:00
|
|
|
never -> false;
|
|
|
|
mod_last_required ->
|
|
|
|
?ERROR_MSG("extauth is used, extauth_cache is enabled "
|
|
|
|
"but mod_last is not enabled in that "
|
|
|
|
"host",
|
|
|
|
[]),
|
|
|
|
false;
|
|
|
|
TimeStamp ->
|
|
|
|
case is_fresh_enough(TimeStamp, CacheTime) of
|
2016-04-27 08:44:32 +02:00
|
|
|
true -> get_password_mnesia(User, Server);
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> false
|
|
|
|
end
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Check the password using extauth; if success then cache it
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password_external_cache(User, AuthzId, Server, Password) ->
|
|
|
|
case check_password_extauth(User, AuthzId, Server, Password) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2016-04-27 08:44:32 +02:00
|
|
|
set_password_mnesia(User, Server, Password), true;
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> false
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Try to register using extauth; if success then cache it
|
|
|
|
try_register_external_cache(User, Server, Password) ->
|
|
|
|
case try_register_extauth(User, Server, Password) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, ok} = R ->
|
2016-04-27 08:44:32 +02:00
|
|
|
set_password_mnesia(User, Server, Password), R;
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> {error, not_allowed}
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-09 03:21:09 +02:00
|
|
|
%% @spec (User, AuthzId, Server, Password) -> true | false
|
2016-04-27 08:44:32 +02:00
|
|
|
check_password_mnesia(User, AuthzId, Server, Password) ->
|
|
|
|
ejabberd_auth_mnesia:check_password(User, AuthzId, Server,
|
2013-03-14 10:33:02 +01:00
|
|
|
Password).
|
2010-05-10 16:42:54 +02:00
|
|
|
|
|
|
|
%% @spec (User, Server, Password) -> ok | {error, invalid_jid}
|
2016-04-27 08:44:32 +02:00
|
|
|
set_password_mnesia(User, Server, Password) ->
|
2010-05-10 16:42:54 +02:00
|
|
|
%% @spec (TimeLast, CacheTime) -> true | false
|
|
|
|
%% TimeLast = online | never | integer()
|
|
|
|
%% CacheTime = integer() | false
|
2016-04-27 08:44:32 +02:00
|
|
|
ejabberd_auth_mnesia:set_password(User, Server,
|
2013-03-14 10:33:02 +01:00
|
|
|
Password).
|
|
|
|
|
2010-05-10 16:42:54 +02:00
|
|
|
is_fresh_enough(TimeStampLast, CacheTime) ->
|
2015-12-07 16:08:57 +01:00
|
|
|
Now = p1_time_compat:system_time(seconds),
|
2013-03-14 10:33:02 +01:00
|
|
|
TimeStampLast + CacheTime > Now.
|
2010-05-10 16:42:54 +02:00
|
|
|
|
|
|
|
%% Code copied from mod_configure.erl
|
|
|
|
%% Code copied from web/ejabberd_web_admin.erl
|
|
|
|
%% TODO: Update time format to XEP-0202: Entity Time
|
2016-03-07 14:47:42 +01:00
|
|
|
-spec(get_last_access(User::binary(), Server::binary()) -> (online | never | mod_last_required | integer())).
|
2010-05-10 16:42:54 +02:00
|
|
|
get_last_access(User, Server) ->
|
|
|
|
case ejabberd_sm:get_user_resources(User, Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] ->
|
|
|
|
case get_last_info(User, Server) of
|
|
|
|
mod_last_required -> mod_last_required;
|
|
|
|
not_found -> never;
|
|
|
|
{ok, Timestamp, _Status} -> Timestamp
|
|
|
|
end;
|
|
|
|
_ -> online
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
|
|
|
%% @spec (User, Server) -> {ok, Timestamp, Status} | not_found | mod_last_required
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-05-10 16:42:54 +02:00
|
|
|
get_last_info(User, Server) ->
|
|
|
|
case get_mod_last_enabled(Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
mod_last -> mod_last:get_last_info(User, Server);
|
|
|
|
no_mod_last -> mod_last_required
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
%% @spec (Server) -> mod_last | no_mod_last
|
2010-05-10 16:42:54 +02:00
|
|
|
get_mod_last_enabled(Server) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
case gen_mod:is_loaded(Server, mod_last) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> mod_last;
|
|
|
|
false -> no_mod_last
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
2010-12-03 17:23:24 +01:00
|
|
|
|
|
|
|
get_mod_last_configured(Server) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
case is_configured(Server, mod_last) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> mod_last;
|
|
|
|
false -> no_mod_last
|
2010-12-03 17:23:24 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
is_configured(Host, Module) ->
|
2015-09-24 17:21:14 +02:00
|
|
|
Os = ejabberd_config:get_local_option({modules, Host},
|
|
|
|
fun(M) when is_list(M) -> M end),
|
|
|
|
lists:keymember(Module, 1, Os).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
opt_type(extauth_cache) ->
|
|
|
|
fun (false) -> undefined;
|
|
|
|
(I) when is_integer(I), I >= 0 -> I
|
|
|
|
end;
|
|
|
|
opt_type(extauth_program) ->
|
|
|
|
fun (V) -> binary_to_list(iolist_to_binary(V)) end;
|
|
|
|
opt_type(_) -> [extauth_cache, extauth_program].
|