Class: LDAP::Server::Router

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

Constant Summary collapse

BaseObject =

Scope

0
SingleLevel =
1
WholeSubtree =
2
NeverDerefAliases =

DerefAliases

0
DerefInSearching =
1
DerefFindingBaseObj =
2
DerefAlways =
3

Instance Method Summary collapse

Constructor Details

#initialize(logger, &block) ⇒ Router

Returns a new instance of Router.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ldap/server/router.rb', line 24

def initialize(logger, &block)
  @logger = logger

  @routes = Hash.new
  @routes = Trie.new do |trie|
    # Add an artificial LDAP component
    trie << "op=bind"
    trie << "op=search"
  end

  self.instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/ldap/server/router.rb', line 58

def method_missing(name, *args, &block)
  if [:bind, :search, :add, :modify, :modifydn, :del, :compare].include? name
    send :route, name, *args
  else
    super
  end
end

Instance Method Details

#do_bind(connection, messageId, protocolOp, controls) ⇒ Object

:nodoc:



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
# File 'lib/ldap/server/router.rb', line 89

def do_bind(connection, messageId, protocolOp, controls) # :nodoc:
  request = Request.new(connection, messageId)
  version = protocolOp.value[0].value
  dn = protocolOp.value[1].value
  dn = nil if dn.empty?
  authentication = protocolOp.value[2]

  @logger.debug "subject:#{connection.binddn} predicate:bind object:#{dn}"

  # Find a route in the routing tree
  class_name, method_name, params = parse_route(dn, :bind)

  case authentication.tag   # tag_class == :CONTEXT_SPECIFIC (check why)
  when 0
    Object.const_get(class_name).send method_name, request, version, dn, authentication.value, params
  when 3
    mechanism = authentication.value[0].value
    credentials = authentication.value[1].value
    # sasl_bind(version, dn, mechanism, credentials)
    # FIXME: needs to exchange further BindRequests
    # route_sasl_bind(request, version, dn, mechanism, credentials)
    raise LDAP::ResultError::AuthMethodNotSupported
  else
    raise LDAP::ResultError::ProtocolError, "BindRequest bad AuthenticationChoice"
  end
  request.send_BindResponse(0)
  return dn, version
rescue NoMethodError => e
  log_exception e
  request.send_BindResponse(LDAP::ResultError::OperationsError.new.to_i, :errorMessage => e.message)
  return nil, version
rescue LDAP::ResultError => e
  log_exception e
  request.send_BindResponse(e.to_i, :errorMessage => e.message)
  return nil, version
end

#do_search(connection, messageId, protocolOp, controls) ⇒ Object

:nodoc:



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
# File 'lib/ldap/server/router.rb', line 126

def do_search(connection, messageId, protocolOp, controls) # :nodoc:
  request = Request.new(connection, messageId)
  server = connection.opt[:server]
  schema = connection.opt[:schema]
  baseObject = protocolOp.value[0].value
  scope = protocolOp.value[1].value
  deref = protocolOp.value[2].value
  client_sizelimit = protocolOp.value[3].value
  client_timelimit = protocolOp.value[4].value.to_i
  request.typesOnly = protocolOp.value[5].value
  filter = LDAP::Server::Filter::parse(protocolOp.value[6], schema)
  request.attributes = protocolOp.value[7].value.collect {|x| x.value}

  sizelimit = request.server_sizelimit
  sizelimit = client_sizelimit if client_sizelimit > 0 and
               (sizelimit.nil? or client_sizelimit < sizelimit)
  request.sizelimit = sizelimit

  if baseObject.empty? and scope == BaseObject
    request.send_SearchResultEntry("", server.root_dse) if
      server.root_dse and LDAP::Server::Filter.run(filter, server.root_dse)
    request.send_SearchResultDone(0)
    return
  elsif schema and baseObject == schema.subschema_dn
    request.send_SearchResultEntry(baseObject, schema.subschema_subentry) if
      schema and schema.subschema_subentry and
      LDAP::Server::Filter.run(filter, schema.subschema_subentry)
    request.send_SearchResultDone(0)
    return
  end

  t = request.server_timelimit || 10
  t = client_timelimit if client_timelimit > 0 and client_timelimit < t

  @logger.debug "subject:#{connection.binddn} predicate:search object:#{baseObject}"

  # Find a route in the routing tree
  class_name, method_name, params = parse_route(baseObject, :search)

  Timeout::timeout(t, LDAP::ResultError::TimeLimitExceeded) do
    Object.const_get(class_name).send method_name, request, baseObject, scope, deref, filter, params
  end
  request.send_SearchResultDone(0)

# Note that TimeLimitExceeded is a subclass of LDAP::ResultError
rescue LDAP::ResultError => e
  request.send_SearchResultDone(e.to_i, :errorMessage=>e.message)

rescue Abandon
  # send no response

# Since this Operation is running in its own thread, we have to
# catch all other exceptions. Otherwise, in the event of a programming
# error, this thread will silently terminate and the client will wait
# forever for a response.

rescue Exception => e
  log_exception e
  request.send_SearchResultDone(LDAP::ResultError::OperationsError.new.to_i, :errorMessage=>e.message)
end

#log_exception(e, level = :error) ⇒ Object



37
38
39
40
# File 'lib/ldap/server/router.rb', line 37

def log_exception(e, level = :error)
  @logger.send level, e.message
  e.backtrace.each { |line| @logger.send level, "\tfrom#{line}" } if e.backtrace
end

#parse_route(dn, method) ⇒ Object

Methods to parse and route each request type ###



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ldap/server/router.rb', line 70

def parse_route(dn, method)
  route, action = @routes.match("#{dn},op=#{method.to_s}")
  if not route or route.empty?
    @logger.warn "No route defined for \'#{route}\'"
    raise LDAP::ResultError::UnwillingToPerform
  end
  if action.nil?
    @logger.error "No action defined for route \'#{route}\'"
    raise LDAP::ResultError::UnwillingToPerform
  end

  class_name = action.split('#').first
  method_name = action.split('#').last

  params = LDAP::Server::DN.new("#{dn},op=#{method.to_s}").parse(route)

  return class_name, method_name, params
end

#route(operation, hash) ⇒ Object

Initialization ###



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ldap/server/router.rb', line 46

def route(operation, hash)
  hash.each do |key, value|
    if key.nil?
      @routes.insert "op=#{operation.to_s}", value
      @logger.debug "map operation #{operation.to_s} all routes to #{value}"
    else
      @routes.insert "#{key},op=#{operation.to_s}", value
      @logger.debug "map #{operation.to_s} #{key} to #{value}"
    end
  end
end