1 |
|
%%%------------------------------------------------------------------- |
2 |
|
%%% File : eldap_pool.erl |
3 |
|
%%% Author : Evgeniy Khramtsov <xram@jabber.ru> |
4 |
|
%%% Purpose : LDAP connections pool |
5 |
|
%%% Created : 12 Nov 2006 by Evgeniy Khramtsov <xram@jabber.ru> |
6 |
|
%%% |
7 |
|
%%% |
8 |
|
%%% ejabberd, Copyright (C) 2002-2013 ProcessOne |
9 |
|
%%% |
10 |
|
%%% This program is free software; you can redistribute it and/or |
11 |
|
%%% modify it under the terms of the GNU General Public License as |
12 |
|
%%% published by the Free Software Foundation; either version 2 of the |
13 |
|
%%% License, or (at your option) any later version. |
14 |
|
%%% |
15 |
|
%%% This program is distributed in the hope that it will be useful, |
16 |
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 |
|
%%% General Public License for more details. |
19 |
|
%%% |
20 |
|
%%% You should have received a copy of the GNU General Public License |
21 |
|
%%% along with this program; if not, write to the Free Software |
22 |
|
%%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 |
|
%%% |
24 |
|
%%%------------------------------------------------------------------- |
25 |
|
|
26 |
|
-module(eldap_pool). |
27 |
|
|
28 |
|
-author('xram@jabber.ru'). |
29 |
|
|
30 |
|
%% API |
31 |
|
-export([bind/3, search/2, delete/2, add/3, modify_passwd/3]). |
32 |
|
-import(eldap_utils, [maybe_b2list/1]). |
33 |
|
-include("mongoose.hrl"). |
34 |
|
-include("eldap.hrl"). |
35 |
|
|
36 |
|
%%==================================================================== |
37 |
|
%% API |
38 |
|
%%==================================================================== |
39 |
|
|
40 |
|
bind(PoolName, DN, Passwd) -> |
41 |
2813 |
do_request(PoolName, {simple_bind, [maybe_b2list(DN), maybe_b2list(Passwd)]}). |
42 |
|
|
43 |
|
parse_search_opts(Opts) -> |
44 |
9159 |
[parse_opt(O) || O <- Opts]. |
45 |
|
|
46 |
9159 |
parse_opt({base, Bin}) -> {base, maybe_b2list(Bin)}; |
47 |
9159 |
parse_opt({attributes, BinList}) -> {attributes, [maybe_b2list(B) || B <- BinList]}; |
48 |
18708 |
parse_opt({Atom, List}) -> {Atom, List}. |
49 |
|
|
50 |
|
search(PoolName, Opts) -> |
51 |
9159 |
parse_search_result(do_request(PoolName, {search, [parse_search_opts(Opts)]})). |
52 |
|
|
53 |
|
parse_search_result({ok, #eldap_search_result{entries = Entries, referrals = Refs}}) -> |
54 |
9159 |
#eldap_search_result{entries = parse_entries(Entries), referrals = parse_refs(Refs)}; |
55 |
|
parse_search_result(R) -> |
56 |
:-( |
R. |
57 |
|
|
58 |
|
parse_entries(Entries) -> |
59 |
9159 |
[#eldap_entry{object_name = list_to_binary(Obj), attributes = parse_attrs(Attrs)} || |
60 |
9159 |
#eldap_entry{object_name = Obj, attributes = Attrs} <- Entries]. |
61 |
|
|
62 |
|
parse_attrs(Attrs) -> |
63 |
7533 |
[{list_to_binary(Name), parse_values(Values)} || {Name, Values} <- Attrs]. |
64 |
|
|
65 |
|
parse_values(Values) -> |
66 |
7648 |
[list_to_binary(V) || V <- Values]. |
67 |
|
|
68 |
9159 |
parse_refs(R) -> R. |
69 |
|
|
70 |
|
|
71 |
|
modify_passwd(PoolName, DN, Passwd) -> |
72 |
21 |
do_request(PoolName, {modify_password, [maybe_b2list(DN), maybe_b2list(Passwd)]}). |
73 |
|
|
74 |
|
|
75 |
|
delete(PoolName, DN) -> |
76 |
2286 |
case do_request(PoolName, {delete, [maybe_b2list(DN)]}) of |
77 |
:-( |
false -> not_exists; |
78 |
2286 |
R -> R |
79 |
|
end. |
80 |
|
|
81 |
|
%% Applies eldap:add/3 |
82 |
|
add(PoolName, DN, Attrs) -> |
83 |
2286 |
do_request(PoolName, {add, [maybe_b2list(DN), parse_add_atrs(Attrs)]}). |
84 |
|
|
85 |
|
parse_add_atrs(Attrs) -> |
86 |
2286 |
[parse_add_attr(A) || A <- Attrs]. |
87 |
|
|
88 |
|
parse_add_attr({N, List}) -> |
89 |
11430 |
{maybe_b2list(N), [maybe_b2list(L) || L <- List]}. |
90 |
|
|
91 |
|
|
92 |
|
%%==================================================================== |
93 |
|
%% Internal functions |
94 |
|
%%==================================================================== |
95 |
|
|
96 |
|
%% Calls mongoose_ldap_worker |
97 |
|
%% Which calls eldap:F |
98 |
|
do_request({Host, PoolTag}, {_F, _Args} = Request) -> |
99 |
16565 |
mongoose_wpool:call(ldap, Host, PoolTag, Request). |