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
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2004-07-30 23:09:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(extauth).
|
|
|
|
-author('leifj@it.su.se').
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
-export([start/2, stop/1, init/2,
|
|
|
|
check_password/3, set_password/3, is_user_exists/2]).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2008-03-21 15:44:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2008-12-01 16:53:30 +01:00
|
|
|
-define(INIT_TIMEOUT, 60000). % Timeout is in milliseconds: 60 seconds == 60000
|
|
|
|
-define(CALL_TIMEOUT, 10000). % Timeout is in milliseconds: 10 seconds == 10000
|
2008-05-17 13:44:25 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
start(Host, ExtPrg) ->
|
|
|
|
spawn(?MODULE, init, [Host, ExtPrg]).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
init(Host, ExtPrg) ->
|
|
|
|
register(gen_mod:get_module_proc(Host, eauth), self()),
|
2004-07-30 23:09:55 +02:00
|
|
|
process_flag(trap_exit,true),
|
|
|
|
Port = open_port({spawn, ExtPrg}, [{packet,2}]),
|
2008-12-01 16:53:30 +01:00
|
|
|
loop(Port, ?INIT_TIMEOUT).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
stop(Host) ->
|
|
|
|
gen_mod:get_module_proc(Host, eauth) ! stop.
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
check_password(User, Server, Password) ->
|
|
|
|
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) ->
|
|
|
|
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) ->
|
|
|
|
call_port(Server, ["setpass", User, Server, Password]).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
call_port(Server, Msg) ->
|
2008-10-13 11:37:48 +02:00
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2005-07-13 05:24:13 +02:00
|
|
|
gen_mod:get_module_proc(LServer, eauth) ! {call, self(), Msg},
|
2004-07-30 23:09:55 +02:00
|
|
|
receive
|
2005-07-13 05:24:13 +02:00
|
|
|
{eauth,Result} ->
|
2004-07-30 23:09:55 +02:00
|
|
|
Result
|
|
|
|
end.
|
|
|
|
|
2008-12-01 16:53:30 +01:00
|
|
|
loop(Port, Timeout) ->
|
2004-07-30 23:09:55 +02:00
|
|
|
receive
|
|
|
|
{call, Caller, Msg} ->
|
|
|
|
Port ! {self(), {command, encode(Msg)}},
|
|
|
|
receive
|
|
|
|
{Port, {data, Data}} ->
|
2008-05-17 13:44:25 +02:00
|
|
|
?DEBUG("extauth call '~p' received data response:~n~p", [Msg, Data]),
|
2008-12-01 16:53:30 +01:00
|
|
|
Caller ! {eauth, decode(Data)};
|
|
|
|
{Port, Other} ->
|
|
|
|
?ERROR_MSG("extauth call '~p' received strange response:~n~p", [Msg, Other]),
|
|
|
|
Caller ! {eauth, false}
|
2008-05-17 13:44:25 +02:00
|
|
|
after
|
2008-12-01 16:53:30 +01:00
|
|
|
Timeout ->
|
|
|
|
?ERROR_MSG("extauth call '~p' didn't receive response", [Msg]),
|
|
|
|
Caller ! {eauth, false}
|
2004-07-30 23:09:55 +02:00
|
|
|
end,
|
2008-12-01 16:53:30 +01:00
|
|
|
loop(Port, ?CALL_TIMEOUT);
|
2004-07-30 23:09:55 +02:00
|
|
|
stop ->
|
|
|
|
Port ! {self(), close},
|
|
|
|
receive
|
|
|
|
{Port, closed} ->
|
|
|
|
exit(normal)
|
|
|
|
end;
|
|
|
|
{'EXIT', Port, Reason} ->
|
2008-03-21 15:44:16 +01:00
|
|
|
?CRITICAL_MSG("~p ~n", [Reason]),
|
2004-07-30 23:09:55 +02:00
|
|
|
exit(port_terminated)
|
|
|
|
end.
|
|
|
|
|
|
|
|
join(List, Sep) ->
|
|
|
|
lists:foldl(fun(A, "") -> A;
|
|
|
|
(A, Acc) -> Acc ++ Sep ++ A
|
|
|
|
end, "", List).
|
|
|
|
|
|
|
|
encode(L) ->
|
|
|
|
join(L,":").
|
|
|
|
|
|
|
|
decode([0,0]) ->
|
|
|
|
false;
|
|
|
|
decode([0,1]) ->
|
|
|
|
true.
|
|
|
|
|