123 lines
3.9 KiB
Erlang
123 lines
3.9 KiB
Erlang
|
%%%----------------------------------------------------------------------
|
||
|
%%% File : ejabberd_auth.erl
|
||
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
||
|
%%% Purpose :
|
||
|
%%% Created : 23 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
|
||
|
%%% Id : $Id$
|
||
|
%%%----------------------------------------------------------------------
|
||
|
|
||
|
-module(ejabberd_auth).
|
||
|
-author('alexey@sevcom.net').
|
||
|
-vsn('$Revision$ ').
|
||
|
|
||
|
%%-compile(export_all).
|
||
|
%%-export([Function/Arity, ...]).
|
||
|
|
||
|
-behaviour(gen_server).
|
||
|
|
||
|
%% External exports
|
||
|
-export([start/0, start_link/0, set_password/2, check_password/2]).
|
||
|
|
||
|
%% gen_server callbacks
|
||
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
|
||
|
|
||
|
-record(state, {}).
|
||
|
|
||
|
-record(passwd, {jid, password}).
|
||
|
|
||
|
%%%----------------------------------------------------------------------
|
||
|
%%% API
|
||
|
%%%----------------------------------------------------------------------
|
||
|
start() ->
|
||
|
gen_server:start({local, ejabberd_auth}, ejabberd_auth, [], []).
|
||
|
start_link() ->
|
||
|
gen_server:start_link({local, ejabberd_auth}, ejabberd_auth, [], []).
|
||
|
|
||
|
%%%----------------------------------------------------------------------
|
||
|
%%% Callback functions from gen_server
|
||
|
%%%----------------------------------------------------------------------
|
||
|
|
||
|
%%----------------------------------------------------------------------
|
||
|
%% Func: init/1
|
||
|
%% Returns: {ok, State} |
|
||
|
%% {ok, State, Timeout} |
|
||
|
%% ignore |
|
||
|
%% {stop, Reason}
|
||
|
%%----------------------------------------------------------------------
|
||
|
init([]) ->
|
||
|
mnesia:create_table(passwd,[{disc_copies, [node()]},
|
||
|
{attributes, record_info(fields, passwd)}]),
|
||
|
{ok, #state{}}.
|
||
|
|
||
|
%%----------------------------------------------------------------------
|
||
|
%% Func: handle_call/3
|
||
|
%% Returns: {reply, Reply, State} |
|
||
|
%% {reply, Reply, State, Timeout} |
|
||
|
%% {noreply, State} |
|
||
|
%% {noreply, State, Timeout} |
|
||
|
%% {stop, Reason, Reply, State} | (terminate/2 is called)
|
||
|
%% {stop, Reason, State} (terminate/2 is called)
|
||
|
%%----------------------------------------------------------------------
|
||
|
handle_call(Request, From, State) ->
|
||
|
Reply = ok,
|
||
|
{reply, Reply, State}.
|
||
|
|
||
|
%%----------------------------------------------------------------------
|
||
|
%% Func: handle_cast/2
|
||
|
%% Returns: {noreply, State} |
|
||
|
%% {noreply, State, Timeout} |
|
||
|
%% {stop, Reason, State} (terminate/2 is called)
|
||
|
%%----------------------------------------------------------------------
|
||
|
handle_cast(Msg, State) ->
|
||
|
{noreply, State}.
|
||
|
|
||
|
%%----------------------------------------------------------------------
|
||
|
%% Func: handle_info/2
|
||
|
%% Returns: {noreply, State} |
|
||
|
%% {noreply, State, Timeout} |
|
||
|
%% {stop, Reason, State} (terminate/2 is called)
|
||
|
%%----------------------------------------------------------------------
|
||
|
handle_info(Info, State) ->
|
||
|
{noreply, State}.
|
||
|
|
||
|
%%----------------------------------------------------------------------
|
||
|
%% Func: terminate/2
|
||
|
%% Purpose: Shutdown the server
|
||
|
%% Returns: any (ignored by gen_server)
|
||
|
%%----------------------------------------------------------------------
|
||
|
terminate(Reason, State) ->
|
||
|
ok.
|
||
|
|
||
|
%%%----------------------------------------------------------------------
|
||
|
%%% Internal functions
|
||
|
%%%----------------------------------------------------------------------
|
||
|
|
||
|
check_password(Jid, Password) ->
|
||
|
F = fun() ->
|
||
|
case mnesia:read({passwd, Jid}) of
|
||
|
[E] ->
|
||
|
E#passwd.password
|
||
|
end
|
||
|
end,
|
||
|
case mnesia:transaction(F) of
|
||
|
{atomic, Password} ->
|
||
|
true;
|
||
|
_ ->
|
||
|
false
|
||
|
end.
|
||
|
|
||
|
|
||
|
set_password(Jid, Password) ->
|
||
|
F = fun() ->
|
||
|
case mnesia:read({passwd, Jid}) of
|
||
|
[] ->
|
||
|
New = #passwd{jid = Jid, password = Password},
|
||
|
mnesia:write(New);
|
||
|
[E] ->
|
||
|
New = E#passwd{password = Password},
|
||
|
mnesia:write(New)
|
||
|
end
|
||
|
end,
|
||
|
mnesia:transaction(F).
|
||
|
|