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$ ').
|
|
|
|
|
|
|
|
-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-03-12 20:48:05 +01:00
|
|
|
get_password/1,
|
2003-01-21 21:36:55 +01:00
|
|
|
get_password_s/1,
|
2003-01-26 21:16:53 +01:00
|
|
|
is_user_exists/1,
|
2003-02-21 20:52:15 +01:00
|
|
|
remove_user/1,
|
2003-11-23 21:11:21 +01:00
|
|
|
remove_user/2,
|
|
|
|
plain_password_required/0,
|
|
|
|
check_password_ldap/2, % TODO: remove
|
|
|
|
is_user_exists_ldap/1 % TODO: remove
|
|
|
|
]).
|
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
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
-include("eldap/eldap.hrl").
|
|
|
|
|
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() ->
|
2004-07-30 23:09:55 +02:00
|
|
|
case auth_method() of
|
|
|
|
external ->
|
|
|
|
extauth:start(ejabberd_config:get_local_option(extauth_program));
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2002-11-23 21:55:05 +01:00
|
|
|
gen_server:start({local, ejabberd_auth}, ejabberd_auth, [], []).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2002-11-23 21:55:05 +01:00
|
|
|
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)}]),
|
2003-11-23 21:11:21 +01:00
|
|
|
case auth_method() of
|
|
|
|
internal ->
|
|
|
|
ok;
|
2004-07-30 23:09:55 +02:00
|
|
|
external ->
|
|
|
|
ok;
|
2003-11-23 21:11:21 +01:00
|
|
|
ldap ->
|
|
|
|
LDAPServers = ejabberd_config:get_local_option(ldap_servers),
|
2004-07-25 23:27:56 +02:00
|
|
|
eldap:start_link("ejabberd", LDAPServers, 389, "", ""),
|
|
|
|
eldap:start_link("ejabberd_bind", LDAPServers, 389, "", "")
|
2003-11-23 21:11:21 +01:00
|
|
|
end,
|
2002-11-23 21:55:05 +01:00
|
|
|
{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)
|
|
|
|
%%----------------------------------------------------------------------
|
2003-10-27 20:35:03 +01:00
|
|
|
handle_call(_Request, _From, State) ->
|
2002-11-23 21:55:05 +01:00
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, State}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_cast/2
|
|
|
|
%% Returns: {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State} (terminate/2 is called)
|
|
|
|
%%----------------------------------------------------------------------
|
2003-10-27 20:35:03 +01:00
|
|
|
handle_cast(_Msg, State) ->
|
2002-11-23 21:55:05 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2003-10-27 20:35:03 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
{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)
|
|
|
|
%%----------------------------------------------------------------------
|
2003-10-27 20:35:03 +01:00
|
|
|
handle_info(_Info, State) ->
|
2002-11-23 21:55:05 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/2
|
|
|
|
%% Purpose: Shutdown the server
|
|
|
|
%% Returns: any (ignored by gen_server)
|
|
|
|
%%----------------------------------------------------------------------
|
2003-10-27 20:35:03 +01:00
|
|
|
terminate(_Reason, _State) ->
|
2002-11-23 21:55:05 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
auth_method() ->
|
|
|
|
case ejabberd_config:get_local_option(auth_method) of
|
2004-07-30 23:09:55 +02:00
|
|
|
external ->
|
|
|
|
external;
|
|
|
|
ldap ->
|
|
|
|
ldap;
|
|
|
|
_ ->
|
|
|
|
internal
|
|
|
|
end.
|
|
|
|
|
|
|
|
user_method() ->
|
|
|
|
case ejabberd_config:get_local_option(user_method) of
|
2003-11-23 21:11:21 +01:00
|
|
|
ldap ->
|
|
|
|
ldap;
|
|
|
|
_ ->
|
|
|
|
internal
|
|
|
|
end.
|
|
|
|
|
|
|
|
plain_password_required() ->
|
|
|
|
case auth_method() of
|
|
|
|
internal ->
|
|
|
|
false;
|
2004-07-30 23:09:55 +02:00
|
|
|
external ->
|
|
|
|
true;
|
2003-11-23 21:11:21 +01:00
|
|
|
ldap ->
|
|
|
|
true
|
|
|
|
end.
|
|
|
|
|
2002-12-08 18:23:21 +01:00
|
|
|
check_password(User, Password) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
case auth_method() of
|
|
|
|
internal ->
|
|
|
|
check_password_internal(User, Password);
|
2004-07-30 23:09:55 +02:00
|
|
|
external ->
|
|
|
|
check_password_external(User, Password);
|
2003-11-23 21:11:21 +01:00
|
|
|
ldap ->
|
|
|
|
check_password_ldap(User, Password)
|
|
|
|
end.
|
|
|
|
|
2004-07-30 23:09:55 +02:00
|
|
|
check_password_external(User, Password) ->
|
|
|
|
extauth:check_password(User, Password).
|
|
|
|
|
|
|
|
set_password_external(User, Password) ->
|
|
|
|
extauth:set_password(User, Password).
|
|
|
|
|
|
|
|
is_user_exists_external(User) ->
|
|
|
|
extauth:is_user_exists(User).
|
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
check_password_internal(User, Password) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2003-03-09 21:46:47 +01:00
|
|
|
case catch mnesia:dirty_read({passwd, LUser}) of
|
|
|
|
[#passwd{password = Password}] ->
|
2002-11-23 21:55:05 +01:00
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2002-12-20 21:42:08 +01:00
|
|
|
check_password(User, Password, StreamID, Digest) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
case auth_method() of
|
|
|
|
internal ->
|
|
|
|
check_password_internal(User, Password, StreamID, Digest);
|
2004-07-30 23:09:55 +02:00
|
|
|
external ->
|
|
|
|
check_password_external(User, Password, StreamID, Digest);
|
2003-11-23 21:11:21 +01:00
|
|
|
ldap ->
|
|
|
|
check_password_ldap(User, Password, StreamID, Digest)
|
|
|
|
end.
|
|
|
|
|
|
|
|
check_password_internal(User, Password, StreamID, Digest) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2003-10-27 20:35:03 +01:00
|
|
|
case catch mnesia:dirty_read({passwd, LUser}) of
|
|
|
|
[#passwd{password = Passwd}] ->
|
2002-12-20 21:42:08 +01:00
|
|
|
DigRes = if
|
|
|
|
Digest /= "" ->
|
|
|
|
Digest == sha:sha(StreamID ++ Passwd);
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end,
|
|
|
|
if DigRes ->
|
|
|
|
true;
|
|
|
|
true ->
|
2004-07-05 23:51:23 +02:00
|
|
|
(Passwd == Password) and (Password /= "")
|
2002-12-20 21:42:08 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2002-12-08 18:23:21 +01:00
|
|
|
set_password(User, Password) ->
|
2004-07-30 23:09:55 +02:00
|
|
|
case auth_method() of
|
|
|
|
internal ->
|
|
|
|
set_password_internal(User,Password);
|
|
|
|
external ->
|
|
|
|
set_password_external(User,Password);
|
|
|
|
ldap -> {error, not_allowed}
|
|
|
|
end.
|
|
|
|
|
|
|
|
set_password_internal(User, Password) ->
|
2003-10-17 21:15:38 +02:00
|
|
|
case jlib:nodeprep(User) of
|
|
|
|
error -> {error, invalid_jid};
|
|
|
|
LUser ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#passwd{user = LUser,
|
|
|
|
password = Password})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2002-12-08 18:23:21 +01:00
|
|
|
try_register(User, Password) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
case auth_method() of
|
|
|
|
internal ->
|
|
|
|
try_register_internal(User, Password);
|
2004-07-30 23:09:55 +02:00
|
|
|
external ->
|
|
|
|
{error, not_allowed};
|
2003-11-23 21:11:21 +01:00
|
|
|
ldap ->
|
|
|
|
{error, not_allowed}
|
|
|
|
end.
|
|
|
|
|
|
|
|
try_register_internal(User, Password) ->
|
2003-10-17 21:15:38 +02:00
|
|
|
case jlib:nodeprep(User) of
|
|
|
|
error -> {error, invalid_jid};
|
|
|
|
LUser ->
|
|
|
|
F = fun() ->
|
|
|
|
case mnesia:read({passwd, LUser}) of
|
|
|
|
[] ->
|
|
|
|
mnesia:write(#passwd{user = LUser,
|
|
|
|
password = Password}),
|
|
|
|
ok;
|
2003-10-27 20:35:03 +01:00
|
|
|
[_E] ->
|
2003-10-17 21:15:38 +02:00
|
|
|
exists
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end.
|
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-03-12 20:48:05 +01:00
|
|
|
get_password(User) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2003-03-12 20:48:05 +01:00
|
|
|
case catch mnesia:dirty_read(passwd, LUser) of
|
|
|
|
[#passwd{password = Password}] ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2003-01-21 21:36:55 +01:00
|
|
|
get_password_s(User) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2003-01-21 21:36:55 +01:00
|
|
|
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) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
case auth_method() of
|
|
|
|
internal ->
|
|
|
|
is_user_exists_internal(User);
|
2004-07-30 23:09:55 +02:00
|
|
|
external ->
|
|
|
|
is_user_exists_external(User);
|
2003-11-23 21:11:21 +01:00
|
|
|
ldap ->
|
|
|
|
is_user_exists_ldap(User)
|
|
|
|
end.
|
|
|
|
|
|
|
|
is_user_exists_internal(User) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(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) ->
|
2004-07-30 23:09:55 +02:00
|
|
|
case user_method() of
|
2003-11-23 21:11:21 +01:00
|
|
|
internal ->
|
|
|
|
remove_user_internal(User);
|
|
|
|
ldap ->
|
|
|
|
{error, not_allowed}
|
|
|
|
end.
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
remove_user_internal(User) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2003-01-26 21:16:53 +01:00
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({passwd, LUser})
|
|
|
|
end,
|
2003-11-01 22:06:50 +01:00
|
|
|
mnesia:transaction(F),
|
|
|
|
catch mod_roster:remove_user(User),
|
|
|
|
catch mod_offline:remove_user(User),
|
|
|
|
catch mod_last:remove_user(User),
|
|
|
|
catch mod_vcard:remove_user(User),
|
|
|
|
catch mod_private:remove_user(User).
|
2003-02-21 20:52:15 +01:00
|
|
|
|
|
|
|
remove_user(User, Password) ->
|
2004-07-30 23:09:55 +02:00
|
|
|
case user_method() of
|
2003-11-23 21:11:21 +01:00
|
|
|
internal ->
|
|
|
|
remove_user_internal(User, Password);
|
|
|
|
ldap ->
|
|
|
|
not_allowed
|
|
|
|
end.
|
|
|
|
|
|
|
|
remove_user_internal(User, Password) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2003-02-21 20:52:15 +01:00
|
|
|
F = fun() ->
|
|
|
|
case mnesia:read({passwd, LUser}) of
|
|
|
|
[#passwd{password = Password}] ->
|
|
|
|
mnesia:delete({passwd, LUser}),
|
|
|
|
ok;
|
|
|
|
[_] ->
|
|
|
|
not_allowed;
|
|
|
|
_ ->
|
|
|
|
not_exists
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
2003-11-01 22:06:50 +01:00
|
|
|
{atomic, ok} ->
|
|
|
|
catch mod_roster:remove_user(User),
|
|
|
|
catch mod_offline:remove_user(User),
|
|
|
|
catch mod_last:remove_user(User),
|
|
|
|
catch mod_vcard:remove_user(User),
|
|
|
|
catch mod_private:remove_user(User),
|
|
|
|
ok;
|
2003-02-21 20:52:15 +01:00
|
|
|
{atomic, Res} ->
|
|
|
|
Res;
|
|
|
|
_ ->
|
|
|
|
bad_request
|
|
|
|
end.
|
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
check_password_ldap(User, Password, StreamID, Digest) ->
|
|
|
|
check_password_ldap(User, Password).
|
|
|
|
|
2004-07-30 23:09:55 +02:00
|
|
|
check_password_external(User, Password, StreamID, Digest) ->
|
|
|
|
check_password_external(User, Password).
|
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
check_password_ldap(User, Password) ->
|
|
|
|
case find_user_dn(User) of
|
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
DN ->
|
2004-07-25 23:27:56 +02:00
|
|
|
case eldap:bind("ejabberd_bind", DN, Password) of
|
2003-11-23 21:11:21 +01:00
|
|
|
ok ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
is_user_exists_ldap(User) ->
|
|
|
|
case find_user_dn(User) of
|
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
_DN ->
|
|
|
|
true
|
|
|
|
end.
|
|
|
|
|
|
|
|
find_user_dn(User) ->
|
2003-11-27 21:16:10 +01:00
|
|
|
Attr = ejabberd_config:get_local_option(ldap_uidattr),
|
|
|
|
Filter = eldap:equalityMatch(Attr, User),
|
2003-11-23 21:11:21 +01:00
|
|
|
Base = ejabberd_config:get_local_option(ldap_base),
|
|
|
|
case eldap:search("ejabberd", [{base, Base},
|
|
|
|
{filter, Filter},
|
|
|
|
{attributes, []}]) of
|
|
|
|
#eldap_search_result{entries = [E | _]} ->
|
|
|
|
E#eldap_entry.object_name;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|