2003-11-23 21:11:21 +01:00
|
|
|
-module(eldap).
|
|
|
|
%%% --------------------------------------------------------------------
|
|
|
|
%%% Created: 12 Oct 2000 by Tobbe <tnt@home.se>
|
|
|
|
%%% Function: Erlang client LDAP implementation according RFC 2251.
|
|
|
|
%%% The interface is based on RFC 1823, and
|
|
|
|
%%% draft-ietf-asid-ldap-c-api-00.txt
|
|
|
|
%%%
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Copyright (C) 2000 Torbjorn Tornkvist, tnt@home.se
|
2003-11-23 21:11:21 +01:00
|
|
|
%%%
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%
|
2003-11-23 21:11:21 +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.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%% Modified by Sean Hinde <shinde@iee.org> 7th Dec 2000
|
|
|
|
%%% Turned into gen_fsm, made non-blocking, added timers etc to support this.
|
|
|
|
%%% Now has the concept of a name (string() or atom()) per instance which allows
|
|
|
|
%%% multiple users to call by name if so desired.
|
|
|
|
%%%
|
|
|
|
%%% Can be configured with start_link parameters or use a config file to get
|
|
|
|
%%% host to connect to, dn, password, log function etc.
|
|
|
|
|
|
|
|
%%% Modified by Alexey Shchepin <alexey@sevcom.net>
|
2007-01-27 17:40:37 +01:00
|
|
|
|
2010-04-19 12:57:24 +02:00
|
|
|
%%% Modified by Evgeniy Khramtsov <ekhramtsov@process-one.net>
|
2007-01-27 17:40:37 +01:00
|
|
|
%%% Implemented queue for bind() requests to prevent pending binds.
|
2010-04-19 06:08:00 +02:00
|
|
|
%%% Implemented extensibleMatch/2 function.
|
2010-04-19 12:57:24 +02:00
|
|
|
%%% Implemented LDAP Extended Operations (currently only Password Modify
|
|
|
|
%%% is supported - RFC 3062).
|
2008-03-20 17:28:36 +01:00
|
|
|
|
|
|
|
%%% Modified by Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%% Improve error case handling
|
|
|
|
|
|
|
|
%%% Modified by Mickael Remond <mremond@process-one.net>
|
|
|
|
%%% Now use ejabberd log mechanism
|
|
|
|
|
2009-05-25 19:15:48 +02:00
|
|
|
%%% Modified by:
|
|
|
|
%%% Thomas Baden <roo@ham9.net> 2008 April 6th
|
|
|
|
%%% Andy Harb <Ahmad.N.Abou-Harb@jpl.nasa.gov> 2008 April 28th
|
|
|
|
%%% Anton Podavalov <a.podavalov@gmail.com> 2009 February 22th
|
|
|
|
%%% Added LDAPS support, modeled off jungerl eldap.erl version.
|
|
|
|
%%% NOTICE: STARTTLS is not supported.
|
2008-03-20 17:28:36 +01:00
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
%%% --------------------------------------------------------------------
|
|
|
|
-vc('$Id$ ').
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% LDAP Client state machine.
|
|
|
|
%%% Possible states are:
|
|
|
|
%%% connecting - actually disconnected, but retrying periodically
|
|
|
|
%%% wait_bind_response - connected and sent bind request
|
|
|
|
%%% active - bound to LDAP Server and ready to handle commands
|
2007-01-27 17:40:37 +01:00
|
|
|
%%% active_bind - sent bind() request and waiting for response
|
2003-11-23 21:11:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
2008-03-20 17:28:36 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2008-03-20 17:28:36 +01:00
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
%% External exports
|
2009-05-25 19:15:48 +02:00
|
|
|
-export([start_link/1, start_link/6]).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([baseObject/0, singleLevel/0, wholeSubtree/0,
|
|
|
|
close/1, equalityMatch/2, greaterOrEqual/2,
|
|
|
|
lessOrEqual/2, approxMatch/2, search/2, substrings/2,
|
|
|
|
present/1, extensibleMatch/2, 'and'/1, 'or'/1, 'not'/1,
|
|
|
|
modify/3, mod_add/2, mod_delete/2, mod_replace/2, add/3,
|
|
|
|
delete/2, modify_dn/5, modify_passwd/3, bind/3]).
|
|
|
|
|
2008-03-20 17:28:36 +01:00
|
|
|
-export([get_status/1]).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, connecting/2, connecting/3,
|
|
|
|
wait_bind_response/3, active/3, active_bind/3,
|
|
|
|
handle_event/3, handle_sync_event/4, handle_info/3,
|
2015-06-03 15:05:17 +02:00
|
|
|
terminate/3, code_change/4]).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export_type([filter/0]).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
-include("ELDAPv3.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
-include("eldap.hrl").
|
|
|
|
|
|
|
|
-define(LDAP_VERSION, 3).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-03-20 17:28:36 +01:00
|
|
|
-define(RETRY_TIMEOUT, 500).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
-define(BIND_TIMEOUT, 10000).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-01-27 17:40:37 +01:00
|
|
|
-define(CMD_TIMEOUT, 100000).
|
2009-01-27 14:24:18 +01:00
|
|
|
%% Used in gen_fsm sync calls.
|
|
|
|
%% Used as a timeout for gen_tcp:send/2
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-define(CALL_TIMEOUT,
|
|
|
|
(?CMD_TIMEOUT) + (?BIND_TIMEOUT) + (?RETRY_TIMEOUT)).
|
|
|
|
|
2009-01-27 14:24:18 +01:00
|
|
|
-define(SEND_TIMEOUT, 30000).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
-define(MAX_TRANSACTION_ID, 65535).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
-define(MIN_TRANSACTION_ID, 0).
|
2009-05-16 15:18:15 +02:00
|
|
|
%% Grace period after "soft" LDAP bind errors:
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-05-16 15:18:15 +02:00
|
|
|
-define(GRACEFUL_RETRY_TIMEOUT, 5000).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(SUPPORTEDEXTENSION,
|
|
|
|
<<"1.3.6.1.4.1.1466.101.120.7">>).
|
|
|
|
|
|
|
|
-define(SUPPORTEDEXTENSIONSYNTAX,
|
|
|
|
<<"1.3.6.1.4.1.1466.115.121.1.38">>).
|
|
|
|
|
|
|
|
-define(STARTTLS, <<"1.3.6.1.4.1.1466.20037">>).
|
|
|
|
|
|
|
|
-type handle() :: pid() | atom() | binary().
|
|
|
|
|
|
|
|
-record(eldap,
|
|
|
|
{version = ?LDAP_VERSION :: non_neg_integer(),
|
|
|
|
hosts = [] :: [binary()],
|
|
|
|
host :: binary(),
|
|
|
|
port = 389 :: inet:port_number(),
|
|
|
|
sockmod = gen_tcp :: ssl | gen_tcp,
|
|
|
|
tls = none :: none | tls,
|
|
|
|
tls_options = [] :: [{cacertfile, string()} |
|
|
|
|
{depth, non_neg_integer()} |
|
|
|
|
{verify, non_neg_integer()}],
|
|
|
|
fd,
|
|
|
|
rootdn = <<"">> :: binary(),
|
|
|
|
passwd = <<"">> :: binary(),
|
|
|
|
id = 0 :: non_neg_integer(),
|
|
|
|
bind_timer = make_ref() :: reference(),
|
2015-02-11 12:08:16 +01:00
|
|
|
dict = dict:new() :: ?TDICT,
|
|
|
|
req_q = queue:new() :: ?TQUEUE}).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
start_link(Name) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Reg_name = jlib:binary_to_atom(<<"eldap_",
|
|
|
|
Name/binary>>),
|
2003-11-23 21:11:21 +01:00
|
|
|
gen_fsm:start_link({local, Reg_name}, ?MODULE, [], []).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec start_link(binary(), [binary()], inet:port_number(), binary(),
|
|
|
|
binary(), tlsopts()) -> any().
|
|
|
|
|
2010-05-10 11:37:37 +02:00
|
|
|
start_link(Name, Hosts, Port, Rootdn, Passwd, Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Reg_name = jlib:binary_to_atom(<<"eldap_",
|
|
|
|
Name/binary>>),
|
2010-05-10 11:37:37 +02:00
|
|
|
gen_fsm:start_link({local, Reg_name}, ?MODULE,
|
2013-03-14 10:33:02 +01:00
|
|
|
[Hosts, Port, Rootdn, Passwd, Opts], []).
|
|
|
|
|
|
|
|
-spec get_status(handle()) -> any().
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%% --------------------------------------------------------------------
|
|
|
|
%%% Get status of connection.
|
|
|
|
%%% --------------------------------------------------------------------
|
|
|
|
get_status(Handle) ->
|
|
|
|
Handle1 = get_handle(Handle),
|
|
|
|
gen_fsm:sync_send_all_state_event(Handle1, get_status).
|
|
|
|
|
|
|
|
%%% --------------------------------------------------------------------
|
|
|
|
%%% Shutdown connection (and process) asynchronous.
|
|
|
|
%%% --------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec close(handle()) -> any().
|
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
close(Handle) ->
|
|
|
|
Handle1 = get_handle(Handle),
|
|
|
|
gen_fsm:send_all_state_event(Handle1, close).
|
|
|
|
|
|
|
|
%%% --------------------------------------------------------------------
|
|
|
|
%%% Add an entry. The entry field MUST NOT exist for the AddRequest
|
|
|
|
%%% to succeed. The parent of the entry MUST exist.
|
|
|
|
%%% Example:
|
|
|
|
%%%
|
|
|
|
%%% add(Handle,
|
|
|
|
%%% "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com",
|
|
|
|
%%% [{"objectclass", ["person"]},
|
|
|
|
%%% {"cn", ["Bill Valentine"]},
|
|
|
|
%%% {"sn", ["Valentine"]},
|
|
|
|
%%% {"telephoneNumber", ["545 555 00"]}]
|
|
|
|
%%% )
|
|
|
|
%%% --------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
add(Handle, Entry, Attributes) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
Handle1 = get_handle(Handle),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:sync_send_event(Handle1,
|
|
|
|
{add, Entry, add_attrs(Attributes)}, ?CALL_TIMEOUT).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%% Do sanity check !
|
|
|
|
add_attrs(Attrs) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun ({Type, Vals}) ->
|
|
|
|
{'AddRequest_attributes', Type, Vals}
|
2003-11-23 21:11:21 +01:00
|
|
|
end,
|
|
|
|
case catch lists:map(F, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', _} -> throw({error, attribute_values});
|
|
|
|
Else -> Else
|
2003-11-23 21:11:21 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%% --------------------------------------------------------------------
|
|
|
|
%%% Delete an entry. The entry consists of the DN of
|
|
|
|
%%% the entry to be deleted.
|
|
|
|
%%% Example:
|
|
|
|
%%%
|
|
|
|
%%% delete(Handle,
|
|
|
|
%%% "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com"
|
|
|
|
%%% )
|
|
|
|
%%% --------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
delete(Handle, Entry) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
Handle1 = get_handle(Handle),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:sync_send_event(Handle1, {delete, Entry},
|
|
|
|
?CALL_TIMEOUT).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%% --------------------------------------------------------------------
|
|
|
|
%%% Modify an entry. Given an entry a number of modification
|
|
|
|
%%% operations can be performed as one atomic operation.
|
|
|
|
%%% Example:
|
|
|
|
%%%
|
|
|
|
%%% modify(Handle,
|
|
|
|
%%% "cn=Torbjorn Tornkvist, ou=people, o=Bluetail AB, dc=bluetail, dc=com",
|
|
|
|
%%% [replace("telephoneNumber", ["555 555 00"]),
|
|
|
|
%%% add("description", ["LDAP hacker"])]
|
|
|
|
%%% )
|
|
|
|
%%% --------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec modify(handle(), any(), [add | delete | replace]) -> any().
|
|
|
|
|
|
|
|
modify(Handle, Object, Mods) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
Handle1 = get_handle(Handle),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:sync_send_event(Handle1, {modify, Object, Mods},
|
|
|
|
?CALL_TIMEOUT).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Modification operations.
|
|
|
|
%%% Example:
|
|
|
|
%%% replace("telephoneNumber", ["555 555 00"])
|
|
|
|
%%%
|
2013-03-14 10:33:02 +01:00
|
|
|
mod_add(Type, Values) ->
|
|
|
|
m(add, Type, Values).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
mod_delete(Type, Values) ->
|
|
|
|
m(delete, Type, Values).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%% --------------------------------------------------------------------
|
|
|
|
%%% Modify an entry. Given an entry a number of modification
|
|
|
|
%%% operations can be performed as one atomic operation.
|
|
|
|
%%% Example:
|
|
|
|
%%%
|
|
|
|
%%% modify_dn(Handle,
|
|
|
|
%%% "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com",
|
|
|
|
%%% "cn=Ben Emerson",
|
|
|
|
%%% true,
|
|
|
|
%%% ""
|
|
|
|
%%% )
|
|
|
|
%%% --------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
mod_replace(Type, Values) ->
|
|
|
|
m(replace, Type, Values).
|
|
|
|
|
|
|
|
m(Operation, Type, Values) ->
|
|
|
|
#'ModifyRequest_modification_SEQOF'{operation =
|
|
|
|
Operation,
|
|
|
|
modification =
|
|
|
|
#'AttributeTypeAndValues'{type =
|
|
|
|
Type,
|
|
|
|
vals =
|
|
|
|
Values}}.
|
|
|
|
|
|
|
|
modify_dn(Handle, Entry, NewRDN, DelOldRDN, NewSup) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
Handle1 = get_handle(Handle),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:sync_send_event(Handle1,
|
|
|
|
{modify_dn, Entry, NewRDN, bool_p(DelOldRDN),
|
|
|
|
optional(NewSup)},
|
|
|
|
?CALL_TIMEOUT).
|
|
|
|
|
|
|
|
-spec modify_passwd(handle(), binary(), binary()) -> any().
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
modify_passwd(Handle, DN, Passwd) ->
|
2010-04-19 12:57:24 +02:00
|
|
|
Handle1 = get_handle(Handle),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:sync_send_event(Handle1,
|
|
|
|
{modify_passwd, DN, Passwd}, ?CALL_TIMEOUT).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%% --------------------------------------------------------------------
|
|
|
|
%%% Bind.
|
|
|
|
%%% Example:
|
|
|
|
%%%
|
|
|
|
%%% bind(Handle,
|
|
|
|
%%% "cn=Bill Valentine, ou=people, o=Bluetail AB, dc=bluetail, dc=com",
|
|
|
|
%%% "secret")
|
|
|
|
%%% --------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec bind(handle(), binary(), binary()) -> any().
|
|
|
|
|
|
|
|
bind(Handle, RootDN, Passwd) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
Handle1 = get_handle(Handle),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:sync_send_event(Handle1, {bind, RootDN, Passwd},
|
|
|
|
?CALL_TIMEOUT).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%% Sanity checks !
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
bool_p(Bool) when Bool == true; Bool == false -> Bool.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
optional([]) -> asn1_NOVALUE;
|
2003-11-23 21:11:21 +01:00
|
|
|
optional(Value) -> Value.
|
|
|
|
|
|
|
|
%%% --------------------------------------------------------------------
|
|
|
|
%%% Synchronous search of the Directory returning a
|
|
|
|
%%% requested set of attributes.
|
|
|
|
%%%
|
|
|
|
%%% Example:
|
|
|
|
%%%
|
|
|
|
%%% Filter = eldap:substrings("sn", [{any,"o"}]),
|
|
|
|
%%% eldap:search(S, [{base, "dc=bluetail, dc=com"},
|
|
|
|
%%% {filter, Filter},
|
|
|
|
%%% {attributes,["cn"]}])),
|
|
|
|
%%%
|
|
|
|
%%% Returned result: {ok, #eldap_search_result{}}
|
|
|
|
%%%
|
|
|
|
%%% Example:
|
|
|
|
%%%
|
|
|
|
%%% {ok,{eldap_search_result,
|
|
|
|
%%% [{eldap_entry,
|
|
|
|
%%% "cn=Magnus Froberg, dc=bluetail, dc=com",
|
|
|
|
%%% [{"cn",["Magnus Froberg"]}]},
|
|
|
|
%%% {eldap_entry,
|
|
|
|
%%% "cn=Torbjorn Tornkvist, dc=bluetail, dc=com",
|
|
|
|
%%% [{"cn",["Torbjorn Tornkvist"]}]}],
|
|
|
|
%%% []}}
|
|
|
|
%%%
|
|
|
|
%%% --------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
-type search_args() :: [{base, binary()} |
|
|
|
|
{filter, filter()} |
|
|
|
|
{scope, scope()} |
|
|
|
|
{attributes, [binary()]} |
|
|
|
|
{types_only, boolean()} |
|
|
|
|
{timeout, non_neg_integer()} |
|
|
|
|
{limit, non_neg_integer()} |
|
|
|
|
{deref_aliases, never | searching | finding | always}].
|
|
|
|
|
|
|
|
-spec search(handle(), eldap_search() | search_args()) -> any().
|
|
|
|
|
2009-05-06 18:54:43 +02:00
|
|
|
search(Handle, A) when is_record(A, eldap_search) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
call_search(Handle, A);
|
2009-05-06 18:54:43 +02:00
|
|
|
search(Handle, L) when is_list(L) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
case catch parse_search_args(L) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, Emsg} -> {error, Emsg};
|
|
|
|
{'EXIT', Emsg} -> {error, Emsg};
|
|
|
|
A when is_record(A, eldap_search) ->
|
|
|
|
call_search(Handle, A)
|
2003-11-23 21:11:21 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
call_search(Handle, A) ->
|
|
|
|
Handle1 = get_handle(Handle),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_fsm:sync_send_event(Handle1, {search, A},
|
|
|
|
?CALL_TIMEOUT).
|
|
|
|
|
|
|
|
-spec parse_search_args(search_args()) -> eldap_search().
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
parse_search_args(Args) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
parse_search_args(Args,
|
|
|
|
#eldap_search{scope = wholeSubtree}).
|
|
|
|
|
|
|
|
parse_search_args([{base, Base} | T], A) ->
|
|
|
|
parse_search_args(T, A#eldap_search{base = Base});
|
|
|
|
parse_search_args([{filter, Filter} | T], A) ->
|
|
|
|
parse_search_args(T, A#eldap_search{filter = Filter});
|
|
|
|
parse_search_args([{scope, Scope} | T], A) ->
|
|
|
|
parse_search_args(T, A#eldap_search{scope = Scope});
|
|
|
|
parse_search_args([{attributes, Attrs} | T], A) ->
|
|
|
|
parse_search_args(T,
|
|
|
|
A#eldap_search{attributes = Attrs});
|
|
|
|
parse_search_args([{types_only, TypesOnly} | T], A) ->
|
|
|
|
parse_search_args(T,
|
|
|
|
A#eldap_search{types_only = TypesOnly});
|
|
|
|
parse_search_args([{timeout, Timeout} | T], A)
|
|
|
|
when is_integer(Timeout) ->
|
|
|
|
parse_search_args(T, A#eldap_search{timeout = Timeout});
|
|
|
|
parse_search_args([{limit, Limit} | T], A)
|
|
|
|
when is_integer(Limit) ->
|
|
|
|
parse_search_args(T, A#eldap_search{limit = Limit});
|
|
|
|
parse_search_args([{deref_aliases, never} | T], A) ->
|
|
|
|
parse_search_args(T,
|
|
|
|
A#eldap_search{deref_aliases = neverDerefAliases});
|
|
|
|
parse_search_args([{deref_aliases, searching} | T],
|
|
|
|
A) ->
|
|
|
|
parse_search_args(T,
|
|
|
|
A#eldap_search{deref_aliases = derefInSearching});
|
|
|
|
parse_search_args([{deref_aliases, finding} | T], A) ->
|
|
|
|
parse_search_args(T,
|
|
|
|
A#eldap_search{deref_aliases = derefFindingBaseObj});
|
|
|
|
parse_search_args([{deref_aliases, always} | T], A) ->
|
|
|
|
parse_search_args(T,
|
|
|
|
A#eldap_search{deref_aliases = derefAlways});
|
|
|
|
parse_search_args([H | _], _) ->
|
|
|
|
throw({error, {unknown_arg, H}});
|
|
|
|
parse_search_args([], A) -> A.
|
|
|
|
|
|
|
|
baseObject() -> baseObject.
|
|
|
|
|
|
|
|
singleLevel() -> singleLevel.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% The Scope parameter
|
|
|
|
%%%
|
|
|
|
wholeSubtree() -> wholeSubtree.
|
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Boolean filter operations
|
|
|
|
%%%
|
2013-03-14 10:33:02 +01:00
|
|
|
-type filter() :: 'and'() | 'or'() | 'not'() | equalityMatch() |
|
|
|
|
greaterOrEqual() | lessOrEqual() | approxMatch() |
|
|
|
|
present() | substrings() | extensibleMatch().
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% The following Filter parameters consist of an attribute
|
|
|
|
%%% and an attribute value. Example: F("uid","tobbe")
|
|
|
|
%%%
|
2013-03-14 10:33:02 +01:00
|
|
|
-type 'and'() :: {'and', [filter()]}.
|
|
|
|
-spec 'and'([filter()]) -> 'and'().
|
|
|
|
|
|
|
|
'and'(ListOfFilters) when is_list(ListOfFilters) ->
|
|
|
|
{'and', ListOfFilters}.
|
|
|
|
|
|
|
|
-type 'or'() :: {'or', [filter()]}.
|
|
|
|
-spec 'or'([filter()]) -> 'or'().
|
|
|
|
|
|
|
|
'or'(ListOfFilters) when is_list(ListOfFilters) ->
|
|
|
|
{'or', ListOfFilters}.
|
|
|
|
|
|
|
|
-type 'not'() :: {'not', filter()}.
|
|
|
|
-spec 'not'(filter()) -> 'not'().
|
|
|
|
|
|
|
|
'not'(Filter) when is_tuple(Filter) -> {'not', Filter}.
|
|
|
|
|
|
|
|
-type equalityMatch() :: {equalityMatch, 'AttributeValueAssertion'()}.
|
|
|
|
-spec equalityMatch(binary(), binary()) -> equalityMatch().
|
|
|
|
|
|
|
|
equalityMatch(Desc, Value) ->
|
|
|
|
{equalityMatch, av_assert(Desc, Value)}.
|
|
|
|
|
|
|
|
-type greaterOrEqual() :: {greaterOrEqual, 'AttributeValueAssertion'()}.
|
|
|
|
-spec greaterOrEqual(binary(), binary()) -> greaterOrEqual().
|
|
|
|
|
|
|
|
greaterOrEqual(Desc, Value) ->
|
|
|
|
{greaterOrEqual, av_assert(Desc, Value)}.
|
|
|
|
|
|
|
|
-type lessOrEqual() :: {lessOrEqual, 'AttributeValueAssertion'()}.
|
|
|
|
-spec lessOrEqual(binary(), binary()) -> lessOrEqual().
|
|
|
|
|
|
|
|
lessOrEqual(Desc, Value) ->
|
|
|
|
{lessOrEqual, av_assert(Desc, Value)}.
|
|
|
|
|
|
|
|
-type approxMatch() :: {approxMatch, 'AttributeValueAssertion'()}.
|
|
|
|
-spec approxMatch(binary(), binary()) -> approxMatch().
|
|
|
|
|
|
|
|
approxMatch(Desc, Value) ->
|
|
|
|
{approxMatch, av_assert(Desc, Value)}.
|
|
|
|
|
|
|
|
-type 'AttributeValueAssertion'() ::
|
|
|
|
#'AttributeValueAssertion'{attributeDesc :: binary(),
|
|
|
|
assertionValue :: binary()}.
|
|
|
|
|
|
|
|
-spec av_assert(binary(), binary()) -> 'AttributeValueAssertion'().
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
av_assert(Desc, Value) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#'AttributeValueAssertion'{attributeDesc = Desc,
|
2003-11-23 21:11:21 +01:00
|
|
|
assertionValue = Value}.
|
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Filter to check for the presence of an attribute
|
|
|
|
%%%
|
2013-03-14 10:33:02 +01:00
|
|
|
-type present() :: {present, binary()}.
|
|
|
|
-spec present(binary()) -> present().
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% A substring filter seem to be based on a pattern:
|
|
|
|
%%%
|
|
|
|
%%% InitValue*AnyValue*FinalValue
|
|
|
|
%%%
|
|
|
|
%%% where all three parts seem to be optional (at least when
|
|
|
|
%%% talking with an OpenLDAP server). Thus, the arguments
|
|
|
|
%%% to substrings/2 looks like this:
|
|
|
|
%%%
|
|
|
|
%%% Type ::= string( <attribute> )
|
|
|
|
%%% SubStr ::= listof( {initial,Value} | {any,Value}, {final,Value})
|
|
|
|
%%%
|
|
|
|
%%% Example: substrings("sn",[{initial,"To"},{any,"kv"},{final,"st"}])
|
|
|
|
%%% will match entries containing: 'sn: Tornkvist'
|
|
|
|
%%%
|
2013-03-14 10:33:02 +01:00
|
|
|
present(Attribute) ->
|
|
|
|
{present, Attribute}.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2010-04-19 06:08:00 +02:00
|
|
|
%%%
|
|
|
|
%%% extensibleMatch filter.
|
|
|
|
%%% FIXME: Describe the purpose of this filter.
|
|
|
|
%%%
|
|
|
|
%%% Value ::= string( <attribute> )
|
|
|
|
%%% Opts ::= listof( {matchingRule, Str} | {type, Str} | {dnAttributes, true} )
|
|
|
|
%%%
|
|
|
|
%%% Example: extensibleMatch("Fred", [{matchingRule, "1.2.3.4.5"}, {type, "cn"}]).
|
|
|
|
%%%
|
2013-03-14 10:33:02 +01:00
|
|
|
-type substr() :: [{initial | any | final, binary()}].
|
|
|
|
-type 'SubstringFilter'() ::
|
|
|
|
#'SubstringFilter'{type :: binary(),
|
2015-04-21 10:49:41 +02:00
|
|
|
substrings :: substr()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-type substrings() :: {substrings, 'SubstringFilter'()}.
|
|
|
|
-spec substrings(binary(), substr()) -> substrings().
|
|
|
|
|
|
|
|
substrings(Type, SubStr) ->
|
|
|
|
{substrings,
|
2015-04-21 10:49:41 +02:00
|
|
|
#'SubstringFilter'{type = Type, substrings = SubStr}}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-type match_opts() :: [{matchingRule | type, binary()} |
|
|
|
|
{dnAttributes, boolean()}].
|
|
|
|
|
|
|
|
-type 'MatchingRuleAssertion'() ::
|
|
|
|
#'MatchingRuleAssertion'{matchValue :: binary(),
|
|
|
|
type :: asn1_NOVALUE | binary(),
|
|
|
|
matchingRule :: asn1_NOVALUE | binary(),
|
|
|
|
dnAttributes :: asn1_DEFAULT | true}.
|
|
|
|
|
|
|
|
-type extensibleMatch() :: {extensibleMatch, 'MatchingRuleAssertion'()}.
|
|
|
|
-spec extensibleMatch(binary(), match_opts()) -> extensibleMatch().
|
|
|
|
|
|
|
|
extensibleMatch(Value, Opts) ->
|
|
|
|
MRA = #'MatchingRuleAssertion'{matchValue = Value},
|
|
|
|
{extensibleMatch, extensibleMatch_opts(Opts, MRA)}.
|
|
|
|
|
|
|
|
extensibleMatch_opts([{matchingRule, Rule} | Opts], MRA) ->
|
|
|
|
extensibleMatch_opts(Opts,
|
|
|
|
MRA#'MatchingRuleAssertion'{matchingRule = Rule});
|
|
|
|
extensibleMatch_opts([{type, Desc} | Opts], MRA) ->
|
|
|
|
extensibleMatch_opts(Opts,
|
|
|
|
MRA#'MatchingRuleAssertion'{type = Desc});
|
|
|
|
extensibleMatch_opts([{dnAttributes, true} | Opts],
|
|
|
|
MRA) ->
|
|
|
|
extensibleMatch_opts(Opts,
|
|
|
|
MRA#'MatchingRuleAssertion'{dnAttributes = true});
|
2010-04-19 06:08:00 +02:00
|
|
|
extensibleMatch_opts([_ | Opts], MRA) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
extensibleMatch_opts(Opts, MRA);
|
|
|
|
extensibleMatch_opts([], MRA) -> MRA.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_handle(Pid) when is_pid(Pid) -> Pid;
|
2009-05-06 18:54:43 +02:00
|
|
|
get_handle(Atom) when is_atom(Atom) -> Atom;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_handle(Name) when is_binary(Name) ->
|
|
|
|
jlib:binary_to_atom(<<"eldap_",
|
|
|
|
Name/binary>>).
|
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, StateName, StateData} |
|
|
|
|
%% {ok, StateName, StateData, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, StopReason}
|
|
|
|
%% I use the trick of setting a timeout of 0 to pass control into the
|
|
|
|
%% process.
|
|
|
|
%%----------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
init([Hosts, Port, Rootdn, Passwd, Opts]) ->
|
|
|
|
Encrypt = case gen_mod:get_opt(encrypt, Opts,
|
|
|
|
fun(tls) -> tls;
|
|
|
|
(starttls) -> starttls;
|
|
|
|
(none) -> none
|
|
|
|
end) of
|
|
|
|
tls -> tls;
|
|
|
|
_ -> none
|
2010-05-10 11:37:37 +02:00
|
|
|
end,
|
2009-05-25 19:15:48 +02:00
|
|
|
PortTemp = case Port of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined ->
|
|
|
|
case Encrypt of
|
|
|
|
tls -> ?LDAPS_PORT;
|
|
|
|
_ -> ?LDAP_PORT
|
|
|
|
end;
|
|
|
|
PT -> PT
|
2009-05-25 19:15:48 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
CacertOpts = case gen_mod:get_opt(
|
|
|
|
tls_cacertfile, Opts,
|
|
|
|
fun(S) when is_binary(S) ->
|
|
|
|
binary_to_list(S);
|
|
|
|
(undefined) ->
|
|
|
|
undefined
|
|
|
|
end) of
|
|
|
|
undefined ->
|
|
|
|
[];
|
|
|
|
Path ->
|
|
|
|
[{cacertfile, Path}]
|
2011-07-13 07:40:27 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
DepthOpts = case gen_mod:get_opt(
|
|
|
|
tls_depth, Opts,
|
|
|
|
fun(I) when is_integer(I), I>=0 ->
|
|
|
|
I;
|
|
|
|
(undefined) ->
|
|
|
|
undefined
|
|
|
|
end) of
|
|
|
|
undefined ->
|
|
|
|
[];
|
|
|
|
Depth ->
|
|
|
|
[{depth, Depth}]
|
2011-07-13 07:40:27 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Verify = gen_mod:get_opt(tls_verify, Opts,
|
|
|
|
fun(hard) -> hard;
|
|
|
|
(soft) -> soft;
|
|
|
|
(false) -> false
|
|
|
|
end, false),
|
2011-07-13 07:40:27 +02:00
|
|
|
TLSOpts = if (Verify == hard orelse Verify == soft)
|
2013-03-14 10:33:02 +01:00
|
|
|
andalso CacertOpts == [] ->
|
|
|
|
?WARNING_MSG("TLS verification is enabled but no CA "
|
|
|
|
"certfiles configured, so verification "
|
|
|
|
"is disabled.",
|
|
|
|
[]),
|
|
|
|
[];
|
|
|
|
Verify == soft ->
|
|
|
|
[{verify, 1}] ++ CacertOpts ++ DepthOpts;
|
|
|
|
Verify == hard ->
|
|
|
|
[{verify, 2}] ++ CacertOpts ++ DepthOpts;
|
|
|
|
true -> []
|
|
|
|
end,
|
|
|
|
{ok, connecting,
|
|
|
|
#eldap{hosts = Hosts, port = PortTemp, rootdn = Rootdn,
|
|
|
|
passwd = Passwd, tls = Encrypt, tls_options = TLSOpts,
|
|
|
|
id = 0, dict = dict:new(), req_q = queue:new()},
|
|
|
|
0}.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
connecting(timeout, S) ->
|
|
|
|
{ok, NextState, NewS} = connect_bind(S),
|
|
|
|
{next_state, NextState, NewS}.
|
|
|
|
|
2009-01-27 14:24:18 +01:00
|
|
|
connecting(Event, From, S) ->
|
|
|
|
Q = queue:in({Event, From}, S#eldap.req_q),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, connecting, S#eldap{req_q = Q}}.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2009-01-27 14:24:18 +01:00
|
|
|
wait_bind_response(Event, From, S) ->
|
|
|
|
Q = queue:in({Event, From}, S#eldap.req_q),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, wait_bind_response, S#eldap{req_q = Q}}.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2007-01-27 17:40:37 +01:00
|
|
|
active_bind(Event, From, S) ->
|
2009-01-27 14:24:18 +01:00
|
|
|
Q = queue:in({Event, From}, S#eldap.req_q),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, active_bind, S#eldap{req_q = Q}}.
|
2009-01-27 14:24:18 +01:00
|
|
|
|
|
|
|
active(Event, From, S) ->
|
|
|
|
process_command(S, Event, From).
|
2007-01-27 17:40:37 +01:00
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_event/3
|
|
|
|
%% Called when gen_fsm:send_all_state_event/2 is invoked.
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 02:40:24 +01:00
|
|
|
handle_event(close, _StateName, S) ->
|
2009-05-25 19:15:48 +02:00
|
|
|
catch (S#eldap.sockmod):close(S#eldap.fd),
|
2009-01-27 14:24:18 +01:00
|
|
|
{stop, normal, S};
|
2007-12-07 02:40:24 +01:00
|
|
|
handle_event(_Event, StateName, S) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
{next_state, StateName, S}.
|
|
|
|
|
2007-12-07 02:40:24 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName, S) ->
|
|
|
|
{reply, {StateName, S}, StateName, S}.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%
|
|
|
|
%% Packets arriving in various states
|
|
|
|
%%
|
2009-05-25 19:15:48 +02:00
|
|
|
handle_info({Tag, _Socket, Data}, connecting, S)
|
|
|
|
when Tag == tcp; Tag == ssl ->
|
2015-10-07 00:06:58 +02:00
|
|
|
?DEBUG("tcp packet received when disconnected!~n~p", [Data]),
|
2003-11-23 21:11:21 +01:00
|
|
|
{next_state, connecting, S};
|
2009-05-25 19:15:48 +02:00
|
|
|
handle_info({Tag, _Socket, Data}, wait_bind_response, S)
|
|
|
|
when Tag == tcp; Tag == ssl ->
|
2003-11-23 21:11:21 +01:00
|
|
|
cancel_timer(S#eldap.bind_timer),
|
|
|
|
case catch recvd_wait_bind_response(Data, S) of
|
2013-03-14 10:33:02 +01:00
|
|
|
bound -> dequeue_commands(S);
|
|
|
|
{fail_bind, Reason} ->
|
|
|
|
report_bind_failure(S#eldap.host, S#eldap.port, Reason),
|
|
|
|
{next_state, connecting,
|
|
|
|
close_and_retry(S, ?GRACEFUL_RETRY_TIMEOUT)};
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
report_bind_failure(S#eldap.host, S#eldap.port, Reason),
|
|
|
|
{next_state, connecting, close_and_retry(S)};
|
|
|
|
{error, Reason} ->
|
|
|
|
report_bind_failure(S#eldap.host, S#eldap.port, Reason),
|
|
|
|
{next_state, connecting, close_and_retry(S)}
|
2003-11-23 21:11:21 +01:00
|
|
|
end;
|
2009-05-25 19:15:48 +02:00
|
|
|
handle_info({Tag, _Socket, Data}, StateName, S)
|
2015-10-07 00:06:58 +02:00
|
|
|
when (StateName == active orelse StateName == active_bind)
|
2013-03-14 10:33:02 +01:00
|
|
|
andalso (Tag == tcp orelse Tag == ssl) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
case catch recvd_packet(Data, S) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{response, Response, RequestType} ->
|
|
|
|
NewS = case Response of
|
|
|
|
{reply, Reply, To, S1} -> gen_fsm:reply(To, Reply), S1;
|
|
|
|
{ok, S1} -> S1
|
|
|
|
end,
|
|
|
|
if StateName == active_bind andalso
|
|
|
|
RequestType == bindRequest
|
|
|
|
orelse StateName == active ->
|
|
|
|
dequeue_commands(NewS);
|
|
|
|
true -> {next_state, StateName, NewS}
|
|
|
|
end;
|
|
|
|
_ -> {next_state, StateName, S}
|
2003-11-23 21:11:21 +01:00
|
|
|
end;
|
2009-05-25 19:15:48 +02:00
|
|
|
handle_info({Tag, _Socket}, Fsm_state, S)
|
|
|
|
when Tag == tcp_closed; Tag == ssl_closed ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?WARNING_MSG("LDAP server closed the connection: ~s:~p~nIn "
|
|
|
|
"State: ~p",
|
|
|
|
[S#eldap.host, S#eldap.port, Fsm_state]),
|
2009-01-27 14:24:18 +01:00
|
|
|
{next_state, connecting, close_and_retry(S)};
|
2009-05-25 19:15:48 +02:00
|
|
|
handle_info({Tag, _Socket, Reason}, Fsm_state, S)
|
|
|
|
when Tag == tcp_error; Tag == ssl_error ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("eldap received tcp_error: ~p~nIn State: ~p",
|
|
|
|
[Reason, Fsm_state]),
|
2009-01-27 14:24:18 +01:00
|
|
|
{next_state, connecting, close_and_retry(S)};
|
2003-11-23 21:11:21 +01:00
|
|
|
%%
|
|
|
|
%% Timers
|
|
|
|
%%
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({timeout, Timer, {cmd_timeout, Id}},
|
|
|
|
StateName, S) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
case cmd_timeout(Timer, Id, S) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, To, Reason, NewS} ->
|
|
|
|
gen_fsm:reply(To, Reason),
|
|
|
|
{next_state, StateName, NewS};
|
|
|
|
{error, _Reason} -> {next_state, StateName, S}
|
2003-11-23 21:11:21 +01:00
|
|
|
end;
|
|
|
|
handle_info({timeout, retry_connect}, connecting, S) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, NextState, NewS} = connect_bind(S),
|
2003-11-23 21:11:21 +01:00
|
|
|
{next_state, NextState, NewS};
|
2015-10-07 00:06:58 +02:00
|
|
|
handle_info({timeout, _Timer, bind_timeout}, wait_bind_response, S) ->
|
2009-01-27 14:24:18 +01:00
|
|
|
{next_state, connecting, close_and_retry(S)};
|
2003-11-23 21:11:21 +01:00
|
|
|
%%
|
|
|
|
%% Make sure we don't fill the message queue with rubbish
|
|
|
|
%%
|
|
|
|
handle_info(Info, StateName, S) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("eldap. Unexpected Info: ~p~nIn state: "
|
|
|
|
"~p~n when StateData is: ~p",
|
|
|
|
[Info, StateName, S]),
|
2003-11-23 21:11:21 +01:00
|
|
|
{next_state, StateName, S}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
terminate(_Reason, _StateName, _StatData) -> ok.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: code_change/4
|
|
|
|
%% Purpose: Convert process state when code is changed
|
|
|
|
%% Returns: {ok, NewState, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 02:40:24 +01:00
|
|
|
code_change(_OldVsn, StateName, S, _Extra) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
{ok, StateName, S}.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-01-27 14:24:18 +01:00
|
|
|
dequeue_commands(S) ->
|
|
|
|
case queue:out(S#eldap.req_q) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{{value, {Event, From}}, Q} ->
|
|
|
|
case process_command(S#eldap{req_q = Q}, Event, From) of
|
|
|
|
{_, active, NewS} -> dequeue_commands(NewS);
|
|
|
|
Res -> Res
|
|
|
|
end;
|
|
|
|
{empty, _} -> {next_state, active, S}
|
2009-01-27 14:24:18 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
process_command(S, Event, From) ->
|
|
|
|
case send_command(Event, From, S) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, NewS} ->
|
|
|
|
case Event of
|
|
|
|
{bind, _, _} -> {next_state, active_bind, NewS};
|
|
|
|
_ -> {next_state, active, NewS}
|
|
|
|
end;
|
|
|
|
{error, _Reason} ->
|
|
|
|
Q = queue:in_r({Event, From}, S#eldap.req_q),
|
|
|
|
NewS = close_and_retry(S#eldap{req_q = Q}),
|
|
|
|
{next_state, connecting, NewS}
|
2009-01-27 14:24:18 +01:00
|
|
|
end.
|
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
send_command(Command, From, S) ->
|
|
|
|
Id = bump_id(S),
|
|
|
|
{Name, Request} = gen_req(Command),
|
2013-03-14 10:33:02 +01:00
|
|
|
Message = #'LDAPMessage'{messageID = Id,
|
2003-11-23 21:11:21 +01:00
|
|
|
protocolOp = {Name, Request}},
|
2015-09-03 16:27:42 +02:00
|
|
|
?DEBUG("~p~n", [{Name, ejabberd_config:may_hide_data(Request)}]),
|
2015-10-07 00:06:58 +02:00
|
|
|
{ok, Bytes} = 'ELDAPv3':encode('LDAPMessage', Message),
|
2009-05-25 19:15:48 +02:00
|
|
|
case (S#eldap.sockmod):send(S#eldap.fd, Bytes) of
|
2013-03-14 10:33:02 +01:00
|
|
|
ok ->
|
2015-10-07 00:06:58 +02:00
|
|
|
Timer = erlang:start_timer(?CMD_TIMEOUT, self(), {cmd_timeout, Id}),
|
|
|
|
New_dict = dict:store(Id, [{Timer, Command, From, Name}], S#eldap.dict),
|
|
|
|
{ok, S#eldap{id = Id, dict = New_dict}};
|
|
|
|
Error ->
|
|
|
|
Error
|
2008-03-20 17:28:36 +01:00
|
|
|
end.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
gen_req({search, A}) ->
|
|
|
|
{searchRequest,
|
2013-03-14 10:33:02 +01:00
|
|
|
#'SearchRequest'{baseObject = A#eldap_search.base,
|
|
|
|
scope = A#eldap_search.scope,
|
2011-09-19 08:58:55 +02:00
|
|
|
derefAliases = A#eldap_search.deref_aliases,
|
2013-03-14 10:33:02 +01:00
|
|
|
sizeLimit = A#eldap_search.limit,
|
|
|
|
timeLimit = A#eldap_search.timeout,
|
|
|
|
typesOnly = A#eldap_search.types_only,
|
|
|
|
filter = A#eldap_search.filter,
|
|
|
|
attributes = A#eldap_search.attributes}};
|
2003-11-23 21:11:21 +01:00
|
|
|
gen_req({add, Entry, Attrs}) ->
|
|
|
|
{addRequest,
|
2013-03-14 10:33:02 +01:00
|
|
|
#'AddRequest'{entry = Entry, attributes = Attrs}};
|
|
|
|
gen_req({delete, Entry}) -> {delRequest, Entry};
|
2003-11-23 21:11:21 +01:00
|
|
|
gen_req({modify, Obj, Mod}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{modifyRequest,
|
|
|
|
#'ModifyRequest'{object = Obj, modification = Mod}};
|
|
|
|
gen_req({modify_dn, Entry, NewRDN, DelOldRDN,
|
|
|
|
NewSup}) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
{modDNRequest,
|
2013-03-14 10:33:02 +01:00
|
|
|
#'ModifyDNRequest'{entry = Entry, newrdn = NewRDN,
|
|
|
|
deleteoldrdn = DelOldRDN, newSuperior = NewSup}};
|
2010-04-19 12:57:24 +02:00
|
|
|
gen_req({modify_passwd, DN, Passwd}) ->
|
2015-01-15 12:46:14 +01:00
|
|
|
{ok, ReqVal} = 'ELDAPv3':encode('PasswdModifyRequestValue',
|
|
|
|
#'PasswdModifyRequestValue'{userIdentity = DN,
|
|
|
|
newPasswd =
|
|
|
|
Passwd}),
|
2010-04-19 12:57:24 +02:00
|
|
|
{extendedReq,
|
|
|
|
#'ExtendedRequest'{requestName = ?passwdModifyOID,
|
2013-03-14 10:33:02 +01:00
|
|
|
requestValue = iolist_to_binary(ReqVal)}};
|
2003-11-23 21:11:21 +01:00
|
|
|
gen_req({bind, RootDN, Passwd}) ->
|
|
|
|
{bindRequest,
|
2013-03-14 10:33:02 +01:00
|
|
|
#'BindRequest'{version = ?LDAP_VERSION, name = RootDN,
|
2003-11-23 21:11:21 +01:00
|
|
|
authentication = {simple, Passwd}}}.
|
|
|
|
|
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
%% recvd_packet
|
|
|
|
%% Deals with incoming packets in the active state
|
|
|
|
%% Will return one of:
|
|
|
|
%% {ok, NewS} - Don't reply to client yet as this is part of a search
|
|
|
|
%% result and we haven't got all the answers yet.
|
|
|
|
%% {reply, Result, From, NewS} - Reply with result to client From
|
|
|
|
%% {error, Reason}
|
|
|
|
%% {'EXIT', Reason} - Broke
|
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
recvd_packet(Pkt, S) ->
|
2015-01-15 12:46:14 +01:00
|
|
|
case 'ELDAPv3':decode('LDAPMessage', Pkt) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Msg} ->
|
|
|
|
Op = Msg#'LDAPMessage'.protocolOp,
|
|
|
|
?DEBUG("~p", [Op]),
|
|
|
|
Dict = S#eldap.dict,
|
|
|
|
Id = Msg#'LDAPMessage'.messageID,
|
|
|
|
{Timer, From, Name, Result_so_far} = get_op_rec(Id,
|
|
|
|
Dict),
|
|
|
|
Answer = case {Name, Op} of
|
|
|
|
{searchRequest, {searchResEntry, R}}
|
|
|
|
when is_record(R, 'SearchResultEntry') ->
|
|
|
|
New_dict = dict:append(Id, R, Dict),
|
|
|
|
{ok, S#eldap{dict = New_dict}};
|
|
|
|
{searchRequest, {searchResDone, Result}} ->
|
|
|
|
Reason = Result#'LDAPResult'.resultCode,
|
|
|
|
if Reason == success; Reason == sizeLimitExceeded ->
|
|
|
|
{Res, Ref} = polish(Result_so_far),
|
|
|
|
New_dict = dict:erase(Id, Dict),
|
|
|
|
cancel_timer(Timer),
|
|
|
|
{reply,
|
|
|
|
#eldap_search_result{entries = Res,
|
|
|
|
referrals = Ref},
|
|
|
|
From, S#eldap{dict = New_dict}};
|
|
|
|
true ->
|
|
|
|
New_dict = dict:erase(Id, Dict),
|
|
|
|
cancel_timer(Timer),
|
|
|
|
{reply, {error, Reason}, From,
|
|
|
|
S#eldap{dict = New_dict}}
|
|
|
|
end;
|
|
|
|
{searchRequest, {searchResRef, R}} ->
|
|
|
|
New_dict = dict:append(Id, R, Dict),
|
|
|
|
{ok, S#eldap{dict = New_dict}};
|
|
|
|
{addRequest, {addResponse, Result}} ->
|
|
|
|
New_dict = dict:erase(Id, Dict),
|
|
|
|
cancel_timer(Timer),
|
|
|
|
Reply = check_reply(Result, From),
|
|
|
|
{reply, Reply, From, S#eldap{dict = New_dict}};
|
|
|
|
{delRequest, {delResponse, Result}} ->
|
|
|
|
New_dict = dict:erase(Id, Dict),
|
|
|
|
cancel_timer(Timer),
|
|
|
|
Reply = check_reply(Result, From),
|
|
|
|
{reply, Reply, From, S#eldap{dict = New_dict}};
|
|
|
|
{modifyRequest, {modifyResponse, Result}} ->
|
|
|
|
New_dict = dict:erase(Id, Dict),
|
|
|
|
cancel_timer(Timer),
|
|
|
|
Reply = check_reply(Result, From),
|
|
|
|
{reply, Reply, From, S#eldap{dict = New_dict}};
|
|
|
|
{modDNRequest, {modDNResponse, Result}} ->
|
|
|
|
New_dict = dict:erase(Id, Dict),
|
|
|
|
cancel_timer(Timer),
|
|
|
|
Reply = check_reply(Result, From),
|
|
|
|
{reply, Reply, From, S#eldap{dict = New_dict}};
|
|
|
|
{bindRequest, {bindResponse, Result}} ->
|
|
|
|
New_dict = dict:erase(Id, Dict),
|
|
|
|
cancel_timer(Timer),
|
|
|
|
Reply = check_bind_reply(Result, From),
|
|
|
|
{reply, Reply, From, S#eldap{dict = New_dict}};
|
|
|
|
{extendedReq, {extendedResp, Result}} ->
|
|
|
|
New_dict = dict:erase(Id, Dict),
|
|
|
|
cancel_timer(Timer),
|
|
|
|
Reply = check_extended_reply(Result, From),
|
|
|
|
{reply, Reply, From, S#eldap{dict = New_dict}};
|
|
|
|
{OtherName, OtherResult} ->
|
|
|
|
New_dict = dict:erase(Id, Dict),
|
|
|
|
cancel_timer(Timer),
|
|
|
|
{reply,
|
|
|
|
{error, {invalid_result, OtherName, OtherResult}},
|
|
|
|
From, S#eldap{dict = New_dict}}
|
|
|
|
end,
|
|
|
|
{response, Answer, Name};
|
|
|
|
Error -> Error
|
2003-11-23 21:11:21 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
check_reply(#'LDAPResult'{resultCode = success},
|
|
|
|
_From) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
ok;
|
2013-03-14 10:33:02 +01:00
|
|
|
check_reply(#'LDAPResult'{resultCode = Reason},
|
|
|
|
_From) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
{error, Reason};
|
2013-03-14 10:33:02 +01:00
|
|
|
check_reply(Other, _From) -> {error, Other}.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
check_bind_reply(#'BindResponse'{resultCode = success},
|
|
|
|
_From) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
ok;
|
2013-03-14 10:33:02 +01:00
|
|
|
check_bind_reply(#'BindResponse'{resultCode = Reason},
|
|
|
|
_From) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
{error, Reason};
|
2013-03-14 10:33:02 +01:00
|
|
|
check_bind_reply(Other, _From) -> {error, Other}.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2010-04-19 12:57:24 +02:00
|
|
|
%% TODO: process reply depending on requestName:
|
|
|
|
%% this requires BER-decoding of #'ExtendedResponse'.response
|
2013-03-14 10:33:02 +01:00
|
|
|
check_extended_reply(#'ExtendedResponse'{resultCode =
|
|
|
|
success},
|
|
|
|
_From) ->
|
2010-04-19 12:57:24 +02:00
|
|
|
ok;
|
2013-03-14 10:33:02 +01:00
|
|
|
check_extended_reply(#'ExtendedResponse'{resultCode =
|
|
|
|
Reason},
|
|
|
|
_From) ->
|
2010-04-19 12:57:24 +02:00
|
|
|
{error, Reason};
|
2013-03-14 10:33:02 +01:00
|
|
|
check_extended_reply(Other, _From) -> {error, Other}.
|
2010-04-19 12:57:24 +02:00
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
get_op_rec(Id, Dict) ->
|
|
|
|
case dict:find(Id, Dict) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, [{Timer, _Command, From, Name} | Res]} ->
|
|
|
|
{Timer, From, Name, Res};
|
|
|
|
error -> throw({error, unkown_id})
|
2003-11-23 21:11:21 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
%% recvd_wait_bind_response packet
|
|
|
|
%% Deals with incoming packets in the wait_bind_response state
|
|
|
|
%% Will return one of:
|
|
|
|
%% bound - Success - move to active state
|
|
|
|
%% {fail_bind, Reason} - Failed
|
|
|
|
%% {error, Reason}
|
|
|
|
%% {'EXIT', Reason} - Broken packet
|
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
recvd_wait_bind_response(Pkt, S) ->
|
2015-01-15 12:46:14 +01:00
|
|
|
case 'ELDAPv3':decode('LDAPMessage', Pkt) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Msg} ->
|
|
|
|
?DEBUG("~p", [Msg]),
|
|
|
|
check_id(S#eldap.id, Msg#'LDAPMessage'.messageID),
|
|
|
|
case Msg#'LDAPMessage'.protocolOp of
|
|
|
|
{bindResponse, Result} ->
|
|
|
|
case Result#'BindResponse'.resultCode of
|
|
|
|
success -> bound;
|
|
|
|
Error -> {fail_bind, Error}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
Else -> {fail_bind, Else}
|
2003-11-23 21:11:21 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
check_id(Id, Id) -> ok;
|
2013-03-14 10:33:02 +01:00
|
|
|
check_id(_, _) -> throw({error, wrong_bind_id}).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
%% General Helpers
|
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
|
|
|
|
cancel_timer(Timer) ->
|
|
|
|
erlang:cancel_timer(Timer),
|
2013-03-14 10:33:02 +01:00
|
|
|
receive {timeout, Timer, _} -> ok after 0 -> ok end.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
|
2009-05-16 15:18:15 +02:00
|
|
|
close_and_retry(S, Timeout) ->
|
2009-05-25 19:15:48 +02:00
|
|
|
catch (S#eldap.sockmod):close(S#eldap.fd),
|
2013-03-14 10:33:02 +01:00
|
|
|
Queue = dict:fold(fun (_Id,
|
|
|
|
[{Timer, Command, From, _Name} | _], Q) ->
|
|
|
|
cancel_timer(Timer),
|
|
|
|
queue:in_r({Command, From}, Q);
|
|
|
|
(_, _, Q) -> Q
|
|
|
|
end,
|
|
|
|
S#eldap.req_q, S#eldap.dict),
|
|
|
|
erlang:send_after(Timeout, self(),
|
|
|
|
{timeout, retry_connect}),
|
|
|
|
S#eldap{fd = undefined, req_q = Queue, dict = dict:new()}.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2009-05-16 15:18:15 +02:00
|
|
|
close_and_retry(S) ->
|
|
|
|
close_and_retry(S, ?RETRY_TIMEOUT).
|
|
|
|
|
|
|
|
report_bind_failure(Host, Port, Reason) ->
|
|
|
|
?WARNING_MSG("LDAP bind failed on ~s:~p~nReason: ~p",
|
2013-03-14 10:33:02 +01:00
|
|
|
[Host, Port, Reason]).
|
2009-05-16 15:18:15 +02:00
|
|
|
|
2003-11-23 21:11:21 +01:00
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
%% Sort out timed out commands
|
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
cmd_timeout(Timer, Id, S) ->
|
|
|
|
Dict = S#eldap.dict,
|
|
|
|
case dict:find(Id, Dict) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, [{Timer, _Command, From, Name} | Res]} ->
|
|
|
|
case Name of
|
|
|
|
searchRequest ->
|
|
|
|
{Res1, Ref1} = polish(Res),
|
|
|
|
New_dict = dict:erase(Id, Dict),
|
|
|
|
{reply, From,
|
|
|
|
{timeout,
|
|
|
|
#eldap_search_result{entries = Res1, referrals = Ref1}},
|
|
|
|
S#eldap{dict = New_dict}};
|
|
|
|
_ ->
|
|
|
|
New_dict = dict:erase(Id, Dict),
|
|
|
|
{reply, From, {error, timeout},
|
|
|
|
S#eldap{dict = New_dict}}
|
|
|
|
end;
|
|
|
|
error -> {error, timed_out_cmd_not_in_dict}
|
2003-11-23 21:11:21 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
%% Common stuff for results
|
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
%%%
|
|
|
|
%%% Polish the returned search result
|
|
|
|
%%%
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
polish(Entries) -> polish(Entries, [], []).
|
2003-11-23 21:11:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
polish([H | T], Res, Ref)
|
|
|
|
when is_record(H, 'SearchResultEntry') ->
|
2003-11-23 21:11:21 +01:00
|
|
|
ObjectName = H#'SearchResultEntry'.objectName,
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun ({_, A, V}) -> {A, V} end,
|
2003-11-23 21:11:21 +01:00
|
|
|
Attrs = lists:map(F, H#'SearchResultEntry'.attributes),
|
2013-03-14 10:33:02 +01:00
|
|
|
polish(T,
|
|
|
|
[#eldap_entry{object_name = ObjectName,
|
|
|
|
attributes = Attrs}
|
|
|
|
| Res],
|
|
|
|
Ref);
|
|
|
|
polish([H | T], Res,
|
|
|
|
Ref) -> % No special treatment of referrals at the moment.
|
|
|
|
polish(T, Res, [H | Ref]);
|
|
|
|
polish([], Res, Ref) -> {Res, Ref}.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
%% Connect to next server in list and attempt to bind to it.
|
|
|
|
%%-----------------------------------------------------------------------
|
|
|
|
connect_bind(S) ->
|
|
|
|
Host = next_host(S#eldap.host, S#eldap.hosts),
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("LDAP connection on ~s:~p",
|
|
|
|
[Host, S#eldap.port]),
|
2011-07-13 07:40:27 +02:00
|
|
|
Opts = if S#eldap.tls == tls ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[{packet, asn1}, {active, true}, {keepalive, true},
|
|
|
|
binary
|
|
|
|
| S#eldap.tls_options];
|
|
|
|
true ->
|
|
|
|
[{packet, asn1}, {active, true}, {keepalive, true},
|
|
|
|
{send_timeout, ?SEND_TIMEOUT}, binary]
|
|
|
|
end,
|
|
|
|
HostS = binary_to_list(Host),
|
2009-05-25 19:15:48 +02:00
|
|
|
SocketData = case S#eldap.tls of
|
2013-03-14 10:33:02 +01:00
|
|
|
tls ->
|
|
|
|
SockMod = ssl, ssl:connect(HostS, S#eldap.port, Opts);
|
|
|
|
%% starttls -> %% TODO: Implement STARTTLS;
|
|
|
|
_ ->
|
|
|
|
SockMod = gen_tcp,
|
|
|
|
gen_tcp:connect(HostS, S#eldap.port, Opts)
|
2009-05-25 19:15:48 +02:00
|
|
|
end,
|
|
|
|
case SocketData of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Socket} ->
|
|
|
|
case bind_request(Socket, S#eldap{sockmod = SockMod}) of
|
|
|
|
{ok, NewS} ->
|
|
|
|
Timer = erlang:start_timer(?BIND_TIMEOUT, self(),
|
|
|
|
{timeout, bind_timeout}),
|
|
|
|
{ok, wait_bind_response,
|
|
|
|
NewS#eldap{fd = Socket, sockmod = SockMod, host = Host,
|
|
|
|
bind_timer = Timer}};
|
|
|
|
{error, Reason} ->
|
|
|
|
report_bind_failure(Host, S#eldap.port, Reason),
|
|
|
|
NewS = close_and_retry(S),
|
|
|
|
{ok, connecting, NewS#eldap{host = Host}}
|
|
|
|
end;
|
|
|
|
{error, Reason} ->
|
|
|
|
?ERROR_MSG("LDAP connection failed:~n** Server: "
|
|
|
|
"~s:~p~n** Reason: ~p~n** Socket options: ~p",
|
|
|
|
[Host, S#eldap.port, Reason, Opts]),
|
|
|
|
NewS = close_and_retry(S),
|
|
|
|
{ok, connecting, NewS#eldap{host = Host}}
|
2003-11-23 21:11:21 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
bind_request(Socket, S) ->
|
|
|
|
Id = bump_id(S),
|
2013-03-14 10:33:02 +01:00
|
|
|
Req = #'BindRequest'{version = S#eldap.version,
|
|
|
|
name = S#eldap.rootdn,
|
2003-11-23 21:11:21 +01:00
|
|
|
authentication = {simple, S#eldap.passwd}},
|
2013-03-14 10:33:02 +01:00
|
|
|
Message = #'LDAPMessage'{messageID = Id,
|
2003-11-23 21:11:21 +01:00
|
|
|
protocolOp = {bindRequest, Req}},
|
2015-09-03 16:27:42 +02:00
|
|
|
?DEBUG("Bind Request Message:~p~n", [ejabberd_config:may_hide_data(Message)]),
|
2015-10-07 00:06:58 +02:00
|
|
|
{ok, Bytes} = 'ELDAPv3':encode('LDAPMessage', Message),
|
2009-05-25 19:15:48 +02:00
|
|
|
case (S#eldap.sockmod):send(Socket, Bytes) of
|
2013-03-14 10:33:02 +01:00
|
|
|
ok -> {ok, S#eldap{id = Id}};
|
|
|
|
Error -> Error
|
2008-03-20 17:28:36 +01:00
|
|
|
end.
|
2003-11-23 21:11:21 +01:00
|
|
|
|
|
|
|
%% Given last tried Server, find next one to try
|
2013-03-14 10:33:02 +01:00
|
|
|
next_host(undefined, [H | _]) ->
|
|
|
|
H; % First time, take first
|
|
|
|
next_host(Host,
|
|
|
|
Hosts) -> % Find next in turn
|
2003-11-23 21:11:21 +01:00
|
|
|
next_host(Host, Hosts, Hosts).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
next_host(Host, [Host], Hosts) ->
|
|
|
|
hd(Hosts); % Wrap back to first
|
|
|
|
next_host(Host, [Host | Tail], _Hosts) ->
|
|
|
|
hd(Tail); % Take next
|
|
|
|
next_host(_Host, [], Hosts) ->
|
|
|
|
hd(Hosts); % Never connected before? (shouldn't happen)
|
|
|
|
next_host(Host, [_ | T], Hosts) ->
|
|
|
|
next_host(Host, T, Hosts).
|
|
|
|
|
|
|
|
bump_id(#eldap{id = Id})
|
|
|
|
when Id > (?MAX_TRANSACTION_ID) ->
|
2003-11-23 21:11:21 +01:00
|
|
|
?MIN_TRANSACTION_ID;
|
2013-03-14 10:33:02 +01:00
|
|
|
bump_id(#eldap{id = Id}) -> Id + 1.
|