2004-07-30 23:09:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : extauth.erl
|
|
|
|
%%% Author : Leif Johansson <leifj@it.su.se>
|
|
|
|
%%% Purpose : External authentication using a simple port-driver
|
|
|
|
%%% Created : 30 Jul 2004 by Leif Johansson <leifj@it.su.se>
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
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-07-30 23:09:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(extauth).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2004-07-30 23:09:55 +02:00
|
|
|
-author('leifj@it.su.se').
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/2, stop/1, init/2, check_password/3,
|
|
|
|
set_password/3, try_register/3, remove_user/2,
|
2015-06-01 14:38:27 +02:00
|
|
|
remove_user/3, is_user_exists/2, opt_type/1]).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2008-03-21 15:44:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(INIT_TIMEOUT, 60000).
|
|
|
|
|
|
|
|
-define(CALL_TIMEOUT, 10000).
|
2008-05-17 13:44:25 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
start(Host, ExtPrg) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (This) ->
|
|
|
|
start_instance(get_process_name(Host, This), ExtPrg)
|
|
|
|
end,
|
|
|
|
lists:seq(0, get_instances(Host) - 1)).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2011-04-11 21:27:19 +02:00
|
|
|
start_instance(ProcessName, ExtPrg) ->
|
|
|
|
spawn(?MODULE, init, [ProcessName, ExtPrg]).
|
|
|
|
|
|
|
|
restart_instance(ProcessName, ExtPrg) ->
|
|
|
|
unregister(ProcessName),
|
|
|
|
start_instance(ProcessName, ExtPrg).
|
|
|
|
|
2010-07-30 20:33:03 +02:00
|
|
|
init(ProcessName, ExtPrg) ->
|
|
|
|
register(ProcessName, self()),
|
2013-03-14 10:33:02 +01:00
|
|
|
process_flag(trap_exit, true),
|
|
|
|
Port = open_port({spawn, ExtPrg}, [{packet, 2}]),
|
2011-01-19 19:06:46 +01:00
|
|
|
loop(Port, ?INIT_TIMEOUT, ProcessName, ExtPrg).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
stop(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (This) ->
|
|
|
|
get_process_name(Host, This) ! stop
|
|
|
|
end,
|
|
|
|
lists:seq(0, get_instances(Host) - 1)).
|
2010-07-30 20:33:03 +02:00
|
|
|
|
|
|
|
get_process_name(Host, Integer) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_mod:get_module_proc(iolist_to_binary([Host,
|
|
|
|
integer_to_list(Integer)]),
|
|
|
|
eauth).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
check_password(User, Server, Password) ->
|
2013-06-26 04:29:50 +02:00
|
|
|
call_port(Server, [<<"auth">>, User, Server, Password]).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
is_user_exists(User, Server) ->
|
2013-06-26 04:29:50 +02:00
|
|
|
call_port(Server, [<<"isuser">>, User, Server]).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
set_password(User, Server, Password) ->
|
2013-06-26 04:29:50 +02:00
|
|
|
call_port(Server, [<<"setpass">>, User, Server, Password]).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2010-05-10 16:42:54 +02:00
|
|
|
try_register(User, Server, Password) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case call_port(Server,
|
2013-06-26 04:29:50 +02:00
|
|
|
[<<"tryregister">>, User, Server, Password])
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
true -> {atomic, ok};
|
|
|
|
false -> {error, not_allowed}
|
2010-05-10 16:42:54 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
remove_user(User, Server) ->
|
2013-06-26 04:29:50 +02:00
|
|
|
call_port(Server, [<<"removeuser">>, User, Server]).
|
2010-05-10 16:42:54 +02:00
|
|
|
|
|
|
|
remove_user(User, Server, Password) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
call_port(Server,
|
2013-06-26 04:29:50 +02:00
|
|
|
[<<"removeuser3">>, User, Server, Password]).
|
2010-05-10 16:42:54 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
call_port(Server, Msg) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
ProcessName = get_process_name(LServer,
|
|
|
|
random_instance(get_instances(LServer))),
|
2010-07-30 20:33:03 +02:00
|
|
|
ProcessName ! {call, self(), Msg},
|
2013-03-14 10:33:02 +01:00
|
|
|
receive {eauth, Result} -> Result end.
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2010-07-30 20:33:03 +02:00
|
|
|
random_instance(MaxNum) ->
|
2016-10-18 07:17:21 +02:00
|
|
|
randoms:uniform(MaxNum) - 1.
|
2010-07-30 20:33:03 +02:00
|
|
|
|
|
|
|
get_instances(Server) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
{extauth_instances, Server},
|
|
|
|
fun(V) when is_integer(V), V > 0 ->
|
|
|
|
V
|
|
|
|
end, 1).
|
2010-07-30 20:33:03 +02:00
|
|
|
|
2011-01-19 19:06:46 +01:00
|
|
|
loop(Port, Timeout, ProcessName, ExtPrg) ->
|
2004-07-30 23:09:55 +02:00
|
|
|
receive
|
2013-03-14 10:33:02 +01:00
|
|
|
{call, Caller, Msg} ->
|
|
|
|
port_command(Port, encode(Msg)),
|
|
|
|
receive
|
|
|
|
{Port, {data, Data}} ->
|
|
|
|
?DEBUG("extauth call '~p' received data response:~n~p",
|
|
|
|
[Msg, Data]),
|
|
|
|
Caller ! {eauth, decode(Data)},
|
|
|
|
loop(Port, ?CALL_TIMEOUT, ProcessName, ExtPrg);
|
|
|
|
{Port, Other} ->
|
|
|
|
?ERROR_MSG("extauth call '~p' received strange response:~n~p",
|
|
|
|
[Msg, Other]),
|
|
|
|
Caller ! {eauth, false},
|
|
|
|
loop(Port, ?CALL_TIMEOUT, ProcessName, ExtPrg)
|
|
|
|
after Timeout ->
|
|
|
|
?ERROR_MSG("extauth call '~p' didn't receive response",
|
|
|
|
[Msg]),
|
|
|
|
Caller ! {eauth, false},
|
|
|
|
Pid = restart_instance(ProcessName, ExtPrg),
|
|
|
|
flush_buffer_and_forward_messages(Pid),
|
|
|
|
exit(port_terminated)
|
|
|
|
end;
|
|
|
|
stop ->
|
|
|
|
Port ! {self(), close},
|
|
|
|
receive {Port, closed} -> exit(normal) end;
|
|
|
|
{'EXIT', Port, Reason} ->
|
|
|
|
?CRITICAL_MSG("extauth script has exitted abruptly "
|
|
|
|
"with reason '~p'",
|
|
|
|
[Reason]),
|
|
|
|
Pid = restart_instance(ProcessName, ExtPrg),
|
|
|
|
flush_buffer_and_forward_messages(Pid),
|
|
|
|
exit(port_terminated)
|
2004-07-30 23:09:55 +02:00
|
|
|
end.
|
|
|
|
|
2011-02-21 16:13:41 +01:00
|
|
|
flush_buffer_and_forward_messages(Pid) ->
|
|
|
|
receive
|
2013-03-14 10:33:02 +01:00
|
|
|
Message ->
|
|
|
|
Pid ! Message, flush_buffer_and_forward_messages(Pid)
|
|
|
|
after 0 -> true
|
2011-02-21 16:13:41 +01:00
|
|
|
end.
|
|
|
|
|
2013-06-26 04:29:50 +02:00
|
|
|
encode(L) -> str:join(L, <<":">>).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
decode([0, 0]) -> false;
|
|
|
|
decode([0, 1]) -> true.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
opt_type(extauth_instances) ->
|
|
|
|
fun (V) when is_integer(V), V > 0 -> V end;
|
|
|
|
opt_type(_) -> [extauth_instances].
|