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
|
|
|
|
2018-11-02 13:52:33 -04:00
|
|
|
require_relative 'generic'
|
2002-01-10 03:00:51 -05:00
|
|
|
|
|
|
|
module URI
|
|
|
|
|
2004-03-24 06:53:31 -05:00
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# LDAP URI SCHEMA (described in RFC2255).
|
|
|
|
#--
|
2002-01-10 03:00:51 -05:00
|
|
|
# ldap://<host>/<dn>[?<attrs>[?<scope>[?<filter>[?<extensions>]]]]
|
2018-04-21 16:04:05 -04:00
|
|
|
#++
|
2002-01-10 03:00:51 -05:00
|
|
|
class LDAP < Generic
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
2018-04-21 16:04:05 -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
|
2018-04-21 16:04:05 -04:00
|
|
|
# not including any entries under this
|
|
|
|
# * SCOPE_SUB - subtrees, 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
|
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# Creates a new URI::LDAP object from components, with syntax checking.
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# If an Array is used, the components must be passed in the
|
|
|
|
# order <code>[host, port, dn, attributes, scope, filter, extensions]</code>.
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# uri = URI::LDAP.build({:host => 'ldap.example.com',
|
2018-04-17 15:49:51 -04:00
|
|
|
# :dn => '/dc=example'})
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# uri = URI::LDAP.build(["ldap.example.com", nil,
|
2011-05-12 16:39:11 -04:00
|
|
|
# "/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
|
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# Creates a new URI::LDAP object from generic URI components as per
|
2011-05-12 16:39:11 -04:00
|
|
|
# RFC 2396. No LDAP-specific syntax checking is performed.
|
|
|
|
#
|
|
|
|
# Arguments are +scheme+, +userinfo+, +host+, +port+, +registry+, +path+,
|
2018-04-21 16:04:05 -04:00
|
|
|
# +opaque+, +query+, and +fragment+, in that order.
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
2018-04-17 15:49:51 -04:00
|
|
|
# uri = URI::LDAP.new("ldap", nil, "ldap.example.com", nil, nil,
|
|
|
|
# "/dc=example;dc=com", nil, "query", nil)
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
2018-04-21 16:04:05 -04:00
|
|
|
# See also URI::Generic.new.
|
2011-05-12 16:39:11 -04:00
|
|
|
#
|
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
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -04:00
|
|
|
# Returns dn.
|
2002-01-10 03:00:51 -05:00
|
|
|
def dn
|
|
|
|
@dn
|
|
|
|
end
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -04:00
|
|
|
# Returns attributes.
|
2002-01-10 03:00:51 -05:00
|
|
|
def attributes
|
|
|
|
@attributes
|
|
|
|
end
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -04:00
|
|
|
# Returns scope.
|
2002-01-10 03:00:51 -05:00
|
|
|
def scope
|
|
|
|
@scope
|
|
|
|
end
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -04:00
|
|
|
# Returns filter.
|
2002-01-10 03:00:51 -05:00
|
|
|
def filter
|
|
|
|
@filter
|
|
|
|
end
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -04:00
|
|
|
# Returns extensions.
|
2002-01-10 03:00:51 -05:00
|
|
|
def extensions
|
|
|
|
@extensions
|
|
|
|
end
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -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
|
|
|
|
|
2018-04-21 16:04:05 -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
|