2002-11-23 21:55:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% 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
|
2002-12-08 18:23:21 +01:00
|
|
|
-export([start/0, start_link/0,
|
|
|
|
set_password/2,
|
|
|
|
check_password/2,
|
2002-12-20 21:42:08 +01:00
|
|
|
check_password/4,
|
2003-01-05 21:24:59 +01:00
|
|
|
try_register/2,
|
|
|
|
dirty_get_registered_users/0,
|
2003-01-21 21:36:55 +01:00
|
|
|
get_password_s/1,
|
2003-01-26 21:16:53 +01:00
|
|
|
is_user_exists/1,
|
|
|
|
remove_user/1]).
|
2002-11-23 21:55:05 +01:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
2003-01-24 21:18:33 +01:00
|
|
|
-export([init/1,
|
|
|
|
handle_call/3,
|
|
|
|
handle_cast/2,
|
|
|
|
code_change/3,
|
|
|
|
handle_info/2,
|
|
|
|
terminate/2]).
|
2002-11-23 21:55:05 +01:00
|
|
|
|
|
|
|
-record(state, {}).
|
|
|
|
|
2002-12-08 18:23:21 +01:00
|
|
|
-record(passwd, {user, password}).
|
2002-11-23 21:55:05 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% 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}.
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
|
|
|
|
code_change(OldVsn, State, Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
2002-11-23 21:55:05 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% 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
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2002-12-08 18:23:21 +01:00
|
|
|
check_password(User, Password) ->
|
2002-12-14 21:07:26 +01:00
|
|
|
LUser = jlib:tolower(User),
|
2002-11-23 21:55:05 +01:00
|
|
|
F = fun() ->
|
2002-12-14 21:07:26 +01:00
|
|
|
case mnesia:read({passwd, LUser}) of
|
2002-11-23 21:55:05 +01:00
|
|
|
[E] ->
|
|
|
|
E#passwd.password
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Password} ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2002-12-20 21:42:08 +01:00
|
|
|
check_password(User, Password, StreamID, Digest) ->
|
|
|
|
LUser = jlib:tolower(User),
|
|
|
|
F = fun() ->
|
|
|
|
case mnesia:read({passwd, LUser}) of
|
|
|
|
[E] ->
|
|
|
|
E#passwd.password
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Passwd} ->
|
|
|
|
DigRes = if
|
|
|
|
Digest /= "" ->
|
|
|
|
Digest == sha:sha(StreamID ++ Passwd);
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end,
|
|
|
|
if DigRes ->
|
|
|
|
true;
|
|
|
|
true ->
|
|
|
|
Passwd == Password
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2002-12-08 18:23:21 +01:00
|
|
|
set_password(User, Password) ->
|
2002-12-14 21:07:26 +01:00
|
|
|
LUser = jlib:tolower(User),
|
2002-12-08 18:23:21 +01:00
|
|
|
F = fun() ->
|
2002-12-14 21:07:26 +01:00
|
|
|
mnesia:write(#passwd{user = LUser, password = Password})
|
2002-12-08 18:23:21 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
try_register(User, Password) ->
|
2002-12-14 21:07:26 +01:00
|
|
|
LUser = jlib:tolower(User),
|
2002-11-23 21:55:05 +01:00
|
|
|
F = fun() ->
|
2002-12-14 21:07:26 +01:00
|
|
|
case mnesia:read({passwd, LUser}) of
|
2002-11-23 21:55:05 +01:00
|
|
|
[] ->
|
2002-12-14 21:07:26 +01:00
|
|
|
mnesia:write(#passwd{user = LUser,
|
|
|
|
password = Password}),
|
2002-12-08 18:23:21 +01:00
|
|
|
ok;
|
2002-11-23 21:55:05 +01:00
|
|
|
[E] ->
|
2002-12-08 18:23:21 +01:00
|
|
|
exists
|
2002-11-23 21:55:05 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2003-01-01 20:54:44 +01:00
|
|
|
dirty_get_registered_users() ->
|
|
|
|
mnesia:dirty_all_keys(passwd).
|
|
|
|
|
2003-01-21 21:36:55 +01:00
|
|
|
get_password_s(User) ->
|
|
|
|
LUser = jlib:tolower(User),
|
|
|
|
case catch mnesia:dirty_read(passwd, LUser) of
|
|
|
|
[#passwd{password = Password}] ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
is_user_exists(User) ->
|
|
|
|
LUser = jlib:tolower(User),
|
2003-01-29 18:12:23 +01:00
|
|
|
case catch mnesia:dirty_read({passwd, LUser}) of
|
|
|
|
[] ->
|
|
|
|
false;
|
|
|
|
[_] ->
|
|
|
|
true;
|
2003-01-05 21:24:59 +01:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
2003-01-26 21:16:53 +01:00
|
|
|
|
|
|
|
remove_user(User) ->
|
|
|
|
LUser = jlib:tolower(User),
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({passwd, LUser})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|