2011-08-21 08:43:55 +04:00
|
|
|
# Redmine - project management software
|
2013-01-12 13:29:31 +04:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2007-03-12 20:59:02 +03:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-21 08:43:55 +04:00
|
|
|
#
|
2007-03-12 20:59:02 +03:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-08-21 08:43:55 +04:00
|
|
|
#
|
2007-03-12 20:59:02 +03:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
require 'iconv'
|
2012-02-19 19:04:10 +04:00
|
|
|
require 'net/ldap'
|
2012-03-17 16:09:59 +04:00
|
|
|
require 'net/ldap/dn'
|
2012-07-07 13:36:04 +04:00
|
|
|
require 'timeout'
|
2007-03-12 20:59:02 +03:00
|
|
|
|
2011-08-21 08:43:55 +04:00
|
|
|
class AuthSourceLdap < AuthSource
|
2007-03-12 20:59:02 +03:00
|
|
|
validates_presence_of :host, :port, :attr_login
|
2011-02-26 16:09:25 +03:00
|
|
|
validates_length_of :name, :host, :maximum => 60, :allow_nil => true
|
2012-03-01 20:26:10 +04:00
|
|
|
validates_length_of :account, :account_password, :base_dn, :filter, :maximum => 255, :allow_blank => true
|
2008-07-19 14:47:19 +04:00
|
|
|
validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true
|
|
|
|
validates_numericality_of :port, :only_integer => true
|
2012-07-07 13:36:04 +04:00
|
|
|
validates_numericality_of :timeout, :only_integer => true, :allow_blank => true
|
2012-03-01 20:26:10 +04:00
|
|
|
validate :validate_filter
|
2011-08-21 08:43:55 +04:00
|
|
|
|
2008-09-21 17:28:12 +04:00
|
|
|
before_validation :strip_ldap_attributes
|
2011-08-21 08:43:55 +04:00
|
|
|
|
2011-12-18 18:57:58 +04:00
|
|
|
def initialize(attributes=nil, *args)
|
|
|
|
super
|
2007-03-12 20:59:02 +03:00
|
|
|
self.port = 389 if self.port == 0
|
|
|
|
end
|
2011-08-21 08:43:55 +04:00
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
def authenticate(login, password)
|
2008-02-22 20:26:53 +03:00
|
|
|
return nil if login.blank? || password.blank?
|
2011-08-21 08:43:55 +04:00
|
|
|
|
2012-07-07 13:36:04 +04:00
|
|
|
with_timeout do
|
|
|
|
attrs = get_user_dn(login, password)
|
|
|
|
if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password)
|
|
|
|
logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
|
|
|
|
return attrs.except(:dn)
|
|
|
|
end
|
2010-02-17 19:35:35 +03:00
|
|
|
end
|
2012-07-07 13:36:04 +04:00
|
|
|
rescue Net::LDAP::LdapError => e
|
2012-02-19 22:13:46 +04:00
|
|
|
raise AuthSourceException.new(e.message)
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# test the connection to the LDAP
|
|
|
|
def test_connection
|
2012-07-07 13:36:04 +04:00
|
|
|
with_timeout do
|
|
|
|
ldap_con = initialize_ldap_con(self.account, self.account_password)
|
|
|
|
ldap_con.open { }
|
|
|
|
end
|
|
|
|
rescue Net::LDAP::LdapError => e
|
|
|
|
raise AuthSourceException.new(e.message)
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
2011-08-21 08:43:55 +04:00
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
def auth_method_name
|
|
|
|
"LDAP"
|
|
|
|
end
|
2011-08-21 08:43:55 +04:00
|
|
|
|
2012-12-26 15:23:53 +04:00
|
|
|
# Returns true if this source can be searched for users
|
|
|
|
def searchable?
|
|
|
|
!account.to_s.include?("$login") && %w(login firstname lastname mail).all? {|a| send("attr_#{a}?")}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Searches the source for users and returns an array of results
|
|
|
|
def search(q)
|
|
|
|
q = q.to_s.strip
|
|
|
|
return [] unless searchable? && q.present?
|
|
|
|
|
|
|
|
results = []
|
|
|
|
search_filter = base_filter & Net::LDAP::Filter.begins(self.attr_login, q)
|
|
|
|
ldap_con = initialize_ldap_con(self.account, self.account_password)
|
|
|
|
ldap_con.search(:base => self.base_dn,
|
|
|
|
:filter => search_filter,
|
|
|
|
:attributes => ['dn', self.attr_login, self.attr_firstname, self.attr_lastname, self.attr_mail],
|
|
|
|
:size => 10) do |entry|
|
|
|
|
attrs = get_user_attributes_from_ldap_entry(entry)
|
|
|
|
attrs[:login] = AuthSourceLdap.get_attr(entry, self.attr_login)
|
|
|
|
results << attrs
|
|
|
|
end
|
|
|
|
results
|
|
|
|
rescue Net::LDAP::LdapError => e
|
|
|
|
raise AuthSourceException.new(e.message)
|
|
|
|
end
|
|
|
|
|
2008-09-21 17:28:12 +04:00
|
|
|
private
|
2011-08-21 08:43:55 +04:00
|
|
|
|
2012-07-07 13:36:04 +04:00
|
|
|
def with_timeout(&block)
|
|
|
|
timeout = self.timeout
|
|
|
|
timeout = 20 unless timeout && timeout > 0
|
|
|
|
Timeout.timeout(timeout) do
|
|
|
|
return yield
|
|
|
|
end
|
|
|
|
rescue Timeout::Error => e
|
|
|
|
raise AuthSourceTimeoutException.new(e.message)
|
|
|
|
end
|
|
|
|
|
2012-03-01 20:26:10 +04:00
|
|
|
def ldap_filter
|
|
|
|
if filter.present?
|
|
|
|
Net::LDAP::Filter.construct(filter)
|
|
|
|
end
|
|
|
|
rescue Net::LDAP::LdapError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2012-12-26 15:23:53 +04:00
|
|
|
def base_filter
|
|
|
|
filter = Net::LDAP::Filter.eq("objectClass", "*")
|
|
|
|
if f = ldap_filter
|
|
|
|
filter = filter & f
|
|
|
|
end
|
|
|
|
filter
|
|
|
|
end
|
|
|
|
|
2012-03-01 20:26:10 +04:00
|
|
|
def validate_filter
|
|
|
|
if filter.present? && ldap_filter.nil?
|
|
|
|
errors.add(:filter, :invalid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-09-21 17:28:12 +04:00
|
|
|
def strip_ldap_attributes
|
|
|
|
[:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr|
|
|
|
|
write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil?
|
|
|
|
end
|
|
|
|
end
|
2011-08-21 08:43:55 +04:00
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
def initialize_ldap_con(ldap_user, ldap_password)
|
2008-03-05 15:43:14 +03:00
|
|
|
options = { :host => self.host,
|
|
|
|
:port => self.port,
|
|
|
|
:encryption => (self.tls ? :simple_tls : nil)
|
|
|
|
}
|
2008-03-06 20:22:21 +03:00
|
|
|
options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
|
2008-03-05 15:43:14 +03:00
|
|
|
Net::LDAP.new options
|
2007-03-12 20:59:02 +03:00
|
|
|
end
|
2010-02-16 20:03:54 +03:00
|
|
|
|
|
|
|
def get_user_attributes_from_ldap_entry(entry)
|
2010-02-26 12:13:12 +03:00
|
|
|
{
|
2010-02-19 20:00:49 +03:00
|
|
|
:dn => entry.dn,
|
2010-02-16 20:03:54 +03:00
|
|
|
:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
|
|
|
|
:lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
|
|
|
|
:mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
|
|
|
|
:auth_source_id => self.id
|
2010-02-26 12:13:12 +03:00
|
|
|
}
|
2010-02-16 20:03:54 +03:00
|
|
|
end
|
2010-02-17 19:35:35 +03:00
|
|
|
|
2010-02-19 19:33:01 +03:00
|
|
|
# Return the attributes needed for the LDAP search. It will only
|
|
|
|
# include the user attributes if on-the-fly registration is enabled
|
|
|
|
def search_attributes
|
|
|
|
if onthefly_register?
|
|
|
|
['dn', self.attr_firstname, self.attr_lastname, self.attr_mail]
|
|
|
|
else
|
|
|
|
['dn']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-17 19:35:35 +03:00
|
|
|
# Check if a DN (user record) authenticates with the password
|
|
|
|
def authenticate_dn(dn, password)
|
2010-02-18 19:55:10 +03:00
|
|
|
if dn.present? && password.present?
|
|
|
|
initialize_ldap_con(dn, password).bind
|
|
|
|
end
|
2010-02-17 19:35:35 +03:00
|
|
|
end
|
2010-02-19 20:00:49 +03:00
|
|
|
|
|
|
|
# Get the user's dn and any attributes for them, given their login
|
2012-03-17 16:09:59 +04:00
|
|
|
def get_user_dn(login, password)
|
|
|
|
ldap_con = nil
|
2012-03-17 16:19:01 +04:00
|
|
|
if self.account && self.account.include?("$login")
|
2012-03-17 16:09:59 +04:00
|
|
|
ldap_con = initialize_ldap_con(self.account.sub("$login", Net::LDAP::DN.escape(login)), password)
|
|
|
|
else
|
|
|
|
ldap_con = initialize_ldap_con(self.account, self.account_password)
|
|
|
|
end
|
2010-02-26 12:13:12 +03:00
|
|
|
attrs = {}
|
2012-12-26 15:23:53 +04:00
|
|
|
search_filter = base_filter & Net::LDAP::Filter.eq(self.attr_login, login)
|
2012-03-01 20:26:10 +04:00
|
|
|
|
2011-08-21 08:43:55 +04:00
|
|
|
ldap_con.search( :base => self.base_dn,
|
2012-03-01 20:26:10 +04:00
|
|
|
:filter => search_filter,
|
2010-02-19 20:00:49 +03:00
|
|
|
:attributes=> search_attributes) do |entry|
|
|
|
|
|
|
|
|
if onthefly_register?
|
|
|
|
attrs = get_user_attributes_from_ldap_entry(entry)
|
|
|
|
else
|
2010-02-26 12:13:12 +03:00
|
|
|
attrs = {:dn => entry.dn}
|
2010-02-19 20:00:49 +03:00
|
|
|
end
|
|
|
|
|
2010-02-26 12:13:12 +03:00
|
|
|
logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug?
|
2010-02-19 20:00:49 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
attrs
|
|
|
|
end
|
2011-08-21 08:43:55 +04:00
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
def self.get_attr(entry, attr_name)
|
2008-11-25 22:33:41 +03:00
|
|
|
if !attr_name.blank?
|
|
|
|
entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
|
|
|
|
end
|
2006-07-29 13:32:58 +04:00
|
|
|
end
|
|
|
|
end
|