2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2004-03-24 06:53:31 -05:00
|
|
|
# = uri/ldap.rb
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# Author::
|
2004-03-24 06:53:31 -05:00
|
|
|
# Takaaki Tateishi <ttate@jaist.ac.jp>
|
|
|
|
# Akira Yamada <akira@ruby-lang.org>
|
2009-03-05 22:56:38 -05:00
|
|
|
# License::
|
2004-03-24 06:53:31 -05:00
|
|
|
# URI::LDAP is copyrighted free software by Takaaki Tateishi and Akira Yamada.
|
|
|
|
# You can redistribute it and/or modify it under the same term as Ruby.
|
|
|
|
# Revision:: $Id$
|
2002-01-10 03:00:51 -05:00
|
|
|
#
|
2011-05-13 16:03:21 -04:00
|
|
|
# See URI for general documentation
|
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
|
|
|
|
require 'uri/generic'
|
|
|
|
|
|
|
|
module URI
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
# LDAP URI SCHEMA (described in RFC2255)
|
|
|
|
# ldap://<host>/<dn>[?<attrs>[?<scope>[?<filter>[?<extensions>]]]]
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
class LDAP < Generic
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# A Default port of 389 for URI::LDAP
|
2002-01-10 03:00:51 -05:00
|
|
|
DEFAULT_PORT = 389
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# An Array of the available components for URI::LDAP
|
2002-01-10 03:00:51 -05:00
|
|
|
COMPONENT = [
|
|
|
|
:scheme,
|
|
|
|
:host, :port,
|
|
|
|
:dn,
|
|
|
|
:attributes,
|
|
|
|
:scope,
|
|
|
|
:filter,
|
|
|
|
:extensions,
|
|
|
|
].freeze
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# Scopes available for the starting point.
|
|
|
|
#
|
|
|
|
# * SCOPE_BASE - the Base DN
|
2011-05-13 08:54:57 -04:00
|
|
|
# * SCOPE_ONE - one level under the Base DN, not including the base DN and
|
|
|
|
# not including any entries under this.
|
|
|
|
# * SCOPE_SUB - subtress, all entries at all levels
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
SCOPE = [
|
|
|
|
SCOPE_ONE = 'one',
|
|
|
|
SCOPE_SUB = 'sub',
|
|
|
|
SCOPE_BASE = 'base',
|
|
|
|
].freeze
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
#
|
|
|
|
# Create a new URI::LDAP object from components, with syntax checking.
|
|
|
|
#
|
|
|
|
# The components accepted are host, port, dn, attributes,
|
|
|
|
# scope, filter, and extensions.
|
|
|
|
#
|
|
|
|
# The components should be provided either as an Array, or as a Hash
|
|
|
|
# with keys formed by preceding the component names with a colon.
|
|
|
|
#
|
|
|
|
# If an Array is used, the components must be passed in the order
|
|
|
|
# [host, port, dn, attributes, scope, filter, extensions].
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# newuri = URI::LDAP.build({:host => 'ldap.example.com',
|
|
|
|
# :dn> => '/dc=example'})
|
|
|
|
#
|
|
|
|
# newuri = URI::LDAP.build(["ldap.example.com", nil,
|
|
|
|
# "/dc=example;dc=com", "query", nil, nil, nil])
|
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
def self.build(args)
|
|
|
|
tmp = Util::make_components_hash(self, args)
|
|
|
|
|
|
|
|
if tmp[:dn]
|
2004-03-24 06:53:31 -05:00
|
|
|
tmp[:path] = tmp[:dn]
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
query = []
|
|
|
|
[:extensions, :filter, :scope, :attributes].collect do |x|
|
2004-03-24 06:53:31 -05:00
|
|
|
next if !tmp[x] && query.size == 0
|
|
|
|
query.unshift(tmp[x])
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
tmp[:query] = query.join('?')
|
|
|
|
|
|
|
|
return super(tmp)
|
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
#
|
|
|
|
# Create a new URI::LDAP object from generic URI components as per
|
|
|
|
# RFC 2396. No LDAP-specific syntax checking is performed.
|
|
|
|
#
|
|
|
|
# Arguments are +scheme+, +userinfo+, +host+, +port+, +registry+, +path+,
|
|
|
|
# +opaque+, +query+ and +fragment+, in that order.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# uri = URI::LDAP.new("ldap", nil, "ldap.example.com", nil,
|
|
|
|
# "/dc=example;dc=com", "query", nil, nil, nil, nil)
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# See also URI::Generic.new
|
|
|
|
#
|
2002-01-10 03:00:51 -05:00
|
|
|
def initialize(*arg)
|
|
|
|
super(*arg)
|
|
|
|
|
|
|
|
if @fragment
|
2004-03-24 06:53:31 -05:00
|
|
|
raise InvalidURIError, 'bad LDAP URL'
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
parse_dn
|
|
|
|
parse_query
|
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# private method to cleanup +dn+ from using the +path+ component attribute
|
2002-01-10 03:00:51 -05:00
|
|
|
def parse_dn
|
|
|
|
@dn = @path[1..-1]
|
|
|
|
end
|
|
|
|
private :parse_dn
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# private method to cleanup +attributes+, +scope+, +filter+ and +extensions+,
|
|
|
|
# from using the +query+ component attribute
|
2002-01-10 03:00:51 -05:00
|
|
|
def parse_query
|
|
|
|
@attributes = nil
|
|
|
|
@scope = nil
|
|
|
|
@filter = nil
|
|
|
|
@extensions = nil
|
|
|
|
|
|
|
|
if @query
|
2004-03-24 06:53:31 -05:00
|
|
|
attrs, scope, filter, extensions = @query.split('?')
|
2002-01-10 03:00:51 -05:00
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
@attributes = attrs if attrs && attrs.size > 0
|
|
|
|
@scope = scope if scope && scope.size > 0
|
|
|
|
@filter = filter if filter && filter.size > 0
|
|
|
|
@extensions = extensions if extensions && extensions.size > 0
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
private :parse_query
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# private method to assemble +query+ from +attributes+, +scope+, +filter+ and +extensions+.
|
2002-01-10 03:00:51 -05:00
|
|
|
def build_path_query
|
|
|
|
@path = '/' + @dn
|
|
|
|
|
|
|
|
query = []
|
|
|
|
[@extensions, @filter, @scope, @attributes].each do |x|
|
2004-03-24 06:53:31 -05:00
|
|
|
next if !x && query.size == 0
|
|
|
|
query.unshift(x)
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
@query = query.join('?')
|
|
|
|
end
|
|
|
|
private :build_path_query
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# returns dn.
|
2002-01-10 03:00:51 -05:00
|
|
|
def dn
|
|
|
|
@dn
|
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# private setter for dn +val+
|
2002-01-10 03:00:51 -05:00
|
|
|
def set_dn(val)
|
|
|
|
@dn = val
|
|
|
|
build_path_query
|
2002-10-04 02:26:45 -04:00
|
|
|
@dn
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
protected :set_dn
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# setter for dn +val+
|
2002-01-10 03:00:51 -05:00
|
|
|
def dn=(val)
|
|
|
|
set_dn(val)
|
2003-02-14 00:32:48 -05:00
|
|
|
val
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# returns attributes.
|
2002-01-10 03:00:51 -05:00
|
|
|
def attributes
|
|
|
|
@attributes
|
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# private setter for attributes +val+
|
2002-01-10 03:00:51 -05:00
|
|
|
def set_attributes(val)
|
|
|
|
@attributes = val
|
|
|
|
build_path_query
|
2002-10-04 02:26:45 -04:00
|
|
|
@attributes
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
protected :set_attributes
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# setter for attributes +val+
|
2002-01-10 03:00:51 -05:00
|
|
|
def attributes=(val)
|
|
|
|
set_attributes(val)
|
2003-02-14 00:32:48 -05:00
|
|
|
val
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# returns scope.
|
2002-01-10 03:00:51 -05:00
|
|
|
def scope
|
|
|
|
@scope
|
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# private setter for scope +val+
|
2002-01-10 03:00:51 -05:00
|
|
|
def set_scope(val)
|
|
|
|
@scope = val
|
|
|
|
build_path_query
|
2002-10-04 02:26:45 -04:00
|
|
|
@scope
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
protected :set_scope
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# setter for scope +val+
|
2002-01-10 03:00:51 -05:00
|
|
|
def scope=(val)
|
|
|
|
set_scope(val)
|
2003-02-14 00:32:48 -05:00
|
|
|
val
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# returns filter.
|
2002-01-10 03:00:51 -05:00
|
|
|
def filter
|
|
|
|
@filter
|
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# private setter for filter +val+
|
2002-01-10 03:00:51 -05:00
|
|
|
def set_filter(val)
|
|
|
|
@filter = val
|
|
|
|
build_path_query
|
2002-10-04 02:26:45 -04:00
|
|
|
@filter
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
protected :set_filter
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# setter for filter +val+
|
2002-01-10 03:00:51 -05:00
|
|
|
def filter=(val)
|
|
|
|
set_filter(val)
|
2003-02-14 00:32:48 -05:00
|
|
|
val
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# returns extensions.
|
2002-01-10 03:00:51 -05:00
|
|
|
def extensions
|
|
|
|
@extensions
|
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# private setter for extensions +val+
|
2002-01-10 03:00:51 -05:00
|
|
|
def set_extensions(val)
|
|
|
|
@extensions = val
|
|
|
|
build_path_query
|
2002-10-04 02:26:45 -04:00
|
|
|
@extensions
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
protected :set_extensions
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# setter for extensions +val+
|
2002-01-10 03:00:51 -05:00
|
|
|
def extensions=(val)
|
|
|
|
set_extensions(val)
|
2003-02-14 00:32:48 -05:00
|
|
|
val
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
2011-05-12 16:39:11 -04:00
|
|
|
# Checks if URI has a path
|
|
|
|
# For URI::LDAP this will return +false+
|
2004-07-17 09:14:35 -04:00
|
|
|
def hierarchical?
|
|
|
|
false
|
|
|
|
end
|
2002-01-10 03:00:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@@schemes['LDAP'] = LDAP
|
|
|
|
end
|