1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
require 'active_ldap/adapter/base'
module ActiveLdap
module Adapter
class Base
class << self
def ldap_connection(options)
require 'active_ldap/adapter/ldap_ext'
Ldap.new(options)
end
end
end
class Ldap < Base
module Method
class Base
def ssl?
false
end
def start_tls?
false
end
end
class SSL < Base
def connect(host, port, options={})
LDAP::SSLConn.new(host, port, false)
end
def ssl?
true
end
end
class TLS < Base
def connect(host, port, options={})
connection = LDAP::Conn.new(host, port)
if connection.get_option(LDAP::LDAP_OPT_PROTOCOL_VERSION) < 3
connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
end
tls_options = options[:tls_options]
if tls_options and LDAP.const_defined?(:LDAP_OPT_X_TLS_NEWCTX)
tls_options.each do |key, value|
case key
when :verify_mode
case value
when :none, OpenSSL::SSL::SSL_VERIFY_NONE
connection.set_option(LDAP::LDAP_OPT_X_TLS_REQUIRE_CERT,
LDAP::LDAP_OPT_X_TLS_NEVER)
when :peer, OpenSSL::SSL::SSL_VERIFY_PEER
connection.set_option(LDAP::LDAP_OPT_X_TLS_REQUIRE_CERT,
LDAP::LDAP_OPT_X_TLS_DEMAND)
end
when :verify_hostname
unless value
connection.set_option(LDAP::LDAP_OPT_X_TLS_REQUIRE_CERT,
LDAP::LDAP_OPT_X_TLS_ALLOW)
end
end
end
connection.set_option(LDAP::LDAP_OPT_X_TLS_NEWCTX, 0)
end
connection.start_tls
connection
end
def start_tls?
true
end
end
class Plain < Base
def connect(host, port, options={})
LDAP::Conn.new(host, port)
end
end
end
def connect(options={})
super do |host, port, method|
uri = construct_uri(host, port, method.ssl?)
with_start_tls = method.start_tls?
info = {
:uri => uri,
:with_start_tls => with_start_tls,
:tls_options => @tls_options,
}
connection = log("connect", info) do
method.connect(host, port, :tls_options => @tls_options)
end
[connection, uri, with_start_tls]
end
end
def unbind(options={})
super do
execute(:unbind)
end
end
def bind(options={})
super do
@connection.error_message
end
end
def bind_as_anonymous(options={})
super do
execute(:bind, :name => "bind: anonymous")
true
end
end
def search(options={})
super(options) do |search_options|
begin
scope = search_options[:scope]
info = search_options.merge(scope: scope_name(scope))
execute(:search_full, info, search_options) do |entry|
attributes = {}
entry.attrs.each do |attr|
value = entry.vals(attr)
attributes[attr] = value if value
end
yield([entry.dn, attributes])
end
rescue RuntimeError
if $!.message == "no result returned by search"
@logger.debug do
args = [
search_options[:filter],
search_options[:attributes].inspect,
]
_("No matches: filter: %s: attributes: %s") % args
end
else
raise
end
end
end
end
def delete(targets, options={})
super do |target|
controls = options[:controls]
info = {:dn => target}
if controls
info.merge!(:name => :delete, :controls => controls)
execute(:delete_ext, info,
target, controls, [])
else
execute(:delete, info, target)
end
end
end
def add(dn, entries, options={})
super do |_dn, _entries|
controls = options[:controls]
attributes = parse_entries(_entries)
info = {:dn => _dn, :attributes => _entries}
if controls
info.merge!(:name => :add, :controls => controls)
execute(:add_ext, info, _dn, attributes, controls, [])
else
execute(:add, info, _dn, attributes)
end
end
end
def modify(dn, entries, options={})
super do |_dn, _entries|
controls = options[:controls]
attributes = parse_entries(_entries)
info = {:dn => _dn, :attributes => _entries}
if controls
info.merge!(:name => :modify, :controls => controls)
execute(:modify_ext, info, _dn, attributes, controls, [])
else
execute(:modify, info, _dn, attributes)
end
end
end
def modify_rdn(dn, new_rdn, delete_old_rdn, new_superior, options={})
super do |_dn, _new_rdn, _delete_old_rdn, _new_superior|
rename_available_p = @connection.respond_to?(:rename)
if _new_superior and not rename_available_p
raise NotImplemented.new(_("modify RDN with new superior"))
end
info = {
:name => "modify: RDN",
:dn => _dn,
:new_rdn => _new_rdn,
:new_superior => _new_superior,
:delete_old_rdn => _delete_old_rdn
}
if rename_available_p
execute(:rename, info,
_dn, _new_rdn, _new_superior, _delete_old_rdn, [], [])
else
execute(:modrdn, info, _dn, _new_rdn, _delete_old_rdn)
end
end
end
private
def prepare_connection(options={})
operation(options) do
@connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
@ldap_follow_referrals = follow_referrals?(options) ? 1 : 0
@connection.set_option(LDAP::LDAP_OPT_REFERRALS,
@ldap_follow_referrals)
end
end
def execute(method, info=nil, *args, &block)
begin
name = (info || {}).delete(:name) || method
@connection.set_option(LDAP::LDAP_OPT_REFERRALS,
@ldap_follow_referrals)
log(name, info) {@connection.send(method, *args, &block)}
rescue LDAP::ResultError
@connection.assert_error_code
raise $!.message
end
end
def ensure_method(method)
normalized_method = method.to_s.downcase
Method.constants.each do |name|
if normalized_method == name.to_s.downcase
return Method.const_get(name).new
end
end
available_methods = Method.constants.collect do |name|
name.downcase.to_sym.inspect
end.join(", ")
format = _("%s is not one of the available connect methods: %s")
raise ConfigurationError, format % [method.inspect, available_methods]
end
def ensure_scope(scope)
scope_map = {
:base => LDAP::LDAP_SCOPE_BASE,
:sub => LDAP::LDAP_SCOPE_SUBTREE,
:one => LDAP::LDAP_SCOPE_ONELEVEL,
}
value = scope_map[scope || :sub]
if value.nil?
available_scopes = scope_map.keys.inspect
format = _("%s is not one of the available LDAP scope: %s")
raise ArgumentError, format % [scope.inspect, available_scopes]
end
value
end
def scope_name(scope)
{
LDAP::LDAP_SCOPE_BASE => :base,
LDAP::LDAP_SCOPE_SUBTREE => :sub,
LDAP::LDAP_SCOPE_ONELEVEL => :one,
}[scope]
end
def sasl_bind(bind_dn, options={})
super do |_bind_dn, mechanism, quiet|
begin
_bind_dn ||= ''
sasl_quiet = @connection.sasl_quiet
@connection.sasl_quiet = quiet unless quiet.nil?
args = [_bind_dn, mechanism]
credential = nil
if need_credential_sasl_mechanism?(mechanism)
credential = password(_bind_dn, options)
end
if @sasl_options
credential ||= ""
args.concat([credential, nil, nil, @sasl_options])
else
args << credential if credential
end
info = {
:name => "bind: SASL", :dn => _bind_dn, :mechanism => mechanism
}
execute(:sasl_bind, info, *args)
true
ensure
@connection.sasl_quiet = sasl_quiet
end
end
end
def simple_bind(bind_dn, options={})
super do |_bind_dn, password|
execute(:bind, {:dn => _bind_dn}, _bind_dn, password)
true
end
end
def parse_entries(entries)
result = []
entries.each do |type, key, attributes|
mod_type = ensure_mod_type(type)
binary = schema.attribute(key).binary?
mod_type |= LDAP::LDAP_MOD_BVALUES if binary
attributes.each do |name, values|
additional_mod_type = 0
if values.any? {|value| Ldif::Attribute.binary_value?(value)}
additional_mod_type |= LDAP::LDAP_MOD_BVALUES
end
result << LDAP.mod(mod_type | additional_mod_type, name, values)
end
end
result
end
def ensure_mod_type(type)
case type
when :replace, :add, :delete
LDAP.const_get("LDAP_MOD_#{type.to_s.upcase}")
else
raise ArgumentError, _("unknown type: %s") % type
end
end
end
end
end
|