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
|
require 'strscan'
module ActiveLdap
class DistinguishedName
include GetTextSupport
class Parser
include GetTextSupport
attr_reader :dn
def initialize(source)
@dn = nil
source = source.to_s if source.is_a?(DN)
unless source.is_a?(String)
raise DistinguishedNameInputInvalid.new(source)
end
@source = source
end
def parse
return @dn if @dn
rdns = []
scanner = StringScanner.new(@source)
scanner.scan(/\s*/)
raise rdn_is_missing if scanner.scan(/\s*\+\s*/)
raise name_component_is_missing if scanner.scan(/\s*,\s*/)
rdn = {}
until scanner.eos?
type = scan_attribute_type(scanner)
skip_attribute_type_and_value_separator(scanner)
value = scan_attribute_value(scanner)
rdn[type] = value
if scanner.scan(/\s*\+\s*/)
raise rdn_is_missing if scanner.eos?
elsif scanner.scan(/\s*\,\s*/)
rdns << rdn
rdn = {}
raise name_component_is_missing if scanner.eos?
else
scanner.scan(/\s*/)
rdns << rdn if scanner.eos?
end
end
@dn = DN.new(*rdns)
@dn
end
private
ATTRIBUTE_TYPE_RE = /\s*([a-zA-Z][a-zA-Z\d\-]*|\d+(?:\.\d+)*)\s*/
def scan_attribute_type(scanner)
raise attribute_type_is_missing unless scanner.scan(ATTRIBUTE_TYPE_RE)
scanner[1]
end
def skip_attribute_type_and_value_separator(scanner)
raise attribute_value_is_missing unless scanner.scan(/\s*=\s*/)
end
HEX_PAIR = "(?:[\\da-fA-F]{2})"
STRING_CHARS_RE = /[^,=\+<>\#;\\\"]*/ #
PAIR_RE = /\\([,=\+<>\#;]|\\|\"| |(#{HEX_PAIR}))/ #
HEX_STRING_RE = /\#(#{HEX_PAIR}+)/ #
def scan_attribute_value(scanner)
if scanner.scan(HEX_STRING_RE)
value = scanner[1].scan(/../).collect do |hex_pair|
hex_pair.hex
end.pack("C*")
elsif scanner.scan(/\"/)
value = scan_quoted_attribute_value(scanner)
else
value = scan_not_quoted_attribute_value(scanner)
end
raise attribute_value_is_missing if value.blank?
value
end
def scan_quoted_attribute_value(scanner)
result = ""
until scanner.scan(/\"/)
scanner.scan(/([^\\\"]*)/)
quoted_strings = scanner[1]
pairs = collect_pairs(scanner)
if scanner.eos? or (quoted_strings.empty? and pairs.empty?)
raise found_unmatched_quotation
end
result << quoted_strings
result << pairs
end
result
end
def scan_not_quoted_attribute_value(scanner)
result = ""
until scanner.eos?
prev_size = result.size
pairs = collect_pairs(scanner)
strings = scanner.scan(STRING_CHARS_RE)
result << pairs if !pairs.nil? and !pairs.empty?
unless strings.nil?
if scanner.peek(1) == ","
result << strings.rstrip
else
result << strings
end
end
break if prev_size == result.size
end
result
end
def collect_pairs(scanner)
result = ""
while scanner.scan(PAIR_RE)
if scanner[2]
result << [scanner[2].hex].pack("C*")
else
result << scanner[1]
end
end
result.force_encoding("utf-8") if result.respond_to?(:force_encoding)
result
end
def invalid_dn(reason)
DistinguishedNameInvalid.new(@source, reason)
end
def name_component_is_missing
invalid_dn(_("name component is missing"))
end
def rdn_is_missing
invalid_dn(_("relative distinguished name (RDN) is missing"))
end
def attribute_type_is_missing
invalid_dn(_("attribute type is missing"))
end
def attribute_value_is_missing
invalid_dn(_("attribute value is missing"))
end
def found_unmatched_quotation
invalid_dn(_("found unmatched quotation"))
end
end
class << self
def parse(source)
Parser.new(source).parse
end
def escape_value(value)
if /(\A | \z)/.match(value)
'"' + value.gsub(/([\\\"])/, '\\\\\1') + '"'
else
value.gsub(/([,=\+<>#;\\\"])/, '\\\\\1')
end
end
end
attr_reader :rdns
def initialize(*rdns)
@rdns = rdns.collect do |rdn|
if rdn.is_a?(Array) and rdn.size == 2
{rdn[0] => rdn[1]}
else
rdn
end
end
end
def blank?
@rdns.blank?
end
def +(other)
self.class.new(*(@rdns + other.rdns))
end
def -(other)
rdns = @rdns.dup
normalized_rdns = normalize(@rdns)
normalize(other.rdns).reverse_each do |rdn|
if rdn == normalized_rdns.pop
rdns.pop
else
raise ArgumentError, _("%s isn't sub DN of %s") % [other, self]
end
end
self.class.new(*rdns)
end
def <<(rdn)
@rdns << rdn
self
end
def unshift(rdn)
@rdns.unshift(rdn)
end
def parent
return nil if @rdns.size <= 1
self.class.new(*@rdns[1..-1])
end
def <=>(other)
other = DN.parse(other) if other.is_a?(String)
return nil unless other.is_a?(self.class)
normalize_for_comparing(@rdns) <=>
normalize_for_comparing(other.rdns)
end
def ==(other)
case other
when self.class
normalize(@rdns) == normalize(other.rdns)
when String
parsed_other = nil
begin
parsed_other = self.class.parse(other)
rescue DistinguishedNameInvalid
return false
end
self == parsed_other
else
false
end
end
def eql?(other)
other.is_a?(self.class) and
normalize(@rdns).to_s.eql?(normalize(other.rdns).to_s)
end
def hash
normalize(@rdns).to_s.hash
end
def to_s
klass = self.class
@rdns.collect do |rdn|
rdn.sort_by do |type, value|
type.upcase
end.collect do |type, value|
"#{type}=#{klass.escape_value(value)}"
end.join("+")
end.join(",")
end
def to_str # for backward compatibility
to_s
end
def to_human_readable_format
to_s.inspect
end
private
def normalize(rdns)
rdns.collect do |rdn|
normalized_rdn = {}
rdn.each do |key, value|
normalized_rdn[key.upcase] = value.upcase
end
normalized_rdn
end
end
def normalize_for_comparing(rdns)
normalize(rdns).collect do |rdn|
rdn.sort_by do |key, value|
key
end
end.collect do |key, value|
[key, value]
end
end
end
DN = DistinguishedName
end
|