2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_auth_ldap.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2004-12-12 22:00:34 +01:00
|
|
|
%%% Purpose : Authentification via LDAP
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 12 Dec 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2015 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_auth_ldap).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2006-09-14 04:54:21 +02:00
|
|
|
-behaviour(gen_server).
|
2013-03-14 10:33:02 +01:00
|
|
|
-behaviour(ejabberd_auth).
|
2006-09-14 04:54:21 +02:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_info/2, handle_call/3,
|
|
|
|
handle_cast/2, terminate/2, code_change/3]).
|
2006-09-14 04:54:21 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/1, stop/1, start_link/1, set_password/3,
|
|
|
|
check_password/3, check_password/5, try_register/3,
|
|
|
|
dirty_get_registered_users/0, get_vh_registered_users/1,
|
2015-06-01 14:38:27 +02:00
|
|
|
get_vh_registered_users/2,
|
|
|
|
get_vh_registered_users_number/1,
|
2013-03-14 10:33:02 +01:00
|
|
|
get_vh_registered_users_number/2, get_password/2,
|
|
|
|
get_password_s/2, is_user_exists/2, remove_user/2,
|
2015-06-01 14:38:27 +02:00
|
|
|
remove_user/3, store_type/0, plain_password_required/0,
|
2015-06-03 15:05:17 +02:00
|
|
|
opt_type/1]).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2005-04-27 03:08:18 +02:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2006-09-14 04:54:21 +02:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("eldap.hrl").
|
2008-04-08 11:37:06 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(state,
|
|
|
|
{host = <<"">> :: binary(),
|
|
|
|
eldap_id = <<"">> :: binary(),
|
|
|
|
bind_eldap_id = <<"">> :: binary(),
|
|
|
|
servers = [] :: [binary()],
|
|
|
|
backups = [] :: [binary()],
|
|
|
|
port = ?LDAP_PORT :: inet:port_number(),
|
|
|
|
tls_options = [] :: list(),
|
|
|
|
dn = <<"">> :: binary(),
|
|
|
|
password = <<"">> :: binary(),
|
|
|
|
base = <<"">> :: binary(),
|
|
|
|
uids = [] :: [{binary()} | {binary(), binary()}],
|
|
|
|
ufilter = <<"">> :: binary(),
|
|
|
|
sfilter = <<"">> :: binary(),
|
|
|
|
lfilter :: {any(), any()},
|
|
|
|
deref_aliases = never :: never | searching | finding | always,
|
|
|
|
dn_filter :: binary(),
|
|
|
|
dn_filter_attrs = [] :: [binary()]}).
|
|
|
|
|
|
|
|
handle_cast(_Request, State) -> {noreply, State}.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
|
|
|
|
|
|
|
handle_info(_Info, State) -> {noreply, State}.
|
|
|
|
|
|
|
|
-define(LDAP_SEARCH_TIMEOUT, 5).
|
2008-04-08 11:37:06 +02:00
|
|
|
|
2004-12-12 22:00:34 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2006-09-14 04:54:21 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
start(Host) ->
|
2006-09-14 04:54:21 +02:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
2013-03-14 10:33:02 +01:00
|
|
|
ChildSpec = {Proc, {?MODULE, start_link, [Host]},
|
|
|
|
transient, 1000, worker, [?MODULE]},
|
2006-09-14 04:54:21 +02:00
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
|
|
|
|
|
|
|
stop(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
|
|
|
gen_server:call(Proc, stop),
|
|
|
|
supervisor:terminate_child(ejabberd_sup, Proc),
|
|
|
|
supervisor:delete_child(ejabberd_sup, Proc).
|
|
|
|
|
|
|
|
start_link(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
|
|
|
gen_server:start_link({local, Proc}, ?MODULE, Host, []).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
terminate(_Reason, _State) -> ok.
|
2006-09-14 04:54:21 +02:00
|
|
|
|
|
|
|
init(Host) ->
|
|
|
|
State = parse_options(Host),
|
2007-01-27 17:40:37 +01:00
|
|
|
eldap_pool:start_link(State#state.eldap_id,
|
2013-03-14 10:33:02 +01:00
|
|
|
State#state.servers, State#state.backups,
|
|
|
|
State#state.port, State#state.dn,
|
|
|
|
State#state.password, State#state.tls_options),
|
2007-01-27 17:40:37 +01:00
|
|
|
eldap_pool:start_link(State#state.bind_eldap_id,
|
2013-03-14 10:33:02 +01:00
|
|
|
State#state.servers, State#state.backups,
|
|
|
|
State#state.port, State#state.dn,
|
|
|
|
State#state.password, State#state.tls_options),
|
2006-09-14 04:54:21 +02:00
|
|
|
{ok, State}.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
plain_password_required() -> true.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
store_type() -> external.
|
2011-08-16 00:25:03 +02:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
check_password(User, Server, Password) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
if Password == <<"">> -> false;
|
|
|
|
true ->
|
|
|
|
case catch check_password_ldap(User, Server, Password)
|
|
|
|
of
|
|
|
|
{'EXIT', _} -> false;
|
|
|
|
Result -> Result
|
|
|
|
end
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
check_password(User, Server, Password, _Digest,
|
|
|
|
_DigestGen) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
check_password(User, Server, Password).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2010-04-19 12:57:24 +02:00
|
|
|
set_password(User, Server, Password) ->
|
|
|
|
{ok, State} = eldap_utils:get_state(Server, ?MODULE),
|
|
|
|
case find_user_dn(User, State) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> {error, user_not_found};
|
|
|
|
DN ->
|
|
|
|
eldap_pool:modify_passwd(State#state.eldap_id, DN,
|
|
|
|
Password)
|
2010-04-19 12:57:24 +02:00
|
|
|
end.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2008-12-23 20:15:33 +01:00
|
|
|
%% @spec (User, Server, Password) -> {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() ->
|
2008-02-19 14:49:29 +01:00
|
|
|
Servers = ejabberd_config:get_vh_by_auth_method(ldap),
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:flatmap(fun (Server) ->
|
|
|
|
get_vh_registered_users(Server)
|
|
|
|
end,
|
|
|
|
Servers).
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2005-04-27 03:08:18 +02:00
|
|
|
get_vh_registered_users(Server) ->
|
2007-01-27 17:40:37 +01:00
|
|
|
case catch get_vh_registered_users_ldap(Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', _} -> [];
|
|
|
|
Result -> Result
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_vh_registered_users(Server, _) ->
|
|
|
|
get_vh_registered_users(Server).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2008-04-08 11:37:06 +02:00
|
|
|
get_vh_registered_users_number(Server) ->
|
|
|
|
length(get_vh_registered_users(Server)).
|
2008-04-04 20:57:01 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_vh_registered_users_number(Server, _) ->
|
|
|
|
get_vh_registered_users_number(Server).
|
|
|
|
|
|
|
|
get_password(_User, _Server) -> false.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_password_s(_User, _Server) -> <<"">>.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2009-03-04 19:34:02 +01:00
|
|
|
%% @spec (User, Server) -> true | false | {error, Error}
|
2005-07-13 05:24:13 +02:00
|
|
|
is_user_exists(User, Server) ->
|
2007-01-27 17:40:37 +01:00
|
|
|
case catch is_user_exists_ldap(User, Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', Error} -> {error, Error};
|
|
|
|
Result -> Result
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
remove_user(_User, _Server) -> {error, not_allowed}.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
remove_user(_User, _Server, _Password) -> not_allowed.
|
2004-12-12 22:00:34 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
2007-01-27 17:40:37 +01:00
|
|
|
check_password_ldap(User, Server, Password) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, State} = eldap_utils:get_state(Server, ?MODULE),
|
|
|
|
case find_user_dn(User, State) of
|
|
|
|
false -> false;
|
|
|
|
DN ->
|
|
|
|
case eldap_pool:bind(State#state.bind_eldap_id, DN,
|
|
|
|
Password)
|
|
|
|
of
|
|
|
|
ok -> true;
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end.
|
2007-01-27 17:40:37 +01:00
|
|
|
|
|
|
|
get_vh_registered_users_ldap(Server) ->
|
|
|
|
{ok, State} = eldap_utils:get_state(Server, ?MODULE),
|
2006-10-17 14:35:47 +02:00
|
|
|
UIDs = State#state.uids,
|
2006-09-14 04:54:21 +02:00
|
|
|
Eldap_ID = State#state.eldap_id,
|
|
|
|
Server = State#state.host,
|
2010-03-08 08:44:14 +01:00
|
|
|
ResAttrs = result_attrs(State),
|
2007-01-27 17:40:37 +01:00
|
|
|
case eldap_filter:parse(State#state.sfilter) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, EldapFilter} ->
|
|
|
|
case eldap_pool:search(Eldap_ID,
|
|
|
|
[{base, State#state.base},
|
|
|
|
{filter, EldapFilter},
|
|
|
|
{timeout, ?LDAP_SEARCH_TIMEOUT},
|
|
|
|
{deref_aliases, State#state.deref_aliases},
|
|
|
|
{attributes, ResAttrs}])
|
|
|
|
of
|
|
|
|
#eldap_search_result{entries = Entries} ->
|
|
|
|
lists:flatmap(fun (#eldap_entry{attributes = Attrs,
|
|
|
|
object_name = DN}) ->
|
2006-09-14 04:54:21 +02:00
|
|
|
case is_valid_dn(DN, Attrs, State) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> [];
|
|
|
|
_ ->
|
|
|
|
case
|
|
|
|
eldap_utils:find_ldap_attrs(UIDs,
|
|
|
|
Attrs)
|
|
|
|
of
|
|
|
|
<<"">> -> [];
|
|
|
|
{User, UIDFormat} ->
|
|
|
|
case
|
|
|
|
eldap_utils:get_user_part(User,
|
|
|
|
UIDFormat)
|
|
|
|
of
|
|
|
|
{ok, U} ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:nodeprep(U) of
|
2013-03-14 10:33:02 +01:00
|
|
|
error -> [];
|
|
|
|
LU ->
|
|
|
|
[{LU,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:nameprep(Server)}]
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
end
|
2006-09-14 04:54:21 +02:00
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
Entries);
|
|
|
|
_ -> []
|
|
|
|
end;
|
|
|
|
_ -> []
|
|
|
|
end.
|
2006-09-14 04:54:21 +02:00
|
|
|
|
2007-01-27 17:40:37 +01:00
|
|
|
is_user_exists_ldap(User, Server) ->
|
|
|
|
{ok, State} = eldap_utils:get_state(Server, ?MODULE),
|
|
|
|
case find_user_dn(User, State) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> false;
|
|
|
|
_DN -> true
|
|
|
|
end.
|
2007-01-27 17:40:37 +01:00
|
|
|
|
|
|
|
handle_call(get_state, _From, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, {ok, State}, State};
|
2006-09-14 04:54:21 +02:00
|
|
|
handle_call(stop, _From, State) ->
|
|
|
|
{stop, normal, ok, State};
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
{reply, bad_request, State}.
|
|
|
|
|
|
|
|
find_user_dn(User, State) ->
|
2010-03-08 08:44:14 +01:00
|
|
|
ResAttrs = result_attrs(State),
|
2013-03-14 10:33:02 +01:00
|
|
|
case eldap_filter:parse(State#state.ufilter,
|
|
|
|
[{<<"%u">>, User}])
|
|
|
|
of
|
|
|
|
{ok, Filter} ->
|
|
|
|
case eldap_pool:search(State#state.eldap_id,
|
|
|
|
[{base, State#state.base}, {filter, Filter},
|
|
|
|
{deref_aliases, State#state.deref_aliases},
|
|
|
|
{attributes, ResAttrs}])
|
|
|
|
of
|
|
|
|
#eldap_search_result{entries =
|
|
|
|
[#eldap_entry{attributes = Attrs,
|
|
|
|
object_name = DN}
|
|
|
|
| _]} ->
|
|
|
|
dn_filter(DN, Attrs, State);
|
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
_ -> false
|
2004-12-12 22:00:34 +01:00
|
|
|
end.
|
|
|
|
|
2007-02-13 11:01:58 +01:00
|
|
|
%% apply the dn filter and the local filter:
|
|
|
|
dn_filter(DN, Attrs, State) ->
|
|
|
|
case check_local_filter(Attrs, State) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> false;
|
|
|
|
true -> is_valid_dn(DN, Attrs, State)
|
2007-02-13 11:01:58 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Check that the DN is valid, based on the dn filter
|
2013-03-14 10:33:02 +01:00
|
|
|
is_valid_dn(DN, _, #state{dn_filter = undefined}) -> DN;
|
2006-09-14 04:54:21 +02:00
|
|
|
is_valid_dn(DN, Attrs, State) ->
|
|
|
|
DNAttrs = State#state.dn_filter_attrs,
|
2006-10-17 14:35:47 +02:00
|
|
|
UIDs = State#state.uids,
|
2013-03-14 10:33:02 +01:00
|
|
|
Values = [{<<"%s">>,
|
|
|
|
eldap_utils:get_ldap_attr(Attr, Attrs), 1}
|
|
|
|
|| Attr <- DNAttrs],
|
|
|
|
SubstValues = case eldap_utils:find_ldap_attrs(UIDs,
|
|
|
|
Attrs)
|
|
|
|
of
|
|
|
|
<<"">> -> Values;
|
|
|
|
{S, UAF} ->
|
|
|
|
case eldap_utils:get_user_part(S, UAF) of
|
|
|
|
{ok, U} -> [{<<"%u">>, U} | Values];
|
|
|
|
_ -> Values
|
|
|
|
end
|
|
|
|
end
|
|
|
|
++ [{<<"%d">>, State#state.host}, {<<"%D">>, DN}],
|
|
|
|
case eldap_filter:parse(State#state.dn_filter,
|
|
|
|
SubstValues)
|
|
|
|
of
|
|
|
|
{ok, EldapFilter} ->
|
|
|
|
case eldap_pool:search(State#state.eldap_id,
|
|
|
|
[{base, State#state.base},
|
|
|
|
{filter, EldapFilter},
|
|
|
|
{deref_aliases, State#state.deref_aliases},
|
|
|
|
{attributes, [<<"dn">>]}])
|
|
|
|
of
|
|
|
|
#eldap_search_result{entries = [_ | _]} -> DN;
|
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
_ -> false
|
2006-09-14 04:54:21 +02:00
|
|
|
end.
|
|
|
|
|
2007-02-13 11:01:58 +01:00
|
|
|
%% The local filter is used to check an attribute in ejabberd
|
|
|
|
%% and not in LDAP to limit the load on the LDAP directory.
|
|
|
|
%% A local rule can be either:
|
|
|
|
%% {equal, {"accountStatus",["active"]}}
|
|
|
|
%% {notequal, {"accountStatus",["disabled"]}}
|
|
|
|
%% {ldap_local_filter, {notequal, {"accountStatus",["disabled"]}}}
|
2013-03-14 10:33:02 +01:00
|
|
|
check_local_filter(_Attrs,
|
|
|
|
#state{lfilter = undefined}) ->
|
2007-02-13 11:01:58 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
check_local_filter(Attrs,
|
|
|
|
#state{lfilter = LocalFilter}) ->
|
2007-02-13 11:01:58 +01:00
|
|
|
{Operation, FilterMatch} = LocalFilter,
|
|
|
|
local_filter(Operation, Attrs, FilterMatch).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-02-13 11:01:58 +01:00
|
|
|
local_filter(equal, Attrs, FilterMatch) ->
|
|
|
|
{Attr, Value} = FilterMatch,
|
|
|
|
case lists:keysearch(Attr, 1, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> false;
|
|
|
|
{value, {Attr, Value}} -> true;
|
|
|
|
_ -> false
|
2007-02-13 11:01:58 +01:00
|
|
|
end;
|
|
|
|
local_filter(notequal, Attrs, FilterMatch) ->
|
|
|
|
not local_filter(equal, Attrs, FilterMatch).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
result_attrs(#state{uids = UIDs,
|
|
|
|
dn_filter_attrs = DNFilterAttrs}) ->
|
|
|
|
lists:foldl(fun ({UID}, Acc) -> [UID | Acc];
|
|
|
|
({UID, _}, Acc) -> [UID | Acc]
|
|
|
|
end,
|
|
|
|
DNFilterAttrs, UIDs).
|
2010-03-08 08:44:14 +01:00
|
|
|
|
2006-09-14 04:54:21 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Auxiliary functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
parse_options(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Cfg = eldap_utils:get_config(Host, []),
|
|
|
|
Eldap_ID = jlib:atom_to_binary(gen_mod:get_module_proc(Host, ?MODULE)),
|
|
|
|
Bind_Eldap_ID = jlib:atom_to_binary(
|
|
|
|
gen_mod:get_module_proc(Host, bind_ejabberd_auth_ldap)),
|
2015-05-31 15:14:57 +02:00
|
|
|
UIDsTemp = gen_mod:get_opt(
|
2013-03-14 10:33:02 +01:00
|
|
|
{ldap_uids, Host}, [],
|
|
|
|
fun(Us) ->
|
|
|
|
lists:map(
|
|
|
|
fun({U, P}) ->
|
|
|
|
{iolist_to_binary(U),
|
|
|
|
iolist_to_binary(P)};
|
|
|
|
({U}) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
{iolist_to_binary(U)};
|
|
|
|
(U) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{iolist_to_binary(U)}
|
2013-08-12 14:25:05 +02:00
|
|
|
end, lists:flatten(Us))
|
2013-03-14 10:33:02 +01:00
|
|
|
end, [{<<"uid">>, <<"%u">>}]),
|
|
|
|
UIDs = eldap_utils:uids_domain_subst(Host, UIDsTemp),
|
|
|
|
SubFilter = eldap_utils:generate_subfilter(UIDs),
|
2015-05-31 15:14:57 +02:00
|
|
|
UserFilter = case gen_mod:get_opt(
|
2013-03-14 10:33:02 +01:00
|
|
|
{ldap_filter, Host}, [],
|
|
|
|
fun check_filter/1, <<"">>) of
|
|
|
|
<<"">> ->
|
|
|
|
SubFilter;
|
|
|
|
F ->
|
|
|
|
<<"(&", SubFilter/binary, F/binary, ")">>
|
|
|
|
end,
|
|
|
|
SearchFilter = eldap_filter:do_sub(UserFilter,
|
|
|
|
[{<<"%u">>, <<"*">>}]),
|
2006-09-14 04:54:21 +02:00
|
|
|
{DNFilter, DNFilterAttrs} =
|
2015-05-31 15:14:57 +02:00
|
|
|
gen_mod:get_opt({ldap_dn_filter, Host}, [],
|
2014-11-22 01:33:23 +01:00
|
|
|
fun([{DNF, DNFA}]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
NewDNFA = case DNFA of
|
|
|
|
undefined ->
|
|
|
|
[];
|
|
|
|
_ ->
|
|
|
|
[iolist_to_binary(A)
|
|
|
|
|| A <- DNFA]
|
|
|
|
end,
|
|
|
|
NewDNF = check_filter(DNF),
|
|
|
|
{NewDNF, NewDNFA}
|
|
|
|
end, {undefined, []}),
|
2015-05-31 15:14:57 +02:00
|
|
|
LocalFilter = gen_mod:get_opt(
|
2013-03-14 10:33:02 +01:00
|
|
|
{ldap_local_filter, Host}, [], fun(V) -> V end),
|
|
|
|
#state{host = Host, eldap_id = Eldap_ID,
|
|
|
|
bind_eldap_id = Bind_Eldap_ID,
|
|
|
|
servers = Cfg#eldap_config.servers,
|
|
|
|
backups = Cfg#eldap_config.backups,
|
|
|
|
port = Cfg#eldap_config.port,
|
|
|
|
tls_options = Cfg#eldap_config.tls_options,
|
|
|
|
dn = Cfg#eldap_config.dn,
|
|
|
|
password = Cfg#eldap_config.password,
|
|
|
|
base = Cfg#eldap_config.base,
|
|
|
|
deref_aliases = Cfg#eldap_config.deref_aliases,
|
|
|
|
uids = UIDs, ufilter = UserFilter,
|
|
|
|
sfilter = SearchFilter, lfilter = LocalFilter,
|
|
|
|
dn_filter = DNFilter, dn_filter_attrs = DNFilterAttrs}.
|
|
|
|
|
|
|
|
check_filter(F) ->
|
|
|
|
NewF = iolist_to_binary(F),
|
|
|
|
{ok, _} = eldap_filter:parse(NewF),
|
|
|
|
NewF.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
opt_type(ldap_dn_filter) ->
|
|
|
|
fun ([{DNF, DNFA}]) ->
|
|
|
|
NewDNFA = case DNFA of
|
|
|
|
undefined -> [];
|
|
|
|
_ -> [iolist_to_binary(A) || A <- DNFA]
|
|
|
|
end,
|
|
|
|
NewDNF = check_filter(DNF),
|
|
|
|
{NewDNF, NewDNFA}
|
|
|
|
end;
|
|
|
|
opt_type(ldap_filter) -> fun check_filter/1;
|
|
|
|
opt_type(ldap_local_filter) -> fun (V) -> V end;
|
|
|
|
opt_type(ldap_uids) ->
|
|
|
|
fun (Us) ->
|
|
|
|
lists:map(fun ({U, P}) ->
|
|
|
|
{iolist_to_binary(U), iolist_to_binary(P)};
|
|
|
|
({U}) -> {iolist_to_binary(U)};
|
|
|
|
(U) -> {iolist_to_binary(U)}
|
|
|
|
end,
|
|
|
|
lists:flatten(Us))
|
|
|
|
end;
|
|
|
|
opt_type(_) ->
|
|
|
|
[ldap_dn_filter, ldap_filter, ldap_local_filter,
|
|
|
|
ldap_uids].
|