2004-07-30 23:09:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_vcard_ldap.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2006-09-14 04:54:21 +02:00
|
|
|
%%% Purpose : Support for VCards from LDAP storage.
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 2 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2015 ProcessOne
|
2007-12-24 13:58:05 +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 13:58:05 +01:00
|
|
|
%%%
|
2004-07-30 23:09:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_vcard_ldap).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2006-09-14 04:54:21 +02:00
|
|
|
-behaviour(gen_server).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2004-07-30 23:09:55 +02:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
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]).
|
|
|
|
|
|
|
|
-export([start/2, start_link/2, stop/1,
|
|
|
|
get_sm_features/5, process_local_iq/3, process_sm_iq/3,
|
2015-06-01 14:38:27 +02:00
|
|
|
remove_user/1, route/4, transform_module_options/1,
|
|
|
|
mod_opt_type/1, opt_type/1]).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("eldap.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2004-07-30 23:09:55 +02:00
|
|
|
-include("jlib.hrl").
|
|
|
|
|
2005-08-25 22:48:45 +02:00
|
|
|
-define(PROCNAME, ejabberd_mod_vcard_ldap).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(state,
|
|
|
|
{serverhost = <<"">> :: binary(),
|
|
|
|
myhost = <<"">> :: binary(),
|
|
|
|
eldap_id = <<"">> :: binary(),
|
|
|
|
search = true :: boolean(),
|
|
|
|
servers = [] :: [binary()],
|
|
|
|
backups = [] :: [binary()],
|
|
|
|
port = ?LDAP_PORT :: inet:port_number(),
|
|
|
|
tls_options = [] :: list(),
|
|
|
|
dn = <<"">> :: binary(),
|
|
|
|
base = <<"">> :: binary(),
|
|
|
|
password = <<"">> :: binary(),
|
|
|
|
uids = [] :: [{binary()} | {binary(), binary()}],
|
|
|
|
vcard_map = [] :: [{binary(), binary(), [binary()]}],
|
|
|
|
vcard_map_attrs = [] :: [binary()],
|
|
|
|
user_filter = <<"">> :: binary(),
|
|
|
|
search_filter :: eldap:filter(),
|
|
|
|
search_fields = [] :: [{binary(), binary()}],
|
|
|
|
search_reported = [] :: [{binary(), binary()}],
|
|
|
|
search_reported_attrs = [] :: [binary()],
|
|
|
|
deref_aliases = never :: never | searching | finding | always,
|
|
|
|
matches = 0 :: non_neg_integer()}).
|
2006-09-14 04:54:21 +02:00
|
|
|
|
|
|
|
-define(VCARD_MAP,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{<<"NICKNAME">>, <<"%u">>, []},
|
|
|
|
{<<"FN">>, <<"%s">>, [<<"displayName">>]},
|
|
|
|
{<<"FAMILY">>, <<"%s">>, [<<"sn">>]},
|
|
|
|
{<<"GIVEN">>, <<"%s">>, [<<"givenName">>]},
|
|
|
|
{<<"MIDDLE">>, <<"%s">>, [<<"initials">>]},
|
|
|
|
{<<"ORGNAME">>, <<"%s">>, [<<"o">>]},
|
|
|
|
{<<"ORGUNIT">>, <<"%s">>, [<<"ou">>]},
|
|
|
|
{<<"CTRY">>, <<"%s">>, [<<"c">>]},
|
|
|
|
{<<"LOCALITY">>, <<"%s">>, [<<"l">>]},
|
|
|
|
{<<"STREET">>, <<"%s">>, [<<"street">>]},
|
|
|
|
{<<"REGION">>, <<"%s">>, [<<"st">>]},
|
|
|
|
{<<"PCODE">>, <<"%s">>, [<<"postalCode">>]},
|
|
|
|
{<<"TITLE">>, <<"%s">>, [<<"title">>]},
|
|
|
|
{<<"URL">>, <<"%s">>, [<<"labeleduri">>]},
|
|
|
|
{<<"DESC">>, <<"%s">>, [<<"description">>]},
|
|
|
|
{<<"TEL">>, <<"%s">>, [<<"telephoneNumber">>]},
|
|
|
|
{<<"EMAIL">>, <<"%s">>, [<<"mail">>]},
|
|
|
|
{<<"BDAY">>, <<"%s">>, [<<"birthDay">>]},
|
|
|
|
{<<"ROLE">>, <<"%s">>, [<<"employeeType">>]},
|
|
|
|
{<<"PHOTO">>, <<"%s">>, [<<"jpegPhoto">>]}]).
|
2006-09-14 04:54:21 +02:00
|
|
|
|
|
|
|
-define(SEARCH_FIELDS,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{<<"User">>, <<"%u">>},
|
|
|
|
{<<"Full Name">>, <<"displayName">>},
|
|
|
|
{<<"Given Name">>, <<"givenName">>},
|
|
|
|
{<<"Middle Name">>, <<"initials">>},
|
|
|
|
{<<"Family Name">>, <<"sn">>},
|
|
|
|
{<<"Nickname">>, <<"%u">>},
|
|
|
|
{<<"Birthday">>, <<"birthDay">>},
|
|
|
|
{<<"Country">>, <<"c">>}, {<<"City">>, <<"l">>},
|
|
|
|
{<<"Email">>, <<"mail">>},
|
|
|
|
{<<"Organization Name">>, <<"o">>},
|
|
|
|
{<<"Organization Unit">>, <<"ou">>}]).
|
2006-09-14 04:54:21 +02:00
|
|
|
|
|
|
|
-define(SEARCH_REPORTED,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{<<"Full Name">>, <<"FN">>},
|
|
|
|
{<<"Given Name">>, <<"FIRST">>},
|
|
|
|
{<<"Middle Name">>, <<"MIDDLE">>},
|
|
|
|
{<<"Family Name">>, <<"LAST">>},
|
|
|
|
{<<"Nickname">>, <<"NICK">>},
|
|
|
|
{<<"Birthday">>, <<"BDAY">>},
|
|
|
|
{<<"Country">>, <<"CTRY">>},
|
|
|
|
{<<"City">>, <<"LOCALITY">>},
|
|
|
|
{<<"Email">>, <<"EMAIL">>},
|
|
|
|
{<<"Organization Name">>, <<"ORGNAME">>},
|
|
|
|
{<<"Organization Unit">>, <<"ORGUNIT">>}]).
|
|
|
|
|
|
|
|
handle_cast(_Request, State) -> {noreply, State}.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2006-09-14 04:54:21 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2006-09-14 04:54:21 +02:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]},
|
|
|
|
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, ?PROCNAME),
|
|
|
|
gen_server:call(Proc, stop),
|
|
|
|
supervisor:terminate_child(ejabberd_sup, Proc),
|
|
|
|
supervisor:delete_child(ejabberd_sup, Proc).
|
|
|
|
|
|
|
|
terminate(_Reason, State) ->
|
|
|
|
Host = State#state.serverhost,
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_VCARD),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_VCARD),
|
|
|
|
ejabberd_hooks:delete(disco_sm_features, Host, ?MODULE,
|
|
|
|
get_sm_features, 50),
|
2006-09-14 04:54:21 +02:00
|
|
|
case State#state.search of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
ejabberd_router:unregister_route(State#state.myhost);
|
|
|
|
_ -> ok
|
2006-09-14 04:54:21 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_server:start_link({local, Proc}, ?MODULE,
|
|
|
|
[Host, Opts], []).
|
2006-09-14 04:54:21 +02:00
|
|
|
|
|
|
|
init([Host, Opts]) ->
|
|
|
|
State = parse_options(Host, Opts),
|
2013-03-14 10:33:02 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
one_queue),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_VCARD, ?MODULE, process_local_iq, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_VCARD, ?MODULE, process_sm_iq, IQDisc),
|
|
|
|
ejabberd_hooks:add(disco_sm_features, Host, ?MODULE,
|
|
|
|
get_sm_features, 50),
|
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),
|
2006-09-14 04:54:21 +02:00
|
|
|
case State#state.search of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
ejabberd_router:register_route(State#state.myhost);
|
|
|
|
_ -> ok
|
2006-09-14 04:54:21 +02:00
|
|
|
end,
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
handle_info({route, From, To, Packet}, State) ->
|
|
|
|
case catch do_route(State, From, To, Packet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
Pid when is_pid(Pid) -> ok;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_INTERNAL_SERVER_ERROR),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
2007-01-27 17:40:37 +01:00
|
|
|
end,
|
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(_Info, State) -> {noreply, State}.
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_sm_features({error, _Error} = Acc, _From, _To,
|
|
|
|
_Node, _Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Acc;
|
|
|
|
get_sm_features(Acc, _From, _To, Node, _Lang) ->
|
|
|
|
case Node of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> ->
|
|
|
|
case Acc of
|
|
|
|
{result, Features} -> {result, [?NS_VCARD | Features]};
|
|
|
|
empty -> {result, [?NS_VCARD]}
|
|
|
|
end;
|
|
|
|
_ -> Acc
|
2005-09-04 03:58:47 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_local_iq(_From, _To,
|
|
|
|
#iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
|
2004-07-30 23:09:55 +02:00
|
|
|
case Type of
|
2013-03-14 10:33:02 +01:00
|
|
|
set ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
|
|
|
|
get ->
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"vCard">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_VCARD}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"FN">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata, <<"ejabberd">>}]},
|
|
|
|
#xmlel{name = <<"URL">>, attrs = [],
|
|
|
|
children = [{xmlcdata, ?EJABBERD_URI}]},
|
|
|
|
#xmlel{name = <<"DESC">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<(translate:translate(Lang,
|
|
|
|
<<"Erlang Jabber Server">>))/binary,
|
2015-01-08 17:34:43 +01:00
|
|
|
"\nCopyright (c) 2002-2015 ProcessOne">>}]},
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"BDAY">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata, <<"2002-11-16">>}]}]}]}
|
2004-07-30 23:09:55 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_sm_iq(_From, #jid{lserver = LServer} = To,
|
|
|
|
#iq{sub_el = SubEl} = IQ) ->
|
2007-01-27 17:40:37 +01:00
|
|
|
case catch process_vcard_ldap(To, IQ, LServer) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', _} ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]};
|
|
|
|
Other -> Other
|
2004-07-30 23:09:55 +02:00
|
|
|
end.
|
|
|
|
|
2007-01-27 17:40:37 +01:00
|
|
|
process_vcard_ldap(To, IQ, Server) ->
|
|
|
|
{ok, State} = eldap_utils:get_state(Server, ?PROCNAME),
|
2006-09-14 04:54:21 +02:00
|
|
|
#iq{type = Type, sub_el = SubEl} = IQ,
|
2007-01-27 17:40:37 +01:00
|
|
|
case Type of
|
2013-03-14 10:33:02 +01:00
|
|
|
set ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
|
|
|
|
get ->
|
|
|
|
#jid{luser = LUser} = To,
|
|
|
|
LServer = State#state.serverhost,
|
|
|
|
case ejabberd_auth:is_user_exists(LUser, LServer) of
|
|
|
|
true ->
|
|
|
|
VCardMap = State#state.vcard_map,
|
|
|
|
case find_ldap_user(LUser, State) of
|
|
|
|
#eldap_entry{attributes = Attributes} ->
|
|
|
|
Vcard = ldap_attributes_to_vcard(Attributes, VCardMap,
|
|
|
|
{LUser, LServer}),
|
|
|
|
IQ#iq{type = result, sub_el = Vcard};
|
|
|
|
_ -> IQ#iq{type = result, sub_el = []}
|
|
|
|
end;
|
|
|
|
_ -> IQ#iq{type = result, sub_el = []}
|
|
|
|
end
|
|
|
|
end.
|
2006-09-14 04:54:21 +02:00
|
|
|
|
2007-01-27 17:40:37 +01:00
|
|
|
handle_call(get_state, _From, State) ->
|
|
|
|
{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_ldap_user(User, State) ->
|
|
|
|
Base = State#state.base,
|
|
|
|
RFC2254_Filter = State#state.user_filter,
|
|
|
|
Eldap_ID = State#state.eldap_id,
|
|
|
|
VCardAttrs = State#state.vcard_map_attrs,
|
2013-03-14 10:33:02 +01:00
|
|
|
case eldap_filter:parse(RFC2254_Filter,
|
|
|
|
[{<<"%u">>, User}])
|
|
|
|
of
|
|
|
|
{ok, EldapFilter} ->
|
|
|
|
case eldap_pool:search(Eldap_ID,
|
|
|
|
[{base, Base}, {filter, EldapFilter},
|
|
|
|
{deref_aliases, State#state.deref_aliases},
|
|
|
|
{attributes, VCardAttrs}])
|
|
|
|
of
|
|
|
|
#eldap_search_result{entries = [E | _]} -> E;
|
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
_ -> false
|
2004-07-30 23:09:55 +02:00
|
|
|
end.
|
|
|
|
|
2006-09-14 04:54:21 +02:00
|
|
|
ldap_attributes_to_vcard(Attributes, VCardMap, UD) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs = lists:map(fun ({VCardName, _, _}) ->
|
|
|
|
{stringprep:tolower(VCardName),
|
|
|
|
map_vcard_attr(VCardName, Attributes, VCardMap,
|
|
|
|
UD)}
|
|
|
|
end,
|
|
|
|
VCardMap),
|
|
|
|
Elts = [ldap_attribute_to_vcard(vCard, Attr)
|
|
|
|
|| Attr <- Attrs],
|
|
|
|
NElts = [ldap_attribute_to_vcard(vCardN, Attr)
|
|
|
|
|| Attr <- Attrs],
|
|
|
|
OElts = [ldap_attribute_to_vcard(vCardO, Attr)
|
|
|
|
|| Attr <- Attrs],
|
|
|
|
AElts = [ldap_attribute_to_vcard(vCardA, Attr)
|
|
|
|
|| Attr <- Attrs],
|
|
|
|
[#xmlel{name = <<"vCard">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_VCARD}],
|
|
|
|
children =
|
|
|
|
lists:append([X || X <- Elts, X /= none],
|
|
|
|
[#xmlel{name = <<"N">>, attrs = [],
|
|
|
|
children = [X || X <- NElts, X /= none]},
|
|
|
|
#xmlel{name = <<"ORG">>, attrs = [],
|
|
|
|
children = [X || X <- OElts, X /= none]},
|
|
|
|
#xmlel{name = <<"ADR">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[X || X <- AElts, X /= none]}])}].
|
|
|
|
|
|
|
|
ldap_attribute_to_vcard(vCard, {<<"fn">>, Value}) ->
|
|
|
|
#xmlel{name = <<"FN">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCard,
|
|
|
|
{<<"nickname">>, Value}) ->
|
|
|
|
#xmlel{name = <<"NICKNAME">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCard, {<<"title">>, Value}) ->
|
|
|
|
#xmlel{name = <<"TITLE">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCard, {<<"bday">>, Value}) ->
|
|
|
|
#xmlel{name = <<"BDAY">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCard, {<<"url">>, Value}) ->
|
|
|
|
#xmlel{name = <<"URL">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCard, {<<"desc">>, Value}) ->
|
|
|
|
#xmlel{name = <<"DESC">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCard, {<<"role">>, Value}) ->
|
|
|
|
#xmlel{name = <<"ROLE">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCard, {<<"tel">>, Value}) ->
|
|
|
|
#xmlel{name = <<"TEL">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"VOICE">>, attrs = [], children = []},
|
|
|
|
#xmlel{name = <<"WORK">>, attrs = [], children = []},
|
|
|
|
#xmlel{name = <<"NUMBER">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]}]};
|
|
|
|
ldap_attribute_to_vcard(vCard, {<<"email">>, Value}) ->
|
|
|
|
#xmlel{name = <<"EMAIL">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"INTERNET">>, attrs = [],
|
|
|
|
children = []},
|
|
|
|
#xmlel{name = <<"PREF">>, attrs = [], children = []},
|
|
|
|
#xmlel{name = <<"USERID">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]}]};
|
|
|
|
ldap_attribute_to_vcard(vCard, {<<"photo">>, Value}) ->
|
|
|
|
#xmlel{name = <<"PHOTO">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"TYPE">>, attrs = [],
|
|
|
|
children = [{xmlcdata, <<"image/jpeg">>}]},
|
|
|
|
#xmlel{name = <<"BINVAL">>, attrs = [],
|
|
|
|
children = [{xmlcdata, jlib:encode_base64(Value)}]}]};
|
|
|
|
ldap_attribute_to_vcard(vCardN,
|
|
|
|
{<<"family">>, Value}) ->
|
|
|
|
#xmlel{name = <<"FAMILY">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCardN, {<<"given">>, Value}) ->
|
|
|
|
#xmlel{name = <<"GIVEN">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCardN,
|
|
|
|
{<<"middle">>, Value}) ->
|
|
|
|
#xmlel{name = <<"MIDDLE">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCardO,
|
|
|
|
{<<"orgname">>, Value}) ->
|
|
|
|
#xmlel{name = <<"ORGNAME">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCardO,
|
|
|
|
{<<"orgunit">>, Value}) ->
|
|
|
|
#xmlel{name = <<"ORGUNIT">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCardA,
|
|
|
|
{<<"locality">>, Value}) ->
|
|
|
|
#xmlel{name = <<"LOCALITY">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCardA,
|
|
|
|
{<<"street">>, Value}) ->
|
|
|
|
#xmlel{name = <<"STREET">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCardA, {<<"ctry">>, Value}) ->
|
|
|
|
#xmlel{name = <<"CTRY">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCardA,
|
|
|
|
{<<"region">>, Value}) ->
|
|
|
|
#xmlel{name = <<"REGION">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(vCardA, {<<"pcode">>, Value}) ->
|
|
|
|
#xmlel{name = <<"PCODE">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]};
|
|
|
|
ldap_attribute_to_vcard(_, _) -> none.
|
2004-07-30 23:09:55 +02:00
|
|
|
|
|
|
|
-define(TLFIELD(Type, Label, Var),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"field">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"type">>, Type},
|
|
|
|
{<<"label">>, translate:translate(Lang, Label)},
|
|
|
|
{<<"var">>, Var}],
|
|
|
|
children = []}).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2006-09-14 04:54:21 +02:00
|
|
|
-define(FORM(JID, SearchFields),
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"instructions">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(Lang,
|
|
|
|
<<"You need an x:data capable client to "
|
|
|
|
"search">>)}]},
|
|
|
|
#xmlel{name = <<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_XDATA}, {<<"type">>, <<"form">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"title">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<(translate:translate(Lang,
|
|
|
|
<<"Search users in ">>))/binary,
|
|
|
|
(jlib:jid_to_string(JID))/binary>>}]},
|
|
|
|
#xmlel{name = <<"instructions">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(Lang,
|
|
|
|
<<"Fill in fields to search for any matching "
|
|
|
|
"Jabber User">>)}]}]
|
|
|
|
++
|
|
|
|
lists:map(fun ({X, Y}) ->
|
|
|
|
?TLFIELD(<<"text-single">>, X, Y)
|
|
|
|
end,
|
|
|
|
SearchFields)}]).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2006-09-14 04:54:21 +02:00
|
|
|
do_route(State, From, To, Packet) ->
|
2007-01-27 17:40:37 +01:00
|
|
|
spawn(?MODULE, route, [State, From, To, Packet]).
|
|
|
|
|
|
|
|
route(State, From, To, Packet) ->
|
2004-07-30 23:09:55 +02:00
|
|
|
#jid{user = User, resource = Resource} = To,
|
2009-07-17 22:45:44 +02:00
|
|
|
ServerHost = State#state.serverhost,
|
2013-03-14 10:33:02 +01:00
|
|
|
if (User /= <<"">>) or (Resource /= <<"">>) ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
true ->
|
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case IQ of
|
|
|
|
#iq{type = Type, xmlns = ?NS_SEARCH, lang = Lang,
|
|
|
|
sub_el = SubEl} ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
|
|
|
XDataEl = find_xdata_el(SubEl),
|
|
|
|
case XDataEl of
|
|
|
|
false ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_BAD_REQUEST),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ ->
|
|
|
|
XData = jlib:parse_xdata_submit(XDataEl),
|
|
|
|
case XData of
|
|
|
|
invalid ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_BAD_REQUEST),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ ->
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_SEARCH}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name =
|
|
|
|
<<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_XDATA},
|
|
|
|
{<<"type">>,
|
|
|
|
<<"result">>}],
|
|
|
|
children
|
|
|
|
=
|
|
|
|
search_result(Lang,
|
|
|
|
To,
|
|
|
|
State,
|
|
|
|
XData)}]}]},
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
jlib:iq_to_xml(ResIQ))
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
get ->
|
|
|
|
SearchFields = State#state.search_fields,
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_SEARCH}],
|
|
|
|
children =
|
|
|
|
?FORM(To, SearchFields)}]},
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(ResIQ))
|
|
|
|
end;
|
|
|
|
#iq{type = Type, xmlns = ?NS_DISCO_INFO, lang = Lang} ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
|
|
|
Err = jlib:make_error_reply(Packet, ?ERR_NOT_ALLOWED),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
get ->
|
|
|
|
Info = ejabberd_hooks:run_fold(disco_info, ServerHost,
|
|
|
|
[],
|
|
|
|
[ServerHost, ?MODULE,
|
|
|
|
<<"">>, <<"">>]),
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_DISCO_INFO}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name =
|
|
|
|
<<"identity">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"category">>,
|
|
|
|
<<"directory">>},
|
|
|
|
{<<"type">>,
|
|
|
|
<<"user">>},
|
|
|
|
{<<"name">>,
|
|
|
|
translate:translate(Lang,
|
|
|
|
<<"vCard User Search">>)}],
|
|
|
|
children = []},
|
|
|
|
#xmlel{name =
|
|
|
|
<<"feature">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"var">>,
|
|
|
|
?NS_SEARCH}],
|
|
|
|
children = []},
|
|
|
|
#xmlel{name =
|
|
|
|
<<"feature">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"var">>,
|
|
|
|
?NS_VCARD}],
|
|
|
|
children = []}]
|
|
|
|
++ Info}]},
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(ResIQ))
|
|
|
|
end;
|
|
|
|
#iq{type = Type, xmlns = ?NS_DISCO_ITEMS} ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
|
|
|
Err = jlib:make_error_reply(Packet, ?ERR_NOT_ALLOWED),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
get ->
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_DISCO_ITEMS}],
|
|
|
|
children = []}]},
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(ResIQ))
|
|
|
|
end;
|
|
|
|
#iq{type = get, xmlns = ?NS_VCARD, lang = Lang} ->
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"vCard">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_VCARD}],
|
|
|
|
children = iq_get_vcard(Lang)}]},
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(ResIQ));
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end
|
2004-07-30 23:09:55 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
iq_get_vcard(Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"FN">>, attrs = [],
|
|
|
|
children = [{xmlcdata, <<"ejabberd/mod_vcard">>}]},
|
|
|
|
#xmlel{name = <<"URL">>, attrs = [],
|
|
|
|
children = [{xmlcdata, ?EJABBERD_URI}]},
|
|
|
|
#xmlel{name = <<"DESC">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<(translate:translate(Lang,
|
|
|
|
<<"ejabberd vCard module">>))/binary,
|
2015-01-08 17:34:43 +01:00
|
|
|
"\nCopyright (c) 2003-2015 ProcessOne">>}]}].
|
2004-07-30 23:09:55 +02:00
|
|
|
|
|
|
|
-define(LFIELD(Label, Var),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"field">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"label">>, translate:translate(Lang, Label)},
|
|
|
|
{<<"var">>, Var}],
|
|
|
|
children = []}).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2006-09-14 04:54:21 +02:00
|
|
|
search_result(Lang, JID, State, Data) ->
|
|
|
|
SearchReported = State#state.search_reported,
|
2013-03-14 10:33:02 +01:00
|
|
|
Header = [#xmlel{name = <<"title">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<(translate:translate(Lang,
|
|
|
|
<<"Search Results for ">>))/binary,
|
|
|
|
(jlib:jid_to_string(JID))/binary>>}]},
|
|
|
|
#xmlel{name = <<"reported">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[?TLFIELD(<<"text-single">>, <<"Jabber ID">>,
|
|
|
|
<<"jid">>)]
|
|
|
|
++
|
|
|
|
lists:map(fun ({Name, Value}) ->
|
|
|
|
?TLFIELD(<<"text-single">>, Name,
|
|
|
|
Value)
|
|
|
|
end,
|
|
|
|
SearchReported)}],
|
2006-09-14 04:54:21 +02:00
|
|
|
case search(State, Data) of
|
2013-03-14 10:33:02 +01:00
|
|
|
error -> Header;
|
|
|
|
Result -> Header ++ Result
|
2006-09-14 04:54:21 +02:00
|
|
|
end.
|
2004-07-30 23:09:55 +02:00
|
|
|
|
|
|
|
-define(FIELD(Var, Val),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"field">>, attrs = [{<<"var">>, Var}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"value">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Val}]}]}).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2006-09-14 04:54:21 +02:00
|
|
|
search(State, Data) ->
|
|
|
|
Base = State#state.base,
|
|
|
|
SearchFilter = State#state.search_filter,
|
|
|
|
Eldap_ID = State#state.eldap_id,
|
2006-10-17 14:35:47 +02:00
|
|
|
UIDs = State#state.uids,
|
2007-01-27 17:40:37 +01:00
|
|
|
Limit = State#state.matches,
|
2006-09-14 04:54:21 +02:00
|
|
|
ReportedAttrs = State#state.search_reported_attrs,
|
2013-03-14 10:33:02 +01:00
|
|
|
Filter = eldap:'and'([SearchFilter,
|
|
|
|
eldap_utils:make_filter(Data, UIDs)]),
|
2011-09-19 08:58:55 +02:00
|
|
|
case eldap_pool:search(Eldap_ID,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{base, Base}, {filter, Filter}, {limit, Limit},
|
|
|
|
{deref_aliases, State#state.deref_aliases},
|
|
|
|
{attributes, ReportedAttrs}])
|
|
|
|
of
|
|
|
|
#eldap_search_result{entries = E} ->
|
|
|
|
search_items(E, State);
|
|
|
|
_ -> error
|
2004-07-30 23:09:55 +02:00
|
|
|
end.
|
|
|
|
|
2006-09-14 04:54:21 +02:00
|
|
|
search_items(Entries, State) ->
|
|
|
|
LServer = State#state.serverhost,
|
|
|
|
SearchReported = State#state.search_reported,
|
|
|
|
VCardMap = State#state.vcard_map,
|
2013-03-14 10:33:02 +01:00
|
|
|
UIDs = State#state.uids,
|
|
|
|
Attributes = lists:map(fun (E) ->
|
|
|
|
#eldap_entry{attributes = Attrs} = E, Attrs
|
|
|
|
end,
|
|
|
|
Entries),
|
|
|
|
lists:flatmap(fun (Attrs) ->
|
|
|
|
case eldap_utils:find_ldap_attrs(UIDs, Attrs) of
|
|
|
|
{U, UIDAttrFormat} ->
|
|
|
|
case eldap_utils:get_user_part(U, UIDAttrFormat)
|
|
|
|
of
|
|
|
|
{ok, Username} ->
|
|
|
|
case
|
|
|
|
ejabberd_auth:is_user_exists(Username,
|
|
|
|
LServer)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
RFields = lists:map(fun ({_,
|
|
|
|
VCardName}) ->
|
|
|
|
{VCardName,
|
|
|
|
map_vcard_attr(VCardName,
|
|
|
|
Attrs,
|
|
|
|
VCardMap,
|
|
|
|
{Username,
|
|
|
|
?MYNAME})}
|
|
|
|
end,
|
|
|
|
SearchReported),
|
|
|
|
Result = [?FIELD(<<"jid">>,
|
|
|
|
<<Username/binary,
|
|
|
|
"@",
|
|
|
|
LServer/binary>>)]
|
|
|
|
++
|
|
|
|
[?FIELD(Name, Value)
|
|
|
|
|| {Name, Value}
|
|
|
|
<- RFields],
|
|
|
|
[#xmlel{name = <<"item">>,
|
|
|
|
attrs = [],
|
|
|
|
children = Result}];
|
|
|
|
_ -> []
|
|
|
|
end;
|
|
|
|
_ -> []
|
|
|
|
end;
|
|
|
|
<<"">> -> []
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Attributes).
|
|
|
|
|
|
|
|
remove_user(_User) -> true.
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2006-09-14 04:54:21 +02:00
|
|
|
%%%-----------------------
|
|
|
|
%%% Auxiliary functions.
|
|
|
|
%%%-----------------------
|
|
|
|
|
|
|
|
map_vcard_attr(VCardName, Attributes, Pattern, UD) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Res = lists:filter(fun ({Name, _, _}) ->
|
|
|
|
eldap_utils:case_insensitive_match(Name,
|
|
|
|
VCardName)
|
|
|
|
end,
|
|
|
|
Pattern),
|
2006-09-14 04:54:21 +02:00
|
|
|
case Res of
|
2013-03-14 10:33:02 +01:00
|
|
|
[{_, Str, Attrs}] ->
|
|
|
|
process_pattern(Str, UD,
|
|
|
|
[eldap_utils:get_ldap_attr(X, Attributes)
|
|
|
|
|| X <- Attrs]);
|
|
|
|
_ -> <<"">>
|
2006-09-14 04:54:21 +02:00
|
|
|
end.
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2006-09-23 06:28:03 +02:00
|
|
|
process_pattern(Str, {User, Domain}, AttrValues) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
eldap_filter:do_sub(Str,
|
|
|
|
[{<<"%u">>, User}, {<<"%d">>, Domain}] ++
|
|
|
|
[{<<"%s">>, V, 1} || V <- AttrValues]).
|
2006-09-14 04:54:21 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
find_xdata_el(#xmlel{children = SubEls}) ->
|
2006-09-14 04:54:21 +02:00
|
|
|
find_xdata_el1(SubEls).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
find_xdata_el1([]) -> false;
|
|
|
|
find_xdata_el1([#xmlel{name = Name, attrs = Attrs,
|
|
|
|
children = SubEls}
|
|
|
|
| Els]) ->
|
|
|
|
case xml:get_attr_s(<<"xmlns">>, Attrs) of
|
|
|
|
?NS_XDATA ->
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = SubEls};
|
|
|
|
_ -> find_xdata_el1(Els)
|
2006-09-14 04:54:21 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
find_xdata_el1([_ | Els]) -> find_xdata_el1(Els).
|
2004-07-30 23:09:55 +02:00
|
|
|
|
2006-09-14 04:54:21 +02:00
|
|
|
parse_options(Host, Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
MyHost = gen_mod:get_opt_host(Host, Opts,
|
|
|
|
<<"vjud.@HOST@">>),
|
|
|
|
Search = gen_mod:get_opt(search, Opts,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
true),
|
|
|
|
Matches = gen_mod:get_opt(matches, Opts,
|
|
|
|
fun(infinity) -> 0;
|
|
|
|
(I) when is_integer(I), I>0 -> I
|
|
|
|
end, 30),
|
|
|
|
Eldap_ID = jlib:atom_to_binary(gen_mod:get_module_proc(Host, ?PROCNAME)),
|
|
|
|
Cfg = eldap_utils:get_config(Host, Opts),
|
2015-05-31 15:14:57 +02:00
|
|
|
UIDsTemp = gen_mod:get_opt(
|
2013-03-14 10:33:02 +01:00
|
|
|
{ldap_uids, Host}, Opts,
|
|
|
|
fun(Us) ->
|
|
|
|
lists:map(
|
|
|
|
fun({U, P}) ->
|
|
|
|
{iolist_to_binary(U),
|
|
|
|
iolist_to_binary(P)};
|
|
|
|
({U}) ->
|
|
|
|
{iolist_to_binary(U)}
|
|
|
|
end, Us)
|
|
|
|
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}, Opts,
|
|
|
|
fun check_filter/1, <<"">>) of
|
|
|
|
<<"">> ->
|
|
|
|
SubFilter;
|
|
|
|
F ->
|
|
|
|
<<"(&", SubFilter/binary, F/binary, ")">>
|
|
|
|
end,
|
|
|
|
{ok, SearchFilter} =
|
|
|
|
eldap_filter:parse(eldap_filter:do_sub(UserFilter,
|
|
|
|
[{<<"%u">>, <<"*">>}])),
|
|
|
|
VCardMap = gen_mod:get_opt(ldap_vcard_map, Opts,
|
|
|
|
fun(Ls) ->
|
|
|
|
lists:map(
|
2013-08-12 14:25:05 +02:00
|
|
|
fun({S, [{P, L}]}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{iolist_to_binary(S),
|
|
|
|
iolist_to_binary(P),
|
|
|
|
[iolist_to_binary(E)
|
|
|
|
|| E <- L]}
|
|
|
|
end, Ls)
|
|
|
|
end, ?VCARD_MAP),
|
|
|
|
SearchFields = gen_mod:get_opt(ldap_search_fields, Opts,
|
|
|
|
fun(Ls) ->
|
|
|
|
[{iolist_to_binary(S),
|
|
|
|
iolist_to_binary(P)}
|
|
|
|
|| {S, P} <- Ls]
|
|
|
|
end, ?SEARCH_FIELDS),
|
|
|
|
SearchReported = gen_mod:get_opt(ldap_search_reported, Opts,
|
|
|
|
fun(Ls) ->
|
|
|
|
[{iolist_to_binary(S),
|
|
|
|
iolist_to_binary(P)}
|
|
|
|
|| {S, P} <- Ls]
|
|
|
|
end, ?SEARCH_REPORTED),
|
|
|
|
UIDAttrs = [UAttr || {UAttr, _} <- UIDs],
|
|
|
|
VCardMapAttrs = lists:usort(lists:append([A
|
|
|
|
|| {_, _, A} <- VCardMap])
|
|
|
|
++ UIDAttrs),
|
|
|
|
SearchReportedAttrs = lists:usort(lists:flatmap(fun ({_,
|
|
|
|
N}) ->
|
|
|
|
case
|
|
|
|
lists:keysearch(N,
|
|
|
|
1,
|
|
|
|
VCardMap)
|
|
|
|
of
|
|
|
|
{value,
|
|
|
|
{_, _, L}} ->
|
|
|
|
L;
|
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
SearchReported)
|
|
|
|
++ UIDAttrs),
|
|
|
|
#state{serverhost = Host, myhost = MyHost,
|
|
|
|
eldap_id = Eldap_ID, search = Search,
|
|
|
|
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, vcard_map = VCardMap,
|
2006-09-14 04:54:21 +02:00
|
|
|
vcard_map_attrs = VCardMapAttrs,
|
2013-03-14 10:33:02 +01:00
|
|
|
user_filter = UserFilter, search_filter = SearchFilter,
|
2006-09-14 04:54:21 +02:00
|
|
|
search_fields = SearchFields,
|
|
|
|
search_reported = SearchReported,
|
2007-01-27 17:40:37 +01:00
|
|
|
search_reported_attrs = SearchReportedAttrs,
|
2013-03-14 10:33:02 +01:00
|
|
|
matches = Matches}.
|
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
transform_module_options(Opts) ->
|
|
|
|
lists:map(
|
|
|
|
fun({ldap_vcard_map, Map}) ->
|
|
|
|
NewMap = lists:map(
|
|
|
|
fun({Field, Pattern, Attrs}) ->
|
|
|
|
{Field, [{Pattern, Attrs}]};
|
|
|
|
(Opt) ->
|
|
|
|
Opt
|
|
|
|
end, Map),
|
|
|
|
{ldap_vcard_map, NewMap};
|
|
|
|
(Opt) ->
|
|
|
|
Opt
|
|
|
|
end, Opts).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
check_filter(F) ->
|
|
|
|
NewF = iolist_to_binary(F),
|
|
|
|
{ok, _} = eldap_filter:parse(NewF),
|
|
|
|
NewF.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(host) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
|
|
|
|
mod_opt_type(ldap_filter) -> fun check_filter/1;
|
|
|
|
mod_opt_type(ldap_search_fields) ->
|
|
|
|
fun (Ls) ->
|
|
|
|
[{iolist_to_binary(S), iolist_to_binary(P)}
|
|
|
|
|| {S, P} <- Ls]
|
|
|
|
end;
|
|
|
|
mod_opt_type(ldap_search_reported) ->
|
|
|
|
fun (Ls) ->
|
|
|
|
[{iolist_to_binary(S), iolist_to_binary(P)}
|
|
|
|
|| {S, P} <- Ls]
|
|
|
|
end;
|
|
|
|
mod_opt_type(ldap_uids) ->
|
|
|
|
fun (Us) ->
|
|
|
|
lists:map(fun ({U, P}) ->
|
|
|
|
{iolist_to_binary(U), iolist_to_binary(P)};
|
|
|
|
({U}) -> {iolist_to_binary(U)}
|
|
|
|
end,
|
|
|
|
Us)
|
|
|
|
end;
|
|
|
|
mod_opt_type(ldap_vcard_map) ->
|
|
|
|
fun (Ls) ->
|
|
|
|
lists:map(fun ({S, [{P, L}]}) ->
|
|
|
|
{iolist_to_binary(S), iolist_to_binary(P),
|
|
|
|
[iolist_to_binary(E) || E <- L]}
|
|
|
|
end,
|
|
|
|
Ls)
|
|
|
|
end;
|
|
|
|
mod_opt_type(matches) ->
|
|
|
|
fun (infinity) -> 0;
|
|
|
|
(I) when is_integer(I), I > 0 -> I
|
|
|
|
end;
|
|
|
|
mod_opt_type(search) ->
|
|
|
|
fun (B) when is_boolean(B) -> B end;
|
|
|
|
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(_) ->
|
|
|
|
[host, iqdisc, ldap_filter, ldap_search_fields,
|
|
|
|
ldap_search_reported, ldap_uids, ldap_vcard_map,
|
|
|
|
matches, search, 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,
|
|
|
|
ldap_tls_verify].
|
|
|
|
|
|
|
|
opt_type(ldap_filter) -> fun check_filter/1;
|
|
|
|
opt_type(ldap_uids) ->
|
|
|
|
fun (Us) ->
|
|
|
|
lists:map(fun ({U, P}) ->
|
|
|
|
{iolist_to_binary(U), iolist_to_binary(P)};
|
|
|
|
({U}) -> {iolist_to_binary(U)}
|
|
|
|
end,
|
|
|
|
Us)
|
|
|
|
end;
|
|
|
|
opt_type(_) ->
|
|
|
|
[ldap_filter, ldap_uids, 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,
|
|
|
|
ldap_tls_verify].
|