2010-11-04 18:34:45 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% File : mod_shared_roster_ldap.erl
|
|
|
|
%%% Author : Realloc <realloc@realloc.spb.ru>
|
|
|
|
%%% Marcin Owsiany <marcin@owsiany.pl>
|
|
|
|
%%% Evgeniy Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Description : LDAP shared roster management
|
|
|
|
%%% Created : 5 Mar 2005 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
2010-11-04 18:34:45 +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.
|
|
|
|
%%%
|
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.
|
2010-11-04 18:34:45 +01:00
|
|
|
%%%
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(mod_shared_roster_ldap).
|
|
|
|
|
2016-03-25 17:44:12 +01:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2010-11-04 18:34:45 +01:00
|
|
|
-behaviour(gen_server).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-11-04 18:34:45 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
|
|
|
%% API
|
2017-04-21 09:43:14 +02:00
|
|
|
-export([start/2, stop/1, reload/3]).
|
2010-11-04 18:34:45 +01:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
|
|
|
handle_info/2, terminate/2, code_change/3]).
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-export([get_user_roster/2, c2s_session_opened/1,
|
2013-03-14 10:33:02 +01:00
|
|
|
get_jid_info/4, process_item/2, in_subscription/6,
|
2017-04-21 09:43:14 +02:00
|
|
|
out_subscription/4, mod_opt_type/1, opt_type/1, depends/2,
|
|
|
|
transform_module_options/1]).
|
2010-11-04 18:34:45 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2016-03-25 17:44:12 +01:00
|
|
|
-include("logger.hrl").
|
2016-07-30 07:37:34 +02:00
|
|
|
-include("xmpp.hrl").
|
2010-11-04 18:34:45 +01:00
|
|
|
-include("mod_roster.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("eldap.hrl").
|
2010-11-04 18:34:45 +01:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-define(SETS, gb_sets).
|
2017-04-21 09:43:14 +02:00
|
|
|
-define(USER_CACHE, shared_roster_ldap_user_cache).
|
|
|
|
-define(GROUP_CACHE, shared_roster_ldap_group_cache).
|
2016-03-25 17:44:12 +01:00
|
|
|
-define(LDAP_SEARCH_TIMEOUT, 5). %% Timeout for LDAP search queries in seconds
|
2016-02-26 15:27:12 +01:00
|
|
|
-define(INVALID_SETTING_MSG, "~s is not properly set! ~s will not function.").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-record(state,
|
|
|
|
{host = <<"">> :: binary(),
|
|
|
|
eldap_id = <<"">> :: binary(),
|
|
|
|
servers = [] :: [binary()],
|
|
|
|
backups = [] :: [binary()],
|
|
|
|
port = ?LDAP_PORT :: inet:port_number(),
|
|
|
|
tls_options = [] :: list(),
|
|
|
|
dn = <<"">> :: binary(),
|
|
|
|
base = <<"">> :: binary(),
|
|
|
|
password = <<"">> :: binary(),
|
|
|
|
uid = <<"">> :: binary(),
|
|
|
|
deref_aliases = never :: never | searching |
|
|
|
|
finding | always,
|
|
|
|
group_attr = <<"">> :: binary(),
|
|
|
|
group_desc = <<"">> :: binary(),
|
|
|
|
user_desc = <<"">> :: binary(),
|
2016-03-25 17:44:12 +01:00
|
|
|
user_uid = <<"">> :: binary(),
|
2013-03-14 10:33:02 +01:00
|
|
|
uid_format = <<"">> :: binary(),
|
|
|
|
uid_format_re = <<"">> :: binary(),
|
|
|
|
filter = <<"">> :: binary(),
|
|
|
|
ufilter = <<"">> :: binary(),
|
|
|
|
rfilter = <<"">> :: binary(),
|
|
|
|
gfilter = <<"">> :: binary(),
|
2017-04-21 09:43:14 +02:00
|
|
|
auth_check = true :: boolean()}).
|
2016-02-26 15:27:12 +01:00
|
|
|
|
2016-03-25 17:44:12 +01:00
|
|
|
-record(group_info, {desc, members}).
|
2016-02-26 15:27:12 +01:00
|
|
|
|
2010-11-04 18:34:45 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
start(Host, Opts) ->
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:start_child(?MODULE, Host, Opts).
|
2010-11-04 18:34:45 +01:00
|
|
|
|
|
|
|
stop(Host) ->
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:stop_child(?MODULE, Host).
|
2010-11-04 18:34:45 +01:00
|
|
|
|
2017-04-21 09:43:14 +02:00
|
|
|
reload(Host, NewOpts, _OldOpts) ->
|
|
|
|
case init_cache(Host, NewOpts) of
|
|
|
|
true ->
|
|
|
|
ets_cache:setopts(?USER_CACHE, cache_opts(Host, NewOpts)),
|
|
|
|
ets_cache:setopts(?GROUP_CACHE, cache_opts(Host, NewOpts));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
|
|
|
gen_server:cast(Proc, {set_state, parse_options(Host, NewOpts)}).
|
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[{mod_roster, hard}].
|
|
|
|
|
2010-11-04 18:34:45 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Hooks
|
|
|
|
%%--------------------------------------------------------------------
|
2016-08-12 12:17:42 +02:00
|
|
|
-spec get_user_roster([#roster{}], {binary(), binary()}) -> [#roster{}].
|
2010-11-04 18:34:45 +01:00
|
|
|
get_user_roster(Items, {U, S} = US) ->
|
2016-03-25 17:44:12 +01:00
|
|
|
SRUsers = get_user_to_groups_map(US, true),
|
|
|
|
{NewItems1, SRUsersRest} = lists:mapfoldl(fun (Item,
|
|
|
|
SRUsers1) ->
|
|
|
|
{_, _, {U1, S1, _}} =
|
|
|
|
Item#roster.usj,
|
|
|
|
US1 = {U1, S1},
|
|
|
|
case dict:find(US1,
|
|
|
|
SRUsers1)
|
|
|
|
of
|
2017-03-09 19:21:45 +01:00
|
|
|
{ok, GroupNames} ->
|
2016-03-25 17:44:12 +01:00
|
|
|
{Item#roster{subscription
|
|
|
|
=
|
|
|
|
both,
|
2017-03-09 19:21:45 +01:00
|
|
|
groups =
|
|
|
|
Item#roster.groups ++ GroupNames,
|
2016-03-25 17:44:12 +01:00
|
|
|
ask =
|
|
|
|
none},
|
|
|
|
dict:erase(US1,
|
|
|
|
SRUsers1)};
|
|
|
|
error ->
|
|
|
|
{Item, SRUsers1}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
SRUsers, Items),
|
2013-03-14 10:33:02 +01:00
|
|
|
SRItems = [#roster{usj = {U, S, {U1, S1, <<"">>}},
|
2016-03-25 17:44:12 +01:00
|
|
|
us = US, jid = {U1, S1, <<"">>},
|
|
|
|
name = get_user_name(U1, S1), subscription = both,
|
|
|
|
ask = none, groups = GroupNames}
|
|
|
|
|| {{U1, S1}, GroupNames} <- dict:to_list(SRUsersRest)],
|
2010-11-04 18:34:45 +01:00
|
|
|
SRItems ++ NewItems1.
|
|
|
|
|
|
|
|
%% This function in use to rewrite the roster entries when moving or renaming
|
|
|
|
%% them in the user contact list.
|
2016-08-12 12:17:42 +02:00
|
|
|
-spec process_item(#roster{}, binary()) -> #roster{}.
|
2010-11-04 18:34:45 +01:00
|
|
|
process_item(RosterItem, _Host) ->
|
2016-03-25 17:44:12 +01:00
|
|
|
USFrom = RosterItem#roster.us,
|
|
|
|
{User, Server, _Resource} = RosterItem#roster.jid,
|
|
|
|
USTo = {User, Server},
|
|
|
|
Map = get_user_to_groups_map(USFrom, false),
|
|
|
|
case dict:find(USTo, Map) of
|
|
|
|
error -> RosterItem;
|
|
|
|
{ok, []} -> RosterItem;
|
|
|
|
{ok, GroupNames}
|
|
|
|
when RosterItem#roster.subscription == remove ->
|
|
|
|
RosterItem#roster{subscription = both, ask = none,
|
|
|
|
groups = GroupNames};
|
|
|
|
_ -> RosterItem#roster{subscription = both, ask = none}
|
2010-11-04 18:34:45 +01:00
|
|
|
end.
|
|
|
|
|
2017-01-23 11:51:05 +01:00
|
|
|
c2s_session_opened(#{jid := #jid{luser = LUser, lserver = LServer},
|
2017-01-09 15:02:17 +01:00
|
|
|
pres_f := PresF, pres_t := PresT} = State) ->
|
2016-03-25 17:44:12 +01:00
|
|
|
US = {LUser, LServer},
|
|
|
|
DisplayedGroups = get_user_displayed_groups(US),
|
2017-01-09 15:02:17 +01:00
|
|
|
SRUsers = lists:flatmap(fun(Group) ->
|
2016-03-25 17:44:12 +01:00
|
|
|
get_group_users(LServer, Group)
|
|
|
|
end,
|
2017-01-09 15:02:17 +01:00
|
|
|
DisplayedGroups),
|
|
|
|
PresBoth = lists:foldl(
|
2017-01-23 11:51:05 +01:00
|
|
|
fun({U, S, _}, Acc) ->
|
|
|
|
?SETS:add_element({U, S, <<"">>}, Acc);
|
|
|
|
({U, S}, Acc) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
?SETS:add_element({U, S, <<"">>}, Acc)
|
2017-01-23 11:51:05 +01:00
|
|
|
end, ?SETS:new(), SRUsers),
|
2017-01-09 15:02:17 +01:00
|
|
|
State#{pres_f => ?SETS:union(PresBoth, PresF),
|
|
|
|
pres_t => ?SETS:union(PresBoth, PresT)}.
|
2010-11-04 18:34:45 +01:00
|
|
|
|
2016-08-12 12:17:42 +02:00
|
|
|
-spec get_jid_info({subscription(), [binary()]}, binary(), binary(), jid())
|
|
|
|
-> {subscription(), [binary()]}.
|
2016-03-25 17:44:12 +01:00
|
|
|
get_jid_info({Subscription, Groups}, User, Server,
|
|
|
|
JID) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
2016-02-29 14:35:45 +01:00
|
|
|
{U1, S1, _} = jid:tolower(JID),
|
2010-11-04 18:34:45 +01:00
|
|
|
US1 = {U1, S1},
|
2016-03-25 17:44:12 +01:00
|
|
|
SRUsers = get_user_to_groups_map(US, false),
|
|
|
|
case dict:find(US1, SRUsers) of
|
|
|
|
{ok, GroupNames} ->
|
|
|
|
NewGroups = if Groups == [] -> GroupNames;
|
|
|
|
true -> Groups
|
|
|
|
end,
|
|
|
|
{both, NewGroups};
|
|
|
|
error -> {Subscription, Groups}
|
2010-11-04 18:34:45 +01:00
|
|
|
end.
|
|
|
|
|
2016-08-12 12:17:42 +02:00
|
|
|
-spec in_subscription(boolean(), binary(), binary(), jid(),
|
|
|
|
subscribe | subscribed | unsubscribe | unsubscribed,
|
|
|
|
binary()) -> boolean().
|
2016-03-25 17:44:12 +01:00
|
|
|
in_subscription(Acc, User, Server, JID, Type,
|
|
|
|
_Reason) ->
|
2010-11-04 18:34:45 +01:00
|
|
|
process_subscription(in, User, Server, JID, Type, Acc).
|
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec out_subscription(
|
|
|
|
binary(), binary(), jid(),
|
|
|
|
subscribed | unsubscribed | subscribe | unsubscribe) -> boolean().
|
2010-11-04 18:34:45 +01:00
|
|
|
out_subscription(User, Server, JID, Type) ->
|
2016-03-25 17:44:12 +01:00
|
|
|
process_subscription(out, User, Server, JID, Type,
|
|
|
|
false).
|
|
|
|
|
|
|
|
process_subscription(Direction, User, Server, JID,
|
|
|
|
_Type, Acc) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
{U1, S1, _} =
|
|
|
|
jid:tolower(jid:remove_resource(JID)),
|
2010-11-04 18:34:45 +01:00
|
|
|
US1 = {U1, S1},
|
2016-03-25 17:44:12 +01:00
|
|
|
DisplayedGroups = get_user_displayed_groups(US),
|
|
|
|
SRUsers = lists:usort(lists:flatmap(fun (Group) ->
|
|
|
|
get_group_users(LServer, Group)
|
|
|
|
end,
|
|
|
|
DisplayedGroups)),
|
|
|
|
case lists:member(US1, SRUsers) of
|
|
|
|
true ->
|
|
|
|
case Direction of
|
|
|
|
in -> {stop, false};
|
|
|
|
out -> stop
|
|
|
|
end;
|
|
|
|
false -> Acc
|
2010-11-04 18:34:45 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
init([Host, Opts]) ->
|
2017-02-14 08:25:08 +01:00
|
|
|
process_flag(trap_exit, true),
|
2010-11-04 18:34:45 +01:00
|
|
|
State = parse_options(Host, Opts),
|
2017-04-21 09:43:14 +02:00
|
|
|
init_cache(Host, Opts),
|
2016-03-25 17:44:12 +01:00
|
|
|
ejabberd_hooks:add(roster_get, Host, ?MODULE,
|
|
|
|
get_user_roster, 70),
|
2010-11-04 18:34:45 +01:00
|
|
|
ejabberd_hooks:add(roster_in_subscription, Host,
|
2016-03-25 17:44:12 +01:00
|
|
|
?MODULE, in_subscription, 30),
|
2010-11-04 18:34:45 +01:00
|
|
|
ejabberd_hooks:add(roster_out_subscription, Host,
|
2016-03-25 17:44:12 +01:00
|
|
|
?MODULE, out_subscription, 30),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(c2s_session_opened, Host,
|
|
|
|
?MODULE, c2s_session_opened, 70),
|
2016-03-25 17:44:12 +01:00
|
|
|
ejabberd_hooks:add(roster_get_jid_info, Host, ?MODULE,
|
|
|
|
get_jid_info, 70),
|
|
|
|
ejabberd_hooks:add(roster_process_item, Host, ?MODULE,
|
|
|
|
process_item, 50),
|
2010-11-04 18:34:45 +01:00
|
|
|
eldap_pool:start_link(State#state.eldap_id,
|
2016-03-25 17:44:12 +01:00
|
|
|
State#state.servers, State#state.backups,
|
|
|
|
State#state.port, State#state.dn,
|
|
|
|
State#state.password, State#state.tls_options),
|
2010-11-04 18:34:45 +01:00
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
handle_call(get_state, _From, State) ->
|
|
|
|
{reply, {ok, State}, State};
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
{reply, {error, badarg}, State}.
|
|
|
|
|
2017-04-21 09:43:14 +02:00
|
|
|
handle_cast({set_state, NewState}, _State) ->
|
|
|
|
{noreply, NewState};
|
2016-03-25 17:44:12 +01:00
|
|
|
handle_cast(_Msg, State) -> {noreply, State}.
|
2010-11-04 18:34:45 +01:00
|
|
|
|
2016-03-25 17:44:12 +01:00
|
|
|
handle_info(_Info, State) -> {noreply, State}.
|
2010-11-04 18:34:45 +01:00
|
|
|
|
|
|
|
terminate(_Reason, State) ->
|
|
|
|
Host = State#state.host,
|
2016-03-25 17:44:12 +01:00
|
|
|
ejabberd_hooks:delete(roster_get, Host, ?MODULE,
|
|
|
|
get_user_roster, 70),
|
2010-11-04 18:34:45 +01:00
|
|
|
ejabberd_hooks:delete(roster_in_subscription, Host,
|
2016-03-25 17:44:12 +01:00
|
|
|
?MODULE, in_subscription, 30),
|
2010-11-04 18:34:45 +01:00
|
|
|
ejabberd_hooks:delete(roster_out_subscription, Host,
|
2016-03-25 17:44:12 +01:00
|
|
|
?MODULE, out_subscription, 30),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(c2s_session_opened,
|
|
|
|
Host, ?MODULE, c2s_session_opened, 70),
|
2010-11-04 18:34:45 +01:00
|
|
|
ejabberd_hooks:delete(roster_get_jid_info, Host,
|
2016-03-25 17:44:12 +01:00
|
|
|
?MODULE, get_jid_info, 70),
|
2010-11-04 18:34:45 +01:00
|
|
|
ejabberd_hooks:delete(roster_process_item, Host,
|
2016-03-25 17:44:12 +01:00
|
|
|
?MODULE, process_item, 50).
|
2010-11-04 18:34:45 +01:00
|
|
|
|
2016-03-25 17:44:12 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2010-11-04 18:34:45 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2015-10-07 00:06:58 +02:00
|
|
|
|
2016-03-25 17:44:12 +01:00
|
|
|
get_user_to_groups_map({_, Server} = US, SkipUS) ->
|
|
|
|
DisplayedGroups = get_user_displayed_groups(US),
|
|
|
|
lists:foldl(fun (Group, Dict1) ->
|
|
|
|
GroupName = get_group_name(Server, Group),
|
|
|
|
lists:foldl(fun (Contact, Dict) ->
|
|
|
|
if SkipUS, Contact == US -> Dict;
|
|
|
|
true ->
|
|
|
|
dict:append(Contact,
|
|
|
|
GroupName, Dict)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Dict1, get_group_users(Server, Group))
|
|
|
|
end,
|
|
|
|
dict:new(), DisplayedGroups).
|
|
|
|
|
|
|
|
eldap_search(State, FilterParseArgs, AttributesList) ->
|
|
|
|
case apply(eldap_filter, parse, FilterParseArgs) of
|
|
|
|
{ok, EldapFilter} ->
|
|
|
|
case eldap_pool:search(State#state.eldap_id,
|
|
|
|
[{base, State#state.base},
|
|
|
|
{filter, EldapFilter},
|
|
|
|
{timeout, ?LDAP_SEARCH_TIMEOUT},
|
|
|
|
{deref_aliases, State#state.deref_aliases},
|
|
|
|
{attributes, AttributesList}])
|
|
|
|
of
|
|
|
|
#eldap_search_result{entries = Es} ->
|
|
|
|
%% A result with entries. Return their list.
|
|
|
|
Es;
|
|
|
|
_ ->
|
|
|
|
%% Something else. Pretend we got no results.
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
%% Filter parsing failed. Pretend we got no results.
|
|
|
|
[]
|
2010-11-04 18:34:45 +01:00
|
|
|
end.
|
|
|
|
|
2016-03-25 17:44:12 +01:00
|
|
|
get_user_displayed_groups({User, Host}) ->
|
|
|
|
{ok, State} = eldap_utils:get_state(Host, ?MODULE),
|
|
|
|
GroupAttr = State#state.group_attr,
|
|
|
|
Entries = eldap_search(State,
|
|
|
|
[eldap_filter:do_sub(State#state.rfilter,
|
|
|
|
[{<<"%u">>, User}])],
|
|
|
|
[GroupAttr]),
|
|
|
|
Reply = lists:flatmap(fun (#eldap_entry{attributes =
|
|
|
|
Attrs}) ->
|
|
|
|
case Attrs of
|
|
|
|
[{GroupAttr, ValuesList}] -> ValuesList;
|
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Entries),
|
|
|
|
lists:usort(Reply).
|
|
|
|
|
|
|
|
get_group_users(Host, Group) ->
|
|
|
|
{ok, State} = eldap_utils:get_state(Host, ?MODULE),
|
2017-04-21 09:43:14 +02:00
|
|
|
case ets_cache:lookup(?GROUP_CACHE,
|
|
|
|
{Group, Host},
|
|
|
|
fun () -> search_group_info(State, Group) end) of
|
2016-03-25 17:44:12 +01:00
|
|
|
{ok, #group_info{members = Members}}
|
|
|
|
when Members /= undefined ->
|
|
|
|
Members;
|
|
|
|
_ -> []
|
2010-11-04 18:34:45 +01:00
|
|
|
end.
|
|
|
|
|
2016-03-25 17:44:12 +01:00
|
|
|
get_group_name(Host, Group) ->
|
|
|
|
{ok, State} = eldap_utils:get_state(Host, ?MODULE),
|
2017-04-21 09:43:14 +02:00
|
|
|
case ets_cache:lookup(?GROUP_CACHE,
|
|
|
|
{Group, Host},
|
|
|
|
fun () -> search_group_info(State, Group) end)
|
2016-03-25 17:44:12 +01:00
|
|
|
of
|
|
|
|
{ok, #group_info{desc = GroupName}}
|
|
|
|
when GroupName /= undefined ->
|
|
|
|
GroupName;
|
|
|
|
_ -> Group
|
2010-11-04 18:34:45 +01:00
|
|
|
end.
|
|
|
|
|
2016-03-25 17:44:12 +01:00
|
|
|
get_user_name(User, Host) ->
|
|
|
|
{ok, State} = eldap_utils:get_state(Host, ?MODULE),
|
2017-04-21 09:43:14 +02:00
|
|
|
case ets_cache:lookup(?USER_CACHE,
|
|
|
|
{User, Host},
|
|
|
|
fun () -> search_user_name(State, User) end)
|
2016-03-25 17:44:12 +01:00
|
|
|
of
|
|
|
|
{ok, UserName} -> UserName;
|
|
|
|
error -> User
|
2010-11-04 18:34:45 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
search_group_info(State, Group) ->
|
2016-03-25 17:44:12 +01:00
|
|
|
Extractor = case State#state.uid_format_re of
|
|
|
|
<<"">> ->
|
|
|
|
fun (UID) ->
|
|
|
|
catch eldap_utils:get_user_part(UID,
|
|
|
|
State#state.uid_format)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
fun (UID) ->
|
|
|
|
catch get_user_part_re(UID,
|
|
|
|
State#state.uid_format_re)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
AuthChecker = case State#state.auth_check of
|
|
|
|
true -> fun ejabberd_auth:is_user_exists/2;
|
|
|
|
_ -> fun (_U, _S) -> true end
|
|
|
|
end,
|
|
|
|
case eldap_search(State,
|
|
|
|
[eldap_filter:do_sub(State#state.gfilter,
|
|
|
|
[{<<"%g">>, Group}])],
|
|
|
|
[State#state.group_attr, State#state.group_desc,
|
|
|
|
State#state.uid])
|
|
|
|
of
|
2016-02-26 15:27:12 +01:00
|
|
|
[] ->
|
|
|
|
error;
|
2016-03-25 17:44:12 +01:00
|
|
|
LDAPEntries ->
|
|
|
|
{GroupDesc, MembersLists} = lists:foldl(fun(Entry, Acc) ->
|
|
|
|
extract_members(State, Extractor, AuthChecker, Entry, Acc)
|
|
|
|
end,
|
|
|
|
{Group, []}, LDAPEntries),
|
|
|
|
{ok, #group_info{desc = GroupDesc, members = lists:usort(lists:flatten(MembersLists))}}
|
2016-02-26 15:27:12 +01:00
|
|
|
end.
|
|
|
|
|
2016-03-25 17:44:12 +01:00
|
|
|
extract_members(State, Extractor, AuthChecker, #eldap_entry{attributes = Attrs}, {DescAcc, JIDsAcc}) ->
|
2010-11-04 18:34:45 +01:00
|
|
|
Host = State#state.host,
|
2016-03-25 17:44:12 +01:00
|
|
|
case {eldap_utils:get_ldap_attr(State#state.group_attr, Attrs),
|
|
|
|
eldap_utils:get_ldap_attr(State#state.group_desc, Attrs),
|
|
|
|
lists:keysearch(State#state.uid, 1, Attrs)} of
|
|
|
|
{ID, Desc, {value, {GroupMemberAttr, Members}}} when ID /= <<"">>,
|
|
|
|
GroupMemberAttr == State#state.uid ->
|
|
|
|
JIDs = lists:foldl(fun({ok, UID}, L) ->
|
|
|
|
PUID = jid:nodeprep(UID),
|
|
|
|
case PUID of
|
|
|
|
error ->
|
|
|
|
L;
|
|
|
|
_ ->
|
|
|
|
case AuthChecker(PUID, Host) of
|
|
|
|
true ->
|
|
|
|
[{PUID, Host} | L];
|
|
|
|
_ ->
|
|
|
|
L
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
(_, L) -> L
|
|
|
|
end,
|
|
|
|
[],
|
|
|
|
lists:map(Extractor, Members)),
|
|
|
|
{Desc, [JIDs | JIDsAcc]};
|
2016-02-26 15:27:12 +01:00
|
|
|
_ ->
|
2016-03-25 17:44:12 +01:00
|
|
|
{DescAcc, JIDsAcc}
|
2010-11-04 18:34:45 +01:00
|
|
|
end.
|
|
|
|
|
2016-03-25 17:44:12 +01:00
|
|
|
search_user_name(State, User) ->
|
|
|
|
case eldap_search(State,
|
|
|
|
[eldap_filter:do_sub(State#state.ufilter,
|
|
|
|
[{<<"%u">>, User}])],
|
|
|
|
[State#state.user_desc, State#state.user_uid])
|
|
|
|
of
|
|
|
|
[#eldap_entry{attributes = Attrs} | _] ->
|
|
|
|
case {eldap_utils:get_ldap_attr(State#state.user_uid,
|
|
|
|
Attrs),
|
|
|
|
eldap_utils:get_ldap_attr(State#state.user_desc, Attrs)}
|
|
|
|
of
|
|
|
|
{UID, Desc} when UID /= <<"">> -> {ok, Desc};
|
|
|
|
_ -> error
|
|
|
|
end;
|
|
|
|
[] -> error
|
2010-11-04 18:34:45 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Getting User ID part by regex pattern
|
|
|
|
get_user_part_re(String, Pattern) ->
|
|
|
|
case catch re:run(String, Pattern) of
|
2016-03-25 17:44:12 +01:00
|
|
|
{match, Captured} ->
|
|
|
|
{First, Len} = lists:nth(2, Captured),
|
|
|
|
Result = str:sub_string(String, First + 1, First + Len),
|
|
|
|
{ok, Result};
|
|
|
|
_ -> {error, badmatch}
|
2010-11-04 18:34:45 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
parse_options(Host, Opts) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
Eldap_ID = misc:atom_to_binary(gen_mod:get_module_proc(Host, ?MODULE)),
|
2013-03-14 10:33:02 +01:00
|
|
|
Cfg = eldap_utils:get_config(Host, Opts),
|
|
|
|
GroupAttr = gen_mod:get_opt(ldap_groupattr, Opts,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
<<"cn">>),
|
|
|
|
GroupDesc = gen_mod:get_opt(ldap_groupdesc, Opts,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
GroupAttr),
|
|
|
|
UserDesc = gen_mod:get_opt(ldap_userdesc, Opts,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
<<"cn">>),
|
|
|
|
UserUID = gen_mod:get_opt(ldap_useruid, Opts,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
<<"cn">>),
|
|
|
|
UIDAttr = gen_mod:get_opt(ldap_memberattr, Opts,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
<<"memberUid">>),
|
|
|
|
UIDAttrFormat = gen_mod:get_opt(ldap_memberattr_format, Opts,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
<<"%u">>),
|
|
|
|
UIDAttrFormatRe = gen_mod:get_opt(ldap_memberattr_format_re, Opts,
|
|
|
|
fun(S) ->
|
|
|
|
Re = iolist_to_binary(S),
|
|
|
|
{ok, MP} = re:compile(Re),
|
|
|
|
MP
|
|
|
|
end, <<"">>),
|
|
|
|
AuthCheck = gen_mod:get_opt(ldap_auth_check, Opts,
|
|
|
|
fun(on) -> true;
|
|
|
|
(off) -> false;
|
|
|
|
(false) -> false;
|
|
|
|
(true) -> true
|
|
|
|
end, true),
|
2016-02-29 14:35:45 +01:00
|
|
|
ConfigFilter = gen_mod:get_opt({ldap_filter, Host}, Opts,
|
2017-04-28 12:23:32 +02:00
|
|
|
fun eldap_utils:check_filter/1, <<"">>),
|
2016-02-29 14:35:45 +01:00
|
|
|
ConfigUserFilter = gen_mod:get_opt({ldap_ufilter, Host}, Opts,
|
2017-04-28 12:23:32 +02:00
|
|
|
fun eldap_utils:check_filter/1, <<"">>),
|
2016-02-29 14:35:45 +01:00
|
|
|
ConfigGroupFilter = gen_mod:get_opt({ldap_gfilter, Host}, Opts,
|
2017-04-28 12:23:32 +02:00
|
|
|
fun eldap_utils:check_filter/1, <<"">>),
|
2016-02-29 14:35:45 +01:00
|
|
|
RosterFilter = gen_mod:get_opt({ldap_rfilter, Host}, Opts,
|
2017-04-28 12:23:32 +02:00
|
|
|
fun eldap_utils:check_filter/1, <<"">>),
|
2013-03-14 10:33:02 +01:00
|
|
|
SubFilter = <<"(&(", UIDAttr/binary, "=",
|
2016-03-25 17:44:12 +01:00
|
|
|
UIDAttrFormat/binary, ")(", GroupAttr/binary, "=%g))">>,
|
2010-11-04 18:34:45 +01:00
|
|
|
UserSubFilter = case ConfigUserFilter of
|
2016-03-25 17:44:12 +01:00
|
|
|
<<"">> ->
|
|
|
|
eldap_filter:do_sub(SubFilter, [{<<"%g">>, <<"*">>}]);
|
|
|
|
UString -> UString
|
|
|
|
end,
|
2010-11-04 18:34:45 +01:00
|
|
|
GroupSubFilter = case ConfigGroupFilter of
|
2016-03-25 17:44:12 +01:00
|
|
|
<<"">> ->
|
|
|
|
eldap_filter:do_sub(SubFilter,
|
|
|
|
[{<<"%u">>, <<"*">>}]);
|
|
|
|
GString -> GString
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2010-11-04 18:34:45 +01:00
|
|
|
Filter = case ConfigFilter of
|
2016-03-25 17:44:12 +01:00
|
|
|
<<"">> -> SubFilter;
|
|
|
|
_ ->
|
|
|
|
<<"(&", SubFilter/binary, ConfigFilter/binary, ")">>
|
|
|
|
end,
|
2010-11-04 18:34:45 +01:00
|
|
|
UserFilter = case ConfigFilter of
|
2016-03-25 17:44:12 +01:00
|
|
|
<<"">> -> UserSubFilter;
|
|
|
|
_ ->
|
|
|
|
<<"(&", UserSubFilter/binary, ConfigFilter/binary, ")">>
|
|
|
|
end,
|
2010-11-04 18:34:45 +01:00
|
|
|
GroupFilter = case ConfigFilter of
|
2016-03-25 17:44:12 +01:00
|
|
|
<<"">> -> GroupSubFilter;
|
|
|
|
_ ->
|
|
|
|
<<"(&", GroupSubFilter/binary, ConfigFilter/binary,
|
|
|
|
")">>
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
#state{host = Host, eldap_id = Eldap_ID,
|
2016-03-25 17:44:12 +01:00
|
|
|
servers = Cfg#eldap_config.servers,
|
|
|
|
backups = Cfg#eldap_config.backups,
|
2013-03-14 10:33:02 +01:00
|
|
|
port = Cfg#eldap_config.port,
|
2016-03-25 17:44:12 +01:00
|
|
|
tls_options = Cfg#eldap_config.tls_options,
|
|
|
|
dn = Cfg#eldap_config.dn,
|
2013-03-14 10:33:02 +01:00
|
|
|
password = Cfg#eldap_config.password,
|
|
|
|
base = Cfg#eldap_config.base,
|
|
|
|
deref_aliases = Cfg#eldap_config.deref_aliases,
|
2016-03-25 17:44:12 +01:00
|
|
|
uid = UIDAttr,
|
|
|
|
group_attr = GroupAttr, group_desc = GroupDesc,
|
|
|
|
user_desc = UserDesc, user_uid = UserUID,
|
|
|
|
uid_format = UIDAttrFormat,
|
|
|
|
uid_format_re = UIDAttrFormatRe, filter = Filter,
|
|
|
|
ufilter = UserFilter, rfilter = RosterFilter,
|
2017-04-21 09:43:14 +02:00
|
|
|
gfilter = GroupFilter, auth_check = AuthCheck}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-04-21 09:43:14 +02:00
|
|
|
init_cache(Host, Opts) ->
|
|
|
|
UseCache = use_cache(Host, Opts),
|
|
|
|
case UseCache of
|
|
|
|
true ->
|
|
|
|
CacheOpts = cache_opts(Host, Opts),
|
|
|
|
ets_cache:new(?USER_CACHE, CacheOpts),
|
|
|
|
ets_cache:new(?GROUP_CACHE, CacheOpts);
|
|
|
|
false ->
|
|
|
|
ets_cache:delete(?USER_CACHE),
|
|
|
|
ets_cache:delete(?GROUP_CACHE)
|
|
|
|
end,
|
|
|
|
UseCache.
|
|
|
|
|
|
|
|
use_cache(Host, Opts) ->
|
|
|
|
gen_mod:get_opt(use_cache, Opts, mod_opt_type(use_cache),
|
|
|
|
ejabberd_config:use_cache(Host)).
|
|
|
|
|
|
|
|
cache_opts(Host, Opts) ->
|
|
|
|
MaxSize = gen_mod:get_opt(cache_size, Opts,
|
|
|
|
mod_opt_type(cache_size),
|
|
|
|
ejabberd_config:cache_size(Host)),
|
|
|
|
CacheMissed = gen_mod:get_opt(cache_missed, Opts,
|
|
|
|
mod_opt_type(cache_missed),
|
|
|
|
ejabberd_config:cache_missed(Host)),
|
|
|
|
LifeTime = case gen_mod:get_opt(cache_life_time, Opts,
|
|
|
|
mod_opt_type(cache_life_time),
|
|
|
|
ejabberd_config:cache_life_time(Host)) of
|
|
|
|
infinity -> infinity;
|
|
|
|
I -> timer:seconds(I)
|
|
|
|
end,
|
|
|
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
|
|
|
|
|
|
|
transform_module_options(Opts) ->
|
|
|
|
lists:map(
|
|
|
|
fun({ldap_group_cache_size, I}) ->
|
|
|
|
?WARNING_MSG("Option 'ldap_group_cache_size' is deprecated, "
|
|
|
|
"use 'cache_size' instead", []),
|
|
|
|
{cache_size, I};
|
|
|
|
({ldap_user_cache_size, I}) ->
|
|
|
|
?WARNING_MSG("Option 'ldap_user_cache_size' is deprecated, "
|
|
|
|
"use 'cache_size' instead", []),
|
|
|
|
{cache_size, I};
|
|
|
|
({ldap_group_cache_validity, Secs}) ->
|
|
|
|
?WARNING_MSG("Option 'ldap_group_cache_validity' is deprecated, "
|
|
|
|
"use 'cache_life_time' instead", []),
|
|
|
|
{cache_life_time, Secs};
|
|
|
|
({ldap_user_cache_validity, Secs}) ->
|
|
|
|
?WARNING_MSG("Option 'ldap_user_cache_validity' is deprecated, "
|
|
|
|
"use 'cache_life_time' instead", []),
|
|
|
|
{cache_life_time, Secs};
|
|
|
|
(Opt) ->
|
|
|
|
Opt
|
|
|
|
end, Opts).
|
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(deref_aliases) ->
|
|
|
|
fun (never) -> never;
|
|
|
|
(searching) -> searching;
|
|
|
|
(finding) -> finding;
|
|
|
|
(always) -> always
|
|
|
|
end;
|
|
|
|
mod_opt_type(ldap_backups) ->
|
|
|
|
fun (L) -> [iolist_to_binary(H) || H <- L] end;
|
|
|
|
mod_opt_type(ldap_base) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(ldap_deref_aliases) ->
|
|
|
|
fun (never) -> never;
|
|
|
|
(searching) -> searching;
|
|
|
|
(finding) -> finding;
|
|
|
|
(always) -> always
|
|
|
|
end;
|
|
|
|
mod_opt_type(ldap_encrypt) ->
|
|
|
|
fun (tls) -> tls;
|
|
|
|
(starttls) -> starttls;
|
|
|
|
(none) -> none
|
|
|
|
end;
|
|
|
|
mod_opt_type(ldap_password) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(ldap_port) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
|
|
|
mod_opt_type(ldap_rootdn) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(ldap_servers) ->
|
|
|
|
fun (L) -> [iolist_to_binary(H) || H <- L] end;
|
|
|
|
mod_opt_type(ldap_tls_cacertfile) ->
|
|
|
|
fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(ldap_tls_certfile) ->
|
|
|
|
fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(ldap_tls_depth) ->
|
|
|
|
fun (I) when is_integer(I), I >= 0 -> I end;
|
|
|
|
mod_opt_type(ldap_tls_verify) ->
|
|
|
|
fun (hard) -> hard;
|
|
|
|
(soft) -> soft;
|
|
|
|
(false) -> false
|
|
|
|
end;
|
|
|
|
mod_opt_type(ldap_auth_check) ->
|
|
|
|
fun (on) -> true;
|
|
|
|
(off) -> false;
|
|
|
|
(false) -> false;
|
|
|
|
(true) -> true
|
|
|
|
end;
|
2017-04-28 12:23:32 +02:00
|
|
|
mod_opt_type(ldap_filter) -> fun eldap_utils:check_filter/1;
|
|
|
|
mod_opt_type(ldap_gfilter) -> fun eldap_utils:check_filter/1;
|
2017-04-21 09:43:14 +02:00
|
|
|
mod_opt_type(O) when O == cache_size;
|
|
|
|
O == cache_life_time ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I;
|
|
|
|
(infinity) -> infinity
|
|
|
|
end;
|
|
|
|
mod_opt_type(O) when O == use_cache; O == cache_missed ->
|
|
|
|
fun (B) when is_boolean(B) -> B end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(ldap_groupattr) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(ldap_groupdesc) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(ldap_memberattr) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(ldap_memberattr_format) ->
|
|
|
|
fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(ldap_memberattr_format_re) ->
|
|
|
|
fun (S) ->
|
|
|
|
Re = iolist_to_binary(S), {ok, MP} = re:compile(Re), MP
|
|
|
|
end;
|
2017-04-28 12:23:32 +02:00
|
|
|
mod_opt_type(ldap_rfilter) -> fun eldap_utils:check_filter/1;
|
|
|
|
mod_opt_type(ldap_ufilter) -> fun eldap_utils:check_filter/1;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(ldap_userdesc) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(ldap_useruid) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(_) ->
|
|
|
|
[ldap_auth_check, ldap_filter, ldap_gfilter,
|
|
|
|
ldap_groupattr, ldap_groupdesc, ldap_memberattr,
|
|
|
|
ldap_memberattr_format, ldap_memberattr_format_re,
|
2017-04-21 09:43:14 +02:00
|
|
|
ldap_rfilter, ldap_ufilter, ldap_userdesc, ldap_useruid,
|
2015-06-01 14:38:27 +02:00
|
|
|
deref_aliases, ldap_backups, ldap_base,
|
|
|
|
ldap_deref_aliases, ldap_encrypt, ldap_password,
|
|
|
|
ldap_port, ldap_rootdn, ldap_servers,
|
|
|
|
ldap_tls_cacertfile, ldap_tls_certfile, ldap_tls_depth,
|
2017-04-21 09:43:14 +02:00
|
|
|
ldap_tls_verify, use_cache, cache_missed, cache_size, cache_life_time].
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2017-04-28 12:23:32 +02:00
|
|
|
opt_type(ldap_gfilter) -> fun eldap_utils:check_filter/1;
|
|
|
|
opt_type(ldap_rfilter) -> fun eldap_utils:check_filter/1;
|
|
|
|
opt_type(ldap_ufilter) -> fun eldap_utils:check_filter/1;
|
2015-06-01 14:38:27 +02:00
|
|
|
opt_type(_) ->
|
2017-04-28 12:23:32 +02:00
|
|
|
[ldap_gfilter, ldap_rfilter, ldap_ufilter].
|