15
16:- module(ldap4pl_util, [
17 ldap_parse_search_result/3, 18 ldap_simple_auth/3, 19 ldap_add_s2/3, 20 ldap_modify_s2/3 21]).
33:- use_module(ldap4pl).
47ldap_parse_search_result(LDAP, Result, List) :-
48 iterate_entries(LDAP, Result, List).
49
50iterate_entries(LDAP, Result, List) :-
51 ( ldap_first_entry(LDAP, Result, Entry)
52 -> iterate_entries0(LDAP, Entry, List0),
53 parse_entry(LDAP, Entry, Dict),
54 List = [Dict|List0]
55 ; List = []
56 ).
57
58iterate_entries0(LDAP, Entry, List) :-
59 ( ldap_next_entry(LDAP, Entry, NextEntry)
60 -> iterate_entries0(LDAP, NextEntry, List0),
61 parse_entry(LDAP, NextEntry, Dict),
62 List = [Dict|List0]
63 ; List = []
64 ).
65
66parse_entry(LDAP, Entry, Dict) :-
67 iterate_attributes(LDAP, Entry, Attributes),
68 ldap_get_dn(LDAP, Entry, DN),
69 Dict = _{dn:DN, attributes:Attributes}.
70
71iterate_attributes(LDAP, Entry, Attributes) :-
72 ( ldap_first_attribute(LDAP, Entry, Attribute, Ber)
73 -> iterate_attributes0(LDAP, Entry, Ber, Attributes0),
74 ldap_get_values(LDAP, Entry, Attribute, Values),
75 Attributes = Attributes0.put(Attribute, Values)
76 ; Attributes = _{}
77 ).
78
79iterate_attributes0(LDAP, Entry, Ber, Attributes) :-
80 ( ldap_next_attribute(LDAP, Entry, Attribute, Ber)
81 -> iterate_attributes0(LDAP, Entry, Ber, Attributes0),
82 ldap_get_values(LDAP, Entry, Attribute, Values),
83 Attributes = Attributes0.put(Attribute, Values)
84 ; ldap_ber_free(Ber, false),
85 Attributes = _{}
86 ).
92ldap_simple_auth(URI, Who, Passwd) :-
93 setup_call_cleanup(
94 (ldap_initialize(LDAP, URI), ldap_set_option(LDAP, ldap_opt_protocol_version, 3)),
95 ldap_simple_bind_s(LDAP, Who, Passwd),
96 ldap_unbind(LDAP)
97 ).
108ldap_add_s2(LDAP, DN, Entry) :-
109 dict_pairs(Entry, _, Pairs),
110 build_ldapmod_add_list(Pairs, List),
111 ldap_add_s(LDAP, DN, List).
112
113build_ldapmod_add_list([], []) :- !.
114build_ldapmod_add_list([Attribute-V|T], List) :-
115 build_ldapmod_add_list(T, List0),
116 ( is_list(V)
117 -> Values = V
118 ; Values = [V]
119 ),
120 LDAPMod = ldapmod(mod_op([ldap_mod_add]), mod_type(Attribute), mod_values(Values)),
121 List = [LDAPMod|List0].
138ldap_modify_s2(LDAP, DN, Operations) :-
139 build_ldapmod_modify_list(Operations, List),
140 ldap_modify_s(LDAP, DN, List).
141
142build_ldapmod_modify_list([], []) :- !.
143build_ldapmod_modify_list([Op-Attribute:V|T], List) :- !,
144 build_ldapmod_modify_list(T, List0),
145 ( is_list(V)
146 -> Values = V
147 ; Values = [V]
148 ),
149 atom_concat(ldap_mod_, Op, Operation),
150 LDAPMod = ldapmod(mod_op([Operation]), mod_type(Attribute), mod_values(Values)),
151 List = [LDAPMod|List0].
152build_ldapmod_modify_list([delete-Attribute|T], List) :- !,
153 build_ldapmod_modify_list(T, List0),
154 LDAPMod = ldapmod(mod_op([ldap_mod_delete]), mod_type(Attribute)),
155 List = [LDAPMod|List0]
Utilities to make life easier.
This module provides utilities for OpenLDAP API Prolog bindings.