Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: require 'iconv' Chris@1296: require 'net/ldap' Chris@1296: require 'net/ldap/dn' Chris@1296: require 'timeout' Chris@1296: Chris@1296: class AuthSourceLdap < AuthSource Chris@1296: validates_presence_of :host, :port, :attr_login Chris@1296: validates_length_of :name, :host, :maximum => 60, :allow_nil => true Chris@1296: validates_length_of :account, :account_password, :base_dn, :filter, :maximum => 255, :allow_blank => true Chris@1296: validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true Chris@1296: validates_numericality_of :port, :only_integer => true Chris@1296: validates_numericality_of :timeout, :only_integer => true, :allow_blank => true Chris@1296: validate :validate_filter Chris@1296: Chris@1296: before_validation :strip_ldap_attributes Chris@1296: Chris@1296: def initialize(attributes=nil, *args) Chris@1296: super Chris@1296: self.port = 389 if self.port == 0 Chris@1296: end Chris@1296: Chris@1296: def authenticate(login, password) Chris@1296: return nil if login.blank? || password.blank? Chris@1296: Chris@1296: with_timeout do Chris@1296: attrs = get_user_dn(login, password) Chris@1296: if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password) Chris@1296: logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? Chris@1296: return attrs.except(:dn) Chris@1296: end Chris@1296: end Chris@1296: rescue Net::LDAP::LdapError => e Chris@1296: raise AuthSourceException.new(e.message) Chris@1296: end Chris@1296: Chris@1296: # test the connection to the LDAP Chris@1296: def test_connection Chris@1296: with_timeout do Chris@1296: ldap_con = initialize_ldap_con(self.account, self.account_password) Chris@1296: ldap_con.open { } Chris@1296: end Chris@1296: rescue Net::LDAP::LdapError => e Chris@1296: raise AuthSourceException.new(e.message) Chris@1296: end Chris@1296: Chris@1296: def auth_method_name Chris@1296: "LDAP" Chris@1296: end Chris@1296: Chris@1296: private Chris@1296: Chris@1296: def with_timeout(&block) Chris@1296: timeout = self.timeout Chris@1296: timeout = 20 unless timeout && timeout > 0 Chris@1296: Timeout.timeout(timeout) do Chris@1296: return yield Chris@1296: end Chris@1296: rescue Timeout::Error => e Chris@1296: raise AuthSourceTimeoutException.new(e.message) Chris@1296: end Chris@1296: Chris@1296: def ldap_filter Chris@1296: if filter.present? Chris@1296: Net::LDAP::Filter.construct(filter) Chris@1296: end Chris@1296: rescue Net::LDAP::LdapError Chris@1296: nil Chris@1296: end Chris@1296: Chris@1296: def validate_filter Chris@1296: if filter.present? && ldap_filter.nil? Chris@1296: errors.add(:filter, :invalid) Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def strip_ldap_attributes Chris@1296: [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr| Chris@1296: write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil? Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def initialize_ldap_con(ldap_user, ldap_password) Chris@1296: options = { :host => self.host, Chris@1296: :port => self.port, Chris@1296: :encryption => (self.tls ? :simple_tls : nil) Chris@1296: } Chris@1296: options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank? Chris@1296: Net::LDAP.new options Chris@1296: end Chris@1296: Chris@1296: def get_user_attributes_from_ldap_entry(entry) Chris@1296: { Chris@1296: :dn => entry.dn, Chris@1296: :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), Chris@1296: :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), Chris@1296: :mail => AuthSourceLdap.get_attr(entry, self.attr_mail), Chris@1296: :auth_source_id => self.id Chris@1296: } Chris@1296: end Chris@1296: Chris@1296: # Return the attributes needed for the LDAP search. It will only Chris@1296: # include the user attributes if on-the-fly registration is enabled Chris@1296: def search_attributes Chris@1296: if onthefly_register? Chris@1296: ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] Chris@1296: else Chris@1296: ['dn'] Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: # Check if a DN (user record) authenticates with the password Chris@1296: def authenticate_dn(dn, password) Chris@1296: if dn.present? && password.present? Chris@1296: initialize_ldap_con(dn, password).bind Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: # Get the user's dn and any attributes for them, given their login Chris@1296: def get_user_dn(login, password) Chris@1296: ldap_con = nil Chris@1296: if self.account && self.account.include?("$login") Chris@1296: ldap_con = initialize_ldap_con(self.account.sub("$login", Net::LDAP::DN.escape(login)), password) Chris@1296: else Chris@1296: ldap_con = initialize_ldap_con(self.account, self.account_password) Chris@1296: end Chris@1296: login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) Chris@1296: object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) Chris@1296: attrs = {} Chris@1296: Chris@1296: search_filter = object_filter & login_filter Chris@1296: if f = ldap_filter Chris@1296: search_filter = search_filter & f Chris@1296: end Chris@1296: Chris@1296: ldap_con.search( :base => self.base_dn, Chris@1296: :filter => search_filter, Chris@1296: :attributes=> search_attributes) do |entry| Chris@1296: Chris@1296: if onthefly_register? Chris@1296: attrs = get_user_attributes_from_ldap_entry(entry) Chris@1296: else Chris@1296: attrs = {:dn => entry.dn} Chris@1296: end Chris@1296: Chris@1296: logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug? Chris@1296: end Chris@1296: Chris@1296: attrs Chris@1296: end Chris@1296: Chris@1296: def self.get_attr(entry, attr_name) Chris@1296: if !attr_name.blank? Chris@1296: entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] Chris@1296: end Chris@1296: end Chris@1296: end