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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
|
module ActiveLdap
class Schema
include GetTextSupport
def initialize(entries)
@entries = normalize_entries(entries || {})
@schema_info = {}
@class_attributes_info = {}
@cache = {}
end
def ids(group)
ensure_parse(group)
info, ids, aliases = ensure_schema_info(group)
_ = info = aliases # for suppress a warning on Ruby 1.9.3
ids.keys
end
def names(group)
alias_map(group).keys
end
def exist_name?(group, name)
alias_map(group).has_key?(normalize_schema_name(name))
end
def resolve_name(group, name)
alias_map(group)[normalize_schema_name(name)]
end
# fetch
#
# This is just like LDAP::Schema#attribute except that it allows
# look up in any of the given keys.
# e.g.
# fetch('attributeTypes', 'cn', 'DESC')
# fetch('ldapSyntaxes', '1.3.6.1.4.1.1466.115.121.1.5', 'DESC')
def fetch(group, id_or_name, attribute_name)
return [] if attribute_name.empty?
attribute_name = normalize_attribute_name(attribute_name)
value = entry(group, id_or_name)[attribute_name]
value ? value.dup : []
end
alias_method :[], :fetch
NUMERIC_OID_RE = "\\d[\\d\\.]+"
DESCRIPTION_RE = "[a-zA-Z][a-zA-Z\\d\\-]*"
OID_RE = "(?:#{NUMERIC_OID_RE}|#{DESCRIPTION_RE}-oid)"
def entry(group, id_or_name)
return {} if group.empty? or id_or_name.empty?
unless @entries.has_key?(group)
raise ArgumentError, _("Unknown schema group: %s") % group
end
# Initialize anything that is required
info, ids, aliases = ensure_schema_info(group)
_ = info # for suppress a warning on Ruby 1.9.3
id, name = determine_id_or_name(id_or_name, aliases)
# Check already parsed options first
return ids[id] if ids.has_key?(id)
schemata = @entries[group] || []
while schema = schemata.shift
next unless /\A\s*\(\s*(#{OID_RE})\s*(.*)\s*\)\s*\z/ =~ schema
schema_id = $1
rest = $2
if ids.has_key?(schema_id)
attributes = ids[schema_id]
else
attributes = {}
ids[schema_id] = attributes
end
parse_attributes(rest, attributes)
(attributes["NAME"] || []).each do |v|
normalized_name = normalize_schema_name(v)
aliases[normalized_name] = schema_id
id = schema_id if id.nil? and name == normalized_name
end
break if id == schema_id
end
ids[id || aliases[name]] || {}
end
def attribute(name)
name = name.to_s if name.is_a?(Symbol)
cache([:attribute, name]) do
Attribute.new(name, self)
end
end
def attributes
cache([:attributes]) do
names("attributeTypes").collect do |name|
attribute(name)
end
end
end
def attribute_type(name, attribute_name)
cache([:attribute_type, name, attribute_name]) do
fetch("attributeTypes", name, attribute_name)
end
end
def object_class(name)
cache([:object_class, name]) do
ObjectClass.new(name, self)
end
end
def object_classes
cache([:object_classes]) do
names("objectClasses").collect do |name|
object_class(name)
end
end
end
def object_class_attribute(name, attribute_name)
cache([:object_class_attribute, name, attribute_name]) do
fetch("objectClasses", name, attribute_name)
end
end
def dit_content_rule_attribute(name, attribute_name)
cache([:dit_content_rule_attribute, name, attribute_name]) do
fetch("dITContentRules", name, attribute_name)
end
end
def ldap_syntax(name)
cache([:ldap_syntax, name]) do
Syntax.new(name, self)
end
end
def ldap_syntaxes
cache([:ldap_syntaxes]) do
ids("ldapSyntaxes").collect do |id|
ldap_syntax(id)
end
end
end
def ldap_syntax_attribute(name, attribute_name)
cache([:ldap_syntax_attribute, name, attribute_name]) do
fetch("ldapSyntaxes", name, attribute_name)
end
end
def dump(output=nil)
require 'pp'
output ||= STDOUT
if output.respond_to?(:write)
PP.pp(@entries, output)
else
open(output, "w") {|out| PP.pp(@entries, out)}
end
nil
end
private
def cache(key)
(@cache[key] ||= [yield])[0]
end
def ensure_schema_info(group)
@schema_info[group] ||= {:ids => {}, :aliases => {}}
info = @schema_info[group]
[info, info[:ids], info[:aliases]]
end
def determine_id_or_name(id_or_name, aliases)
if /\A[\d\.]+\z/ =~ id_or_name
id = id_or_name
name = nil
else
name = normalize_schema_name(id_or_name)
id = aliases[name]
end
[id, name]
end
# from RFC 2252
attribute_type_description_reserved_names =
["NAME", "DESC", "OBSOLETE", "SUP", "EQUALITY", "ORDERING", "SUBSTR",
"SYNTAX", "SINGLE-VALUE", "COLLECTIVE", "NO-USER-MODIFICATION", "USAGE"]
syntax_description_reserved_names = ["DESC"]
object_class_description_reserved_names =
["NAME", "DESC", "OBSOLETE", "SUP", "ABSTRACT", "STRUCTURAL",
"AUXILIARY", "MUST", "MAY"]
matching_rule_description_reserved_names =
["NAME", "DESC", "OBSOLETE", "SYNTAX"]
matching_rule_use_description_reserved_names =
["NAME", "DESC", "OBSOLETE", "APPLIES"]
private_experiment_reserved_names = ["X-[A-Z\\-_]+"]
reserved_names =
(attribute_type_description_reserved_names +
syntax_description_reserved_names +
object_class_description_reserved_names +
matching_rule_description_reserved_names +
matching_rule_use_description_reserved_names +
private_experiment_reserved_names).uniq
RESERVED_NAMES_RE = /(?:#{reserved_names.join('|')})/
def parse_attributes(str, attributes)
str.scan(/([A-Z\-_]+)\s+
(?:\(\s*(\w[\w\-;]*(?:\s+\$\s+\w[\w\-;]*)*)\s*\)|
\(\s*([^\)]*)\s*\)|
'([^\']*)'|
((?!#{RESERVED_NAMES_RE})[a-zA-Z][a-zA-Z\d\-;]*)|
(\d[\d\.\{\}]+)|
()
)/x
) do |name, multi_amp, multi, string, literal, syntax, no_value|
case
when multi_amp
values = multi_amp.rstrip.split(/\s*\$\s*/)
when multi
values = multi.scan(/\s*'([^\']*)'\s*/).collect {|value| value[0]}
when string
values = [string]
when literal
values = [literal]
when syntax
values = [syntax]
when no_value
values = ["TRUE"]
end
attributes[normalize_attribute_name(name)] ||= []
attributes[normalize_attribute_name(name)].concat(values)
end
end
def alias_map(group)
ensure_parse(group)
return {} if @schema_info[group].nil?
@schema_info[group][:aliases] || {}
end
def ensure_parse(group)
return if @entries[group].nil?
unless @entries[group].empty?
fetch(group, 'nonexistent', 'nonexistent')
end
end
def normalize_schema_name(name)
name.downcase.sub(/;.*$/, '')
end
def normalize_attribute_name(name)
name.upcase.gsub(/_/, "-")
end
def default_entries
{
"objectClasses" => [],
"attributeTypes" => [],
"ldapSyntaxes" => [],
"dITContentRules" => [],
"matchingRules" => [],
}
end
def normalize_entries(entries)
normalized_entries = default_entries
normalized_keys = normalized_entries.keys
entries.each do |name, values|
normalized_name = normalized_keys.find do |key|
key.downcase == name
end
normalized_entries[normalized_name || name] = values
end
normalized_entries
end
class Entry
include Comparable
attr_reader :id, :name, :aliases, :description
def initialize(name, schema, group)
@schema = schema
@name, *@aliases = attribute("NAME", name)
@name ||= name
@id = @schema.resolve_name(group, @name)
collect_info
@schema = nil
end
def eql?(other)
self.class == other.class and
(id == other.id or
(id.nil? and other.nil? and name == other.name))
end
def hash
id.nil? ? name.hash : id.hash
end
def <=>(other)
name <=> other.name
end
def to_param
name
end
end
class Syntax < Entry
attr_reader :length
def initialize(id, schema)
if /\{(\d+)\}\z/ =~ id
id = $PREMATCH
@length = Integer($1)
else
@length = nil
end
super(id, schema, "ldapSyntaxes")
@id = id
@name = nil if @name == @id
@built_in_syntax = Syntaxes[@id]
end
def binary?
return true if @built_in_syntax and @built_in_syntax.binary?
binary_transfer_required? or !human_readable?
end
def binary_transfer_required?
@binary_transfer_required
end
def human_readable?
@human_readable
end
def valid?(value)
validate(value).nil?
end
def validate(value)
if @built_in_syntax
@built_in_syntax.validate(value)
else
nil
end
end
def type_cast(value)
if @built_in_syntax
@built_in_syntax.type_cast(value)
else
value
end
end
def normalize_value(value)
if @built_in_syntax
@built_in_syntax.normalize_value(value)
else
value
end
end
def <=>(other)
id <=> other.id
end
def to_param
id
end
private
def attribute(attribute_name, name=@name)
@schema.ldap_syntax_attribute(name, attribute_name)
end
def collect_info
@description = attribute("DESC")[0]
@binary_transfer_required =
(attribute('X-BINARY-TRANSFER-REQUIRED')[0] == 'TRUE')
@human_readable = (attribute('X-NOT-HUMAN-READABLE')[0] != 'TRUE')
end
end
class Attribute < Entry
include GetTextSupport
include HumanReadable
attr_reader :super_attribute
def initialize(name, schema)
super(name, schema, "attributeTypes")
end
# read_only?
#
# Returns true if an attribute is read-only
# NO-USER-MODIFICATION
def read_only?
@read_only
end
# single_value?
#
# Returns true if an attribute can only have one
# value defined
# SINGLE-VALUE
def single_value?
@single_value
end
# binary?
#
# Returns true if the given attribute's syntax is binary syntax,
# X-NOT-HUMAN-READABLE or X-BINARY-TRANSFER-REQUIRED
def binary?
@binary
end
# Sets binary encoding to value if the given attribute's syntax
# is binary syntax. Does nothing otherwise.
# @return [void]
def apply_encoding(value)
return unless binary?
case value
when Hash
value.each_value do |sub_value|
apply_encoding(sub_value)
end
when Array
value.each do |sub_value|
apply_encoding(sub_value)
end
else
return unless value.respond_to?(:force_encoding)
value.force_encoding("ASCII-8BIT")
end
end
# binary_required?
#
# Returns true if the value MUST be transferred in binary
def binary_required?
@binary_required
end
# directory_operation?
#
# Returns true if an attribute is directory operation.
# It means that USAGE contains directoryOperation.
def directory_operation?
@directory_operation
end
def syntax
@derived_syntax
end
def valid?(value)
validate(value).nil?
end
def validate(value)
error_info = validate_each_value(value)
return error_info if error_info
begin
normalize_value(value)
nil
rescue AttributeValueInvalid
[$!.message]
end
end
def type_cast(value)
send_to_syntax(value, :type_cast, value)
end
def normalize_value(value)
normalize_value_internal(value, false)
end
def syntax_description
send_to_syntax(nil, :description)
end
def human_attribute_name
self.class.human_attribute_name(self)
end
def human_attribute_description
self.class.human_attribute_description(self)
end
def to_hash
{
:read_only => read_only?,
:single_value => single_value?,
:binary => binary?,
:binary_required => binary_required?,
:directory_operation => directory_operation?,
:syntax => syntax,
:syntax_description => syntax_description,
}
end
private
def attribute(attribute_name, name=@name)
@schema.attribute_type(name, attribute_name)
end
def collect_info
@description = attribute("DESC")[0]
@super_attribute = attribute("SUP")[0]
if @super_attribute
@super_attribute = @schema.attribute(@super_attribute)
@super_attribute = nil if @super_attribute.id.nil?
end
@read_only = attribute('NO-USER-MODIFICATION')[0] == 'TRUE'
@single_value = attribute('SINGLE-VALUE')[0] == 'TRUE'
@syntax = attribute("SYNTAX")[0]
@syntax = @schema.ldap_syntax(@syntax) if @syntax
if @syntax
@binary_required = @syntax.binary_transfer_required?
@binary = @syntax.binary?
@derived_syntax = @syntax
else
@binary_required = false
@binary = false
@derived_syntax = nil
@derived_syntax = @super_attribute.syntax if @super_attribute
end
@directory_operation = attribute("USAGE").include?("directoryOperation")
end
def send_to_syntax(default_value, method_name, *args)
_syntax = syntax
if _syntax
_syntax.send(method_name, *args)
else
default_value
end
end
def validate_each_value(value, option=nil)
failed_reason = nil
case value
when Hash
original_option = option
value.each do |sub_option, val|
opt = [original_option, sub_option].compact.join(";")
failed_reason, option = validate_each_value(val, opt)
break if failed_reason
end
when Array
original_option = option
value.each do |val|
failed_reason, option = validate_each_value(val, original_option)
break if failed_reason
end
else
failed_reason = send_to_syntax(nil, :validate, value)
end
return nil if failed_reason.nil?
[failed_reason, option]
end
def normalize_value_internal(value, have_binary_mark)
case value
when Array
normalize_array_value(value, have_binary_mark)
when Hash
normalize_hash_value(value, have_binary_mark)
else
if value.nil?
value = []
else
value = send_to_syntax(value, :normalize_value, value)
end
if !have_binary_mark and binary_required?
[{'binary' => value}]
else
value.is_a?(Array) ? value : [value]
end
end
end
def normalize_array_value(value, have_binary_mark)
if single_value? and value.reject {|v| v.is_a?(Hash)}.size > 1
format = _("Attribute %s can only have a single value: %s")
message = format % [human_attribute_name, value.inspect]
raise AttributeValueInvalid.new(self, value, message)
end
if value.empty?
if !have_binary_mark and binary_required?
[{'binary' => value}]
else
value
end
else
value.collect do |entry|
normalize_value_internal(entry, have_binary_mark)[0]
end
end
end
def normalize_hash_value(value, have_binary_mark)
if value.size > 1
format = _("Attribute %s: Hash must have one key-value pair only: %s")
message = format % [human_attribute_name, value.inspect]
raise AttributeValueInvalid.new(self, value, message)
end
if !have_binary_mark and binary_required? and !have_binary_key?(value)
[append_binary_key(value)]
else
key = value.keys[0]
have_binary_mark ||= key == "binary"
[{key => normalize_value_internal(value.values[0], have_binary_mark)}]
end
end
def have_binary_key?(hash)
key, value = hash.to_a[0]
return true if key == "binary"
return have_binary_key?(value) if value.is_a?(Hash)
false
end
def append_binary_key(hash)
key, value = hash.to_a[0]
if value.is_a?(Hash)
append_binary_key(value)
else
hash.merge(key => {"binary" => value})
end
end
end
class ObjectClass < Entry
attr_reader :super_classes
def initialize(name, schema)
super(name, schema, "objectClasses")
end
def super_class?(object_class)
@super_classes.include?(object_class)
end
def must(include_super_class=true)
if include_super_class
@all_must
else
@must
end
end
def may(include_super_class=true)
if include_super_class
@all_may
else
@may
end
end
private
def collect_info
@description = attribute("DESC")[0]
@super_classes = collect_super_classes
@must, @may, @all_must, @all_may = collect_attributes
end
def collect_super_classes
super_classes = attribute('SUP')
loop do
start_size = super_classes.size
new_super_classes = []
super_classes.each do |super_class|
new_super_classes.concat(attribute('SUP', super_class))
end
super_classes.concat(new_super_classes)
super_classes.uniq!
break if super_classes.size == start_size
end
super_classes.collect do |name|
@schema.object_class(name)
end
end
UNWRITABLE_MUST_ATTRIBUTES = ["nTSecurityDescriptor"]
def collect_attributes
must = attribute('MUST').reject do |name|
UNWRITABLE_MUST_ATTRIBUTES.include?(name)
end.uniq
must = must.collect {|name| @schema.attribute(name)}
may = attribute('MAY').uniq.collect {|name| @schema.attribute(name)}
all_must = must.dup
all_may = may.dup
@super_classes.each do |super_class|
all_must.concat(super_class.must(false))
all_may.concat(super_class.may(false))
end
# Clean out the dupes.
all_must.uniq!
all_may.uniq!
[must, may, all_must, all_may]
end
def attribute(attribute_name, name=@name)
@schema.object_class_attribute(name, attribute_name) +
@schema.dit_content_rule_attribute(name, attribute_name)
end
end
end
end
require 'active_ldap/schema/syntaxes'
|