Module: LDAP

Defined in:
lib/ldap/dn.rb,
lib/ldap/escape.rb,
lib/ldap/filter.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Filter Classes: DN, RDN

Class Method Summary (collapse)

Class Method Details

+ (Object) DN(dn, source = nil)

Instantiate a new LDAP::DN object with the arguments given. Unlike LDAP::DN.new(dn), this method coerces the first argument to a string, unless it is already a string or an array. If the first argument is nil, nil is returned.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ldap/dn.rb', line 9

def self.DN(dn, source = nil)
  return nil if dn.nil?
  dn = dn.dn if dn.respond_to?(:dn)
  if dn.kind_of?(::LDAP::DN)
    if source
      dn = dn.dup
      dn.source = source
    end
    return dn
  end
  if dn.respond_to?(:to_hash)
    dn = [dn]
  elsif ! dn.respond_to?(:to_ary)
    dn = dn.to_s
  end
  DN.new(dn,source)
end

+ (Object) encode(string)

Encode an object with LDAP semantics. Generally this is just to_s, but dates and booleans get special treatment.

If a symbol is passed in, underscores are replaced by dashes, aiding in bridging the gap between LDAP and Ruby conventions.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ldap/escape.rb', line 8

def self.encode(string)
  if string.respond_to?(:utc)
    string.utc.strftime("%Y%m%d%H%M%S.0Z")
  elsif [true,false].include?(string)
    string.to_s.upcase
  elsif string.kind_of?(Symbol)
    string.to_s.gsub('_','-')
  elsif string.respond_to?(:dn)
    string.dn.dup
  else
    string.to_s.dup
  end
end

+ (Object) escape(string, allow_asterisks = false)

Escape a string for use in an LDAP filter, or in a DN. If the second argument is true, asterisks are not escaped.

If the first argument is not a string, it is handed off to LDAP::encode.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ldap/escape.rb', line 26

def self.escape(string, allow_asterisks = false)
  if string.kind_of?(Symbol) && allow_asterisks
    warn "deprecated call to escape with Symbol and allow_asterisks"
    string = LDAP.encode(string).upcase
  else
    string = LDAP.encode(string)
  end
  enc = lambda {|l| "\\%02X" % l.ord }
  string.gsub!(/[()\\\0-\37"+,;<>]/,&enc)
  string.gsub!(/\A[# ]| \Z/,&enc)
  if allow_asterisks
    string.gsub!('**','\\\\2A')
  else
    string.gsub!('*','\\\\2A')
  end
  string
end

+ (Object) Filter(argument)

If the argument is already a valid LDAP::Filter object, return it untouched. Otherwise, pass it to the appropriate constructer of the appropriate subclass.

Ldapter::Filter("(cn=Wu*)").to_s        #=> '(cn=Wu*)'
Ldapter::Filter({:cn=>"Wu*"}).to_s      #=> '(cn=Wu\2A)'
Ldapter::Filter(["(cn=?*)","Wu*"]).to_s #=> '(cn=Wu\2A*)'


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ldap/filter.rb', line 12

def self.Filter(argument)
  case argument
  when Filter::Abstract then argument
  when [],nil then nil
  when Array  then Filter::Array    .new(argument)
  when Hash   then Filter::Hash     .new(argument)
  when String then Filter::String   .new(argument)
  when Symbol then Filter::Attribute.new(argument)
  when Proc, Method
    LDAP::Filter(if argument.arity > 0
      argument.call(Filter::Spawner)
    elsif Filter::Spawner.respond_to?(:instance_exec)
      Filter::Spawner.instance_exec(&argument)
    else
      Filter::Spawner.instance_eval(&argument)
    end)
  else raise TypeError, "Unknown LDAP Filter type", caller
  end
end

+ (Object) RDN(rdn)



193
194
195
196
197
198
199
200
# File 'lib/ldap/dn.rb', line 193

def self.RDN(rdn)
  rdn = rdn.rdn if rdn.respond_to?(:rdn)
  if rdn.respond_to?(:to_rdn)
    rdn.to_rdn
  else
    RDN.new(rdn||{})
  end
end

+ (Object) split(string, character)

Split on a given character where it is not escaped. Either an integer or string represenation of the character may be used.

LDAP.split("a*b",'*')    # => ["a","b"]
LDAP.split("a\\*b",'*')  # => ["a\\*b"]
LDAP.split("a\\\\*b",?*) # => ["a\\\\","b"]


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ldap/escape.rb', line 85

def self.split(string, character)
  return [] if string.empty?
  array = [""]
  character = character.to_str.ord if character.respond_to?(:to_str)
  backslash = false

  string.each_byte do |byte|
    if backslash
      array.last << byte
      backslash = false
    elsif byte == 92 # ?\\
      array.last << byte
      backslash = true
    elsif byte == character
      array << ""
    else
      array.last << byte
    end
  end
  array
end

+ (Object) unescape(string)



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
# File 'lib/ldap/escape.rb', line 44

def self.unescape(string)
  dest = ""
  string = string.strip # Leading and trailing whitespace MUST be encoded
  if string[0,1] == "#"
    [string[1..-1]].pack("H*")
  else
    backslash = nil
    string.each_byte do |byte|
      case backslash
      when true
        char = byte.chr
        if ('0'..'9').include?(char) || ('a'..'f').include?(char.downcase)
          backslash = char
        else
          dest << byte
          backslash = nil
        end

      when String
        dest << (backslash << byte).to_i(16)
        backslash = nil

      else
        backslash = nil
        if byte == 92 # ?\\
          backslash = true
        else
          dest << byte
        end
      end
    end
    dest
  end
end