2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_auth_ldap.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose : Authentification via LDAP
|
|
|
|
%%% Created : 12 Dec 2004 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_auth_ldap).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
|
|
|
%% External exports
|
2005-07-13 05:24:13 +02:00
|
|
|
-export([start/1,
|
2005-04-17 20:08:34 +02:00
|
|
|
set_password/3,
|
|
|
|
check_password/3,
|
|
|
|
check_password/5,
|
|
|
|
try_register/3,
|
2004-12-12 22:00:34 +01:00
|
|
|
dirty_get_registered_users/0,
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_registered_users/1,
|
|
|
|
get_password/2,
|
|
|
|
get_password_s/2,
|
|
|
|
is_user_exists/2,
|
2004-12-12 22:00:34 +01:00
|
|
|
remove_user/2,
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user/3,
|
2004-12-12 22:00:34 +01:00
|
|
|
plain_password_required/0
|
|
|
|
]).
|
|
|
|
|
2005-04-27 03:08:18 +02:00
|
|
|
-include("ejabberd.hrl").
|
2004-12-12 22:00:34 +01:00
|
|
|
-include("eldap/eldap.hrl").
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2005-07-13 05:24:13 +02:00
|
|
|
start(Host) ->
|
|
|
|
LDAPServers = ejabberd_config:get_local_option({ldap_servers, Host}),
|
|
|
|
RootDN = ejabberd_config:get_local_option({ldap_rootdn, Host}),
|
|
|
|
Password = ejabberd_config:get_local_option({ldap_password, Host}),
|
2005-07-31 21:51:52 +02:00
|
|
|
eldap:start_link(get_eldap_id(Host, ejabberd),
|
2005-07-13 05:24:13 +02:00
|
|
|
LDAPServers, 389, RootDN, Password),
|
2005-07-31 21:51:52 +02:00
|
|
|
eldap:start_link(get_eldap_id(Host, ejabberd_bind),
|
2005-07-13 05:24:13 +02:00
|
|
|
LDAPServers, 389, RootDN, Password),
|
2006-02-20 05:07:42 +01:00
|
|
|
ejabberd_ctl:register_commands(
|
|
|
|
Host,
|
|
|
|
[{"registered-users", "list all registered users"}],
|
|
|
|
ejabberd_auth, ctl_process_get_registered),
|
2004-12-12 22:00:34 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
plain_password_required() ->
|
|
|
|
true.
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
check_password(User, Server, Password) ->
|
|
|
|
case find_user_dn(User, Server) of
|
2004-12-12 22:00:34 +01:00
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
DN ->
|
2005-07-13 05:24:13 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2005-07-31 21:51:52 +02:00
|
|
|
case eldap:bind(get_eldap_id(LServer, ejabberd_bind),
|
2005-07-13 05:24:13 +02:00
|
|
|
DN, Password) of
|
2004-12-12 22:00:34 +01:00
|
|
|
ok ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
check_password(User, Server, Password, _StreamID, _Digest) ->
|
|
|
|
check_password(User, Server, Password).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
set_password(_User, _Server, _Password) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
{error, not_allowed}.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
try_register(_User, _Server, _Password) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
{error, not_allowed}.
|
|
|
|
|
|
|
|
dirty_get_registered_users() ->
|
2005-04-27 03:08:18 +02:00
|
|
|
get_vh_registered_users(?MYNAME).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2005-04-27 03:08:18 +02:00
|
|
|
get_vh_registered_users(Server) ->
|
|
|
|
LServer = jlib:nameprep(Server),
|
2005-07-13 05:24:13 +02:00
|
|
|
Attr = ejabberd_config:get_local_option({ldap_uidattr, LServer}),
|
2005-04-27 03:08:18 +02:00
|
|
|
Filter = eldap:present(Attr),
|
2005-07-13 05:24:13 +02:00
|
|
|
Base = ejabberd_config:get_local_option({ldap_base, LServer}),
|
2005-07-31 21:51:52 +02:00
|
|
|
case eldap:search(get_eldap_id(LServer, ejabberd),
|
2005-07-13 05:24:13 +02:00
|
|
|
[{base, Base},
|
|
|
|
{filter, Filter},
|
|
|
|
{attributes, [Attr]}]) of
|
2005-04-27 03:08:18 +02:00
|
|
|
#eldap_search_result{entries = Es} ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun(E) ->
|
|
|
|
case lists:keysearch(Attr, 1, E#eldap_entry.attributes) of
|
|
|
|
{value, {_, [U]}} ->
|
|
|
|
case jlib:nodeprep(U) of
|
|
|
|
error ->
|
|
|
|
[];
|
|
|
|
LU ->
|
|
|
|
[{LU, LServer}]
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, Es);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
get_password(_User, _Server) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
false.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_password_s(_User, _Server) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
"".
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
is_user_exists(User, Server) ->
|
|
|
|
case find_user_dn(User, Server) of
|
2004-12-12 22:00:34 +01:00
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
_DN ->
|
|
|
|
true
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(_User, _Server) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
{error, not_allowed}.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(_User, _Server, _Password) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
not_allowed.
|
|
|
|
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
find_user_dn(User, Server) ->
|
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
Attr = ejabberd_config:get_local_option({ldap_uidattr, LServer}),
|
2004-12-12 22:00:34 +01:00
|
|
|
Filter = eldap:equalityMatch(Attr, User),
|
2005-07-13 05:24:13 +02:00
|
|
|
Base = ejabberd_config:get_local_option({ldap_base, LServer}),
|
2005-07-31 21:51:52 +02:00
|
|
|
case eldap:search(get_eldap_id(LServer, ejabberd),
|
2005-07-13 05:24:13 +02:00
|
|
|
[{base, Base},
|
|
|
|
{filter, Filter},
|
|
|
|
{attributes, []}]) of
|
2004-12-12 22:00:34 +01:00
|
|
|
#eldap_search_result{entries = [E | _]} ->
|
|
|
|
E#eldap_entry.object_name;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2005-07-31 21:51:52 +02:00
|
|
|
get_eldap_id(Host, Name) ->
|
|
|
|
atom_to_list(gen_mod:get_module_proc(Host, Name)).
|