25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00
xmpp.chapril.org-ejabberd/src/extauth.erl

220 lines
6.8 KiB
Erlang
Raw Normal View History

%%%-------------------------------------------------------------------
%%% Created : 7 May 2018 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%
%%%
2019-01-08 22:53:27 +01:00
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
%%%
%%% 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.
%%%
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.
%%%
%%%-------------------------------------------------------------------
-module(extauth).
-ifndef(GEN_SERVER).
-define(GEN_SERVER, gen_server).
-endif.
-behaviour(?GEN_SERVER).
2015-06-01 14:38:27 +02:00
-define(CALL_TIMEOUT, timer:seconds(30)).
%% API
-export([start/1, stop/1, reload/1, start_link/2]).
-export([check_password/3, set_password/3, try_register/3, remove_user/2,
2019-07-24 09:13:51 +02:00
remove_user/3, user_exists/2, check_certificate/3]).
-export([prog_name/1, pool_name/1, worker_name/2, pool_size/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include("logger.hrl").
-record(state, {port :: port(),
prog :: string(),
start_time :: integer(),
os_pid :: integer() | undefined}).
%%%===================================================================
%%% API
%%%===================================================================
start(Host) ->
extauth_sup:start(Host).
stop(Host) ->
extauth_sup:stop(Host).
reload(Host) ->
extauth_sup:reload(Host).
start_link(Name, Prog) ->
?GEN_SERVER:start_link({local, Name}, ?MODULE, [Prog], []).
check_password(User, Server, Password) ->
2013-06-26 04:29:50 +02:00
call_port(Server, [<<"auth">>, User, Server, Password]).
2019-07-24 09:13:51 +02:00
check_certificate(User, Server, Certificate) ->
call_port(Server, [<<"certauth">>, User, Server, Certificate]).
2017-05-11 14:49:06 +02:00
user_exists(User, Server) ->
2013-06-26 04:29:50 +02:00
call_port(Server, [<<"isuser">>, User, Server]).
set_password(User, Server, Password) ->
2013-06-26 04:29:50 +02:00
call_port(Server, [<<"setpass">>, User, Server, Password]).
try_register(User, Server, Password) ->
call_port(Server, [<<"tryregister">>, User, Server, Password]).
remove_user(User, Server) ->
2013-06-26 04:29:50 +02:00
call_port(Server, [<<"removeuser">>, User, Server]).
remove_user(User, Server, Password) ->
call_port(Server, [<<"removeuser3">>, User, Server, Password]).
-spec prog_name(binary()) -> string() | undefined.
prog_name(Host) ->
2019-06-14 11:33:26 +02:00
ejabberd_option:extauth_program(Host).
-spec pool_name(binary()) -> atom().
pool_name(Host) ->
2019-06-14 11:33:26 +02:00
case ejabberd_option:extauth_pool_name(Host) of
2018-07-30 22:49:18 +02:00
undefined ->
list_to_atom("extauth_pool_" ++ binary_to_list(Host));
Name ->
list_to_atom("extauth_pool_" ++ binary_to_list(Name))
end.
-spec worker_name(atom(), integer()) -> atom().
worker_name(Pool, N) ->
list_to_atom(atom_to_list(Pool) ++ "_" ++ integer_to_list(N)).
-spec pool_size(binary()) -> pos_integer().
pool_size(Host) ->
2019-06-14 11:33:26 +02:00
case ejabberd_option:extauth_pool_size(Host) of
undefined ->
try erlang:system_info(logical_processors)
catch _:_ -> 1
end;
Size ->
Size
end.
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init([Prog]) ->
process_flag(trap_exit, true),
{Port, OSPid} = start_port(Prog),
Time = curr_time(),
{ok, #state{port = Port, start_time = Time,
prog = Prog, os_pid = OSPid}}.
handle_call({cmd, Cmd, EndTime}, _From, State) ->
Timeout = EndTime - curr_time(),
if Timeout > 0 ->
Port = State#state.port,
port_command(Port, Cmd),
receive
{Port, {data, [0, N] = Data}} when N == 0; N == 1 ->
?DEBUG("Received response from external authentication "
"program: ~p", [Data]),
{reply, decode_bool(N), State};
{Port, Data} ->
?ERROR_MSG("Received unexpected response from external "
"authentication program '~ts': ~p "
"(port = ~p, pid = ~w)",
[State#state.prog, Data, Port, State#state.os_pid]),
{reply, {error, unexpected_response}, State};
{'EXIT', Port, Reason} ->
handle_info({'EXIT', Port, Reason}, State)
after Timeout ->
{stop, normal, State}
end;
true ->
2018-05-07 21:43:01 +02:00
{noreply, State}
end.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info({'EXIT', Port, _Reason}, #state{port = Port,
start_time = Time} = State) ->
case curr_time() - Time of
Diff when Diff < 1000 ->
?ERROR_MSG("Failed to start external authentication program '~ts'",
[State#state.prog]),
{stop, normal, State};
_ ->
?ERROR_MSG("External authentication program '~ts' has terminated "
"unexpectedly (pid=~w), restarting via supervisor...",
[State#state.prog, State#state.os_pid]),
{stop, normal, State}
end;
handle_info(Info, State) ->
?WARNING_MSG("Unexpected info: ~p", [Info]),
{noreply, State}.
terminate(_Reason, State) ->
catch port_close(State#state.port),
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
-spec curr_time() -> non_neg_integer().
curr_time() ->
erlang:monotonic_time(millisecond).
-spec start_port(string()) -> {port(), integer() | undefined}.
start_port(Path) ->
Port = open_port({spawn, Path}, [{packet, 2}]),
link(Port),
case erlang:port_info(Port, os_pid) of
{os_pid, OSPid} ->
{Port, OSPid};
undefined ->
{Port, undefined}
end.
call_port(Server, Args) ->
call_port(Server, Args, ?CALL_TIMEOUT).
call_port(Server, Args, Timeout) ->
StartTime = erlang:monotonic_time(millisecond),
Pool = pool_name(Server),
PoolSize = pool_size(Server),
I = p1_rand:round_robin(PoolSize),
Cmd = str:join(Args, <<":">>),
do_call(Cmd, I, I + PoolSize, Pool, PoolSize,
StartTime + Timeout, StartTime).
do_call(_, Max, Max, _, _, _, _) ->
{error, disconnected};
do_call(Cmd, I, Max, Pool, PoolSize, EndTime, CurrTime) ->
Timeout = EndTime - CurrTime,
if Timeout > 0 ->
Proc = worker_name(Pool, (I rem PoolSize) + 1),
try ?GEN_SERVER:call(Proc, {cmd, Cmd, EndTime}, Timeout)
catch exit:{timeout, {?GEN_SERVER, call, _}} ->
{error, timeout};
exit:{_, {?GEN_SERVER, call, _}} ->
do_call(Cmd, I+1, Max, Pool, PoolSize, EndTime, curr_time())
end;
true ->
{error, timeout}
end.
2015-06-01 14:38:27 +02:00
decode_bool(0) -> false;
decode_bool(1) -> true.