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>
|
2019-07-16 21:07:39 +02:00
|
|
|
%%% Purpose : Authentication via LDAP external script
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 12 Dec 2004 by Alexey Shchepin <alexey@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
|
|
|
%%%
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_auth_external).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
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).
|
|
|
|
|
2018-05-07 18:27:18 +02:00
|
|
|
-export([start/1, stop/1, reload/1, set_password/3, check_password/4,
|
2017-05-11 14:49:06 +02:00
|
|
|
try_register/3, user_exists/2, remove_user/2,
|
2019-06-14 11:33:26 +02:00
|
|
|
store_type/1, plain_password_required/1]).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
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) ->
|
2018-05-07 18:27:18 +02:00
|
|
|
extauth:start(Host).
|
2010-05-10 16:42:54 +02:00
|
|
|
|
2017-02-23 14:19:22 +01:00
|
|
|
stop(Host) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
extauth:stop(Host).
|
2017-02-23 14:19:22 +01:00
|
|
|
|
2018-05-07 18:27:18 +02:00
|
|
|
reload(Host) ->
|
|
|
|
extauth:reload(Host).
|
|
|
|
|
2017-05-11 13:37:21 +02:00
|
|
|
plain_password_required(_) -> true.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2017-05-11 13:37:21 +02: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 ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{nocache, false};
|
2016-05-24 00:40:25 +02:00
|
|
|
true ->
|
2017-05-11 13:37:21 +02:00
|
|
|
check_password_extauth(User, AuthzId, Server, Password)
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
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
|
2019-06-30 16:15:43 +02:00
|
|
|
Res when is_boolean(Res) -> {cache, {ok, Password}};
|
2018-05-08 11:06:58 +02:00
|
|
|
{error, Reason} -> failure(User, Server, set_password, Reason)
|
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) ->
|
2018-05-07 18:27:18 +02:00
|
|
|
case extauth:try_register(User, Server, Password) of
|
2019-06-30 16:15:43 +02:00
|
|
|
true -> {cache, {ok, Password}};
|
|
|
|
false -> {cache, {error, not_allowed}};
|
2018-05-08 11:06:58 +02:00
|
|
|
{error, Reason} -> failure(User, Server, try_register, Reason)
|
2018-05-07 18:27:18 +02:00
|
|
|
end.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2017-05-11 14:49:06 +02:00
|
|
|
user_exists(User, Server) ->
|
2018-05-07 18:27:18 +02:00
|
|
|
case extauth:user_exists(User, Server) of
|
2019-06-30 16:15:43 +02:00
|
|
|
Res when is_boolean(Res) -> {cache, Res};
|
2018-05-08 11:06:58 +02:00
|
|
|
{error, Reason} -> failure(User, Server, user_exists, Reason)
|
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
|
2017-05-11 13:37:21 +02:00
|
|
|
false -> {error, not_allowed};
|
2018-05-07 18:27:18 +02:00
|
|
|
true -> ok;
|
2019-06-30 16:15:43 +02:00
|
|
|
{error, Reason} ->
|
|
|
|
{_, Err} = failure(User, Server, remove_user, Reason),
|
|
|
|
Err
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-09 03:21:09 +02:00
|
|
|
check_password_extauth(User, _AuthzId, Server, Password) ->
|
2018-05-07 18:27:18 +02:00
|
|
|
if Password /= <<"">> ->
|
|
|
|
case extauth:check_password(User, Server, Password) of
|
2019-06-30 16:15:43 +02:00
|
|
|
Res when is_boolean(Res) -> {cache, Res};
|
2018-05-07 18:27:18 +02:00
|
|
|
{error, Reason} ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{Tag, _} = failure(User, Server, check_password, Reason),
|
|
|
|
{Tag, false}
|
2018-05-07 18:27:18 +02:00
|
|
|
end;
|
|
|
|
true ->
|
2019-06-30 16:15:43 +02:00
|
|
|
{nocache, false}
|
2018-05-07 18:27:18 +02:00
|
|
|
end.
|
|
|
|
|
2019-06-30 16:15:43 +02:00
|
|
|
-spec failure(binary(), binary(), atom(), any()) -> {nocache, {error, db_failure}}.
|
2018-05-08 08:36:34 +02:00
|
|
|
failure(User, Server, Fun, Reason) ->
|
|
|
|
?ERROR_MSG("External authentication program failed when calling "
|
|
|
|
"'~s' for ~s@~s: ~p", [Fun, User, Server, Reason]),
|
2019-06-30 16:15:43 +02:00
|
|
|
{nocache, {error, db_failure}}.
|