Class: LDAP::Server::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/ldap/server/syntax.rb,
lib/ldap/server/syntax.rb

Overview

A class which describes LDAP SyntaxDescriptions. For now there is a global pool of Syntax objects (rather than each Schema object having its own set)

Constant Summary collapse

KEYSTR =

Shared constants for regexp-based syntax parsers

"[a-zA-Z][a-zA-Z0-9;-]*"
NUMERICOID =
"( \\d[\\d.]+\\d )"
WOID =
"\\s* ( #{KEYSTR} | \\d[\\d.]+\\d ) \\s*"
OIDS =
"( #{_WOID} | \\s* \\( #{_WOID} (?: \\$ #{_WOID} )* \\) \\s* )"
QDESCRS =
"( #{_QDESCR} | \\s* \\( (?:#{_QDESCR})+ \\) \\s* )"
QDSTRING =
"\\s* ' (.*?) ' \\s*"
NOIDLEN =
"(\\d[\\d.]+\\d) (?: \\{ (\\d+) \\} )?"
ATTRIBUTEUSAGE =
"(userApplications|directoryOperation|distributedOperation|dSAOperation)"
AttributeTypeDescription =

These are the ‘SHOULD’ support syntaxes from RFC2252 section 6

add("1.3.6.1.4.1.1466.115.121.1.3", "Attribute Type Description", :re=>
%r! \A \s* \( \s*
 #{NUMERICOID} \s*
	(?: NAME #{QDESCRS} )?
	(?: DESC #{QDSTRING} )?
	(   OBSOLETE \s* )?
	(?: SUP #{WOID} )?
	(?: EQUALITY #{WOID} )?
	(?: ORDERING #{WOID} )?
	(?: SUBSTR #{WOID} )?
	(?: SYNTAX \s* #{NOIDLEN} \s* )?	# capture 2
	(   SINGLE-VALUE \s* )?
	(   COLLECTIVE \s* )?
	(   NO-USER-MODIFICATION \s* )?
	(?: USAGE \s* #{ATTRIBUTEUSAGE} )?
\s* \) \s* \z !xu)
MatchingRuleDescription =
add("1.3.6.1.4.1.1466.115.121.1.30", "Matching Rule Description", :re=>
%r! \A \s* \( \s*
 #{NUMERICOID} \s*
	(?: NAME #{QDESCRS} )?
	(?: DESC #{QDSTRING} )?
	(   OBSOLETE \s* )?
 SYNTAX \s* #{NUMERICOID} \s*
\s* \) \s* \z !xu)
MatchingRuleUseDescription =
add("1.3.6.1.4.1.1466.115.121.1.31", "Matching Rule Use Description", :re=>
%r! \A \s* \( \s*
 #{NUMERICOID} \s*
	(?: NAME #{QDESCRS} )?
	(?: DESC #{QDSTRING} )?
	(   OBSOLETE \s* )?
 APPLIES \s* #{OIDS} \s*
\s* \) \s* \z !xu)
ObjectClassDescription =
add("1.3.6.1.4.1.1466.115.121.1.37", "Object Class Description", :re=>
%r! \A \s* \( \s*
	#{NUMERICOID} \s*
	(?: NAME #{QDESCRS} )?
	(?: DESC #{QDSTRING} )?
	(   OBSOLETE \s* )?
	(?: SUP #{OIDS} )?
	(?: ( ABSTRACT|STRUCTURAL|AUXILIARY ) \s* )?
	(?: MUST #{OIDS} )?
	(?: MAY #{OIDS} )?
\s* \) \s* \z !xu)
LDAPSyntaxDescription =
add("1.3.6.1.4.1.1466.115.121.1.54", "LDAP Syntax Description", :re=>
%r! \A \s* \( \s*
 #{NUMERICOID} \s*
	(?: DESC #{QDSTRING} )?
	(?: X-BINARY-TRANSFER-REQUIRED \s* ' (TRUE|FALSE) ' \s* )?
	(?: X-NOT-HUMAN-READABLE \s* ' (TRUE|FALSE) ' \s* )?
\s* \) \s* \z !xu)
@@syntaxes =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oid, desc = nil, opt = {}, &blk) ⇒ Syntax

Create a new Syntax object



13
14
15
16
17
18
19
20
21
# File 'lib/ldap/server/syntax.rb', line 13

def initialize(oid, desc=nil, opt={}, &blk)
  @oid = oid
  @desc = desc
  @nhr = opt[:nhr]		# not human-readable?
  @binary = opt[:binary]	# binary encoding forced?
  @re = opt[:re]		# regular expression for parsing
  @def = nil
  instance_eval(&blk) if blk
end

Instance Attribute Details

#binaryObject (readonly)

Returns the value of attribute binary.



9
10
11
# File 'lib/ldap/server/syntax.rb', line 9

def binary
  @binary
end

#descObject (readonly)

Returns the value of attribute desc.



9
10
11
# File 'lib/ldap/server/syntax.rb', line 9

def desc
  @desc
end

#nhrObject (readonly)

Returns the value of attribute nhr.



9
10
11
# File 'lib/ldap/server/syntax.rb', line 9

def nhr
  @nhr
end

#oidObject (readonly)

Returns the value of attribute oid.



9
10
11
# File 'lib/ldap/server/syntax.rb', line 9

def oid
  @oid
end

Class Method Details

.add(*args, &blk) ⇒ Object

Add a new syntax definition



75
76
77
78
# File 'lib/ldap/server/syntax.rb', line 75

def self.add(*args, &blk)
  s = new(*args, &blk)
  @@syntaxes[s.oid] = s
end

.all_syntaxesObject

Return all known syntax objects



91
92
93
# File 'lib/ldap/server/syntax.rb', line 91

def self.all_syntaxes
  @@syntaxes.values.uniq
end

.find(oid) ⇒ Object

Find a Syntax object given an oid. If not known, return a new empty Syntax object associated with this oid.



83
84
85
86
87
# File 'lib/ldap/server/syntax.rb', line 83

def self.find(oid)
  return oid if oid.nil? or oid.is_a?(LDAP::Server::Syntax)
  return @@syntaxes[oid] if @@syntaxes[oid]
  add(oid)
end

.from_def(str, &blk) ⇒ Object

Create a new Syntax object, given its description string



29
30
31
32
33
34
# File 'lib/ldap/server/syntax.rb', line 29

def self.from_def(str, &blk)
  m = LDAPSyntaxDescription.match(str)
  raise LDAP::ResultError::InvalidAttributeSyntax,
    "Bad SyntaxTypeDescription #{str.inspect}" unless m
  new(m[1], m[2], :nhr=>(m[3] == 'TRUE'), :binary=>(m[4] == 'TRUE'), &blk)
end

Instance Method Details

#match(val) ⇒ Object

Return true or a MatchData object if the given value is allowed by this syntax



52
53
54
55
# File 'lib/ldap/server/syntax.rb', line 52

def match(val)
  return true if @re.nil?
  @re.match(value_to_s(val))
end

#to_defObject

Convert this object to its description string



38
39
40
41
42
43
44
45
46
47
# File 'lib/ldap/server/syntax.rb', line 38

def to_def
  return @def if @def
  ans = "( #@oid "
  ans << "DESC '#@desc' " if @desc
  # These are OpenLDAP extensions
  ans << "X-BINARY-TRANSFER-REQUIRED 'TRUE' " if @binary
  ans << "X-NOT-HUMAN-READABLE 'TRUE' " if @nhr
  ans << ")"
  @def = ans
end

#to_sObject



23
24
25
# File 'lib/ldap/server/syntax.rb', line 23

def to_s
  @oid
end

#value_from_s(val) ⇒ Object

Convert a string value for this syntax into a Ruby-like value (not yet used, but seemed like a good idea)



67
68
69
# File 'lib/ldap/server/syntax.rb', line 67

def value_from_s(val)
  val
end

#value_to_s(val) ⇒ Object

Convert a value for this syntax into its canonical string representation (not yet used, but seemed like a good idea)



60
61
62
# File 'lib/ldap/server/syntax.rb', line 60

def value_to_s(val)
  val.to_s
end