aboutsummaryrefslogtreecommitdiff
path: root/src/extauth.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/extauth.erl')
-rw-r--r--src/extauth.erl162
1 files changed, 77 insertions, 85 deletions
diff --git a/src/extauth.erl b/src/extauth.erl
index f9162beb3..52b3be2a0 100644
--- a/src/extauth.erl
+++ b/src/extauth.erl
@@ -25,30 +25,24 @@
%%%----------------------------------------------------------------------
-module(extauth).
+
-author('leifj@it.su.se').
--export([start/2,
- stop/1,
- init/2,
- check_password/3,
- set_password/3,
- try_register/3,
- remove_user/2,
- remove_user/3,
- is_user_exists/2]).
+-export([start/2, stop/1, init/2, check_password/3,
+ set_password/3, try_register/3, remove_user/2,
+ remove_user/3, is_user_exists/2]).
-include("ejabberd.hrl").
--define(INIT_TIMEOUT, 60000). % Timeout is in milliseconds: 60 seconds == 60000
--define(CALL_TIMEOUT, 10000). % Timeout is in milliseconds: 10 seconds == 10000
+-define(INIT_TIMEOUT, 60000).
+
+-define(CALL_TIMEOUT, 10000).
start(Host, ExtPrg) ->
- lists:foreach(
- fun(This) ->
- start_instance(get_process_name(Host, This), ExtPrg)
- end,
- lists:seq(0, get_instances(Host)-1)
- ).
+ lists:foreach(fun (This) ->
+ start_instance(get_process_name(Host, This), ExtPrg)
+ end,
+ lists:seq(0, get_instances(Host) - 1)).
start_instance(ProcessName, ExtPrg) ->
spawn(?MODULE, init, [ProcessName, ExtPrg]).
@@ -59,20 +53,20 @@ restart_instance(ProcessName, ExtPrg) ->
init(ProcessName, ExtPrg) ->
register(ProcessName, self()),
- process_flag(trap_exit,true),
- Port = open_port({spawn, ExtPrg}, [{packet,2}]),
+ process_flag(trap_exit, true),
+ Port = open_port({spawn, ExtPrg}, [{packet, 2}]),
loop(Port, ?INIT_TIMEOUT, ProcessName, ExtPrg).
stop(Host) ->
- lists:foreach(
- fun(This) ->
- get_process_name(Host, This) ! stop
- end,
- lists:seq(0, get_instances(Host)-1)
- ).
+ lists:foreach(fun (This) ->
+ get_process_name(Host, This) ! stop
+ end,
+ lists:seq(0, get_instances(Host) - 1)).
get_process_name(Host, Integer) ->
- gen_mod:get_module_proc(lists:append([Host, integer_to_list(Integer)]), eauth).
+ gen_mod:get_module_proc(iolist_to_binary([Host,
+ integer_to_list(Integer)]),
+ eauth).
check_password(User, Server, Password) ->
call_port(Server, ["auth", User, Server, Password]).
@@ -84,90 +78,88 @@ set_password(User, Server, Password) ->
call_port(Server, ["setpass", User, Server, Password]).
try_register(User, Server, Password) ->
- case call_port(Server, ["tryregister", User, Server, Password]) of
- true -> {atomic, ok};
- false -> {error, not_allowed}
+ case call_port(Server,
+ ["tryregister", User, Server, Password])
+ of
+ true -> {atomic, ok};
+ false -> {error, not_allowed}
end.
remove_user(User, Server) ->
call_port(Server, ["removeuser", User, Server]).
remove_user(User, Server, Password) ->
- call_port(Server, ["removeuser3", User, Server, Password]).
+ call_port(Server,
+ ["removeuser3", User, Server, Password]).
call_port(Server, Msg) ->
LServer = jlib:nameprep(Server),
- ProcessName = get_process_name(LServer, random_instance(get_instances(LServer))),
+ ProcessName = get_process_name(LServer,
+ random_instance(get_instances(LServer))),
ProcessName ! {call, self(), Msg},
- receive
- {eauth,Result} ->
- Result
- end.
+ receive {eauth, Result} -> Result end.
random_instance(MaxNum) ->
- {A1,A2,A3} = now(),
+ {A1, A2, A3} = now(),
random:seed(A1, A2, A3),
random:uniform(MaxNum) - 1.
get_instances(Server) ->
- case ejabberd_config:get_local_option({extauth_instances, Server}) of
- Num when is_integer(Num) -> Num;
- _ -> 1
- end.
+ ejabberd_config:get_local_option(
+ {extauth_instances, Server},
+ fun(V) when is_integer(V), V > 0 ->
+ V
+ end, 1).
loop(Port, Timeout, ProcessName, ExtPrg) ->
receive
- {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)
+ {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)
end.
flush_buffer_and_forward_messages(Pid) ->
receive
- Message ->
- Pid ! Message,
- flush_buffer_and_forward_messages(Pid)
- after 0 ->
- true
+ Message ->
+ Pid ! Message, flush_buffer_and_forward_messages(Pid)
+ after 0 -> true
end.
join(List, Sep) ->
- lists:foldl(fun(A, "") -> A;
- (A, Acc) -> Acc ++ Sep ++ A
- end, "", List).
-
-encode(L) ->
- join(L,":").
+ lists:foldl(fun (A, "") -> A;
+ (A, Acc) -> Acc ++ Sep ++ A
+ end,
+ "", List).
-decode([0,0]) ->
- false;
-decode([0,1]) ->
- true.
+encode(L) -> join(L, ":").
+decode([0, 0]) -> false;
+decode([0, 1]) -> true.