Class: ActiveRecordOperation

Inherits:
LDAP::Server::Operation
  • Object
show all
Defined in:
lib/arldap/active_record_operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, messageID, config, ar_class, logger) ⇒ ActiveRecordOperation

Returns a new instance of ActiveRecordOperation.



7
8
9
10
11
12
# File 'lib/arldap/active_record_operation.rb', line 7

def initialize(connection, messageID, config, ar_class, logger)
  @config, @ar_class, @logger = config, ar_class, logger
  @logger.debug "Received connection request (#{messageID})."
  @connection = connection
  super(connection, messageID)
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



5
6
7
# File 'lib/arldap/active_record_operation.rb', line 5

def attributes
  @attributes
end

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/arldap/active_record_operation.rb', line 5

def connection
  @connection
end

#schemaObject

Returns the value of attribute schema.



5
6
7
# File 'lib/arldap/active_record_operation.rb', line 5

def schema
  @schema
end

#serverObject

Returns the value of attribute server.



5
6
7
# File 'lib/arldap/active_record_operation.rb', line 5

def server
  @server
end

Instance Method Details

#parse_filter(filter) ⇒ Object



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
# File 'lib/arldap/active_record_operation.rb', line 80

def parse_filter(filter)
  # format of mozilla and OS X address book searches are always this: 
  # [:or, [:substrings, "mail",      nil, nil, "XXX", nil], 
  #       [:substrings, "cn",        nil, nil, "XXX", nil], 
  #       [:substrings, "givenName", nil, nil, "XXX", nil], 
  #       [:substrings, "sn",        nil, nil, "XXX", nil]]
  # (with the order of the subgroups maybe turned around)
  ##
  # [:or, [:substrings, "givenname", nil, "j", nil], 
  # [:substrings, "sn", nil, "j", nil], 
  # [:substrings, "mail", nil, "j", nil], 
  # [:substrings, "cn", nil, "j", nil]]

  @logger.info filter

  unless filter
    @logger.info "Denying complex query (error 1): #{filter.inspect}"
    raise LDAP::ResultError::UnwillingToPerform, "This query is way too complex: #{filter.inspect}"
  end

  if filter[0] != :or
    @logger.info "Denying complex query (error 1): #{filter.inspect}"
    raise LDAP::ResultError::UnwillingToPerform, "This query is way too complex: #{filter.inspect}"
  end

  query = filter[1]

  if !(query.length > 3)
    @logger.info "Denying complex query (error 2): #{filter.inspect}"
    raise LDAP::ResultError::UnwillingToPerform, "This query is way too complex: #{filter.inspect}"      
  end
  
  if !query[1]
    @logger.info "Refusing to respond to blank query string: #{filter.inspect}."
    raise LDAP::ResultError::UnwillingToPerform, "Refusing to respond to blank query string: #{filter.inspect}"
  end

  if !(%w{mail cn givenname sn}.include? query[1].downcase)
    @logger.info "Denying complex query (error 3): #{filter.inspect}"
    raise LDAP::ResultError::UnwillingToPerform, "This query is way too complex: #{filter.inspect}"      
  end
  
  query_string = query[2..-1].compact.first # We're just going to take the first non-nil element as the search string
  
  if !query_string
    @logger.info "Refusing to respond to blank query string: #{filter.inspect}."
    raise LDAP::ResultError::UnwillingToPerform, "Refusing to respond to blank query string: #{filter.inspect}"
  end
  
  query_string
end

#search(basedn, scope, deref, filter) ⇒ Object



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
# File 'lib/arldap/active_record_operation.rb', line 32

def search(basedn, scope, deref, filter)
  if @connection.opt[:authentic]
    @logger.info "Valid credentials: #{@connection.opt[:authentic]}"
  else
    @logger.info "Invalid credentials"
    raise LDAP::ResultError::InvalidCredentials, "Invalid credentials: #{@connection.opt[:authentic]}"
  end

  # This is needed to force the ruby ldap server to return our parameters, 
  # even though the client didn't explicitly ask for them
  @attributes << "*"
  if basedn != @config[:basedn]
    @logger.info "Denying request with missmatched basedn (wanted \"#{@config[:basedn]}\", but got \"#{basedn}\")"
    raise LDAP::ResultError::UnwillingToPerform, "Bad base DN"
  end

  if scope == LDAP::Server::BaseObject
    @logger.info "Denying request for BaseObject: #{filter.inspect}"
    raise LDAP::ResultError::UnwillingToPerform, "BaseObject not implemented: #{filter}"
  end

  query_string = parse_filter(filter)
  
  @logger.debug "Running #{@ar_class.name}.ldap_search(\"#{query_string}\")"
  
  begin
    @records = @ar_class.ldap_search(query_string)
  rescue Exception => e
    @logger.error "ERROR running #{@ar_class.name}.ldap_search(#{query_string}): #{e}"
    # raise LDAP::ResultError::OperationsError, "Error encountered during processing: #{e}."
  end 

  @logger.info "Returning #{@records.size} records matching \"#{query_string}\"."
  
  @records.each do |record| 
    begin
      ret = record.to_ldap_entry
    rescue
      @logger.error "ERROR converting AR instance to ldap entry: #{$!}"
      raise LDAP::ResultError::OperationsError, "Error encountered during processing."
    end
    
    ret_basedn = "uid=#{ret["uid"]},#{@config[:basedn]}"
    @logger.debug "Sending #{ret_basedn} - #{ret.inspect}" 
    send_SearchResultEntry(ret_basedn, ret)
  end
end

#simple_bind(version, dn, password) ⇒ Object



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

def simple_bind(version, dn, password)
  @logger.debug "config password"
  @logger.debug @config[:password]
  @logger.debug "config password class"
  @logger.debug @config[:password].class
  @logger.debug "passed password"
  @logger.debug password
  @logger.debug "passed password class"
  @logger.debug password.class
  
  @connection.opt[:authentic] = password == @config[:password]
  if @connection.opt[:authentic]
    @logger.info  "simple_bind authentic: #{@connection.opt[:authentic]}"
  else
    @logger.info  "simple_bind not authentic: #{@connection.opt[:authentic]}"
  end
end