powerhome-activeldap 3.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +6 -0
- data/COPYING +340 -0
- data/Gemfile +12 -0
- data/LICENSE +59 -0
- data/README.textile +140 -0
- data/TODO +32 -0
- data/benchmark/README.md +64 -0
- data/benchmark/bench-backend.rb +247 -0
- data/benchmark/bench-instantiate.rb +98 -0
- data/benchmark/config.yaml.sample +5 -0
- data/doc/text/development.textile +54 -0
- data/doc/text/news.textile +811 -0
- data/doc/text/rails.textile +144 -0
- data/doc/text/tutorial.textile +1010 -0
- data/examples/config.yaml.example +5 -0
- data/examples/example.der +0 -0
- data/examples/example.jpg +0 -0
- data/examples/groupadd +41 -0
- data/examples/groupdel +35 -0
- data/examples/groupls +49 -0
- data/examples/groupmod +42 -0
- data/examples/lpasswd +55 -0
- data/examples/objects/group.rb +13 -0
- data/examples/objects/ou.rb +4 -0
- data/examples/objects/user.rb +20 -0
- data/examples/ouadd +38 -0
- data/examples/useradd +45 -0
- data/examples/useradd-binary +53 -0
- data/examples/userdel +34 -0
- data/examples/userls +50 -0
- data/examples/usermod +42 -0
- data/examples/usermod-binary-add +50 -0
- data/examples/usermod-binary-add-time +54 -0
- data/examples/usermod-binary-del +48 -0
- data/examples/usermod-lang-add +43 -0
- data/lib/active_ldap.rb +85 -0
- data/lib/active_ldap/action_controller/ldap_benchmarking.rb +55 -0
- data/lib/active_ldap/acts/tree.rb +78 -0
- data/lib/active_ldap/adapter/base.rb +707 -0
- data/lib/active_ldap/adapter/jndi.rb +184 -0
- data/lib/active_ldap/adapter/jndi_connection.rb +185 -0
- data/lib/active_ldap/adapter/ldap.rb +290 -0
- data/lib/active_ldap/adapter/ldap_ext.rb +105 -0
- data/lib/active_ldap/adapter/net_ldap.rb +309 -0
- data/lib/active_ldap/adapter/net_ldap_ext.rb +23 -0
- data/lib/active_ldap/association/belongs_to.rb +47 -0
- data/lib/active_ldap/association/belongs_to_many.rb +58 -0
- data/lib/active_ldap/association/children.rb +21 -0
- data/lib/active_ldap/association/collection.rb +105 -0
- data/lib/active_ldap/association/has_many.rb +31 -0
- data/lib/active_ldap/association/has_many_utils.rb +44 -0
- data/lib/active_ldap/association/has_many_wrap.rb +75 -0
- data/lib/active_ldap/association/proxy.rb +107 -0
- data/lib/active_ldap/associations.rb +205 -0
- data/lib/active_ldap/attribute_methods.rb +23 -0
- data/lib/active_ldap/attribute_methods/before_type_cast.rb +24 -0
- data/lib/active_ldap/attribute_methods/dirty.rb +43 -0
- data/lib/active_ldap/attribute_methods/query.rb +31 -0
- data/lib/active_ldap/attribute_methods/read.rb +44 -0
- data/lib/active_ldap/attribute_methods/write.rb +38 -0
- data/lib/active_ldap/attributes.rb +176 -0
- data/lib/active_ldap/base.rb +1410 -0
- data/lib/active_ldap/callbacks.rb +71 -0
- data/lib/active_ldap/command.rb +49 -0
- data/lib/active_ldap/compatible.rb +44 -0
- data/lib/active_ldap/configuration.rb +147 -0
- data/lib/active_ldap/connection.rb +299 -0
- data/lib/active_ldap/distinguished_name.rb +291 -0
- data/lib/active_ldap/entry_attribute.rb +78 -0
- data/lib/active_ldap/escape.rb +12 -0
- data/lib/active_ldap/get_text.rb +20 -0
- data/lib/active_ldap/get_text/parser.rb +161 -0
- data/lib/active_ldap/helper.rb +92 -0
- data/lib/active_ldap/human_readable.rb +133 -0
- data/lib/active_ldap/ldap_error.rb +74 -0
- data/lib/active_ldap/ldif.rb +930 -0
- data/lib/active_ldap/log_subscriber.rb +50 -0
- data/lib/active_ldap/object_class.rb +95 -0
- data/lib/active_ldap/operations.rb +624 -0
- data/lib/active_ldap/persistence.rb +100 -0
- data/lib/active_ldap/populate.rb +53 -0
- data/lib/active_ldap/railtie.rb +43 -0
- data/lib/active_ldap/railties/controller_runtime.rb +48 -0
- data/lib/active_ldap/schema.rb +701 -0
- data/lib/active_ldap/schema/syntaxes.rb +422 -0
- data/lib/active_ldap/timeout.rb +75 -0
- data/lib/active_ldap/timeout_stub.rb +17 -0
- data/lib/active_ldap/user_password.rb +99 -0
- data/lib/active_ldap/validations.rb +200 -0
- data/lib/active_ldap/version.rb +3 -0
- data/lib/active_ldap/xml.rb +139 -0
- data/lib/rails/generators/active_ldap/model/USAGE +18 -0
- data/lib/rails/generators/active_ldap/model/model_generator.rb +47 -0
- data/lib/rails/generators/active_ldap/model/templates/model_active_ldap.rb +3 -0
- data/lib/rails/generators/active_ldap/scaffold/scaffold_generator.rb +14 -0
- data/lib/rails/generators/active_ldap/scaffold/templates/ldap.yml +19 -0
- data/po/en/active-ldap.po +4029 -0
- data/po/ja/active-ldap.po +4060 -0
- data/test/add-phonetic-attribute-options-to-slapd.ldif +10 -0
- data/test/al-test-utils.rb +428 -0
- data/test/command.rb +111 -0
- data/test/config.yaml.sample +6 -0
- data/test/fixtures/lower_case_object_class_schema.rb +802 -0
- data/test/run-test.rb +34 -0
- data/test/test_acts_as_tree.rb +60 -0
- data/test/test_adapter.rb +121 -0
- data/test/test_associations.rb +701 -0
- data/test/test_attributes.rb +117 -0
- data/test/test_base.rb +1214 -0
- data/test/test_base_per_instance.rb +61 -0
- data/test/test_bind.rb +62 -0
- data/test/test_callback.rb +31 -0
- data/test/test_configuration.rb +40 -0
- data/test/test_connection.rb +82 -0
- data/test/test_connection_per_class.rb +112 -0
- data/test/test_connection_per_dn.rb +112 -0
- data/test/test_dirty.rb +98 -0
- data/test/test_dn.rb +172 -0
- data/test/test_find.rb +176 -0
- data/test/test_groupadd.rb +50 -0
- data/test/test_groupdel.rb +46 -0
- data/test/test_groupls.rb +107 -0
- data/test/test_groupmod.rb +51 -0
- data/test/test_ldif.rb +1890 -0
- data/test/test_load.rb +133 -0
- data/test/test_lpasswd.rb +75 -0
- data/test/test_object_class.rb +74 -0
- data/test/test_persistence.rb +131 -0
- data/test/test_reflection.rb +175 -0
- data/test/test_schema.rb +559 -0
- data/test/test_syntax.rb +444 -0
- data/test/test_user.rb +217 -0
- data/test/test_user_password.rb +108 -0
- data/test/test_useradd-binary.rb +62 -0
- data/test/test_useradd.rb +57 -0
- data/test/test_userdel.rb +48 -0
- data/test/test_userls.rb +91 -0
- data/test/test_usermod-binary-add-time.rb +65 -0
- data/test/test_usermod-binary-add.rb +64 -0
- data/test/test_usermod-binary-del.rb +66 -0
- data/test/test_usermod-lang-add.rb +59 -0
- data/test/test_usermod.rb +58 -0
- data/test/test_validation.rb +274 -0
- metadata +379 -0
@@ -0,0 +1,184 @@
|
|
1
|
+
require 'active_ldap/adapter/base'
|
2
|
+
|
3
|
+
module ActiveLdap
|
4
|
+
module Adapter
|
5
|
+
class Base
|
6
|
+
class << self
|
7
|
+
def jndi_connection(options)
|
8
|
+
require 'active_ldap/adapter/jndi_connection'
|
9
|
+
Jndi.new(options)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Jndi < Base
|
15
|
+
METHOD = {
|
16
|
+
:ssl => :ssl,
|
17
|
+
:tls => :start_tls,
|
18
|
+
:plain => nil,
|
19
|
+
}
|
20
|
+
|
21
|
+
def connect(options={})
|
22
|
+
super do |host, port, method|
|
23
|
+
uri = construct_uri(host, port, method == :ssl)
|
24
|
+
with_start_tls = method == :start_tls
|
25
|
+
info = {:uri => uri, :with_start_tls => with_start_tls}
|
26
|
+
[log("connect", info) {JndiConnection.new(host, port, method)},
|
27
|
+
uri, with_start_tls]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def unbind(options={})
|
32
|
+
super do
|
33
|
+
execute(:unbind)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def bind_as_anonymous(options={})
|
38
|
+
super do
|
39
|
+
execute(:bind_as_anonymous, :name => "bind: anonymous")
|
40
|
+
true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def search(options={}, &block)
|
45
|
+
super(options) do |base, scope, filter, attrs, limit|
|
46
|
+
info = {
|
47
|
+
:base => base, :scope => scope_name(scope), :filter => filter,
|
48
|
+
:attributes => attrs, :limit => limit,
|
49
|
+
}
|
50
|
+
execute(:search, info, base, scope, filter, attrs, limit, &block)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def delete(targets, options={})
|
55
|
+
super do |target|
|
56
|
+
execute(:delete, {:dn => target}, target)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def add(dn, entries, options={})
|
61
|
+
super do |_dn, _entries|
|
62
|
+
info = {:dn => _dn, :attributes => _entries}
|
63
|
+
execute(:add, info, _dn, parse_entries(_entries))
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def modify(dn, entries, options={})
|
68
|
+
super do |_dn, _entries|
|
69
|
+
info = {:dn => _dn, :attributes => _entries}
|
70
|
+
execute(:modify, info, _dn, parse_entries(_entries))
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options={})
|
75
|
+
super do |_dn, _new_rdn, _delete_old_rdn, _new_superior|
|
76
|
+
info = {
|
77
|
+
:name => "modify: RDN",
|
78
|
+
:dn => _dn,
|
79
|
+
:new_rdn => _new_rdn,
|
80
|
+
:new_superior => _new_superior,
|
81
|
+
:delete_old_rdn => _delete_old_rdn
|
82
|
+
}
|
83
|
+
_new_rdn = "#{_new_rdn},#{_new_superior}" if _new_superior
|
84
|
+
execute(:modify_rdn, info, _dn, _new_rdn, _delete_old_rdn)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
def execute(method, info=nil, *args, &block)
|
90
|
+
name = (info || {}).delete(:name) || method
|
91
|
+
log(name, info) {@connection.send(method, *args, &block)}
|
92
|
+
rescue JndiConnection::NamingException
|
93
|
+
if /\[LDAP: error code (\d+) - ([^\]]+)\]/ =~ $!.to_s
|
94
|
+
message = $2
|
95
|
+
klass = LdapError::ERRORS[Integer($1)]
|
96
|
+
klass ||= ActiveLdap::LdapError
|
97
|
+
raise klass, message
|
98
|
+
end
|
99
|
+
raise
|
100
|
+
end
|
101
|
+
|
102
|
+
def ensure_method(method)
|
103
|
+
method ||= "plain"
|
104
|
+
normalized_method = method.to_s.downcase.to_sym
|
105
|
+
return METHOD[normalized_method] if METHOD.has_key?(normalized_method)
|
106
|
+
|
107
|
+
available_methods = METHOD.keys.collect {|m| m.inspect}.join(", ")
|
108
|
+
format = _("%s is not one of the available connect methods: %s")
|
109
|
+
raise ConfigurationError, format % [method.inspect, available_methods]
|
110
|
+
end
|
111
|
+
|
112
|
+
def ensure_scope(scope)
|
113
|
+
scope_map = {
|
114
|
+
:base => JndiConnection::Scope::OBJECT,
|
115
|
+
:one => JndiConnection::Scope::ONE_LEVEL,
|
116
|
+
:sub => JndiConnection::Scope::SUBTREE,
|
117
|
+
}
|
118
|
+
value = scope_map[scope || :sub]
|
119
|
+
if value.nil?
|
120
|
+
available_scopes = scope_map.keys.inspect
|
121
|
+
format = _("%s is not one of the available LDAP scope: %s")
|
122
|
+
raise ArgumentError, format % [scope.inspect, available_scopes]
|
123
|
+
end
|
124
|
+
value
|
125
|
+
end
|
126
|
+
|
127
|
+
def scope_name(scope)
|
128
|
+
{
|
129
|
+
JndiConnection::Scope::OBJECT => :base,
|
130
|
+
JndiConnection::Scope::ONE_LEVEL => :one,
|
131
|
+
JndiConnection::Scope::SUBTREE => :sub,
|
132
|
+
}[scope]
|
133
|
+
end
|
134
|
+
|
135
|
+
def sasl_bind(bind_dn, options={})
|
136
|
+
super do |_bind_dn, mechanism, quiet|
|
137
|
+
info = {
|
138
|
+
:name => "bind: SASL",
|
139
|
+
:dn => _bind_dn,
|
140
|
+
:mechanism => mechanism
|
141
|
+
}
|
142
|
+
execute(:sasl_bind, info, _bind_dn, mechanism, quiet)
|
143
|
+
true
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def simple_bind(bind_dn, options={})
|
148
|
+
super do |_bind_dn, password|
|
149
|
+
info = {:name => "bind", :dn => _bind_dn}
|
150
|
+
execute(:simple_bind, info, _bind_dn, password)
|
151
|
+
true
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def parse_entries(entries)
|
156
|
+
result = []
|
157
|
+
entries.each do |type, key, attributes|
|
158
|
+
mod_type = ensure_mod_type(type)
|
159
|
+
binary = schema.attribute(key).binary?
|
160
|
+
attributes.each do |name, values|
|
161
|
+
real_binary = binary
|
162
|
+
if values.any? {|value| Ldif::Attribute.binary_value?(value)}
|
163
|
+
real_binary = true
|
164
|
+
end
|
165
|
+
result << JndiConnection::ModifyRecord.new(mod_type, name,
|
166
|
+
values, real_binary)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
result
|
170
|
+
end
|
171
|
+
|
172
|
+
def ensure_mod_type(type)
|
173
|
+
case type
|
174
|
+
when :replace, :add
|
175
|
+
type
|
176
|
+
when :delete
|
177
|
+
:remove
|
178
|
+
else
|
179
|
+
raise ArgumentError, _("unknown type: %s") % type
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
require 'java'
|
2
|
+
|
3
|
+
java.util.Enumeration.module_eval do
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
def each
|
7
|
+
while has_more_elements
|
8
|
+
yield(next_element)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module ActiveLdap
|
14
|
+
module Adapter
|
15
|
+
class JndiConnection
|
16
|
+
HashTable = java.util.Hashtable
|
17
|
+
naming = javax.naming
|
18
|
+
directory = naming.directory
|
19
|
+
ldap = naming.ldap
|
20
|
+
InitialDirContext = directory.InitialDirContext
|
21
|
+
InitialLdapContext = ldap.InitialLdapContext
|
22
|
+
SearchControls = directory.SearchControls
|
23
|
+
ModificationItem = directory.ModificationItem
|
24
|
+
BasicAttributes = directory.BasicAttributes
|
25
|
+
Context = naming.Context
|
26
|
+
StartTlsRequest = ldap.StartTlsRequest
|
27
|
+
Control = ldap.Control
|
28
|
+
|
29
|
+
NamingException = naming.NamingException
|
30
|
+
NameNotFoundException = naming.NameNotFoundException
|
31
|
+
|
32
|
+
module Scope
|
33
|
+
OBJECT = SearchControls::OBJECT_SCOPE
|
34
|
+
ONE_LEVEL = SearchControls::ONELEVEL_SCOPE
|
35
|
+
SUBTREE = SearchControls::SUBTREE_SCOPE
|
36
|
+
end
|
37
|
+
|
38
|
+
class ModifyRecord
|
39
|
+
directory = javax.naming.directory
|
40
|
+
DirContext = directory.DirContext
|
41
|
+
BasicAttribute = directory.BasicAttribute
|
42
|
+
|
43
|
+
ADD_ATTRIBUTE = DirContext::ADD_ATTRIBUTE
|
44
|
+
REPLACE_ATTRIBUTE = DirContext::REPLACE_ATTRIBUTE
|
45
|
+
REMOVE_ATTRIBUTE = DirContext::REMOVE_ATTRIBUTE
|
46
|
+
|
47
|
+
attr_reader :type, :name, :values
|
48
|
+
def initialize(type, name, values, binary)
|
49
|
+
@type = self.class.const_get("#{type.to_s.upcase}_ATTRIBUTE")
|
50
|
+
@name = name
|
51
|
+
@values = values
|
52
|
+
@binary = binary
|
53
|
+
end
|
54
|
+
|
55
|
+
def binary?
|
56
|
+
@binary
|
57
|
+
end
|
58
|
+
|
59
|
+
def to_java_modification_item
|
60
|
+
ModificationItem.new(@type, to_java_attribute)
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_java_attribute
|
64
|
+
attribute = BasicAttribute.new(@name)
|
65
|
+
values = @values
|
66
|
+
values = values.collect(&:to_java_bytes) if binary?
|
67
|
+
values.each do |value|
|
68
|
+
attribute.add(value)
|
69
|
+
end
|
70
|
+
attribute
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def initialize(host, port, method)
|
75
|
+
@host = host
|
76
|
+
@port = port
|
77
|
+
@method = method
|
78
|
+
@context = nil
|
79
|
+
@tls = nil
|
80
|
+
end
|
81
|
+
|
82
|
+
def unbind
|
83
|
+
@tls.close if @tls
|
84
|
+
@tls = nil
|
85
|
+
@context.close if @context
|
86
|
+
@context = nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def bound?
|
90
|
+
not @context.nil?
|
91
|
+
end
|
92
|
+
|
93
|
+
def sasl_bind(bind_dn, mechanism, quiet)
|
94
|
+
setup_context(bind_dn, password, mechanism)
|
95
|
+
bound?
|
96
|
+
end
|
97
|
+
|
98
|
+
def simple_bind(bind_dn, password)
|
99
|
+
setup_context(bind_dn, password, "simple")
|
100
|
+
bound?
|
101
|
+
end
|
102
|
+
|
103
|
+
def bind_as_anonymous
|
104
|
+
setup_context(nil, nil, "none")
|
105
|
+
bound?
|
106
|
+
end
|
107
|
+
|
108
|
+
def search(base, scope, filter, attrs, limit)
|
109
|
+
controls = SearchControls.new
|
110
|
+
controls.search_scope = scope
|
111
|
+
|
112
|
+
controls.count_limit = limit if limit
|
113
|
+
unless attrs.blank?
|
114
|
+
controls.returning_attributes = attrs.to_java(:string)
|
115
|
+
end
|
116
|
+
|
117
|
+
@context.search(base, filter, controls).each do |result|
|
118
|
+
attributes = {}
|
119
|
+
result.attributes.get_all.each do |attribute|
|
120
|
+
attributes[attribute.get_id] = attribute.get_all.collect do |value|
|
121
|
+
value.is_a?(String) ? value : String.from_java_bytes(value)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
yield([result.name_in_namespace, attributes])
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def add(dn, records)
|
129
|
+
attributes = BasicAttributes.new
|
130
|
+
records.each do |record|
|
131
|
+
attributes.put(record.to_java_attribute)
|
132
|
+
end
|
133
|
+
@context.create_subcontext(dn, attributes)
|
134
|
+
end
|
135
|
+
|
136
|
+
def modify(dn, records)
|
137
|
+
items = records.collect(&:to_java_modification_item)
|
138
|
+
@context.modify_attributes(dn, items.to_java(ModificationItem))
|
139
|
+
end
|
140
|
+
|
141
|
+
def modify_rdn(dn, new_rdn, delete_old_rdn)
|
142
|
+
# should use mutex
|
143
|
+
delete_rdn_key = "java.naming.ldap.deleteRDN"
|
144
|
+
@context.add_to_environment(delete_rdn_key, delete_old_rdn.to_s)
|
145
|
+
@context.rename(dn, new_rdn)
|
146
|
+
ensure
|
147
|
+
@context.remove_from_environment(delete_rdn_key)
|
148
|
+
end
|
149
|
+
|
150
|
+
def delete(dn)
|
151
|
+
@context.destroy_subcontext(dn)
|
152
|
+
end
|
153
|
+
|
154
|
+
private
|
155
|
+
def setup_context(bind_dn, password, authentication)
|
156
|
+
unbind
|
157
|
+
environment = {
|
158
|
+
Context::INITIAL_CONTEXT_FACTORY => "com.sun.jndi.ldap.LdapCtxFactory",
|
159
|
+
Context::PROVIDER_URL => ldap_uri,
|
160
|
+
}
|
161
|
+
environment = HashTable.new(environment)
|
162
|
+
context = InitialLdapContext.new(environment, nil)
|
163
|
+
if @method == :start_tls
|
164
|
+
@tls = context.extended_operation(StartTlsRequest.new)
|
165
|
+
@tls.negotiate
|
166
|
+
end
|
167
|
+
context.add_to_environment(Context::SECURITY_AUTHENTICATION,
|
168
|
+
authentication)
|
169
|
+
if bind_dn
|
170
|
+
context.add_to_environment(Context::SECURITY_PRINCIPAL, bind_dn)
|
171
|
+
end
|
172
|
+
if password
|
173
|
+
context.add_to_environment(Context::SECURITY_CREDENTIALS, password)
|
174
|
+
end
|
175
|
+
context.reconnect(nil)
|
176
|
+
@context = context
|
177
|
+
end
|
178
|
+
|
179
|
+
def ldap_uri
|
180
|
+
protocol = @method == :ssl ? "ldaps" : "ldap"
|
181
|
+
"#{protocol}://#{@host}:#{@port}/"
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
@@ -0,0 +1,290 @@
|
|
1
|
+
require 'active_ldap/adapter/base'
|
2
|
+
|
3
|
+
module ActiveLdap
|
4
|
+
module Adapter
|
5
|
+
class Base
|
6
|
+
class << self
|
7
|
+
def ldap_connection(options)
|
8
|
+
require 'active_ldap/adapter/ldap_ext'
|
9
|
+
Ldap.new(options)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Ldap < Base
|
15
|
+
module Method
|
16
|
+
class Base
|
17
|
+
def ssl?
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
def start_tls?
|
22
|
+
false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class SSL < Base
|
27
|
+
def connect(host, port)
|
28
|
+
LDAP::SSLConn.new(host, port, false)
|
29
|
+
end
|
30
|
+
|
31
|
+
def ssl?
|
32
|
+
true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class TLS < Base
|
37
|
+
def connect(host, port)
|
38
|
+
LDAP::SSLConn.new(host, port, true)
|
39
|
+
end
|
40
|
+
|
41
|
+
def start_tls?
|
42
|
+
true
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Plain < Base
|
47
|
+
def connect(host, port)
|
48
|
+
LDAP::Conn.new(host, port)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def connect(options={})
|
54
|
+
super do |host, port, method|
|
55
|
+
uri = construct_uri(host, port, method.ssl?)
|
56
|
+
with_start_tls = method.start_tls?
|
57
|
+
info = {:uri => uri, :with_start_tls => with_start_tls}
|
58
|
+
[log("connect", info) {method.connect(host, port)},
|
59
|
+
uri, with_start_tls]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def unbind(options={})
|
64
|
+
super do
|
65
|
+
execute(:unbind)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def bind(options={})
|
70
|
+
super do
|
71
|
+
@connection.error_message
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def bind_as_anonymous(options={})
|
76
|
+
super do
|
77
|
+
execute(:bind, :name => "bind: anonymous")
|
78
|
+
true
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def search(options={})
|
83
|
+
super(options) do |base, scope, filter, attrs, limit|
|
84
|
+
begin
|
85
|
+
info = {
|
86
|
+
:base => base, :scope => scope_name(scope),
|
87
|
+
:filter => filter, :attributes => attrs, :limit => limit,
|
88
|
+
}
|
89
|
+
execute(:search_with_limit,
|
90
|
+
info, base, scope, filter, attrs, limit) do |entry|
|
91
|
+
attributes = {}
|
92
|
+
entry.attrs.each do |attr|
|
93
|
+
value = entry.vals(attr)
|
94
|
+
attributes[attr] = value if value
|
95
|
+
end
|
96
|
+
yield([entry.dn, attributes])
|
97
|
+
end
|
98
|
+
rescue RuntimeError
|
99
|
+
if $!.message == "no result returned by search"
|
100
|
+
@logger.debug do
|
101
|
+
args = [filter, attrs.inspect]
|
102
|
+
_("No matches: filter: %s: attributes: %s") % args
|
103
|
+
end
|
104
|
+
else
|
105
|
+
raise
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def delete(targets, options={})
|
112
|
+
super do |target|
|
113
|
+
controls = options[:controls]
|
114
|
+
info = {:dn => target}
|
115
|
+
if controls
|
116
|
+
info.merge!(:name => :delete, :controls => controls)
|
117
|
+
execute(:delete_ext, info,
|
118
|
+
target, controls, [])
|
119
|
+
else
|
120
|
+
execute(:delete, info, target)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def add(dn, entries, options={})
|
126
|
+
super do |_dn, _entries|
|
127
|
+
controls = options[:controls]
|
128
|
+
attributes = parse_entries(_entries)
|
129
|
+
info = {:dn => _dn, :attributes => _entries}
|
130
|
+
if controls
|
131
|
+
info.merge!(:name => :add, :controls => controls)
|
132
|
+
execute(:add_ext, info, _dn, attributes, controls, [])
|
133
|
+
else
|
134
|
+
execute(:add, info, _dn, attributes)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def modify(dn, entries, options={})
|
140
|
+
super do |_dn, _entries|
|
141
|
+
controls = options[:controls]
|
142
|
+
attributes = parse_entries(_entries)
|
143
|
+
info = {:dn => _dn, :attributes => _entries}
|
144
|
+
if controls
|
145
|
+
info.merge!(:name => :modify, :controls => controls)
|
146
|
+
execute(:modify_ext, info, _dn, attributes, controls, [])
|
147
|
+
else
|
148
|
+
execute(:modify, info, _dn, attributes)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options={})
|
154
|
+
super do |_dn, _new_rdn, _delete_old_rdn, _new_superior|
|
155
|
+
if _new_superior
|
156
|
+
raise NotImplemented.new(_("modify RDN with new superior"))
|
157
|
+
end
|
158
|
+
info = {
|
159
|
+
:name => "modify: RDN",
|
160
|
+
:dn => _dn,
|
161
|
+
:new_rdn => _new_rdn,
|
162
|
+
:new_superior => _new_superior,
|
163
|
+
:delete_old_rdn => _delete_old_rdn
|
164
|
+
}
|
165
|
+
execute(:modrdn, info, _dn, _new_rdn, _delete_old_rdn)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
private
|
170
|
+
def prepare_connection(options={})
|
171
|
+
operation(options) do
|
172
|
+
@connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def execute(method, info=nil, *args, &block)
|
177
|
+
begin
|
178
|
+
name = (info || {}).delete(:name) || method
|
179
|
+
log(name, info) {@connection.send(method, *args, &block)}
|
180
|
+
rescue LDAP::ResultError
|
181
|
+
@connection.assert_error_code
|
182
|
+
raise $!.message
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
def do_in_timeout(timeout, &block)
|
187
|
+
Timeout.timeout(timeout, &block)
|
188
|
+
end
|
189
|
+
|
190
|
+
def ensure_method(method)
|
191
|
+
normalized_method = method.to_s.downcase
|
192
|
+
Method.constants.each do |name|
|
193
|
+
if normalized_method == name.to_s.downcase
|
194
|
+
return Method.const_get(name).new
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
available_methods = Method.constants.collect do |name|
|
199
|
+
name.downcase.to_sym.inspect
|
200
|
+
end.join(", ")
|
201
|
+
format = _("%s is not one of the available connect methods: %s")
|
202
|
+
raise ConfigurationError, format % [method.inspect, available_methods]
|
203
|
+
end
|
204
|
+
|
205
|
+
def ensure_scope(scope)
|
206
|
+
scope_map = {
|
207
|
+
:base => LDAP::LDAP_SCOPE_BASE,
|
208
|
+
:sub => LDAP::LDAP_SCOPE_SUBTREE,
|
209
|
+
:one => LDAP::LDAP_SCOPE_ONELEVEL,
|
210
|
+
}
|
211
|
+
value = scope_map[scope || :sub]
|
212
|
+
if value.nil?
|
213
|
+
available_scopes = scope_map.keys.inspect
|
214
|
+
format = _("%s is not one of the available LDAP scope: %s")
|
215
|
+
raise ArgumentError, format % [scope.inspect, available_scopes]
|
216
|
+
end
|
217
|
+
value
|
218
|
+
end
|
219
|
+
|
220
|
+
def scope_name(scope)
|
221
|
+
{
|
222
|
+
LDAP::LDAP_SCOPE_BASE => :base,
|
223
|
+
LDAP::LDAP_SCOPE_SUBTREE => :sub,
|
224
|
+
LDAP::LDAP_SCOPE_ONELEVEL => :one,
|
225
|
+
}[scope]
|
226
|
+
end
|
227
|
+
|
228
|
+
def sasl_bind(bind_dn, options={})
|
229
|
+
super do |_bind_dn, mechanism, quiet|
|
230
|
+
begin
|
231
|
+
_bind_dn ||= ''
|
232
|
+
sasl_quiet = @connection.sasl_quiet
|
233
|
+
@connection.sasl_quiet = quiet unless quiet.nil?
|
234
|
+
args = [_bind_dn, mechanism]
|
235
|
+
credential = nil
|
236
|
+
if need_credential_sasl_mechanism?(mechanism)
|
237
|
+
credential = password(_bind_dn, options)
|
238
|
+
end
|
239
|
+
if @sasl_options
|
240
|
+
credential ||= ""
|
241
|
+
args.concat([credential, nil, nil, @sasl_options])
|
242
|
+
else
|
243
|
+
args << credential if credential
|
244
|
+
end
|
245
|
+
info = {
|
246
|
+
:name => "bind: SASL", :dn => _bind_dn, :mechanism => mechanism
|
247
|
+
}
|
248
|
+
execute(:sasl_bind, info, *args)
|
249
|
+
true
|
250
|
+
ensure
|
251
|
+
@connection.sasl_quiet = sasl_quiet
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
def simple_bind(bind_dn, options={})
|
257
|
+
super do |_bind_dn, password|
|
258
|
+
execute(:bind, {:dn => _bind_dn}, _bind_dn, password)
|
259
|
+
true
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
def parse_entries(entries)
|
264
|
+
result = []
|
265
|
+
entries.each do |type, key, attributes|
|
266
|
+
mod_type = ensure_mod_type(type)
|
267
|
+
binary = schema.attribute(key).binary?
|
268
|
+
mod_type |= LDAP::LDAP_MOD_BVALUES if binary
|
269
|
+
attributes.each do |name, values|
|
270
|
+
additional_mod_type = 0
|
271
|
+
if values.any? {|value| Ldif::Attribute.binary_value?(value)}
|
272
|
+
additional_mod_type |= LDAP::LDAP_MOD_BVALUES
|
273
|
+
end
|
274
|
+
result << LDAP.mod(mod_type | additional_mod_type, name, values)
|
275
|
+
end
|
276
|
+
end
|
277
|
+
result
|
278
|
+
end
|
279
|
+
|
280
|
+
def ensure_mod_type(type)
|
281
|
+
case type
|
282
|
+
when :replace, :add, :delete
|
283
|
+
LDAP.const_get("LDAP_MOD_#{type.to_s.upcase}")
|
284
|
+
else
|
285
|
+
raise ArgumentError, _("unknown type: %s") % type
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|