Chris@909
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 require 'net/ldap'
|
Chris@1115
|
19 require 'net/ldap/dn'
|
Chris@1115
|
20 require 'timeout'
|
Chris@0
|
21
|
Chris@909
|
22 class AuthSourceLdap < AuthSource
|
Chris@0
|
23 validates_presence_of :host, :port, :attr_login
|
Chris@245
|
24 validates_length_of :name, :host, :maximum => 60, :allow_nil => true
|
Chris@1115
|
25 validates_length_of :account, :account_password, :base_dn, :filter, :maximum => 255, :allow_blank => true
|
Chris@0
|
26 validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true
|
Chris@0
|
27 validates_numericality_of :port, :only_integer => true
|
Chris@1115
|
28 validates_numericality_of :timeout, :only_integer => true, :allow_blank => true
|
Chris@1115
|
29 validate :validate_filter
|
Chris@909
|
30
|
Chris@0
|
31 before_validation :strip_ldap_attributes
|
Chris@909
|
32
|
Chris@1115
|
33 def initialize(attributes=nil, *args)
|
Chris@1115
|
34 super
|
Chris@0
|
35 self.port = 389 if self.port == 0
|
Chris@0
|
36 end
|
Chris@909
|
37
|
Chris@0
|
38 def authenticate(login, password)
|
Chris@0
|
39 return nil if login.blank? || password.blank?
|
Chris@909
|
40
|
Chris@1115
|
41 with_timeout do
|
Chris@1115
|
42 attrs = get_user_dn(login, password)
|
Chris@1115
|
43 if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password)
|
Chris@1115
|
44 logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
|
Chris@1115
|
45 return attrs.except(:dn)
|
Chris@1115
|
46 end
|
Chris@0
|
47 end
|
Chris@1115
|
48 rescue Net::LDAP::LdapError => e
|
Chris@1115
|
49 raise AuthSourceException.new(e.message)
|
Chris@0
|
50 end
|
Chris@0
|
51
|
Chris@0
|
52 # test the connection to the LDAP
|
Chris@0
|
53 def test_connection
|
Chris@1115
|
54 with_timeout do
|
Chris@1115
|
55 ldap_con = initialize_ldap_con(self.account, self.account_password)
|
Chris@1115
|
56 ldap_con.open { }
|
Chris@1115
|
57 end
|
Chris@1115
|
58 rescue Net::LDAP::LdapError => e
|
Chris@1115
|
59 raise AuthSourceException.new(e.message)
|
Chris@0
|
60 end
|
Chris@909
|
61
|
Chris@0
|
62 def auth_method_name
|
Chris@0
|
63 "LDAP"
|
Chris@0
|
64 end
|
Chris@909
|
65
|
Chris@1464
|
66 # Returns true if this source can be searched for users
|
Chris@1464
|
67 def searchable?
|
Chris@1464
|
68 !account.to_s.include?("$login") && %w(login firstname lastname mail).all? {|a| send("attr_#{a}?")}
|
Chris@1464
|
69 end
|
Chris@1464
|
70
|
Chris@1464
|
71 # Searches the source for users and returns an array of results
|
Chris@1464
|
72 def search(q)
|
Chris@1464
|
73 q = q.to_s.strip
|
Chris@1464
|
74 return [] unless searchable? && q.present?
|
Chris@1464
|
75
|
Chris@1464
|
76 results = []
|
Chris@1464
|
77 search_filter = base_filter & Net::LDAP::Filter.begins(self.attr_login, q)
|
Chris@1464
|
78 ldap_con = initialize_ldap_con(self.account, self.account_password)
|
Chris@1464
|
79 ldap_con.search(:base => self.base_dn,
|
Chris@1464
|
80 :filter => search_filter,
|
Chris@1464
|
81 :attributes => ['dn', self.attr_login, self.attr_firstname, self.attr_lastname, self.attr_mail],
|
Chris@1464
|
82 :size => 10) do |entry|
|
Chris@1464
|
83 attrs = get_user_attributes_from_ldap_entry(entry)
|
Chris@1464
|
84 attrs[:login] = AuthSourceLdap.get_attr(entry, self.attr_login)
|
Chris@1464
|
85 results << attrs
|
Chris@1464
|
86 end
|
Chris@1464
|
87 results
|
Chris@1464
|
88 rescue Net::LDAP::LdapError => e
|
Chris@1464
|
89 raise AuthSourceException.new(e.message)
|
Chris@1464
|
90 end
|
Chris@1464
|
91
|
Chris@0
|
92 private
|
Chris@909
|
93
|
Chris@1115
|
94 def with_timeout(&block)
|
Chris@1115
|
95 timeout = self.timeout
|
Chris@1115
|
96 timeout = 20 unless timeout && timeout > 0
|
Chris@1115
|
97 Timeout.timeout(timeout) do
|
Chris@1115
|
98 return yield
|
Chris@1115
|
99 end
|
Chris@1115
|
100 rescue Timeout::Error => e
|
Chris@1115
|
101 raise AuthSourceTimeoutException.new(e.message)
|
Chris@1115
|
102 end
|
Chris@1115
|
103
|
Chris@1115
|
104 def ldap_filter
|
Chris@1115
|
105 if filter.present?
|
Chris@1115
|
106 Net::LDAP::Filter.construct(filter)
|
Chris@1115
|
107 end
|
Chris@1115
|
108 rescue Net::LDAP::LdapError
|
Chris@1115
|
109 nil
|
Chris@1115
|
110 end
|
Chris@1115
|
111
|
Chris@1464
|
112 def base_filter
|
Chris@1464
|
113 filter = Net::LDAP::Filter.eq("objectClass", "*")
|
Chris@1464
|
114 if f = ldap_filter
|
Chris@1464
|
115 filter = filter & f
|
Chris@1464
|
116 end
|
Chris@1464
|
117 filter
|
Chris@1464
|
118 end
|
Chris@1464
|
119
|
Chris@1115
|
120 def validate_filter
|
Chris@1115
|
121 if filter.present? && ldap_filter.nil?
|
Chris@1115
|
122 errors.add(:filter, :invalid)
|
Chris@1115
|
123 end
|
Chris@1115
|
124 end
|
Chris@1115
|
125
|
Chris@0
|
126 def strip_ldap_attributes
|
Chris@0
|
127 [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr|
|
Chris@0
|
128 write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil?
|
Chris@0
|
129 end
|
Chris@0
|
130 end
|
Chris@909
|
131
|
Chris@0
|
132 def initialize_ldap_con(ldap_user, ldap_password)
|
Chris@0
|
133 options = { :host => self.host,
|
Chris@0
|
134 :port => self.port,
|
Chris@0
|
135 :encryption => (self.tls ? :simple_tls : nil)
|
Chris@0
|
136 }
|
Chris@0
|
137 options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
|
Chris@0
|
138 Net::LDAP.new options
|
Chris@0
|
139 end
|
Chris@0
|
140
|
Chris@0
|
141 def get_user_attributes_from_ldap_entry(entry)
|
Chris@0
|
142 {
|
Chris@0
|
143 :dn => entry.dn,
|
Chris@0
|
144 :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
|
Chris@0
|
145 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
|
Chris@0
|
146 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
|
Chris@0
|
147 :auth_source_id => self.id
|
Chris@0
|
148 }
|
Chris@0
|
149 end
|
Chris@0
|
150
|
Chris@0
|
151 # Return the attributes needed for the LDAP search. It will only
|
Chris@0
|
152 # include the user attributes if on-the-fly registration is enabled
|
Chris@0
|
153 def search_attributes
|
Chris@0
|
154 if onthefly_register?
|
Chris@0
|
155 ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail]
|
Chris@0
|
156 else
|
Chris@0
|
157 ['dn']
|
Chris@0
|
158 end
|
Chris@0
|
159 end
|
Chris@0
|
160
|
Chris@0
|
161 # Check if a DN (user record) authenticates with the password
|
Chris@0
|
162 def authenticate_dn(dn, password)
|
Chris@0
|
163 if dn.present? && password.present?
|
Chris@0
|
164 initialize_ldap_con(dn, password).bind
|
Chris@0
|
165 end
|
Chris@0
|
166 end
|
Chris@0
|
167
|
Chris@0
|
168 # Get the user's dn and any attributes for them, given their login
|
Chris@1115
|
169 def get_user_dn(login, password)
|
Chris@1115
|
170 ldap_con = nil
|
Chris@1115
|
171 if self.account && self.account.include?("$login")
|
Chris@1115
|
172 ldap_con = initialize_ldap_con(self.account.sub("$login", Net::LDAP::DN.escape(login)), password)
|
Chris@1115
|
173 else
|
Chris@1115
|
174 ldap_con = initialize_ldap_con(self.account, self.account_password)
|
Chris@1115
|
175 end
|
Chris@0
|
176 attrs = {}
|
Chris@1464
|
177 search_filter = base_filter & Net::LDAP::Filter.eq(self.attr_login, login)
|
Chris@1115
|
178
|
Chris@909
|
179 ldap_con.search( :base => self.base_dn,
|
Chris@1115
|
180 :filter => search_filter,
|
Chris@0
|
181 :attributes=> search_attributes) do |entry|
|
Chris@0
|
182
|
Chris@0
|
183 if onthefly_register?
|
Chris@0
|
184 attrs = get_user_attributes_from_ldap_entry(entry)
|
Chris@0
|
185 else
|
Chris@0
|
186 attrs = {:dn => entry.dn}
|
Chris@0
|
187 end
|
Chris@0
|
188
|
Chris@0
|
189 logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug?
|
Chris@0
|
190 end
|
Chris@0
|
191
|
Chris@0
|
192 attrs
|
Chris@0
|
193 end
|
Chris@909
|
194
|
Chris@0
|
195 def self.get_attr(entry, attr_name)
|
Chris@0
|
196 if !attr_name.blank?
|
Chris@0
|
197 entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
|
Chris@0
|
198 end
|
Chris@0
|
199 end
|
Chris@0
|
200 end
|